emailcollectoraction.class.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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 class/emailcollectoraction.class.php
  20. * \ingroup emailcollector
  21. * \brief This file is a CRUD class file for EmailCollectorAction (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 EmailCollectorAction
  29. */
  30. class EmailCollectorAction extends CommonObject
  31. {
  32. /**
  33. * @var string ID to identify managed object
  34. */
  35. public $element = 'emailcollectoraction';
  36. /**
  37. * @var string Name of table without prefix where object is stored
  38. */
  39. public $table_element = 'emailcollector_emailcollectoraction';
  40. /**
  41. * @var int Does emailcollectoraction support multicompany module ? 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
  42. */
  43. public $ismultientitymanaged = 0;
  44. /**
  45. * @var int Does emailcollectoraction support extrafields ? 0=No, 1=Yes
  46. */
  47. public $isextrafieldmanaged = 0;
  48. /**
  49. * @var string String with name of icon for emailcollectoraction. Must be the part after the 'object_' into object_emailcollectoraction.png
  50. */
  51. public $picto = 'emailcollectoraction@emailcollector';
  52. /**
  53. * 'type' if the field format.
  54. * 'label' the translation key.
  55. * 'enabled' is a condition when the field must be managed.
  56. * '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. Using a negative value means field is not shown by default on list but can be selected for viewing)
  57. * 'notnull' is set to 1 if not null in database. Set to -1 if we must set data to null if empty ('' or 0).
  58. * 'default' is a default value for creation (can still be replaced by the global setup of default values)
  59. * 'index' if we want an index in database.
  60. * 'foreignkey'=>'tablename.field' if the field is a foreign key (it is recommanded to name the field fk_...).
  61. * 'position' is the sort order of field.
  62. * 'searchall' is 1 if we want to search in this field when making a search from the quick search button.
  63. * '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).
  64. * 'css' is the CSS style to use on field. For example: 'maxwidth200'
  65. * 'help' is a string visible as a tooltip on field
  66. * 'comment' is not used. You can store here any text of your choice. It is not used by application.
  67. * 'showoncombobox' if value of the field must be visible into the label of the combobox that list record
  68. * 'arrayofkeyval' to set list of value if type is a list of predefined values. For example: array("0"=>"Draft","1"=>"Active","-1"=>"Cancel")
  69. */
  70. // BEGIN MODULEBUILDER PROPERTIES
  71. /**
  72. * @var array Array with all fields and their property. Do not use it as a static var. It may be modified by constructor.
  73. */
  74. public $fields = array(
  75. 'rowid' => array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-1, 'position'=>1, 'notnull'=>1, 'index'=>1, 'comment'=>"Id",),
  76. 'fk_emailcollector' => array('type'=>'integer', 'label'=>'Id of emailcollector', 'foreignkey'=>'emailcollector.rowid'),
  77. 'type' => array('type'=>'varchar(128)', 'label'=>'Type', 'enabled'=>1, 'visible'=>1, 'position'=>10, 'notnull'=>1, 'index'=>1),
  78. 'actionparam' => array('type'=>'varchar(255)', 'label'=>'ParamForAction', 'enabled'=>1, 'visible'=>1, 'position'=>40, 'notnull'=>-1),
  79. 'date_creation' => array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-2, 'position'=>500, 'notnull'=>1,),
  80. 'tms' => array('type'=>'timestamp', 'label'=>'DateModification', 'enabled'=>1, 'visible'=>-2, 'position'=>501, 'notnull'=>1,),
  81. 'fk_user_creat' => array('type'=>'integer', 'label'=>'UserAuthor', 'enabled'=>1, 'visible'=>-2, 'position'=>510, 'notnull'=>1, 'foreignkey'=>'llx_user.rowid',),
  82. 'fk_user_modif' => array('type'=>'integer', 'label'=>'UserModif', 'enabled'=>1, 'visible'=>-2, 'position'=>511, 'notnull'=>-1,),
  83. 'position' => array('type'=>'integer', 'label'=>'Position', 'enabled'=>1, 'visible'=>1, 'position'=>600, 'default'=>'0',),
  84. 'import_key' => array('type'=>'varchar(14)', 'label'=>'ImportId', 'enabled'=>1, 'visible'=>-2, 'position'=>1000, 'notnull'=>-1,),
  85. 'status' => array('type'=>'integer', 'label'=>'Status', 'enabled'=>1, 'visible'=>1, 'position'=>1000, 'notnull'=>1, 'default'=>1, 'arrayofkeyval'=>array('0'=>'Disabled', '1'=>'Enabled')),
  86. );
  87. public $rowid;
  88. public $fk_emailcollector;
  89. public $type;
  90. public $actionparam;
  91. /**
  92. * @var integer|string date_creation
  93. */
  94. public $date_creation;
  95. public $tms;
  96. public $fk_user_creat;
  97. public $fk_user_modif;
  98. public $position;
  99. public $import_key;
  100. public $status;
  101. // END MODULEBUILDER PROPERTIES
  102. // If this object has a subtable with lines
  103. // /**
  104. // * @var string Name of subtable line
  105. // */
  106. //public $table_element_line = 'emailcollectoractiondet';
  107. // /**
  108. // * @var string Field with ID of parent key if this field has a parent
  109. // */
  110. //public $fk_element = 'fk_emailcollectoraction';
  111. // /**
  112. // * @var string Name of subtable class that manage subtable lines
  113. // */
  114. //public $class_element_line = 'EmailcollectorActionline';
  115. // /**
  116. // * @var array List of child tables. To test if we can delete object.
  117. // */
  118. //protected $childtables=array();
  119. // /**
  120. // * @var EmailcollectorActionLine[] Array of subtable lines
  121. // */
  122. //public $lines = array();
  123. /**
  124. * Constructor
  125. *
  126. * @param DoliDb $db Database handler
  127. */
  128. public function __construct(DoliDB $db)
  129. {
  130. global $conf, $langs;
  131. $this->db = $db;
  132. if (empty($conf->global->MAIN_SHOW_TECHNICAL_ID) && isset($this->fields['rowid'])) {
  133. $this->fields['rowid']['visible'] = 0;
  134. }
  135. if (empty($conf->multicompany->enabled) && isset($this->fields['entity'])) {
  136. $this->fields['entity']['enabled'] = 0;
  137. }
  138. // Unset fields that are disabled
  139. foreach ($this->fields as $key => $val) {
  140. if (isset($val['enabled']) && empty($val['enabled'])) {
  141. unset($this->fields[$key]);
  142. }
  143. }
  144. // Translate some data of arrayofkeyval
  145. foreach ($this->fields as $key => $val) {
  146. if (!empty($val['arrayofkeyval']) && is_array($val['arrayofkeyval'])) {
  147. foreach ($val['arrayofkeyval'] as $key2 => $val2) {
  148. $this->fields[$key]['arrayofkeyval'][$key2] = $langs->trans($val2);
  149. }
  150. }
  151. }
  152. }
  153. /**
  154. * Create object into database
  155. *
  156. * @param User $user User that creates
  157. * @param bool $notrigger false=launch triggers after, true=disable triggers
  158. * @return int <0 if KO, Id of created object if OK
  159. */
  160. public function create(User $user, $notrigger = false)
  161. {
  162. global $langs;
  163. if (empty($this->type)) {
  164. $langs->load("errors");
  165. $this->errors[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Type"));
  166. return -1;
  167. }
  168. return $this->createCommon($user, $notrigger);
  169. }
  170. /**
  171. * Clone and object into another one
  172. *
  173. * @param User $user User that creates
  174. * @param int $fromid Id of object to clone
  175. * @return mixed New object created, <0 if KO
  176. */
  177. public function createFromClone(User $user, $fromid)
  178. {
  179. global $langs, $hookmanager, $extrafields;
  180. $error = 0;
  181. dol_syslog(__METHOD__, LOG_DEBUG);
  182. $object = new self($this->db);
  183. $this->db->begin();
  184. // Load source object
  185. $object->fetchCommon($fromid);
  186. // Reset some properties
  187. unset($object->id);
  188. unset($object->fk_user_creat);
  189. unset($object->import_key);
  190. // Clear fields
  191. $object->ref = "copy_of_".$object->ref;
  192. $object->title = $langs->trans("CopyOf")." ".$object->title;
  193. // ...
  194. // Clear extrafields that are unique
  195. if (is_array($object->array_options) && count($object->array_options) > 0) {
  196. $extrafields->fetch_name_optionals_label($this->table_element);
  197. foreach ($object->array_options as $key => $option) {
  198. $shortkey = preg_replace('/options_/', '', $key);
  199. if (!empty($extrafields->attributes[$this->element]['unique'][$shortkey])) {
  200. //var_dump($key);
  201. //var_dump($clonedObj->array_options[$key]); exit;
  202. unset($object->array_options[$key]);
  203. }
  204. }
  205. }
  206. // Create clone
  207. $object->context['createfromclone'] = 'createfromclone';
  208. $result = $object->createCommon($user);
  209. if ($result < 0) {
  210. $error++;
  211. $this->error = $object->error;
  212. $this->errors = $object->errors;
  213. }
  214. unset($object->context['createfromclone']);
  215. // End
  216. if (!$error) {
  217. $this->db->commit();
  218. return $object;
  219. } else {
  220. $this->db->rollback();
  221. return -1;
  222. }
  223. }
  224. /**
  225. * Load object in memory from the database
  226. *
  227. * @param int $id Id object
  228. * @param string $ref Ref
  229. * @return int <0 if KO, 0 if not found, >0 if OK
  230. */
  231. public function fetch($id, $ref = null)
  232. {
  233. $result = $this->fetchCommon($id, $ref);
  234. if ($result > 0 && !empty($this->table_element_line)) {
  235. $this->fetchLines();
  236. }
  237. return $result;
  238. }
  239. /**
  240. * Load object lines in memory from the database
  241. *
  242. * @return int <0 if KO, 0 if not found, >0 if OK
  243. */
  244. /*public function fetchLines()
  245. {
  246. $this->lines=array();
  247. // Load lines with object EmailcollectorActionLine
  248. return count($this->lines)?1:0;
  249. }*/
  250. /**
  251. * Update object into database
  252. *
  253. * @param User $user User that modifies
  254. * @param bool $notrigger false=launch triggers after, true=disable triggers
  255. * @return int <0 if KO, >0 if OK
  256. */
  257. public function update(User $user, $notrigger = false)
  258. {
  259. return $this->updateCommon($user, $notrigger);
  260. }
  261. /**
  262. * Delete object in database
  263. *
  264. * @param User $user User that deletes
  265. * @param bool $notrigger false=launch triggers after, true=disable triggers
  266. * @return int <0 if KO, >0 if OK
  267. */
  268. public function delete(User $user, $notrigger = false)
  269. {
  270. return $this->deleteCommon($user, $notrigger);
  271. }
  272. /**
  273. * Return a link to the object card (with optionaly the picto)
  274. *
  275. * @param int $withpicto Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto)
  276. * @param string $option On what the link point to ('nolink', ...)
  277. * @param int $notooltip 1=Disable tooltip
  278. * @param string $morecss Add more css on link
  279. * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
  280. * @return string String with URL
  281. */
  282. public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
  283. {
  284. global $db, $conf, $langs, $hookmanager;
  285. global $dolibarr_main_authentication, $dolibarr_main_demo;
  286. global $menumanager;
  287. if (!empty($conf->dol_no_mouse_hover)) {
  288. $notooltip = 1; // Force disable tooltips
  289. }
  290. $result = '';
  291. $companylink = '';
  292. $label = '<u>'.$langs->trans("EmailcollectorAction").'</u>';
  293. $label .= '<br>';
  294. $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
  295. $url = dol_buildpath('/emailcollector/emailcollectoraction_card.php', 1).'?id='.$this->id;
  296. if ($option != 'nolink') {
  297. // Add param to save lastsearch_values or not
  298. $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
  299. if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
  300. $add_save_lastsearch_values = 1;
  301. }
  302. if ($add_save_lastsearch_values) {
  303. $url .= '&save_lastsearch_values=1';
  304. }
  305. }
  306. $linkclose = '';
  307. if (empty($notooltip)) {
  308. if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
  309. $label = $langs->trans("ShowEmailcollectorAction");
  310. $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
  311. }
  312. $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
  313. $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
  314. /*
  315. $hookmanager->initHooks(array('emailcollectoractiondao'));
  316. $parameters=array('id'=>$this->id);
  317. $reshook=$hookmanager->executeHooks('getnomurltooltip',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
  318. if ($reshook > 0) $linkclose = $hookmanager->resPrint;
  319. */
  320. } else {
  321. $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
  322. }
  323. $linkstart = '<a href="'.$url.'"';
  324. $linkstart .= $linkclose.'>';
  325. $linkend = '</a>';
  326. $result .= $linkstart;
  327. if ($withpicto) {
  328. $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);
  329. }
  330. if ($withpicto != 2) {
  331. $result .= $this->ref;
  332. }
  333. $result .= $linkend;
  334. //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
  335. global $action, $hookmanager;
  336. $hookmanager->initHooks(array('emailcollectoractiondao'));
  337. $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
  338. $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
  339. if ($reshook > 0) {
  340. $result = $hookmanager->resPrint;
  341. } else {
  342. $result .= $hookmanager->resPrint;
  343. }
  344. return $result;
  345. }
  346. /**
  347. * Return label of the status
  348. *
  349. * @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
  350. * @return string Label of status
  351. */
  352. public function getLibStatut($mode = 0)
  353. {
  354. return $this->LibStatut($this->status, $mode);
  355. }
  356. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  357. /**
  358. * Return the status
  359. *
  360. * @param int $status Id status
  361. * @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
  362. * @return string Label of status
  363. */
  364. public function LibStatut($status, $mode = 0)
  365. {
  366. // phpcs:enable
  367. if (empty($this->labelStatus)) {
  368. global $langs;
  369. //$langs->load("emailcollector");
  370. $this->labelStatus[1] = $langs->trans('Enabled');
  371. $this->labelStatus[0] = $langs->trans('Disabled');
  372. }
  373. if ($mode == 0) {
  374. return $this->labelStatus[$status];
  375. } elseif ($mode == 1) {
  376. return $this->labelStatus[$status];
  377. } elseif ($mode == 2) {
  378. if ($status == 1) {
  379. return img_picto($this->labelStatus[$status], 'statut4', '', false, 0, 0, '', 'valignmiddle').' '.$this->labelStatus[$status];
  380. } elseif ($status == 0) {
  381. return img_picto($this->labelStatus[$status], 'statut5', '', false, 0, 0, '', 'valignmiddle').' '.$this->labelStatus[$status];
  382. }
  383. } elseif ($mode == 3) {
  384. if ($status == 1) {
  385. return img_picto($this->labelStatus[$status], 'statut4', '', false, 0, 0, '', 'valignmiddle');
  386. } elseif ($status == 0) {
  387. return img_picto($this->labelStatus[$status], 'statut5', '', false, 0, 0, '', 'valignmiddle');
  388. }
  389. } elseif ($mode == 4) {
  390. if ($status == 1) {
  391. return img_picto($this->labelStatus[$status], 'statut4', '', false, 0, 0, '', 'valignmiddle').' '.$this->labelStatus[$status];
  392. } elseif ($status == 0) {
  393. return img_picto($this->labelStatus[$status], 'statut5', '', false, 0, 0, '', 'valignmiddle').' '.$this->labelStatus[$status];
  394. }
  395. } elseif ($mode == 5) {
  396. if ($status == 1) {
  397. return $this->labelStatus[$status].' '.img_picto($this->labelStatus[$status], 'statut4', '', false, 0, 0, '', 'valignmiddle');
  398. } elseif ($status == 0) {
  399. return $this->labelStatus[$status].' '.img_picto($this->labelStatus[$status], 'statut5', '', false, 0, 0, '', 'valignmiddle');
  400. }
  401. } elseif ($mode == 6) {
  402. if ($status == 1) {
  403. return $this->labelStatus[$status].' '.img_picto($this->labelStatus[$status], 'statut4', '', false, 0, 0, '', 'valignmiddle');
  404. } elseif ($status == 0) {
  405. return $this->labelStatus[$status].' '.img_picto($this->labelStatus[$status], 'statut5', '', false, 0, 0, '', 'valignmiddle');
  406. }
  407. }
  408. }
  409. /**
  410. * Charge les informations d'ordre info dans l'objet commande
  411. *
  412. * @param int $id Id of order
  413. * @return void
  414. */
  415. public function info($id)
  416. {
  417. $sql = 'SELECT rowid, date_creation as datec, tms as datem,';
  418. $sql .= ' fk_user_creat, fk_user_modif';
  419. $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
  420. $sql .= ' WHERE t.rowid = '.((int) $id);
  421. $result = $this->db->query($sql);
  422. if ($result) {
  423. if ($this->db->num_rows($result)) {
  424. $obj = $this->db->fetch_object($result);
  425. $this->id = $obj->rowid;
  426. if ($obj->fk_user_author) {
  427. $cuser = new User($this->db);
  428. $cuser->fetch($obj->fk_user_author);
  429. $this->user_creation = $cuser;
  430. }
  431. if ($obj->fk_user_valid) {
  432. $vuser = new User($this->db);
  433. $vuser->fetch($obj->fk_user_valid);
  434. $this->user_validation = $vuser;
  435. }
  436. if ($obj->fk_user_cloture) {
  437. $cluser = new User($this->db);
  438. $cluser->fetch($obj->fk_user_cloture);
  439. $this->user_cloture = $cluser;
  440. }
  441. $this->date_creation = $this->db->jdate($obj->datec);
  442. $this->date_modification = $this->db->jdate($obj->datem);
  443. $this->date_validation = $this->db->jdate($obj->datev);
  444. }
  445. $this->db->free($result);
  446. } else {
  447. dol_print_error($this->db);
  448. }
  449. }
  450. /**
  451. * Initialise object with example values
  452. * Id must be 0 if object instance is a specimen
  453. *
  454. * @return void
  455. */
  456. public function initAsSpecimen()
  457. {
  458. $this->initAsSpecimenCommon();
  459. }
  460. }