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