dolibarr  7.0.0-beta
thirdparties.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@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 
20 
25 {
26  var $name='ThirdPartiesByCategories';
27  // This label is used if no translation is found for key XXX neither MailingModuleDescXXX where XXX=name is found
28  var $desc="Third parties (by categories)";
29  var $require_admin=0;
30 
31  var $require_module=array("societe"); // This module allows to select by categories must be also enabled if category module is not activated
32  var $picto='company';
33  var $db;
34 
35 
41  function __construct($db)
42  {
43  global $conf;
44 
45  $this->db=$db;
46  }
47 
48 
56  function add_to_target($mailing_id,$filtersarray=array())
57  {
58  global $conf, $langs;
59 
60  $cibles = array();
61 
62  // Select the third parties from category
63  if (empty($_POST['filter']))
64  {
65  $sql = "SELECT s.rowid as id, s.email as email, s.nom as name, null as fk_contact, null as firstname, null as label";
66  $sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
67  $sql.= " WHERE s.email <> ''";
68  $sql.= " AND s.entity IN (".getEntity('societe').")";
69  $sql.= " AND s.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".$mailing_id.")";
70  }
71  else
72  {
73  $sql = "SELECT s.rowid as id, s.email as email, s.nom as name, null as fk_contact, null as firstname, c.label as label";
74  $sql.= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."categorie_societe as cs, ".MAIN_DB_PREFIX."categorie as c";
75  $sql.= " WHERE s.email <> ''";
76  $sql.= " AND s.entity IN (".getEntity('societe').")";
77  $sql.= " AND s.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".$mailing_id.")";
78  $sql.= " AND cs.fk_soc = s.rowid";
79  $sql.= " AND c.rowid = cs.fk_categorie";
80  $sql.= " AND c.rowid='".$this->db->escape($_POST['filter'])."'";
81  $sql.= " UNION ";
82  $sql.= "SELECT s.rowid as id, s.email as email, s.nom as name, null as fk_contact, null as firstname, c.label as label";
83  $sql.= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."categorie_fournisseur as cs, ".MAIN_DB_PREFIX."categorie as c";
84  $sql.= " WHERE s.email <> ''";
85  $sql.= " AND s.entity IN (".getEntity('societe').")";
86  $sql.= " AND s.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".$mailing_id.")";
87  $sql.= " AND cs.fk_soc = s.rowid";
88  $sql.= " AND c.rowid = cs.fk_categorie";
89  $sql.= " AND c.rowid='".$this->db->escape($_POST['filter'])."'";
90  }
91  $sql.= " ORDER BY email";
92 
93  // Stock recipients emails into targets table
94  $result=$this->db->query($sql);
95  if ($result)
96  {
97  $num = $this->db->num_rows($result);
98  $i = 0;
99  $j = 0;
100 
101  dol_syslog(get_class($this)."::add_to_target mailing ".$num." targets found");
102 
103  $old = '';
104  while ($i < $num)
105  {
106  $obj = $this->db->fetch_object($result);
107  if ($old <> $obj->email)
108  {
109  $cibles[$j] = array(
110  'email' => $obj->email,
111  'fk_contact' => $obj->fk_contact,
112  'lastname' => $obj->name, // For a thirdparty, we must use name
113  'firstname' => '', // For a thirdparty, lastname is ''
114  'other' => ($obj->label?$langs->transnoentities("Category").'='.$obj->label:''),
115  'source_url' => $this->url($obj->id),
116  'source_id' => $obj->id,
117  'source_type' => 'thirdparty'
118  );
119  $old = $obj->email;
120  $j++;
121  }
122 
123  $i++;
124  }
125  }
126  else
127  {
128  dol_syslog($this->db->error());
129  $this->error=$this->db->error();
130  return -1;
131  }
132 
133  return parent::add_to_target($mailing_id, $cibles);
134  }
135 
136 
146  {
147  // CHANGE THIS: Optionnal
148 
149  //var $statssql=array();
150  //$this->statssql[0]="SELECT field1 as label, count(distinct(email)) as nb FROM mytable WHERE email IS NOT NULL";
151  return array();
152  }
153 
154 
163  function getNbOfRecipients($sql='')
164  {
165  global $conf;
166 
167  $sql = "SELECT count(distinct(s.email)) as nb";
168  $sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
169  $sql.= " WHERE s.email != ''";
170  $sql.= " AND s.entity IN (".getEntity('societe').")";
171 
172  // La requete doit retourner un champ "nb" pour etre comprise
173  // par parent::getNbOfRecipients
174  return parent::getNbOfRecipients($sql);
175  }
176 
183  function formFilter()
184  {
185  global $conf, $langs;
186 
187  $langs->load("companies");
188 
189  $s='';
190  $s.='<select name="filter" class="flat">';
191 
192  // Show categories
193  $sql = "SELECT rowid, label, type, visible";
194  $sql.= " FROM ".MAIN_DB_PREFIX."categorie";
195  $sql.= " WHERE type in (1,2)"; // We keep only categories for suppliers and customers/prospects
196  // $sql.= " AND visible > 0"; // We ignore the property visible because third party's categories does not use this property (only products categories use it).
197  $sql.= " AND entity = ".$conf->entity;
198  $sql.= " ORDER BY label";
199 
200  //print $sql;
201  $resql = $this->db->query($sql);
202  if ($resql)
203  {
204  $num = $this->db->num_rows($resql);
205 
206  if (empty($conf->categorie->enabled)) $num=0; // Force empty list if category module is not enabled
207 
208  if ($num) $s.='<option value="0">&nbsp;</option>';
209  else $s.='<option value="0">'.$langs->trans("ContactsAllShort").'</option>';
210 
211  $i = 0;
212  while ($i < $num)
213  {
214  $obj = $this->db->fetch_object($resql);
215 
216  $type='';
217  if ($obj->type == 1) $type=$langs->trans("Supplier");
218  if ($obj->type == 2) $type=$langs->trans("Customer");
219  $s.='<option value="'.$obj->rowid.'">'.dol_trunc($obj->label,38,'middle');
220  if ($type) $s.=' ('.$type.')';
221  $s.='</option>';
222  $i++;
223  }
224  }
225  else
226  {
227  dol_print_error($this->db);
228  }
229 
230  $s.='</select>';
231  return $s;
232 
233  }
234 
235 
242  function url($id)
243  {
244  return '<a href="'.DOL_URL_ROOT.'/societe/card.php?socid='.$id.'">'.img_object('',"company").'</a>';
245  }
246 
247 }
248 
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.
__construct($db)
Constructor.
getNbOfRecipients($sql='')
Return here number of distinct emails returned by your selector.
Class to manage a list of personalised recipients for mailing feature.
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
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.
img_object($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
url($id)
Can include an URL link on each record provided by selector shown on target page. ...
add_to_target($mailing_id, $filtersarray=array())
This is the main function that returns the array of emails.
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.