dolibarr  13.0.2
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 
51  public function __construct($db)
52  {
53  $this->db = $db;
54  }
55 
56 
57  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
67  public function add_to_target_spec($mailing_id, $socid, $type_of_target, $contactid)
68  {
69  // phpcs:enable
70  global $conf, $langs;
71 
72  dol_syslog(get_class($this)."::add_to_target_spec socid=".var_export($socid, true).' contactid='.var_export($contactid, true));
73 
74  $cibles = array();
75 
76  if (($type_of_target == 1) || ($type_of_target == 3)) {
77  // Select the third parties from category
78  if (count($socid) > 0)
79  {
80  $sql = "SELECT s.rowid as id, s.email as email, s.nom as name, null as fk_contact";
81  $sql .= " FROM ".MAIN_DB_PREFIX."societe as s LEFT OUTER JOIN ".MAIN_DB_PREFIX."societe_extrafields se ON se.fk_object=s.rowid";
82  $sql .= " WHERE s.entity IN (".getEntity('societe').")";
83  $sql .= " AND s.rowid IN (".implode(',', $socid).")";
84  $sql .= " ORDER BY email";
85 
86  // Stock recipients emails into targets table
87  $result = $this->db->query($sql);
88  if ($result)
89  {
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  {
97  $obj = $this->db->fetch_object($result);
98 
99  if (!empty($obj->email) && filter_var($obj->email, FILTER_VALIDATE_EMAIL)) {
100  if (!array_key_exists($obj->email, $cibles)) {
101  $cibles[$obj->email] = array(
102  'email' => $obj->email,
103  'fk_contact' => $obj->fk_contact,
104  'name' => $obj->name,
105  'firstname' => $obj->firstname,
106  'other' => '',
107  'source_url' => $this->url($obj->id, 'thirdparty'),
108  'source_id' => $obj->id,
109  'source_type' => 'thirdparty'
110  );
111  }
112  }
113 
114  $i++;
115  }
116  } else {
117  dol_syslog($this->db->error());
118  $this->error = $this->db->error();
119  return -1;
120  }
121  }
122  }
123 
124  if (($type_of_target == 1) || ($type_of_target == 2) || ($type_of_target == 4)) {
125  // Select the third parties from category
126  if (count($socid) > 0 || count($contactid) > 0)
127  {
128  $sql = "SELECT socp.rowid as id, socp.email as email, socp.lastname as lastname, socp.firstname as firstname";
129  $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as socp";
130  $sql .= " WHERE socp.entity IN (".getEntity('socpeople').")";
131  if (count($contactid) > 0) {
132  $sql .= " AND socp.rowid IN (".implode(',', $contactid).")";
133  }
134  if (count($socid) > 0) {
135  $sql .= " AND socp.fk_soc IN (".implode(',', $socid).")";
136  }
137  $sql .= " ORDER BY email";
138 
139  // Stock recipients emails into targets table
140  $result = $this->db->query($sql);
141  if ($result)
142  {
143  $num = $this->db->num_rows($result);
144  $i = 0;
145 
146  dol_syslog(get_class($this)."::add_to_target_spec mailing ".$num." targets found");
147 
148  while ($i < $num)
149  {
150  $obj = $this->db->fetch_object($result);
151 
152  if (!empty($obj->email) && filter_var($obj->email, FILTER_VALIDATE_EMAIL)) {
153  if (!array_key_exists($obj->email, $cibles)) {
154  $cibles[$obj->email] = array(
155  'email' => $obj->email,
156  'fk_contact' =>$obj->id,
157  'lastname' => $obj->lastname,
158  'firstname' => $obj->firstname,
159  'other' => '',
160  'source_url' => $this->url($obj->id, 'contact'),
161  'source_id' => $obj->id,
162  'source_type' => 'contact'
163  );
164  }
165  }
166 
167  $i++;
168  }
169  } else {
170  dol_syslog($this->db->error());
171  $this->error = $this->db->error();
172  return -1;
173  }
174  }
175  }
176 
177 
178  dol_syslog(get_class($this)."::add_to_target_spec mailing cibles=".var_export($cibles, true), LOG_DEBUG);
179 
180  return parent::addTargetsToDatabase($mailing_id, $cibles);
181  }
182 
183 
192  public function getSqlArrayForStats()
193  {
194  // CHANGE THIS: Optionnal
195 
196  //var $statssql=array();
197  //$this->statssql[0]="SELECT field1 as label, count(distinct(email)) as nb FROM mytable WHERE email IS NOT NULL";
198  return array();
199  }
200 
201 
210  public function getNbOfRecipients($sql = '')
211  {
212  global $conf;
213 
214  $sql = "SELECT count(distinct(s.email)) as nb";
215  $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
216  $sql .= " WHERE s.email != ''";
217  $sql .= " AND s.entity IN (".getEntity('societe').")";
218 
219  // La requete doit retourner un champ "nb" pour etre comprise
220  // par parent::getNbOfRecipients
221  return parent::getNbOfRecipients($sql);
222  }
223 
230  public function formFilter()
231  {
232  global $conf, $langs;
233 
234  $langs->load("companies");
235 
236  $s = '';
237  $s .= '<select name="filter" class="flat">';
238 
239  // Show categories
240  $sql = "SELECT rowid, label, type, visible";
241  $sql .= " FROM ".MAIN_DB_PREFIX."categorie";
242  $sql .= " WHERE type in (1,2)"; // We keep only categories for suppliers and customers/prospects
243  // $sql.= " AND visible > 0"; // We ignore the property visible because third party's categories does not use this property (only products categories use it).
244  $sql .= " AND entity = ".$conf->entity;
245  $sql .= " ORDER BY label";
246 
247  //print $sql;
248  $resql = $this->db->query($sql);
249  if ($resql)
250  {
251  $num = $this->db->num_rows($resql);
252 
253  if (empty($conf->categorie->enabled)) $num = 0; // Force empty list if category module is not enabled
254 
255  if ($num) $s .= '<option value="0">&nbsp;</option>';
256  else $s .= '<option value="0">'.$langs->trans("ContactsAllShort").'</option>';
257 
258  $i = 0;
259  while ($i < $num)
260  {
261  $obj = $this->db->fetch_object($resql);
262 
263  $type = '';
264  if ($obj->type == 1) $type = $langs->trans("Supplier");
265  if ($obj->type == 2) $type = $langs->trans("Customer");
266  $s .= '<option value="'.$obj->rowid.'">'.dol_trunc($obj->label, 38, 'middle');
267  if ($type) $s .= ' ('.$type.')';
268  $s .= '</option>';
269  $i++;
270  }
271  } else {
272  dol_print_error($this->db);
273  }
274 
275  $s .= '</select>';
276  return $s;
277  }
278 
279 
287  public function url($id, $type)
288  {
289  if ($type == 'thirdparty') {
290  $companystatic = new Societe($this->db);
291  $companystatic->fetch($id);
292  return $companystatic->getNomUrl(0, '', 0, 1);
293  } elseif ($type == 'contact') {
294  $contactstatic = new Contact($this->db);
295  $contactstatic->fetch($id);
296  return $contactstatic->getNomUrl(0, '', 0, '', -1, 1);
297  }
298  }
299 }
getSqlArrayForStats()
On the main mailing area, there is a box with statistics.
Class to manage a list of personalised recipients for mailing feature.
Class to manage contact/addresses.
$conf db
API class for accounts.
Definition: inc.php:54
url($id, $type)
Can include an URL link on each record provided by selector shown on target page. ...
Class to manage third parties objects (customers, suppliers, prospects...)
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
add_to_target_spec($mailing_id, $socid, $type_of_target, $contactid)
This is the main function that returns the array of emails.
getNbOfRecipients($sql= '')
Return here number of distinct emails returned by your selector.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->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)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
dol_print_error($db= '', $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 &#39;...&#39; if string larger than length.
Parent class of emailing target selectors modules.
formFilter()
This is to add a form filter to provide variant of selector If used, the HTML select must be called &quot;...