dolibarr  9.0.0
advthirdparties.modules.php
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  var $name='ThirdPartyAdvancedTargeting';
29  // This label is used if no translation is found for key XXX neither MailingModuleDescXXX where XXX=name is found
30  var $desc="Third parties";
31  var $require_admin=0;
32 
33  var $require_module=array("none"); // This module should not be displayed as Selector in mailling
34  var $picto='company';
35 
39  public $db;
40 
41 
47  function __construct($db)
48  {
49  $this->db=$db;
50  }
51 
52 
53  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
63  function add_to_target_spec($mailing_id,$socid,$type_of_target, $contactid)
64  {
65  // phpcs:enable
66  global $conf, $langs;
67 
68  dol_syslog(get_class($this)."::add_to_target socid=".var_export($socid,true).' contactid='.var_export($contactid,true));
69 
70  $cibles = array();
71 
72  if (($type_of_target==1) || ($type_of_target==3)) {
73  // Select the third parties from category
74  if (count($socid)>0)
75  {
76  $sql= "SELECT s.rowid as id, s.email as email, s.nom as name, null as fk_contact";
77  $sql.= " FROM ".MAIN_DB_PREFIX."societe as s LEFT OUTER JOIN ".MAIN_DB_PREFIX."societe_extrafields se ON se.fk_object=s.rowid";
78  $sql.= " WHERE s.entity IN (".getEntity('societe').")";
79  $sql.= " AND s.rowid IN (".implode(',',$socid).")";
80  $sql.= " ORDER BY email";
81 
82  // Stock recipients emails into targets table
83  $result=$this->db->query($sql);
84  if ($result)
85  {
86  $num = $this->db->num_rows($result);
87  $i = 0;
88 
89  dol_syslog(get_class($this)."::add_to_target mailing ".$num." targets found", LOG_DEBUG);
90 
91  $old = '';
92  while ($i < $num)
93  {
94  $obj = $this->db->fetch_object($result);
95 
96  if (!empty($obj->email) && filter_var($obj->email, FILTER_VALIDATE_EMAIL)) {
97  if (!array_key_exists($obj->email, $cibles)) {
98  $cibles[$obj->email] = array(
99  'email' => $obj->email,
100  'fk_contact' => $obj->fk_contact,
101  'name' => $obj->name,
102  'firstname' => $obj->firstname,
103  'other' => '',
104  'source_url' => $this->url($obj->id,'thirdparty'),
105  'source_id' => $obj->id,
106  'source_type' => 'thirdparty'
107  );
108  }
109  }
110 
111  $i++;
112  }
113  }
114  else
115  {
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  {
127  $sql= "SELECT socp.rowid as id, socp.email as email, socp.lastname as lastname, socp.firstname as firstname";
128  $sql.= " FROM ".MAIN_DB_PREFIX."socpeople as socp";
129  $sql.= " WHERE socp.entity IN (".getEntity('socpeople').")";
130  if (count($contactid)>0) {
131  $sql.= " AND socp.rowid IN (".implode(',',$contactid).")";
132  }
133  if (count($socid)>0) {
134  $sql.= " AND socp.fk_soc IN (".implode(',',$socid).")";
135  }
136  $sql.= " ORDER BY email";
137 
138  // Stock recipients emails into targets table
139  $result=$this->db->query($sql);
140  if ($result)
141  {
142  $num = $this->db->num_rows($result);
143  $i = 0;
144 
145  dol_syslog(get_class($this)."::add_to_target mailing ".$num." targets found");
146 
147  $old = '';
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  }
170  else
171  {
172  dol_syslog($this->db->error());
173  $this->error=$this->db->error();
174  return -1;
175  }
176  }
177  }
178 
179 
180  dol_syslog(get_class($this)."::add_to_target mailing cibles=".var_export($cibles,true), LOG_DEBUG);
181  return parent::add_to_target($mailing_id, $cibles);
182  }
183 
184 
194  {
195  // CHANGE THIS: Optionnal
196 
197  //var $statssql=array();
198  //$this->statssql[0]="SELECT field1 as label, count(distinct(email)) as nb FROM mytable WHERE email IS NOT NULL";
199  return array();
200  }
201 
202 
211  function getNbOfRecipients($sql='')
212  {
213  global $conf;
214 
215  $sql = "SELECT count(distinct(s.email)) as nb";
216  $sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
217  $sql.= " WHERE s.email != ''";
218  $sql.= " AND s.entity IN (".getEntity('societe').")";
219 
220  // La requete doit retourner un champ "nb" pour etre comprise
221  // par parent::getNbOfRecipients
222  return parent::getNbOfRecipients($sql);
223  }
224 
231  function formFilter()
232  {
233  global $conf, $langs;
234 
235  $langs->load("companies");
236 
237  $s='';
238  $s.='<select name="filter" class="flat">';
239 
240  // Show categories
241  $sql = "SELECT rowid, label, type, visible";
242  $sql.= " FROM ".MAIN_DB_PREFIX."categorie";
243  $sql.= " WHERE type in (1,2)"; // We keep only categories for suppliers and customers/prospects
244  // $sql.= " AND visible > 0"; // We ignore the property visible because third party's categories does not use this property (only products categories use it).
245  $sql.= " AND entity = ".$conf->entity;
246  $sql.= " ORDER BY label";
247 
248  //print $sql;
249  $resql = $this->db->query($sql);
250  if ($resql)
251  {
252  $num = $this->db->num_rows($resql);
253 
254  if (empty($conf->categorie->enabled)) $num=0; // Force empty list if category module is not enabled
255 
256  if ($num) $s.='<option value="0">&nbsp;</option>';
257  else $s.='<option value="0">'.$langs->trans("ContactsAllShort").'</option>';
258 
259  $i = 0;
260  while ($i < $num)
261  {
262  $obj = $this->db->fetch_object($resql);
263 
264  $type='';
265  if ($obj->type == 1) $type=$langs->trans("Supplier");
266  if ($obj->type == 2) $type=$langs->trans("Customer");
267  $s.='<option value="'.$obj->rowid.'">'.dol_trunc($obj->label,38,'middle');
268  if ($type) $s.=' ('.$type.')';
269  $s.='</option>';
270  $i++;
271  }
272  }
273  else
274  {
275  dol_print_error($this->db);
276  }
277 
278  $s.='</select>';
279  return $s;
280  }
281 
282 
290  function url($id,$type)
291  {
292  if ($type=='thirdparty') {
293  $companystatic=new Societe($this->db);
294  $companystatic->fetch($id);
295  return $companystatic->getNomUrl(0, '', 0, 1);
296  } elseif ($type=='contact') {
297  $contactstatic=new Contact($this->db);
298  $contactstatic->fetch($id);
299  return $contactstatic->getNomUrl(0, '', 0, '', -1, 1);
300  }
301  }
302 }
getSqlArrayForStats()
On the main mailing area, there is a box with statistics.
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.
if(! empty($conf->facture->enabled) && $user->rights->facture->lire) if(! empty($conf->fournisseur->enabled) && $user->rights->fournisseur->facture->lire) if(! empty($conf->don->enabled) && $user->rights->societe->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) && $user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1053
Class to manage a list of personalised recipients for mailing feature.
Class to manage contact/addresses.
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
url($id, $type)
Can include an URL link on each record provided by selector shown on target page. ...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
Class to manage third parties objects (customers, suppliers, prospects...)
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.
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 "...