dolibarr 18.0.6
cticketcategory.class.php
1<?php
2/* Copyright (C) 2017 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) ---Put here your own copyright and developer email---
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
25// Put here all includes required by your class file
26require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
27//require_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php';
28//require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php';
29
34{
38 public $module = 'ticket';
39
43 public $element = 'cticketcategory';
44
48 public $table_element = 'c_ticket_category';
49
54 public $ismultientitymanaged = 0;
55
59 public $isextrafieldmanaged = 0;
60
64 public $picto = 'ticket';
65
66
67 const STATUS_DISABLED = 0;
68 const STATUS_ENABLED = 1;
69
70
97 // BEGIN MODULEBUILDER PROPERTIES
101 public $fields=array(
102 'rowid' =>array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>10),
103 'entity' =>array('type'=>'integer', 'label'=>'Entity', 'default'=>1, 'enabled'=>1, 'visible'=>-2, 'position'=>15, 'index'=>1),
104 'code' =>array('type'=>'varchar(32)', 'label'=>'Code', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>20),
105 'pos' =>array('type'=>'integer', 'label'=>'Pos', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>25),
106 'label' =>array('type'=>'varchar(128)', 'label'=>'Label', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>30, 'showoncombobox'=>1),
107 'active' =>array('type'=>'integer', 'label'=>'Active', 'enabled'=>1, 'visible'=>-1, 'position'=>35),
108 'use_default' =>array('type'=>'integer', 'label'=>'Usedefault', 'enabled'=>1, 'visible'=>-1, 'position'=>40),
109 'description' =>array('type'=>'varchar(255)', 'label'=>'Description', 'enabled'=>1, 'visible'=>-1, 'position'=>45),
110 'fk_parent' =>array('type'=>'integer', 'label'=>'Fkparent', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>50),
111 'force_severity' =>array('type'=>'varchar(32)', 'label'=>'Forceseverity', 'enabled'=>1, 'visible'=>-1, 'position'=>55),
112 'public' =>array('type'=>'integer', 'label'=>'Public', 'enabled'=>1, 'visible'=>-1, 'position'=>60),
113 );
114
118 public $rowid;
119
123 public $ref;
124
128 public $entity;
129
133 public $label;
134
138 public $amount;
139
143 public $status;
144
148 public $date_creation;
149
153 public $tms;
154
158 public $fk_user_creat;
159
163 public $fk_user_modif;
164
168 public $last_main_doc;
169
173 public $import_key;
174 // END MODULEBUILDER PROPERTIES
175
176
177
183 public function __construct(DoliDB $db)
184 {
185 global $conf, $langs;
186
187 $this->db = $db;
188
189 if (!getDolGlobalString('MAIN_SHOW_TECHNICAL_ID') && isset($this->fields['rowid'])) {
190 $this->fields['rowid']['visible'] = 0;
191 }
192 if (!isModEnabled('multicompany') && isset($this->fields['entity'])) {
193 $this->fields['entity']['enabled'] = 0;
194 }
195
196 // Example to show how to set values of fields definition dynamically
197 /*if ($user->hasRight('mymodule', 'myobject', 'read')) {
198 $this->fields['myfield']['visible'] = 1;
199 $this->fields['myfield']['noteditable'] = 0;
200 }*/
201
202 // Unset fields that are disabled
203 foreach ($this->fields as $key => $val) {
204 if (isset($val['enabled']) && empty($val['enabled'])) {
205 unset($this->fields[$key]);
206 }
207 }
208
209 // Translate some data of arrayofkeyval
210 if (is_object($langs)) {
211 foreach ($this->fields as $key => $val) {
212 if (!empty($val['arrayofkeyval']) && is_array($val['arrayofkeyval'])) {
213 foreach ($val['arrayofkeyval'] as $key2 => $val2) {
214 $this->fields[$key]['arrayofkeyval'][$key2] = $langs->trans($val2);
215 }
216 }
217 }
218 }
219 }
220
228 public function create(User $user, $notrigger = false)
229 {
230 $resultcreate = $this->createCommon($user, $notrigger);
231
232 //$resultvalidate = $this->validate($user, $notrigger);
233
234 return $resultcreate;
235 }
236
244 public function createFromClone(User $user, $fromid)
245 {
246 global $langs, $extrafields;
247 $error = 0;
248
249 dol_syslog(__METHOD__, LOG_DEBUG);
250
251 $object = new self($this->db);
252
253 $this->db->begin();
254
255 // Load source object
256 $result = $object->fetchCommon($fromid);
257 if ($result > 0 && !empty($object->table_element_line)) {
258 $object->fetchLines();
259 }
260
261 // get lines so they will be clone
262 //foreach($this->lines as $line)
263 // $line->fetch_optionals();
264
265 // Reset some properties
266 unset($object->id);
267 unset($object->fk_user_creat);
268 unset($object->import_key);
269
270 // Clear fields
271 if (property_exists($object, 'ref')) {
272 $object->ref = empty($this->fields['ref']['default']) ? "Copy_Of_".$object->ref : $this->fields['ref']['default'];
273 }
274 if (property_exists($object, 'label')) {
275 $object->label = empty($this->fields['label']['default']) ? $langs->trans("CopyOf")." ".$object->label : $this->fields['label']['default'];
276 }
277 if (property_exists($object, 'date_creation')) {
278 $object->date_creation = dol_now();
279 }
280 if (property_exists($object, 'date_modification')) {
281 $object->date_modification = null;
282 }
283 // ...
284 // Clear extrafields that are unique
285 if (is_array($object->array_options) && count($object->array_options) > 0) {
286 $extrafields->fetch_name_optionals_label($this->table_element);
287 foreach ($object->array_options as $key => $option) {
288 $shortkey = preg_replace('/options_/', '', $key);
289 if (!empty($extrafields->attributes[$this->table_element]['unique'][$shortkey])) {
290 //var_dump($key);
291 //var_dump($clonedObj->array_options[$key]); exit;
292 unset($object->array_options[$key]);
293 }
294 }
295 }
296
297 // Create clone
298 $object->context['createfromclone'] = 'createfromclone';
299 $result = $object->createCommon($user);
300 if ($result < 0) {
301 $error++;
302 $this->error = $object->error;
303 $this->errors = $object->errors;
304 }
305
306 if (!$error) {
307 // copy internal contacts
308 if ($this->copy_linked_contact($object, 'internal') < 0) {
309 $error++;
310 }
311 }
312
313 if (!$error) {
314 // copy external contacts if same company
315 if (property_exists($this, 'fk_soc') && $this->fk_soc == $object->socid) {
316 if ($this->copy_linked_contact($object, 'external') < 0) {
317 $error++;
318 }
319 }
320 }
321
322 unset($object->context['createfromclone']);
323
324 // End
325 if (!$error) {
326 $this->db->commit();
327 return $object;
328 } else {
329 $this->db->rollback();
330 return -1;
331 }
332 }
333
341 public function fetch($id, $ref = null)
342 {
343 $result = $this->fetchCommon($id, $ref);
344 if ($result > 0 && !empty($this->table_element_line)) {
345 $this->fetchLines();
346 }
347 return $result;
348 }
349
355 public function fetchLines()
356 {
357 $this->lines = array();
358
359 $result = $this->fetchLinesCommon();
360 return $result;
361 }
362
363
375 public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND')
376 {
377 global $conf;
378
379 dol_syslog(__METHOD__, LOG_DEBUG);
380
381 $records = array();
382
383 $sql = 'SELECT ';
384 $sql .= $this->getFieldList('t');
385 $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
386 if (isset($this->ismultientitymanaged) && $this->ismultientitymanaged == 1) {
387 $sql .= ' WHERE t.entity IN ('.getEntity($this->element).')';
388 } else {
389 $sql .= ' WHERE 1 = 1';
390 }
391 // Manage filter
392 $sqlwhere = array();
393 if (count($filter) > 0) {
394 foreach ($filter as $key => $value) {
395 if ($key == 't.rowid') {
396 $sqlwhere[] = $key." = ".((int) $value);
397 } elseif (in_array($this->fields[$key]['type'], array('date', 'datetime', 'timestamp'))) {
398 $sqlwhere[] = $key." = '".$this->db->idate($value)."'";
399 } elseif ($key == 'customsql') {
400 $sqlwhere[] = $value;
401 } elseif (strpos($value, '%') === false) {
402 $sqlwhere[] = $key." IN (".$this->db->sanitize($this->db->escape($value)).")";
403 } else {
404 $sqlwhere[] = $key." LIKE '%".$this->db->escape($value)."%'";
405 }
406 }
407 }
408 if (count($sqlwhere) > 0) {
409 $sql .= ' AND ('.implode(' '.$this->db->escape($filtermode).' ', $sqlwhere).')';
410 }
411
412 if (!empty($sortfield)) {
413 $sql .= $this->db->order($sortfield, $sortorder);
414 }
415 if (!empty($limit)) {
416 $sql .= $this->db->plimit($limit, $offset);
417 }
418
419 $resql = $this->db->query($sql);
420 if ($resql) {
421 $num = $this->db->num_rows($resql);
422 $i = 0;
423 while ($i < ($limit ? min($limit, $num) : $num)) {
424 $obj = $this->db->fetch_object($resql);
425
426 $record = new self($this->db);
427 $record->setVarsFromFetchObj($obj);
428
429 $records[$record->id] = $record;
430
431 $i++;
432 }
433 $this->db->free($resql);
434
435 return $records;
436 } else {
437 $this->errors[] = 'Error '.$this->db->lasterror();
438 dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
439
440 return -1;
441 }
442 }
443
451 public function update(User $user, $notrigger = false)
452 {
453 return $this->updateCommon($user, $notrigger);
454 }
455
463 public function delete(User $user, $notrigger = false)
464 {
465 return $this->deleteCommon($user, $notrigger);
466 //return $this->deleteCommon($user, $notrigger, 1);
467 }
468
477 public function deleteLine(User $user, $idline, $notrigger = false)
478 {
479 if ($this->status < 0) {
480 $this->error = 'ErrorDeleteLineNotAllowedByObjectStatus';
481 return -2;
482 }
483
484 return $this->deleteLineCommon($user, $idline, $notrigger);
485 }
486
487
498 public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
499 {
500 global $conf, $langs, $hookmanager;
501
502 if (!empty($conf->dol_no_mouse_hover)) {
503 $notooltip = 1; // Force disable tooltips
504 }
505
506 $result = '';
507
508 /*$label = img_picto('', $this->picto).' <u>'.$langs->trans("MyObject").'</u>';
509 if (isset($this->status)) {
510 $label .= ' '.$this->getLibStatut(5);
511 }
512 $label .= '<br>';
513 $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
514 */
515 $label = '';
516
517 //$url = dol_buildpath('/mymodule/myobject_card.php', 1).'?id='.$this->id;
518 $url = '';
519
520 if ($option != 'nolink') {
521 // Add param to save lastsearch_values or not
522 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
523 if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
524 $add_save_lastsearch_values = 1;
525 }
526 if ($add_save_lastsearch_values) {
527 $url .= '&save_lastsearch_values=1';
528 }
529 }
530
531 $linkclose = '';
532 if (empty($notooltip)) {
533 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
534 $label = $langs->trans("ShowMyObject");
535 $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
536 }
537 $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
538 $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
539 } else {
540 $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
541 }
542
543 if ($option == 'nolink') {
544 $linkstart = '<span';
545 } else {
546 $linkstart = '<a href="'.$url.'"';
547 }
548 $linkstart .= $linkclose.'>';
549 if ($option == 'nolink') {
550 $linkend = '</span>';
551 } else {
552 $linkend = '</a>';
553 }
554
555 //$result .= $linkstart;
556 $result .= $this->label;
557 //$result .= $linkend;
558
559 global $action, $hookmanager;
560 $hookmanager->initHooks(array('cticketcategorydao'));
561 $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
562 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
563 if ($reshook > 0) {
564 $result = $hookmanager->resPrint;
565 } else {
566 $result .= $hookmanager->resPrint;
567 }
568
569 return $result;
570 }
571
578 public function getLibStatut($mode = 0)
579 {
580 return $this->LibStatut($this->status, $mode);
581 }
582
583 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
591 public function LibStatut($status, $mode = 0)
592 {
593 // phpcs:enable
594 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
595 global $langs;
596 //$langs->load("mymodule@mymodule");
597 $this->labelStatus[self::STATUS_ENABLED] = $langs->transnoentitiesnoconv('Enabled');
598 $this->labelStatus[self::STATUS_DISABLED] = $langs->transnoentitiesnoconv('Disabled');
599 $this->labelStatusShort[self::STATUS_ENABLED] = $langs->transnoentitiesnoconv('Enabled');
600 $this->labelStatusShort[self::STATUS_DISABLED] = $langs->transnoentitiesnoconv('Disabled');
601 }
602
603 $statusType = 'status'.$status;
604 //if ($status == self::STATUS_VALIDATED) $statusType = 'status1';
605 if ($status == self::STATUS_DISABLED) {
606 $statusType = 'status6';
607 }
608
609 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
610 }
611
618 public function info($id)
619 {
620 $sql = 'SELECT rowid, date_creation as datec, tms as datem,';
621 $sql .= ' fk_user_creat, fk_user_modif';
622 $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
623 $sql .= ' WHERE t.rowid = '.((int) $id);
624 $result = $this->db->query($sql);
625 if ($result) {
626 if ($this->db->num_rows($result)) {
627 $obj = $this->db->fetch_object($result);
628 $this->id = $obj->rowid;
629
630
631 $this->user_creation_id = $obj->fk_user_creat;
632 $this->user_modification_id = $obj->fk_user_modif;
633 $this->date_creation = $this->db->jdate($obj->datec);
634 $this->date_modification = empty($obj->datem) ? '' : $this->db->jdate($obj->datem);
635 }
636
637 $this->db->free($result);
638 } else {
639 dol_print_error($this->db);
640 }
641 }
642
649 public function initAsSpecimen()
650 {
651 $this->initAsSpecimenCommon();
652 }
653}
Class for MyObject.
info($id)
Load the info information in the object.
createFromClone(User $user, $fromid)
Clone an object into another one.
getLibStatut($mode=0)
Return the label of the status.
LibStatut($status, $mode=0)
Return the status.
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
update(User $user, $notrigger=false)
Update object into database.
create(User $user, $notrigger=false)
Create object into database.
getNomUrl($withpicto=0, $option='', $notooltip=0, $morecss='', $save_lastsearch_value=-1)
Return a link to the object card (with optionaly the picto)
fetch($id, $ref=null)
Load object in memory from the database.
deleteLine(User $user, $idline, $notrigger=false)
Delete a line of object in database.
__construct(DoliDB $db)
Constructor.
fetchLines()
Load object lines in memory from the database.
fetchAll($sortorder='', $sortfield='', $limit=0, $offset=0, array $filter=array(), $filtermode='AND')
Load list of objects in memory from the database.
Parent class of all other business classes (invoices, contracts, proposals, orders,...
deleteLineCommon(User $user, $idline, $notrigger=false)
Delete a line of object in database.
fetchCommon($id, $ref=null, $morewhere='')
Load object in memory from the database.
createCommon(User $user, $notrigger=false)
Create object into database.
deleteCommon(User $user, $notrigger=false, $forcechilddeletion=0)
Delete object in database.
getFieldList($alias='', $excludefields=array())
Function to concat keys of fields.
initAsSpecimenCommon()
Initialise object with example values Id must be 0 if object instance is a specimen.
copy_linked_contact($objFrom, $source='internal')
Copy contact from one element to current.
updateCommon(User $user, $notrigger=false)
Update object into database.
fetchLinesCommon($morewhere='')
Load object in memory from the database.
Class to manage Dolibarr database access.
Class to manage Dolibarr users.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_now($mode='auto')
Return date for now.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.