From c42697dd789156bc11d6f350633e214fa7861b3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Ferry?= Date: Sat, 15 Mar 2014 22:31:11 +0100 Subject: [PATCH] New : can add new resource in database --- htdocs/resource/add.php | 167 ++++++++++++++++++ .../resource/class/actions_resource.class.php | 17 +- htdocs/resource/class/resource.class.php | 92 +++++++++- 3 files changed, 261 insertions(+), 15 deletions(-) create mode 100755 htdocs/resource/add.php diff --git a/htdocs/resource/add.php b/htdocs/resource/add.php new file mode 100755 index 00000000000..e42edda4c66 --- /dev/null +++ b/htdocs/resource/add.php @@ -0,0 +1,167 @@ + + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +/** + * \file resource/add.php + * \ingroup resource + * \brief Page to manage resource object + * Initialy built by build_class_from_table on 2013-07-24 16:03 + */ + + +// Change this following line to use the correct relative path (../, ../../, etc) +$res=0; +$res=@include("../main.inc.php"); // For root directory +if (! $res) $res=@include("../../main.inc.php"); // For "custom" directory +if (! $res) die("Include of main fails"); + +require_once 'class/resource.class.php'; + +// Load traductions files requiredby by page +$langs->load("resource"); +$langs->load("companies"); +$langs->load("other"); + +// Get parameters +$id = GETPOST('id','int'); +$action = GETPOST('action','alpha'); +if (empty($sortorder)) $sortorder="DESC"; +if (empty($sortfield)) $sortfield="t.rowid"; +if (empty($arch)) $arch = 0; + +if ($page == -1) { + $page = 0 ; +} + +$limit = $conf->global->limit; +$offset = $limit * $page ; +$pageprev = $page - 1; +$pagenext = $page + 1; + + +// Protection if external user +if ($user->societe_id > 0) +{ + accessforbidden(); +} + +$object = new Resource($db); + +if ($action == 'confirm_add_resource') +{ + $error=''; + + $ref=GETPOST('ref','alpha'); + $description=GETPOST('description','alpha'); + $fk_code_type_resource=GETPOST('fk_code_type_resource','alpha'); + + if (empty($ref)) + { + $mesg=$langs->trans("ErrorFieldRequired",$langs->transnoentities("Ref")); + setEventMessage($mesg, 'errors'); + $error++; + } + + if (! $error) + { + $object=new Resource($db); + $object->ref=$ref; + $object->description=$description; + $object->fk_code_type_resource=$fk_code_type_resource; + + $result=$object->create($user); + if ($result > 0) + { + // Creation OK + $db->commit(); + setEventMessage($langs->trans('ResourceCreatedWithSuccess')); + Header("Location: card.php?id=" . $object->id); + return; + } + else + { + // Creation KO + setEventMessage($object->error, 'errors'); + $action = ''; + } + } + else + { + $action = ''; + } +} + +/*************************************************** +* VIEW +* +* Put here all code to build page +****************************************************/ + +$form=new Form($db); + +if ( !$action ) +{ + $pagetitle=$langs->trans('AddResource'); + llxHeader('',$pagetitle,''); + print_fiche_titre($pagetitle,'','resource_32.png'); + + print '
'; + print ''; + + print ''; + + // Ref / label + $field = 'ref'; + print ''; + print ''; + print ''; + print ''; + + // Description + $field = 'description'; + print ''; + print ''; + print ''; + print ''; + + print '
'; + print ''; + print ''; + print '
'; + print ''; + print ''; + require_once (DOL_DOCUMENT_ROOT . "/core/class/doleditor.class.php"); + $doleditor = new DolEditor($field, $$field, 160, '', '', false); + $doleditor->Create(); + print '
'; + + echo '
', + ' ', + '
'; + + print '
'; +} + + +// End of page +llxFooter(); +$db->close(); +?> diff --git a/htdocs/resource/class/actions_resource.class.php b/htdocs/resource/class/actions_resource.class.php index 2daa45f8475..9e1f9b44f80 100644 --- a/htdocs/resource/class/actions_resource.class.php +++ b/htdocs/resource/class/actions_resource.class.php @@ -42,21 +42,20 @@ class ActionsResource } /** - * doActions + * doActions for resource module * - * @param array $parameters parameters - * @param object &$object object - * @param string &$action action - * @return void + * @param array $parameters parameters + * @param Object $object object + * @param string $action action */ - function doActions($parameters, &$object, &$action) { - + function doActions($parameters, &$object, &$action) + { global $langs,$user; $langs->load('resource'); - + if (in_array('element_resource',explode(':',$parameters['context']))) { - // Efface une ressource + // Delete a resource linked to an element if ($action == 'confirm_delete_resource' && $user->rights->resource->delete && GETPOST('confirm') == 'yes') { $res = $object->fetch(GETPOST('lineid')); diff --git a/htdocs/resource/class/resource.class.php b/htdocs/resource/class/resource.class.php index 0dd0d271a69..90549ebf2de 100644 --- a/htdocs/resource/class/resource.class.php +++ b/htdocs/resource/class/resource.class.php @@ -60,13 +60,96 @@ class Resource extends CommonObject return 1; } + /** + * Create object into database + * + * @param User $user User that creates + * @param int $notrigger 0=launch triggers after, 1=disable triggers + * @return int <0 if KO, Id of created object if OK + */ + function create($user, $notrigger=0) + { + global $conf, $langs; + $error=0; + + // Clean parameters + + if (isset($this->ref)) $this->ref=trim($this->ref); + if (isset($this->description)) $this->description=trim($this->description); + if (isset($this->fk_code_type_resource)) $this->fk_code_type_resource=trim($this->fk_code_type_resource); + if (isset($this->note_public)) $this->note_public=trim($this->note_public); + if (isset($this->note_private)) $this->note_private=trim($this->note_private); + + + // Insert request + $sql = "INSERT INTO ".MAIN_DB_PREFIX."resource("; + + $sql.= " entity,"; + $sql.= "ref,"; + $sql.= "description,"; + $sql.= "fk_code_type_resource,"; + $sql.= "note_public,"; + $sql.= "note_private"; + + $sql.= ") VALUES ("; + + $sql.= $conf->entity.", "; + $sql.= " ".(! isset($this->ref)?'NULL':"'".$this->db->escape($this->ref)."'").","; + $sql.= " ".(! isset($this->description)?'NULL':"'".$this->db->escape($this->description)."'").","; + $sql.= " ".(! isset($this->fk_code_type_resource)?'NULL':"'".$this->db->escape($this->fk_code_type_resource)."'").","; + $sql.= " ".(! isset($this->note_public)?'NULL':"'".$this->db->escape($this->note_public)."'").","; + $sql.= " ".(! isset($this->note_private)?'NULL':"'".$this->db->escape($this->note_private)."'"); + + $sql.= ")"; + + $this->db->begin(); + + dol_syslog(get_class($this)."::create sql=".$sql, LOG_DEBUG); + $resql=$this->db->query($sql); + if (! $resql) { + $error++; $this->errors[]="Error ".$this->db->lasterror(); + } + + if (! $error) + { + $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."place"); + + if (! $notrigger) + { + //// Call triggers + //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php'; + //$interface=new Interfaces($this->db); + //$result=$interface->run_triggers('RESOURCE_CREATE',$this,$user,$langs,$conf); + //if ($result < 0) { $error++; $this->errors=$interface->errors; } + //// End call triggers + } + } + + // Commit or rollback + if ($error) + { + foreach($this->errors as $errmsg) + { + dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR); + $this->error.=($this->error?', '.$errmsg:$errmsg); + } + $this->db->rollback(); + return -1*$error; + } + else + { + $this->db->commit(); + return $this->id; + } + } + /** * Load object in memory from database * * @param int $id id object * @return int <0 if KO, >0 if OK */ - function fetch($id) + function fetch_element_resource($id) { global $langs; $sql = "SELECT"; @@ -299,13 +382,13 @@ class Resource extends CommonObject /** - * Update object into database + * Update element resource into database * * @param User $user User that modifies * @param int $notrigger 0=launch triggers after, 1=disable triggers * @return int <0 if KO, >0 if OK */ - function update($user=0, $notrigger=0) + function update_element_resource($user=0, $notrigger=0) { global $conf, $langs; $error=0; @@ -319,9 +402,6 @@ class Resource extends CommonObject if (isset($this->mandatory)) $this->mandatory=trim($this->mandatory); - // Check parameters - // Put here code to add a control on parameters values - // Update request $sql = "UPDATE ".MAIN_DB_PREFIX."element_resources SET"; $sql.= " resource_id=".(isset($this->resource_id)?"'".$this->db->escape($this->resource_id)."'":"null").",";