emailcollectoraction.class.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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); var_dump($clonedObj->array_options[$key]); exit;
  201. unset($object->array_options[$key]);
  202. }
  203. }
  204. }
  205. // Create clone
  206. $object->context['createfromclone'] = 'createfromclone';
  207. $result = $object->createCommon($user);
  208. if ($result < 0) {
  209. $error++;
  210. $this->error = $object->error;
  211. $this->errors = $object->errors;
  212. }
  213. unset($object->context['createfromclone']);
  214. // End
  215. if (!$error) {
  216. $this->db->commit();
  217. return $object;
  218. } else {
  219. $this->db->rollback();
  220. return -1;
  221. }
  222. }
  223. /**
  224. * Load object in memory from the database
  225. *
  226. * @param int $id Id object
  227. * @param string $ref Ref
  228. * @return int <0 if KO, 0 if not found, >0 if OK
  229. */
  230. public function fetch($id, $ref = null)
  231. {
  232. $result = $this->fetchCommon($id, $ref);
  233. if ($result > 0 && !empty($this->table_element_line)) {
  234. $this->fetchLines();
  235. }
  236. return $result;
  237. }
  238. /**
  239. * Load object lines in memory from the database
  240. *
  241. * @return int <0 if KO, 0 if not found, >0 if OK
  242. */
  243. /*public function fetchLines()
  244. {
  245. $this->lines=array();
  246. // Load lines with object EmailcollectorActionLine
  247. return count($this->lines)?1:0;
  248. }*/
  249. /**
  250. * Update object into database
  251. *
  252. * @param User $user User that modifies
  253. * @param bool $notrigger false=launch triggers after, true=disable triggers
  254. * @return int <0 if KO, >0 if OK
  255. */
  256. public function update(User $user, $notrigger = false)
  257. {
  258. return $this->updateCommon($user, $notrigger);
  259. }
  260. /**
  261. * Delete object in database
  262. *
  263. * @param User $user User that deletes
  264. * @param bool $notrigger false=launch triggers after, true=disable triggers
  265. * @return int <0 if KO, >0 if OK
  266. */
  267. public function delete(User $user, $notrigger = false)
  268. {
  269. return $this->deleteCommon($user, $notrigger);
  270. }
  271. /**
  272. * Return a link to the object card (with optionaly the picto)
  273. *
  274. * @param int $withpicto Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto)
  275. * @param string $option On what the link point to ('nolink', ...)
  276. * @param int $notooltip 1=Disable tooltip
  277. * @param string $morecss Add more css on link
  278. * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
  279. * @return string String with URL
  280. */
  281. public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
  282. {
  283. global $db, $conf, $langs, $hookmanager;
  284. global $dolibarr_main_authentication, $dolibarr_main_demo;
  285. global $menumanager;
  286. if (!empty($conf->dol_no_mouse_hover)) {
  287. $notooltip = 1; // Force disable tooltips
  288. }
  289. $result = '';
  290. $companylink = '';
  291. $label = '<u>'.$langs->trans("EmailcollectorAction").'</u>';
  292. $label .= '<br>';
  293. $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
  294. $url = dol_buildpath('/emailcollector/emailcollectoraction_card.php', 1).'?id='.$this->id;
  295. if ($option != 'nolink') {
  296. // Add param to save lastsearch_values or not
  297. $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
  298. if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
  299. $add_save_lastsearch_values = 1;
  300. }
  301. if ($add_save_lastsearch_values) {
  302. $url .= '&save_lastsearch_values=1';
  303. }
  304. }
  305. $linkclose = '';
  306. if (empty($notooltip)) {
  307. if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
  308. $label = $langs->trans("ShowEmailcollectorAction");
  309. $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
  310. }
  311. $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
  312. $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
  313. /*
  314. $hookmanager->initHooks(array('emailcollectoractiondao'));
  315. $parameters=array('id'=>$this->id);
  316. $reshook=$hookmanager->executeHooks('getnomurltooltip',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
  317. if ($reshook > 0) $linkclose = $hookmanager->resPrint;
  318. */
  319. } else {
  320. $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
  321. }
  322. $linkstart = '<a href="'.$url.'"';
  323. $linkstart .= $linkclose.'>';
  324. $linkend = '</a>';
  325. $result .= $linkstart;
  326. if ($withpicto) {
  327. $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);
  328. }
  329. if ($withpicto != 2) {
  330. $result .= $this->ref;
  331. }
  332. $result .= $linkend;
  333. //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
  334. global $action, $hookmanager;
  335. $hookmanager->initHooks(array('emailcollectoractiondao'));
  336. $parameters = array('id'=>$this->id, 'getnomurl'=>$result);
  337. $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
  338. if ($reshook > 0) {
  339. $result = $hookmanager->resPrint;
  340. } else {
  341. $result .= $hookmanager->resPrint;
  342. }
  343. return $result;
  344. }
  345. /**
  346. * Return label of the status
  347. *
  348. * @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
  349. * @return string Label of status
  350. */
  351. public function getLibStatut($mode = 0)
  352. {
  353. return $this->LibStatut($this->status, $mode);
  354. }
  355. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  356. /**
  357. * Return the status
  358. *
  359. * @param int $status Id status
  360. * @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
  361. * @return string Label of status
  362. */
  363. public function LibStatut($status, $mode = 0)
  364. {
  365. // phpcs:enable
  366. if (empty($this->labelStatus)) {
  367. global $langs;
  368. //$langs->load("emailcollector");
  369. $this->labelStatus[1] = $langs->trans('Enabled');
  370. $this->labelStatus[0] = $langs->trans('Disabled');
  371. }
  372. if ($mode == 0) {
  373. return $this->labelStatus[$status];
  374. } elseif ($mode == 1) {
  375. return $this->labelStatus[$status];
  376. } elseif ($mode == 2) {
  377. if ($status == 1) {
  378. return img_picto($this->labelStatus[$status], 'statut4', '', false, 0, 0, '', 'valignmiddle').' '.$this->labelStatus[$status];
  379. } elseif ($status == 0) {
  380. return img_picto($this->labelStatus[$status], 'statut5', '', false, 0, 0, '', 'valignmiddle').' '.$this->labelStatus[$status];
  381. }
  382. } elseif ($mode == 3) {
  383. if ($status == 1) {
  384. return img_picto($this->labelStatus[$status], 'statut4', '', false, 0, 0, '', 'valignmiddle');
  385. } elseif ($status == 0) {
  386. return img_picto($this->labelStatus[$status], 'statut5', '', false, 0, 0, '', 'valignmiddle');
  387. }
  388. } elseif ($mode == 4) {
  389. if ($status == 1) {
  390. return img_picto($this->labelStatus[$status], 'statut4', '', false, 0, 0, '', 'valignmiddle').' '.$this->labelStatus[$status];
  391. } elseif ($status == 0) {
  392. return img_picto($this->labelStatus[$status], 'statut5', '', false, 0, 0, '', 'valignmiddle').' '.$this->labelStatus[$status];
  393. }
  394. } elseif ($mode == 5) {
  395. if ($status == 1) {
  396. return $this->labelStatus[$status].' '.img_picto($this->labelStatus[$status], 'statut4', '', false, 0, 0, '', 'valignmiddle');
  397. } elseif ($status == 0) {
  398. return $this->labelStatus[$status].' '.img_picto($this->labelStatus[$status], 'statut5', '', false, 0, 0, '', 'valignmiddle');
  399. }
  400. } elseif ($mode == 6) {
  401. if ($status == 1) {
  402. return $this->labelStatus[$status].' '.img_picto($this->labelStatus[$status], 'statut4', '', false, 0, 0, '', 'valignmiddle');
  403. } elseif ($status == 0) {
  404. return $this->labelStatus[$status].' '.img_picto($this->labelStatus[$status], 'statut5', '', false, 0, 0, '', 'valignmiddle');
  405. }
  406. }
  407. }
  408. /**
  409. * Charge les informations d'ordre info dans l'objet commande
  410. *
  411. * @param int $id Id of order
  412. * @return void
  413. */
  414. public function info($id)
  415. {
  416. $sql = 'SELECT rowid, date_creation as datec, tms as datem,';
  417. $sql .= ' fk_user_creat, fk_user_modif';
  418. $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
  419. $sql .= ' WHERE t.rowid = '.((int) $id);
  420. $result = $this->db->query($sql);
  421. if ($result) {
  422. if ($this->db->num_rows($result)) {
  423. $obj = $this->db->fetch_object($result);
  424. $this->id = $obj->rowid;
  425. if ($obj->fk_user_author) {
  426. $cuser = new User($this->db);
  427. $cuser->fetch($obj->fk_user_author);
  428. $this->user_creation = $cuser;
  429. }
  430. if ($obj->fk_user_valid) {
  431. $vuser = new User($this->db);
  432. $vuser->fetch($obj->fk_user_valid);
  433. $this->user_validation = $vuser;
  434. }
  435. if ($obj->fk_user_cloture) {
  436. $cluser = new User($this->db);
  437. $cluser->fetch($obj->fk_user_cloture);
  438. $this->user_cloture = $cluser;
  439. }
  440. $this->date_creation = $this->db->jdate($obj->datec);
  441. $this->date_modification = $this->db->jdate($obj->datem);
  442. $this->date_validation = $this->db->jdate($obj->datev);
  443. }
  444. $this->db->free($result);
  445. } else {
  446. dol_print_error($this->db);
  447. }
  448. }
  449. /**
  450. * Initialise object with example values
  451. * Id must be 0 if object instance is a specimen
  452. *
  453. * @return void
  454. */
  455. public function initAsSpecimen()
  456. {
  457. $this->initAsSpecimenCommon();
  458. }
  459. }