dolibarr  7.0.0-beta
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@capnetworks.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  var $db;
36 
37 
43  function __construct($db)
44  {
45  global $conf;
46 
47  $this->db=$db;
48  }
49 
50 
60  function add_to_target_spec($mailing_id,$socid,$type_of_target, $contactid)
61  {
62  global $conf, $langs;
63 
64  dol_syslog(get_class($this)."::add_to_target socid=".var_export($socid,true).' contactid='.var_export($contactid,true));
65 
66  $cibles = array();
67 
68  if (($type_of_target==1) || ($type_of_target==3)) {
69  // Select the third parties from category
70  if (count($socid)>0)
71  {
72  $sql= "SELECT s.rowid as id, s.email as email, s.nom as name, null as fk_contact";
73  $sql.= " FROM ".MAIN_DB_PREFIX."societe as s LEFT OUTER JOIN ".MAIN_DB_PREFIX."societe_extrafields se ON se.fk_object=s.rowid";
74  $sql.= " WHERE s.entity IN (".getEntity('societe').")";
75  $sql.= " AND s.rowid IN (".implode(',',$socid).")";
76  $sql.= " ORDER BY email";
77 
78  // Stock recipients emails into targets table
79  $result=$this->db->query($sql);
80  if ($result)
81  {
82  $num = $this->db->num_rows($result);
83  $i = 0;
84 
85  dol_syslog(get_class($this)."::add_to_target mailing ".$num." targets found", LOG_DEBUG);
86 
87  $old = '';
88  while ($i < $num)
89  {
90  $obj = $this->db->fetch_object($result);
91 
92  if (!empty($obj->email) && filter_var($obj->email, FILTER_VALIDATE_EMAIL)) {
93  if (!array_key_exists($obj->email, $cibles)) {
94  $cibles[$obj->email] = array(
95  'email' => $obj->email,
96  'fk_contact' => $obj->fk_contact,
97  'name' => $obj->name,
98  'firstname' => $obj->firstname,
99  'other' => '',
100  'source_url' => $this->url($obj->id,'thirdparty'),
101  'source_id' => $obj->id,
102  'source_type' => 'thirdparty'
103  );
104  }
105  }
106 
107  $i++;
108  }
109  }
110  else
111  {
112  dol_syslog($this->db->error());
113  $this->error=$this->db->error();
114  return -1;
115  }
116  }
117  }
118 
119  if (($type_of_target==1) || ($type_of_target==2) || ($type_of_target==4)) {
120  // Select the third parties from category
121  if (count($socid)>0 || count($contactid)>0)
122  {
123  $sql= "SELECT socp.rowid as id, socp.email as email, socp.lastname as lastname, socp.firstname as firstname";
124  $sql.= " FROM ".MAIN_DB_PREFIX."socpeople as socp";
125  $sql.= " WHERE socp.entity IN (".getEntity('societe').")";
126  if (count($contactid)>0) {
127  $sql.= " AND socp.rowid IN (".implode(',',$contactid).")";
128  }
129  if (count($socid)>0) {
130  $sql.= " AND socp.fk_soc IN (".implode(',',$socid).")";
131  }
132  $sql.= " ORDER BY email";
133 
134  // Stock recipients emails into targets table
135  $result=$this->db->query($sql);
136  if ($result)
137  {
138  $num = $this->db->num_rows($result);
139  $i = 0;
140 
141  dol_syslog(get_class($this)."::add_to_target mailing ".$num." targets found");
142 
143  $old = '';
144  while ($i < $num)
145  {
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  }
166  else
167  {
168  dol_syslog($this->db->error());
169  $this->error=$this->db->error();
170  return -1;
171  }
172  }
173  }
174 
175 
176  dol_syslog(get_class($this)."::add_to_target mailing cibles=".var_export($cibles,true), LOG_DEBUG);
177  return parent::add_to_target($mailing_id, $cibles);
178  }
179 
180 
190  {
191  // CHANGE THIS: Optionnal
192 
193  //var $statssql=array();
194  //$this->statssql[0]="SELECT field1 as label, count(distinct(email)) as nb FROM mytable WHERE email IS NOT NULL";
195  return array();
196  }
197 
198 
207  function getNbOfRecipients($sql='')
208  {
209  global $conf;
210 
211  $sql = "SELECT count(distinct(s.email)) as nb";
212  $sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
213  $sql.= " WHERE s.email != ''";
214  $sql.= " AND s.entity IN (".getEntity('societe').")";
215 
216  // La requete doit retourner un champ "nb" pour etre comprise
217  // par parent::getNbOfRecipients
218  return parent::getNbOfRecipients($sql);
219  }
220 
227  function formFilter()
228  {
229  global $conf, $langs;
230 
231  $langs->load("companies");
232 
233  $s='';
234  $s.='<select name="filter" class="flat">';
235 
236  // Show categories
237  $sql = "SELECT rowid, label, type, visible";
238  $sql.= " FROM ".MAIN_DB_PREFIX."categorie";
239  $sql.= " WHERE type in (1,2)"; // We keep only categories for suppliers and customers/prospects
240  // $sql.= " AND visible > 0"; // We ignore the property visible because third party's categories does not use this property (only products categories use it).
241  $sql.= " AND entity = ".$conf->entity;
242  $sql.= " ORDER BY label";
243 
244  //print $sql;
245  $resql = $this->db->query($sql);
246  if ($resql)
247  {
248  $num = $this->db->num_rows($resql);
249 
250  if (empty($conf->categorie->enabled)) $num=0; // Force empty list if category module is not enabled
251 
252  if ($num) $s.='<option value="0">&nbsp;</option>';
253  else $s.='<option value="0">'.$langs->trans("ContactsAllShort").'</option>';
254 
255  $i = 0;
256  while ($i < $num)
257  {
258  $obj = $this->db->fetch_object($resql);
259 
260  $type='';
261  if ($obj->type == 1) $type=$langs->trans("Supplier");
262  if ($obj->type == 2) $type=$langs->trans("Customer");
263  $s.='<option value="'.$obj->rowid.'">'.dol_trunc($obj->label,38,'middle');
264  if ($type) $s.=' ('.$type.')';
265  $s.='</option>';
266  $i++;
267  }
268  }
269  else
270  {
271  dol_print_error($this->db);
272  }
273 
274  $s.='</select>';
275  return $s;
276 
277  }
278 
279 
287  function url($id,$type)
288  {
289  if ($type=='thirdparty') {
290  $companystatic=new Societe($this->db);
291  $companystatic->fetch($id);
292  return $companystatic->getNomUrl(0);
293  } elseif ($type=='contact') {
294  $contactstatic=new Contact($this->db);
295  $contactstatic->fetch($id);
296  return $contactstatic->getNomUrl(0);
297  }
298  }
299 
300 }
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 '...' if string larger than length.
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.
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:1013
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 "...