dolibarr  16.0.5
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 
19 include_once DOL_DOCUMENT_ROOT.'/core/modules/mailings/modules_mailings.php';
20 
21 
26 {
27  public $name = 'PartnershipThirdartiesOrMembers';
28  // This label is used if no translation is found for key XXX neither MailingModuleDescXXX where XXX=name is found
29  public $desc = "Thirdparties or members included into a partnership program";
30  public $require_admin = 0;
31 
32  public $require_module = array('partnership'); // This module allows to select by categories must be also enabled if category module is not activated
33 
37  public $picto = 'partnership';
38 
42  public $db;
43 
44  public $enabled = '$conf->partnership->enabled';
45 
46 
52  public function __construct($db)
53  {
54  global $conf, $langs;
55  $langs->load("companies");
56 
57  $this->db = $db;
58  }
59 
60 
61  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
68  public function add_to_target($mailing_id)
69  {
70  // phpcs:enable
71  global $conf, $langs;
72 
73  $cibles = array();
74  $addDescription = '';
75 
76  $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";
77  $sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."partnership as p, ".MAIN_DB_PREFIX."c_partnership_type as pt";
78  $sql .= " WHERE s.email <> ''";
79  $sql .= " AND s.entity IN (".getEntity('societe').")";
80  $sql .= " AND s.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".((int) $mailing_id).")";
81  $sql .= " AND p.fk_soc = s.rowid";
82  $sql .= " AND pt.rowid = p.fk_type";
83  if (GETPOST('filter', 'int') > 0) {
84  $sql .= " AND pt.rowid=".((int) GETPOST('filter', 'int'));
85  }
86 
87  $sql .= " UNION ";
88 
89  $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";
90  $sql .= " FROM ".MAIN_DB_PREFIX."adherent as s, ".MAIN_DB_PREFIX."partnership as p, ".MAIN_DB_PREFIX."c_partnership_type as pt";
91  $sql .= " WHERE s.email <> ''";
92  $sql .= " AND s.entity IN (".getEntity('member').")";
93  $sql .= " AND s.email NOT IN (SELECT email FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE fk_mailing=".((int) $mailing_id).")";
94  $sql .= " AND p.fk_member = s.rowid";
95  $sql .= " AND pt.rowid = p.fk_type";
96  if (GETPOST('filter', 'int') > 0) {
97  $sql .= " AND pt.rowid=".((int) GETPOST('filter', 'int'));
98  }
99 
100  $sql .= " ORDER BY email";
101 
102  // Stock recipients emails into targets table
103  $result = $this->db->query($sql);
104  if ($result) {
105  $num = $this->db->num_rows($result);
106  $i = 0;
107  $j = 0;
108 
109  dol_syslog(get_class($this)."::add_to_target mailing ".$num." targets found");
110 
111  $old = '';
112  while ($i < $num) {
113  $obj = $this->db->fetch_object($result);
114  if ($old <> $obj->email) {
115  $otherTxt = ($obj->label ? $langs->transnoentities("PartnershipType").'='.$obj->label : '');
116  if (strlen($addDescription) > 0 && strlen($otherTxt) > 0) {
117  $otherTxt .= ";";
118  }
119  $otherTxt .= $addDescription;
120  $cibles[$j] = array(
121  'email' => $obj->email,
122  'fk_contact' => $obj->fk_contact,
123  'lastname' => $obj->name, // For a thirdparty, we must use name
124  'firstname' => '', // For a thirdparty, lastname is ''
125  'other' => $otherTxt,
126  'source_url' => $this->url($obj->id, $obj->source),
127  'source_id' => $obj->id,
128  'source_type' => $obj->source
129  );
130  $old = $obj->email;
131  $j++;
132  }
133 
134  $i++;
135  }
136  } else {
137  dol_syslog($this->db->error());
138  $this->error = $this->db->error();
139  return -1;
140  }
141 
142  return parent::addTargetsToDatabase($mailing_id, $cibles);
143  }
144 
145 
154  public function getSqlArrayForStats()
155  {
156  // CHANGE THIS: Optionnal
157 
158  //var $statssql=array();
159  //$this->statssql[0]="SELECT field1 as label, count(distinct(email)) as nb FROM mytable WHERE email IS NOT NULL";
160  return array();
161  }
162 
163 
172  public function getNbOfRecipients($sql = '')
173  {
174  global $conf;
175 
176  $sql = "SELECT count(distinct(s.email)) as nb";
177  $sql .= " FROM ".MAIN_DB_PREFIX."partnership as p, ".MAIN_DB_PREFIX."societe as s";
178  $sql .= " WHERE s.rowid = p.fk_soc AND s.email <> ''";
179  $sql .= " AND s.entity IN (".getEntity('societe').")";
180 
181  $sql .= " UNION ";
182 
183  $sql .= "SELECT count(distinct(s.email)) as nb";
184  $sql .= " FROM ".MAIN_DB_PREFIX."partnership as p, ".MAIN_DB_PREFIX."adherent as s";
185  $sql .= " WHERE s.rowid = p.fk_member AND s.email <> ''";
186  $sql .= " AND s.entity IN (".getEntity('member').")";
187 
188  //print $sql;
189 
190  // La requete doit retourner un champ "nb" pour etre comprise par parent::getNbOfRecipients
191  return parent::getNbOfRecipients($sql);
192  }
193 
200  public function formFilter()
201  {
202  global $conf, $langs;
203 
204  $langs->load("companies");
205 
206  $s = '<select id="filter_partnership" name="filter" class="flat">';
207 
208  // Show categories
209  $sql = "SELECT rowid, label, code, active";
210  $sql .= " FROM ".MAIN_DB_PREFIX."c_partnership_type";
211  $sql .= " WHERE active = 1";
212  $sql .= " AND entity = ".$conf->entity;
213  $sql .= " ORDER BY label";
214 
215  //print $sql;
216  $resql = $this->db->query($sql);
217  if ($resql) {
218  $num = $this->db->num_rows($resql);
219 
220  if (empty($conf->partnership->enabled)) {
221  $num = 0; // Force empty list if category module is not enabled
222  }
223 
224  if ($num) {
225  $s .= '<option value="-1">'.$langs->trans("PartnershipType").'</option>';
226  }
227 
228  $i = 0;
229  while ($i < $num) {
230  $obj = $this->db->fetch_object($resql);
231 
232  $s .= '<option value="'.$obj->rowid.'">'.dol_escape_htmltag($obj->label);
233  $s .= '</option>';
234  $i++;
235  }
236  $s .= ajax_combobox("filter_partnership");
237  } else {
238  dol_print_error($this->db);
239  }
240 
241  $s .= '</select> ';
242 
243  return $s;
244  }
245 
246 
254  public function url($id, $sourcetype = 'thirdparty')
255  {
256  if ($sourcetype == 'thirparty') {
257  return '<a href="'.DOL_URL_ROOT.'/societe/card.php?socid='.((int) $id).'">'.img_object('', "societe").'</a>';
258  }
259  if ($sourcetype == 'member') {
260  return '<a href="'.DOL_URL_ROOT.'/adherent/card.php?id='.((int) $id).'">'.img_object('', "member").'</a>';
261  }
262 
263  return '';
264  }
265 }
mailing_partnership\add_to_target
add_to_target($mailing_id)
This is the main function that returns the array of emails.
Definition: partnership.modules.php:68
ajax_combobox
ajax_combobox($htmlname, $events=array(), $minLengthToAutocomplete=0, $forcefocus=0, $widthTypeOfAutocomplete='resolve', $idforemptyvalue='-1')
Convert a html select field into an ajax combobox.
Definition: ajax.lib.php:438
db
$conf db
API class for accounts.
Definition: inc.php:41
dol_escape_htmltag
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.
Definition: functions.lib.php:1468
mailing_partnership\url
url($id, $sourcetype='thirdparty')
Can include an URL link on each record provided by selector shown on target page.
Definition: partnership.modules.php:254
GETPOST
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:484
dol_print_error
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Definition: functions.lib.php:4844
MailingTargets
Parent class of emailing target selectors modules.
Definition: modules_mailings.php:32
mailing_partnership\getSqlArrayForStats
getSqlArrayForStats()
On the main mailing area, there is a box with statistics.
Definition: partnership.modules.php:154
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
mailing_partnership
Class to manage a list of personalised recipients for mailing feature.
Definition: partnership.modules.php:25
mailing_partnership\__construct
__construct($db)
Constructor.
Definition: partnership.modules.php:52
mailing_partnership\getNbOfRecipients
getNbOfRecipients($sql='')
Return here number of distinct emails returned by your selector.
Definition: partnership.modules.php:172
img_object
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
Definition: functions.lib.php:4211
$resql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->facture->lire)||(isModEnabled('supplier_invoice') && $user->rights->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->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:742
mailing_partnership\formFilter
formFilter()
This is to add a form filter to provide variant of selector If used, the HTML select must be called "...
Definition: partnership.modules.php:200