20include_once DOL_DOCUMENT_ROOT.
'/core/modules/mailings/modules_mailings.php';
21include_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
22include_once DOL_DOCUMENT_ROOT.
'/contact/class/contact.class.php';
33 public $name =
'ThirdPartyAdvancedTargeting';
38 public $desc =
"Third parties";
43 public $require_admin = 0;
48 public $require_module = array(
"none");
53 public $picto =
'company';
58 public $enabled =
'isModEnabled("societe")';
87 dol_syslog(get_class($this).
"::add_to_target_spec socid=".var_export($socid,
true).
' contactid='.var_export($contactid,
true));
91 if (($type_of_target == 1) || ($type_of_target == 3)) {
93 if (count($socid) > 0) {
94 $sql =
"SELECT s.rowid as id, s.email as email, s.nom as name, null as fk_contact";
95 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe as s LEFT OUTER JOIN ".MAIN_DB_PREFIX.
"societe_extrafields se ON se.fk_object=s.rowid";
96 $sql .=
" WHERE s.entity IN (".getEntity(
'societe').
")";
97 $sql .=
" AND s.rowid IN (".$this->db->sanitize(implode(
',', $socid)).
")";
98 if (empty($this->evenunsubscribe)) {
99 $sql .=
" AND NOT EXISTS (SELECT rowid FROM ".MAIN_DB_PREFIX.
"mailing_unsubscribe as mu WHERE mu.email = s.email and mu.entity = ".((int)
$conf->entity).
")";
101 $sql .=
" ORDER BY email";
104 $result = $this->db->query($sql);
106 $num = $this->db->num_rows($result);
109 dol_syslog(get_class($this).
"::add_to_target_spec mailing ".$num.
" targets found", LOG_DEBUG);
112 $obj = $this->db->fetch_object($result);
114 if (!empty($obj->email) && filter_var($obj->email, FILTER_VALIDATE_EMAIL)) {
115 if (!array_key_exists($obj->email, $cibles)) {
116 $cibles[$obj->email] = array(
117 'email' => $obj->email,
118 'fk_contact' => (
int) $obj->fk_contact,
119 'name' => $obj->name,
120 'firstname' => $obj->firstname,
122 'source_url' => $this->url($obj->id,
'thirdparty'),
123 'source_id' => (
int) $obj->id,
124 'source_type' =>
'thirdparty'
133 $this->error = $this->db->error();
139 if (($type_of_target == 1) || ($type_of_target == 2) || ($type_of_target == 4)) {
141 if (count($socid) > 0 || count($contactid) > 0) {
142 $sql =
"SELECT socp.rowid as id, socp.email as email, socp.lastname as lastname, socp.firstname as firstname";
143 $sql .=
" FROM ".MAIN_DB_PREFIX.
"socpeople as socp";
144 $sql .=
" WHERE socp.entity IN (".getEntity(
'contact').
")";
145 if (count($contactid) > 0) {
146 $sql .=
" AND socp.rowid IN (".$this->db->sanitize(implode(
',', $contactid)).
")";
148 if (count($socid) > 0) {
149 $sql .=
" AND socp.fk_soc IN (".$this->db->sanitize(implode(
',', $socid)).
")";
151 if (empty($this->evenunsubscribe)) {
152 $sql .=
" AND NOT EXISTS (SELECT rowid FROM ".MAIN_DB_PREFIX.
"mailing_unsubscribe as mu WHERE mu.email = socp.email and mu.entity = ".((int)
$conf->entity).
")";
154 $sql .=
" ORDER BY email";
157 $result = $this->db->query($sql);
159 $num = $this->db->num_rows($result);
162 dol_syslog(get_class($this).
"::add_to_target_spec mailing ".$num.
" targets found");
165 $obj = $this->db->fetch_object($result);
167 if (!empty($obj->email) && filter_var($obj->email, FILTER_VALIDATE_EMAIL)) {
168 if (!array_key_exists($obj->email, $cibles)) {
169 $cibles[$obj->email] = array(
170 'email' => $obj->email,
171 'fk_contact' => (
int) $obj->id,
172 'lastname' => $obj->lastname,
173 'firstname' => $obj->firstname,
175 'source_url' => $this->url($obj->id,
'contact'),
176 'source_id' => (
int) $obj->id,
177 'source_type' =>
'contact'
186 $this->error = $this->db->error();
193 dol_syslog(get_class($this).
"::add_to_target_spec mailing cibles=".var_export($cibles,
true), LOG_DEBUG);
195 return parent::addTargetsToDatabase($mailing_id, $cibles);
229 $sql =
"SELECT count(distinct(s.email)) as nb";
230 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe as s";
231 $sql .=
" WHERE s.email != ''";
232 $sql .=
" AND s.entity IN (".getEntity(
'societe').
")";
233 if (empty($this->evenunsubscribe)) {
234 $sql .=
" AND NOT EXISTS (SELECT rowid FROM ".MAIN_DB_PREFIX.
"mailing_unsubscribe as mu WHERE mu.email = s.email and mu.entity = ".((int)
$conf->entity).
")";
238 return parent::getNbOfRecipients($sql);
249 global
$conf, $langs;
251 $langs->load(
"companies");
254 $s .=
'<select name="filter" class="flat">';
257 $sql =
"SELECT rowid, label, type, visible";
258 $sql .=
" FROM ".MAIN_DB_PREFIX.
"categorie";
259 $sql .=
" WHERE type in (1,2)";
261 $sql .=
" AND entity = ".$conf->entity;
262 $sql .=
" ORDER BY label";
265 $resql = $this->db->query($sql);
267 $num = $this->db->num_rows($resql);
269 if (!isModEnabled(
"category")) {
274 $s .=
'<option value="0"> </option>';
276 $s .=
'<option value="0">'.$langs->trans(
"ContactsAllShort").
'</option>';
281 $obj = $this->db->fetch_object($resql);
284 if ($obj->type == 1) {
285 $type = $langs->trans(
"Supplier");
287 if ($obj->type == 2) {
288 $type = $langs->trans(
"Customer");
290 $s .=
'<option value="'.$obj->rowid.
'">'.
dol_trunc($obj->label, 38,
'middle');
292 $s .=
' ('.$type.
')';
315 if ($type ==
'thirdparty') {
316 $companystatic =
new Societe($this->db);
317 $companystatic->fetch(
$id);
318 return $companystatic->getNomUrl(0,
'', 0, 1);
319 } elseif ($type ==
'contact') {
320 $contactstatic =
new Contact($this->db);
321 $contactstatic->fetch(
$id);
322 return $contactstatic->getNomUrl(0,
'', 0,
'', -1, 1);
Parent class of emailing target selectors modules.
Class to manage third parties objects (customers, suppliers, prospects...)
Class to manage a list of personalised recipients for mailing feature.
url($id, $type)
Can include an URL link on each record provided by selector shown on target page.
formFilter()
This is to add a form filter to provide variant of selector If used, the HTML select must be called "...
getSqlArrayForStats()
On the main mailing area, there is a box with statistics.
add_to_target_spec($mailing_id, $socid, $type_of_target, $contactid)
This is the main function that returns the array of emails.
__construct($db)
Constructor.
getNbOfRecipients($sql='')
Return here number of distinct emails returned by your selector.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding '…' if string larger than length.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...