dolibarr 25.0.0-alpha
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 * Copyright (C) 2024-2025 Frédéric France <frederic.france@free.fr>
5 * Copyright (C) 2024-2026 MDW <mdeweerd@users.noreply.github.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
27// Put here all includes required by your class file
28require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
29//require_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php';
30//require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php';
31
36{
40 public $module = 'ticket';
41
45 public $element = 'cticketcategory';
46
50 public $table_element = 'c_ticket_category';
51
55 public $picto = 'ticket';
56
57
58 const STATUS_DISABLED = 0;
59 const STATUS_ENABLED = 1;
60
61
88 // BEGIN MODULEBUILDER PROPERTIES
92 public $fields = array(
93 'rowid' => array('type' => 'integer', 'label' => 'TechnicalID', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 10),
94 'entity' => array('type' => 'integer', 'label' => 'Entity', 'default' => '1', 'enabled' => 1, 'visible' => -2, 'position' => 15, 'index' => 1),
95 'code' => array('type' => 'varchar(32)', 'label' => 'Code', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 20),
96 'pos' => array('type' => 'integer', 'label' => 'Pos', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 25),
97 'label' => array('type' => 'varchar(128)', 'label' => 'Label', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 30, 'showoncombobox' => 1),
98 'active' => array('type' => 'integer', 'label' => 'Active', 'enabled' => 1, 'visible' => -1, 'position' => 35),
99 'use_default' => array('type' => 'integer', 'label' => 'Usedefault', 'enabled' => 1, 'visible' => -1, 'position' => 40),
100 'description' => array('type' => 'varchar(255)', 'label' => 'Description', 'enabled' => 1, 'visible' => -1, 'position' => 45),
101 'fk_parent' => array('type' => 'integer', 'label' => 'ParentCategory', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 50),
102 'force_severity' => array('type' => 'varchar(32)', 'label' => 'Forceseverity', 'enabled' => 1, 'visible' => -1, 'position' => 55),
103 'public' => array('type' => 'integer', 'label' => 'Public', 'enabled' => 1, 'visible' => -1, 'position' => 60),
104 );
105
109 public $rowid;
110
114 public $ref;
115
119 public $entity;
120
124 public $label;
125
129 public $amount;
130
134 public $status;
135
139 public $fk_user_creat;
140
144 public $fk_user_modif;
145
149 public $last_main_doc;
150
154 public $import_key;
155 // END MODULEBUILDER PROPERTIES
156
157
158
164 public function __construct(DoliDB $db)
165 {
166 global $conf, $langs;
167
168 $this->db = $db;
169
170 $this->ismultientitymanaged = 0;
171 $this->isextrafieldmanaged = 0;
172
173 if (!getDolGlobalString('MAIN_SHOW_TECHNICAL_ID') && isset($this->fields['rowid'])) {
174 $this->fields['rowid']['visible'] = 0;
175 }
176 if (!isModEnabled('multicompany') && isset($this->fields['entity'])) {
177 $this->fields['entity']['enabled'] = 0;
178 }
179
180 // Unset fields that are disabled
181 foreach ($this->fields as $key => $val) {
182 if (isset($val['enabled']) && empty($val['enabled'])) {
183 unset($this->fields[$key]);
184 }
185 }
186
187 // Translate some data of arrayofkeyval
188 if (is_object($langs)) {
189 foreach ($this->fields as $key => $val) {
190 if (!empty($val['arrayofkeyval']) && is_array($val['arrayofkeyval'])) {
191 foreach ($val['arrayofkeyval'] as $key2 => $val2) {
192 $this->fields[$key]['arrayofkeyval'][$key2] = $langs->trans($val2);
193 }
194 }
195 }
196 }
197 }
198
206 public function create(User $user, $notrigger = 0)
207 {
208 $resultcreate = $this->createCommon($user, $notrigger);
209
210 //$resultvalidate = $this->validate($user, $notrigger);
211
212 return $resultcreate;
213 }
214
222 public function createFromClone(User $user, $fromid)
223 {
224 global $langs, $extrafields;
225 $error = 0;
226
227 dol_syslog(__METHOD__, LOG_DEBUG);
228
229 $object = new self($this->db);
230
231 $this->db->begin();
232
233 // Load source object
234 $result = $object->fetchCommon($fromid);
235 if ($result > 0 && !empty($object->table_element_line)) {
236 $object->fetchLines();
237 }
238
239 // get lines so they will be clone
240 //foreach($this->lines as $line)
241 // $line->fetch_optionals();
242
243 // Reset some properties
244 unset($object->id);
245 unset($object->fk_user_creat);
246 unset($object->import_key);
247
248 // Clear fields
249 if (property_exists($object, 'ref')) {
250 $object->ref = empty($this->fields['ref']['default']) ? "Copy_Of_".$object->ref : $this->fields['ref']['default'];
251 }
252 if (property_exists($object, 'label')) {
253 // @phan-suppress-next-line PhanTypeInvalidDimOffset
254 $object->label = empty($this->fields['label']['default']) ? $langs->trans("CopyOf")." ".$object->label : $this->fields['label']['default'];
255 }
256 if (property_exists($object, 'date_creation')) {
257 $object->date_creation = dol_now();
258 }
259 if (property_exists($object, 'date_modification')) {
260 $object->date_modification = null;
261 }
262 // ...
263 // Clear extrafields that are unique
264 if (is_array($object->array_options) && count($object->array_options) > 0) {
265 $extrafields->fetch_name_optionals_label($this->table_element);
266 foreach ($object->array_options as $key => $option) {
267 $shortkey = preg_replace('/options_/', '', $key);
268 if (!empty($extrafields->attributes[$this->table_element]['unique'][$shortkey])) {
269 //var_dump($key);
270 //var_dump($clonedObj->array_options[$key]); exit;
271 unset($object->array_options[$key]);
272 }
273 }
274 }
275
276 // Create clone
277 $object->context['createfromclone'] = 'createfromclone';
278 $result = $object->createCommon($user);
279 if ($result < 0) {
280 $error++;
282 }
283
284 if (!$error) {
285 // copy internal contacts
286 if ($this->copy_linked_contact($object, 'internal') < 0) {
287 $error++;
288 }
289 }
290
291 unset($object->context['createfromclone']);
292
293 // End
294 if (!$error) {
295 $this->db->commit();
296 return $object;
297 } else {
298 $this->db->rollback();
299 return -1;
300 }
301 }
302
310 public function fetch($id, $ref = null)
311 {
312 $result = $this->fetchCommon($id, $ref);
313 if ($result > 0 && !empty($this->table_element_line)) {
314 $this->fetchLines();
315 }
316 return $result;
317 }
318
324 public function fetchLines()
325 {
326 $this->lines = array();
327
328 $result = $this->fetchLinesCommon();
329 return $result;
330 }
331
332
345 public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, $filter = '', $filtermode = 'AND')
346 {
347 dol_syslog(__METHOD__, LOG_DEBUG);
348
349 $records = array();
350
351 $sql = 'SELECT ';
352 $sql .= $this->getFieldList('t');
353 $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
354 if (isset($this->ismultientitymanaged) && $this->ismultientitymanaged == 1) {
355 $sql .= ' WHERE t.entity IN ('.getEntity($this->element).')';
356 } else {
357 $sql .= ' WHERE 1 = 1';
358 }
359
360 // Manage filter
361 $errormessage = '';
362 $sql .= forgeSQLFromUniversalSearchCriteria($filter, $errormessage);
363 if ($errormessage) {
364 $this->errors[] = $errormessage;
365 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
366 return -1;
367 }
368
369 if (!empty($sortfield)) {
370 $sql .= $this->db->order($sortfield, $sortorder);
371 }
372 if (!empty($limit)) {
373 $sql .= $this->db->plimit($limit, $offset);
374 }
375
376 $resql = $this->db->query($sql);
377 if ($resql) {
378 $num = $this->db->num_rows($resql);
379 $i = 0;
380 while ($i < ($limit ? min($limit, $num) : $num)) {
381 $obj = $this->db->fetch_object($resql);
382
383 $record = new self($this->db);
384 $record->setVarsFromFetchObj($obj);
385
386 $records[$record->id] = $record;
387
388 $i++;
389 }
390 $this->db->free($resql);
391
392 return $records;
393 } else {
394 $this->errors[] = 'Error '.$this->db->lasterror();
395 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
396
397 return -1;
398 }
399 }
400
408 public function update(User $user, $notrigger = 0)
409 {
410 return $this->updateCommon($user, $notrigger);
411 }
412
420 public function delete(User $user, $notrigger = 0)
421 {
422 return $this->deleteCommon($user, $notrigger);
423 //return $this->deleteCommon($user, $notrigger, 1);
424 }
425
434 public function deleteLine(User $user, $idline, $notrigger = 0)
435 {
436 if ($this->status < 0) {
437 $this->error = 'ErrorDeleteLineNotAllowedByObjectStatus';
438 return -2;
439 }
440
441 return $this->deleteLineCommon($user, $idline, $notrigger);
442 }
443
444
455 public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
456 {
457 global $conf, $langs, $hookmanager;
458
459 if (!empty($conf->dol_no_mouse_hover)) {
460 $notooltip = 1; // Force disable tooltips
461 }
462
463 $result = '';
464
465 /*$label = img_picto('', $this->picto).' <u>'.$langs->trans("MyObject").'</u>';
466 if (isset($this->status)) {
467 $label .= ' '.$this->getLibStatut(5);
468 }
469 $label .= '<br>';
470 $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
471 */
472 $label = '';
473
474 $url = '';
475
476 if ($option != 'nolink') {
477 // Add param to save lastsearch_values or not
478 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
479 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
480 $add_save_lastsearch_values = 1;
481 }
482 if ($add_save_lastsearch_values) {
483 $url .= '&save_lastsearch_values=1';
484 }
485 }
486
487 $linkclose = '';
488 if (empty($notooltip)) {
489 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
490 $label = $langs->trans("ShowMyObject");
491 $linkclose .= ' alt="'.dolPrintHTMLForAttribute($label).'"';
492 }
493 $linkclose .= ' title="'.dolPrintHTMLForAttribute($label).'"';
494 $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
495 } else {
496 $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
497 }
498
499 if ($option == 'nolink') {
500 $linkstart = '<span';
501 } else {
502 $linkstart = '<a href="'.$url.'"';
503 }
504 $linkstart .= $linkclose.'>';
505 if ($option == 'nolink') {
506 $linkend = '</span>';
507 } else {
508 $linkend = '</a>';
509 }
510
511 //$result .= $linkstart;
512 $result .= $this->label;
513 //$result .= $linkend;
514
515 global $action, $hookmanager;
516 $hookmanager->initHooks(array('cticketcategorydao'));
517 $parameters = array('id' => $this->id, 'getnomurl' => &$result);
518 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
519 if ($reshook > 0) {
520 $result = $hookmanager->resPrint;
521 } else {
522 $result .= $hookmanager->resPrint;
523 }
524
525 return $result;
526 }
527
534 public function getLibStatut($mode = 0)
535 {
536 return $this->LibStatut($this->status, $mode);
537 }
538
539 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
547 public function LibStatut($status, $mode = 0)
548 {
549 // phpcs:enable
550 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
551 global $langs;
552 //$langs->load("mymodule@mymodule");
553 $this->labelStatus[self::STATUS_ENABLED] = $langs->transnoentitiesnoconv('Enabled');
554 $this->labelStatus[self::STATUS_DISABLED] = $langs->transnoentitiesnoconv('Disabled');
555 $this->labelStatusShort[self::STATUS_ENABLED] = $langs->transnoentitiesnoconv('Enabled');
556 $this->labelStatusShort[self::STATUS_DISABLED] = $langs->transnoentitiesnoconv('Disabled');
557 }
558
559 $statusType = 'status'.$status;
560 //if ($status == self::STATUS_VALIDATED) $statusType = 'status1';
561 if ($status == self::STATUS_DISABLED) {
562 $statusType = 'status6';
563 }
564
565 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
566 }
567
574 public function info($id)
575 {
576 $sql = 'SELECT rowid, date_creation as datec, tms as datem,';
577 $sql .= ' fk_user_creat, fk_user_modif';
578 $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
579 $sql .= ' WHERE t.rowid = '.((int) $id);
580 $result = $this->db->query($sql);
581 if ($result) {
582 if ($this->db->num_rows($result)) {
583 $obj = $this->db->fetch_object($result);
584
585 $this->id = $obj->rowid;
586
587 $this->user_creation_id = $obj->fk_user_creat;
588 $this->user_modification_id = $obj->fk_user_modif;
589 $this->date_creation = $this->db->jdate($obj->datec);
590 $this->date_modification = empty($obj->datem) ? '' : $this->db->jdate($obj->datem);
591 }
592
593 $this->db->free($result);
594 } else {
595 dol_print_error($this->db);
596 }
597 }
598
605 public function initAsSpecimen()
606 {
607 return $this->initAsSpecimenCommon();
608 }
609}
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
Class for MyObject.
deleteLine(User $user, $idline, $notrigger=0)
Delete a line of object in database.
info($id)
Load the info information in the object.
createFromClone(User $user, $fromid)
Clone an object into another one.
update(User $user, $notrigger=0)
Update object into database.
fetchAll($sortorder='', $sortfield='', $limit=0, $offset=0, $filter='', $filtermode='AND')
Load list of objects in memory from the database.
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.
getNomUrl($withpicto=0, $option='', $notooltip=0, $morecss='', $save_lastsearch_value=-1)
Return a link to the object card (with optionally the picto)
fetch($id, $ref=null)
Load object in memory from the database.
create(User $user, $notrigger=0)
Create object into database.
__construct(DoliDB $db)
Constructor.
fetchLines()
Load object lines in memory from the database.
deleteLineCommon(User $user, $idline, $notrigger=0)
Delete a line of object in database.
setErrorsFromObject($object)
setErrorsFromObject
createCommon(User $user, $notrigger=0)
Create object in the database.
getFieldList($alias='', $excludefields=array())
Function to concat keys of fields.
updateCommon(User $user, $notrigger=0)
Update object into database.
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.
fetchLinesCommon($morewhere='', $noextrafields=0)
Load object in memory from the database.
fetchCommon($id, $ref=null, $morewhere='', $noextrafields=0)
Load object in memory from the database.
deleteCommon(User $user, $notrigger=0, $forcechilddeletion=0)
Delete object in database.
Class to manage Dolibarr database access.
Class to manage Dolibarr users.
print $langs trans("Ref").' m titre as m m statut as status
Or an array listing all the potential status of the object: array: int of the status => translated la...
Definition index.php:169
dol_now($mode='gmt')
Return date for now.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0, $forbiddenfields=array())
forgeSQLFromUniversalSearchCriteria
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.