emailsenderprofile.class.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. public $fk_user_creat;
  52. const STATUS_DISABLED = 0;
  53. const STATUS_ENABLED = 1;
  54. /**
  55. * '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')
  56. * Note: Filter can be a string like "(t.ref:like:'SO-%') or (t.date_creation:<:'20160101') or (t.nature:is:NULL)"
  57. * 'label' the translation key.
  58. * 'enabled' is a condition when the field must be managed.
  59. * 'position' is the sort order of field.
  60. * 'notnull' is set to 1 if not null in database. Set to -1 if we must set data to null if empty ('' or 0).
  61. * '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)
  62. * 'noteditable' says if field is not editable (1 or 0)
  63. * '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.
  64. * 'index' if we want an index in database.
  65. * 'foreignkey'=>'tablename.field' if the field is a foreign key (it is recommanded to name the field fk_...).
  66. * 'searchall' is 1 if we want to search in this field when making a search from the quick search button.
  67. * '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).
  68. * 'css' is the CSS style to use on field. For example: 'maxwidth200'
  69. * 'help' is a string visible as a tooltip on field
  70. * 'showoncombobox' if value of the field must be visible into the label of the combobox that list record
  71. * '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.
  72. * 'arrayofkeyval' to set list of value if type is a list of predefined values. For example: array("0"=>"Draft","1"=>"Active","-1"=>"Cancel")
  73. * 'comment' is not used. You can store here any text of your choice. It is not used by application.
  74. *
  75. * Note: To have value dynamic, you can set value to 0 in definition and edit the value on the fly into the constructor.
  76. */
  77. // BEGIN MODULEBUILDER PROPERTIES
  78. /**
  79. * @var array Array with all fields and their property. Do not use it as a static var. It may be modified by constructor.
  80. */
  81. public $fields = array(
  82. 'rowid' => array('type'=>'integer', 'label'=>'TechnicalID', 'visible'=>-1, 'enabled'=>1, 'position'=>1, 'notnull'=>1, 'index'=>1, 'comment'=>'Id',),
  83. 'entity' => array('type'=>'integer', 'label'=>'Entity', 'visible'=>-1, 'enabled'=>1, 'position'=>20, 'notnull'=>1, 'index'=>1,),
  84. 'label' => array('type'=>'varchar(255)', 'label'=>'Label', 'visible'=>1, 'enabled'=>1, 'position'=>30, 'notnull'=>1),
  85. 'email' => array('type'=>'varchar(255)', 'label'=>'Email', 'visible'=>1, 'enabled'=>1, 'position'=>40, 'notnull'=>-1),
  86. 'private' => array('type'=>'integer:User:user/class/user.class.php', 'label'=>'User', 'visible'=>-1, 'enabled'=>1, 'position'=>50, 'default'=>'0', 'notnull'=>1),
  87. 'signature' => array('type'=>'text', 'label'=>'Signature', 'visible'=>3, 'enabled'=>1, 'position'=>400, 'notnull'=>-1, 'index'=>1,),
  88. 'position' => array('type'=>'integer', 'label'=>'Position', 'visible'=>1, 'enabled'=>1, 'position'=>405, 'notnull'=>-1, 'index'=>1,),
  89. 'date_creation' => array('type'=>'datetime', 'label'=>'DateCreation', 'visible'=>-1, 'enabled'=>1, 'position'=>500, 'notnull'=>1,),
  90. 'tms' => array('type'=>'timestamp', 'label'=>'DateModification', 'visible'=>-1, 'enabled'=>1, 'position'=>500, 'notnull'=>1,),
  91. 'active' => array('type'=>'integer', 'label'=>'Status', 'visible'=>1, 'enabled'=>1, 'default'=>1, 'position'=>1000, 'notnull'=>1, 'index'=>1, 'arrayofkeyval'=>array(0=>'Disabled', 1=>'Enabled')),
  92. );
  93. /**
  94. * @var int ID
  95. */
  96. public $rowid;
  97. /**
  98. * @var int Entity
  99. */
  100. public $entity;
  101. /**
  102. * @var string Email Sender Profile label
  103. */
  104. public $label;
  105. public $email;
  106. /**
  107. * @var integer|string date_creation
  108. */
  109. public $date_creation;
  110. public $tms;
  111. public $private;
  112. public $signature;
  113. public $position;
  114. public $active;
  115. // END MODULEBUILDER PROPERTIES
  116. /**
  117. * Constructor
  118. *
  119. * @param DoliDb $db Database handler
  120. */
  121. public function __construct(DoliDB $db)
  122. {
  123. global $conf;
  124. $this->db = $db;
  125. if (empty($conf->global->MAIN_SHOW_TECHNICAL_ID)) {
  126. $this->fields['rowid']['visible'] = 0;
  127. }
  128. if (!isModEnabled('multicompany')) {
  129. $this->fields['entity']['enabled'] = 0;
  130. }
  131. }
  132. /**
  133. * Create object into database
  134. *
  135. * @param User $user User that creates
  136. * @param bool $notrigger false=launch triggers after, true=disable triggers
  137. * @return int <0 if KO, Id of created object if OK
  138. */
  139. public function create(User $user, $notrigger = false)
  140. {
  141. return $this->createCommon($user, $notrigger);
  142. }
  143. /**
  144. * Clone and object into another one
  145. *
  146. * @param User $user User that creates
  147. * @param int $fromid Id of object to clone
  148. * @return mixed New object created, <0 if KO
  149. */
  150. public function createFromClone(User $user, $fromid)
  151. {
  152. global $hookmanager, $langs;
  153. $error = 0;
  154. dol_syslog(__METHOD__, LOG_DEBUG);
  155. $object = new self($this->db);
  156. $this->db->begin();
  157. // Load source object
  158. $object->fetchCommon($fromid);
  159. // Reset some properties
  160. unset($object->id);
  161. unset($object->fk_user_creat);
  162. unset($object->import_key);
  163. // Clear fields
  164. $object->ref = "copy_of_".$object->ref;
  165. $object->title = $langs->trans("CopyOf")." ".$object->title;
  166. // ...
  167. // Create clone
  168. $object->context['createfromclone'] = 'createfromclone';
  169. $result = $object->createCommon($user);
  170. if ($result < 0) {
  171. $error++;
  172. $this->error = $object->error;
  173. $this->errors = $object->errors;
  174. }
  175. unset($object->context['createfromclone']);
  176. // End
  177. if (!$error) {
  178. $this->db->commit();
  179. return $object;
  180. } else {
  181. $this->db->rollback();
  182. return -1;
  183. }
  184. }
  185. /**
  186. * Load object in memory from the database
  187. *
  188. * @param int $id Id object
  189. * @param string $ref Ref
  190. * @return int <0 if KO, 0 if not found, >0 if OK
  191. */
  192. public function fetch($id, $ref = null)
  193. {
  194. $result = $this->fetchCommon($id, $ref);
  195. if ($result > 0 && !empty($this->table_element_line)) {
  196. $this->fetchLines();
  197. }
  198. return $result;
  199. }
  200. /**
  201. * Load object lines in memory from the database
  202. *
  203. * @return int <0 if KO, 0 if not found, >0 if OK
  204. */
  205. public function fetchLines()
  206. {
  207. $this->lines = array();
  208. // Load lines with object EmailSenderProfileLine
  209. return count($this->lines) ? 1 : 0;
  210. }
  211. /**
  212. * Update object into database
  213. *
  214. * @param User $user User that modifies
  215. * @param bool $notrigger false=launch triggers after, true=disable triggers
  216. * @return int <0 if KO, >0 if OK
  217. */
  218. public function update(User $user, $notrigger = false)
  219. {
  220. return $this->updateCommon($user, $notrigger);
  221. }
  222. /**
  223. * Delete object in database
  224. *
  225. * @param User $user User that deletes
  226. * @param bool $notrigger false=launch triggers after, true=disable triggers
  227. * @return int <0 if KO, >0 if OK
  228. */
  229. public function delete(User $user, $notrigger = false)
  230. {
  231. return $this->deleteCommon($user, $notrigger);
  232. }
  233. /**
  234. * Return a link to the object card (with optionaly the picto)
  235. *
  236. * @param int $withpicto Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto)
  237. * @return string String with URL
  238. */
  239. public function getNomUrl($withpicto = 0)
  240. {
  241. global $db, $conf, $langs;
  242. global $dolibarr_main_authentication, $dolibarr_main_demo;
  243. global $menumanager;
  244. $result = '';
  245. $companylink = '';
  246. $label = $this->label;
  247. $url = '';
  248. //$url = dol_buildpath('/monmodule/emailsenderprofile_card.php',1).'?id='.$this->id;
  249. $linkstart = '';
  250. $linkend = '';
  251. if ($withpicto) {
  252. $result .= ($linkstart.img_object($label, 'label', 'class="classfortooltip"').$linkend);
  253. if ($withpicto != 2) {
  254. $result .= ' ';
  255. }
  256. }
  257. $result .= $linkstart.$this->label.$linkend;
  258. return $result;
  259. }
  260. /**
  261. * Retourne le libelle du status d'un user (actif, inactif)
  262. *
  263. * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
  264. * @return string Label of status
  265. */
  266. public function getLibStatut($mode = 0)
  267. {
  268. return $this->LibStatut($this->active, $mode);
  269. }
  270. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  271. /**
  272. * Return the status
  273. *
  274. * @param int $status Id status
  275. * @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
  276. * @return string Label of status
  277. */
  278. public static function LibStatut($status, $mode = 0)
  279. {
  280. global $langs;
  281. if ($status == 1) {
  282. $label = $labelshort = $langs->transnoentitiesnoconv('Enabled');
  283. } else {
  284. $label = $labelshort = $langs->transnoentitiesnoconv('Disabled');
  285. }
  286. $statusType = 'status'.$status;
  287. if ($status == self::STATUS_ENABLED) {
  288. $statusType = 'status4';
  289. }
  290. return dolGetStatus($label, $labelshort, '', $statusType, $mode);
  291. }
  292. /**
  293. * Charge les informations d'ordre info dans l'objet commande
  294. *
  295. * @param int $id Id of order
  296. * @return void
  297. */
  298. public function info($id)
  299. {
  300. $sql = "SELECT rowid, date_creation as datec, tms as datem";
  301. $sql .= " FROM ".$this->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. $this->date_creation = $this->db->jdate($obj->datec);
  309. $this->date_modification = $this->db->jdate($obj->datem);
  310. }
  311. $this->db->free($result);
  312. } else {
  313. dol_print_error($this->db);
  314. }
  315. }
  316. /**
  317. * Initialise object with example values
  318. * Id must be 0 if object instance is a specimen
  319. *
  320. * @return void
  321. */
  322. public function initAsSpecimen()
  323. {
  324. $this->initAsSpecimenCommon();
  325. }
  326. }
  327. /**
  328. * Class EmailSenderProfileLine. You can also remove this and generate a CRUD class for lines objects.
  329. */
  330. /*
  331. class EmailSenderProfileLine
  332. {
  333. // @var int ID
  334. public $id;
  335. // @var mixed Sample line property 1
  336. public $prop1;
  337. // @var mixed Sample line property 2
  338. public $prop2;
  339. }
  340. */