148 public function create($user, $notrigger = 0)
150 global $conf, $langs;
154 if (isset($this->fk_element)) {
155 $this->fk_element = (int) $this->fk_element;
157 if (isset($this->type_element)) {
158 $this->type_element = trim($this->type_element);
161 if (isset($this->
name)) {
164 if (isset($this->filtervalue)) {
165 $this->filtervalue = trim($this->filtervalue);
172 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"mailing_advtarget(";
175 $sql .=
"fk_element,";
176 $sql .=
"type_element,";
177 $sql .=
"filtervalue,";
178 $sql .=
"fk_user_author,";
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;
194 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
195 $resql = $this->db->query($sql);
198 $this->errors[] =
"Error ".$this->db->lasterror();
202 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
"mailing_advtarget");
207 foreach ($this->errors as $errmsg) {
208 dol_syslog(get_class($this).
"::create ".$errmsg, LOG_ERR);
209 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
211 $this->db->rollback();
232 $sql .=
" t.entity,";
233 $sql .=
" t.fk_element,";
234 $sql .=
" t.type_element,";
235 $sql .=
" t.filtervalue,";
236 $sql .=
" t.fk_user_author,";
238 $sql .=
" t.fk_user_mod,";
241 $sql .=
" FROM ".MAIN_DB_PREFIX.
"mailing_advtarget as t";
242 $sql .=
" WHERE t.rowid = ".((int) $id);
244 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
245 $resql = $this->db->query($sql);
247 if ($this->db->num_rows($resql)) {
248 $obj = $this->db->fetch_object($resql);
250 $this->
id = $obj->rowid;
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);
262 $this->db->free($resql);
266 $this->error =
"Error ".$this->db->lasterror();
267 dol_syslog(get_class($this).
"::fetch ".$this->error, LOG_ERR);
287 $sql .=
" t.entity,";
288 $sql .=
" t.fk_element,";
289 $sql .=
" t.type_element,";
290 $sql .=
" t.filtervalue,";
291 $sql .=
" t.fk_user_author,";
293 $sql .=
" t.fk_user_mod,";
296 $sql .=
" FROM ".MAIN_DB_PREFIX.
"mailing_advtarget as t";
298 $sql .=
" WHERE t.fk_element = ".((int) $id).
" AND type_element = 'mailing'";
300 $sql .=
" WHERE t.fk_element = ".((int) $this->fk_element).
" AND type_element = 'mailing'";
303 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
304 $resql = $this->db->query($sql);
306 if ($this->db->num_rows($resql)) {
307 $obj = $this->db->fetch_object($resql);
309 $this->
id = $obj->rowid;
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);
321 $this->db->free($resql);
325 $this->error =
"Error ".$this->db->lasterror();
326 dol_syslog(get_class($this).
"::fetch ".$this->error, LOG_ERR);
350 $sql .=
" t.entity,";
351 $sql .=
" t.fk_element,";
352 $sql .=
" t.type_element,";
353 $sql .=
" t.filtervalue,";
354 $sql .=
" t.fk_user_author,";
356 $sql .=
" t.fk_user_mod,";
359 $sql .=
" FROM ".MAIN_DB_PREFIX.
"mailing_advtarget as t";
361 $sql .=
" WHERE t.fk_element = ".((int) $id).
" AND type_element = '".$this->db->escape($type_element).
"'";
363 $sql .=
" WHERE t.fk_element = ".((int) $this->fk_element).
" AND type_element = '".$this->db->escape($type_element).
"'";
366 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
367 $resql = $this->db->query($sql);
369 if ($this->db->num_rows($resql)) {
370 $obj = $this->db->fetch_object($resql);
372 $this->
id = $obj->rowid;
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);
384 $this->db->free($resql);
388 $this->error =
"Error ".$this->db->lasterror();
389 dol_syslog(get_class($this).
"::fetch ".$this->error, LOG_ERR);
401 public function update($user, $notrigger = 0)
403 global $conf, $langs;
407 if (isset($this->fk_element)) {
408 $this->fk_element = (int) $this->fk_element;
410 if (isset($this->type_element)) {
411 $this->type_element = trim($this->type_element);
413 if (isset($this->
name)) {
416 if (isset($this->filtervalue)) {
417 $this->filtervalue = trim($this->filtervalue);
424 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"mailing_advtarget SET";
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;
433 $sql .=
" WHERE rowid=".((int) $this->
id);
436 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
437 $resql = $this->db->query($sql);
440 $this->errors[] =
"Error ".$this->db->lasterror();
445 foreach ($this->errors as $errmsg) {
446 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
447 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
449 $this->db->rollback();
536 global $langs, $conf, $extrafields;
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 ";
545 $sqlwhere[] =
't.entity IN ('.getEntity(
'societe').
')';
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 ";
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 ";
555 if (!empty($arrayquery[
'cust_name'])) {
556 $sqlwhere[] = $this->
transformToSQL(
't.nom', $arrayquery[
'cust_name']);
558 if (!empty($arrayquery[
'cust_code'])) {
559 $sqlwhere[] = $this->
transformToSQL(
't.code_client', $arrayquery[
'cust_code']);
561 if (!empty($arrayquery[
'cust_adress'])) {
562 $sqlwhere[] = $this->
transformToSQL(
't.address', $arrayquery[
'cust_adress']);
564 if (!empty($arrayquery[
'cust_zip'])) {
565 $sqlwhere[] = $this->
transformToSQL(
't.zip', $arrayquery[
'cust_zip']);
567 if (!empty($arrayquery[
'cust_city'])) {
568 $sqlwhere[] = $this->
transformToSQL(
't.town', $arrayquery[
'cust_city']);
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.
")))";
574 if (!empty($arrayquery[
'cust_status']) && count($arrayquery[
'cust_status']) > 0) {
575 $sqlwhere[] =
" (t.status IN (".$this->db->sanitize(implode(
',', $arrayquery[
'cust_status'])).
"))";
577 if (!empty($arrayquery[
'cust_typecust']) && count($arrayquery[
'cust_typecust']) > 0) {
578 $sqlwhere[] =
" (t.client IN (".$this->db->sanitize(implode(
',', $arrayquery[
'cust_typecust'])).
"))";
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'])).
"))";
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).
"))";
586 if (!empty($arrayquery[
'cust_typeent']) && count($arrayquery[
'cust_typeent']) > 0) {
587 $sqlwhere[] =
" (t.fk_typent IN (".$this->db->sanitize(implode(
',', $arrayquery[
'cust_typeent'])).
"))";
589 if (!empty($arrayquery[
'cust_saleman']) && count($arrayquery[
'cust_saleman']) > 0) {
590 $sqlwhere[] =
" (saleman.fk_user IN (".$this->db->sanitize(implode(
',', $arrayquery[
'cust_saleman'])).
"))";
592 if (!empty($arrayquery[
'cust_country']) && count($arrayquery[
'cust_country']) > 0) {
593 $sqlwhere[] =
" (t.fk_pays IN (".$this->db->sanitize(implode(
',', $arrayquery[
'cust_country'])).
"))";
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'])).
"))";
598 if (!empty($arrayquery[
'cust_categ']) && count($arrayquery[
'cust_categ']) > 0) {
599 $sqlwhere[] =
" (custcateg.fk_categorie IN (".$this->db->sanitize(implode(
',', $arrayquery[
'cust_categ'])).
"))";
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).
"))";
607 $socstatic =
new Societe($this->db);
608 $elementtype = $socstatic->table_element;
610 $extrafields->fetch_name_optionals_label($elementtype);
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]).
"')";
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']).
")";
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"]).
"')";
628 } elseif ($extrafields->attributes[$elementtype][
'type'][$key] ==
'boolean') {
629 if ($arrayquery[
'options_'.$key] !=
'') {
630 $sqlwhere[] =
" (te.".$key.
" = ".((int) $arrayquery[
"options_".$key]).
")";
632 } elseif ($extrafields->attributes[$elementtype][
'type'][$key] ==
'link') {
633 if ($arrayquery[
'options_'.$key] > 0) {
634 $sqlwhere[]=
" (te.".$key.
" = ".((int) $arrayquery[
"options_".$key]).
")";
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]).
"')";
646 if (count($sqlwhere) > 0) {
647 $sql .=
" WHERE ".implode(
" AND ", $sqlwhere);
652 dol_syslog(get_class($this).
"::query_thirdparty", LOG_DEBUG);
653 $resql = $this->db->query($sql);
655 $this->thirdparty_lines = array();
656 $num = $this->db->num_rows($resql);
661 $obj = $this->db->fetch_object($resql);
662 $this->thirdparty_lines[$i] = $obj->rowid;
667 $this->db->free($resql);
671 $this->error =
"Error ".$this->db->lasterror();
672 dol_syslog(get_class($this).
"::query_thirdparty ".$this->error, LOG_ERR);
688 global $langs, $conf;
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 ";
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 ";
702 $sqlwhere[] =
't.entity IN ('.getEntity(
'contact').
')';
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 ";
709 if (!empty($arrayquery[
'contact_lastname'])) {
710 $sqlwhere[] = $this->
transformToSQL(
't.lastname', $arrayquery[
'contact_lastname']);
712 if (!empty($arrayquery[
'contact_firstname'])) {
713 $sqlwhere[] = $this->
transformToSQL(
't.firstname', $arrayquery[
'contact_firstname']);
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']))).
"))";
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']))).
"))";
721 if (!empty($arrayquery[
'contact_civility']) && count($arrayquery[
'contact_civility']) > 0) {
722 $sqlwhere[] =
" (t.civility IN (".$this->db->sanitize(
"'".implode(
"','", $arrayquery[
'contact_civility']).
"'", 1).
"))";
724 if ($arrayquery[
'contact_no_email'] !=
'') {
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']).
"'))";
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']).
"'))";
731 $sqlwhere[] = $tmpwhere;
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']).
"')";
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']).
"')";
739 if (!empty($arrayquery[
'contact_categ']) && count($arrayquery[
'contact_categ']) > 0) {
740 $sqlwhere[] =
" (contactcateg.fk_categorie IN (".$this->db->escape(implode(
",", $arrayquery[
'contact_categ'])).
"))";
745 $contactstatic =
new Contact($this->db);
746 $elementtype = $contactstatic->table_element;
751 $extrafields->fetch_name_optionals_label($elementtype);
753 $extrafields->fetch_name_optionals_label($elementtype);
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"]).
"')";
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"]).
")";
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"]).
"')";
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)))";
776 $sqlwhere[] =
" (te.".$key.
" = ".((int) $arrayquery[
"options_".$key.
"_cnct"]).
")";
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"]).
"')";
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 ";
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 ";
796 if (!empty($arrayquery[
'cust_name'])) {
797 $sqlwhere[] = $this->
transformToSQL(
'ts.nom', $arrayquery[
'cust_name']);
799 if (!empty($arrayquery[
'cust_code'])) {
800 $sqlwhere[] = $this->
transformToSQL(
'ts.code_client', $arrayquery[
'cust_code']);
802 if (!empty($arrayquery[
'cust_adress'])) {
803 $sqlwhere[] = $this->
transformToSQL(
'ts.address', $arrayquery[
'cust_adress']);
805 if (!empty($arrayquery[
'cust_zip'])) {
806 $sqlwhere[] = $this->
transformToSQL(
'ts.zip', $arrayquery[
'cust_zip']);
808 if (!empty($arrayquery[
'cust_city'])) {
809 $sqlwhere[] = $this->
transformToSQL(
'ts.town', $arrayquery[
'cust_city']);
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.
")))";
815 if (!empty($arrayquery[
'cust_status']) && count($arrayquery[
'cust_status']) > 0) {
816 $sqlwhere[] =
" (ts.status IN (".$this->db->sanitize(implode(
',', $arrayquery[
'cust_status'])).
"))";
818 if (!empty($arrayquery[
'cust_typecust']) && count($arrayquery[
'cust_typecust']) > 0) {
819 $sqlwhere[] =
" (ts.client IN (".$this->db->sanitize(implode(
',', $arrayquery[
'cust_typecust'])).
"))";
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'])).
"))";
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'])).
"'))";
827 if (!empty($arrayquery[
'cust_typeent']) && count($arrayquery[
'cust_typeent']) > 0) {
828 $sqlwhere[] =
" (ts.fk_typent IN (".$this->db->sanitize(implode(
',', $arrayquery[
'cust_typeent'])).
"))";
830 if (!empty($arrayquery[
'cust_saleman']) && count($arrayquery[
'cust_saleman']) > 0) {
831 $sqlwhere[] =
" (saleman.fk_user IN (".$this->db->sanitize(implode(
',', $arrayquery[
'cust_saleman'])).
"))";
833 if (!empty($arrayquery[
'cust_country']) && count($arrayquery[
'cust_country']) > 0) {
834 $sqlwhere[] =
" (ts.fk_pays IN (".$this->db->sanitize(implode(
',', $arrayquery[
'cust_country'])).
"))";
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'])).
"))";
839 if (!empty($arrayquery[
'cust_categ']) && count($arrayquery[
'cust_categ']) > 0) {
840 $sqlwhere[] =
" (custcateg.fk_categorie IN (".$this->db->sanitize(implode(
',', $arrayquery[
'cust_categ'])).
"))";
842 if (!empty($arrayquery[
'cust_language']) && count($arrayquery[
'cust_language']) > 0) {
843 $sqlwhere[] =
" (ts.default_lang IN ('".$this->db->sanitize(implode(
"','", $arrayquery[
'cust_language'])).
"'))";
848 $socstatic =
new Societe($this->db);
849 $elementtype = $socstatic->table_element;
855 $extrafields->fetch_name_optionals_label($elementtype);
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]).
"')";
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"]).
")";
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"]).
"')";
873 } elseif ($extrafields->attributes[$elementtype][
'type'][$key] ==
'boolean') {
874 if ($arrayquery[
'options_'.$key] !=
'') {
875 $sqlwhere[] =
" (tse.".$key.
" = ".((int) $arrayquery[
"options_".$key]).
")";
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]).
"')";
888 if (count($sqlwhere) > 0) {
889 $sql .=
" WHERE ".implode(
" AND ", $sqlwhere);
893 dol_syslog(get_class($this).
"::query_contact", LOG_DEBUG);
894 $resql = $this->db->query($sql);
896 $this->contact_lines = array();
897 $num = $this->db->num_rows($resql);
902 $obj = $this->db->fetch_object($resql);
904 $this->contact_lines[$i] = $obj->rowid;
909 $this->db->free($resql);
913 $this->error =
"Error ".$this->db->lasterror();
914 dol_syslog(get_class($this).
"::query_contact ".$this->error, LOG_ERR);