myobject.class.php 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144
  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 MyObject extends CommonObject
  31. {
  32. /**
  33. * @var string ID of module.
  34. */
  35. public $module = 'mymodule';
  36. /**
  37. * @var string ID to identify managed object.
  38. */
  39. public $element = 'myobject';
  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 = 'mymodule_myobject';
  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 = 1;
  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 = 'myobject@mymodule';
  57. const STATUS_DRAFT = 0;
  58. const STATUS_VALIDATED = 1;
  59. const STATUS_CANCELED = 9;
  60. /**
  61. * 'type' field format ('integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter[:Sortfield]]]', 'sellist:TableName:LabelFieldName[:KeyFieldName[:KeyFieldParent[:Filter[:Sortfield]]]]', 'varchar(x)', 'double(24,8)', 'real', 'price', 'text', 'text:none', 'html', 'date', 'datetime', 'timestamp', 'duration', 'mail', 'phone', 'url', 'password')
  62. * Note: Filter can be a string like "(t.ref:like:'SO-%') or (t.date_creation:<:'20160101') or (t.nature:is:NULL)"
  63. * 'label' the translation key.
  64. * 'picto' is code of a picto to show before value in forms
  65. * 'enabled' is a condition when the field must be managed (Example: 1 or '$conf->global->MY_SETUP_PARAM' or '!empty($conf->multicurrency->enabled)' ...)
  66. * 'position' is the sort order of field.
  67. * 'notnull' is set to 1 if not null in database. Set to -1 if we must set data to null if empty ('' or 0).
  68. * '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)
  69. * 'noteditable' says if field is not editable (1 or 0)
  70. * '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.
  71. * 'index' if we want an index in database.
  72. * 'foreignkey'=>'tablename.field' if the field is a foreign key (it is recommanded to name the field fk_...).
  73. * 'searchall' is 1 if we want to search in this field when making a search from the quick search button.
  74. * 'isameasure' must be set to 1 or 2 if field can be used for measure. Field type must be summable like integer or double(24,8). Use 1 in most cases, or 2 if you don't want to see the column total into list (for example for percentage)
  75. * '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: 'css'=>'minwidth300 maxwidth500 widthcentpercentminusx', 'cssview'=>'wordbreak', 'csslist'=>'tdoverflowmax200'
  76. * 'help' is a 'TranslationString' to use to show a tooltip on field. You can also use 'TranslationString:keyfortooltiponlick' for a tooltip on click.
  77. * 'showoncombobox' if value of the field must be visible into the label of the combobox that list record
  78. * '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.
  79. * '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'
  80. * 'autofocusoncreate' to have field having the focus on a create form. Only 1 field should have this property set to 1.
  81. * 'comment' is not used. You can store here any text of your choice. It is not used by application.
  82. * 'validate' is 1 if need to validate with $this->validateField()
  83. * 'copytoclipboard' is 1 or 2 to allow to add a picto to copy value into clipboard (1=picto after label, 2=picto after value)
  84. *
  85. * Note: To have value dynamic, you can set value to 0 in definition and edit the value on the fly into the constructor.
  86. */
  87. // BEGIN MODULEBUILDER PROPERTIES
  88. /**
  89. * @var array Array with all fields and their property. Do not use it as a static var. It may be modified by constructor.
  90. */
  91. public $fields = array(
  92. 'rowid' => array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-2, 'noteditable'=>1, 'notnull'=> 1, 'index'=>1, 'position'=>1, 'comment'=>'Id', 'css'=>'left'),
  93. 'entity' => array('type'=>'integer', 'label'=>'Entity', 'enabled'=>1, 'visible'=>0, 'notnull'=> 1, 'default'=>1, 'index'=>1, 'position'=>10),
  94. 'ref' => array('type'=>'varchar(128)', 'label'=>'Ref', 'enabled'=>1, 'visible'=>1, 'noteditable'=>0, 'default'=>'', 'notnull'=> 1, 'showoncombobox'=>1, 'index'=>1, 'position'=>20, 'searchall'=>1, 'comment'=>'Reference of object', 'validate'=>1),
  95. 'label' => array('type'=>'varchar(255)', 'label'=>'Label', 'enabled'=>1, 'visible'=>1, 'position'=>30, 'searchall'=>1, 'css'=>'minwidth300', 'cssview'=>'wordbreak', 'help'=>'Help text', 'showoncombobox'=>2, 'validate'=>1),
  96. 'amount' => array('type'=>'price', 'label'=>'Amount', 'enabled'=>1, 'visible'=>1, 'default'=>'null', 'position'=>40, 'searchall'=>0, 'isameasure'=>1, 'help'=>'Help text for amount', 'validate'=>1),
  97. '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', 'validate'=>1),
  98. 'fk_soc' => array('type'=>'integer:Societe:societe/class/societe.class.php:1:status=1 AND entity IN (__SHARED_ENTITIES__)', 'picto'=>'company', 'label'=>'ThirdParty', 'visible'=> 1, 'enabled'=>'$conf->societe->enabled', 'position'=>50, 'notnull'=>-1, 'index'=>1, 'help'=>'LinkToThirparty', 'validate'=>1, 'css'=>'maxwidth500 widthcentpercentminusxx'),
  99. 'fk_project' => array('type'=>'integer:Project:projet/class/project.class.php:1', 'label'=>'Project', 'picto'=>'project', 'enabled'=>'$conf->projet->enabled', 'visible'=>-1, 'position'=>52, 'notnull'=>-1, 'index'=>1, 'validate'=>1, 'css'=>'maxwidth500 widthcentpercentminusxx'),
  100. 'description' => array('type'=>'text', 'label'=>'Description', 'enabled'=>1, 'visible'=>3, 'position'=>60, 'validate'=>1),
  101. 'note_public' => array('type'=>'html', 'label'=>'NotePublic', 'enabled'=>1, 'visible'=>0, 'position'=>61, 'validate'=>1, 'cssview'=>'wordbreak'),
  102. 'note_private' => array('type'=>'html', 'label'=>'NotePrivate', 'enabled'=>1, 'visible'=>0, 'position'=>62, 'validate'=>1, 'cssview'=>'wordbreak'),
  103. 'date_creation' => array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-2, 'notnull'=> 1, 'position'=>500),
  104. 'tms' => array('type'=>'timestamp', 'label'=>'DateModification', 'enabled'=>1, 'visible'=>-2, 'notnull'=> 0, 'position'=>501),
  105. //'date_validation ' =>array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-2, 'position'=>502),
  106. 'fk_user_creat' => array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserAuthor', 'picto'=>'user', 'enabled'=>1, 'visible'=>-2, 'notnull'=> 1, 'position'=>510, 'foreignkey'=>'user.rowid'),
  107. 'fk_user_modif' => array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserModif', 'picto'=>'user', 'enabled'=>1, 'visible'=>-2, 'notnull'=>-1, 'position'=>511),
  108. //'fk_user_valid' => array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserValidation', 'enabled'=>1, 'visible'=>-1, 'position'=>512),
  109. 'last_main_doc' => array('type'=>'varchar(255)', 'label'=>'LastMainDoc', 'enabled'=>1, 'visible'=>0, 'notnull'=>0, 'position'=>600),
  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'=>'integer', 'label'=>'Status', 'enabled'=>1, 'visible'=>1, 'notnull'=> 1, 'default'=>0, 'index'=>1, 'position'=>2000, 'arrayofkeyval'=>array(0=>'Draft', 1=>'Validated', 9=>'Canceled'), 'validate'=>1),
  113. );
  114. /**
  115. * @var int ID
  116. */
  117. public $rowid;
  118. /**
  119. * @var string Ref
  120. */
  121. public $ref;
  122. /**
  123. * @var int Entity
  124. */
  125. public $entity;
  126. /**
  127. * @var string label
  128. */
  129. public $label;
  130. /**
  131. * @var string amount
  132. */
  133. public $amount;
  134. /**
  135. * @var int Thirdparty ID
  136. */
  137. public $socid; // both socid and fk_soc are used
  138. public $fk_soc; // both socid and fk_soc are used
  139. /**
  140. * @var int Status
  141. */
  142. public $status;
  143. /**
  144. * @var integer|string date_creation
  145. */
  146. public $date_creation;
  147. /**
  148. * @var integer tms
  149. */
  150. public $tms;
  151. /**
  152. * @var int ID
  153. */
  154. public $fk_user_creat;
  155. /**
  156. * @var int ID
  157. */
  158. public $fk_user_modif;
  159. /**
  160. * @var string public $last_main_doc
  161. */
  162. public $last_main_doc;
  163. /**
  164. * @var string import_key
  165. */
  166. public $import_key;
  167. // END MODULEBUILDER PROPERTIES
  168. // If this object has a subtable with lines
  169. // /**
  170. // * @var string Name of subtable line
  171. // */
  172. // public $table_element_line = 'mymodule_myobjectline';
  173. // /**
  174. // * @var string Field with ID of parent key if this object has a parent
  175. // */
  176. // public $fk_element = 'fk_myobject';
  177. // /**
  178. // * @var string Name of subtable class that manage subtable lines
  179. // */
  180. // public $class_element_line = 'MyObjectline';
  181. // /**
  182. // * @var array List of child tables. To test if we can delete object.
  183. // */
  184. // protected $childtables = array();
  185. // /**
  186. // * @var array List of child tables. To know object to delete on cascade.
  187. // * If name matches '@ClassNAme:FilePathClass;ParentFkFieldName' it will
  188. // * call method deleteByParentField(parentId, ParentFkFieldName) to fetch and delete child object
  189. // */
  190. // protected $childtablesoncascade = array('mymodule_myobjectdet');
  191. // /**
  192. // * @var MyObjectLine[] Array of subtable lines
  193. // */
  194. // public $lines = array();
  195. /**
  196. * Constructor
  197. *
  198. * @param DoliDb $db Database handler
  199. */
  200. public function __construct(DoliDB $db)
  201. {
  202. global $conf, $langs;
  203. $this->db = $db;
  204. if (empty($conf->global->MAIN_SHOW_TECHNICAL_ID) && isset($this->fields['rowid'])) {
  205. $this->fields['rowid']['visible'] = 0;
  206. }
  207. if (empty($conf->multicompany->enabled) && isset($this->fields['entity'])) {
  208. $this->fields['entity']['enabled'] = 0;
  209. }
  210. // Example to show how to set values of fields definition dynamically
  211. /*if ($user->rights->mymodule->myobject->read) {
  212. $this->fields['myfield']['visible'] = 1;
  213. $this->fields['myfield']['noteditable'] = 0;
  214. }*/
  215. // Unset fields that are disabled
  216. foreach ($this->fields as $key => $val) {
  217. if (isset($val['enabled']) && empty($val['enabled'])) {
  218. unset($this->fields[$key]);
  219. }
  220. }
  221. // Translate some data of arrayofkeyval
  222. if (is_object($langs)) {
  223. foreach ($this->fields as $key => $val) {
  224. if (!empty($val['arrayofkeyval']) && is_array($val['arrayofkeyval'])) {
  225. foreach ($val['arrayofkeyval'] as $key2 => $val2) {
  226. $this->fields[$key]['arrayofkeyval'][$key2] = $langs->trans($val2);
  227. }
  228. }
  229. }
  230. }
  231. }
  232. /**
  233. * Create object into database
  234. *
  235. * @param User $user User that creates
  236. * @param bool $notrigger false=launch triggers after, true=disable triggers
  237. * @return int <0 if KO, Id of created object if OK
  238. */
  239. public function create(User $user, $notrigger = false)
  240. {
  241. $resultcreate = $this->createCommon($user, $notrigger);
  242. //$resultvalidate = $this->validate($user, $notrigger);
  243. return $resultcreate;
  244. }
  245. /**
  246. * Clone an object into another one
  247. *
  248. * @param User $user User that creates
  249. * @param int $fromid Id of object to clone
  250. * @return mixed New object created, <0 if KO
  251. */
  252. public function createFromClone(User $user, $fromid)
  253. {
  254. global $langs, $extrafields;
  255. $error = 0;
  256. dol_syslog(__METHOD__, LOG_DEBUG);
  257. $object = new self($this->db);
  258. $this->db->begin();
  259. // Load source object
  260. $result = $object->fetchCommon($fromid);
  261. if ($result > 0 && !empty($object->table_element_line)) {
  262. $object->fetchLines();
  263. }
  264. // get lines so they will be clone
  265. //foreach($this->lines as $line)
  266. // $line->fetch_optionals();
  267. // Reset some properties
  268. unset($object->id);
  269. unset($object->fk_user_creat);
  270. unset($object->import_key);
  271. // Clear fields
  272. if (property_exists($object, 'ref')) {
  273. $object->ref = empty($this->fields['ref']['default']) ? "Copy_Of_".$object->ref : $this->fields['ref']['default'];
  274. }
  275. if (property_exists($object, 'label')) {
  276. $object->label = empty($this->fields['label']['default']) ? $langs->trans("CopyOf")." ".$object->label : $this->fields['label']['default'];
  277. }
  278. if (property_exists($object, 'status')) {
  279. $object->status = self::STATUS_DRAFT;
  280. }
  281. if (property_exists($object, 'date_creation')) {
  282. $object->date_creation = dol_now();
  283. }
  284. if (property_exists($object, 'date_modification')) {
  285. $object->date_modification = null;
  286. }
  287. // ...
  288. // Clear extrafields that are unique
  289. if (is_array($object->array_options) && count($object->array_options) > 0) {
  290. $extrafields->fetch_name_optionals_label($this->table_element);
  291. foreach ($object->array_options as $key => $option) {
  292. $shortkey = preg_replace('/options_/', '', $key);
  293. if (!empty($extrafields->attributes[$this->table_element]['unique'][$shortkey])) {
  294. //var_dump($key);
  295. //var_dump($clonedObj->array_options[$key]); exit;
  296. unset($object->array_options[$key]);
  297. }
  298. }
  299. }
  300. // Create clone
  301. $object->context['createfromclone'] = 'createfromclone';
  302. $result = $object->createCommon($user);
  303. if ($result < 0) {
  304. $error++;
  305. $this->error = $object->error;
  306. $this->errors = $object->errors;
  307. }
  308. if (!$error) {
  309. // copy internal contacts
  310. if ($this->copy_linked_contact($object, 'internal') < 0) {
  311. $error++;
  312. }
  313. }
  314. if (!$error) {
  315. // copy external contacts if same company
  316. if (!empty($object->socid) && property_exists($this, 'fk_soc') && $this->fk_soc == $object->socid) {
  317. if ($this->copy_linked_contact($object, 'external') < 0) {
  318. $error++;
  319. }
  320. }
  321. }
  322. unset($object->context['createfromclone']);
  323. // End
  324. if (!$error) {
  325. $this->db->commit();
  326. return $object;
  327. } else {
  328. $this->db->rollback();
  329. return -1;
  330. }
  331. }
  332. /**
  333. * Load object in memory from the database
  334. *
  335. * @param int $id Id object
  336. * @param string $ref Ref
  337. * @return int <0 if KO, 0 if not found, >0 if OK
  338. */
  339. public function fetch($id, $ref = null)
  340. {
  341. $result = $this->fetchCommon($id, $ref);
  342. if ($result > 0 && !empty($this->table_element_line)) {
  343. $this->fetchLines();
  344. }
  345. return $result;
  346. }
  347. /**
  348. * Load object lines in memory from the database
  349. *
  350. * @return int <0 if KO, 0 if not found, >0 if OK
  351. */
  352. public function fetchLines()
  353. {
  354. $this->lines = array();
  355. $result = $this->fetchLinesCommon();
  356. return $result;
  357. }
  358. /**
  359. * Load list of objects in memory from the database.
  360. *
  361. * @param string $sortorder Sort Order
  362. * @param string $sortfield Sort field
  363. * @param int $limit limit
  364. * @param int $offset Offset
  365. * @param array $filter Filter array. Example array('field'=>'valueforlike', 'customurl'=>...)
  366. * @param string $filtermode Filter mode (AND or OR)
  367. * @return array|int int <0 if KO, array of pages if OK
  368. */
  369. public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND')
  370. {
  371. global $conf;
  372. dol_syslog(__METHOD__, LOG_DEBUG);
  373. $records = array();
  374. $sql = "SELECT ";
  375. $sql .= $this->getFieldList('t');
  376. $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element." as t";
  377. if (isset($this->ismultientitymanaged) && $this->ismultientitymanaged == 1) {
  378. $sql .= " WHERE t.entity IN (".getEntity($this->table_element).")";
  379. } else {
  380. $sql .= " WHERE 1 = 1";
  381. }
  382. // Manage filter
  383. $sqlwhere = array();
  384. if (count($filter) > 0) {
  385. foreach ($filter as $key => $value) {
  386. if ($key == 't.rowid') {
  387. $sqlwhere[] = $key." = ".((int) $value);
  388. } elseif (in_array($this->fields[$key]['type'], array('date', 'datetime', 'timestamp'))) {
  389. $sqlwhere[] = $key." = '".$this->db->idate($value)."'";
  390. } elseif ($key == 'customsql') {
  391. $sqlwhere[] = $value;
  392. } elseif (strpos($value, '%') === false) {
  393. $sqlwhere[] = $key." IN (".$this->db->sanitize($this->db->escape($value)).")";
  394. } else {
  395. $sqlwhere[] = $key." LIKE '%".$this->db->escape($value)."%'";
  396. }
  397. }
  398. }
  399. if (count($sqlwhere) > 0) {
  400. $sql .= " AND (".implode(" ".$filtermode." ", $sqlwhere).")";
  401. }
  402. if (!empty($sortfield)) {
  403. $sql .= $this->db->order($sortfield, $sortorder);
  404. }
  405. if (!empty($limit)) {
  406. $sql .= $this->db->plimit($limit, $offset);
  407. }
  408. $resql = $this->db->query($sql);
  409. if ($resql) {
  410. $num = $this->db->num_rows($resql);
  411. $i = 0;
  412. while ($i < ($limit ? min($limit, $num) : $num)) {
  413. $obj = $this->db->fetch_object($resql);
  414. $record = new self($this->db);
  415. $record->setVarsFromFetchObj($obj);
  416. $records[$record->id] = $record;
  417. $i++;
  418. }
  419. $this->db->free($resql);
  420. return $records;
  421. } else {
  422. $this->errors[] = 'Error '.$this->db->lasterror();
  423. dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
  424. return -1;
  425. }
  426. }
  427. /**
  428. * Update object into database
  429. *
  430. * @param User $user User that modifies
  431. * @param bool $notrigger false=launch triggers after, true=disable triggers
  432. * @return int <0 if KO, >0 if OK
  433. */
  434. public function update(User $user, $notrigger = false)
  435. {
  436. return $this->updateCommon($user, $notrigger);
  437. }
  438. /**
  439. * Delete object in database
  440. *
  441. * @param User $user User that deletes
  442. * @param bool $notrigger false=launch triggers after, true=disable triggers
  443. * @return int <0 if KO, >0 if OK
  444. */
  445. public function delete(User $user, $notrigger = false)
  446. {
  447. return $this->deleteCommon($user, $notrigger);
  448. //return $this->deleteCommon($user, $notrigger, 1);
  449. }
  450. /**
  451. * Delete a line of object in database
  452. *
  453. * @param User $user User that delete
  454. * @param int $idline Id of line to delete
  455. * @param bool $notrigger false=launch triggers after, true=disable triggers
  456. * @return int >0 if OK, <0 if KO
  457. */
  458. public function deleteLine(User $user, $idline, $notrigger = false)
  459. {
  460. if ($this->status < 0) {
  461. $this->error = 'ErrorDeleteLineNotAllowedByObjectStatus';
  462. return -2;
  463. }
  464. return $this->deleteLineCommon($user, $idline, $notrigger);
  465. }
  466. /**
  467. * Validate object
  468. *
  469. * @param User $user User making status change
  470. * @param int $notrigger 1=Does not execute triggers, 0= execute triggers
  471. * @return int <=0 if OK, 0=Nothing done, >0 if KO
  472. */
  473. public function validate($user, $notrigger = 0)
  474. {
  475. global $conf, $langs;
  476. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  477. $error = 0;
  478. // Protection
  479. if ($this->status == self::STATUS_VALIDATED) {
  480. dol_syslog(get_class($this)."::validate action abandonned: already validated", LOG_WARNING);
  481. return 0;
  482. }
  483. /*if (! ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->mymodule->myobject->write))
  484. || (! empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->mymodule->myobject->myobject_advance->validate))))
  485. {
  486. $this->error='NotEnoughPermissions';
  487. dol_syslog(get_class($this)."::valid ".$this->error, LOG_ERR);
  488. return -1;
  489. }*/
  490. $now = dol_now();
  491. $this->db->begin();
  492. // Define new ref
  493. if (!$error && (preg_match('/^[\(]?PROV/i', $this->ref) || empty($this->ref))) { // empty should not happened, but when it occurs, the test save life
  494. $num = $this->getNextNumRef();
  495. } else {
  496. $num = $this->ref;
  497. }
  498. $this->newref = $num;
  499. if (!empty($num)) {
  500. // Validate
  501. $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element;
  502. $sql .= " SET ref = '".$this->db->escape($num)."',";
  503. $sql .= " status = ".self::STATUS_VALIDATED;
  504. if (!empty($this->fields['date_validation'])) {
  505. $sql .= ", date_validation = '".$this->db->idate($now)."'";
  506. }
  507. if (!empty($this->fields['fk_user_valid'])) {
  508. $sql .= ", fk_user_valid = ".((int) $user->id);
  509. }
  510. $sql .= " WHERE rowid = ".((int) $this->id);
  511. dol_syslog(get_class($this)."::validate()", LOG_DEBUG);
  512. $resql = $this->db->query($sql);
  513. if (!$resql) {
  514. dol_print_error($this->db);
  515. $this->error = $this->db->lasterror();
  516. $error++;
  517. }
  518. if (!$error && !$notrigger) {
  519. // Call trigger
  520. $result = $this->call_trigger('MYOBJECT_VALIDATE', $user);
  521. if ($result < 0) {
  522. $error++;
  523. }
  524. // End call triggers
  525. }
  526. }
  527. if (!$error) {
  528. $this->oldref = $this->ref;
  529. // Rename directory if dir was a temporary ref
  530. if (preg_match('/^[\(]?PROV/i', $this->ref)) {
  531. // Now we rename also files into index
  532. $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)."'";
  533. $sql .= " WHERE filename LIKE '".$this->db->escape($this->ref)."%' AND filepath = 'myobject/".$this->db->escape($this->ref)."' and entity = ".$conf->entity;
  534. $resql = $this->db->query($sql);
  535. if (!$resql) {
  536. $error++; $this->error = $this->db->lasterror();
  537. }
  538. // We rename directory ($this->ref = old ref, $num = new ref) in order not to lose the attachments
  539. $oldref = dol_sanitizeFileName($this->ref);
  540. $newref = dol_sanitizeFileName($num);
  541. $dirsource = $conf->mymodule->dir_output.'/myobject/'.$oldref;
  542. $dirdest = $conf->mymodule->dir_output.'/myobject/'.$newref;
  543. if (!$error && file_exists($dirsource)) {
  544. dol_syslog(get_class($this)."::validate() rename dir ".$dirsource." into ".$dirdest);
  545. if (@rename($dirsource, $dirdest)) {
  546. dol_syslog("Rename ok");
  547. // Rename docs starting with $oldref with $newref
  548. $listoffiles = dol_dir_list($conf->mymodule->dir_output.'/myobject/'.$newref, 'files', 1, '^'.preg_quote($oldref, '/'));
  549. foreach ($listoffiles as $fileentry) {
  550. $dirsource = $fileentry['name'];
  551. $dirdest = preg_replace('/^'.preg_quote($oldref, '/').'/', $newref, $dirsource);
  552. $dirsource = $fileentry['path'].'/'.$dirsource;
  553. $dirdest = $fileentry['path'].'/'.$dirdest;
  554. @rename($dirsource, $dirdest);
  555. }
  556. }
  557. }
  558. }
  559. }
  560. // Set new ref and current status
  561. if (!$error) {
  562. $this->ref = $num;
  563. $this->status = self::STATUS_VALIDATED;
  564. }
  565. if (!$error) {
  566. $this->db->commit();
  567. return 1;
  568. } else {
  569. $this->db->rollback();
  570. return -1;
  571. }
  572. }
  573. /**
  574. * Set draft status
  575. *
  576. * @param User $user Object user that modify
  577. * @param int $notrigger 1=Does not execute triggers, 0=Execute triggers
  578. * @return int <0 if KO, >0 if OK
  579. */
  580. public function setDraft($user, $notrigger = 0)
  581. {
  582. // Protection
  583. if ($this->status <= self::STATUS_DRAFT) {
  584. return 0;
  585. }
  586. /*if (! ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->mymodule->write))
  587. || (! empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->mymodule->mymodule_advance->validate))))
  588. {
  589. $this->error='Permission denied';
  590. return -1;
  591. }*/
  592. return $this->setStatusCommon($user, self::STATUS_DRAFT, $notrigger, 'MYOBJECT_UNVALIDATE');
  593. }
  594. /**
  595. * Set cancel status
  596. *
  597. * @param User $user Object user that modify
  598. * @param int $notrigger 1=Does not execute triggers, 0=Execute triggers
  599. * @return int <0 if KO, 0=Nothing done, >0 if OK
  600. */
  601. public function cancel($user, $notrigger = 0)
  602. {
  603. // Protection
  604. if ($this->status != self::STATUS_VALIDATED) {
  605. return 0;
  606. }
  607. /*if (! ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->mymodule->write))
  608. || (! empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->mymodule->mymodule_advance->validate))))
  609. {
  610. $this->error='Permission denied';
  611. return -1;
  612. }*/
  613. return $this->setStatusCommon($user, self::STATUS_CANCELED, $notrigger, 'MYOBJECT_CANCEL');
  614. }
  615. /**
  616. * Set back to validated status
  617. *
  618. * @param User $user Object user that modify
  619. * @param int $notrigger 1=Does not execute triggers, 0=Execute triggers
  620. * @return int <0 if KO, 0=Nothing done, >0 if OK
  621. */
  622. public function reopen($user, $notrigger = 0)
  623. {
  624. // Protection
  625. if ($this->status != self::STATUS_CANCELED) {
  626. return 0;
  627. }
  628. /*if (! ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->mymodule->write))
  629. || (! empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->mymodule->mymodule_advance->validate))))
  630. {
  631. $this->error='Permission denied';
  632. return -1;
  633. }*/
  634. return $this->setStatusCommon($user, self::STATUS_VALIDATED, $notrigger, 'MYOBJECT_REOPEN');
  635. }
  636. /**
  637. * Return a link to the object card (with optionaly the picto)
  638. *
  639. * @param int $withpicto Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto)
  640. * @param string $option On what the link point to ('nolink', ...)
  641. * @param int $notooltip 1=Disable tooltip
  642. * @param string $morecss Add more css on link
  643. * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
  644. * @return string String with URL
  645. */
  646. public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
  647. {
  648. global $conf, $langs, $hookmanager;
  649. if (!empty($conf->dol_no_mouse_hover)) {
  650. $notooltip = 1; // Force disable tooltips
  651. }
  652. $result = '';
  653. $label = img_picto('', $this->picto).' <u>'.$langs->trans("MyObject").'</u>';
  654. if (isset($this->status)) {
  655. $label .= ' '.$this->getLibStatut(5);
  656. }
  657. $label .= '<br>';
  658. $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
  659. $url = dol_buildpath('/mymodule/myobject_card.php', 1).'?id='.$this->id;
  660. if ($option != 'nolink') {
  661. // Add param to save lastsearch_values or not
  662. $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
  663. if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
  664. $add_save_lastsearch_values = 1;
  665. }
  666. if ($url && $add_save_lastsearch_values) {
  667. $url .= '&save_lastsearch_values=1';
  668. }
  669. }
  670. $linkclose = '';
  671. if (empty($notooltip)) {
  672. if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
  673. $label = $langs->trans("ShowMyObject");
  674. $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
  675. }
  676. $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
  677. $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
  678. } else {
  679. $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
  680. }
  681. if ($option == 'nolink' || empty($url)) {
  682. $linkstart = '<span';
  683. } else {
  684. $linkstart = '<a href="'.$url.'"';
  685. }
  686. $linkstart .= $linkclose.'>';
  687. if ($option == 'nolink' || empty($url)) {
  688. $linkend = '</span>';
  689. } else {
  690. $linkend = '</a>';
  691. }
  692. $result .= $linkstart;
  693. if (empty($this->showphoto_on_popup)) {
  694. if ($withpicto) {
  695. $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);
  696. }
  697. } else {
  698. if ($withpicto) {
  699. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  700. list($class, $module) = explode('@', $this->picto);
  701. $upload_dir = $conf->$module->multidir_output[$conf->entity]."/$class/".dol_sanitizeFileName($this->ref);
  702. $filearray = dol_dir_list($upload_dir, "files");
  703. $filename = $filearray[0]['name'];
  704. if (!empty($filename)) {
  705. $pospoint = strpos($filearray[0]['name'], '.');
  706. $pathtophoto = $class.'/'.$this->ref.'/thumbs/'.substr($filename, 0, $pospoint).'_mini'.substr($filename, $pospoint);
  707. if (empty($conf->global->{strtoupper($module.'_'.$class).'_FORMATLISTPHOTOSASUSERS'})) {
  708. $result .= '<div class="floatleft inline-block valignmiddle divphotoref"><div class="photoref"><img class="photo'.$module.'" alt="No photo" border="0" src="'.DOL_URL_ROOT.'/viewimage.php?modulepart='.$module.'&entity='.$conf->entity.'&file='.urlencode($pathtophoto).'"></div></div>';
  709. } else {
  710. $result .= '<div class="floatleft inline-block valignmiddle divphotoref"><img class="photouserphoto userphoto" alt="No photo" border="0" src="'.DOL_URL_ROOT.'/viewimage.php?modulepart='.$module.'&entity='.$conf->entity.'&file='.urlencode($pathtophoto).'"></div>';
  711. }
  712. $result .= '</div>';
  713. } else {
  714. $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);
  715. }
  716. }
  717. }
  718. if ($withpicto != 2) {
  719. $result .= $this->ref;
  720. }
  721. $result .= $linkend;
  722. //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
  723. global $action, $hookmanager;
  724. $hookmanager->initHooks(array('myobjectdao'));
  725. $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
  726. $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
  727. if ($reshook > 0) {
  728. $result = $hookmanager->resPrint;
  729. } else {
  730. $result .= $hookmanager->resPrint;
  731. }
  732. return $result;
  733. }
  734. /**
  735. * Return the label of the status
  736. *
  737. * @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
  738. * @return string Label of status
  739. */
  740. public function getLabelStatus($mode = 0)
  741. {
  742. return $this->LibStatut($this->status, $mode);
  743. }
  744. /**
  745. * Return the label of the status
  746. *
  747. * @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
  748. * @return string Label of status
  749. */
  750. public function getLibStatut($mode = 0)
  751. {
  752. return $this->LibStatut($this->status, $mode);
  753. }
  754. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  755. /**
  756. * Return the status
  757. *
  758. * @param int $status Id status
  759. * @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
  760. * @return string Label of status
  761. */
  762. public function LibStatut($status, $mode = 0)
  763. {
  764. // phpcs:enable
  765. if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
  766. global $langs;
  767. //$langs->load("mymodule@mymodule");
  768. $this->labelStatus[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Draft');
  769. $this->labelStatus[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Enabled');
  770. $this->labelStatus[self::STATUS_CANCELED] = $langs->transnoentitiesnoconv('Disabled');
  771. $this->labelStatusShort[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Draft');
  772. $this->labelStatusShort[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Enabled');
  773. $this->labelStatusShort[self::STATUS_CANCELED] = $langs->transnoentitiesnoconv('Disabled');
  774. }
  775. $statusType = 'status'.$status;
  776. //if ($status == self::STATUS_VALIDATED) $statusType = 'status1';
  777. if ($status == self::STATUS_CANCELED) {
  778. $statusType = 'status6';
  779. }
  780. return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
  781. }
  782. /**
  783. * Load the info information in the object
  784. *
  785. * @param int $id Id of object
  786. * @return void
  787. */
  788. public function info($id)
  789. {
  790. $sql = "SELECT rowid, date_creation as datec, tms as datem,";
  791. $sql .= " fk_user_creat, fk_user_modif";
  792. $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element." as t";
  793. $sql .= " WHERE t.rowid = ".((int) $id);
  794. $result = $this->db->query($sql);
  795. if ($result) {
  796. if ($this->db->num_rows($result)) {
  797. $obj = $this->db->fetch_object($result);
  798. $this->id = $obj->rowid;
  799. if (!empty($obj->fk_user_author)) {
  800. $cuser = new User($this->db);
  801. $cuser->fetch($obj->fk_user_author);
  802. $this->user_creation = $cuser;
  803. }
  804. if (!empty($obj->fk_user_valid)) {
  805. $vuser = new User($this->db);
  806. $vuser->fetch($obj->fk_user_valid);
  807. $this->user_validation = $vuser;
  808. }
  809. if (!empty($obj->fk_user_cloture)) {
  810. $cluser = new User($this->db);
  811. $cluser->fetch($obj->fk_user_cloture);
  812. $this->user_cloture = $cluser;
  813. }
  814. $this->date_creation = $this->db->jdate($obj->datec);
  815. $this->date_modification = $this->db->jdate($obj->datem);
  816. $this->date_validation = $this->db->jdate($obj->datev);
  817. }
  818. $this->db->free($result);
  819. } else {
  820. dol_print_error($this->db);
  821. }
  822. }
  823. /**
  824. * Initialise object with example values
  825. * Id must be 0 if object instance is a specimen
  826. *
  827. * @return void
  828. */
  829. public function initAsSpecimen()
  830. {
  831. // Set here init that are not commonf fields
  832. // $this->property1 = ...
  833. // $this->property2 = ...
  834. $this->initAsSpecimenCommon();
  835. }
  836. /**
  837. * Create an array of lines
  838. *
  839. * @return array|int array of lines if OK, <0 if KO
  840. */
  841. public function getLinesArray()
  842. {
  843. $this->lines = array();
  844. $objectline = new MyObjectLine($this->db);
  845. $result = $objectline->fetchAll('ASC', 'position', 0, 0, array('customsql'=>'fk_myobject = '.((int) $this->id)));
  846. if (is_numeric($result)) {
  847. $this->error = $objectline->error;
  848. $this->errors = $objectline->errors;
  849. return $result;
  850. } else {
  851. $this->lines = $result;
  852. return $this->lines;
  853. }
  854. }
  855. /**
  856. * Returns the reference to the following non used object depending on the active numbering module.
  857. *
  858. * @return string Object free reference
  859. */
  860. public function getNextNumRef()
  861. {
  862. global $langs, $conf;
  863. $langs->load("mymodule@mymodule");
  864. if (empty($conf->global->MYMODULE_MYOBJECT_ADDON)) {
  865. $conf->global->MYMODULE_MYOBJECT_ADDON = 'mod_myobject_standard';
  866. }
  867. if (!empty($conf->global->MYMODULE_MYOBJECT_ADDON)) {
  868. $mybool = false;
  869. $file = $conf->global->MYMODULE_MYOBJECT_ADDON.".php";
  870. $classname = $conf->global->MYMODULE_MYOBJECT_ADDON;
  871. // Include file with class
  872. $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
  873. foreach ($dirmodels as $reldir) {
  874. $dir = dol_buildpath($reldir."core/modules/mymodule/");
  875. // Load file with numbering class (if found)
  876. $mybool |= @include_once $dir.$file;
  877. }
  878. if ($mybool === false) {
  879. dol_print_error('', "Failed to include file ".$file);
  880. return '';
  881. }
  882. if (class_exists($classname)) {
  883. $obj = new $classname();
  884. $numref = $obj->getNextValue($this);
  885. if ($numref != '' && $numref != '-1') {
  886. return $numref;
  887. } else {
  888. $this->error = $obj->error;
  889. //dol_print_error($this->db,get_class($this)."::getNextNumRef ".$obj->error);
  890. return "";
  891. }
  892. } else {
  893. print $langs->trans("Error")." ".$langs->trans("ClassNotFound").' '.$classname;
  894. return "";
  895. }
  896. } else {
  897. print $langs->trans("ErrorNumberingModuleNotSetup", $this->element);
  898. return "";
  899. }
  900. }
  901. /**
  902. * Create a document onto disk according to template module.
  903. *
  904. * @param string $modele Force template to use ('' to not force)
  905. * @param Translate $outputlangs objet lang a utiliser pour traduction
  906. * @param int $hidedetails Hide details of lines
  907. * @param int $hidedesc Hide description
  908. * @param int $hideref Hide ref
  909. * @param null|array $moreparams Array to provide more information
  910. * @return int 0 if KO, 1 if OK
  911. */
  912. public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams = null)
  913. {
  914. global $conf, $langs;
  915. $result = 0;
  916. $includedocgeneration = 0;
  917. $langs->load("mymodule@mymodule");
  918. if (!dol_strlen($modele)) {
  919. $modele = 'standard_myobject';
  920. if (!empty($this->model_pdf)) {
  921. $modele = $this->model_pdf;
  922. } elseif (!empty($conf->global->MYOBJECT_ADDON_PDF)) {
  923. $modele = $conf->global->MYOBJECT_ADDON_PDF;
  924. }
  925. }
  926. $modelpath = "core/modules/mymodule/doc/";
  927. if ($includedocgeneration && !empty($modele)) {
  928. $result = $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
  929. }
  930. return $result;
  931. }
  932. /**
  933. * Action executed by scheduler
  934. * CAN BE A CRON TASK. In such a case, parameters come from the schedule job setup field 'Parameters'
  935. * Use public function doScheduledJob($param1, $param2, ...) to get parameters
  936. *
  937. * @return int 0 if OK, <>0 if KO (this function is used also by cron so only 0 is OK)
  938. */
  939. public function doScheduledJob()
  940. {
  941. global $conf, $langs;
  942. //$conf->global->SYSLOG_FILE = 'DOL_DATA_ROOT/dolibarr_mydedicatedlofile.log';
  943. $error = 0;
  944. $this->output = '';
  945. $this->error = '';
  946. dol_syslog(__METHOD__, LOG_DEBUG);
  947. $now = dol_now();
  948. $this->db->begin();
  949. // ...
  950. $this->db->commit();
  951. return $error;
  952. }
  953. }
  954. require_once DOL_DOCUMENT_ROOT.'/core/class/commonobjectline.class.php';
  955. /**
  956. * Class MyObjectLine. You can also remove this and generate a CRUD class for lines objects.
  957. */
  958. class MyObjectLine extends CommonObjectLine
  959. {
  960. // To complete with content of an object MyObjectLine
  961. // We should have a field rowid, fk_myobject and position
  962. /**
  963. * @var int Does object support extrafields ? 0=No, 1=Yes
  964. */
  965. public $isextrafieldmanaged = 0;
  966. /**
  967. * Constructor
  968. *
  969. * @param DoliDb $db Database handler
  970. */
  971. public function __construct(DoliDB $db)
  972. {
  973. $this->db = $db;
  974. }
  975. }