adherent_type.class.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  1. <?php
  2. /* Copyright (C) 2002 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2008 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2009-2017 Regis Houssin <regis.houssin@inodbox.com>
  5. * Copyright (C) 2016 Charlie Benke <charlie@patas-monkey.com>
  6. * Copyright (C) 2018-2019 Thibault Foucart <support@ptibogxiv.net>
  7. * Copyright (C) 2021 Waël Almoman <info@almoman.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. */
  22. /**
  23. * \file htdocs/adherents/class/adherent_type.class.php
  24. * \ingroup member
  25. * \brief File of class to manage members types
  26. */
  27. require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
  28. /**
  29. * Class to manage members type
  30. */
  31. class AdherentType extends CommonObject
  32. {
  33. /**
  34. * @var string Name of table without prefix where object is stored
  35. */
  36. public $table_element = 'adherent_type';
  37. /**
  38. * @var string ID to identify managed object
  39. */
  40. public $element = 'adherent_type';
  41. /**
  42. * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png
  43. */
  44. public $picto = 'members';
  45. /**
  46. * 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
  47. * @var int
  48. */
  49. public $ismultientitymanaged = 1;
  50. /**
  51. * @var string
  52. * @deprecated Use label
  53. * @see $label
  54. */
  55. public $libelle;
  56. /**
  57. * @var string Adherent type label
  58. */
  59. public $label;
  60. /**
  61. * @var string Adherent type nature
  62. */
  63. public $morphy;
  64. public $duration;
  65. /**
  66. * type expiration
  67. */
  68. public $duration_value;
  69. /**
  70. * Expiration unit
  71. */
  72. public $duration_unit;
  73. /**
  74. * @var int Subsription required (0 or 1)
  75. */
  76. public $subscription;
  77. /**
  78. * @var float|string Amount for subscription (null or '' means not defined)
  79. */
  80. public $amount;
  81. /**
  82. * @var int Amount can be choosen by the visitor during subscription (0 or 1)
  83. */
  84. public $caneditamount;
  85. /**
  86. * @var string Public note
  87. * @deprecated
  88. */
  89. public $note;
  90. /** @var string Public note */
  91. public $note_public;
  92. /** @var integer Can vote */
  93. public $vote;
  94. /** @var string Email sent during validation of member */
  95. public $mail_valid;
  96. /** @var string Email sent after recording a new subscription */
  97. public $mail_subscription = '';
  98. /** @var string Email sent after resiliation */
  99. public $mail_resiliate = '';
  100. /** @var string Email sent after exclude */
  101. public $mail_exclude = '';
  102. /** @var array Array of members */
  103. public $members = array();
  104. /** @var string string other */
  105. public $other = array();
  106. /**
  107. * @var string description
  108. */
  109. public $description;
  110. /**
  111. * @var string email
  112. */
  113. public $email;
  114. /**
  115. * @var array multilangs
  116. */
  117. public $multilangs = array();
  118. /**
  119. * Constructor
  120. *
  121. * @param DoliDB $db Database handler
  122. */
  123. public function __construct($db)
  124. {
  125. $this->db = $db;
  126. $this->status = 1;
  127. }
  128. /**
  129. * Load array this->multilangs
  130. *
  131. * @return int Return integer <0 if KO, >0 if OK
  132. */
  133. public function getMultiLangs()
  134. {
  135. global $langs;
  136. $current_lang = $langs->getDefaultLang();
  137. $sql = "SELECT lang, label, description, email";
  138. $sql .= " FROM ".MAIN_DB_PREFIX."adherent_type_lang";
  139. $sql .= " WHERE fk_type = ".((int) $this->id);
  140. $result = $this->db->query($sql);
  141. if ($result) {
  142. while ($obj = $this->db->fetch_object($result)) {
  143. //print 'lang='.$obj->lang.' current='.$current_lang.'<br>';
  144. if ($obj->lang == $current_lang) { // si on a les traduct. dans la langue courante on les charge en infos principales.
  145. $this->label = $obj->label;
  146. $this->description = $obj->description;
  147. $this->email = $obj->email;
  148. }
  149. $this->multilangs["$obj->lang"]["label"] = $obj->label;
  150. $this->multilangs["$obj->lang"]["description"] = $obj->description;
  151. $this->multilangs["$obj->lang"]["email"] = $obj->email;
  152. }
  153. return 1;
  154. } else {
  155. $this->error = "Error: ".$this->db->lasterror()." - ".$sql;
  156. return -1;
  157. }
  158. }
  159. /**
  160. * Update or add a translation for this member type
  161. *
  162. * @param User $user Object user making update
  163. * @return int Return integer <0 if KO, >0 if OK
  164. */
  165. public function setMultiLangs($user)
  166. {
  167. global $conf, $langs;
  168. $langs_available = $langs->get_available_languages(DOL_DOCUMENT_ROOT, 0, 2);
  169. $current_lang = $langs->getDefaultLang();
  170. foreach ($langs_available as $key => $value) {
  171. if ($key == $current_lang) {
  172. $sql = "SELECT rowid";
  173. $sql .= " FROM ".MAIN_DB_PREFIX."adherent_type_lang";
  174. $sql .= " WHERE fk_type = ".((int) $this->id);
  175. $sql .= " AND lang = '".$this->db->escape($key)."'";
  176. $result = $this->db->query($sql);
  177. if ($this->db->num_rows($result)) { // if there is already a description line for this language
  178. $sql2 = "UPDATE ".MAIN_DB_PREFIX."adherent_type_lang";
  179. $sql2 .= " SET";
  180. $sql2 .= " label = '".$this->db->escape($this->label)."',";
  181. $sql2 .= " description = '".$this->db->escape($this->description)."'";
  182. $sql2 .= " WHERE fk_type = ".((int) $this->id)." AND lang='".$this->db->escape($key)."'";
  183. } else {
  184. $sql2 = "INSERT INTO ".MAIN_DB_PREFIX."adherent_type_lang (fk_type, lang, label, description";
  185. $sql2 .= ")";
  186. $sql2 .= " VALUES(".((int) $this->id).",'".$this->db->escape($key)."','".$this->db->escape($this->label)."',";
  187. $sql2 .= " '".$this->db->escape($this->description)."'";
  188. $sql2 .= ")";
  189. }
  190. dol_syslog(get_class($this).'::setMultiLangs key = current_lang = '.$key);
  191. if (!$this->db->query($sql2)) {
  192. $this->error = $this->db->lasterror();
  193. return -1;
  194. }
  195. } elseif (isset($this->multilangs[$key])) {
  196. $sql = "SELECT rowid";
  197. $sql .= " FROM ".MAIN_DB_PREFIX."adherent_type_lang";
  198. $sql .= " WHERE fk_type = ".((int) $this->id);
  199. $sql .= " AND lang = '".$this->db->escape($key)."'";
  200. $result = $this->db->query($sql);
  201. if ($this->db->num_rows($result)) { // if there is already a description line for this language
  202. $sql2 = "UPDATE ".MAIN_DB_PREFIX."adherent_type_lang";
  203. $sql2 .= " SET ";
  204. $sql2 .= " label = '".$this->db->escape($this->multilangs["$key"]["label"])."',";
  205. $sql2 .= " description = '".$this->db->escape($this->multilangs["$key"]["description"])."'";
  206. $sql2 .= " WHERE fk_type = ".((int) $this->id)." AND lang='".$this->db->escape($key)."'";
  207. } else {
  208. $sql2 = "INSERT INTO ".MAIN_DB_PREFIX."adherent_type_lang (fk_type, lang, label, description";
  209. $sql2 .= ")";
  210. $sql2 .= " VALUES(".$this->id.",'".$this->db->escape($key)."','".$this->db->escape($this->multilangs["$key"]["label"])."',";
  211. $sql2 .= " '".$this->db->escape($this->multilangs["$key"]["description"])."'";
  212. $sql2 .= ")";
  213. }
  214. // We do not save if main fields are empty
  215. if ($this->multilangs["$key"]["label"] || $this->multilangs["$key"]["description"]) {
  216. if (!$this->db->query($sql2)) {
  217. $this->error = $this->db->lasterror();
  218. return -1;
  219. }
  220. }
  221. } else {
  222. // language is not current language and we didn't provide a multilang description for this language
  223. }
  224. }
  225. // Call trigger
  226. $result = $this->call_trigger('MEMBER_TYPE_SET_MULTILANGS', $user);
  227. if ($result < 0) {
  228. $this->error = $this->db->lasterror();
  229. return -1;
  230. }
  231. // End call triggers
  232. return 1;
  233. }
  234. /**
  235. * Delete a language for this member type
  236. *
  237. * @param string $langtodelete Language code to delete
  238. * @param User $user Object user making delete
  239. * @return int Return integer <0 if KO, >0 if OK
  240. */
  241. public function delMultiLangs($langtodelete, $user)
  242. {
  243. $sql = "DELETE FROM ".MAIN_DB_PREFIX."adherent_type_lang";
  244. $sql .= " WHERE fk_type = ".((int) $this->id)." AND lang = '".$this->db->escape($langtodelete)."'";
  245. dol_syslog(get_class($this).'::delMultiLangs', LOG_DEBUG);
  246. $result = $this->db->query($sql);
  247. if ($result) {
  248. // Call trigger
  249. $result = $this->call_trigger('MEMBER_TYPE_DEL_MULTILANGS', $user);
  250. if ($result < 0) {
  251. $this->error = $this->db->lasterror();
  252. dol_syslog(get_class($this).'::delMultiLangs error='.$this->error, LOG_ERR);
  253. return -1;
  254. }
  255. // End call triggers
  256. return 1;
  257. } else {
  258. $this->error = $this->db->lasterror();
  259. dol_syslog(get_class($this).'::delMultiLangs error='.$this->error, LOG_ERR);
  260. return -1;
  261. }
  262. }
  263. /**
  264. * Function to create the member type
  265. *
  266. * @param User $user User making creation
  267. * @param int $notrigger 1=do not execute triggers, 0 otherwise
  268. * @return int >0 if OK, < 0 if KO
  269. */
  270. public function create($user, $notrigger = 0)
  271. {
  272. global $langs, $conf;
  273. $error = 0;
  274. $this->status = (int) $this->status;
  275. $this->label = trim($this->label);
  276. $this->db->begin();
  277. $sql = "INSERT INTO ".MAIN_DB_PREFIX."adherent_type (";
  278. $sql .= " morphy";
  279. $sql .= ", libelle";
  280. $sql .= ", entity";
  281. $sql .= ") VALUES (";
  282. $sql .= "'".$this->db->escape($this->morphy)."'";
  283. $sql .= ", '".$this->db->escape($this->label)."'";
  284. $sql .= ", ".((int) $conf->entity);
  285. $sql .= ")";
  286. dol_syslog("Adherent_type::create", LOG_DEBUG);
  287. $result = $this->db->query($sql);
  288. if ($result) {
  289. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."adherent_type");
  290. $result = $this->update($user, 1);
  291. if ($result < 0) {
  292. $this->db->rollback();
  293. return -3;
  294. }
  295. if (!$notrigger) {
  296. // Call trigger
  297. $result = $this->call_trigger('MEMBER_TYPE_CREATE', $user);
  298. if ($result < 0) {
  299. $error++;
  300. }
  301. // End call triggers
  302. }
  303. if (!$error) {
  304. $this->db->commit();
  305. return $this->id;
  306. } else {
  307. dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
  308. $this->db->rollback();
  309. return -2;
  310. }
  311. } else {
  312. $this->error = $this->db->lasterror();
  313. $this->db->rollback();
  314. return -1;
  315. }
  316. }
  317. /**
  318. * Updating the type in the database
  319. *
  320. * @param User $user Object user making change
  321. * @param int $notrigger 1=do not execute triggers, 0 otherwise
  322. * @return int >0 if OK, < 0 if KO
  323. */
  324. public function update($user, $notrigger = 0)
  325. {
  326. global $langs, $conf, $hookmanager;
  327. $error = 0;
  328. $this->label = trim($this->label);
  329. if (empty($this->note_public) && !empty($this->note)) { // For backward compatibility
  330. $this->note_public = $this->note;
  331. }
  332. $this->db->begin();
  333. $sql = "UPDATE ".MAIN_DB_PREFIX."adherent_type ";
  334. $sql .= "SET ";
  335. $sql .= "statut = ".((int) $this->status).",";
  336. $sql .= "libelle = '".$this->db->escape($this->label)."',";
  337. $sql .= "morphy = '".$this->db->escape($this->morphy)."',";
  338. $sql .= "subscription = '".$this->db->escape($this->subscription)."',";
  339. $sql .= "amount = ".((empty($this->amount) && $this->amount == '') ? 'null' : ((float) $this->amount)).",";
  340. $sql .= "caneditamount = ".((int) $this->caneditamount).",";
  341. $sql .= "duration = '".$this->db->escape($this->duration_value.$this->duration_unit)."',";
  342. $sql .= "note = '".$this->db->escape($this->note_public)."',";
  343. $sql .= "vote = ".(int) $this->db->escape($this->vote).",";
  344. $sql .= "mail_valid = '".$this->db->escape($this->mail_valid)."'";
  345. $sql .= " WHERE rowid =".((int) $this->id);
  346. $result = $this->db->query($sql);
  347. if ($result) {
  348. $this->description = $this->db->escape($this->note_public);
  349. // Multilangs
  350. if (getDolGlobalInt('MAIN_MULTILANGS')) {
  351. if ($this->setMultiLangs($user) < 0) {
  352. $this->error = $langs->trans("Error")." : ".$this->db->error()." - ".$sql;
  353. return -2;
  354. }
  355. }
  356. $action = 'update';
  357. // Actions on extra fields
  358. if (!$error) {
  359. $result = $this->insertExtraFields();
  360. if ($result < 0) {
  361. $error++;
  362. }
  363. }
  364. if (!$error && !$notrigger) {
  365. // Call trigger
  366. $result = $this->call_trigger('MEMBER_TYPE_MODIFY', $user);
  367. if ($result < 0) {
  368. $error++;
  369. }
  370. // End call triggers
  371. }
  372. if (!$error) {
  373. $this->db->commit();
  374. return 1;
  375. } else {
  376. $this->db->rollback();
  377. dol_syslog(get_class($this)."::update ".$this->error, LOG_ERR);
  378. return -$error;
  379. }
  380. } else {
  381. $this->error = $this->db->lasterror();
  382. $this->db->rollback();
  383. return -1;
  384. }
  385. }
  386. /**
  387. * Function to delete the member's status
  388. *
  389. * @param User $user User making the deletion
  390. * @return int > 0 if OK, 0 if not found, < 0 if KO
  391. */
  392. public function delete($user)
  393. {
  394. $error = 0;
  395. $sql = "DELETE FROM ".MAIN_DB_PREFIX."adherent_type";
  396. $sql .= " WHERE rowid = ".((int) $this->id);
  397. $resql = $this->db->query($sql);
  398. if ($resql) {
  399. // Call trigger
  400. $result = $this->call_trigger('MEMBER_TYPE_DELETE', $user);
  401. if ($result < 0) {
  402. $error++;
  403. $this->db->rollback();
  404. return -2;
  405. }
  406. // End call triggers
  407. $this->db->commit();
  408. return 1;
  409. } else {
  410. $this->db->rollback();
  411. $this->error = $this->db->lasterror();
  412. return -1;
  413. }
  414. }
  415. /**
  416. * Function that retrieves the properties of a membership type
  417. *
  418. * @param int $rowid Id of member type to load
  419. * @return int Return integer <0 if KO, >0 if OK
  420. */
  421. public function fetch($rowid)
  422. {
  423. global $langs, $conf;
  424. $sql = "SELECT d.rowid, d.libelle as label, d.morphy, d.statut as status, d.duration, d.subscription, d.amount, d.caneditamount, d.mail_valid, d.note as note_public, d.vote";
  425. $sql .= " FROM ".MAIN_DB_PREFIX."adherent_type as d";
  426. $sql .= " WHERE d.rowid = ".(int) $rowid;
  427. dol_syslog("Adherent_type::fetch", LOG_DEBUG);
  428. $resql = $this->db->query($sql);
  429. if ($resql) {
  430. if ($this->db->num_rows($resql)) {
  431. $obj = $this->db->fetch_object($resql);
  432. $this->id = $obj->rowid;
  433. $this->ref = $obj->rowid;
  434. $this->label = $obj->label;
  435. $this->morphy = $obj->morphy;
  436. $this->status = $obj->status;
  437. $this->duration = $obj->duration;
  438. $this->duration_value = substr($obj->duration, 0, dol_strlen($obj->duration) - 1);
  439. $this->duration_unit = substr($obj->duration, -1);
  440. $this->subscription = $obj->subscription;
  441. $this->amount = $obj->amount;
  442. $this->caneditamount = $obj->caneditamount;
  443. $this->mail_valid = $obj->mail_valid;
  444. $this->note = $obj->note_public; // deprecated
  445. $this->note_public = $obj->note_public;
  446. $this->vote = $obj->vote;
  447. // multilangs
  448. if (getDolGlobalInt('MAIN_MULTILANGS')) {
  449. $this->getMultiLangs();
  450. }
  451. // fetch optionals attributes and labels
  452. $this->fetch_optionals();
  453. }
  454. return 1;
  455. } else {
  456. $this->error = $this->db->lasterror();
  457. return -1;
  458. }
  459. }
  460. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  461. /**
  462. * Return list of members' type
  463. *
  464. * @param int $status Filter on status of type
  465. * @return array List of types of members
  466. */
  467. public function liste_array($status = -1)
  468. {
  469. // phpcs:enable
  470. global $conf, $langs;
  471. $adherenttypes = array();
  472. $sql = "SELECT rowid, libelle as label";
  473. $sql .= " FROM ".MAIN_DB_PREFIX."adherent_type";
  474. $sql .= " WHERE entity IN (".getEntity('member_type').")";
  475. if ($status >= 0) {
  476. $sql .= " AND statut = ".((int) $status);
  477. }
  478. $resql = $this->db->query($sql);
  479. if ($resql) {
  480. $nump = $this->db->num_rows($resql);
  481. if ($nump) {
  482. $i = 0;
  483. while ($i < $nump) {
  484. $obj = $this->db->fetch_object($resql);
  485. $adherenttypes[$obj->rowid] = $langs->trans($obj->label);
  486. $i++;
  487. }
  488. }
  489. } else {
  490. print $this->db->error();
  491. }
  492. return $adherenttypes;
  493. }
  494. /**
  495. * Return the array of all amounts per membership type id
  496. *
  497. * @param int $status Filter on status of type
  498. * @return array Array of membership type
  499. */
  500. public function amountByType($status = null)
  501. {
  502. global $conf, $langs;
  503. $amountbytype = array();
  504. $sql = "SELECT rowid, amount";
  505. $sql .= " FROM ".MAIN_DB_PREFIX."adherent_type";
  506. $sql .= " WHERE entity IN (".getEntity('member_type').")";
  507. if ($status !== null) {
  508. $sql .= " AND statut = ".((int) $status);
  509. }
  510. $resql = $this->db->query($sql);
  511. if ($resql) {
  512. $nump = $this->db->num_rows($resql);
  513. if ($nump) {
  514. $i = 0;
  515. while ($i < $nump) {
  516. $obj = $this->db->fetch_object($resql);
  517. $amountbytype[$obj->rowid] = $obj->amount;
  518. $i++;
  519. }
  520. }
  521. } else {
  522. print $this->db->error();
  523. }
  524. return $amountbytype;
  525. }
  526. /**
  527. * Return array of Member objects for member type this->id (or all if this->id not defined)
  528. *
  529. * @param string $excludefilter Filter to exclude. This value must not come from a user input.
  530. * @param int $mode 0=Return array of member instance
  531. * 1=Return array of member instance without extra data
  532. * 2=Return array of members id only
  533. * @return mixed Array of members or -1 on error
  534. */
  535. public function listMembersForMemberType($excludefilter = '', $mode = 0)
  536. {
  537. global $conf, $user;
  538. $ret = array();
  539. $sql = "SELECT a.rowid";
  540. $sql .= " FROM ".MAIN_DB_PREFIX."adherent as a";
  541. $sql .= " WHERE a.entity IN (".getEntity('member').")";
  542. $sql .= " AND a.fk_adherent_type = ".((int) $this->id);
  543. if (!empty($excludefilter)) {
  544. $sql .= ' AND ('.$excludefilter.')';
  545. }
  546. dol_syslog(get_class($this)."::listMembersForMemberType", LOG_DEBUG);
  547. $resql = $this->db->query($sql);
  548. if ($resql) {
  549. while ($obj = $this->db->fetch_object($resql)) {
  550. if (!array_key_exists($obj->rowid, $ret)) {
  551. if ($mode < 2) {
  552. $memberstatic = new Adherent($this->db);
  553. if ($mode == 1) {
  554. $memberstatic->fetch($obj->rowid, '', '', '', false, false);
  555. } else {
  556. $memberstatic->fetch($obj->rowid);
  557. }
  558. $ret[$obj->rowid] = $memberstatic;
  559. } else {
  560. $ret[$obj->rowid] = $obj->rowid;
  561. }
  562. }
  563. }
  564. $this->db->free($resql);
  565. $this->members = $ret;
  566. return $ret;
  567. } else {
  568. $this->error = $this->db->lasterror();
  569. return -1;
  570. }
  571. }
  572. /**
  573. * Return translated label by the nature of a adherent (physical or moral)
  574. *
  575. * @param string $morphy Nature of the adherent (physical or moral)
  576. * @return string Label
  577. */
  578. public function getmorphylib($morphy = '')
  579. {
  580. global $langs;
  581. if ($morphy == 'phy') {
  582. return $langs->trans("Physical");
  583. } elseif ($morphy == 'mor') {
  584. return $langs->trans("Moral");
  585. } else {
  586. return $langs->trans("MorAndPhy");
  587. }
  588. //return $morphy;
  589. }
  590. /**
  591. * getTooltipContentArray
  592. * @param array $params params to construct tooltip data
  593. * @since v18
  594. * @return array
  595. */
  596. public function getTooltipContentArray($params)
  597. {
  598. global $conf, $langs, $user;
  599. $langs->load('members');
  600. $datas = [];
  601. $datas['picto'] = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("MemberType").'</u> '.$this->getLibStatut(4);
  602. $datas['label'] = '<br>'.$langs->trans("Label").': '.$this->label;
  603. if (isset($this->subscription)) {
  604. $datas['subscription'] = '<br>'.$langs->trans("SubscriptionRequired").': '.yn($this->subscription);
  605. }
  606. if (isset($this->vote)) {
  607. $datas['vote'] = '<br>'.$langs->trans("VoteAllowed").': '.yn($this->vote);
  608. }
  609. if (isset($this->duration)) {
  610. $datas['duration'] = '<br>'.$langs->trans("Duration").': '.$this->duration_value;
  611. if ($this->duration_value > 1) {
  612. $dur = array("i"=>$langs->trans("Minutes"), "h"=>$langs->trans("Hours"), "d"=>$langs->trans("Days"), "w"=>$langs->trans("Weeks"), "m"=>$langs->trans("Months"), "y"=>$langs->trans("Years"));
  613. } elseif ($this->duration_value > 0) {
  614. $dur = array("i"=>$langs->trans("Minute"), "h"=>$langs->trans("Hour"), "d"=>$langs->trans("Day"), "w"=>$langs->trans("Week"), "m"=>$langs->trans("Month"), "y"=>$langs->trans("Year"));
  615. }
  616. $datas['duration'] .= "&nbsp;" . (!empty($this->duration_unit) && isset($dur[$this->duration_unit]) ? $langs->trans($dur[$this->duration_unit]) : '');
  617. }
  618. return $datas;
  619. }
  620. /**
  621. * Return clicable name (with picto eventually)
  622. *
  623. * @param int $withpicto 0=No picto, 1=Include picto into link, 2=Only picto
  624. * @param int $maxlen length max label
  625. * @param int $notooltip 1=Disable tooltip
  626. * @param string $morecss Add more css on link
  627. * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
  628. * @return string String with URL
  629. */
  630. public function getNomUrl($withpicto = 0, $maxlen = 0, $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
  631. {
  632. global $langs;
  633. $result = '';
  634. $option = '';
  635. $classfortooltip = 'classfortooltip';
  636. $dataparams = '';
  637. $params = [
  638. 'id' => $this->id,
  639. 'objecttype' => $this->element,
  640. 'option' => $option,
  641. 'nofetch' => 1,
  642. ];
  643. if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
  644. $classfortooltip = 'classforajaxtooltip';
  645. $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
  646. $label = '';
  647. } else {
  648. $label = implode($this->getTooltipContentArray($params));
  649. }
  650. $url = DOL_URL_ROOT.'/adherents/type.php?rowid='.((int) $this->id);
  651. if ($option != 'nolink') {
  652. // Add param to save lastsearch_values or not
  653. $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
  654. if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
  655. $add_save_lastsearch_values = 1;
  656. }
  657. if ($add_save_lastsearch_values) {
  658. $url .= '&save_lastsearch_values=1';
  659. }
  660. }
  661. $linkstart = '<a href="'.$url.'"';
  662. $linkstart .= ($label ? ' title="'.dol_escape_htmltag($label, 1).'"' : ' title="tocomplete"');
  663. $linkstart .= $dataparams.' class="'.$classfortooltip.'">';
  664. $linkend = '</a>';
  665. $result .= $linkstart;
  666. if ($withpicto) {
  667. $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), (' class="'.(($withpicto != 2) ? 'paddingright' : '').'"'), 0, 0, $notooltip ? 0 : 1);
  668. }
  669. if ($withpicto != 2) {
  670. $result .= ($maxlen ? dol_trunc($this->label, $maxlen) : $this->label);
  671. }
  672. $result .= $linkend;
  673. return $result;
  674. }
  675. /**
  676. * Return label of status (activity, closed)
  677. *
  678. * @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
  679. * @return string Label of status
  680. */
  681. public function getLibStatut($mode = 0)
  682. {
  683. return $this->LibStatut($this->status, $mode);
  684. }
  685. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  686. /**
  687. * Return the label of a given status
  688. *
  689. * @param int $status Status id
  690. * @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
  691. * @return string Status label
  692. */
  693. public function LibStatut($status, $mode = 0)
  694. {
  695. // phpcs:enable
  696. global $langs;
  697. $langs->load('companies');
  698. $statusType = 'status4';
  699. if ($status == 0) {
  700. $statusType = 'status5';
  701. }
  702. if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
  703. $this->labelStatus[0] = $langs->transnoentitiesnoconv("ActivityCeased");
  704. $this->labelStatus[1] = $langs->transnoentitiesnoconv("InActivity");
  705. $this->labelStatusShort[0] = $langs->transnoentitiesnoconv("ActivityCeased");
  706. $this->labelStatusShort[1] = $langs->transnoentitiesnoconv("InActivity");
  707. }
  708. return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
  709. }
  710. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  711. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  712. /**
  713. * Retourne chaine DN complete dans l'annuaire LDAP pour l'objet
  714. *
  715. * @param array $info Info array loaded by _load_ldap_info
  716. * @param int $mode 0=Return full DN (uid=qqq,ou=xxx,dc=aaa,dc=bbb)
  717. * 1=Return DN without key inside (ou=xxx,dc=aaa,dc=bbb)
  718. * 2=Return key only (uid=qqq)
  719. * @return string DN
  720. */
  721. public function _load_ldap_dn($info, $mode = 0)
  722. {
  723. // phpcs:enable
  724. global $conf;
  725. $dn = '';
  726. if ($mode == 0) {
  727. $dn = getDolGlobalString('LDAP_KEY_MEMBERS_TYPES') . "=".$info[getDolGlobalString('LDAP_KEY_MEMBERS_TYPES')]."," . getDolGlobalString('LDAP_MEMBER_TYPE_DN');
  728. }
  729. if ($mode == 1) {
  730. $dn = getDolGlobalString('LDAP_MEMBER_TYPE_DN');
  731. }
  732. if ($mode == 2) {
  733. $dn = getDolGlobalString('LDAP_KEY_MEMBERS_TYPES') . "=".$info[getDolGlobalString('LDAP_KEY_MEMBERS_TYPES')];
  734. }
  735. return $dn;
  736. }
  737. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  738. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  739. /**
  740. * Initialize the info array (array of LDAP values) that will be used to call LDAP functions
  741. *
  742. * @return array Tableau info des attributs
  743. */
  744. public function _load_ldap_info()
  745. {
  746. // phpcs:enable
  747. global $conf, $langs;
  748. $info = array();
  749. // Object classes
  750. $info["objectclass"] = explode(',', getDolGlobalString('LDAP_MEMBER_TYPE_OBJECT_CLASS'));
  751. if (empty($this->note_public) && !empty($this->note)) { // For backward compatibility
  752. $this->note_public = $this->note;
  753. }
  754. // Champs
  755. if ($this->label && getDolGlobalString('LDAP_MEMBER_TYPE_FIELD_FULLNAME')) {
  756. $info[getDolGlobalString('LDAP_MEMBER_TYPE_FIELD_FULLNAME')] = $this->label;
  757. }
  758. if ($this->note_public && getDolGlobalString('LDAP_MEMBER_TYPE_FIELD_DESCRIPTION')) {
  759. $info[getDolGlobalString('LDAP_MEMBER_TYPE_FIELD_DESCRIPTION')] = dol_string_nohtmltag($this->note_public, 0, 'UTF-8', 1);
  760. }
  761. if (getDolGlobalString('LDAP_MEMBER_TYPE_FIELD_GROUPMEMBERS')) {
  762. $valueofldapfield = array();
  763. foreach ($this->members as $key => $val) { // This is array of users for group into dolibarr database.
  764. $member = new Adherent($this->db);
  765. $member->fetch($val->id, '', '', '', false, false);
  766. $info2 = $member->_load_ldap_info();
  767. $valueofldapfield[] = $member->_load_ldap_dn($info2);
  768. }
  769. $info[getDolGlobalString('LDAP_MEMBER_TYPE_FIELD_GROUPMEMBERS')] = (!empty($valueofldapfield) ? $valueofldapfield : '');
  770. }
  771. return $info;
  772. }
  773. /**
  774. * Initialise an instance with random values.
  775. * Used to build previews or test instances.
  776. * id must be 0 if object instance is a specimen.
  777. *
  778. * @return void
  779. */
  780. public function initAsSpecimen()
  781. {
  782. global $user;
  783. // Initialise parametres
  784. $this->id = 0;
  785. $this->ref = 'MTSPEC';
  786. $this->specimen = 1;
  787. $this->label = 'MEMBERS TYPE SPECIMEN';
  788. $this->note_public = 'This is a public note';
  789. $this->mail_valid = 'This is welcome email';
  790. $this->subscription = 1;
  791. $this->caneditamount = 0;
  792. $this->vote = 0;
  793. $this->status = 1;
  794. // Members of this member type is just me
  795. $this->members = array(
  796. $user->id => $user
  797. );
  798. }
  799. /**
  800. * getMailOnValid
  801. *
  802. * @return string Return mail content of type or empty
  803. */
  804. public function getMailOnValid()
  805. {
  806. if (!empty($this->mail_valid) && trim(dol_htmlentitiesbr_decode($this->mail_valid))) {
  807. return $this->mail_valid;
  808. }
  809. return '';
  810. }
  811. /**
  812. * getMailOnSubscription
  813. *
  814. * @return string Return mail content of type or empty
  815. */
  816. public function getMailOnSubscription()
  817. {
  818. // mail_subscription not defined so never used
  819. if (!empty($this->mail_subscription) && trim(dol_htmlentitiesbr_decode($this->mail_subscription))) { // Property not yet defined
  820. return $this->mail_subscription;
  821. }
  822. return '';
  823. }
  824. /**
  825. * getMailOnResiliate
  826. *
  827. * @return string Return mail model content of type or empty
  828. */
  829. public function getMailOnResiliate()
  830. {
  831. // NOTE mail_resiliate not defined so never used
  832. if (!empty($this->mail_resiliate) && trim(dol_htmlentitiesbr_decode($this->mail_resiliate))) { // Property not yet defined
  833. return $this->mail_resiliate;
  834. }
  835. return '';
  836. }
  837. /**
  838. * getMailOnExclude
  839. *
  840. * @return string Return mail model content of type or empty
  841. */
  842. public function getMailOnExclude()
  843. {
  844. // NOTE mail_exclude not defined so never used
  845. if (!empty($this->mail_exclude) && trim(dol_htmlentitiesbr_decode($this->mail_exclude))) { // Property not yet defined
  846. return $this->mail_exclude;
  847. }
  848. return '';
  849. }
  850. /**
  851. * Return clicable link of object (with eventually picto)
  852. *
  853. * @param string $option Where point the link (0=> main card, 1,2 => shipment, 'nolink'=>No link)
  854. * @param array $arraydata Array of data
  855. * @return string HTML Code for Kanban thumb.
  856. */
  857. public function getKanbanView($option = '', $arraydata = null)
  858. {
  859. global $langs, $user;
  860. //$selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
  861. $return = '<div class="box-flex-item box-flex-grow-zero">';
  862. $return .= '<div class="info-box info-box-sm">';
  863. $return .= '<span class="info-box-icon bg-infobox-action">';
  864. $return .= img_picto('', $this->picto);
  865. $return .= '</span>';
  866. $return .= '<div class="info-box-content">';
  867. $return .= '<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.(method_exists($this, 'getNomUrl') ? $this->getNomUrl() : $this->ref).'</span>';
  868. //$return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
  869. if ($user->hasRight('adherent', 'configurer')) {
  870. $return .= '<span class="right paddingleft"><a class="editfielda" href="'.$_SERVER["PHP_SELF"].'?action=edit&rowid='.urlencode($this->ref).'">'.img_edit().'</a></span>';
  871. } else {
  872. $return .= '<span class="right">&nbsp;</span>';
  873. }
  874. if (property_exists($this, 'vote')) {
  875. $return .= '<br><span class="info-box-label opacitymedium">'.$langs->trans("VoteAllowed").' : '.yn($this->vote).'</span>';
  876. }
  877. if (property_exists($this, 'amount')) {
  878. if (is_null($this->amount) || $this->amount === '') {
  879. $return .= '<br>';
  880. } else {
  881. $return .= '<br><span class="info-box-label opacitymedium">'.$langs->trans("Amount").'</span>';
  882. $return .= '<span class="amount"> : '.price($this->amount).'</span>';
  883. }
  884. }
  885. if (method_exists($this, 'getLibStatut')) {
  886. $return .= '<br><div class="info-box-status">'.$this->getLibStatut(3).'</div>';
  887. }
  888. $return .= '</div>';
  889. $return .= '</div>';
  890. $return .= '</div>';
  891. return $return;
  892. }
  893. }