dolibarr  16.0.5
mailinglist_mymodule_myobject.modules.php
1 <?php
2 /* Copyright (C) 2005-2012 Laurent Destailleur <eldy@users.sourceforge.net>
3  *
4  * This file is an example to follow to add your own email selector inside
5  * the Dolibarr email tool.
6  * Follow instructions given in README file to know what to change to build
7  * your own emailing list selector.
8  * Code that need to be changed in this file are marked by "CHANGE THIS" tag.
9  */
10 
11 include_once DOL_DOCUMENT_ROOT.'/core/modules/mailings/modules_mailings.php';
12 dol_include_once("/mymodule/class/myobject.class.php");
13 
14 
19 {
20  // CHANGE THIS: Put here a name not already used
21  public $name = 'mailinglist_mymodule_myobject';
22  // CHANGE THIS: Put here a description of your selector module
23  public $desc = 'My object emailing target selector';
24  // CHANGE THIS: Set to 1 if selector is available for admin users only
25  public $require_admin = 0;
26 
27  public $enabled = '$conf->mymodule->enabled';
28 
29  public $require_module = array();
30 
34  public $picto = 'mymodule@mymodule';
35 
39  public $db;
40 
41 
47  public function __construct($db)
48  {
49  global $conf;
50 
51  $this->db = $db;
52  if (is_array($conf->modules)) {
53  $this->enabled = in_array('mymodule', $conf->modules) ? 1 : 0;
54  }
55  }
56 
57 
63  public function formFilter()
64  {
65  global $langs;
66  $langs->load("members");
67 
68  $form = new Form($this->db);
69 
70  $arraystatus = array(1=>'Option 1', 2=>'Option 2');
71 
72  $s = '';
73  $s .= $langs->trans("Status").': ';
74  $s .= '<select name="filter" class="flat">';
75  $s .= '<option value="none">&nbsp;</option>';
76  foreach ($arraystatus as $status) {
77  $s .= '<option value="'.$status.'">'.$status.'</option>';
78  }
79  $s .= '</select>';
80  $s .= '<br>';
81 
82  return $s;
83  }
84 
85 
92  public function url($id)
93  {
94  return '<a href="'.dol_buildpath('/mymodule/myobject_card.php', 1).'?id='.$id.'">'.img_object('', "generic").'</a>';
95  }
96 
97 
98  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
105  public function add_to_target($mailing_id)
106  {
107  // phpcs:enable
108  $target = array();
109  $j = 0;
110 
111  $sql = " select rowid as id, email, firstname, lastname, plan, partner";
112  $sql .= " from ".MAIN_DB_PREFIX."myobject";
113  $sql .= " where email IS NOT NULL AND email != ''";
114  if (GETPOSTISSET('filter') && GETPOST('filter', 'alphanohtml') != 'none') {
115  $sql .= " AND status = '".$this->db->escape(GETPOST('filter', 'alphanohtml'))."'";
116  }
117  $sql .= " ORDER BY email";
118 
119  // Store recipients in target
120  $result = $this->db->query($sql);
121  if ($result) {
122  $num = $this->db->num_rows($result);
123  $i = 0;
124 
125  dol_syslog("mailinglist_mymodule_myobject.modules.php: mailing ".$num." targets found");
126 
127  $old = '';
128  while ($i < $num) {
129  $obj = $this->db->fetch_object($result);
130  if ($old <> $obj->email) {
131  $target[$j] = array(
132  'email' => $obj->email,
133  'name' => $obj->lastname,
134  'id' => $obj->id,
135  'firstname' => $obj->firstname,
136  'other' => $obj->plan.';'.$obj->partner,
137  'source_url' => $this->url($obj->id),
138  'source_id' => $obj->id,
139  'source_type' => 'dolicloud'
140  );
141  $old = $obj->email;
142  $j++;
143  }
144 
145  $i++;
146  }
147  } else {
148  dol_syslog($this->db->error());
149  $this->error = $this->db->error();
150  return -1;
151  }
152 
153  // You must fill the $target array with record like this
154  // $target[0]=array('email'=>'email_0','name'=>'name_0','firstname'=>'firstname_0');
155  // ...
156  // $target[n]=array('email'=>'email_n','name'=>'name_n','firstname'=>'firstname_n');
157 
158  // Example: $target[0]=array('email'=>'myemail@mydomain.com','name'=>'Doe','firstname'=>'John');
159 
160  // ----- Your code end here -----
161 
162  return parent::addTargetsToDatabase($mailing_id, $target);
163  }
164 
165 
174  public function getSqlArrayForStats()
175  {
176  // CHANGE THIS: Optionnal
177 
178  //var $statssql=array();
179  //$this->statssql[0]="SELECT field1 as label, count(distinct(email)) as nb FROM mytable WHERE email IS NOT NULL";
180 
181  return array();
182  }
183 
184 
194  public function getNbOfRecipients($filter = 1, $option = '')
195  {
196  $a = parent::getNbOfRecipients("select count(distinct(email)) as nb from ".MAIN_DB_PREFIX."myobject as p where email IS NOT NULL AND email != ''");
197 
198  if ($a < 0) {
199  return -1;
200  }
201  return $a;
202  }
203 }
db
$conf db
API class for accounts.
Definition: inc.php:41
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_include_once
if(!function_exists('dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
Definition: functions.lib.php:1033
MailingTargets
Parent class of emailing target selectors modules.
Definition: modules_mailings.php:32
$form
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:142
mailing_mailinglist_mymodule_myobject\formFilter
formFilter()
Displays the filter form that appears in the mailing recipient selection page.
Definition: mailinglist_mymodule_myobject.modules.php:63
mailing_mailinglist_mymodule_myobject\getSqlArrayForStats
getSqlArrayForStats()
On the main mailing area, there is a box with statistics.
Definition: mailinglist_mymodule_myobject.modules.php:174
mailing_mailinglist_mymodule_myobject\url
url($id)
Returns url link to file of the source of the recipient of the mailing.
Definition: mailinglist_mymodule_myobject.modules.php:92
mailing_mailinglist_mymodule_myobject\__construct
__construct($db)
Constructor.
Definition: mailinglist_mymodule_myobject.modules.php:47
mailing_mailinglist_mymodule_myobject\getNbOfRecipients
getNbOfRecipients($filter=1, $option='')
Return here number of distinct emails returned by your selector.
Definition: mailinglist_mymodule_myobject.modules.php:194
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_mailinglist_mymodule_myobject\add_to_target
add_to_target($mailing_id)
This is the main function that returns the array of emails.
Definition: mailinglist_mymodule_myobject.modules.php:105
mailing_mailinglist_mymodule_myobject
mailing_mailinglist_mymodule
Definition: mailinglist_mymodule_myobject.modules.php:18
GETPOSTISSET
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
Definition: functions.lib.php:386
Form
Class to manage generation of HTML components Only common components must be here.
Definition: html.form.class.php:52
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