| Total Complexity | 96 |
| Total Lines | 797 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like MyObject often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MyObject, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | class MyObject extends CommonObject |
||
| 34 | { |
||
| 35 | /** |
||
| 36 | * @var string ID to identify managed object |
||
| 37 | */ |
||
| 38 | public $element = 'myobject'; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var string Name of table without prefix where object is stored |
||
| 42 | */ |
||
| 43 | public $table_element = 'mymodule_myobject'; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var int Does myobject support multicompany module ? 0=No test on entity, 1=Test with field entity, 2=Test with link by societe |
||
| 47 | */ |
||
| 48 | public $ismultientitymanaged = 0; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var int Does myobject support extrafields ? 0=No, 1=Yes |
||
| 52 | */ |
||
| 53 | public $isextrafieldmanaged = 1; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png |
||
| 57 | */ |
||
| 58 | public $picto = 'myobject@mymodule'; |
||
| 59 | |||
| 60 | |||
| 61 | const STATUS_DRAFT = 0; |
||
| 62 | const STATUS_VALIDATED = 1; |
||
| 63 | const STATUS_CANCELED = 9; |
||
| 64 | |||
| 65 | |||
| 66 | /** |
||
| 67 | * 'type' if the field format ('integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter]]', 'varchar(x)', 'double(24,8)', 'text', 'html', 'datetime', 'timestamp', 'float') |
||
| 68 | * Note: Filter can be a string like "(t.ref:like:'SO-%') or (t.date_creation:<:'20160101') or (t.nature:is:NULL)" |
||
| 69 | * 'label' the translation key. |
||
| 70 | * 'enabled' is a condition when the field must be managed. |
||
| 71 | * 'position' is the sort order of field. |
||
| 72 | * 'notnull' is set to 1 if not null in database. Set to -1 if we must set data to null if empty ('' or 0). |
||
| 73 | * 'visible' says if field is visible in list (Examples: 0=Not visible, 1=Visible on list and create/update/view forms, 2=Visible on list only, 3=Visible on create/update/view form only (not list), 4=Visible on list and update/view form only (not create). Using a negative value means field is not shown by default on list but can be selected for viewing) |
||
| 74 | * 'noteditable' says if field is not editable (1 or 0) |
||
| 75 | * 'default' is a default value for creation (can still be overwrote by the Setup of Default Values if field is editable in creation form). Note: If default is set to '(PROV)' and field is 'ref', the default value will be set to '(PROVid)' where id is rowid when a new record is created. |
||
| 76 | * 'index' if we want an index in database. |
||
| 77 | * 'foreignkey'=>'tablename.field' if the field is a foreign key (it is recommanded to name the field fk_...). |
||
| 78 | * 'searchall' is 1 if we want to search in this field when making a search from the quick search button. |
||
| 79 | * 'isameasure' must be set to 1 if you want to have a total on list for this field. Field type must be summable like integer or double(24,8). |
||
| 80 | * 'css' is the CSS style to use on field. For example: 'maxwidth200' |
||
| 81 | * 'help' is a string visible as a tooltip on field |
||
| 82 | * 'showoncombobox' if value of the field must be visible into the label of the combobox that list record |
||
| 83 | * 'disabled' is 1 if we want to have the field locked by a 'disabled' attribute. In most cases, this is never set into the definition of $fields into class, but is set dynamically by some part of code. |
||
| 84 | * 'arraykeyval' to set list of value if type is a list of predefined values. For example: array("0"=>"Draft","1"=>"Active","-1"=>"Cancel") |
||
| 85 | * 'comment' is not used. You can store here any text of your choice. It is not used by application. |
||
| 86 | */ |
||
| 87 | |||
| 88 | // BEGIN MODULEBUILDER PROPERTIES |
||
| 89 | /** |
||
| 90 | * @var array Array with all fields and their property. Do not use it as a static var. It may be modified by constructor. |
||
| 91 | */ |
||
| 92 | public $fields=array( |
||
| 93 | 'rowid' =>array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-2, 'noteditable'=>1, 'notnull'=> 1, 'index'=>1, 'position'=>1, 'comment'=>'Id'), |
||
| 94 | 'ref' =>array('type'=>'varchar(128)', 'label'=>'Ref', 'enabled'=>1, 'visible'=>1, 'noteditable'=>0, 'default'=>'', 'notnull'=> 1, 'showoncombobox'=>1, 'index'=>1, 'position'=>10, 'searchall'=>1, 'comment'=>'Reference of object'), |
||
| 95 | 'entity' =>array('type'=>'integer', 'label'=>'Entity', 'enabled'=>1, 'visible'=>0, 'notnull'=> 1, 'default'=>1, 'index'=>1, 'position'=>20), |
||
| 96 | 'label' =>array('type'=>'varchar(255)', 'label'=>'Label', 'enabled'=>1, 'visible'=>1, 'position'=>30, 'searchall'=>1, 'css'=>'minwidth200', 'help'=>'Help text', 'showoncombobox'=>1), |
||
| 97 | 'amount' =>array('type'=>'double(24,8)', 'label'=>'Amount', 'enabled'=>1, 'visible'=>1, 'default'=>'null', 'position'=>40, 'searchall'=>0, 'isameasure'=>1, 'help'=>'Help text for amount'), |
||
| 98 | 'qty' =>array('type'=>'real', 'label'=>'Qty', 'enabled'=>1, 'visible'=>1, 'default'=>'0', 'position'=>45, 'searchall'=>0, 'isameasure'=>1, 'help'=>'Help text for quantity', 'css'=>'maxwidth75imp'), |
||
| 99 | 'fk_soc' =>array('type'=>'integer:Societe:societe/class/societe.class.php:1:status=1 AND entity IN (__SHARED_ENTITIES__)', 'label'=>'ThirdParty', 'visible'=> 1, 'enabled'=>1, 'position'=>50, 'notnull'=>-1, 'index'=>1, 'help'=>'LinkToThirparty'), |
||
| 100 | 'fk_project' =>array('type'=>'integer:Project:projet/class/project.class.php:1', 'label'=>'Project', 'enabled'=>1, 'visible'=>-1, 'position'=>52, 'notnull'=>-1, 'index'=>1), |
||
| 101 | 'description' =>array('type'=>'text', 'label'=>'Description', 'enabled'=>1, 'visible'=>3, 'position'=>60), |
||
| 102 | 'note_public' =>array('type'=>'html', 'label'=>'NotePublic', 'enabled'=>1, 'visible'=>0, 'position'=>61), |
||
| 103 | 'note_private' =>array('type'=>'html', 'label'=>'NotePrivate', 'enabled'=>1, 'visible'=>0, 'position'=>62), |
||
| 104 | 'date_creation' =>array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-2, 'notnull'=> 1, 'position'=>500), |
||
| 105 | 'tms' =>array('type'=>'timestamp', 'label'=>'DateModification', 'enabled'=>1, 'visible'=>-2, 'notnull'=> 0, 'position'=>501), |
||
| 106 | //'date_validation' =>array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-2, 'position'=>502), |
||
| 107 | 'fk_user_creat' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserAuthor', 'enabled'=>1, 'visible'=>-2, 'notnull'=> 1, 'position'=>510, 'foreignkey'=>'user.rowid'), |
||
| 108 | 'fk_user_modif' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserModif', 'enabled'=>1, 'visible'=>-2, 'notnull'=>-1, 'position'=>511), |
||
| 109 | //'fk_user_valid' =>array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserValidation', 'enabled'=>1, 'visible'=>-1, 'position'=>512), |
||
| 110 | 'import_key' =>array('type'=>'varchar(14)', 'label'=>'ImportId', 'enabled'=>1, 'visible'=>-2, 'notnull'=>-1, 'index'=>0, 'position'=>1000), |
||
| 111 | 'model_pdf' =>array('type'=>'varchar(255)', 'label'=>'Model pdf', 'enabled'=>1, 'visible'=>0, 'notnull'=>-1, 'position'=>1010), |
||
| 112 | 'status' =>array('type'=>'smallint', 'label'=>'Status', 'enabled'=>1, 'visible'=>1, 'notnull'=> 1, 'default'=>0, 'index'=>1, 'position'=>1000, 'arrayofkeyval'=>array(0=>'Draft', 1=>'Validated', 9=>'Canceled')), |
||
| 113 | ); |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @var int ID |
||
| 117 | */ |
||
| 118 | public $rowid; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @var string Ref |
||
| 122 | */ |
||
| 123 | public $ref; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @var int Entity |
||
| 127 | */ |
||
| 128 | public $entity; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @var string label |
||
| 132 | */ |
||
| 133 | public $label; |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @var string amount |
||
| 137 | */ |
||
| 138 | public $amount; |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @var int Status |
||
| 142 | */ |
||
| 143 | public $status; |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @var integer|string date_creation |
||
| 147 | */ |
||
| 148 | public $date_creation; |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @var integer tms |
||
| 152 | */ |
||
| 153 | public $tms; |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @var int ID |
||
| 157 | */ |
||
| 158 | public $fk_user_creat; |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @var int ID |
||
| 162 | */ |
||
| 163 | public $fk_user_modif; |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @var string import_key |
||
| 167 | */ |
||
| 168 | public $import_key; |
||
| 169 | // END MODULEBUILDER PROPERTIES |
||
| 170 | |||
| 171 | |||
| 172 | // If this object has a subtable with lines |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @var int Name of subtable line |
||
| 176 | */ |
||
| 177 | //public $table_element_line = 'mymodule_myobjectline'; |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @var int Field with ID of parent key if this field has a parent |
||
| 181 | */ |
||
| 182 | //public $fk_element = 'fk_myobject'; |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @var int Name of subtable class that manage subtable lines |
||
| 186 | */ |
||
| 187 | //public $class_element_line = 'MyObjectline'; |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @var array List of child tables. To test if we can delete object. |
||
| 191 | */ |
||
| 192 | //protected $childtables=array(); |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @var array List of child tables. To know object to delete on cascade. |
||
| 196 | */ |
||
| 197 | //protected $childtablesoncascade=array('mymodule_myobjectdet'); |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @var MyObjectLine[] Array of subtable lines |
||
| 201 | */ |
||
| 202 | //public $lines = array(); |
||
| 203 | |||
| 204 | |||
| 205 | |||
| 206 | /** |
||
| 207 | * Constructor |
||
| 208 | * |
||
| 209 | * @param DoliDb $db Database handler |
||
| 210 | */ |
||
| 211 | public function __construct(DoliDB $db) |
||
| 212 | { |
||
| 213 | global $conf, $langs; |
||
| 214 | |||
| 215 | $this->db = $db; |
||
| 216 | |||
| 217 | if (empty($conf->global->MAIN_SHOW_TECHNICAL_ID) && isset($this->fields['rowid'])) $this->fields['rowid']['visible']=0; |
||
| 218 | if (empty($conf->multicompany->enabled) && isset($this->fields['entity'])) $this->fields['entity']['enabled']=0; |
||
| 219 | |||
| 220 | // Unset fields that are disabled |
||
| 221 | foreach($this->fields as $key => $val) |
||
| 222 | { |
||
| 223 | if (isset($val['enabled']) && empty($val['enabled'])) |
||
| 224 | { |
||
| 225 | unset($this->fields[$key]); |
||
| 226 | } |
||
| 227 | } |
||
| 228 | |||
| 229 | // Translate some data of arrayofkeyval |
||
| 230 | if (is_object($langs)) |
||
| 231 | { |
||
| 232 | foreach($this->fields as $key => $val) |
||
| 233 | { |
||
| 234 | if (is_array($val['arrayofkeyval'])) |
||
| 235 | { |
||
| 236 | foreach($val['arrayofkeyval'] as $key2 => $val2) |
||
| 237 | { |
||
| 238 | $this->fields[$key]['arrayofkeyval'][$key2]=$langs->trans($val2); |
||
| 239 | } |
||
| 240 | } |
||
| 241 | } |
||
| 242 | } |
||
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Create object into database |
||
| 247 | * |
||
| 248 | * @param User $user User that creates |
||
| 249 | * @param bool $notrigger false=launch triggers after, true=disable triggers |
||
| 250 | * @return int <0 if KO, Id of created object if OK |
||
| 251 | */ |
||
| 252 | public function create(User $user, $notrigger = false) |
||
| 253 | { |
||
| 254 | return $this->createCommon($user, $notrigger); |
||
| 255 | } |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Clone an object into another one |
||
| 259 | * |
||
| 260 | * @param User $user User that creates |
||
| 261 | * @param int $fromid Id of object to clone |
||
| 262 | * @return mixed New object created, <0 if KO |
||
| 263 | */ |
||
| 264 | public function createFromClone(User $user, $fromid) |
||
| 265 | { |
||
| 266 | global $langs, $extrafields; |
||
| 267 | $error = 0; |
||
| 268 | |||
| 269 | dol_syslog(__METHOD__, LOG_DEBUG); |
||
| 270 | |||
| 271 | $object = new self($this->db); |
||
| 272 | |||
| 273 | $this->db->begin(); |
||
| 274 | |||
| 275 | // Load source object |
||
| 276 | $result = $object->fetchCommon($fromid); |
||
| 277 | if ($result > 0 && ! empty($object->table_element_line)) $object->fetchLines(); |
||
| 278 | |||
| 279 | // get lines so they will be clone |
||
| 280 | //foreach($this->lines as $line) |
||
| 281 | // $line->fetch_optionals(); |
||
| 282 | |||
| 283 | // Reset some properties |
||
| 284 | unset($object->id); |
||
| 285 | unset($object->fk_user_creat); |
||
| 286 | unset($object->import_key); |
||
| 287 | |||
| 288 | |||
| 289 | // Clear fields |
||
| 290 | $object->ref = empty($this->fields['ref']['default']) ? "copy_of_".$object->ref: $this->fields['ref']['default']; |
||
| 291 | $object->label = empty($this->fields['label']['default']) ? $langs->trans("CopyOf")." ".$object->label: $this->fields['label']['default']; |
||
| 292 | $object->status = self::STATUS_DRAFT; |
||
| 293 | // ... |
||
| 294 | // Clear extrafields that are unique |
||
| 295 | if (is_array($object->array_options) && count($object->array_options) > 0) |
||
| 296 | { |
||
| 297 | $extrafields->fetch_name_optionals_label($this->table_element); |
||
| 298 | foreach($object->array_options as $key => $option) |
||
| 299 | { |
||
| 300 | $shortkey = preg_replace('/options_/', '', $key); |
||
| 301 | if (! empty($extrafields->attributes[$this->element]['unique'][$shortkey])) |
||
| 302 | { |
||
| 303 | //var_dump($key); var_dump($clonedObj->array_options[$key]); exit; |
||
| 304 | unset($object->array_options[$key]); |
||
| 305 | } |
||
| 306 | } |
||
| 307 | } |
||
| 308 | |||
| 309 | // Create clone |
||
| 310 | $object->context['createfromclone'] = 'createfromclone'; |
||
| 311 | $result = $object->createCommon($user); |
||
| 312 | if ($result < 0) { |
||
| 313 | $error++; |
||
| 314 | $this->error = $object->error; |
||
| 315 | $this->errors = $object->errors; |
||
| 316 | } |
||
| 317 | |||
| 318 | if (! $error) |
||
| 319 | { |
||
| 320 | // copy internal contacts |
||
| 321 | if ($this->copy_linked_contact($object, 'internal') < 0) |
||
| 322 | { |
||
| 323 | $error++; |
||
| 324 | } |
||
| 325 | } |
||
| 326 | |||
| 327 | if (! $error) |
||
| 328 | { |
||
| 329 | // copy external contacts if same company |
||
| 330 | if (property_exists($this, 'socid') && $this->socid == $object->socid) |
||
|
1 ignored issue
–
show
|
|||
| 331 | { |
||
| 332 | if ($this->copy_linked_contact($object, 'external') < 0) |
||
| 333 | $error++; |
||
| 334 | } |
||
| 335 | } |
||
| 336 | |||
| 337 | unset($object->context['createfromclone']); |
||
| 338 | |||
| 339 | // End |
||
| 340 | if (!$error) { |
||
| 341 | $this->db->commit(); |
||
| 342 | return $object; |
||
| 343 | } else { |
||
| 344 | $this->db->rollback(); |
||
| 345 | return -1; |
||
| 346 | } |
||
| 347 | } |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Load object in memory from the database |
||
| 351 | * |
||
| 352 | * @param int $id Id object |
||
| 353 | * @param string $ref Ref |
||
| 354 | * @return int <0 if KO, 0 if not found, >0 if OK |
||
| 355 | */ |
||
| 356 | public function fetch($id, $ref = null) |
||
| 357 | { |
||
| 358 | $result = $this->fetchCommon($id, $ref); |
||
| 359 | if ($result > 0 && ! empty($this->table_element_line)) $this->fetchLines(); |
||
| 360 | return $result; |
||
| 361 | } |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Load object lines in memory from the database |
||
| 365 | * |
||
| 366 | * @return int <0 if KO, 0 if not found, >0 if OK |
||
| 367 | */ |
||
| 368 | public function fetchLines() |
||
| 369 | { |
||
| 370 | $this->lines=array(); |
||
| 371 | |||
| 372 | $result = $this->fetchLinesCommon(); |
||
| 373 | return $result; |
||
| 374 | } |
||
| 375 | |||
| 376 | |||
| 377 | /** |
||
| 378 | * Load list of objects in memory from the database. |
||
| 379 | * |
||
| 380 | * @param string $sortorder Sort Order |
||
| 381 | * @param string $sortfield Sort field |
||
| 382 | * @param int $limit limit |
||
| 383 | * @param int $offset Offset |
||
| 384 | * @param array $filter Filter array. Example array('field'=>'valueforlike', 'customurl'=>...) |
||
| 385 | * @param string $filtermode Filter mode (AND or OR) |
||
| 386 | * @return array|int int <0 if KO, array of pages if OK |
||
| 387 | */ |
||
| 388 | public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND') |
||
| 389 | { |
||
| 390 | global $conf; |
||
| 391 | |||
| 392 | dol_syslog(__METHOD__, LOG_DEBUG); |
||
| 393 | |||
| 394 | $records=array(); |
||
| 395 | |||
| 396 | $sql = 'SELECT '; |
||
| 397 | $sql .= $this->getFieldList(); |
||
| 398 | $sql .= ' FROM ' . MAIN_DB_PREFIX . $this->table_element. ' as t'; |
||
| 399 | if (isset($this->ismultientitymanaged) && $this->ismultientitymanaged == 1) $sql .= ' WHERE t.entity IN ('.getEntity($this->table_element).')'; |
||
| 400 | else $sql .= ' WHERE 1 = 1'; |
||
| 401 | // Manage filter |
||
| 402 | $sqlwhere = array(); |
||
| 403 | if (count($filter) > 0) { |
||
| 404 | foreach ($filter as $key => $value) { |
||
| 405 | if ($key=='t.rowid') { |
||
| 406 | $sqlwhere[] = $key . '='. $value; |
||
| 407 | } |
||
| 408 | elseif (strpos($key, 'date') !== false) { |
||
| 409 | $sqlwhere[] = $key.' = \''.$this->db->idate($value).'\''; |
||
| 410 | } |
||
| 411 | elseif ($key=='customsql') { |
||
| 412 | $sqlwhere[] = $value; |
||
| 413 | } |
||
| 414 | else { |
||
| 415 | $sqlwhere[] = $key . ' LIKE \'%' . $this->db->escape($value) . '%\''; |
||
| 416 | } |
||
| 417 | } |
||
| 418 | } |
||
| 419 | if (count($sqlwhere) > 0) { |
||
| 420 | $sql .= ' AND (' . implode(' '.$filtermode.' ', $sqlwhere).')'; |
||
| 421 | } |
||
| 422 | |||
| 423 | if (!empty($sortfield)) { |
||
| 424 | $sql .= $this->db->order($sortfield, $sortorder); |
||
| 425 | } |
||
| 426 | if (!empty($limit)) { |
||
| 427 | $sql .= ' ' . $this->db->plimit($limit, $offset); |
||
| 428 | } |
||
| 429 | |||
| 430 | $resql = $this->db->query($sql); |
||
| 431 | if ($resql) { |
||
| 432 | $num = $this->db->num_rows($resql); |
||
| 433 | $i = 0; |
||
| 434 | while ($i < min($limit, $num)) |
||
| 435 | { |
||
| 436 | $obj = $this->db->fetch_object($resql); |
||
| 437 | |||
| 438 | $record = new self($this->db); |
||
| 439 | $record->setVarsFromFetchObj($obj); |
||
| 440 | |||
| 441 | $records[$record->id] = $record; |
||
| 442 | |||
| 443 | $i++; |
||
| 444 | } |
||
| 445 | $this->db->free($resql); |
||
| 446 | |||
| 447 | return $records; |
||
| 448 | } else { |
||
| 449 | $this->errors[] = 'Error ' . $this->db->lasterror(); |
||
| 450 | dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR); |
||
| 451 | |||
| 452 | return -1; |
||
| 453 | } |
||
| 454 | } |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Update object into database |
||
| 458 | * |
||
| 459 | * @param User $user User that modifies |
||
| 460 | * @param bool $notrigger false=launch triggers after, true=disable triggers |
||
| 461 | * @return int <0 if KO, >0 if OK |
||
| 462 | */ |
||
| 463 | public function update(User $user, $notrigger = false) |
||
| 464 | { |
||
| 465 | return $this->updateCommon($user, $notrigger); |
||
| 466 | } |
||
| 467 | |||
| 468 | /** |
||
| 469 | * Delete object in database |
||
| 470 | * |
||
| 471 | * @param User $user User that deletes |
||
| 472 | * @param bool $notrigger false=launch triggers after, true=disable triggers |
||
| 473 | * @return int <0 if KO, >0 if OK |
||
| 474 | */ |
||
| 475 | public function delete(User $user, $notrigger = false) |
||
| 476 | { |
||
| 477 | return $this->deleteCommon($user, $notrigger); |
||
| 478 | //return $this->deleteCommon($user, $notrigger, 1); |
||
| 479 | } |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Delete a line of object in database |
||
| 483 | * |
||
| 484 | * @param User $user User that delete |
||
| 485 | * @param int $idline Id of line to delete |
||
| 486 | * @param bool $notrigger false=launch triggers after, true=disable triggers |
||
| 487 | * @return int >0 if OK, <0 if KO |
||
| 488 | */ |
||
| 489 | public function deleteLine(User $user, $idline, $notrigger = false) |
||
| 490 | { |
||
| 491 | if ($this->status < 0) |
||
| 492 | { |
||
| 493 | $this->error = 'ErrorDeleteLineNotAllowedByObjectStatus'; |
||
| 494 | return -2; |
||
| 495 | } |
||
| 496 | |||
| 497 | return $this->deleteLineCommon($user, $idline, $notrigger); |
||
| 498 | } |
||
| 499 | |||
| 500 | |||
| 501 | /** |
||
| 502 | * Set draft status |
||
| 503 | * |
||
| 504 | * @param User $user Object user that modify |
||
| 505 | * @param int $notrigger 1=Does not execute triggers, 0=Execute triggers |
||
| 506 | * @return int <0 if KO, >0 if OK |
||
| 507 | */ |
||
| 508 | public function setDraft($user, $notrigger = 0) |
||
| 509 | { |
||
| 510 | // Protection |
||
| 511 | if ($this->status <= self::STATUS_DRAFT) |
||
| 512 | { |
||
| 513 | return 0; |
||
| 514 | } |
||
| 515 | |||
| 516 | /*if (! ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->mymodule->write)) |
||
| 517 | || (! empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->mymodule->mymodule_advance->validate)))) |
||
| 518 | { |
||
| 519 | $this->error='Permission denied'; |
||
| 520 | return -1; |
||
| 521 | }*/ |
||
| 522 | |||
| 523 | return $this->setStatusCommon($user, self::STATUS_DRAFT, $notrigger, 'BOM_UNVALIDATE'); |
||
| 524 | } |
||
| 525 | |||
| 526 | /** |
||
| 527 | * Set cancel status |
||
| 528 | * |
||
| 529 | * @param User $user Object user that modify |
||
| 530 | * @param int $notrigger 1=Does not execute triggers, 0=Execute triggers |
||
| 531 | * @return int <0 if KO, 0=Nothing done, >0 if OK |
||
| 532 | */ |
||
| 533 | public function cancel($user, $notrigger = 0) |
||
| 549 | } |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Set back to validated status |
||
| 553 | * |
||
| 554 | * @param User $user Object user that modify |
||
| 555 | * @param int $notrigger 1=Does not execute triggers, 0=Execute triggers |
||
| 556 | * @return int <0 if KO, 0=Nothing done, >0 if OK |
||
| 557 | */ |
||
| 558 | public function reopen($user, $notrigger = 0) |
||
| 559 | { |
||
| 560 | // Protection |
||
| 561 | if ($this->status != self::STATUS_CANCELED) |
||
| 562 | { |
||
| 563 | return 0; |
||
| 564 | } |
||
| 565 | |||
| 566 | /*if (! ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->mymodule->write)) |
||
| 567 | || (! empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->mymodule->mymodule_advance->validate)))) |
||
| 568 | { |
||
| 569 | $this->error='Permission denied'; |
||
| 570 | return -1; |
||
| 571 | }*/ |
||
| 572 | |||
| 573 | return $this->setStatusCommon($user, self::STATUS_VALIDATED, $notrigger, 'BOM_REOPEN'); |
||
| 574 | } |
||
| 575 | |||
| 576 | /** |
||
| 577 | * Return a link to the object card (with optionaly the picto) |
||
| 578 | * |
||
| 579 | * @param int $withpicto Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto) |
||
| 580 | * @param string $option On what the link point to ('nolink', ...) |
||
| 581 | * @param int $notooltip 1=Disable tooltip |
||
| 582 | * @param string $morecss Add more css on link |
||
| 583 | * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking |
||
| 584 | * @return string String with URL |
||
| 585 | */ |
||
| 586 | public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1) |
||
| 587 | { |
||
| 588 | global $conf, $langs, $hookmanager; |
||
| 589 | |||
| 590 | if (! empty($conf->dol_no_mouse_hover)) $notooltip=1; // Force disable tooltips |
||
| 591 | |||
| 592 | $result = ''; |
||
| 593 | |||
| 594 | $label = '<u>' . $langs->trans("MyObject") . '</u>'; |
||
| 595 | $label.= '<br>'; |
||
| 596 | $label.= '<b>' . $langs->trans('Ref') . ':</b> ' . $this->ref; |
||
| 597 | |||
| 598 | $url = dol_buildpath('/mymodule/myobject_card.php', 1).'?id='.$this->id; |
||
| 599 | |||
| 600 | if ($option != 'nolink') |
||
| 601 | { |
||
| 602 | // Add param to save lastsearch_values or not |
||
| 603 | $add_save_lastsearch_values=($save_lastsearch_value == 1 ? 1 : 0); |
||
| 604 | if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) $add_save_lastsearch_values=1; |
||
| 605 | if ($add_save_lastsearch_values) $url.='&save_lastsearch_values=1'; |
||
| 606 | } |
||
| 607 | |||
| 608 | $linkclose=''; |
||
| 609 | if (empty($notooltip)) |
||
| 610 | { |
||
| 611 | if (! empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) |
||
| 612 | { |
||
| 613 | $label=$langs->trans("ShowMyObject"); |
||
| 614 | $linkclose.=' alt="'.dol_escape_htmltag($label, 1).'"'; |
||
| 615 | } |
||
| 616 | $linkclose.=' title="'.dol_escape_htmltag($label, 1).'"'; |
||
| 617 | $linkclose.=' class="classfortooltip'.($morecss?' '.$morecss:'').'"'; |
||
| 618 | } |
||
| 619 | else $linkclose = ($morecss?' class="'.$morecss.'"':''); |
||
| 620 | |||
| 621 | $linkstart = '<a href="'.$url.'"'; |
||
| 622 | $linkstart.=$linkclose.'>'; |
||
| 623 | $linkend='</a>'; |
||
| 624 | |||
| 625 | $result .= $linkstart; |
||
| 626 | if ($withpicto) $result.=img_object(($notooltip?'':$label), ($this->picto?$this->picto:'generic'), ($notooltip?(($withpicto != 2) ? 'class="paddingright"' : ''):'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip?0:1); |
||
| 627 | if ($withpicto != 2) $result.= $this->ref; |
||
| 628 | $result .= $linkend; |
||
| 629 | //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : ''); |
||
| 630 | |||
| 631 | global $action,$hookmanager; |
||
| 632 | $hookmanager->initHooks(array('myobjectdao')); |
||
| 633 | $parameters=array('id'=>$this->id, 'getnomurl'=>$result); |
||
| 634 | $reshook=$hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks |
||
| 635 | if ($reshook > 0) $result = $hookmanager->resPrint; |
||
| 636 | else $result .= $hookmanager->resPrint; |
||
| 637 | |||
| 638 | return $result; |
||
| 639 | } |
||
| 640 | |||
| 641 | /** |
||
| 642 | * Return label of the status |
||
| 643 | * |
||
| 644 | * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto, 6=Long label + Picto |
||
| 645 | * @return string Label of status |
||
| 646 | */ |
||
| 647 | public function getLibStatut($mode = 0) |
||
| 648 | { |
||
| 649 | return $this->LibStatut($this->status, $mode); |
||
| 650 | } |
||
| 651 | |||
| 652 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 653 | /** |
||
| 654 | * Return the status |
||
| 655 | * |
||
| 656 | * @param int $status Id status |
||
| 657 | * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto, 6=Long label + Picto |
||
| 658 | * @return string Label of status |
||
| 659 | */ |
||
| 660 | public function LibStatut($status, $mode = 0) |
||
| 661 | { |
||
| 662 | // phpcs:enable |
||
| 663 | if (empty($this->labelStatus) || empty($this->labelStatusShort)) |
||
| 664 | { |
||
| 665 | global $langs; |
||
| 666 | //$langs->load("mymodule"); |
||
| 667 | $this->labelStatus[self::STATUS_DRAFT] = $langs->trans('Draft'); |
||
| 668 | $this->labelStatus[self::STATUS_VALIDATED] = $langs->trans('Enabled'); |
||
| 669 | $this->labelStatus[self::STATUS_CANCELED] = $langs->trans('Disabled'); |
||
| 670 | $this->labelStatusShort[self::STATUS_DRAFT] = $langs->trans('Draft'); |
||
| 671 | $this->labelStatusShort[self::STATUS_VALIDATED] = $langs->trans('Enabled'); |
||
| 672 | $this->labelStatusShort[self::STATUS_CANCELED] = $langs->trans('Disabled'); |
||
| 673 | } |
||
| 674 | |||
| 675 | $statusType = 'status'.$status; |
||
| 676 | if ($status == self::STATUS_VALIDATED) $statusType = 'status4'; |
||
| 677 | |||
| 678 | return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode); |
||
| 679 | } |
||
| 680 | |||
| 681 | /** |
||
| 682 | * Load the info information in the object |
||
| 683 | * |
||
| 684 | * @param int $id Id of object |
||
| 685 | * @return void |
||
| 686 | */ |
||
| 687 | public function info($id) |
||
| 688 | { |
||
| 689 | $sql = 'SELECT rowid, date_creation as datec, tms as datem,'; |
||
| 690 | $sql.= ' fk_user_creat, fk_user_modif'; |
||
| 691 | $sql.= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t'; |
||
| 692 | $sql.= ' WHERE t.rowid = '.$id; |
||
| 693 | $result=$this->db->query($sql); |
||
| 694 | if ($result) |
||
| 695 | { |
||
| 696 | if ($this->db->num_rows($result)) |
||
| 697 | { |
||
| 698 | $obj = $this->db->fetch_object($result); |
||
| 699 | $this->id = $obj->rowid; |
||
| 700 | if ($obj->fk_user_author) |
||
| 701 | { |
||
| 702 | $cuser = new User($this->db); |
||
| 703 | $cuser->fetch($obj->fk_user_author); |
||
| 704 | $this->user_creation = $cuser; |
||
| 705 | } |
||
| 706 | |||
| 707 | if ($obj->fk_user_valid) |
||
| 708 | { |
||
| 709 | $vuser = new User($this->db); |
||
| 710 | $vuser->fetch($obj->fk_user_valid); |
||
| 711 | $this->user_validation = $vuser; |
||
| 712 | } |
||
| 713 | |||
| 714 | if ($obj->fk_user_cloture) |
||
| 715 | { |
||
| 716 | $cluser = new User($this->db); |
||
| 717 | $cluser->fetch($obj->fk_user_cloture); |
||
| 718 | $this->user_cloture = $cluser; |
||
| 719 | } |
||
| 720 | |||
| 721 | $this->date_creation = $this->db->jdate($obj->datec); |
||
| 722 | $this->date_modification = $this->db->jdate($obj->datem); |
||
| 723 | $this->date_validation = $this->db->jdate($obj->datev); |
||
| 724 | } |
||
| 725 | |||
| 726 | $this->db->free($result); |
||
| 727 | } |
||
| 728 | else |
||
| 729 | { |
||
| 730 | dol_print_error($this->db); |
||
| 731 | } |
||
| 732 | } |
||
| 733 | |||
| 734 | /** |
||
| 735 | * Initialise object with example values |
||
| 736 | * Id must be 0 if object instance is a specimen |
||
| 737 | * |
||
| 738 | * @return void |
||
| 739 | */ |
||
| 740 | public function initAsSpecimen() |
||
| 741 | { |
||
| 742 | $this->initAsSpecimenCommon(); |
||
| 743 | } |
||
| 744 | |||
| 745 | /** |
||
| 746 | * Create an array of lines |
||
| 747 | * |
||
| 748 | * @return array|int array of lines if OK, <0 if KO |
||
| 749 | */ |
||
| 750 | public function getLinesArray() |
||
| 767 | } |
||
| 768 | } |
||
| 769 | |||
| 770 | /** |
||
| 771 | * Create a document onto disk according to template module. |
||
| 772 | * |
||
| 773 | * @param string $modele Force template to use ('' to not force) |
||
| 774 | * @param Translate $outputlangs objet lang a utiliser pour traduction |
||
| 775 | * @param int $hidedetails Hide details of lines |
||
| 776 | * @param int $hidedesc Hide description |
||
| 777 | * @param int $hideref Hide ref |
||
| 778 | * @param null|array $moreparams Array to provide more information |
||
| 779 | * @return int 0 if KO, 1 if OK |
||
| 780 | */ |
||
| 781 | public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams = null) |
||
| 800 | } |
||
| 801 | |||
| 802 | /** |
||
| 803 | * Action executed by scheduler |
||
| 804 | * CAN BE A CRON TASK. In such a case, parameters come from the schedule job setup field 'Parameters' |
||
| 805 | * |
||
| 806 | * @return int 0 if OK, <>0 if KO (this function is used also by cron so only 0 is OK) |
||
| 807 | */ |
||
| 808 | //public function doScheduledJob($param1, $param2, ...) |
||
| 809 | public function doScheduledJob() |
||
| 830 | } |
||
| 831 | } |
||
| 832 | |||
| 833 | /** |
||
| 834 | * Class MyObjectLine. You can also remove this and generate a CRUD class for lines objects. |
||
| 835 | */ |
||
| 836 | class MyObjectLine |
||
| 841 |