diff --git a/htdocs/core/class/ctyperesource.class.php b/htdocs/core/class/ctyperesource.class.php new file mode 100644 index 00000000000..ee899ad6b86 --- /dev/null +++ b/htdocs/core/class/ctyperesource.class.php @@ -0,0 +1,500 @@ + + * Copyright (C) 2014-2016 Juanjo Menent + * Copyright (C) 2015 Florian Henry + * Copyright (C) 2015 Raphaƫl Doursenaud + * Copyright (C) ---Put here your own copyright and developer email--- + * + * 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/ctyperesource.class.php + * \ingroup resource + */ + +/** + * Class Ctyperesource + * + * Put here description of your class + * + * @see CommonObject + */ +class Ctyperesource +{ + /** + * @var string Id to identify managed objects + */ + public $element = 'ctyperesource'; + /** + * @var string Name of table without prefix where object is stored + */ + public $table_element = 'c_type_resource'; + + /** + * @var CtyperesourceLine[] Lines + */ + public $lines = array(); + + /** + */ + + public $code; + public $label; + public $active; + + /** + */ + + + /** + * Constructor + * + * @param DoliDb $db Database handler + */ + public function __construct(DoliDB $db) + { + $this->db = $db; + } + + /** + * Create object into database + * + * @param User $user User that creates + * @param bool $notrigger false=launch triggers after, true=disable triggers + * + * @return int <0 if KO, Id of created object if OK + */ + public function create(User $user, $notrigger = false) + { + dol_syslog(__METHOD__, LOG_DEBUG); + + $error = 0; + + // Clean parameters + + if (isset($this->code)) { + $this->code = trim($this->code); + } + if (isset($this->label)) { + $this->label = trim($this->label); + } + if (isset($this->active)) { + $this->active = trim($this->active); + } + + + + // Check parameters + // Put here code to add control on parameters values + + // Insert request + $sql = 'INSERT INTO ' . MAIN_DB_PREFIX . $this->table_element . '('; + + $sql.= 'code,'; + $sql.= 'label'; + $sql.= 'active'; + + + $sql .= ') VALUES ('; + + $sql .= ' '.(! isset($this->code)?'NULL':"'".$this->db->escape($this->code)."'").','; + $sql .= ' '.(! isset($this->label)?'NULL':"'".$this->db->escape($this->label)."'").','; + $sql .= ' '.(! isset($this->active)?'NULL':$this->active); + + + $sql .= ')'; + + $this->db->begin(); + + $resql = $this->db->query($sql); + if (!$resql) { + $error ++; + $this->errors[] = 'Error ' . $this->db->lasterror(); + dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR); + } + + if (!$error) { + $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . $this->table_element); + + if (!$notrigger) { + // Uncomment this and change MYOBJECT to your own tag if you + // want this action to call a trigger. + + //// Call triggers + //$result=$this->call_trigger('MYOBJECT_CREATE',$user); + //if ($result < 0) $error++; + //// End call triggers + } + } + + // Commit or rollback + if ($error) { + $this->db->rollback(); + + return - 1 * $error; + } else { + $this->db->commit(); + + return $this->id; + } + } + + /** + * Load object in memory from the database + * + * @param int $id Id object + * @param string $ref Ref + * + * @return int <0 if KO, 0 if not found, >0 if OK + */ + public function fetch($id,$code='',$label='') + { + dol_syslog(__METHOD__, LOG_DEBUG); + + $sql = 'SELECT'; + $sql .= ' t.rowid,'; + + $sql .= " t.code,"; + $sql .= " t.label,"; + $sql .= " t.active"; + + + $sql .= ' FROM ' . MAIN_DB_PREFIX . $this->table_element . ' as t'; + if ($id) $sql.= " WHERE t.id = ".$id; + elseif ($code) $sql.= " WHERE t.code = '".$this->db->escape($code)."'"; + elseif ($label) $sql.= " WHERE t.label = '".$this->db->escape($label)."'"; + + + $resql = $this->db->query($sql); + if ($resql) { + $numrows = $this->db->num_rows($resql); + if ($numrows) { + $obj = $this->db->fetch_object($resql); + + $this->id = $obj->rowid; + + $this->code = $obj->code; + $this->label = $obj->label; + $this->active = $obj->active; + + + } + + // Retrieve all extrafields for invoice + // fetch optionals attributes and labels + /* + require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php'; + $extrafields=new ExtraFields($this->db); + $extralabels=$extrafields->fetch_name_optionals_label($this->table_element,true); + $this->fetch_optionals($this->id,$extralabels); + */ + + // $this->fetch_lines(); + + $this->db->free($resql); + + if ($numrows) { + return 1; + } else { + return 0; + } + } else { + $this->errors[] = 'Error ' . $this->db->lasterror(); + dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR); + + return - 1; + } + } + + /** + * Load object in memory from the database + * + * @param string $sortorder Sort Order + * @param string $sortfield Sort field + * @param int $limit offset limit + * @param int $offset offset limit + * @param array $filter filter array + * @param string $filtermode filter mode (AND or OR) + * + * @return int <0 if KO, >0 if OK + */ + public function fetchAll($sortorder='', $sortfield='', $limit=0, $offset=0, array $filter = array(), $filtermode='AND') + { + dol_syslog(__METHOD__, LOG_DEBUG); + + $sql = 'SELECT'; + $sql .= ' t.rowid,'; + + $sql .= " t.code,"; + $sql .= " t.label,"; + $sql .= " t.active"; + + + $sql .= ' FROM ' . MAIN_DB_PREFIX . $this->table_element. ' as t'; + + // Manage filter + $sqlwhere = array(); + if (count($filter) > 0) { + foreach ($filter as $key => $value) { + $sqlwhere [] = $key . ' LIKE \'%' . $this->db->escape($value) . '%\''; + } + } + + if (count($sqlwhere) > 0) { + $sql .= ' WHERE ' . implode(' '.$filtermode.' ', $sqlwhere); + } + if (!empty($sortfield)) { + $sql .= $this->db->order($sortfield,$sortorder); + } + if (!empty($limit)) { + $sql .= ' ' . $this->db->plimit($limit, $offset); + } + + $resql = $this->db->query($sql); + if ($resql) { + $num = $this->db->num_rows($resql); + + while ($obj = $this->db->fetch_object($resql)) { + $line = new self($this->db); + + $line->id = $obj->rowid; + + $line->code = $obj->code; + $line->label = $obj->label; + $line->active = $obj->active; + + + } + $this->db->free($resql); + + return $num; + } else { + $this->errors[] = 'Error ' . $this->db->lasterror(); + dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR); + + return - 1; + } + } + + /** + * Update object into database + * + * @param User $user User that modifies + * @param bool $notrigger false=launch triggers after, true=disable triggers + * + * @return int <0 if KO, >0 if OK + */ + public function update(User $user, $notrigger = false) + { + $error = 0; + + dol_syslog(__METHOD__, LOG_DEBUG); + + // Clean parameters + + if (isset($this->code)) { + $this->code = trim($this->code); + } + if (isset($this->label)) { + $this->label = trim($this->label); + } + if (isset($this->active)) { + $this->active = trim($this->active); + } + + // Check parameters + // Put here code to add a control on parameters values + + // Update request + $sql = 'UPDATE ' . MAIN_DB_PREFIX . $this->table_element . ' SET'; + + $sql .= ' code = '.(isset($this->code)?"'".$this->db->escape($this->code)."'":"null").','; + $sql .= ' label = '.(isset($this->label)?"'".$this->db->escape($this->label)."'":"null").','; + $sql .= ' active = '.(isset($this->active)?$this->active:"null"); + + + $sql .= ' WHERE rowid=' . $this->id; + + $this->db->begin(); + + $resql = $this->db->query($sql); + if (!$resql) { + $error ++; + $this->errors[] = 'Error ' . $this->db->lasterror(); + dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR); + } + + if (!$error && !$notrigger) { + // Uncomment this and change MYOBJECT to your own tag if you + // want this action calls a trigger. + + //// Call triggers + //$result=$this->call_trigger('MYOBJECT_MODIFY',$user); + //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail} + //// End call triggers + } + + // Commit or rollback + if ($error) { + $this->db->rollback(); + + return - 1 * $error; + } else { + $this->db->commit(); + + return 1; + } + } + + /** + * Delete object in database + * + * @param User $user User that deletes + * @param bool $notrigger false=launch triggers after, true=disable triggers + * + * @return int <0 if KO, >0 if OK + */ + public function delete(User $user, $notrigger = false) + { + dol_syslog(__METHOD__, LOG_DEBUG); + + $error = 0; + + $this->db->begin(); + + if (!$error) { + if (!$notrigger) { + // Uncomment this and change MYOBJECT to your own tag if you + // want this action calls a trigger. + + //// Call triggers + //$result=$this->call_trigger('MYOBJECT_DELETE',$user); + //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail} + //// End call triggers + } + } + + // If you need to delete child tables to, you can insert them here + + if (!$error) { + $sql = 'DELETE FROM ' . MAIN_DB_PREFIX . $this->table_element; + $sql .= ' WHERE rowid=' . $this->id; + + $resql = $this->db->query($sql); + if (!$resql) { + $error ++; + $this->errors[] = 'Error ' . $this->db->lasterror(); + dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR); + } + } + + // Commit or rollback + if ($error) { + $this->db->rollback(); + + return - 1 * $error; + } else { + $this->db->commit(); + + return 1; + } + } + + /** + * Load an object from its id and create a new one in database + * + * @param int $fromid Id of object to clone + * + * @return int New id of clone + */ + public function createFromClone($fromid) + { + dol_syslog(__METHOD__, LOG_DEBUG); + + global $user; + $error = 0; + $object = new Ctyperesource($this->db); + + $this->db->begin(); + + // Load source object + $object->fetch($fromid); + // Reset object + $object->id = 0; + + // Clear fields + // ... + + // Create clone + $result = $object->create($user); + + // Other options + if ($result < 0) { + $error ++; + $this->errors = $object->errors; + dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR); + } + + // End + if (!$error) { + $this->db->commit(); + + return $object->id; + } else { + $this->db->rollback(); + + return - 1; + } + } + + /** + * Initialise object with example values + * Id must be 0 if object instance is a specimen + * + * @return void + */ + public function initAsSpecimen() + { + $this->id = 0; + + $this->code = ''; + $this->label = ''; + $this->active = ''; + } + +} + +/** + * Class CtyperesourceLine + */ +class CtyperesourceLine +{ + /** + * @var int ID + */ + public $id; + /** + * @var mixed Sample line property 1 + */ + + public $code; + public $label; + public $active; + + /** + * @var mixed Sample line property 2 + */ + +} diff --git a/htdocs/core/modules/modResource.class.php b/htdocs/core/modules/modResource.class.php index d3aa3405d0b..f6674bdcfd7 100644 --- a/htdocs/core/modules/modResource.class.php +++ b/htdocs/core/modules/modResource.class.php @@ -201,22 +201,6 @@ class modResource extends DolibarrModules $this->menu = 1; // This module add menu entries. They are coded into menu manager. - // Add here list of permission defined by - // an id, a label, a boolean and two constant strings. - // Example: - //// Permission id (must not be already used) - //$this->rights[$r][0] = 2000; - //// Permission label - //$this->rights[$r][1] = 'Permision label'; - //// Permission by default for new user (0/1) - //$this->rights[$r][3] = 0; - //// In php code, permission will be checked by test - //// if ($user->rights->permkey->level1->level2) - //$this->rights[$r][4] = 'level1'; - //// In php code, permission will be checked by test - //// if ($user->rights->permkey->level1->level2) - //$this->rights[$r][5] = 'level2'; - //$r++; // Main menu entries $this->menu = array(); // List of menus to add $r = 0; @@ -267,98 +251,63 @@ class modResource extends DolibarrModules 'user'=> 0 ); - // Exports - $r = 1; - // Example: - //$this->export_code[$r]=$this->rights_class.'_'.$r; - //// Translation key (used only if key ExportDataset_xxx_z not found) - //$this->export_label[$r]='CustomersInvoicesAndInvoiceLines'; - //// Condition to show export in list (ie: '$user->id==3'). - //// Set to 1 to always show when module is enabled. - //$this->export_enabled[$r]='1'; - //$this->export_permission[$r]=array(array("facture","facture","export")); - //$this->export_fields_array[$r]=array( - // 's.rowid'=>"IdCompany", - // 's.nom'=>'CompanyName', - // 's.address'=>'Address', - // 's.cp'=>'Zip', - // 's.ville'=>'Town', - // 's.fk_pays'=>'Country', - // 's.tel'=>'Phone', - // 's.siren'=>'ProfId1', - // 's.siret'=>'ProfId2', - // 's.ape'=>'ProfId3', - // 's.idprof4'=>'ProfId4', - // 's.code_compta'=>'CustomerAccountancyCode', - // 's.code_compta_fournisseur'=>'SupplierAccountancyCode', - // 'f.rowid'=>"InvoiceId", - // 'f.facnumber'=>"InvoiceRef", - // 'f.datec'=>"InvoiceDateCreation", - // 'f.datef'=>"DateInvoice", - // 'f.total'=>"TotalHT", - // 'f.total_ttc'=>"TotalTTC", - // 'f.tva'=>"TotalVAT", - // 'f.paye'=>"InvoicePaid", - // 'f.fk_statut'=>'InvoiceStatus', - // 'f.note'=>"InvoiceNote", - // 'fd.rowid'=>'LineId', - // 'fd.description'=>"LineDescription", - // 'fd.price'=>"LineUnitPrice", - // 'fd.tva_tx'=>"LineVATRate", - // 'fd.qty'=>"LineQty", - // 'fd.total_ht'=>"LineTotalHT", - // 'fd.total_tva'=>"LineTotalTVA", - // 'fd.total_ttc'=>"LineTotalTTC", - // 'fd.date_start'=>"DateStart", - // 'fd.date_end'=>"DateEnd", - // 'fd.fk_product'=>'ProductId', - // 'p.ref'=>'ProductRef' - //); - //$this->export_entities_array[$r]=array('s.rowid'=>"company", - // 's.nom'=>'company', - // 's.address'=>'company', - // 's.cp'=>'company', - // 's.ville'=>'company', - // 's.fk_pays'=>'company', - // 's.tel'=>'company', - // 's.siren'=>'company', - // 's.siret'=>'company', - // 's.ape'=>'company', - // 's.idprof4'=>'company', - // 's.code_compta'=>'company', - // 's.code_compta_fournisseur'=>'company', - // 'f.rowid'=>"invoice", - // 'f.facnumber'=>"invoice", - // 'f.datec'=>"invoice", - // 'f.datef'=>"invoice", - // 'f.total'=>"invoice", - // 'f.total_ttc'=>"invoice", - // 'f.tva'=>"invoice", - // 'f.paye'=>"invoice", - // 'f.fk_statut'=>'invoice', - // 'f.note'=>"invoice", - // 'fd.rowid'=>'invoice_line', - // 'fd.description'=>"invoice_line", - // 'fd.price'=>"invoice_line", - // 'fd.total_ht'=>"invoice_line", - // 'fd.total_tva'=>"invoice_line", - // 'fd.total_ttc'=>"invoice_line", - // 'fd.tva_tx'=>"invoice_line", - // 'fd.qty'=>"invoice_line", - // 'fd.date_start'=>"invoice_line", - // 'fd.date_end'=>"invoice_line", - // 'fd.fk_product'=>'product', - // 'p.ref'=>'product' - //); - //$this->export_sql_start[$r] = 'SELECT DISTINCT '; - //$this->export_sql_end[$r] = ' FROM (' . MAIN_DB_PREFIX . 'facture as f, ' - // . MAIN_DB_PREFIX . 'facturedet as fd, ' . MAIN_DB_PREFIX . 'societe as s)'; - //$this->export_sql_end[$r] .= ' LEFT JOIN ' . MAIN_DB_PREFIX - // . 'product as p on (fd.fk_product = p.rowid)'; - //$this->export_sql_end[$r] .= ' WHERE f.fk_soc = s.rowid ' - // . 'AND f.rowid = fd.fk_facture'; - //$r++; + // Exports + //-------- + $r=0; + + $r++; + $this->export_code[$r]=$this->rights_class.'_'.$r; + $this->export_label[$r]="ResourceSingular"; // Translation key (used only if key ExportDataset_xxx_z not found) + $this->export_permission[$r]=array(array("resource","read")); + $this->export_fields_array[$r]=array('r.rowid'=>'IdResource','r.ref'=>'ResourceFormLabel_ref','c.code'=>'ResourceTypeCode','c.label'=>'ResourceType','r.description'=>'ResourceFormLabel_description','r.note_private'=>"NotePrivate",'r.note_public'=>"NotePublic",'r.asset_number'=>'AssetNumber','r.datec'=>"DateCreation",'r.tms'=>"DateLastModification"); + $this->export_TypeFields_array[$r]=array('r.rowid'=>'List:resource:ref','r.ref'=>'Text','r.asset_number'=>'Text','r.description'=>'Text','c.code'=>'Text','c.label'=>'List:c_type_resource:label','r.datec'=>'Date','r.tms'=>'Date','r.note_private'=>'Text','r.note_public'=>'Text'); + $this->export_entities_array[$r]=array('r.rowid'=>'resource','r.ref'=>'resource','c.code'=>'resource','c.label'=>'resource','r.description'=>'resource','r.note_private'=>"resource",'r.resource'=>"resource",'r.asset_number'=>'resource','r.datec'=>"resource",'r.tms'=>"resource"); + $keyforselect='resource'; $keyforelement='resource'; $keyforaliasextra='extra'; + include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php'; + + $this->export_dependencies_array[$r]=array('resource'=>array('r.rowid')); // We must keep this until the aggregate_array is used. To add unique key if we ask a field of a child to avoid the DISTINCT to discard them. + $this->export_sql_start[$r]='SELECT DISTINCT '; + $this->export_sql_end[$r] =' FROM '.MAIN_DB_PREFIX.'resource as r '; + $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'c_type_resource as c ON c.rowid=r.fk_code_type_resource'; + $this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'resource_extrafields as extra ON extra.fk_object = c.rowid'; + $this->export_sql_end[$r] .=' AND r.entity IN ('.getEntity('resource',1).')'; + + + // Imports + //-------- + $r=0; + + // Import list of third parties and attributes + $r++; + $this->import_code[$r]=$this->rights_class.'_'.$r; + $this->import_label[$r]='ImportDataset_resource_1'; + $this->import_icon[$r]='resource'; + $this->import_entities_array[$r]=array(); // We define here only fields that use another icon that the one defined into import_icon + $this->import_tables_array[$r]=array('r'=>MAIN_DB_PREFIX.'resource','extra'=>MAIN_DB_PREFIX.'resource_extrafields'); // List of tables to insert into (insert done in same order) + $this->import_fields_array[$r]=array('r.ref'=>"ResourceFormLabel_ref*",'r.fk_code_type_resource'=>'ResourceTypeCode','r.description'=>'ResourceFormLabel_description','r.note_private'=>"NotePrivate",'r.note_public'=>"NotePublic",'r.asset_number'=>'AssetNumber','r.datec'=>'DateCreation'); + // Add extra fields + $sql="SELECT name, label, fieldrequired FROM ".MAIN_DB_PREFIX."extrafields WHERE elementtype = 'resource' AND entity = ".$conf->entity; + $resql=$this->db->query($sql); + if ($resql) // This can fail when class is used on old database (during migration for example) + { + while ($obj=$this->db->fetch_object($resql)) + { + $fieldname='extra.'.$obj->name; + $fieldlabel=ucfirst($obj->label); + $this->import_fields_array[$r][$fieldname]=$fieldlabel.($obj->fieldrequired?'*':''); + } + } + // End add extra fields + $this->import_fieldshidden_array[$r]=array('r.fk_user_author'=>'user->id','extra.fk_object'=>'lastrowid-'.MAIN_DB_PREFIX.'resource'); // aliastable.field => ('user->id' or 'lastrowid-'.tableparent) + $this->import_convertvalue_array[$r]=array( + 'r.fk_code_type_resource'=>array('rule'=>'fetchidfromcodeorlabel','classfile'=>'/core/class/ctyperesource.class.php','class'=>'Ctyperesource','method'=>'fetch','dict'=>'DictionaryResourceType'), + ); + //$this->import_convertvalue_array[$r]=array('s.fk_soc'=>array('rule'=>'lastrowid',table='t'); + $this->import_regex_array[$r]=array('s.datec'=>'^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]( [0-9][0-9]:[0-9][0-9]:[0-9][0-9])?$'); + $this->import_examplevalues_array[$r]=array('r.ref'=>"REF1",'r.fk_code_type_resource'=>"Code from dictionnary resource type",'r.datec'=>"2017-01-01 or 2017-01-01 12:30:00"); + $this->import_updatekeys_array[$r]=array('r.rf'=>'ResourceFormLabel_ref'); + } /** diff --git a/htdocs/langs/en_US/resource.lang b/htdocs/langs/en_US/resource.lang index f95121db351..5a907f6ba23 100644 --- a/htdocs/langs/en_US/resource.lang +++ b/htdocs/langs/en_US/resource.lang @@ -29,3 +29,8 @@ RessourceSuccessfullyDeleted=Resource successfully deleted DictionaryResourceType=Type of resources SelectResource=Select resource + +IdResource=Id resource +AssetNumber=Serial number +ResourceTypeCode=Resource type code +ImportDataset_resource_1=Resources