dolibarr  17.0.4
partnership.modules.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2018-2018 Andre Schild <a.schild@aarboard.ch>
3  * Copyright (C) 2005-2010 Laurent Destailleur <eldy@users.sourceforge.net>
4  * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@inodbox.com>
5  *
6  * This file is an example to follow to add your own email selector inside
7  * the Dolibarr email tool.
8  * Follow instructions given in README file to know what to change to build
9  * your own emailing list selector.
10  * Code that need to be changed in this file are marked by "CHANGE THIS" tag.
11  */
12 
20 // Load Dolibarr Environment
21 include_once DOL_DOCUMENT_ROOT.'/core/modules/mailings/modules_mailings.php';
22 
23 
28 {
29  // This label is used if no translation is found for key XXX neither MailingModuleDescXXX where XXX=name is found
30  public $name = 'PartnershipThirdpartiesOrMembers';
31  public $desc = "Thirdparties or members included into a partnership program";
32 
33  public $require_admin = 0;
34 
35  public $require_module = array('partnership'); // This module allows to select by categories must be also enabled if category module is not activated
36 
40  public $picto = 'partnership';
41 
45  public $db;
46 
47  public $enabled = 'isModEnabled("partnership")';
48 
49 
55  public function __construct($db)
56  {
57  global $conf, $langs;
58  $langs->load('companies');
59 
60  $this->db = $db;
61  }
62 
63 
64  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
71  public function add_to_target($mailing_id)
72  {
73  // phpcs:enable
74  global $conf, $langs;
75 
76  $cibles = array();
77  $addDescription = '';
78 
79  $sql = "SELECT s.rowid as id, s.email as email, s.nom as name, null as fk_contact, null as firstname, pt.label as label, 'thirdparty' as source";
80  $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."partnership as p, ".MAIN_DB_PREFIX."c_partnership_type as pt";
81  $sql .= " WHERE s.email <> ''";
82  $sql .= " AND s.entity IN (".getEntity('societe').")";
83  $sql .= " AND s.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".((int) $mailing_id).")";
84  $sql .= " AND p.fk_soc = s.rowid";
85  $sql .= " AND pt.rowid = p.fk_type";
86  if (GETPOST('filter', 'int') > 0) {
87  $sql .= " AND pt.rowid=".((int) GETPOST('filter', 'int'));
88  }
89 
90  $sql .= " UNION ";
91 
92  $sql .= "SELECT s.rowid as id, s.email as email, s.lastname as name, null as fk_contact, s.firstname as firstname, pt.label as label, 'member' as source";
93  $sql .= " FROM ".MAIN_DB_PREFIX."adherent as s, ".MAIN_DB_PREFIX."partnership as p, ".MAIN_DB_PREFIX."c_partnership_type as pt";
94  $sql .= " WHERE s.email <> ''";
95  $sql .= " AND s.entity IN (".getEntity('member').")";
96  $sql .= " AND s.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".((int) $mailing_id).")";
97  $sql .= " AND p.fk_member = s.rowid";
98  $sql .= " AND pt.rowid = p.fk_type";
99  if (GETPOST('filter', 'int') > 0) {
100  $sql .= " AND pt.rowid=".((int) GETPOST('filter', 'int'));
101  }
102 
103  $sql .= " ORDER BY email";
104 
105  // Stock recipients emails into targets table
106  $result = $this->db->query($sql);
107  if ($result) {
108  $num = $this->db->num_rows($result);
109  $i = 0;
110  $j = 0;
111 
112  dol_syslog(get_class($this)."::add_to_target mailing ".$num." targets found");
113 
114  $old = '';
115  while ($i < $num) {
116  $obj = $this->db->fetch_object($result);
117  if ($old <> $obj->email) {
118  $otherTxt = ($obj->label ? $langs->transnoentities("PartnershipType").'='.$obj->label : '');
119  if (strlen($addDescription) > 0 && strlen($otherTxt) > 0) {
120  $otherTxt .= ";";
121  }
122  $otherTxt .= $addDescription;
123  $cibles[$j] = array(
124  'email' => $obj->email,
125  'fk_contact' => $obj->fk_contact,
126  'lastname' => $obj->name, // For a thirdparty, we must use name
127  'firstname' => '', // For a thirdparty, lastname is ''
128  'other' => $otherTxt,
129  'source_url' => $this->url($obj->id, $obj->source),
130  'source_id' => $obj->id,
131  'source_type' => $obj->source
132  );
133  $old = $obj->email;
134  $j++;
135  }
136 
137  $i++;
138  }
139  } else {
140  dol_syslog($this->db->error());
141  $this->error = $this->db->error();
142  return -1;
143  }
144 
145  return parent::addTargetsToDatabase($mailing_id, $cibles);
146  }
147 
148 
157  public function getSqlArrayForStats()
158  {
159  // CHANGE THIS: Optionnal
160 
161  //var $statssql=array();
162  //$this->statssql[0]="SELECT field1 as label, count(distinct(email)) as nb FROM mytable WHERE email IS NOT NULL";
163  return array();
164  }
165 
166 
175  public function getNbOfRecipients($sql = '')
176  {
177  global $conf;
178 
179  $sql = "SELECT count(distinct(s.email)) as nb";
180  $sql .= " FROM ".MAIN_DB_PREFIX."partnership as p, ".MAIN_DB_PREFIX."societe as s";
181  $sql .= " WHERE s.rowid = p.fk_soc AND s.email <> ''";
182  $sql .= " AND s.entity IN (".getEntity('societe').")";
183 
184  $sql .= " UNION ";
185 
186  $sql .= "SELECT count(distinct(s.email)) as nb";
187  $sql .= " FROM ".MAIN_DB_PREFIX."partnership as p, ".MAIN_DB_PREFIX."adherent as s";
188  $sql .= " WHERE s.rowid = p.fk_member AND s.email <> ''";
189  $sql .= " AND s.entity IN (".getEntity('member').")";
190 
191  //print $sql;
192 
193  // La requete doit retourner un champ "nb" pour etre comprise par parent::getNbOfRecipients
194  return parent::getNbOfRecipients($sql);
195  }
196 
203  public function formFilter()
204  {
205  global $conf, $langs;
206 
207  $langs->load("companies");
208 
209  $s = '<select id="filter_partnership" name="filter" class="flat">';
210 
211  // Show categories
212  $sql = "SELECT rowid, label, code, active";
213  $sql .= " FROM ".MAIN_DB_PREFIX."c_partnership_type";
214  $sql .= " WHERE active = 1";
215  $sql .= " AND entity = ".$conf->entity;
216  $sql .= " ORDER BY label";
217 
218  //print $sql;
219  $resql = $this->db->query($sql);
220  if ($resql) {
221  $num = $this->db->num_rows($resql);
222 
223  if (empty($conf->partnership->enabled)) {
224  $num = 0; // Force empty list if category module is not enabled
225  }
226 
227  if ($num) {
228  $s .= '<option value="-1">'.$langs->trans("PartnershipType").'</option>';
229  }
230 
231  $i = 0;
232  while ($i < $num) {
233  $obj = $this->db->fetch_object($resql);
234 
235  $s .= '<option value="'.$obj->rowid.'">'.dol_escape_htmltag($obj->label);
236  $s .= '</option>';
237  $i++;
238  }
239  $s .= ajax_combobox("filter_partnership");
240  } else {
241  dol_print_error($this->db);
242  }
243 
244  $s .= '</select> ';
245 
246  return $s;
247  }
248 
249 
257  public function url($id, $sourcetype = 'thirdparty')
258  {
259  if ($sourcetype == 'thirdparty') {
260  return '<a href="'.DOL_URL_ROOT.'/societe/card.php?socid='.((int) $id).'">'.img_object('', "societe").'</a>';
261  }
262  if ($sourcetype == 'member') {
263  return '<a href="'.DOL_URL_ROOT.'/adherent/card.php?id='.((int) $id).'">'.img_object('', "member").'</a>';
264  }
265 
266  return '';
267  }
268 }
ajax_combobox($htmlname, $events=array(), $minLengthToAutocomplete=0, $forcefocus=0, $widthTypeOfAutocomplete='resolve', $idforemptyvalue='-1', $morecss='')
Convert a html select field into an ajax combobox.
Definition: ajax.lib.php:449
Parent class of emailing target selectors modules.
Class to manage a list of personalised recipients for mailing feature.
getSqlArrayForStats()
On the main mailing area, there is a box with statistics.
add_to_target($mailing_id)
This is the main function that returns the array of emails.
__construct($db)
Constructor.
getNbOfRecipients($sql='')
Return here number of distinct emails returned by your selector.
formFilter()
This is to add a form filter to provide variant of selector If used, the HTML select must be called "...
url($id, $sourcetype='thirdparty')
Can include an URL link on each record provided by selector shown on target page.
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("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->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:745
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
$conf db
API class for accounts.
Definition: inc.php:41