dolibarr  13.0.2
advtargetemailing.class.php
Go to the documentation of this file.
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 <https://www.gnu.org/licenses/>.
17  */
18 
30 {
34  public $db;
35 
39  public $element = 'advtargetemailing';
40 
44  public $table_element = 'advtargetemailing';
45 
49  public $id;
50 
54  public $name;
55 
59  public $fk_element;
60 
64  public $type_element;
65 
69  public $filtervalue;
70 
74  public $fk_user_author;
75 
79  public $datec = '';
80 
84  public $fk_user_mod;
85 
89  public $tms = '';
90 
94  public $select_target_type = array();
95 
99  public $type_statuscommprospect = array();
100  public $thirdparty_lines;
101  public $contact_lines;
102 
103 
109  public function __construct($db)
110  {
111  global $langs;
112  $langs->load('customers');
113 
114  $this->db = $db;
115 
116  $this->select_target_type = array(
117  '2' => $langs->trans('Contacts'),
118  '1' => $langs->trans('Contacts').'+'.$langs->trans('ThirdParty'),
119  '3' => $langs->trans('ThirdParty'),
120  '4' => $langs->trans('ContactsWithThirdpartyFilter')
121  );
122  $this->type_statuscommprospect = array(
123  -1 => $langs->trans("StatusProspect-1"),
124  0 => $langs->trans("StatusProspect0"),
125  1 => $langs->trans("StatusProspect1"),
126  2 => $langs->trans("StatusProspect2"),
127  3 => $langs->trans("StatusProspect3")
128  );
129  }
130 
138  public function create($user, $notrigger = 0)
139  {
140  global $conf, $langs;
141  $error = 0;
142 
143  // Clean parameters
144  if (isset($this->fk_element)) {
145  $this->fk_element = (int) $this->fk_element;
146  }
147  if (isset($this->type_element)) {
148  $this->type_element = trim($this->type_element);
149  }
150 
151  if (isset($this->name)) {
152  $this->name = trim($this->name);
153  }
154  if (isset($this->filtervalue)) {
155  $this->filtervalue = trim($this->filtervalue);
156  }
157 
158  // Check parameters
159  // Put here code to add control on parameters values
160 
161  // Insert request
162  $sql = "INSERT INTO ".MAIN_DB_PREFIX."advtargetemailing(";
163  $sql .= "name,";
164  $sql .= "entity,";
165  $sql .= "fk_element,";
166  $sql .= "type_element,";
167  $sql .= "filtervalue,";
168  $sql .= "fk_user_author,";
169  $sql .= "datec,";
170  $sql .= "fk_user_mod";
171  $sql .= ") VALUES (";
172  $sql .= " ".(!isset($this->name) ? 'NULL' : "'".$this->db->escape($this->name)."'").",";
173  $sql .= " ".$conf->entity.",";
174  $sql .= " ".(!isset($this->fk_element) ? 'NULL' : "'".$this->db->escape($this->fk_element)."'").",";
175  $sql .= " ".(!isset($this->type_element) ? 'NULL' : "'".$this->db->escape($this->type_element)."'").",";
176  $sql .= " ".(!isset($this->filtervalue) ? 'NULL' : "'".$this->db->escape($this->filtervalue)."'").",";
177  $sql .= " ".$user->id.",";
178  $sql .= " '".$this->db->idate(dol_now())."',";
179  $sql .= " ".$user->id;
180  $sql .= ")";
181 
182  $this->db->begin();
183 
184  dol_syslog(get_class($this)."::create sql=".$sql, LOG_DEBUG);
185  $resql = $this->db->query($sql);
186  if (!$resql) {
187  $error++; $this->errors[] = "Error ".$this->db->lasterror();
188  }
189 
190  if (!$error) {
191  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."advtargetemailing");
192  }
193 
194  // Commit or rollback
195  if ($error) {
196  foreach ($this->errors as $errmsg) {
197  dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
198  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
199  }
200  $this->db->rollback();
201  return -1 * $error;
202  } else {
203  $this->db->commit();
204  return $this->id;
205  }
206  }
207 
214  public function fetch($id)
215  {
216  global $langs;
217  $sql = "SELECT";
218  $sql .= " t.rowid,";
219 
220  $sql .= " t.name,";
221  $sql .= " t.entity,";
222  $sql .= " t.fk_element,";
223  $sql .= " t.type_element,";
224  $sql .= " t.filtervalue,";
225  $sql .= " t.fk_user_author,";
226  $sql .= " t.datec,";
227  $sql .= " t.fk_user_mod,";
228  $sql .= " t.tms";
229 
230  $sql .= " FROM ".MAIN_DB_PREFIX."advtargetemailing as t";
231  $sql .= " WHERE t.rowid = ".$id;
232 
233  dol_syslog(get_class($this)."::fetch sql=".$sql, LOG_DEBUG);
234  $resql = $this->db->query($sql);
235  if ($resql) {
236  if ($this->db->num_rows($resql)) {
237  $obj = $this->db->fetch_object($resql);
238 
239  $this->id = $obj->rowid;
240 
241  $this->name = $obj->name;
242  $this->entity = $obj->entity;
243  $this->fk_element = $obj->fk_element;
244  $this->type_element = $obj->type_element;
245  $this->filtervalue = $obj->filtervalue;
246  $this->fk_user_author = $obj->fk_user_author;
247  $this->datec = $this->db->jdate($obj->datec);
248  $this->fk_user_mod = $obj->fk_user_mod;
249  $this->tms = $this->db->jdate($obj->tms);
250  }
251  $this->db->free($resql);
252 
253  return 1;
254  } else {
255  $this->error = "Error ".$this->db->lasterror();
256  dol_syslog(get_class($this)."::fetch ".$this->error, LOG_ERR);
257  return -1;
258  }
259  }
260 
261  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
268  public function fetch_by_mailing($id = 0)
269  {
270  // phpcs:enable
271  global $langs;
272  $sql = "SELECT";
273  $sql .= " t.rowid,";
274 
275  $sql .= " t.name,";
276  $sql .= " t.entity,";
277  $sql .= " t.fk_element,";
278  $sql .= " t.type_element,";
279  $sql .= " t.filtervalue,";
280  $sql .= " t.fk_user_author,";
281  $sql .= " t.datec,";
282  $sql .= " t.fk_user_mod,";
283  $sql .= " t.tms";
284 
285  $sql .= " FROM ".MAIN_DB_PREFIX."advtargetemailing as t";
286  if (!empty($id)) {
287  $sql .= " WHERE t.fk_element = ".$id." AND type_element='mailing'";
288  } else {
289  $sql .= " WHERE t.fk_element = ".$this->fk_element." AND type_element='mailing'";
290  }
291 
292  dol_syslog(get_class($this)."::fetch sql=".$sql, LOG_DEBUG);
293  $resql = $this->db->query($sql);
294  if ($resql) {
295  if ($this->db->num_rows($resql)) {
296  $obj = $this->db->fetch_object($resql);
297 
298  $this->id = $obj->rowid;
299 
300  $this->name = $obj->name;
301  $this->entity = $obj->entity;
302  $this->fk_element = $obj->fk_element;
303  $this->type_element = $obj->type_element;
304  $this->filtervalue = $obj->filtervalue;
305  $this->fk_user_author = $obj->fk_user_author;
306  $this->datec = $this->db->jdate($obj->datec);
307  $this->fk_user_mod = $obj->fk_user_mod;
308  $this->tms = $this->db->jdate($obj->tms);
309  }
310  $this->db->free($resql);
311 
312  return 1;
313  } else {
314  $this->error = "Error ".$this->db->lasterror();
315  dol_syslog(get_class($this)."::fetch ".$this->error, LOG_ERR);
316  return -1;
317  }
318  }
319 
320 
321 
322 
323  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
331  public function fetch_by_element($id = 0, $type_element = 'mailing')
332  {
333  // phpcs:enable
334  global $langs;
335  $sql = "SELECT";
336  $sql .= " t.rowid,";
337 
338  $sql .= " t.name,";
339  $sql .= " t.entity,";
340  $sql .= " t.fk_element,";
341  $sql .= " t.type_element,";
342  $sql .= " t.filtervalue,";
343  $sql .= " t.fk_user_author,";
344  $sql .= " t.datec,";
345  $sql .= " t.fk_user_mod,";
346  $sql .= " t.tms";
347 
348  $sql .= " FROM ".MAIN_DB_PREFIX."advtargetemailing as t";
349  if (!empty($id)) {
350  $sql .= " WHERE t.fk_element = ".$id." AND type_element='$type_element'";
351  } else {
352  $sql .= " WHERE t.fk_element = ".$this->fk_element." AND type_element='$type_element'";
353  }
354 
355  dol_syslog(get_class($this)."::fetch sql=".$sql, LOG_DEBUG);
356  $resql = $this->db->query($sql);
357  if ($resql) {
358  if ($this->db->num_rows($resql)) {
359  $obj = $this->db->fetch_object($resql);
360 
361  $this->id = $obj->rowid;
362 
363  $this->name = $obj->name;
364  $this->entity = $obj->entity;
365  $this->fk_element = $obj->fk_element;
366  $this->type_element = $obj->type_element;
367  $this->filtervalue = $obj->filtervalue;
368  $this->fk_user_author = $obj->fk_user_author;
369  $this->datec = $this->db->jdate($obj->datec);
370  $this->fk_user_mod = $obj->fk_user_mod;
371  $this->tms = $this->db->jdate($obj->tms);
372  }
373  $this->db->free($resql);
374 
375  return 1;
376  } else {
377  $this->error = "Error ".$this->db->lasterror();
378  dol_syslog(get_class($this)."::fetch ".$this->error, LOG_ERR);
379  return -1;
380  }
381  }
382 
390  public function update($user, $notrigger = 0)
391  {
392  global $conf, $langs;
393  $error = 0;
394 
395  // Clean parameters
396  if (isset($this->fk_element)) {
397  $this->fk_element = (int) $this->fk_element;
398  }
399  if (isset($this->type_element)) {
400  $this->type_element = trim($this->type_element);
401  }
402  if (isset($this->name)) {
403  $this->name = trim($this->name);
404  }
405  if (isset($this->filtervalue)) {
406  $this->filtervalue = trim($this->filtervalue);
407  }
408 
409  // Check parameters
410  // Put here code to add a control on parameters values
411 
412  // Update request
413  $sql = "UPDATE ".MAIN_DB_PREFIX."advtargetemailing SET";
414 
415  $sql .= " name=".(isset($this->name) ? "'".$this->db->escape($this->name)."'" : "''").",";
416  $sql .= " entity=".$conf->entity.",";
417  $sql .= " fk_element=".(isset($this->fk_element) ? $this->fk_element : "null").",";
418  $sql .= " type_element=".(isset($this->type_element) ? "'".$this->db->escape($this->type_element)."'" : "null").",";
419  $sql .= " filtervalue=".(isset($this->filtervalue) ? "'".$this->db->escape($this->filtervalue)."'" : "null").",";
420  $sql .= " fk_user_mod=".$user->id;
421 
422  $sql .= " WHERE rowid=".$this->id;
423 
424  $this->db->begin();
425  dol_syslog(get_class($this)."::update sql=".$sql, LOG_DEBUG);
426  $resql = $this->db->query($sql);
427  if (!$resql) {
428  $error++;
429  $this->errors[] = "Error ".$this->db->lasterror();
430  }
431 
432  // Commit or rollback
433  if ($error) {
434  foreach ($this->errors as $errmsg) {
435  dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
436  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
437  }
438  $this->db->rollback();
439  return -1 * $error;
440  } else {
441  $this->db->commit();
442  return 1;
443  }
444  }
445 
453  public function delete($user, $notrigger = 0)
454  {
455  global $conf, $langs;
456  $error = 0;
457 
458  $this->db->begin();
459 
460  if (!$error) {
461  $sql = "DELETE FROM ".MAIN_DB_PREFIX."advtargetemailing";
462  $sql .= " WHERE rowid=".$this->id;
463 
464  dol_syslog(get_class($this)."::delete sql=".$sql);
465  $resql = $this->db->query($sql);
466  if (!$resql) {
467  $error++; $this->errors[] = "Error ".$this->db->lasterror();
468  }
469  }
470 
471  // Commit or rollback
472  if ($error) {
473  foreach ($this->errors as $errmsg) {
474  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
475  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
476  }
477  $this->db->rollback();
478  return -1 * $error;
479  } else {
480  $this->db->commit();
481  return 1;
482  }
483  }
484 
485 
493  public function savequery($user, $arrayquery)
494  {
495  global $langs, $conf;
496 
497  if (!empty($arrayquery)) {
498  $result = $this->fetch_by_mailing($this->fk_element);
499  $this->filtervalue = json_encode($arrayquery);
500  if ($result < 0) {
501  return -1;
502  }
503  if (!empty($this->id)) {
504  $this->update($user);
505  } else {
506  $this->create($user);
507  }
508  }
509  }
510 
511 
512 
513  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
520  public function query_thirdparty($arrayquery)
521  {
522  // phpcs:enable
523  global $langs, $conf, $extrafields;
524 
525  $sql = "SELECT";
526  $sql .= " t.rowid";
527  $sql .= " FROM ".MAIN_DB_PREFIX."societe as t";
528  $sql .= " LEFT OUTER JOIN ".MAIN_DB_PREFIX."societe_extrafields as te ON te.fk_object=t.rowid ";
529 
530  $sqlwhere = array();
531 
532  $sqlwhere[] = 't.entity IN ('.getEntity('societe').')';
533 
534  if (count($arrayquery) > 0) {
535  if (array_key_exists('cust_saleman', $arrayquery)) {
536  $sql .= " LEFT OUTER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as saleman ON saleman.fk_soc=t.rowid ";
537  }
538  if (array_key_exists('cust_categ', $arrayquery)) {
539  $sql .= " LEFT OUTER JOIN ".MAIN_DB_PREFIX."categorie_societe as custcateg ON custcateg.fk_soc=t.rowid ";
540  }
541 
542  if (!empty($arrayquery['cust_name'])) {
543  $sqlwhere[] = $this->transformToSQL('t.nom', $arrayquery['cust_name']);
544  }
545  if (!empty($arrayquery['cust_code'])) {
546  $sqlwhere[] = $this->transformToSQL('t.code_client', $arrayquery['cust_code']);
547  }
548  if (!empty($arrayquery['cust_adress'])) {
549  $sqlwhere[] = $this->transformToSQL('t.address', $arrayquery['cust_adress']);
550  }
551  if (!empty($arrayquery['cust_zip'])) {
552  $sqlwhere[] = $this->transformToSQL('t.zip', $arrayquery['cust_zip']);
553  }
554  if (!empty($arrayquery['cust_city'])) {
555  $sqlwhere[] = $this->transformToSQL('t.town', $arrayquery['cust_city']);
556  }
557  if (!empty($arrayquery['cust_mothercompany'])) {
558  $str = $this->transformToSQL('nom', $arrayquery['cust_mothercompany']);
559  $sqlwhere[] = " (t.parent IN (SELECT rowid FROM ".MAIN_DB_PREFIX."societe WHERE (".$str.")))";
560  }
561  if (!empty($arrayquery['cust_status']) && count($arrayquery['cust_status']) > 0) {
562  $sqlwhere[] = " (t.status IN (".implode(',', $arrayquery['cust_status'])."))";
563  }
564  if (!empty($arrayquery['cust_typecust']) && count($arrayquery['cust_typecust']) > 0) {
565  $sqlwhere[] = " (t.client IN (".implode(',', $arrayquery['cust_typecust'])."))";
566  }
567  if (!empty($arrayquery['cust_comm_status']) && count($arrayquery['cust_comm_status'] > 0)) {
568  $sqlwhere[] = " (t.fk_stcomm IN (".implode(',', $arrayquery['cust_comm_status'])."))";
569  }
570  if (!empty($arrayquery['cust_prospect_status']) && count($arrayquery['cust_prospect_status']) > 0) {
571  $sqlwhere[] = " (t.fk_prospectlevel IN ('".implode("','", $arrayquery['cust_prospect_status'])."'))";
572  }
573  if (!empty($arrayquery['cust_typeent']) && count($arrayquery['cust_typeent']) > 0) {
574  $sqlwhere[] = " (t.fk_typent IN (".implode(',', $arrayquery['cust_typeent'])."))";
575  }
576  if (!empty($arrayquery['cust_saleman']) && count($arrayquery['cust_saleman']) > 0) {
577  $sqlwhere[] = " (saleman.fk_user IN (".implode(',', $arrayquery['cust_saleman'])."))";
578  }
579  if (!empty($arrayquery['cust_country']) && count($arrayquery['cust_country']) > 0) {
580  $sqlwhere[] = " (t.fk_pays IN (".implode(',', $arrayquery['cust_country'])."))";
581  }
582  if (!empty($arrayquery['cust_effectif_id']) && count($arrayquery['cust_effectif_id']) > 0) {
583  $sqlwhere[] = " (t.fk_effectif IN (".implode(',', $arrayquery['cust_effectif_id'])."))";
584  }
585  if (!empty($arrayquery['cust_categ']) && count($arrayquery['cust_categ']) > 0) {
586  $sqlwhere[] = " (custcateg.fk_categorie IN (".implode(',', $arrayquery['cust_categ'])."))";
587  }
588  if (!empty($arrayquery['cust_language']) && count($arrayquery['cust_language']) > 0) {
589  $sqlwhere[] = " (t.default_lang IN ('".implode("','", $arrayquery['cust_language'])."'))";
590  }
591 
592  //Standard Extrafield feature
593  if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) {
594  $socstatic = new Societe($this->db);
595  $elementtype = $socstatic->table_element;
596 
597  $extrafields->fetch_name_optionals_label($elementtype);
598 
599  foreach ($extrafields->attributes[$elementtype]['label'] as $key => $val) {
600  if (($extrafields->attributes[$elementtype]['type'][$key] == 'varchar') ||
601  ($extrafields->attributes[$elementtype]['type'][$key] == 'text')) {
602  if (!empty($arrayquery['options_'.$key])) {
603  $sqlwhere[] = " (te.".$key." LIKE '".$this->db->escape($arrayquery['options_'.$key])."')";
604  }
605  } elseif (($extrafields->attributes[$elementtype]['type'][$key] == 'int') ||
606  ($extrafields->attributes[$elementtype]['type'][$key] == 'double')) {
607  if (!empty($arrayquery['options_'.$key.'_max'])) {
608  $sqlwhere[] = " (te.".$key." >= ".$arrayquery['options_'.$key.'_max']." AND te.".$key." <= ".$arrayquery['options_'.$key.'_min'].")";
609  }
610  } elseif (($extrafields->attributes[$elementtype]['type'][$key] == 'date') ||
611  ($extrafields->attributes[$elementtype]['type'][$key] == 'datetime')) {
612  if (!empty($arrayquery['options_'.$key.'_end_dt'])) {
613  $sqlwhere[] = " (te.".$key." >= '".$this->db->idate($arrayquery['options_'.$key.'_st_dt'])."' AND te.".$key." <= '".$this->db->idate($arrayquery['options_'.$key.'_end_dt'])."')";
614  }
615  } elseif ($extrafields->attributes[$elementtype]['type'][$key] == 'boolean') {
616  if ($arrayquery['options_'.$key] != '') {
617  $sqlwhere[] = " (te.".$key." = ".$arrayquery['options_'.$key].")";
618  }
619  } else {
620  if (is_array($arrayquery['options_'.$key])) {
621  $sqlwhere[] = " (te.".$key." IN ('".implode("','", $arrayquery['options_'.$key])."'))";
622  } elseif (!empty($arrayquery['options_'.$key])) {
623  $sqlwhere[] = " (te.".$key." LIKE '".$this->db->escape($arrayquery['options_'.$key])."')";
624  }
625  }
626  }
627  }
628 
629  if (count($sqlwhere) > 0) {
630  $sql .= " WHERE ".implode(" AND ", $sqlwhere);
631  }
632  }
633 
634 
635  dol_syslog(get_class($this)."::query_thirdparty sql=".$sql, LOG_DEBUG);
636  $resql = $this->db->query($sql);
637  if ($resql) {
638  $this->thirdparty_lines = array();
639  $num = $this->db->num_rows($resql);
640  $i = 0;
641 
642  if ($num) {
643  while ($i < $num) {
644  $obj = $this->db->fetch_object($resql);
645 
646  $this->thirdparty_lines[$i] = $obj->rowid;
647 
648  $i++;
649  }
650  }
651  $this->db->free($resql);
652 
653  return $num;
654  } else {
655  $this->error = "Error ".$this->db->lasterror();
656  dol_syslog(get_class($this)."::query_thirdparty ".$this->error, LOG_ERR);
657  return -1;
658  }
659  }
660 
661  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
669  public function query_contact($arrayquery, $withThirdpartyFilter = 0)
670  {
671  // phpcs:enable
672  global $langs, $conf;
673 
674  $sql = "SELECT";
675  $sql .= " t.rowid";
676  $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as t";
677  $sql .= " LEFT OUTER JOIN ".MAIN_DB_PREFIX."socpeople_extrafields as te ON te.fk_object=t.rowid ";
678 
679  if (!empty($withThirdpartyFilter)) {
680  $sql .= " LEFT OUTER JOIN ".MAIN_DB_PREFIX."societe as ts ON ts.rowid=t.fk_soc";
681  $sql .= " LEFT OUTER JOIN ".MAIN_DB_PREFIX."societe_extrafields as tse ON tse.fk_object=ts.rowid ";
682  }
683 
684  $sqlwhere = array();
685 
686  $sqlwhere[] = 't.entity IN ('.getEntity('socpeople').')';
687 
688  if (count($arrayquery) > 0) {
689  if (array_key_exists('contact_categ', $arrayquery)) {
690  $sql .= " LEFT OUTER JOIN ".MAIN_DB_PREFIX."categorie_contact as contactcateg ON contactcateg.fk_socpeople=t.rowid ";
691  }
692 
693  if (!empty($arrayquery['contact_lastname'])) {
694  $sqlwhere[] = $this->transformToSQL('t.lastname', $arrayquery['contact_lastname']);
695  }
696  if (!empty($arrayquery['contact_firstname'])) {
697  $sqlwhere[] = $this->transformToSQL('t.firstname', $arrayquery['contact_firstname']);
698  }
699  if (!empty($arrayquery['contact_country']) && count($arrayquery['contact_country'])) {
700  $sqlwhere[] = " (t.fk_pays IN (".$this->db->sanitize($this->db->escape(implode(',', $arrayquery['contact_country'])))."))";
701  }
702  if (!empty($arrayquery['contact_status']) && count($arrayquery['contact_status']) > 0) {
703  $sqlwhere[] = " (t.statut IN (".$this->db->sanitize($this->db->escape(implode(',', $arrayquery['contact_status'])))."))";
704  }
705  if (!empty($arrayquery['contact_civility']) && count($arrayquery['contact_civility']) > 0) {
706  $sqlwhere[] = " (t.civility IN ('".$this->db->sanitize($this->db->escape(implode("','", $arrayquery['contact_civility'])))."'))";
707  }
708  if ($arrayquery['contact_no_email'] != '') {
709  $tmpwhere = '';
710  if (!empty($arrayquery['contact_no_email'])) {
711  $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'])."'))";
712  } else {
713  $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'])."'))";
714  }
715  $sqlwhere[] = $tmpwhere;
716  }
717  if ($arrayquery['contact_update_st_dt'] != '') {
718  $sqlwhere[] = " (t.tms >= '".$this->db->idate($arrayquery['contact_update_st_dt'])."' AND t.tms <= '".$this->db->idate($arrayquery['contact_update_end_dt'])."')";
719  }
720  if ($arrayquery['contact_create_st_dt'] != '') {
721  $sqlwhere[] = " (t.datec >= '".$this->db->idate($arrayquery['contact_create_st_dt'])."' AND t.datec <= '".$this->db->idate($arrayquery['contact_create_end_dt'])."')";
722  }
723  if (!empty($arrayquery['contact_categ']) && count($arrayquery['contact_categ']) > 0) {
724  $sqlwhere[] = " (contactcateg.fk_categorie IN (".$this->db->escape(implode(",", $arrayquery['contact_categ']))."))";
725  }
726 
727  //Standard Extrafield feature
728  if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) {
729  $contactstatic = new Contact($this->db);
730  $elementtype = $contactstatic->table_element;
731 
732  // fetch optionals attributes and labels
733  dol_include_once('/core/class/extrafields.class.php');
734  $extrafields = new ExtraFields($this->db);
735  $extrafields->fetch_name_optionals_label($elementtype);
736 
737  $extrafields->fetch_name_optionals_label($elementtype);
738 
739  foreach ($extrafields->attributes[$elementtype]['label'] as $key => $val) {
740  if (($extrafields->attributes[$elementtype]['type'][$key] == 'varchar') ||
741  ($extrafields->attributes[$elementtype]['type'][$key] == 'text')) {
742  if (!empty($arrayquery['options_'.$key.'_cnct'])) {
743  $sqlwhere[] = " (te.".$key." LIKE '".$this->db->escape($arrayquery['options_'.$key.'_cnct'])."')";
744  }
745  } elseif (($extrafields->attributes[$elementtype]['type'][$key] == 'int') ||
746  ($extrafields->attributes[$elementtype]['type'][$key] == 'double')) {
747  if (!empty($arrayquery['options_'.$key.'_max_cnct'])) {
748  $sqlwhere[] = " (te.".$key." >= ".$arrayquery['options_'.$key.'_max_cnct']." AND te.".$key." <= ".$arrayquery['options_'.$key.'_min_cnct'].")";
749  }
750  } elseif (($extrafields->attributes[$elementtype]['type'][$key] == 'date') ||
751  ($extrafields->attributes[$elementtype]['type'][$key] == 'datetime')) {
752  if (!empty($arrayquery['options_'.$key.'_end_dt_cnct'])) {
753  $sqlwhere[] = " (te.".$key." >= '".$this->db->idate($arrayquery['options_'.$key.'_st_dt_cnct'])."' AND te.".$key." <= '".$this->db->idate($arrayquery['options_'.$key.'_end_dt_cnct'])."')";
754  }
755  } elseif ($extrafields->attributes[$elementtype]['type'][$key] == 'boolean') {
756  if ($arrayquery['options_'.$key.'_cnct'] != '') {
757  if ($arrayquery['options_'.$key.'_cnct'] == 0) {
758  $sqlwhere[] = " (te.".$key." = ".$arrayquery['options_'.$key.'_cnct']." OR ((te.".$key." IS NULL) AND (te.fk_object IS NOT NULL)))";
759  } else {
760  $sqlwhere[] = " (te.".$key." = ".$arrayquery['options_'.$key.'_cnct'].")";
761  }
762  }
763  } else {
764  if (is_array($arrayquery['options_'.$key.'_cnct'])) {
765  $sqlwhere[] = " (te.".$key." IN ('".implode("','", $arrayquery['options_'.$key.'_cnct'])."'))";
766  } elseif (!empty($arrayquery['options_'.$key.'_cnct'])) {
767  $sqlwhere[] = " (te.".$key." LIKE '".$this->db->escape($arrayquery['options_'.$key.'_cnct'])."')";
768  }
769  }
770  }
771 
772  if (!empty($withThirdpartyFilter)) {
773  if (array_key_exists('cust_saleman', $arrayquery)) {
774  $sql .= " LEFT OUTER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as saleman ON saleman.fk_soc=ts.rowid ";
775  }
776  if (array_key_exists('cust_categ', $arrayquery)) {
777  $sql .= " LEFT OUTER JOIN ".MAIN_DB_PREFIX."categorie_societe as custcateg ON custcateg.fk_soc=ts.rowid ";
778  }
779 
780  if (!empty($arrayquery['cust_name'])) {
781  $sqlwhere[] = $this->transformToSQL('ts.nom', $arrayquery['cust_name']);
782  }
783  if (!empty($arrayquery['cust_code'])) {
784  $sqlwhere[] = $this->transformToSQL('ts.code_client', $arrayquery['cust_code']);
785  }
786  if (!empty($arrayquery['cust_adress'])) {
787  $sqlwhere[] = $this->transformToSQL('ts.address', $arrayquery['cust_adress']);
788  }
789  if (!empty($arrayquery['cust_zip'])) {
790  $sqlwhere[] = $this->transformToSQL('ts.zip', $arrayquery['cust_zip']);
791  }
792  if (!empty($arrayquery['cust_city'])) {
793  $sqlwhere[] = $this->transformToSQL('ts.town', $arrayquery['cust_city']);
794  }
795  if (!empty($arrayquery['cust_mothercompany'])) {
796  $str = $this->transformToSQL('nom', $arrayquery['cust_mothercompany']);
797  $sqlwhere[] = " (ts.parent IN (SELECT rowid FROM ".MAIN_DB_PREFIX."societe WHERE (".$str.")))";
798  }
799  if (!empty($arrayquery['cust_status']) && count($arrayquery['cust_status']) > 0) {
800  $sqlwhere[] = " (ts.status IN (".implode(',', $arrayquery['cust_status'])."))";
801  }
802  if (!empty($arrayquery['cust_typecust']) && count($arrayquery['cust_typecust']) > 0) {
803  $sqlwhere[] = " (ts.client IN (".implode(',', $arrayquery['cust_typecust'])."))";
804  }
805  if (!empty($arrayquery['cust_comm_status']) && count($arrayquery['cust_comm_status'] > 0)) {
806  $sqlwhere[] = " (ts.fk_stcomm IN (".implode(',', $arrayquery['cust_comm_status'])."))";
807  }
808  if (!empty($arrayquery['cust_prospect_status']) && count($arrayquery['cust_prospect_status']) > 0) {
809  $sqlwhere[] = " (ts.fk_prospectlevel IN ('".implode("','", $arrayquery['cust_prospect_status'])."'))";
810  }
811  if (!empty($arrayquery['cust_typeent']) && count($arrayquery['cust_typeent']) > 0) {
812  $sqlwhere[] = " (ts.fk_typent IN (".implode(',', $arrayquery['cust_typeent'])."))";
813  }
814  if (!empty($arrayquery['cust_saleman']) && count($arrayquery['cust_saleman']) > 0) {
815  $sqlwhere[] = " (saleman.fk_user IN (".implode(',', $arrayquery['cust_saleman'])."))";
816  }
817  if (!empty($arrayquery['cust_country']) && count($arrayquery['cust_country']) > 0) {
818  $sqlwhere[] = " (ts.fk_pays IN (".implode(',', $arrayquery['cust_country'])."))";
819  }
820  if (!empty($arrayquery['cust_effectif_id']) && count($arrayquery['cust_effectif_id']) > 0) {
821  $sqlwhere[] = " (ts.fk_effectif IN (".implode(',', $arrayquery['cust_effectif_id'])."))";
822  }
823  if (!empty($arrayquery['cust_categ']) && count($arrayquery['cust_categ']) > 0) {
824  $sqlwhere[] = " (custcateg.fk_categorie IN (".implode(',', $arrayquery['cust_categ'])."))";
825  }
826  if (!empty($arrayquery['cust_language']) && count($arrayquery['cust_language']) > 0) {
827  $sqlwhere[] = " (ts.default_lang IN ('".implode("','", $arrayquery['cust_language'])."'))";
828  }
829 
830  //Standard Extrafield feature
831  if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) {
832  $socstatic = new Societe($this->db);
833  $elementtype = $socstatic->table_element;
834 
835  // fetch optionals attributes and labels
836  dol_include_once('/core/class/extrafields.class.php');
837  $extrafields = new ExtraFields($this->db);
838 
839  $extrafields->fetch_name_optionals_label($elementtype);
840 
841  foreach ($extrafields->attributes[$elementtype]['label'] as $key => $val) {
842  if (($extrafields->attributes[$elementtype]['type'][$key] == 'varchar') ||
843  ($extrafields->attributes[$elementtype]['type'][$key] == 'text')) {
844  if (!empty($arrayquery['options_'.$key])) {
845  $sqlwhere[] = " (tse.".$key." LIKE '".$this->db->escape($arrayquery['options_'.$key])."')";
846  }
847  } elseif (($extrafields->attributes[$elementtype]['type'][$key] == 'int') ||
848  ($extrafields->attributes[$elementtype]['type'][$key] == 'double')) {
849  if (!empty($arrayquery['options_'.$key.'_max'])) {
850  $sqlwhere[] = " (tse.".$key." >= ".$arrayquery['options_'.$key.'_max']." AND tse.".$key." <= ".$arrayquery['options_'.$key.'_min'].")";
851  }
852  } elseif (($extrafields->attributes[$elementtype]['type'][$key] == 'date') ||
853  ($extrafields->attributes[$elementtype]['type'][$key] == 'datetime')) {
854  if (!empty($arrayquery['options_'.$key.'_end_dt'])) {
855  $sqlwhere[] = " (tse.".$key." >= '".$this->db->idate($arrayquery['options_'.$key.'_st_dt'])."' AND tse.".$key." <= '".$this->db->idate($arrayquery['options_'.$key.'_end_dt'])."')";
856  }
857  } elseif ($extrafields->attributes[$elementtype]['type'][$key] == 'boolean') {
858  if ($arrayquery['options_'.$key] != '') {
859  $sqlwhere[] = " (tse.".$key." = ".$arrayquery['options_'.$key].")";
860  }
861  } else {
862  if (is_array($arrayquery['options_'.$key])) {
863  $sqlwhere[] = " (tse.".$key." IN ('".implode("','", $arrayquery['options_'.$key])."'))";
864  } elseif (!empty($arrayquery['options_'.$key])) {
865  $sqlwhere[] = " (tse.".$key." LIKE '".$this->db->escape($arrayquery['options_'.$key])."')";
866  }
867  }
868  }
869  }
870  }
871  }
872  if (count($sqlwhere) > 0) {
873  $sql .= " WHERE ".implode(" AND ", $sqlwhere);
874  }
875  }
876 
877  dol_syslog(get_class($this)."::query_contact sql=".$sql, LOG_DEBUG);
878  $resql = $this->db->query($sql);
879  if ($resql) {
880  $this->contact_lines = array();
881  $num = $this->db->num_rows($resql);
882  $i = 0;
883 
884  if ($num) {
885  while ($i < $num) {
886  $obj = $this->db->fetch_object($resql);
887 
888  $this->contact_lines[$i] = $obj->rowid;
889 
890  $i++;
891  }
892  }
893  $this->db->free($resql);
894 
895  return $num;
896  } else {
897  $this->error = "Error ".$this->db->lasterror();
898  dol_syslog(get_class($this)."::query_contact ".$this->error, LOG_ERR);
899  return -1;
900  }
901  }
902 
903 
913  public function transformToSQL($column_to_test, $criteria)
914  {
915  $return_sql_criteria = '(';
916 
917  //This is a multiple value test
918  if (preg_match('/;/', $criteria)) {
919  $return_sql_not_like = array();
920  $return_sql_like = array();
921 
922  $criteria_array = explode(';', $criteria);
923  foreach ($criteria_array as $inter_criteria) {
924  if (preg_match('/!/', $inter_criteria)) {
925  $return_sql_not_like[] = '('.$column_to_test.' NOT LIKE \''.str_replace('!', '', $inter_criteria).'\')';
926  } else {
927  $return_sql_like[] = '('.$column_to_test.' LIKE \''.$inter_criteria.'\')';
928  }
929  }
930 
931  if (count($return_sql_like) > 0) {
932  $return_sql_criteria .= '('.implode(' OR ', $return_sql_like).')';
933  }
934  if (count($return_sql_not_like) > 0) {
935  $return_sql_criteria .= ' AND ('.implode(' AND ', $return_sql_not_like).')';
936  }
937  } else {
938  $return_sql_criteria .= $column_to_test.' LIKE \''.$this->db->escape($criteria).'\'';
939  }
940 
941  $return_sql_criteria .= ')';
942 
943  return $return_sql_criteria;
944  }
945 }
if(!function_exists('dol_getprefix')) dol_include_once($relpath, $classname= '')
Make an include_once using default root and alternate root if it fails.
fetch($id)
Load object in memory from the database.
Class to manage contact/addresses.
fetch_by_element($id=0, $type_element= 'mailing')
Load object in memory from the database.
query_contact($arrayquery, $withThirdpartyFilter=0)
Load object in memory from database.
dol_now($mode= 'auto')
Return date for now.
query_thirdparty($arrayquery)
Load object in memory from database.
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition: repair.php:108
$conf db
API class for accounts.
Definition: inc.php:54
Class to manage standard extra fields.
savequery($user, $arrayquery)
Save query in database to retrieve it.
fetch_by_mailing($id=0)
Load object in memory from the database.
Class to manage third parties objects (customers, suppliers, prospects...)
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
transformToSQL($column_to_test, $criteria)
Parse criteria to return a SQL qury formated.
Class to manage advanced emailing target selector.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)
create($user, $notrigger=0)
Create object into database.
update($user, $notrigger=0)
Update object into database.