cticketcategory.class.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  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. /**
  19. * \file htdocs/modulebuilder/template/class/myobject.class.php
  20. * \ingroup mymodule
  21. * \brief This file is a CRUD class file for MyObject (Create/Read/Update/Delete)
  22. */
  23. // Put here all includes required by your class file
  24. require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
  25. //require_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php';
  26. //require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php';
  27. /**
  28. * Class for MyObject
  29. */
  30. class CTicketCategory extends CommonObject
  31. {
  32. /**
  33. * @var string ID of module.
  34. */
  35. public $module = 'ticket';
  36. /**
  37. * @var string ID to identify managed object.
  38. */
  39. public $element = 'cticketcategory';
  40. /**
  41. * @var string Name of table without prefix where object is stored. This is also the key used for extrafields management.
  42. */
  43. public $table_element = 'c_ticket_category';
  44. /**
  45. * @var int Does this object support multicompany module ?
  46. * 0=No test on entity, 1=Test with field entity, 'field@table'=Test with link by field@table
  47. */
  48. public $ismultientitymanaged = 0;
  49. /**
  50. * @var int Does object support extrafields ? 0=No, 1=Yes
  51. */
  52. public $isextrafieldmanaged = 0;
  53. /**
  54. * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png
  55. */
  56. public $picto = 'ticket';
  57. const STATUS_DISABLED = 0;
  58. const STATUS_ENABLED = 1;
  59. /**
  60. * 'type' field format ('integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter]]', 'sellist:TableName:LabelFieldName[:KeyFieldName[:KeyFieldParent[:Filter]]]', 'varchar(x)', 'double(24,8)', 'real', 'price', 'text', 'text:none', 'html', 'date', 'datetime', 'timestamp', 'duration', 'mail', 'phone', 'url', 'password')
  61. * Note: Filter can be a string like "(t.ref:like:'SO-%') or (t.date_creation:<:'20160101') or (t.nature:is:NULL)"
  62. * 'label' the translation key.
  63. * 'picto' is code of a picto to show before value in forms
  64. * 'enabled' is a condition when the field must be managed (Example: 1 or '$conf->global->MY_SETUP_PARAM)
  65. * 'position' is the sort order of field.
  66. * 'notnull' is set to 1 if not null in database. Set to -1 if we must set data to null if empty ('' or 0).
  67. * '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). 5=Visible on list and view only (not create/not update). Using a negative value means field is not shown by default on list but can be selected for viewing)
  68. * 'noteditable' says if field is not editable (1 or 0)
  69. * '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.
  70. * 'index' if we want an index in database.
  71. * 'foreignkey'=>'tablename.field' if the field is a foreign key (it is recommanded to name the field fk_...).
  72. * 'searchall' is 1 if we want to search in this field when making a search from the quick search button.
  73. * '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).
  74. * 'css' and 'cssview' and 'csslist' is the CSS style to use on field. 'css' is used in creation and update. 'cssview' is used in view mode. 'csslist' is used for columns in lists. For example: 'maxwidth200', 'wordbreak', 'tdoverflowmax200'
  75. * 'help' is a 'TranslationString' to use to show a tooltip on field. You can also use 'TranslationString:keyfortooltiponlick' for a tooltip on click.
  76. * 'showoncombobox' if value of the field must be visible into the label of the combobox that list record
  77. * '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.
  78. * 'arrayofkeyval' to set a list of values if type is a list of predefined values. For example: array("0"=>"Draft","1"=>"Active","-1"=>"Cancel"). Note that type can be 'integer' or 'varchar'
  79. * 'autofocusoncreate' to have field having the focus on a create form. Only 1 field should have this property set to 1.
  80. * 'comment' is not used. You can store here any text of your choice. It is not used by application.
  81. *
  82. * Note: To have value dynamic, you can set value to 0 in definition and edit the value on the fly into the constructor.
  83. */
  84. // BEGIN MODULEBUILDER PROPERTIES
  85. /**
  86. * @var array Array with all fields and their property. Do not use it as a static var. It may be modified by constructor.
  87. */
  88. public $fields=array(
  89. 'rowid' =>array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>10),
  90. 'entity' =>array('type'=>'integer', 'label'=>'Entity', 'default'=>1, 'enabled'=>1, 'visible'=>-2, 'position'=>15, 'index'=>1),
  91. 'code' =>array('type'=>'varchar(32)', 'label'=>'Code', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>20),
  92. 'pos' =>array('type'=>'integer', 'label'=>'Pos', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>25),
  93. 'label' =>array('type'=>'varchar(128)', 'label'=>'Label', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>30, 'showoncombobox'=>1),
  94. 'active' =>array('type'=>'integer', 'label'=>'Active', 'enabled'=>1, 'visible'=>-1, 'position'=>35),
  95. 'use_default' =>array('type'=>'integer', 'label'=>'Usedefault', 'enabled'=>1, 'visible'=>-1, 'position'=>40),
  96. 'description' =>array('type'=>'varchar(255)', 'label'=>'Description', 'enabled'=>1, 'visible'=>-1, 'position'=>45),
  97. 'fk_parent' =>array('type'=>'integer', 'label'=>'Fkparent', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>50),
  98. 'force_severity' =>array('type'=>'varchar(32)', 'label'=>'Forceseverity', 'enabled'=>1, 'visible'=>-1, 'position'=>55),
  99. 'public' =>array('type'=>'integer', 'label'=>'Public', 'enabled'=>1, 'visible'=>-1, 'position'=>60),
  100. );
  101. /**
  102. * @var int ID
  103. */
  104. public $rowid;
  105. /**
  106. * @var string Ref
  107. */
  108. public $ref;
  109. /**
  110. * @var int Entity
  111. */
  112. public $entity;
  113. /**
  114. * @var string label
  115. */
  116. public $label;
  117. /**
  118. * @var string amount
  119. */
  120. public $amount;
  121. /**
  122. * @var int Status
  123. */
  124. public $status;
  125. /**
  126. * @var integer|string date_creation
  127. */
  128. public $date_creation;
  129. /**
  130. * @var integer tms
  131. */
  132. public $tms;
  133. /**
  134. * @var int ID
  135. */
  136. public $fk_user_creat;
  137. /**
  138. * @var int ID
  139. */
  140. public $fk_user_modif;
  141. /**
  142. * @var string public $last_main_doc
  143. */
  144. public $last_main_doc;
  145. /**
  146. * @var string import_key
  147. */
  148. public $import_key;
  149. // END MODULEBUILDER PROPERTIES
  150. /**
  151. * Constructor
  152. *
  153. * @param DoliDb $db Database handler
  154. */
  155. public function __construct(DoliDB $db)
  156. {
  157. global $conf, $langs;
  158. $this->db = $db;
  159. if (empty($conf->global->MAIN_SHOW_TECHNICAL_ID) && isset($this->fields['rowid'])) {
  160. $this->fields['rowid']['visible'] = 0;
  161. }
  162. if (empty($conf->multicompany->enabled) && isset($this->fields['entity'])) {
  163. $this->fields['entity']['enabled'] = 0;
  164. }
  165. // Example to show how to set values of fields definition dynamically
  166. /*if ($user->rights->mymodule->myobject->read) {
  167. $this->fields['myfield']['visible'] = 1;
  168. $this->fields['myfield']['noteditable'] = 0;
  169. }*/
  170. // Unset fields that are disabled
  171. foreach ($this->fields as $key => $val) {
  172. if (isset($val['enabled']) && empty($val['enabled'])) {
  173. unset($this->fields[$key]);
  174. }
  175. }
  176. // Translate some data of arrayofkeyval
  177. if (is_object($langs)) {
  178. foreach ($this->fields as $key => $val) {
  179. if (!empty($val['arrayofkeyval']) && is_array($val['arrayofkeyval'])) {
  180. foreach ($val['arrayofkeyval'] as $key2 => $val2) {
  181. $this->fields[$key]['arrayofkeyval'][$key2] = $langs->trans($val2);
  182. }
  183. }
  184. }
  185. }
  186. }
  187. /**
  188. * Create object into database
  189. *
  190. * @param User $user User that creates
  191. * @param bool $notrigger false=launch triggers after, true=disable triggers
  192. * @return int <0 if KO, Id of created object if OK
  193. */
  194. public function create(User $user, $notrigger = false)
  195. {
  196. $resultcreate = $this->createCommon($user, $notrigger);
  197. //$resultvalidate = $this->validate($user, $notrigger);
  198. return $resultcreate;
  199. }
  200. /**
  201. * Clone an object into another one
  202. *
  203. * @param User $user User that creates
  204. * @param int $fromid Id of object to clone
  205. * @return mixed New object created, <0 if KO
  206. */
  207. public function createFromClone(User $user, $fromid)
  208. {
  209. global $langs, $extrafields;
  210. $error = 0;
  211. dol_syslog(__METHOD__, LOG_DEBUG);
  212. $object = new self($this->db);
  213. $this->db->begin();
  214. // Load source object
  215. $result = $object->fetchCommon($fromid);
  216. if ($result > 0 && !empty($object->table_element_line)) {
  217. $object->fetchLines();
  218. }
  219. // get lines so they will be clone
  220. //foreach($this->lines as $line)
  221. // $line->fetch_optionals();
  222. // Reset some properties
  223. unset($object->id);
  224. unset($object->fk_user_creat);
  225. unset($object->import_key);
  226. // Clear fields
  227. if (property_exists($object, 'ref')) {
  228. $object->ref = empty($this->fields['ref']['default']) ? "Copy_Of_".$object->ref : $this->fields['ref']['default'];
  229. }
  230. if (property_exists($object, 'label')) {
  231. $object->label = empty($this->fields['label']['default']) ? $langs->trans("CopyOf")." ".$object->label : $this->fields['label']['default'];
  232. }
  233. if (property_exists($object, 'status')) {
  234. $object->status = self::STATUS_DRAFT;
  235. }
  236. if (property_exists($object, 'date_creation')) {
  237. $object->date_creation = dol_now();
  238. }
  239. if (property_exists($object, 'date_modification')) {
  240. $object->date_modification = null;
  241. }
  242. // ...
  243. // Clear extrafields that are unique
  244. if (is_array($object->array_options) && count($object->array_options) > 0) {
  245. $extrafields->fetch_name_optionals_label($this->table_element);
  246. foreach ($object->array_options as $key => $option) {
  247. $shortkey = preg_replace('/options_/', '', $key);
  248. if (!empty($extrafields->attributes[$this->table_element]['unique'][$shortkey])) {
  249. //var_dump($key);
  250. //var_dump($clonedObj->array_options[$key]); exit;
  251. unset($object->array_options[$key]);
  252. }
  253. }
  254. }
  255. // Create clone
  256. $object->context['createfromclone'] = 'createfromclone';
  257. $result = $object->createCommon($user);
  258. if ($result < 0) {
  259. $error++;
  260. $this->error = $object->error;
  261. $this->errors = $object->errors;
  262. }
  263. if (!$error) {
  264. // copy internal contacts
  265. if ($this->copy_linked_contact($object, 'internal') < 0) {
  266. $error++;
  267. }
  268. }
  269. if (!$error) {
  270. // copy external contacts if same company
  271. if (property_exists($this, 'fk_soc') && $this->fk_soc == $object->socid) {
  272. if ($this->copy_linked_contact($object, 'external') < 0) {
  273. $error++;
  274. }
  275. }
  276. }
  277. unset($object->context['createfromclone']);
  278. // End
  279. if (!$error) {
  280. $this->db->commit();
  281. return $object;
  282. } else {
  283. $this->db->rollback();
  284. return -1;
  285. }
  286. }
  287. /**
  288. * Load object in memory from the database
  289. *
  290. * @param int $id Id object
  291. * @param string $ref Ref
  292. * @return int <0 if KO, 0 if not found, >0 if OK
  293. */
  294. public function fetch($id, $ref = null)
  295. {
  296. $result = $this->fetchCommon($id, $ref);
  297. if ($result > 0 && !empty($this->table_element_line)) {
  298. $this->fetchLines();
  299. }
  300. return $result;
  301. }
  302. /**
  303. * Load object lines in memory from the database
  304. *
  305. * @return int <0 if KO, 0 if not found, >0 if OK
  306. */
  307. public function fetchLines()
  308. {
  309. $this->lines = array();
  310. $result = $this->fetchLinesCommon();
  311. return $result;
  312. }
  313. /**
  314. * Load list of objects in memory from the database.
  315. *
  316. * @param string $sortorder Sort Order
  317. * @param string $sortfield Sort field
  318. * @param int $limit limit
  319. * @param int $offset Offset
  320. * @param array $filter Filter array. Example array('field'=>'valueforlike', 'customurl'=>...)
  321. * @param string $filtermode Filter mode (AND or OR)
  322. * @return array|int int <0 if KO, array of pages if OK
  323. */
  324. public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND')
  325. {
  326. global $conf;
  327. dol_syslog(__METHOD__, LOG_DEBUG);
  328. $records = array();
  329. $sql = 'SELECT ';
  330. $sql .= $this->getFieldList('t');
  331. $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
  332. if (isset($this->ismultientitymanaged) && $this->ismultientitymanaged == 1) {
  333. $sql .= ' WHERE t.entity IN ('.getEntity($this->table_element).')';
  334. } else {
  335. $sql .= ' WHERE 1 = 1';
  336. }
  337. // Manage filter
  338. $sqlwhere = array();
  339. if (count($filter) > 0) {
  340. foreach ($filter as $key => $value) {
  341. if ($key == 't.rowid') {
  342. $sqlwhere[] = $key." = ".((int) $value);
  343. } elseif (in_array($this->fields[$key]['type'], array('date', 'datetime', 'timestamp'))) {
  344. $sqlwhere[] = $key." = '".$this->db->idate($value)."'";
  345. } elseif ($key == 'customsql') {
  346. $sqlwhere[] = $value;
  347. } elseif (strpos($value, '%') === false) {
  348. $sqlwhere[] = $key." IN (".$this->db->sanitize($this->db->escape($value)).")";
  349. } else {
  350. $sqlwhere[] = $key." LIKE '%".$this->db->escape($value)."%'";
  351. }
  352. }
  353. }
  354. if (count($sqlwhere) > 0) {
  355. $sql .= ' AND ('.implode(' '.$this->db->escape($filtermode).' ', $sqlwhere).')';
  356. }
  357. if (!empty($sortfield)) {
  358. $sql .= $this->db->order($sortfield, $sortorder);
  359. }
  360. if (!empty($limit)) {
  361. $sql .= $this->db->plimit($limit, $offset);
  362. }
  363. $resql = $this->db->query($sql);
  364. if ($resql) {
  365. $num = $this->db->num_rows($resql);
  366. $i = 0;
  367. while ($i < ($limit ? min($limit, $num) : $num)) {
  368. $obj = $this->db->fetch_object($resql);
  369. $record = new self($this->db);
  370. $record->setVarsFromFetchObj($obj);
  371. $records[$record->id] = $record;
  372. $i++;
  373. }
  374. $this->db->free($resql);
  375. return $records;
  376. } else {
  377. $this->errors[] = 'Error '.$this->db->lasterror();
  378. dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
  379. return -1;
  380. }
  381. }
  382. /**
  383. * Update object into database
  384. *
  385. * @param User $user User that modifies
  386. * @param bool $notrigger false=launch triggers after, true=disable triggers
  387. * @return int <0 if KO, >0 if OK
  388. */
  389. public function update(User $user, $notrigger = false)
  390. {
  391. return $this->updateCommon($user, $notrigger);
  392. }
  393. /**
  394. * Delete object in database
  395. *
  396. * @param User $user User that deletes
  397. * @param bool $notrigger false=launch triggers after, true=disable triggers
  398. * @return int <0 if KO, >0 if OK
  399. */
  400. public function delete(User $user, $notrigger = false)
  401. {
  402. return $this->deleteCommon($user, $notrigger);
  403. //return $this->deleteCommon($user, $notrigger, 1);
  404. }
  405. /**
  406. * Delete a line of object in database
  407. *
  408. * @param User $user User that delete
  409. * @param int $idline Id of line to delete
  410. * @param bool $notrigger false=launch triggers after, true=disable triggers
  411. * @return int >0 if OK, <0 if KO
  412. */
  413. public function deleteLine(User $user, $idline, $notrigger = false)
  414. {
  415. if ($this->status < 0) {
  416. $this->error = 'ErrorDeleteLineNotAllowedByObjectStatus';
  417. return -2;
  418. }
  419. return $this->deleteLineCommon($user, $idline, $notrigger);
  420. }
  421. /**
  422. * Validate object
  423. *
  424. * @param User $user User making status change
  425. * @param int $notrigger 1=Does not execute triggers, 0= execute triggers
  426. * @return int <=0 if OK, 0=Nothing done, >0 if KO
  427. */
  428. public function validate($user, $notrigger = 0)
  429. {
  430. global $conf, $langs;
  431. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  432. $error = 0;
  433. // Protection
  434. if ($this->status == self::STATUS_VALIDATED) {
  435. dol_syslog(get_class($this)."::validate action abandonned: already validated", LOG_WARNING);
  436. return 0;
  437. }
  438. /*if (! ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->mymodule->myobject->write))
  439. || (! empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->mymodule->myobject->myobject_advance->validate))))
  440. {
  441. $this->error='NotEnoughPermissions';
  442. dol_syslog(get_class($this)."::valid ".$this->error, LOG_ERR);
  443. return -1;
  444. }*/
  445. $now = dol_now();
  446. $this->db->begin();
  447. // Define new ref
  448. if (!$error && (preg_match('/^[\(]?PROV/i', $this->ref) || empty($this->ref))) { // empty should not happened, but when it occurs, the test save life
  449. $num = $this->getNextNumRef();
  450. } else {
  451. $num = $this->ref;
  452. }
  453. $this->newref = $num;
  454. if (!empty($num)) {
  455. // Validate
  456. $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element;
  457. $sql .= " SET ref = '".$this->db->escape($num)."',";
  458. $sql .= " status = ".self::STATUS_VALIDATED;
  459. if (!empty($this->fields['date_validation'])) {
  460. $sql .= ", date_validation = '".$this->db->idate($now)."'";
  461. }
  462. if (!empty($this->fields['fk_user_valid'])) {
  463. $sql .= ", fk_user_valid = ".((int) $user->id);
  464. }
  465. $sql .= " WHERE rowid = ".((int) $this->id);
  466. dol_syslog(get_class($this)."::validate()", LOG_DEBUG);
  467. $resql = $this->db->query($sql);
  468. if (!$resql) {
  469. dol_print_error($this->db);
  470. $this->error = $this->db->lasterror();
  471. $error++;
  472. }
  473. if (!$error && !$notrigger) {
  474. // Call trigger
  475. $result = $this->call_trigger('CTICKETCATEGORY_VALIDATE', $user);
  476. if ($result < 0) {
  477. $error++;
  478. }
  479. // End call triggers
  480. }
  481. }
  482. if (!$error) {
  483. $this->oldref = $this->ref;
  484. // Rename directory if dir was a temporary ref
  485. if (preg_match('/^[\(]?PROV/i', $this->ref)) {
  486. // Now we rename also files into index
  487. $sql = 'UPDATE '.MAIN_DB_PREFIX."ecm_files set filename = CONCAT('".$this->db->escape($this->newref)."', SUBSTR(filename, ".(strlen($this->ref) + 1).")), filepath = 'myobject/".$this->db->escape($this->newref)."'";
  488. $sql .= " WHERE filename LIKE '".$this->db->escape($this->ref)."%' AND filepath = 'myobject/".$this->db->escape($this->ref)."' and entity = ".$conf->entity;
  489. $resql = $this->db->query($sql);
  490. if (!$resql) {
  491. $error++; $this->error = $this->db->lasterror();
  492. }
  493. // We rename directory ($this->ref = old ref, $num = new ref) in order not to lose the attachments
  494. $oldref = dol_sanitizeFileName($this->ref);
  495. $newref = dol_sanitizeFileName($num);
  496. $dirsource = $conf->mymodule->dir_output.'/myobject/'.$oldref;
  497. $dirdest = $conf->mymodule->dir_output.'/myobject/'.$newref;
  498. if (!$error && file_exists($dirsource)) {
  499. dol_syslog(get_class($this)."::validate() rename dir ".$dirsource." into ".$dirdest);
  500. if (@rename($dirsource, $dirdest)) {
  501. dol_syslog("Rename ok");
  502. // Rename docs starting with $oldref with $newref
  503. $listoffiles = dol_dir_list($conf->mymodule->dir_output.'/myobject/'.$newref, 'files', 1, '^'.preg_quote($oldref, '/'));
  504. foreach ($listoffiles as $fileentry) {
  505. $dirsource = $fileentry['name'];
  506. $dirdest = preg_replace('/^'.preg_quote($oldref, '/').'/', $newref, $dirsource);
  507. $dirsource = $fileentry['path'].'/'.$dirsource;
  508. $dirdest = $fileentry['path'].'/'.$dirdest;
  509. @rename($dirsource, $dirdest);
  510. }
  511. }
  512. }
  513. }
  514. }
  515. // Set new ref and current status
  516. if (!$error) {
  517. $this->ref = $num;
  518. $this->status = self::STATUS_VALIDATED;
  519. }
  520. if (!$error) {
  521. $this->db->commit();
  522. return 1;
  523. } else {
  524. $this->db->rollback();
  525. return -1;
  526. }
  527. }
  528. /**
  529. * Set cancel status
  530. *
  531. * @param User $user Object user that modify
  532. * @param int $notrigger 1=Does not execute triggers, 0=Execute triggers
  533. * @return int <0 if KO, 0=Nothing done, >0 if OK
  534. */
  535. public function cancel($user, $notrigger = 0)
  536. {
  537. // Protection
  538. if ($this->status != self::STATUS_VALIDATED) {
  539. return 0;
  540. }
  541. /*if (! ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->mymodule->write))
  542. || (! empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->mymodule->mymodule_advance->validate))))
  543. {
  544. $this->error='Permission denied';
  545. return -1;
  546. }*/
  547. return $this->setStatusCommon($user, self::STATUS_DISABLED, $notrigger, 'CTICKETCATEGORY_CANCEL');
  548. }
  549. /**
  550. * Return a link to the object card (with optionaly the picto)
  551. *
  552. * @param int $withpicto Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto)
  553. * @param string $option On what the link point to ('nolink', ...)
  554. * @param int $notooltip 1=Disable tooltip
  555. * @param string $morecss Add more css on link
  556. * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
  557. * @return string String with URL
  558. */
  559. public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
  560. {
  561. global $conf, $langs, $hookmanager;
  562. if (!empty($conf->dol_no_mouse_hover)) {
  563. $notooltip = 1; // Force disable tooltips
  564. }
  565. $result = '';
  566. /*$label = img_picto('', $this->picto).' <u>'.$langs->trans("MyObject").'</u>';
  567. if (isset($this->status)) {
  568. $label .= ' '.$this->getLibStatut(5);
  569. }
  570. $label .= '<br>';
  571. $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
  572. */
  573. $label = '';
  574. //$url = dol_buildpath('/mymodule/myobject_card.php', 1).'?id='.$this->id;
  575. $url = '';
  576. if ($option != 'nolink') {
  577. // Add param to save lastsearch_values or not
  578. $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
  579. if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
  580. $add_save_lastsearch_values = 1;
  581. }
  582. if ($add_save_lastsearch_values) {
  583. $url .= '&save_lastsearch_values=1';
  584. }
  585. }
  586. $linkclose = '';
  587. if (empty($notooltip)) {
  588. if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
  589. $label = $langs->trans("ShowMyObject");
  590. $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
  591. }
  592. $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
  593. $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
  594. } else {
  595. $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
  596. }
  597. if ($option == 'nolink') {
  598. $linkstart = '<span';
  599. } else {
  600. $linkstart = '<a href="'.$url.'"';
  601. }
  602. $linkstart .= $linkclose.'>';
  603. if ($option == 'nolink') {
  604. $linkend = '</span>';
  605. } else {
  606. $linkend = '</a>';
  607. }
  608. //$result .= $linkstart;
  609. $result .= $this->label;
  610. //$result .= $linkend;
  611. global $action, $hookmanager;
  612. $hookmanager->initHooks(array('cticketcategorydao'));
  613. $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
  614. $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
  615. if ($reshook > 0) {
  616. $result = $hookmanager->resPrint;
  617. } else {
  618. $result .= $hookmanager->resPrint;
  619. }
  620. return $result;
  621. }
  622. /**
  623. * Return the label of the status
  624. *
  625. * @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
  626. * @return string Label of status
  627. */
  628. public function getLibStatut($mode = 0)
  629. {
  630. return $this->LibStatut($this->status, $mode);
  631. }
  632. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  633. /**
  634. * Return the status
  635. *
  636. * @param int $status Id status
  637. * @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
  638. * @return string Label of status
  639. */
  640. public function LibStatut($status, $mode = 0)
  641. {
  642. // phpcs:enable
  643. if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
  644. global $langs;
  645. //$langs->load("mymodule@mymodule");
  646. $this->labelStatus[self::STATUS_ENABLED] = $langs->transnoentitiesnoconv('Enabled');
  647. $this->labelStatus[self::STATUS_DISABLED] = $langs->transnoentitiesnoconv('Disabled');
  648. $this->labelStatusShort[self::STATUS_ENABLED] = $langs->transnoentitiesnoconv('Enabled');
  649. $this->labelStatusShort[self::STATUS_DISABLED] = $langs->transnoentitiesnoconv('Disabled');
  650. }
  651. $statusType = 'status'.$status;
  652. //if ($status == self::STATUS_VALIDATED) $statusType = 'status1';
  653. if ($status == self::STATUS_DISABLED) {
  654. $statusType = 'status6';
  655. }
  656. return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
  657. }
  658. /**
  659. * Load the info information in the object
  660. *
  661. * @param int $id Id of object
  662. * @return void
  663. */
  664. public function info($id)
  665. {
  666. $sql = 'SELECT rowid, date_creation as datec, tms as datem,';
  667. $sql .= ' fk_user_creat, fk_user_modif';
  668. $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
  669. $sql .= ' WHERE t.rowid = '.((int) $id);
  670. $result = $this->db->query($sql);
  671. if ($result) {
  672. if ($this->db->num_rows($result)) {
  673. $obj = $this->db->fetch_object($result);
  674. $this->id = $obj->rowid;
  675. if ($obj->fk_user_author) {
  676. $cuser = new User($this->db);
  677. $cuser->fetch($obj->fk_user_author);
  678. $this->user_creation = $cuser;
  679. }
  680. if ($obj->fk_user_valid) {
  681. $vuser = new User($this->db);
  682. $vuser->fetch($obj->fk_user_valid);
  683. $this->user_validation = $vuser;
  684. }
  685. if ($obj->fk_user_cloture) {
  686. $cluser = new User($this->db);
  687. $cluser->fetch($obj->fk_user_cloture);
  688. $this->user_cloture = $cluser;
  689. }
  690. $this->date_creation = $this->db->jdate($obj->datec);
  691. $this->date_modification = $this->db->jdate($obj->datem);
  692. $this->date_validation = $this->db->jdate($obj->datev);
  693. }
  694. $this->db->free($result);
  695. } else {
  696. dol_print_error($this->db);
  697. }
  698. }
  699. /**
  700. * Initialise object with example values
  701. * Id must be 0 if object instance is a specimen
  702. *
  703. * @return void
  704. */
  705. public function initAsSpecimen()
  706. {
  707. $this->initAsSpecimenCommon();
  708. }
  709. }