advtargetemailing.class.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  1. <?php
  2. /* Advance Targeting Emailling for mass emailing module
  3. * Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro>
  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 <http://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file comm/mailing/class/advtargetemailing.class.php
  20. * \ingroup mailing
  21. * \brief This file is an example CRUD class file (Create/Read/Update/Delete)
  22. */
  23. /**
  24. * Class to manage advanced emailing target selector
  25. */
  26. class AdvanceTargetingMailing extends CommonObject
  27. {
  28. /**
  29. * @var DoliDB Database handler.
  30. */
  31. public $db;
  32. /**
  33. * @var string Error code (or message)
  34. */
  35. public $error='';
  36. /**
  37. * @var string[] Error codes (or messages)
  38. */
  39. public $errors = array();
  40. /**
  41. * @var string ID to identify managed object
  42. */
  43. public $element='advtargetemailing';
  44. /**
  45. * @var string Name of table without prefix where object is stored
  46. */
  47. public $table_element='advtargetemailing';
  48. /**
  49. * @var int ID
  50. */
  51. public $id;
  52. var $name;
  53. var $entity;
  54. var $fk_element;
  55. var $type_element;
  56. var $filtervalue;
  57. var $fk_user_author;
  58. var $datec='';
  59. var $fk_user_mod;
  60. var $tms='';
  61. var $select_target_type = array();
  62. var $type_statuscommprospect=array();
  63. var $thirdparty_lines;
  64. var $contact_lines;
  65. /**
  66. * Constructor
  67. *
  68. * @param DoliDb $db Database handler
  69. */
  70. function __construct($db)
  71. {
  72. global $langs;
  73. $langs->load('customers');
  74. $this->db = $db;
  75. $this->select_target_type = array(
  76. '2' => $langs->trans('Contacts'),
  77. '1' => $langs->trans('Contacts') . '+' . $langs->trans('ThirdParty'),
  78. '3' => $langs->trans('ThirdParty'),
  79. '4' => $langs->trans('ContactsWithThirdpartyFilter')
  80. );
  81. $this->type_statuscommprospect = array(
  82. - 1 => $langs->trans("StatusProspect-1"),
  83. 0 => $langs->trans("StatusProspect0"),
  84. 1 => $langs->trans("StatusProspect1"),
  85. 2 => $langs->trans("StatusProspect2"),
  86. 3 => $langs->trans("StatusProspect3")
  87. );
  88. }
  89. /**
  90. * Create object into database
  91. *
  92. * @param User $user User that creates
  93. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  94. * @return int <0 if KO, Id of created object if OK
  95. */
  96. function create($user, $notrigger=0)
  97. {
  98. global $conf, $langs;
  99. $error=0;
  100. // Clean parameters
  101. if (isset($this->fk_element)) $this->fk_element=trim($this->fk_element);
  102. if (isset($this->type_element)) $this->type_element=trim($this->type_element);
  103. if (isset($this->name)) $this->name=trim($this->name);
  104. if (isset($this->filtervalue)) $this->filtervalue=trim($this->filtervalue);
  105. if (isset($this->fk_user_author)) $this->fk_user_author=trim($this->fk_user_author);
  106. if (isset($this->fk_user_mod)) $this->fk_user_mod=trim($this->fk_user_mod);
  107. // Check parameters
  108. // Put here code to add control on parameters values
  109. // Insert request
  110. $sql = "INSERT INTO ".MAIN_DB_PREFIX."advtargetemailing(";
  111. $sql.= "name,";
  112. $sql.= "entity,";
  113. $sql.= "fk_element,";
  114. $sql.= "type_element,";
  115. $sql.= "filtervalue,";
  116. $sql.= "fk_user_author,";
  117. $sql.= "datec,";
  118. $sql.= "fk_user_mod";
  119. $sql.= ") VALUES (";
  120. $sql.= " ".(! isset($this->name)?'NULL':"'".$this->db->escape($this->name)."'").",";
  121. $sql.= " ".$conf->entity.",";
  122. $sql.= " ".(! isset($this->fk_element)?'NULL':"'".$this->db->escape($this->fk_element)."'").",";
  123. $sql.= " ".(! isset($this->type_element)?'NULL':"'".$this->db->escape($this->type_element)."'").",";
  124. $sql.= " ".(! isset($this->filtervalue)?'NULL':"'".$this->db->escape($this->filtervalue)."'").",";
  125. $sql.= " ".$user->id.",";
  126. $sql.= " '".$this->db->idate(dol_now())."',";
  127. $sql.= " ".$user->id;
  128. $sql.= ")";
  129. $this->db->begin();
  130. dol_syslog(get_class($this)."::create sql=".$sql, LOG_DEBUG);
  131. $resql=$this->db->query($sql);
  132. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  133. if (! $error)
  134. {
  135. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."advtargetemailing");
  136. if (! $notrigger)
  137. {
  138. // Uncomment this and change MYOBJECT to your own tag if you
  139. // want this action calls a trigger.
  140. //// Call triggers
  141. //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  142. //$interface=new Interfaces($this->db);
  143. //$result=$interface->run_triggers('MYOBJECT_CREATE',$this,$user,$langs,$conf);
  144. //if ($result < 0) { $error++; $this->errors=$interface->errors; }
  145. //// End call triggers
  146. }
  147. }
  148. // Commit or rollback
  149. if ($error)
  150. {
  151. foreach($this->errors as $errmsg)
  152. {
  153. dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
  154. $this->error.=($this->error?', '.$errmsg:$errmsg);
  155. }
  156. $this->db->rollback();
  157. return -1*$error;
  158. }
  159. else
  160. {
  161. $this->db->commit();
  162. return $this->id;
  163. }
  164. }
  165. /**
  166. * Load object in memory from the database
  167. *
  168. * @param int $id Id object
  169. * @return int <0 if KO, >0 if OK
  170. */
  171. function fetch($id)
  172. {
  173. global $langs;
  174. $sql = "SELECT";
  175. $sql.= " t.rowid,";
  176. $sql.= " t.name,";
  177. $sql.= " t.entity,";
  178. $sql.= " t.fk_element,";
  179. $sql.= " t.type_element,";
  180. $sql.= " t.filtervalue,";
  181. $sql.= " t.fk_user_author,";
  182. $sql.= " t.datec,";
  183. $sql.= " t.fk_user_mod,";
  184. $sql.= " t.tms";
  185. $sql.= " FROM ".MAIN_DB_PREFIX."advtargetemailing as t";
  186. $sql.= " WHERE t.rowid = ".$id;
  187. dol_syslog(get_class($this)."::fetch sql=".$sql, LOG_DEBUG);
  188. $resql=$this->db->query($sql);
  189. if ($resql)
  190. {
  191. if ($this->db->num_rows($resql))
  192. {
  193. $obj = $this->db->fetch_object($resql);
  194. $this->id = $obj->rowid;
  195. $this->name = $obj->name;
  196. $this->entity = $obj->entity;
  197. $this->fk_element = $obj->fk_element;
  198. $this->type_element = $obj->type_element;
  199. $this->filtervalue = $obj->filtervalue;
  200. $this->fk_user_author = $obj->fk_user_author;
  201. $this->datec = $this->db->jdate($obj->datec);
  202. $this->fk_user_mod = $obj->fk_user_mod;
  203. $this->tms = $this->db->jdate($obj->tms);
  204. }
  205. $this->db->free($resql);
  206. return 1;
  207. }
  208. else
  209. {
  210. $this->error="Error ".$this->db->lasterror();
  211. dol_syslog(get_class($this)."::fetch ".$this->error, LOG_ERR);
  212. return -1;
  213. }
  214. }
  215. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  216. /**
  217. * Load object in memory from the database
  218. *
  219. * @param int $id Id object
  220. * @return int <0 if KO, >0 if OK
  221. */
  222. function fetch_by_mailing($id=0)
  223. {
  224. // phpcs:enable
  225. global $langs;
  226. $sql = "SELECT";
  227. $sql.= " t.rowid,";
  228. $sql.= " t.name,";
  229. $sql.= " t.entity,";
  230. $sql.= " t.fk_element,";
  231. $sql.= " t.type_element,";
  232. $sql.= " t.filtervalue,";
  233. $sql.= " t.fk_user_author,";
  234. $sql.= " t.datec,";
  235. $sql.= " t.fk_user_mod,";
  236. $sql.= " t.tms";
  237. $sql.= " FROM ".MAIN_DB_PREFIX."advtargetemailing as t";
  238. if (!empty($id)) {
  239. $sql.= " WHERE t.fk_element = ".$id." AND type_element='mailing'";
  240. }else {
  241. $sql.= " WHERE t.fk_element = ".$this->fk_element." AND type_element='mailing'";
  242. }
  243. dol_syslog(get_class($this)."::fetch sql=".$sql, LOG_DEBUG);
  244. $resql=$this->db->query($sql);
  245. if ($resql)
  246. {
  247. if ($this->db->num_rows($resql))
  248. {
  249. $obj = $this->db->fetch_object($resql);
  250. $this->id = $obj->rowid;
  251. $this->name = $obj->name;
  252. $this->entity = $obj->entity;
  253. $this->fk_element = $obj->fk_element;
  254. $this->type_element = $obj->type_element;
  255. $this->filtervalue = $obj->filtervalue;
  256. $this->fk_user_author = $obj->fk_user_author;
  257. $this->datec = $this->db->jdate($obj->datec);
  258. $this->fk_user_mod = $obj->fk_user_mod;
  259. $this->tms = $this->db->jdate($obj->tms);
  260. }
  261. $this->db->free($resql);
  262. return 1;
  263. }
  264. else
  265. {
  266. $this->error="Error ".$this->db->lasterror();
  267. dol_syslog(get_class($this)."::fetch ".$this->error, LOG_ERR);
  268. return -1;
  269. }
  270. }
  271. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  272. /**
  273. * Load object in memory from the database
  274. *
  275. * @param int $id Id object
  276. * @param string $type_element Type target
  277. * @return int <0 if KO, >0 if OK
  278. */
  279. function fetch_by_element($id=0, $type_element='mailing')
  280. {
  281. // phpcs:enable
  282. global $langs;
  283. $sql = "SELECT";
  284. $sql.= " t.rowid,";
  285. $sql.= " t.name,";
  286. $sql.= " t.entity,";
  287. $sql.= " t.fk_element,";
  288. $sql.= " t.type_element,";
  289. $sql.= " t.filtervalue,";
  290. $sql.= " t.fk_user_author,";
  291. $sql.= " t.datec,";
  292. $sql.= " t.fk_user_mod,";
  293. $sql.= " t.tms";
  294. $sql.= " FROM ".MAIN_DB_PREFIX."advtargetemailing as t";
  295. if (!empty($id)) {
  296. $sql.= " WHERE t.fk_element = ".$id." AND type_element='$type_element'";
  297. }else {
  298. $sql.= " WHERE t.fk_element = ".$this->fk_element." AND type_element='$type_element'";
  299. }
  300. dol_syslog(get_class($this)."::fetch sql=".$sql, LOG_DEBUG);
  301. $resql=$this->db->query($sql);
  302. if ($resql)
  303. {
  304. if ($this->db->num_rows($resql))
  305. {
  306. $obj = $this->db->fetch_object($resql);
  307. $this->id = $obj->rowid;
  308. $this->name = $obj->name;
  309. $this->entity = $obj->entity;
  310. $this->fk_element = $obj->fk_element;
  311. $this->type_element = $obj->type_element;
  312. $this->filtervalue = $obj->filtervalue;
  313. $this->fk_user_author = $obj->fk_user_author;
  314. $this->datec = $this->db->jdate($obj->datec);
  315. $this->fk_user_mod = $obj->fk_user_mod;
  316. $this->tms = $this->db->jdate($obj->tms);
  317. }
  318. $this->db->free($resql);
  319. return 1;
  320. }
  321. else
  322. {
  323. $this->error="Error ".$this->db->lasterror();
  324. dol_syslog(get_class($this)."::fetch ".$this->error, LOG_ERR);
  325. return -1;
  326. }
  327. }
  328. /**
  329. * Update object into database
  330. *
  331. * @param User $user User that modifies
  332. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  333. * @return int <0 if KO, >0 if OK
  334. */
  335. function update($user, $notrigger=0)
  336. {
  337. global $conf, $langs;
  338. $error=0;
  339. // Clean parameters
  340. if (isset($this->fk_element)) $this->fk_element=trim($this->fk_element);
  341. if (isset($this->type_element)) $this->type_element=trim($this->type_element);
  342. if (isset($this->name)) $this->name=trim($this->name);
  343. if (isset($this->filtervalue)) $this->filtervalue=trim($this->filtervalue);
  344. if (isset($this->fk_user_author)) $this->fk_user_author=trim($this->fk_user_author);
  345. if (isset($this->fk_user_mod)) $this->fk_user_mod=trim($this->fk_user_mod);
  346. // Check parameters
  347. // Put here code to add a control on parameters values
  348. // Update request
  349. $sql = "UPDATE ".MAIN_DB_PREFIX."advtargetemailing SET";
  350. $sql.= " name=".(isset($this->name)?"'".$this->db->escape($this->name)."'":"''").",";
  351. $sql.= " entity=".$conf->entity.",";
  352. $sql.= " fk_element=".(isset($this->fk_element)?$this->fk_element:"null").",";
  353. $sql.= " type_element=".(isset($this->type_element)?"'".$this->db->escape($this->type_element)."'":"null").",";
  354. $sql.= " filtervalue=".(isset($this->filtervalue)?"'".$this->db->escape($this->filtervalue)."'":"null").",";
  355. $sql.= " fk_user_mod=".$user->id;
  356. $sql.= " WHERE rowid=".$this->id;
  357. $this->db->begin();
  358. dol_syslog(get_class($this)."::update sql=".$sql, LOG_DEBUG);
  359. $resql = $this->db->query($sql);
  360. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  361. if (! $error)
  362. {
  363. if (! $notrigger)
  364. {
  365. // Uncomment this and change MYOBJECT to your own tag if you
  366. // want this action calls a trigger.
  367. //// Call triggers
  368. //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  369. //$interface=new Interfaces($this->db);
  370. //$result=$interface->run_triggers('MYOBJECT_MODIFY',$this,$user,$langs,$conf);
  371. //if ($result < 0) { $error++; $this->errors=$interface->errors; }
  372. //// End call triggers
  373. }
  374. }
  375. // Commit or rollback
  376. if ($error)
  377. {
  378. foreach($this->errors as $errmsg)
  379. {
  380. dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
  381. $this->error.=($this->error?', '.$errmsg:$errmsg);
  382. }
  383. $this->db->rollback();
  384. return -1*$error;
  385. }
  386. else
  387. {
  388. $this->db->commit();
  389. return 1;
  390. }
  391. }
  392. /**
  393. * Delete object in database
  394. *
  395. * @param User $user User that deletes
  396. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  397. * @return int <0 if KO, >0 if OK
  398. */
  399. function delete($user, $notrigger=0)
  400. {
  401. global $conf, $langs;
  402. $error=0;
  403. $this->db->begin();
  404. if (! $error)
  405. {
  406. if (! $notrigger)
  407. {
  408. // Uncomment this and change MYOBJECT to your own tag if you
  409. // want this action calls a trigger.
  410. //// Call triggers
  411. //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  412. //$interface=new Interfaces($this->db);
  413. //$result=$interface->run_triggers('MYOBJECT_DELETE',$this,$user,$langs,$conf);
  414. //if ($result < 0) { $error++; $this->errors=$interface->errors; }
  415. //// End call triggers
  416. }
  417. }
  418. if (! $error)
  419. {
  420. $sql = "DELETE FROM ".MAIN_DB_PREFIX."advtargetemailing";
  421. $sql.= " WHERE rowid=".$this->id;
  422. dol_syslog(get_class($this)."::delete sql=".$sql);
  423. $resql = $this->db->query($sql);
  424. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  425. }
  426. // Commit or rollback
  427. if ($error)
  428. {
  429. foreach($this->errors as $errmsg)
  430. {
  431. dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
  432. $this->error.=($this->error?', '.$errmsg:$errmsg);
  433. }
  434. $this->db->rollback();
  435. return -1*$error;
  436. }
  437. else
  438. {
  439. $this->db->commit();
  440. return 1;
  441. }
  442. }
  443. /**
  444. * Save query in database to retreive it
  445. *
  446. * @param User $user User that deletes
  447. * @param array $arrayquery All element to Query
  448. * @return int <0 if KO, >0 if OK
  449. */
  450. function savequery($user,$arrayquery)
  451. {
  452. global $langs,$conf;
  453. if (!empty($arrayquery)) {
  454. $result=$this->fetch_by_mailing($this->fk_element);
  455. $this->filtervalue=json_encode($arrayquery);
  456. if ($result<0) {
  457. return -1;
  458. }
  459. if (!empty($this->id)) {
  460. $this->update($user);
  461. }else {
  462. $this->create($user);
  463. }
  464. }
  465. }
  466. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  467. /**
  468. * Load object in memory from database
  469. *
  470. * @param array $arrayquery All element to Query
  471. * @return int <0 if KO, >0 if OK
  472. */
  473. function query_thirdparty($arrayquery)
  474. {
  475. // phpcs:enable
  476. global $langs,$conf;
  477. $sql = "SELECT";
  478. $sql.= " t.rowid";
  479. $sql.= " FROM " . MAIN_DB_PREFIX . "societe as t";
  480. $sql.= " LEFT OUTER JOIN " . MAIN_DB_PREFIX . "societe_extrafields as te ON te.fk_object=t.rowid ";
  481. $sqlwhere=array();
  482. $sqlwhere[]= 't.entity IN ('.getEntity('societe').')';
  483. if (count($arrayquery)>0) {
  484. if (array_key_exists('cust_saleman', $arrayquery)) {
  485. $sql.= " LEFT OUTER JOIN " . MAIN_DB_PREFIX . "societe_commerciaux as saleman ON saleman.fk_soc=t.rowid ";
  486. }
  487. if (array_key_exists('cust_categ', $arrayquery)) {
  488. $sql.= " LEFT OUTER JOIN " . MAIN_DB_PREFIX . "categorie_societe as custcateg ON custcateg.fk_soc=t.rowid ";
  489. }
  490. if (!empty($arrayquery['cust_name'])) {
  491. $sqlwhere[]= $this->transformToSQL('t.nom',$arrayquery['cust_name']);
  492. }
  493. if (!empty($arrayquery['cust_code'])) {
  494. $sqlwhere[]= $this->transformToSQL('t.code_client',$arrayquery['cust_code']);
  495. }
  496. if (!empty($arrayquery['cust_adress'])) {
  497. $sqlwhere[]= $this->transformToSQL('t.address',$arrayquery['cust_adress']);
  498. }
  499. if (!empty($arrayquery['cust_zip'])) {
  500. $sqlwhere[]= $this->transformToSQL('t.zip',$arrayquery['cust_zip']);
  501. }
  502. if (!empty($arrayquery['cust_city'])) {
  503. $sqlwhere[]= $this->transformToSQL('t.town',$arrayquery['cust_city']);
  504. }
  505. if (!empty($arrayquery['cust_mothercompany'])) {
  506. $str=$this->transformToSQL('nom',$arrayquery['cust_mothercompany']);
  507. $sqlwhere[]= " (t.parent IN (SELECT rowid FROM " . MAIN_DB_PREFIX . "societe WHERE (".$str.")))";
  508. }
  509. if (!empty($arrayquery['cust_status']) && count($arrayquery['cust_status'])>0) {
  510. $sqlwhere[]= " (t.status IN (".implode(',',$arrayquery['cust_status'])."))";
  511. }
  512. if (!empty($arrayquery['cust_typecust']) && count($arrayquery['cust_typecust'])>0) {
  513. $sqlwhere[]= " (t.client IN (".implode(',',$arrayquery['cust_typecust'])."))";
  514. }
  515. if (!empty($arrayquery['cust_comm_status']) && count($arrayquery['cust_comm_status']>0)) {
  516. $sqlwhere[]= " (t.fk_stcomm IN (".implode(',',$arrayquery['cust_comm_status'])."))";
  517. }
  518. if (!empty($arrayquery['cust_prospect_status']) && count($arrayquery['cust_prospect_status'])>0) {
  519. $sqlwhere[]= " (t.fk_prospectlevel IN ('".implode("','",$arrayquery['cust_prospect_status'])."'))";
  520. }
  521. if (!empty($arrayquery['cust_typeent']) && count($arrayquery['cust_typeent'])>0) {
  522. $sqlwhere[]= " (t.fk_typent IN (".implode(',',$arrayquery['cust_typeent'])."))";
  523. }
  524. if (!empty($arrayquery['cust_saleman']) && count($arrayquery['cust_saleman'])>0) {
  525. $sqlwhere[]= " (saleman.fk_user IN (".implode(',',$arrayquery['cust_saleman'])."))";
  526. }
  527. if (!empty($arrayquery['cust_country']) && count($arrayquery['cust_country'])>0) {
  528. $sqlwhere[]= " (t.fk_pays IN (".implode(',',$arrayquery['cust_country'])."))";
  529. }
  530. if (!empty($arrayquery['cust_effectif_id']) && count($arrayquery['cust_effectif_id'])>0) {
  531. $sqlwhere[]= " (t.fk_effectif IN (".implode(',',$arrayquery['cust_effectif_id'])."))";
  532. }
  533. if (!empty($arrayquery['cust_categ']) && count($arrayquery['cust_categ'])>0) {
  534. $sqlwhere[]= " (custcateg.fk_categorie IN (".implode(',',$arrayquery['cust_categ'])."))";
  535. }
  536. if (!empty($arrayquery['cust_language']) && count($arrayquery['cust_language'])>0) {
  537. $sqlwhere[]= " (t.default_lang IN ('".implode("','",$arrayquery['cust_language'])."'))";
  538. }
  539. //Standard Extrafield feature
  540. if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) {
  541. // fetch optionals attributes and labels
  542. dol_include_once('/core/class/extrafields.class.php');
  543. $extrafields = new ExtraFields($this->db);
  544. $extralabels=$extrafields->fetch_name_optionals_label('societe');
  545. foreach($extralabels as $key=>$val) {
  546. if (($extrafields->attribute_type[$key] == 'varchar') ||
  547. ($extrafields->attribute_type[$key] == 'text')) {
  548. if (!empty($arrayquery['options_'.$key])) {
  549. $sqlwhere[]= " (te.".$key." LIKE '".$arrayquery['options_'.$key]."')";
  550. }
  551. } elseif (($extrafields->attribute_type[$key] == 'int') ||
  552. ($extrafields->attribute_type[$key] == 'double')) {
  553. if (!empty($arrayquery['options_'.$key.'_max'])) {
  554. $sqlwhere[]= " (te.".$key." >= ".$arrayquery['options_'.$key.'_max']." AND te.".$key." <= ".$arrayquery['options_'.$key.'_min'].")";
  555. }
  556. } elseif (($extrafields->attribute_type[$key] == 'date') ||
  557. ($extrafields->attribute_type[$key] == 'datetime')) {
  558. if (!empty($arrayquery['options_'.$key.'_end_dt'])){
  559. $sqlwhere[]= " (te.".$key." >= '".$this->db->idate($arrayquery['options_'.$key.'_st_dt'])."' AND te.".$key." <= '".$this->db->idate($arrayquery['options_'.$key.'_end_dt'])."')";
  560. }
  561. } elseif ($extrafields->attribute_type[$key] == 'boolean') {
  562. if ($arrayquery['options_'.$key]!=''){
  563. $sqlwhere[]= " (te.".$key." = ".$arrayquery['options_'.$key].")";
  564. }
  565. } else {
  566. if (is_array($arrayquery['options_'.$key])) {
  567. $sqlwhere[]= " (te.".$key." IN ('".implode("','",$arrayquery['options_'.$key])."'))";
  568. } elseif (!empty($arrayquery['options_'.$key])) {
  569. $sqlwhere[]= " (te.".$key." LIKE '".$arrayquery['options_'.$key]."')";
  570. }
  571. }
  572. }
  573. }
  574. if (count($sqlwhere)>0) $sql.= " WHERE ".implode(" AND ",$sqlwhere);
  575. }
  576. dol_syslog(get_class($this) . "::query_thirdparty sql=" . $sql, LOG_DEBUG);
  577. $resql = $this->db->query($sql);
  578. if ($resql) {
  579. $this->thirdparty_lines = array();
  580. $num = $this->db->num_rows($resql);
  581. $i = 0;
  582. if ($num)
  583. {
  584. while( $i < $num)
  585. {
  586. $obj = $this->db->fetch_object($resql);
  587. $this->thirdparty_lines[$i] = $obj->rowid;
  588. $i++;
  589. }
  590. }
  591. $this->db->free($resql);
  592. return $num;
  593. } else {
  594. $this->error = "Error " . $this->db->lasterror();
  595. dol_syslog(get_class($this) . "::query_thirdparty " . $this->error, LOG_ERR);
  596. return -1;
  597. }
  598. }
  599. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  600. /**
  601. * Load object in memory from database
  602. *
  603. * @param array $arrayquery All element to Query
  604. * @param int $withThirdpartyFilter add contact with tridparty filter
  605. * @return int <0 if KO, >0 if OK
  606. */
  607. function query_contact($arrayquery, $withThirdpartyFilter = 0)
  608. {
  609. // phpcs:enable
  610. global $langs,$conf;
  611. $sql = "SELECT";
  612. $sql.= " t.rowid";
  613. $sql.= " FROM " . MAIN_DB_PREFIX . "socpeople as t";
  614. $sql.= " LEFT OUTER JOIN " . MAIN_DB_PREFIX . "socpeople_extrafields as te ON te.fk_object=t.rowid ";
  615. if (! empty($withThirdpartyFilter)) {
  616. $sql .= " LEFT OUTER JOIN " . MAIN_DB_PREFIX . "societe as ts ON ts.rowid=t.fk_soc";
  617. $sql .= " LEFT OUTER JOIN " . MAIN_DB_PREFIX . "societe_extrafields as tse ON tse.fk_object=ts.rowid ";
  618. }
  619. $sqlwhere=array();
  620. $sqlwhere[]= 't.entity IN ('.getEntity('socpeople').')';
  621. if (count($arrayquery)>0) {
  622. if (array_key_exists('contact_categ', $arrayquery)) {
  623. $sql.= " LEFT OUTER JOIN " . MAIN_DB_PREFIX . "categorie_contact as contactcateg ON contactcateg.fk_socpeople=t.rowid ";
  624. }
  625. if (!empty($arrayquery['contact_lastname'])) {
  626. $sqlwhere[]=$this->transformToSQL('t.lastname',$arrayquery['contact_lastname']);
  627. }
  628. if (!empty($arrayquery['contact_firstname'])) {
  629. $sqlwhere[]=$this->transformToSQL('t.firstname',$arrayquery['contact_firstname']);
  630. }
  631. if (!empty($arrayquery['contact_country']) && count($arrayquery['contact_country'])) {
  632. $sqlwhere[]= " (t.fk_pays IN (".$this->db->escape(implode(',',$arrayquery['contact_country']))."))";
  633. }
  634. if (!empty($arrayquery['contact_status']) && count($arrayquery['contact_status'])>0) {
  635. $sqlwhere[]= " (t.statut IN (".$this->db->escape(implode(',',$arrayquery['contact_status']))."))";
  636. }
  637. if (!empty($arrayquery['contact_civility']) && count($arrayquery['contact_civility'])>0) {
  638. $sqlwhere[]= " (t.civility IN ('".$this->db->escape(implode("','",$arrayquery['contact_civility']))."'))";
  639. }
  640. if ($arrayquery['contact_no_email']!='') {
  641. $tmpwhere = '';
  642. if (! empty($arrayquery['contact_no_email']))
  643. {
  644. $tmpwhere.= "(t.email IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_unsubscribe WHERE t.entity IN (".getEntity('mailing').") AND email = '".$this->db->escape($arrayquery['contact_no_email'])."'))";
  645. }
  646. else
  647. {
  648. $tmpwhere.= "(t.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_unsubscribe WHERE t.entity IN (".getEntity('mailing').") AND email = '".$this->db->escape($arrayquery['contact_no_email'])."'))";
  649. }
  650. $sqlwhere[]= $tmpwhere;
  651. }
  652. if ($arrayquery['contact_update_st_dt']!='') {
  653. $sqlwhere[]= " (t.tms >= '".$this->db->idate($arrayquery['contact_update_st_dt'])."' AND t.tms <= '".$this->db->idate($arrayquery['contact_update_end_dt'])."')";
  654. }
  655. if ($arrayquery['contact_create_st_dt']!='') {
  656. $sqlwhere[]= " (t.datec >= '".$this->db->idate($arrayquery['contact_create_st_dt'])."' AND t.datec <= '".$this->db->idate($arrayquery['contact_create_end_dt'])."')";
  657. }
  658. if (!empty($arrayquery['contact_categ']) && count($arrayquery['contact_categ'])>0) {
  659. $sqlwhere[]= " (contactcateg.fk_categorie IN (".$this->db->escape(implode(",",$arrayquery['contact_categ']))."))";
  660. }
  661. //Standard Extrafield feature
  662. if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) {
  663. // fetch optionals attributes and labels
  664. dol_include_once('/core/class/extrafields.class.php');
  665. $extrafields = new ExtraFields($this->db);
  666. $extralabels=$extrafields->fetch_name_optionals_label('socpeople');
  667. foreach($extralabels as $key=>$val) {
  668. if (($extrafields->attribute_type[$key] == 'varchar') ||
  669. ($extrafields->attribute_type[$key] == 'text')) {
  670. if (!empty($arrayquery['options_'.$key.'_cnct'])) {
  671. $sqlwhere[]= " (te.".$key." LIKE '".$arrayquery['options_'.$key.'_cnct']."')";
  672. }
  673. } elseif (($extrafields->attribute_type[$key] == 'int') ||
  674. ($extrafields->attribute_type[$key] == 'double')) {
  675. if (!empty($arrayquery['options_'.$key.'_max'.'_cnct'])) {
  676. $sqlwhere[]= " (te.".$key." >= ".$arrayquery['options_'.$key.'_max'.'_cnct']." AND te.".$key." <= ".$arrayquery['options_'.$key.'_min'.'_cnct'].")";
  677. }
  678. } else if (($extrafields->attribute_type[$key] == 'date') ||
  679. ($extrafields->attribute_type[$key] == 'datetime')) {
  680. if (!empty($arrayquery['options_'.$key.'_end_dt'.'_cnct'])){
  681. $sqlwhere[]= " (te.".$key." >= '".$this->db->idate($arrayquery['options_'.$key.'_st_dt'.'_cnct'])."' AND te.".$key." <= '".$this->db->idate($arrayquery['options_'.$key.'_end_dt'.'_cnct'])."')";
  682. }
  683. }else if ($extrafields->attribute_type[$key] == 'boolean') {
  684. if ($arrayquery['options_'.$key.'_cnct']!=''){
  685. if ($arrayquery['options_'.$key.'_cnct']==0) {
  686. $sqlwhere[]= " (te.".$key." = ".$arrayquery['options_'.$key.'_cnct']." OR ((te.".$key." IS NULL) AND (te.fk_object IS NOT NULL)))";
  687. }else {
  688. $sqlwhere[]= " (te.".$key." = ".$arrayquery['options_'.$key.'_cnct'].")";
  689. }
  690. }
  691. }else{
  692. if (is_array($arrayquery['options_'.$key.'_cnct'])) {
  693. $sqlwhere[]= " (te.".$key." IN ('".implode("','",$arrayquery['options_'.$key.'_cnct'])."'))";
  694. } elseif (!empty($arrayquery['options_'.$key.'_cnct'])) {
  695. $sqlwhere[]= " (te.".$key." LIKE '".$arrayquery['options_'.$key.'_cnct']."')";
  696. }
  697. }
  698. }
  699. if (! empty($withThirdpartyFilter)) {
  700. if (array_key_exists('cust_saleman', $arrayquery)) {
  701. $sql.= " LEFT OUTER JOIN " . MAIN_DB_PREFIX . "societe_commerciaux as saleman ON saleman.fk_soc=ts.rowid ";
  702. }
  703. if (array_key_exists('cust_categ', $arrayquery)) {
  704. $sql.= " LEFT OUTER JOIN " . MAIN_DB_PREFIX . "categorie_societe as custcateg ON custcateg.fk_soc=ts.rowid ";
  705. }
  706. if (!empty($arrayquery['cust_name'])) {
  707. $sqlwhere[]= $this->transformToSQL('ts.nom',$arrayquery['cust_name']);
  708. }
  709. if (!empty($arrayquery['cust_code'])) {
  710. $sqlwhere[]= $this->transformToSQL('ts.code_client',$arrayquery['cust_code']);
  711. }
  712. if (!empty($arrayquery['cust_adress'])) {
  713. $sqlwhere[]= $this->transformToSQL('ts.address',$arrayquery['cust_adress']);
  714. }
  715. if (!empty($arrayquery['cust_zip'])) {
  716. $sqlwhere[]= $this->transformToSQL('ts.zip',$arrayquery['cust_zip']);
  717. }
  718. if (!empty($arrayquery['cust_city'])) {
  719. $sqlwhere[]= $this->transformToSQL('ts.town',$arrayquery['cust_city']);
  720. }
  721. if (!empty($arrayquery['cust_mothercompany'])) {
  722. $str=$this->transformToSQL('nom',$arrayquery['cust_mothercompany']);
  723. $sqlwhere[]= " (ts.parent IN (SELECT rowid FROM " . MAIN_DB_PREFIX . "societe WHERE (".$str.")))";
  724. }
  725. if (!empty($arrayquery['cust_status']) && count($arrayquery['cust_status'])>0) {
  726. $sqlwhere[]= " (ts.status IN (".implode(',',$arrayquery['cust_status'])."))";
  727. }
  728. if (!empty($arrayquery['cust_typecust']) && count($arrayquery['cust_typecust'])>0) {
  729. $sqlwhere[]= " (ts.client IN (".implode(',',$arrayquery['cust_typecust'])."))";
  730. }
  731. if (!empty($arrayquery['cust_comm_status']) && count($arrayquery['cust_comm_status']>0)) {
  732. $sqlwhere[]= " (ts.fk_stcomm IN (".implode(',',$arrayquery['cust_comm_status'])."))";
  733. }
  734. if (!empty($arrayquery['cust_prospect_status']) && count($arrayquery['cust_prospect_status'])>0) {
  735. $sqlwhere[]= " (ts.fk_prospectlevel IN ('".implode("','",$arrayquery['cust_prospect_status'])."'))";
  736. }
  737. if (!empty($arrayquery['cust_typeent']) && count($arrayquery['cust_typeent'])>0) {
  738. $sqlwhere[]= " (ts.fk_typent IN (".implode(',',$arrayquery['cust_typeent'])."))";
  739. }
  740. if (!empty($arrayquery['cust_saleman']) && count($arrayquery['cust_saleman'])>0) {
  741. $sqlwhere[]= " (saleman.fk_user IN (".implode(',',$arrayquery['cust_saleman'])."))";
  742. }
  743. if (!empty($arrayquery['cust_country']) && count($arrayquery['cust_country'])>0) {
  744. $sqlwhere[]= " (ts.fk_pays IN (".implode(',',$arrayquery['cust_country'])."))";
  745. }
  746. if (!empty($arrayquery['cust_effectif_id']) && count($arrayquery['cust_effectif_id'])>0) {
  747. $sqlwhere[]= " (ts.fk_effectif IN (".implode(',',$arrayquery['cust_effectif_id'])."))";
  748. }
  749. if (!empty($arrayquery['cust_categ']) && count($arrayquery['cust_categ'])>0) {
  750. $sqlwhere[]= " (custcateg.fk_categorie IN (".implode(',',$arrayquery['cust_categ'])."))";
  751. }
  752. if (!empty($arrayquery['cust_language']) && count($arrayquery['cust_language'])>0) {
  753. $sqlwhere[]= " (ts.default_lang IN ('".implode("','",$arrayquery['cust_language'])."'))";
  754. }
  755. //Standard Extrafield feature
  756. if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) {
  757. // fetch optionals attributes and labels
  758. dol_include_once('/core/class/extrafields.class.php');
  759. $extrafields = new ExtraFields($this->db);
  760. $extralabels=$extrafields->fetch_name_optionals_label('societe');
  761. foreach($extralabels as $key=>$val) {
  762. if (($extrafields->attribute_type[$key] == 'varchar') ||
  763. ($extrafields->attribute_type[$key] == 'text')) {
  764. if (!empty($arrayquery['options_'.$key])) {
  765. $sqlwhere[]= " (tse.".$key." LIKE '".$arrayquery['options_'.$key]."')";
  766. }
  767. } elseif (($extrafields->attribute_type[$key] == 'int') ||
  768. ($extrafields->attribute_type[$key] == 'double')) {
  769. if (!empty($arrayquery['options_'.$key.'_max'])) {
  770. $sqlwhere[]= " (tse.".$key." >= ".$arrayquery['options_'.$key.'_max']." AND tse.".$key." <= ".$arrayquery['options_'.$key.'_min'].")";
  771. }
  772. } else if (($extrafields->attribute_type[$key] == 'date') ||
  773. ($extrafields->attribute_type[$key] == 'datetime')) {
  774. if (!empty($arrayquery['options_'.$key.'_end_dt'])){
  775. $sqlwhere[]= " (tse.".$key." >= '".$this->db->idate($arrayquery['options_'.$key.'_st_dt'])."' AND tse.".$key." <= '".$this->db->idate($arrayquery['options_'.$key.'_end_dt'])."')";
  776. }
  777. }else if ($extrafields->attribute_type[$key] == 'boolean') {
  778. if ($arrayquery['options_'.$key]!=''){
  779. $sqlwhere[]= " (tse.".$key." = ".$arrayquery['options_'.$key].")";
  780. }
  781. }else{
  782. if (is_array($arrayquery['options_'.$key])) {
  783. $sqlwhere[]= " (tse.".$key." IN ('".implode("','",$arrayquery['options_'.$key])."'))";
  784. } elseif (!empty($arrayquery['options_'.$key])) {
  785. $sqlwhere[]= " (tse.".$key." LIKE '".$arrayquery['options_'.$key]."')";
  786. }
  787. }
  788. }
  789. }
  790. }
  791. }
  792. if (count($sqlwhere)>0) $sql.= " WHERE ".implode(" AND ",$sqlwhere);
  793. }
  794. dol_syslog(get_class($this) . "::query_contact sql=" . $sql, LOG_DEBUG);
  795. $resql = $this->db->query($sql);
  796. if ($resql) {
  797. $this->contact_lines = array();
  798. $num = $this->db->num_rows($resql);
  799. $i = 0;
  800. if ($num)
  801. {
  802. while( $i < $num)
  803. {
  804. $obj = $this->db->fetch_object($resql);
  805. $this->contact_lines[$i] = $obj->rowid;
  806. $i++;
  807. }
  808. }
  809. $this->db->free($resql);
  810. return $num;
  811. } else {
  812. $this->error = "Error " . $this->db->lasterror();
  813. dol_syslog(get_class($this) . "::query_contact " . $this->error, LOG_ERR);
  814. return -1;
  815. }
  816. }
  817. /**
  818. * Parse criteria to return a SQL qury formated
  819. *
  820. * @param string $column_to_test column to test
  821. * @param string $criteria Use %% as magic caracters. For exemple to find all item like <b>jean, joe, jim</b>, you can input <b>j%%</b>, you can also use ; as separator for value,
  822. * and use ! for except this value.
  823. * For exemple jean;joe;jim%%;!jimo;!jima%> will target all jean, joe, start with jim but not jimo and not everythnig taht start by jima
  824. * @return string Sql to use for the where condition
  825. */
  826. public function transformToSQL($column_to_test,$criteria)
  827. {
  828. $return_sql_criteria = '(';
  829. //This is a multiple value test
  830. if (preg_match('/;/',$criteria)) {
  831. $return_sql_not_like=array();
  832. $return_sql_like=array();
  833. $criteria_array=explode(';',$criteria);
  834. foreach($criteria_array as $inter_criteria) {
  835. if (preg_match('/!/',$inter_criteria)) {
  836. $return_sql_not_like[]= '('.$column_to_test.' NOT LIKE \''.str_replace('!', '', $inter_criteria).'\')';
  837. } else {
  838. $return_sql_like[]= '('.$column_to_test.' LIKE \''.$inter_criteria.'\')';
  839. }
  840. }
  841. if (count($return_sql_like)>0) {
  842. $return_sql_criteria .= '(' . implode (' OR ', $return_sql_like) .')';
  843. }
  844. if (count($return_sql_not_like)>0) {
  845. $return_sql_criteria .= ' AND (' . implode (' AND ', $return_sql_not_like).')';
  846. }
  847. }else {
  848. $return_sql_criteria .= $column_to_test . ' LIKE \''.$this->db->escape($criteria).'\'';
  849. }
  850. $return_sql_criteria .= ')';
  851. return $return_sql_criteria;
  852. }
  853. }