dolibarr  16.0.5
advthirdparties.modules.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2005-2010 Laurent Destailleur <eldy@users.sourceforge.net>
3  * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@inodbox.com>
4 *
5 * This file is an example to follow to add your own email selector inside
6 * the Dolibarr email tool.
7 * Follow instructions given in README file to know what to change to build
8 * your own emailing list selector.
9 * Code that need to be changed in this file are marked by "CHANGE THIS" tag.
10 */
11 
18 include_once DOL_DOCUMENT_ROOT.'/core/modules/mailings/modules_mailings.php';
19 include_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
20 include_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
21 
22 
27 {
28  public $name = 'ThirdPartyAdvancedTargeting';
29  // This label is used if no translation is found for key XXX neither MailingModuleDescXXX where XXX=name is found
30  public $desc = "Third parties";
31  public $require_admin = 0;
32 
33  public $require_module = array("none"); // This module should not be displayed as Selector in mailling
34 
38  public $picto = 'company';
39 
43  public $db;
44 
45  public $enabled = '$conf->societe->enabled';
46 
47 
53  public function __construct($db)
54  {
55  $this->db = $db;
56  }
57 
58 
59  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
69  public function add_to_target_spec($mailing_id, $socid, $type_of_target, $contactid)
70  {
71  // phpcs:enable
72  global $conf, $langs;
73 
74  dol_syslog(get_class($this)."::add_to_target_spec socid=".var_export($socid, true).' contactid='.var_export($contactid, true));
75 
76  $cibles = array();
77 
78  if (($type_of_target == 1) || ($type_of_target == 3)) {
79  // Select the third parties from category
80  if (count($socid) > 0) {
81  $sql = "SELECT s.rowid as id, s.email as email, s.nom as name, null as fk_contact";
82  $sql .= " FROM ".MAIN_DB_PREFIX."societe as s LEFT OUTER JOIN ".MAIN_DB_PREFIX."societe_extrafields se ON se.fk_object=s.rowid";
83  $sql .= " WHERE s.entity IN (".getEntity('societe').")";
84  $sql .= " AND s.rowid IN (".$this->db->sanitize(implode(',', $socid)).")";
85  $sql .= " ORDER BY email";
86 
87  // Stock recipients emails into targets table
88  $result = $this->db->query($sql);
89  if ($result) {
90  $num = $this->db->num_rows($result);
91  $i = 0;
92 
93  dol_syslog(get_class($this)."::add_to_target_spec mailing ".$num." targets found", LOG_DEBUG);
94 
95  while ($i < $num) {
96  $obj = $this->db->fetch_object($result);
97 
98  if (!empty($obj->email) && filter_var($obj->email, FILTER_VALIDATE_EMAIL)) {
99  if (!array_key_exists($obj->email, $cibles)) {
100  $cibles[$obj->email] = array(
101  'email' => $obj->email,
102  'fk_contact' => $obj->fk_contact,
103  'name' => $obj->name,
104  'firstname' => $obj->firstname,
105  'other' => '',
106  'source_url' => $this->url($obj->id, 'thirdparty'),
107  'source_id' => $obj->id,
108  'source_type' => 'thirdparty'
109  );
110  }
111  }
112 
113  $i++;
114  }
115  } else {
116  dol_syslog($this->db->error());
117  $this->error = $this->db->error();
118  return -1;
119  }
120  }
121  }
122 
123  if (($type_of_target == 1) || ($type_of_target == 2) || ($type_of_target == 4)) {
124  // Select the third parties from category
125  if (count($socid) > 0 || count($contactid) > 0) {
126  $sql = "SELECT socp.rowid as id, socp.email as email, socp.lastname as lastname, socp.firstname as firstname";
127  $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as socp";
128  $sql .= " WHERE socp.entity IN (".getEntity('contact').")";
129  if (count($contactid) > 0) {
130  $sql .= " AND socp.rowid IN (".$this->db->sanitize(implode(',', $contactid)).")";
131  }
132  if (count($socid) > 0) {
133  $sql .= " AND socp.fk_soc IN (".$this->db->sanitize(implode(',', $socid)).")";
134  }
135  $sql .= " ORDER BY email";
136 
137  // Stock recipients emails into targets table
138  $result = $this->db->query($sql);
139  if ($result) {
140  $num = $this->db->num_rows($result);
141  $i = 0;
142 
143  dol_syslog(get_class($this)."::add_to_target_spec mailing ".$num." targets found");
144 
145  while ($i < $num) {
146  $obj = $this->db->fetch_object($result);
147 
148  if (!empty($obj->email) && filter_var($obj->email, FILTER_VALIDATE_EMAIL)) {
149  if (!array_key_exists($obj->email, $cibles)) {
150  $cibles[$obj->email] = array(
151  'email' => $obj->email,
152  'fk_contact' =>$obj->id,
153  'lastname' => $obj->lastname,
154  'firstname' => $obj->firstname,
155  'other' => '',
156  'source_url' => $this->url($obj->id, 'contact'),
157  'source_id' => $obj->id,
158  'source_type' => 'contact'
159  );
160  }
161  }
162 
163  $i++;
164  }
165  } else {
166  dol_syslog($this->db->error());
167  $this->error = $this->db->error();
168  return -1;
169  }
170  }
171  }
172 
173 
174  dol_syslog(get_class($this)."::add_to_target_spec mailing cibles=".var_export($cibles, true), LOG_DEBUG);
175 
176  return parent::addTargetsToDatabase($mailing_id, $cibles);
177  }
178 
179 
188  public function getSqlArrayForStats()
189  {
190  // CHANGE THIS: Optionnal
191 
192  //var $statssql=array();
193  //$this->statssql[0]="SELECT field1 as label, count(distinct(email)) as nb FROM mytable WHERE email IS NOT NULL";
194  return array();
195  }
196 
197 
206  public function getNbOfRecipients($sql = '')
207  {
208  global $conf;
209 
210  $sql = "SELECT count(distinct(s.email)) as nb";
211  $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
212  $sql .= " WHERE s.email != ''";
213  $sql .= " AND s.entity IN (".getEntity('societe').")";
214 
215  // La requete doit retourner un champ "nb" pour etre comprise par parent::getNbOfRecipients
216  return parent::getNbOfRecipients($sql);
217  }
218 
225  public function formFilter()
226  {
227  global $conf, $langs;
228 
229  $langs->load("companies");
230 
231  $s = '';
232  $s .= '<select name="filter" class="flat">';
233 
234  // Show categories
235  $sql = "SELECT rowid, label, type, visible";
236  $sql .= " FROM ".MAIN_DB_PREFIX."categorie";
237  $sql .= " WHERE type in (1,2)"; // We keep only categories for suppliers and customers/prospects
238  // $sql.= " AND visible > 0"; // We ignore the property visible because third party's categories does not use this property (only products categories use it).
239  $sql .= " AND entity = ".$conf->entity;
240  $sql .= " ORDER BY label";
241 
242  //print $sql;
243  $resql = $this->db->query($sql);
244  if ($resql) {
245  $num = $this->db->num_rows($resql);
246 
247  if (empty($conf->categorie->enabled)) {
248  $num = 0; // Force empty list if category module is not enabled
249  }
250 
251  if ($num) {
252  $s .= '<option value="0">&nbsp;</option>';
253  } else {
254  $s .= '<option value="0">'.$langs->trans("ContactsAllShort").'</option>';
255  }
256 
257  $i = 0;
258  while ($i < $num) {
259  $obj = $this->db->fetch_object($resql);
260 
261  $type = '';
262  if ($obj->type == 1) {
263  $type = $langs->trans("Supplier");
264  }
265  if ($obj->type == 2) {
266  $type = $langs->trans("Customer");
267  }
268  $s .= '<option value="'.$obj->rowid.'">'.dol_trunc($obj->label, 38, 'middle');
269  if ($type) {
270  $s .= ' ('.$type.')';
271  }
272  $s .= '</option>';
273  $i++;
274  }
275  } else {
276  dol_print_error($this->db);
277  }
278 
279  $s .= '</select>';
280  return $s;
281  }
282 
283 
291  public function url($id, $type)
292  {
293  if ($type == 'thirdparty') {
294  $companystatic = new Societe($this->db);
295  $companystatic->fetch($id);
296  return $companystatic->getNomUrl(0, '', 0, 1);
297  } elseif ($type == 'contact') {
298  $contactstatic = new Contact($this->db);
299  $contactstatic->fetch($id);
300  return $contactstatic->getNomUrl(0, '', 0, '', -1, 1);
301  }
302  }
303 }
Societe
Class to manage third parties objects (customers, suppliers, prospects...)
Definition: societe.class.php:48
mailing_advthirdparties\url
url($id, $type)
Can include an URL link on each record provided by selector shown on target page.
Definition: advthirdparties.modules.php:291
db
$conf db
API class for accounts.
Definition: inc.php:41
dol_trunc
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.
Definition: functions.lib.php:3805
dol_print_error
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Definition: functions.lib.php:4844
MailingTargets
Parent class of emailing target selectors modules.
Definition: modules_mailings.php:32
mailing_advthirdparties
Class to manage a list of personalised recipients for mailing feature.
Definition: advthirdparties.modules.php:26
mailing_advthirdparties\__construct
__construct($db)
Constructor.
Definition: advthirdparties.modules.php:53
mailing_advthirdparties\getSqlArrayForStats
getSqlArrayForStats()
On the main mailing area, there is a box with statistics.
Definition: advthirdparties.modules.php:188
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
Contact
Class to manage contact/addresses.
Definition: contact.class.php:40
mailing_advthirdparties\formFilter
formFilter()
This is to add a form filter to provide variant of selector If used, the HTML select must be called "...
Definition: advthirdparties.modules.php:225
mailing_advthirdparties\add_to_target_spec
add_to_target_spec($mailing_id, $socid, $type_of_target, $contactid)
This is the main function that returns the array of emails.
Definition: advthirdparties.modules.php:69
$resql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->facture->lire)||(isModEnabled('supplier_invoice') && $user->rights->supplier_invoice->lire)) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:742
mailing_advthirdparties\getNbOfRecipients
getNbOfRecipients($sql='')
Return here number of distinct emails returned by your selector.
Definition: advthirdparties.modules.php:206