dolibarr 20.0.0
mailing_mymodule_selector1.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
11include_once DOL_DOCUMENT_ROOT.'/core/modules/mailings/modules_mailings.php';
12dol_include_once("/mymodule/class/myobject.class.php");
13
14
19{
20 // CHANGE THIS: Put here a name not already used
21 public $name = 'mailing_mymodule_selector1';
22 // CHANGE THIS: Put here a description of your selector module
23 public $desc = 'Emailing target selector1';
24 // CHANGE THIS: Set to 1 if selector is available for admin users only
25 public $require_admin = 0;
26
27 public $enabled = 'isModEnabled("mymodule")';
28
29 public $require_module = array();
30
34 public $picto = 'generic';
35
39 public $db;
40
41
47 public function __construct($db)
48 {
49 $this->db = $db;
50 //$this->enabled = ...
51 }
52
53
59 public function formFilter()
60 {
61 global $langs;
62 $langs->load("members");
63
64 $arraystatus = array(1=>'Option 1', 2=>'Option 2');
65
66 $s = '';
67 $s .= $langs->trans("Status").': ';
68 $s .= '<select name="filter" class="flat">';
69 $s .= '<option value="none">&nbsp;</option>';
70 foreach ($arraystatus as $status) {
71 $s .= '<option value="'.$status.'">'.$status.'</option>';
72 }
73 $s .= '</select>';
74 $s .= '<br>';
75
76 return $s;
77 }
78
79
86 public function url($id)
87 {
88 return '<a href="'.dol_buildpath('/mymodule/myobject_card.php', 1).'?id='.$id.'">'.img_object('', "generic").'</a>';
89 }
90
91
92 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
99 public function add_to_target($mailing_id)
100 {
101 // phpcs:enable
102 $target = array();
103 $j = 0;
104
105 $sql = "SELECT rowid as id, firstname, lastname, email";
106 $sql .= " FROM ".MAIN_DB_PREFIX."myobject";
107 $sql .= " WHERE email IS NOT NULL AND email <> ''";
108 if (GETPOSTISSET('filter') && GETPOST('filter', 'alphanohtml') != 'none') {
109 $sql .= " AND status = '".$this->db->escape(GETPOST('filter', 'alphanohtml'))."'";
110 }
111 $sql .= " ORDER BY email";
112
113 // Store recipients in target
114 $result = $this->db->query($sql);
115 if ($result) {
116 $num = $this->db->num_rows($result);
117 $i = 0;
118
119 dol_syslog(__METHOD__.":add_to_target ".$num." targets found");
120
121 $old = '';
122 while ($i < $num) {
123 $obj = $this->db->fetch_object($result);
124 if ($old != $obj->email) {
125 $target[$j] = array(
126 'email' => $obj->email,
127 'id' => $obj->id,
128 'firstname' => $obj->firstname,
129 'lastname' => $obj->lastname,
130 //'other' => $obj->label,
131 'source_url' => $this->url($obj->id),
132 'source_id' => $obj->id,
133 'source_type' => 'myobject@mymodule'
134 );
135 $old = $obj->email;
136 $j++;
137 }
138
139 $i++;
140 }
141 } else {
142 dol_syslog($this->db->error());
143 $this->error = $this->db->error();
144 return -1;
145 }
146
147 // You must fill the $target array with record like this
148 // $target[0]=array('email'=>'email_0','name'=>'name_0','firstname'=>'firstname_0');
149 // ...
150 // $target[n]=array('email'=>'email_n','name'=>'name_n','firstname'=>'firstname_n');
151
152 // Example: $target[0]=array('email'=>'myemail@mydomain.com','name'=>'Doe','firstname'=>'John');
153
154 // ----- Your code end here -----
155
156 return parent::addTargetsToDatabase($mailing_id, $target);
157 }
158
159
168 public function getSqlArrayForStats()
169 {
170 // CHANGE THIS: Optional
171
172 //var $statssql=array();
173 //$this->statssql[0]="SELECT field1 as label, count(distinct(email)) as nb FROM mytable WHERE email IS NOT NULL";
174
175 return array();
176 }
177
178
187 public function getNbOfRecipients($sql = '')
188 {
189 $sql = "SELECT COUNT(DISTINCT(email)) as nb";
190 $sql .= " FROM ".MAIN_DB_PREFIX."myobject as p";
191 $sql .= " WHERE email IS NOT NULL AND email <> ''";
192
193 $a = parent::getNbOfRecipients($sql);
194
195 if ($a < 0) {
196 return -1;
197 }
198 return $a;
199 }
200}
Parent class of emailing target selectors modules.
getNbOfRecipients($sql='')
Return here number of distinct emails returned by your selector.
formFilter()
Displays the filter form that appears in the mailing recipient selection page.
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.
url($id)
Returns url link to file of the source of the recipient of the mailing.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
if(!function_exists( 'dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
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.