dolibarr  7.0.0-beta
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 <http://www.gnu.org/licenses/>.
17 */
18 
30 {
31 
32  var $db;
33  var $error;
34  var $errors = array();
35  var $element='advtargetemailing';
36  var $table_element='advtargetemailing';
37 
38  var $id;
39 
40  var $name;
41  var $entity;
42  var $fk_mailing;
43  var $filtervalue;
44  var $fk_user_author;
45  var $datec='';
46  var $fk_user_mod;
47  var $tms='';
48 
49  var $select_target_type = array();
50  var $type_statuscommprospect=array();
51  var $thirdparty_lines;
52  var $contact_lines;
53 
54 
60  function __construct($db)
61  {
62  global $langs;
63  $langs->load('customers');
64 
65  $this->db = $db;
66 
67  $this->select_target_type = array(
68  '2' => $langs->trans('Contacts'),
69  '1' => $langs->trans('Contacts') . '+' . $langs->trans('ThirdParty'),
70  '3' => $langs->trans('ThirdParty'),
71  '4' => $langs->trans('ContactsWithThirdpartyFilter')
72  );
73  $this->type_statuscommprospect = array(
74  - 1 => $langs->trans("StatusProspect-1"),
75  0 => $langs->trans("StatusProspect0"),
76  1 => $langs->trans("StatusProspect1"),
77  2 => $langs->trans("StatusProspect2"),
78  3 => $langs->trans("StatusProspect3")
79  );
80  }
81 
89  function create($user, $notrigger=0)
90  {
91  global $conf, $langs;
92  $error=0;
93 
94  // Clean parameters
95  if (isset($this->fk_mailing)) $this->fk_mailing=trim($this->fk_mailing);
96  if (isset($this->name)) $this->name=trim($this->name);
97  if (isset($this->filtervalue)) $this->filtervalue=trim($this->filtervalue);
98  if (isset($this->fk_user_author)) $this->fk_user_author=trim($this->fk_user_author);
99  if (isset($this->fk_user_mod)) $this->fk_user_mod=trim($this->fk_user_mod);
100 
101 
102 
103  // Check parameters
104  // Put here code to add control on parameters values
105 
106  // Insert request
107  $sql = "INSERT INTO ".MAIN_DB_PREFIX."advtargetemailing(";
108 
109  $sql.= "name,";
110  $sql.= "entity,";
111  $sql.= "fk_mailing,";
112  $sql.= "filtervalue,";
113  $sql.= "fk_user_author,";
114  $sql.= "datec,";
115  $sql.= "fk_user_mod";
116 
117 
118  $sql.= ") VALUES (";
119 
120  $sql.= " ".(! isset($this->name)?'NULL':"'".$this->db->escape($this->name)."'").",";
121  $sql.= " ".$conf->entity.",";
122  $sql.= " ".(! isset($this->fk_mailing)?'NULL':"'".$this->db->escape($this->fk_mailing)."'").",";
123  $sql.= " ".(! isset($this->filtervalue)?'NULL':"'".$this->db->escape($this->filtervalue)."'").",";
124  $sql.= " ".$user->id.",";
125  $sql.= " '".$this->db->idate(dol_now())."',";
126  $sql.= " ".$user->id;
127 
128 
129  $sql.= ")";
130 
131  $this->db->begin();
132 
133  dol_syslog(get_class($this)."::create sql=".$sql, LOG_DEBUG);
134  $resql=$this->db->query($sql);
135  if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
136 
137  if (! $error)
138  {
139  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."advtargetemailing");
140 
141  if (! $notrigger)
142  {
143  // Uncomment this and change MYOBJECT to your own tag if you
144  // want this action calls a trigger.
145 
147  //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
148  //$interface=new Interfaces($this->db);
149  //$result=$interface->run_triggers('MYOBJECT_CREATE',$this,$user,$langs,$conf);
150  //if ($result < 0) { $error++; $this->errors=$interface->errors; }
152  }
153  }
154 
155  // Commit or rollback
156  if ($error)
157  {
158  foreach($this->errors as $errmsg)
159  {
160  dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
161  $this->error.=($this->error?', '.$errmsg:$errmsg);
162  }
163  $this->db->rollback();
164  return -1*$error;
165  }
166  else
167  {
168  $this->db->commit();
169  return $this->id;
170  }
171  }
172 
179  function fetch($id)
180  {
181  global $langs;
182  $sql = "SELECT";
183  $sql.= " t.rowid,";
184 
185  $sql.= " t.name,";
186  $sql.= " t.entity,";
187  $sql.= " t.fk_mailing,";
188  $sql.= " t.filtervalue,";
189  $sql.= " t.fk_user_author,";
190  $sql.= " t.datec,";
191  $sql.= " t.fk_user_mod,";
192  $sql.= " t.tms";
193 
194  $sql.= " FROM ".MAIN_DB_PREFIX."advtargetemailing as t";
195  $sql.= " WHERE t.rowid = ".$id;
196 
197  dol_syslog(get_class($this)."::fetch sql=".$sql, LOG_DEBUG);
198  $resql=$this->db->query($sql);
199  if ($resql)
200  {
201  if ($this->db->num_rows($resql))
202  {
203  $obj = $this->db->fetch_object($resql);
204 
205  $this->id = $obj->rowid;
206 
207  $this->name = $obj->name;
208  $this->entity = $obj->entity;
209  $this->fk_mailing = $obj->fk_mailing;
210  $this->filtervalue = $obj->filtervalue;
211  $this->fk_user_author = $obj->fk_user_author;
212  $this->datec = $this->db->jdate($obj->datec);
213  $this->fk_user_mod = $obj->fk_user_mod;
214  $this->tms = $this->db->jdate($obj->tms);
215 
216  }
217  $this->db->free($resql);
218 
219  return 1;
220  }
221  else
222  {
223  $this->error="Error ".$this->db->lasterror();
224  dol_syslog(get_class($this)."::fetch ".$this->error, LOG_ERR);
225  return -1;
226  }
227  }
228 
235  function fetch_by_mailing($id=0)
236  {
237  global $langs;
238  $sql = "SELECT";
239  $sql.= " t.rowid,";
240 
241  $sql.= " t.name,";
242  $sql.= " t.entity,";
243  $sql.= " t.fk_mailing,";
244  $sql.= " t.filtervalue,";
245  $sql.= " t.fk_user_author,";
246  $sql.= " t.datec,";
247  $sql.= " t.fk_user_mod,";
248  $sql.= " t.tms";
249 
250  $sql.= " FROM ".MAIN_DB_PREFIX."advtargetemailing as t";
251  if (!empty($id)) {
252  $sql.= " WHERE t.fk_mailing = ".$id;
253  }else {
254  $sql.= " WHERE t.fk_mailing = ".$this->fk_mailing;
255  }
256 
257  dol_syslog(get_class($this)."::fetch sql=".$sql, LOG_DEBUG);
258  $resql=$this->db->query($sql);
259  if ($resql)
260  {
261  if ($this->db->num_rows($resql))
262  {
263  $obj = $this->db->fetch_object($resql);
264 
265  $this->id = $obj->rowid;
266 
267  $this->name = $obj->name;
268  $this->entity = $obj->entity;
269  $this->fk_mailing = $obj->fk_mailing;
270  $this->filtervalue = $obj->filtervalue;
271  $this->fk_user_author = $obj->fk_user_author;
272  $this->datec = $this->db->jdate($obj->datec);
273  $this->fk_user_mod = $obj->fk_user_mod;
274  $this->tms = $this->db->jdate($obj->tms);
275 
276  }
277  $this->db->free($resql);
278 
279  return 1;
280  }
281  else
282  {
283  $this->error="Error ".$this->db->lasterror();
284  dol_syslog(get_class($this)."::fetch ".$this->error, LOG_ERR);
285  return -1;
286  }
287  }
288 
296  function update($user, $notrigger=0)
297  {
298  global $conf, $langs;
299  $error=0;
300 
301  // Clean parameters
302  if (isset($this->fk_mailing)) $this->fk_mailing=trim($this->fk_mailing);
303  if (isset($this->name)) $this->name=trim($this->name);
304  if (isset($this->filtervalue)) $this->filtervalue=trim($this->filtervalue);
305  if (isset($this->fk_user_author)) $this->fk_user_author=trim($this->fk_user_author);
306  if (isset($this->fk_user_mod)) $this->fk_user_mod=trim($this->fk_user_mod);
307 
308 
309 
310  // Check parameters
311  // Put here code to add a control on parameters values
312 
313  // Update request
314  $sql = "UPDATE ".MAIN_DB_PREFIX."advtargetemailing SET";
315 
316  $sql.= " name=".(isset($this->name)?"'".$this->db->escape($this->name)."'":"''").",";
317  $sql.= " entity=".$conf->entity.",";
318  $sql.= " fk_mailing=".(isset($this->fk_mailing)?$this->fk_mailing:"null").",";
319  $sql.= " filtervalue=".(isset($this->filtervalue)?"'".$this->db->escape($this->filtervalue)."'":"null").",";
320  $sql.= " fk_user_mod=".$user->id;
321 
322  $sql.= " WHERE rowid=".$this->id;
323 
324  $this->db->begin();
325 
326  dol_syslog(get_class($this)."::update sql=".$sql, LOG_DEBUG);
327  $resql = $this->db->query($sql);
328  if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
329 
330  if (! $error)
331  {
332  if (! $notrigger)
333  {
334  // Uncomment this and change MYOBJECT to your own tag if you
335  // want this action calls a trigger.
336 
338  //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
339  //$interface=new Interfaces($this->db);
340  //$result=$interface->run_triggers('MYOBJECT_MODIFY',$this,$user,$langs,$conf);
341  //if ($result < 0) { $error++; $this->errors=$interface->errors; }
343  }
344  }
345 
346  // Commit or rollback
347  if ($error)
348  {
349  foreach($this->errors as $errmsg)
350  {
351  dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
352  $this->error.=($this->error?', '.$errmsg:$errmsg);
353  }
354  $this->db->rollback();
355  return -1*$error;
356  }
357  else
358  {
359  $this->db->commit();
360  return 1;
361  }
362  }
363 
371  function delete($user, $notrigger=0)
372  {
373  global $conf, $langs;
374  $error=0;
375 
376  $this->db->begin();
377 
378  if (! $error)
379  {
380  if (! $notrigger)
381  {
382  // Uncomment this and change MYOBJECT to your own tag if you
383  // want this action calls a trigger.
384 
386  //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
387  //$interface=new Interfaces($this->db);
388  //$result=$interface->run_triggers('MYOBJECT_DELETE',$this,$user,$langs,$conf);
389  //if ($result < 0) { $error++; $this->errors=$interface->errors; }
391  }
392  }
393 
394  if (! $error)
395  {
396  $sql = "DELETE FROM ".MAIN_DB_PREFIX."advtargetemailing";
397  $sql.= " WHERE rowid=".$this->id;
398 
399  dol_syslog(get_class($this)."::delete sql=".$sql);
400  $resql = $this->db->query($sql);
401  if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
402  }
403 
404  // Commit or rollback
405  if ($error)
406  {
407  foreach($this->errors as $errmsg)
408  {
409  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
410  $this->error.=($this->error?', '.$errmsg:$errmsg);
411  }
412  $this->db->rollback();
413  return -1*$error;
414  }
415  else
416  {
417  $this->db->commit();
418  return 1;
419  }
420  }
421 
422 
430  function savequery($user,$arrayquery)
431  {
432  global $langs,$conf;
433 
434  if (!empty($arrayquery)) {
435  $result=$this->fetch_by_mailing($this->fk_mailing);
436  $this->filtervalue=json_encode($arrayquery);
437  if ($result<0) {
438  return -1;
439  }
440  if (!empty($this->id)) {
441  $this->update($user);
442  }else {
443  $this->create($user);
444  }
445  }
446  }
447 
448 
449 
456  function query_thirdparty($arrayquery)
457  {
458  global $langs,$conf;
459 
460  $sql = "SELECT";
461  $sql.= " t.rowid";
462  $sql.= " FROM " . MAIN_DB_PREFIX . "societe as t";
463  $sql.= " LEFT OUTER JOIN " . MAIN_DB_PREFIX . "societe_extrafields as te ON te.fk_object=t.rowid ";
464 
465  $sqlwhere=array();
466 
467  $sqlwhere[]= 't.entity IN ('.getEntity('societe').')';
468 
469  if (count($arrayquery)>0) {
470 
471  if (array_key_exists('cust_saleman', $arrayquery)) {
472  $sql.= " LEFT OUTER JOIN " . MAIN_DB_PREFIX . "societe_commerciaux as saleman ON saleman.fk_soc=t.rowid ";
473  }
474  if (array_key_exists('cust_categ', $arrayquery)) {
475  $sql.= " LEFT OUTER JOIN " . MAIN_DB_PREFIX . "categorie_societe as custcateg ON custcateg.fk_soc=t.rowid ";
476  }
477 
478  if (!empty($arrayquery['cust_name'])) {
479 
480  $sqlwhere[]= $this->transformToSQL('t.nom',$arrayquery['cust_name']);
481  }
482  if (!empty($arrayquery['cust_code'])) {
483  $sqlwhere[]= $this->transformToSQL('t.code_client',$arrayquery['cust_code']);
484  }
485  if (!empty($arrayquery['cust_adress'])) {
486  $sqlwhere[]= $this->transformToSQL('t.address',$arrayquery['cust_adress']);
487  }
488  if (!empty($arrayquery['cust_zip'])) {
489  $sqlwhere[]= $this->transformToSQL('t.zip',$arrayquery['cust_zip']);
490  }
491  if (!empty($arrayquery['cust_city'])) {
492  $sqlwhere[]= $this->transformToSQL('t.town',$arrayquery['cust_city']);
493  }
494  if (!empty($arrayquery['cust_mothercompany'])) {
495  $str=$this->transformToSQL('nom',$arrayquery['cust_mothercompany']);
496  $sqlwhere[]= " (t.parent IN (SELECT rowid FROM " . MAIN_DB_PREFIX . "societe WHERE (".$str.")))";
497  }
498  if (!empty($arrayquery['cust_status']) && count($arrayquery['cust_status'])>0) {
499  $sqlwhere[]= " (t.status IN (".implode(',',$arrayquery['cust_status'])."))";
500  }
501  if (!empty($arrayquery['cust_typecust']) && count($arrayquery['cust_typecust'])>0) {
502  $sqlwhere[]= " (t.client IN (".implode(',',$arrayquery['cust_typecust'])."))";
503  }
504  if (!empty($arrayquery['cust_comm_status']) && count($arrayquery['cust_comm_status']>0)) {
505  $sqlwhere[]= " (t.fk_stcomm IN (".implode(',',$arrayquery['cust_comm_status'])."))";
506  }
507  if (!empty($arrayquery['cust_prospect_status']) && count($arrayquery['cust_prospect_status'])>0) {
508  $sqlwhere[]= " (t.fk_prospectlevel IN ('".implode("','",$arrayquery['cust_prospect_status'])."'))";
509  }
510  if (!empty($arrayquery['cust_typeent']) && count($arrayquery['cust_typeent'])>0) {
511  $sqlwhere[]= " (t.fk_typent IN (".implode(',',$arrayquery['cust_typeent'])."))";
512  }
513  if (!empty($arrayquery['cust_saleman']) && count($arrayquery['cust_saleman'])>0) {
514  $sqlwhere[]= " (saleman.fk_user IN (".implode(',',$arrayquery['cust_saleman'])."))";
515  }
516  if (!empty($arrayquery['cust_country']) && count($arrayquery['cust_country'])>0) {
517  $sqlwhere[]= " (t.fk_pays IN (".implode(',',$arrayquery['cust_country'])."))";
518  }
519  if (!empty($arrayquery['cust_effectif_id']) && count($arrayquery['cust_effectif_id'])>0) {
520  $sqlwhere[]= " (t.fk_effectif IN (".implode(',',$arrayquery['cust_effectif_id'])."))";
521  }
522  if (!empty($arrayquery['cust_categ']) && count($arrayquery['cust_categ'])>0) {
523  $sqlwhere[]= " (custcateg.fk_categorie IN (".implode(',',$arrayquery['cust_categ'])."))";
524  }
525  if (!empty($arrayquery['cust_language']) && count($arrayquery['cust_language'])>0) {
526  $sqlwhere[]= " (t.default_lang IN ('".implode("','",$arrayquery['cust_language'])."'))";
527  }
528 
529  //Standard Extrafield feature
530  if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) {
531  // fetch optionals attributes and labels
532  dol_include_once('/core/class/extrafields.class.php');
533  $extrafields = new ExtraFields($this->db);
534  $extralabels=$extrafields->fetch_name_optionals_label('societe');
535 
536  foreach($extralabels as $key=>$val) {
537 
538  if (($extrafields->attribute_type[$key] == 'varchar') ||
539  ($extrafields->attribute_type[$key] == 'text')) {
540  if (!empty($arrayquery['options_'.$key])) {
541  $sqlwhere[]= " (te.".$key." LIKE '".$arrayquery['options_'.$key]."')";
542  }
543  } elseif (($extrafields->attribute_type[$key] == 'int') ||
544  ($extrafields->attribute_type[$key] == 'double')) {
545  if (!empty($arrayquery['options_'.$key.'_max'])) {
546  $sqlwhere[]= " (te.".$key." >= ".$arrayquery['options_'.$key.'_max']." AND te.".$key." <= ".$arrayquery['options_'.$key.'_min'].")";
547  }
548  } else if (($extrafields->attribute_type[$key] == 'date') ||
549  ($extrafields->attribute_type[$key] == 'datetime')) {
550  if (!empty($arrayquery['options_'.$key.'_end_dt'])){
551  $sqlwhere[]= " (te.".$key." >= '".$this->db->idate($arrayquery['options_'.$key.'_st_dt'])."' AND te.".$key." <= '".$this->db->idate($arrayquery['options_'.$key.'_end_dt'])."')";
552  }
553  }else if ($extrafields->attribute_type[$key] == 'boolean') {
554  if ($arrayquery['options_'.$key]!=''){
555  $sqlwhere[]= " (te.".$key." = ".$arrayquery['options_'.$key].")";
556  }
557  }else{
558  if (is_array($arrayquery['options_'.$key])) {
559  $sqlwhere[]= " (te.".$key." IN ('".implode("','",$arrayquery['options_'.$key])."'))";
560  } elseif (!empty($arrayquery['options_'.$key])) {
561  $sqlwhere[]= " (te.".$key." LIKE '".$arrayquery['options_'.$key]."')";
562  }
563  }
564 
565  }
566 
567 
568  }
569 
570  if (count($sqlwhere)>0) $sql.= " WHERE ".implode(" AND ",$sqlwhere);
571 
572  }
573 
574 
575  dol_syslog(get_class($this) . "::query_thirdparty sql=" . $sql, LOG_DEBUG);
576  $resql = $this->db->query($sql);
577  if ($resql) {
578  $this->thirdparty_lines = array();
579  $num = $this->db->num_rows($resql);
580  $i = 0;
581 
582  if ($num)
583  {
584  while( $i < $num)
585  {
586  $obj = $this->db->fetch_object($resql);
587 
588  $this->thirdparty_lines[$i] = $obj->rowid;
589 
590  $i++;
591  }
592  }
593  $this->db->free($resql);
594 
595  return $num;
596  } else {
597  $this->error = "Error " . $this->db->lasterror();
598  dol_syslog(get_class($this) . "::query_thirdparty " . $this->error, LOG_ERR);
599  return -1;
600  }
601  }
602 
610  function query_contact($arrayquery, $withThirdpartyFilter = 0)
611  {
612  global $langs,$conf;
613 
614  $sql = "SELECT";
615  $sql.= " t.rowid";
616  $sql.= " FROM " . MAIN_DB_PREFIX . "socpeople as t";
617  $sql.= " LEFT OUTER JOIN " . MAIN_DB_PREFIX . "socpeople_extrafields as te ON te.fk_object=t.rowid ";
618 
619  if (! empty($withThirdpartyFilter)) {
620  $sql .= " LEFT OUTER JOIN " . MAIN_DB_PREFIX . "societe as ts ON ts.rowid=t.fk_soc";
621  $sql .= " LEFT OUTER JOIN " . MAIN_DB_PREFIX . "societe_extrafields as tse ON tse.fk_object=ts.rowid ";
622  }
623 
624  $sqlwhere=array();
625 
626  $sqlwhere[]= 't.entity IN ('.getEntity('socpeople').')';
627 
628  if (count($arrayquery)>0) {
629 
630  if (array_key_exists('contact_categ', $arrayquery)) {
631  $sql.= " LEFT OUTER JOIN " . MAIN_DB_PREFIX . "categorie_contact as contactcateg ON contactcateg.fk_socpeople=t.rowid ";
632  }
633 
634  if (!empty($arrayquery['contact_lastname'])) {
635  $sqlwhere[]=$this->transformToSQL('t.lastname',$arrayquery['contact_lastname']);
636  }
637  if (!empty($arrayquery['contact_firstname'])) {
638  $sqlwhere[]=$this->transformToSQL('t.firstname',$arrayquery['contact_firstname']);
639  }
640  if (!empty($arrayquery['contact_country']) && count($arrayquery['contact_country'])) {
641  $sqlwhere[]= " (t.fk_pays IN (".$this->db->escape(implode(',',$arrayquery['contact_country']))."))";
642  }
643  if (!empty($arrayquery['contact_status']) && count($arrayquery['contact_status'])>0) {
644  $sqlwhere[]= " (t.statut IN (".$this->db->escape(implode(',',$arrayquery['contact_status']))."))";
645  }
646  if (!empty($arrayquery['contact_civility']) && count($arrayquery['contact_civility'])>0) {
647  $sqlwhere[]= " (t.civility IN ('".$this->db->escape(implode("','",$arrayquery['contact_civility']))."'))";
648  }
649  if ($arrayquery['contact_no_email']!='') {
650  $sqlwhere[]= " (t.no_email='".$this->db->escape($arrayquery['contact_no_email'])."')";
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 
662  //Standard Extrafield feature
663  if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) {
664  // fetch optionals attributes and labels
665  dol_include_once('/core/class/extrafields.class.php');
666  $extrafields = new ExtraFields($this->db);
667  $extralabels=$extrafields->fetch_name_optionals_label('socpeople');
668 
669  foreach($extralabels as $key=>$val) {
670 
671  if (($extrafields->attribute_type[$key] == 'varchar') ||
672  ($extrafields->attribute_type[$key] == 'text')) {
673  if (!empty($arrayquery['options_'.$key.'_cnct'])) {
674  $sqlwhere[]= " (te.".$key." LIKE '".$arrayquery['options_'.$key.'_cnct']."')";
675  }
676  } elseif (($extrafields->attribute_type[$key] == 'int') ||
677  ($extrafields->attribute_type[$key] == 'double')) {
678  if (!empty($arrayquery['options_'.$key.'_max'.'_cnct'])) {
679  $sqlwhere[]= " (te.".$key." >= ".$arrayquery['options_'.$key.'_max'.'_cnct']." AND te.".$key." <= ".$arrayquery['options_'.$key.'_min'.'_cnct'].")";
680  }
681  } else if (($extrafields->attribute_type[$key] == 'date') ||
682  ($extrafields->attribute_type[$key] == 'datetime')) {
683  if (!empty($arrayquery['options_'.$key.'_end_dt'.'_cnct'])){
684  $sqlwhere[]= " (te.".$key." >= '".$this->db->idate($arrayquery['options_'.$key.'_st_dt'.'_cnct'])."' AND te.".$key." <= '".$this->db->idate($arrayquery['options_'.$key.'_end_dt'.'_cnct'])."')";
685  }
686  }else if ($extrafields->attribute_type[$key] == 'boolean') {
687  if ($arrayquery['options_'.$key.'_cnct']!=''){
688  if ($arrayquery['options_'.$key.'_cnct']==0) {
689  $sqlwhere[]= " (te.".$key." = ".$arrayquery['options_'.$key.'_cnct']." OR ((te.".$key." IS NULL) AND (te.fk_object IS NOT NULL)))";
690  }else {
691  $sqlwhere[]= " (te.".$key." = ".$arrayquery['options_'.$key.'_cnct'].")";
692  }
693  }
694  }else{
695  if (is_array($arrayquery['options_'.$key.'_cnct'])) {
696  $sqlwhere[]= " (te.".$key." IN ('".implode("','",$arrayquery['options_'.$key.'_cnct'])."'))";
697  } elseif (!empty($arrayquery['options_'.$key.'_cnct'])) {
698  $sqlwhere[]= " (te.".$key." LIKE '".$arrayquery['options_'.$key.'_cnct']."')";
699  }
700  }
701 
702  }
703 
704  if (! empty($withThirdpartyFilter)) {
705  if (array_key_exists('cust_saleman', $arrayquery)) {
706  $sql.= " LEFT OUTER JOIN " . MAIN_DB_PREFIX . "societe_commerciaux as saleman ON saleman.fk_soc=ts.rowid ";
707  }
708  if (array_key_exists('cust_categ', $arrayquery)) {
709  $sql.= " LEFT OUTER JOIN " . MAIN_DB_PREFIX . "categorie_societe as custcateg ON custcateg.fk_soc=ts.rowid ";
710  }
711 
712  if (!empty($arrayquery['cust_name'])) {
713 
714  $sqlwhere[]= $this->transformToSQL('ts.nom',$arrayquery['cust_name']);
715  }
716  if (!empty($arrayquery['cust_code'])) {
717  $sqlwhere[]= $this->transformToSQL('ts.code_client',$arrayquery['cust_code']);
718  }
719  if (!empty($arrayquery['cust_adress'])) {
720  $sqlwhere[]= $this->transformToSQL('ts.address',$arrayquery['cust_adress']);
721  }
722  if (!empty($arrayquery['cust_zip'])) {
723  $sqlwhere[]= $this->transformToSQL('ts.zip',$arrayquery['cust_zip']);
724  }
725  if (!empty($arrayquery['cust_city'])) {
726  $sqlwhere[]= $this->transformToSQL('ts.town',$arrayquery['cust_city']);
727  }
728  if (!empty($arrayquery['cust_mothercompany'])) {
729  $str=$this->transformToSQL('nom',$arrayquery['cust_mothercompany']);
730  $sqlwhere[]= " (ts.parent IN (SELECT rowid FROM " . MAIN_DB_PREFIX . "societe WHERE (".$str.")))";
731  }
732  if (!empty($arrayquery['cust_status']) && count($arrayquery['cust_status'])>0) {
733  $sqlwhere[]= " (ts.status IN (".implode(',',$arrayquery['cust_status'])."))";
734  }
735  if (!empty($arrayquery['cust_typecust']) && count($arrayquery['cust_typecust'])>0) {
736  $sqlwhere[]= " (ts.client IN (".implode(',',$arrayquery['cust_typecust'])."))";
737  }
738  if (!empty($arrayquery['cust_comm_status']) && count($arrayquery['cust_comm_status']>0)) {
739  $sqlwhere[]= " (ts.fk_stcomm IN (".implode(',',$arrayquery['cust_comm_status'])."))";
740  }
741  if (!empty($arrayquery['cust_prospect_status']) && count($arrayquery['cust_prospect_status'])>0) {
742  $sqlwhere[]= " (ts.fk_prospectlevel IN ('".implode("','",$arrayquery['cust_prospect_status'])."'))";
743  }
744  if (!empty($arrayquery['cust_typeent']) && count($arrayquery['cust_typeent'])>0) {
745  $sqlwhere[]= " (ts.fk_typent IN (".implode(',',$arrayquery['cust_typeent'])."))";
746  }
747  if (!empty($arrayquery['cust_saleman']) && count($arrayquery['cust_saleman'])>0) {
748  $sqlwhere[]= " (saleman.fk_user IN (".implode(',',$arrayquery['cust_saleman'])."))";
749  }
750  if (!empty($arrayquery['cust_country']) && count($arrayquery['cust_country'])>0) {
751  $sqlwhere[]= " (ts.fk_pays IN (".implode(',',$arrayquery['cust_country'])."))";
752  }
753  if (!empty($arrayquery['cust_effectif_id']) && count($arrayquery['cust_effectif_id'])>0) {
754  $sqlwhere[]= " (ts.fk_effectif IN (".implode(',',$arrayquery['cust_effectif_id'])."))";
755  }
756  if (!empty($arrayquery['cust_categ']) && count($arrayquery['cust_categ'])>0) {
757  $sqlwhere[]= " (custcateg.fk_categorie IN (".implode(',',$arrayquery['cust_categ'])."))";
758  }
759  if (!empty($arrayquery['cust_language']) && count($arrayquery['cust_language'])>0) {
760  $sqlwhere[]= " (ts.default_lang IN ('".implode("','",$arrayquery['cust_language'])."'))";
761  }
762 
763  //Standard Extrafield feature
764  if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) {
765  // fetch optionals attributes and labels
766  dol_include_once('/core/class/extrafields.class.php');
767  $extrafields = new ExtraFields($this->db);
768  $extralabels=$extrafields->fetch_name_optionals_label('societe');
769 
770  foreach($extralabels as $key=>$val) {
771 
772  if (($extrafields->attribute_type[$key] == 'varchar') ||
773  ($extrafields->attribute_type[$key] == 'text')) {
774  if (!empty($arrayquery['options_'.$key])) {
775  $sqlwhere[]= " (tse.".$key." LIKE '".$arrayquery['options_'.$key]."')";
776  }
777  } elseif (($extrafields->attribute_type[$key] == 'int') ||
778  ($extrafields->attribute_type[$key] == 'double')) {
779  if (!empty($arrayquery['options_'.$key.'_max'])) {
780  $sqlwhere[]= " (tse.".$key." >= ".$arrayquery['options_'.$key.'_max']." AND tse.".$key." <= ".$arrayquery['options_'.$key.'_min'].")";
781  }
782  } else if (($extrafields->attribute_type[$key] == 'date') ||
783  ($extrafields->attribute_type[$key] == 'datetime')) {
784  if (!empty($arrayquery['options_'.$key.'_end_dt'])){
785  $sqlwhere[]= " (tse.".$key." >= '".$this->db->idate($arrayquery['options_'.$key.'_st_dt'])."' AND tse.".$key." <= '".$this->db->idate($arrayquery['options_'.$key.'_end_dt'])."')";
786  }
787  }else if ($extrafields->attribute_type[$key] == 'boolean') {
788  if ($arrayquery['options_'.$key]!=''){
789  $sqlwhere[]= " (tse.".$key." = ".$arrayquery['options_'.$key].")";
790  }
791  }else{
792  if (is_array($arrayquery['options_'.$key])) {
793  $sqlwhere[]= " (tse.".$key." IN ('".implode("','",$arrayquery['options_'.$key])."'))";
794  } elseif (!empty($arrayquery['options_'.$key])) {
795  $sqlwhere[]= " (tse.".$key." LIKE '".$arrayquery['options_'.$key]."')";
796  }
797  }
798  }
799  }
800  }
801  }
802  if (count($sqlwhere)>0) $sql.= " WHERE ".implode(" AND ",$sqlwhere);
803  }
804 
805  dol_syslog(get_class($this) . "::query_contact sql=" . $sql, LOG_DEBUG);
806  $resql = $this->db->query($sql);
807  if ($resql) {
808  $this->contact_lines = array();
809  $num = $this->db->num_rows($resql);
810  $i = 0;
811 
812  if ($num)
813  {
814  while( $i < $num)
815  {
816  $obj = $this->db->fetch_object($resql);
817 
818  $this->contact_lines[$i] = $obj->rowid;
819 
820  $i++;
821  }
822  }
823  $this->db->free($resql);
824 
825  return $num;
826  } else {
827  $this->error = "Error " . $this->db->lasterror();
828  dol_syslog(get_class($this) . "::query_contact " . $this->error, LOG_ERR);
829  return -1;
830  }
831  }
832 
833 
843  public function transformToSQL($column_to_test,$criteria) {
844  $return_sql_criteria = '(';
845 
846  //This is a multiple value test
847  if (preg_match('/;/',$criteria)) {
848  $return_sql_not_like=array();
849  $return_sql_like=array();
850 
851  $criteria_array=explode(';',$criteria);
852  foreach($criteria_array as $inter_criteria) {
853  if (preg_match('/!/',$inter_criteria)) {
854  $return_sql_not_like[]= '('.$column_to_test.' NOT LIKE \''.str_replace('!', '', $inter_criteria).'\')';
855  } else {
856  $return_sql_like[]= '('.$column_to_test.' LIKE \''.$inter_criteria.'\')';
857  }
858  }
859 
860  if (count($return_sql_like)>0) {
861  $return_sql_criteria .= '(' . implode (' OR ', $return_sql_like) .')';
862  }
863  if (count($return_sql_not_like)>0) {
864  $return_sql_criteria .= ' AND (' . implode (' AND ', $return_sql_not_like).')';
865  }
866 
867  }else {
868  $return_sql_criteria .= $column_to_test . ' LIKE \''.$this->db->escape($criteria).'\'';
869  }
870 
871  $return_sql_criteria .= ')';
872 
873  return $return_sql_criteria;
874  }
875 
876 
877 }
fetch($id)
Load object in memory from the database.
query_contact($arrayquery, $withThirdpartyFilter=0)
Load object in memory from database.
query_thirdparty($arrayquery)
Load object in memory from database.
$table_element
Name of table without prefix where object is stored.
$errors
To return several error codes (or messages)
Class to manage standard extra fields.
savequery($user, $arrayquery)
Save query in database to retreive it.
fetch_by_mailing($id=0)
Load object in memory from the database.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
$element
Id that identify managed objects.
dol_now($mode='gmt')
Return date for now.
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition: repair.php:104
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)&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->societe->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)&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1013
if(!function_exists('dol_getprefix')) dol_include_once($relpath, $classname='')
Return a prefix to use for this Dolibarr instance, for session/cookie names or email id...
$error
To return error code (or message)
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.