emailsenderprofile.class.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. <?php
  2. /* Copyright (C) 2007-2017 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2014-2016 Juanjo Menent <jmenent@2byte.es>
  4. * Copyright (C) 2015 Florian Henry <florian.henry@open-concept.pro>
  5. * Copyright (C) 2015 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
  6. * Copyright (C) ---Put here your own copyright and developer email---
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. */
  21. /**
  22. * \file core/class/emailsenderprofile.class.php
  23. * \ingroup core
  24. * \brief This file is a CRUD class file for EmailSenderProfile (Create/Read/Update/Delete)
  25. */
  26. // Put here all includes required by your class file
  27. require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
  28. //require_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php';
  29. //require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php';
  30. /**
  31. * Class for EmailSenderProfile
  32. */
  33. class EmailSenderProfile extends CommonObject
  34. {
  35. /**
  36. * @var string ID to identify managed object
  37. */
  38. public $element = 'emailsenderprofile';
  39. /**
  40. * @var string Name of table without prefix where object is stored
  41. */
  42. public $table_element = 'c_email_senderprofile';
  43. /**
  44. * @var array Does emailsenderprofile support multicompany module ? 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
  45. */
  46. public $ismultientitymanaged = 1;
  47. /**
  48. * @var string String with name of icon for emailsenderprofile
  49. */
  50. public $picto = 'emailsenderprofile';
  51. const STATUS_DISABLED = 0;
  52. const STATUS_ENABLED = 1;
  53. /**
  54. * 'type' if the field format ('integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter]]', 'varchar(x)', 'double(24,8)', 'real', 'price', 'text', 'html', 'date', 'datetime', 'timestamp', 'duration', 'mail', 'phone', 'url', 'password')
  55. * Note: Filter can be a string like "(t.ref:like:'SO-%') or (t.date_creation:<:'20160101') or (t.nature:is:NULL)"
  56. * 'label' the translation key.
  57. * 'enabled' is a condition when the field must be managed.
  58. * 'position' is the sort order of field.
  59. * 'notnull' is set to 1 if not null in database. Set to -1 if we must set data to null if empty ('' or 0).
  60. * '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)
  61. * 'noteditable' says if field is not editable (1 or 0)
  62. * '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.
  63. * 'index' if we want an index in database.
  64. * 'foreignkey'=>'tablename.field' if the field is a foreign key (it is recommanded to name the field fk_...).
  65. * 'searchall' is 1 if we want to search in this field when making a search from the quick search button.
  66. * '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).
  67. * 'css' is the CSS style to use on field. For example: 'maxwidth200'
  68. * 'help' is a string visible as a tooltip on field
  69. * 'showoncombobox' if value of the field must be visible into the label of the combobox that list record
  70. * '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.
  71. * 'arrayofkeyval' to set list of value if type is a list of predefined values. For example: array("0"=>"Draft","1"=>"Active","-1"=>"Cancel")
  72. * 'comment' is not used. You can store here any text of your choice. It is not used by application.
  73. *
  74. * Note: To have value dynamic, you can set value to 0 in definition and edit the value on the fly into the constructor.
  75. */
  76. // BEGIN MODULEBUILDER PROPERTIES
  77. /**
  78. * @var array Array with all fields and their property. Do not use it as a static var. It may be modified by constructor.
  79. */
  80. public $fields = array(
  81. 'rowid' => array('type'=>'integer', 'label'=>'TechnicalID', 'visible'=>-1, 'enabled'=>1, 'position'=>1, 'notnull'=>1, 'index'=>1, 'comment'=>'Id',),
  82. 'entity' => array('type'=>'integer', 'label'=>'Entity', 'visible'=>-1, 'enabled'=>1, 'position'=>20, 'notnull'=>1, 'index'=>1,),
  83. 'label' => array('type'=>'varchar(255)', 'label'=>'Label', 'visible'=>1, 'enabled'=>1, 'position'=>30, 'notnull'=>1),
  84. 'email' => array('type'=>'varchar(255)', 'label'=>'Email', 'visible'=>1, 'enabled'=>1, 'position'=>40, 'notnull'=>-1),
  85. 'private' => array('type'=>'integer:User:user/class/user.class.php', 'label'=>'User', 'visible'=>-1, 'enabled'=>1, 'position'=>50, 'default'=>'0', 'notnull'=>1),
  86. 'signature' => array('type'=>'text', 'label'=>'Signature', 'visible'=>3, 'enabled'=>1, 'position'=>400, 'notnull'=>-1, 'index'=>1,),
  87. 'position' => array('type'=>'integer', 'label'=>'Position', 'visible'=>1, 'enabled'=>1, 'position'=>405, 'notnull'=>-1, 'index'=>1,),
  88. 'date_creation' => array('type'=>'datetime', 'label'=>'DateCreation', 'visible'=>-1, 'enabled'=>1, 'position'=>500, 'notnull'=>1,),
  89. 'tms' => array('type'=>'timestamp', 'label'=>'DateModification', 'visible'=>-1, 'enabled'=>1, 'position'=>500, 'notnull'=>1,),
  90. 'active' => array('type'=>'integer', 'label'=>'Status', 'visible'=>1, 'enabled'=>1, 'default'=>1, 'position'=>1000, 'notnull'=>1, 'index'=>1, 'arrayofkeyval'=>array(0=>'Disabled', 1=>'Enabled')),
  91. );
  92. /**
  93. * @var int ID
  94. */
  95. public $rowid;
  96. /**
  97. * @var int Entity
  98. */
  99. public $entity;
  100. /**
  101. * @var string Email Sender Profile label
  102. */
  103. public $label;
  104. public $email;
  105. /**
  106. * @var integer|string date_creation
  107. */
  108. public $date_creation;
  109. public $tms;
  110. public $private;
  111. public $signature;
  112. public $position;
  113. public $active;
  114. // END MODULEBUILDER PROPERTIES
  115. /**
  116. * Constructor
  117. *
  118. * @param DoliDb $db Database handler
  119. */
  120. public function __construct(DoliDB $db)
  121. {
  122. global $conf;
  123. $this->db = $db;
  124. if (empty($conf->global->MAIN_SHOW_TECHNICAL_ID)) {
  125. $this->fields['rowid']['visible'] = 0;
  126. }
  127. if (empty($conf->multicompany->enabled)) {
  128. $this->fields['entity']['enabled'] = 0;
  129. }
  130. }
  131. /**
  132. * Create object into database
  133. *
  134. * @param User $user User that creates
  135. * @param bool $notrigger false=launch triggers after, true=disable triggers
  136. * @return int <0 if KO, Id of created object if OK
  137. */
  138. public function create(User $user, $notrigger = false)
  139. {
  140. return $this->createCommon($user, $notrigger);
  141. }
  142. /**
  143. * Clone and object into another one
  144. *
  145. * @param User $user User that creates
  146. * @param int $fromid Id of object to clone
  147. * @return mixed New object created, <0 if KO
  148. */
  149. public function createFromClone(User $user, $fromid)
  150. {
  151. global $hookmanager, $langs;
  152. $error = 0;
  153. dol_syslog(__METHOD__, LOG_DEBUG);
  154. $object = new self($this->db);
  155. $this->db->begin();
  156. // Load source object
  157. $object->fetchCommon($fromid);
  158. // Reset some properties
  159. unset($object->id);
  160. unset($object->fk_user_creat);
  161. unset($object->import_key);
  162. // Clear fields
  163. $object->ref = "copy_of_".$object->ref;
  164. $object->title = $langs->trans("CopyOf")." ".$object->title;
  165. // ...
  166. // Create clone
  167. $object->context['createfromclone'] = 'createfromclone';
  168. $result = $object->createCommon($user);
  169. if ($result < 0) {
  170. $error++;
  171. $this->error = $object->error;
  172. $this->errors = $object->errors;
  173. }
  174. unset($object->context['createfromclone']);
  175. // End
  176. if (!$error) {
  177. $this->db->commit();
  178. return $object;
  179. } else {
  180. $this->db->rollback();
  181. return -1;
  182. }
  183. }
  184. /**
  185. * Load object in memory from the database
  186. *
  187. * @param int $id Id object
  188. * @param string $ref Ref
  189. * @return int <0 if KO, 0 if not found, >0 if OK
  190. */
  191. public function fetch($id, $ref = null)
  192. {
  193. $result = $this->fetchCommon($id, $ref);
  194. if ($result > 0 && !empty($this->table_element_line)) {
  195. $this->fetchLines();
  196. }
  197. return $result;
  198. }
  199. /**
  200. * Load object lines in memory from the database
  201. *
  202. * @return int <0 if KO, 0 if not found, >0 if OK
  203. */
  204. public function fetchLines()
  205. {
  206. $this->lines = array();
  207. // Load lines with object EmailSenderProfileLine
  208. return count($this->lines) ? 1 : 0;
  209. }
  210. /**
  211. * Update object into database
  212. *
  213. * @param User $user User that modifies
  214. * @param bool $notrigger false=launch triggers after, true=disable triggers
  215. * @return int <0 if KO, >0 if OK
  216. */
  217. public function update(User $user, $notrigger = false)
  218. {
  219. return $this->updateCommon($user, $notrigger);
  220. }
  221. /**
  222. * Delete object in database
  223. *
  224. * @param User $user User that deletes
  225. * @param bool $notrigger false=launch triggers after, true=disable triggers
  226. * @return int <0 if KO, >0 if OK
  227. */
  228. public function delete(User $user, $notrigger = false)
  229. {
  230. return $this->deleteCommon($user, $notrigger);
  231. }
  232. /**
  233. * Return a link to the object card (with optionaly the picto)
  234. *
  235. * @param int $withpicto Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto)
  236. * @return string String with URL
  237. */
  238. public function getNomUrl($withpicto = 0)
  239. {
  240. global $db, $conf, $langs;
  241. global $dolibarr_main_authentication, $dolibarr_main_demo;
  242. global $menumanager;
  243. $result = '';
  244. $companylink = '';
  245. $label = $this->label;
  246. $url = '';
  247. //$url = dol_buildpath('/monmodule/emailsenderprofile_card.php',1).'?id='.$this->id;
  248. $linkstart = '';
  249. $linkend = '';
  250. if ($withpicto) {
  251. $result .= ($linkstart.img_object($label, 'label', 'class="classfortooltip"').$linkend);
  252. if ($withpicto != 2) {
  253. $result .= ' ';
  254. }
  255. }
  256. $result .= $linkstart.$this->label.$linkend;
  257. return $result;
  258. }
  259. /**
  260. * Retourne le libelle du status d'un user (actif, inactif)
  261. *
  262. * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
  263. * @return string Label of status
  264. */
  265. public function getLibStatut($mode = 0)
  266. {
  267. return $this->LibStatut($this->active, $mode);
  268. }
  269. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  270. /**
  271. * Return the status
  272. *
  273. * @param int $status Id status
  274. * @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
  275. * @return string Label of status
  276. */
  277. public static function LibStatut($status, $mode = 0)
  278. {
  279. global $langs;
  280. if ($status == 1) {
  281. $label = $labelshort = $langs->trans('Enabled');
  282. } else {
  283. $label = $labelshort = $langs->trans('Disabled');
  284. }
  285. $statusType = 'status'.$status;
  286. if ($status == self::STATUS_ENABLED) {
  287. $statusType = 'status4';
  288. }
  289. return dolGetStatus($label, $labelshort, '', $statusType, $mode);
  290. }
  291. /**
  292. * Charge les informations d'ordre info dans l'objet commande
  293. *
  294. * @param int $id Id of order
  295. * @return void
  296. */
  297. public function info($id)
  298. {
  299. $sql = 'SELECT rowid, date_creation as datec, tms as datem,';
  300. $sql .= ' fk_user_creat, fk_user_modif';
  301. $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
  302. $sql .= ' WHERE t.rowid = '.((int) $id);
  303. $result = $this->db->query($sql);
  304. if ($result) {
  305. if ($this->db->num_rows($result)) {
  306. $obj = $this->db->fetch_object($result);
  307. $this->id = $obj->rowid;
  308. if ($obj->fk_user_author) {
  309. $cuser = new User($this->db);
  310. $cuser->fetch($obj->fk_user_author);
  311. $this->user_creation = $cuser;
  312. }
  313. if ($obj->fk_user_valid) {
  314. $vuser = new User($this->db);
  315. $vuser->fetch($obj->fk_user_valid);
  316. $this->user_validation = $vuser;
  317. }
  318. if ($obj->fk_user_cloture) {
  319. $cluser = new User($this->db);
  320. $cluser->fetch($obj->fk_user_cloture);
  321. $this->user_cloture = $cluser;
  322. }
  323. $this->date_creation = $this->db->jdate($obj->datec);
  324. $this->date_modification = $this->db->jdate($obj->datem);
  325. $this->date_validation = $this->db->jdate($obj->datev);
  326. }
  327. $this->db->free($result);
  328. } else {
  329. dol_print_error($this->db);
  330. }
  331. }
  332. /**
  333. * Initialise object with example values
  334. * Id must be 0 if object instance is a specimen
  335. *
  336. * @return void
  337. */
  338. public function initAsSpecimen()
  339. {
  340. $this->initAsSpecimenCommon();
  341. }
  342. }
  343. /**
  344. * Class EmailSenderProfileLine. You can also remove this and generate a CRUD class for lines objects.
  345. */
  346. /*
  347. class EmailSenderProfileLine
  348. {
  349. // @var int ID
  350. public $id;
  351. // @var mixed Sample line property 1
  352. public $prop1;
  353. // @var mixed Sample line property 2
  354. public $prop2;
  355. }
  356. */