dolibarr 21.0.0-alpha
mailing_mymodule_selector1.modules.php
1<?php
2/* Copyright (C) 2005-2012 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) ---Replace with your own copyright and developer email---
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
12include_once DOL_DOCUMENT_ROOT.'/core/modules/mailings/modules_mailings.php';
13dol_include_once("/mymodule/class/myobject.class.php");
14
15
20{
21 // CHANGE THIS: Put here a name not already used
22 public $name = 'mailing_mymodule_selector1';
23 // CHANGE THIS: Put here a description of your selector module
24 public $desc = 'Emailing target selector1';
25
26 // CHANGE THIS: Set to 1 if selector is available for admin users only
30 public $require_admin = 0;
31
32 public $enabled = 'isModEnabled("mymodule")';
33
37 public $require_module = array();
38
42 public $picto = 'generic';
43
47 public $db;
48
49
55 public function __construct($db)
56 {
57 $this->db = $db;
58 //$this->enabled = ...
59 }
60
61
67 public function formFilter()
68 {
69 global $langs;
70 $langs->load("members");
71
72 $arraystatus = array(1 => 'Option 1', 2 => 'Option 2');
73
74 $s = '';
75 $s .= $langs->trans("Status").': ';
76 $s .= '<select name="filter" class="flat">';
77 $s .= '<option value="none">&nbsp;</option>';
78 foreach ($arraystatus as $status) {
79 $s .= '<option value="'.$status.'">'.$status.'</option>';
80 }
81 $s .= '</select>';
82 $s .= '<br>';
83
84 return $s;
85 }
86
87
94 public function url($id)
95 {
96 return '<a href="'.dol_buildpath('/mymodule/myobject_card.php', 1).'?id='.$id.'">'.img_object('', "generic").'</a>';
97 }
98
99
100 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
107 public function add_to_target($mailing_id)
108 {
109 // phpcs:enable
110 $target = array();
111 $j = 0;
112
113 $sql = "SELECT rowid as id, firstname, lastname, email";
114 $sql .= " FROM ".MAIN_DB_PREFIX."myobject";
115 $sql .= " WHERE email IS NOT NULL AND email <> ''";
116 if (GETPOSTISSET('filter') && GETPOST('filter', 'alphanohtml') != 'none') {
117 $sql .= " AND status = '".$this->db->escape(GETPOST('filter', 'alphanohtml'))."'";
118 }
119 $sql .= " ORDER BY email";
120
121 // Store recipients in target
122 $result = $this->db->query($sql);
123 if ($result) {
124 $num = $this->db->num_rows($result);
125 $i = 0;
126
127 dol_syslog(__METHOD__.":add_to_target ".$num." targets found");
128
129 $old = '';
130 while ($i < $num) {
131 $obj = $this->db->fetch_object($result);
132 if ($old != $obj->email) {
133 $target[$j] = array(
134 'email' => $obj->email,
135 'id' => $obj->id,
136 'firstname' => $obj->firstname,
137 'lastname' => $obj->lastname,
138 //'other' => $obj->label,
139 'source_url' => $this->url($obj->id),
140 'source_id' => $obj->id,
141 'source_type' => 'myobject@mymodule'
142 );
143 $old = $obj->email;
144 $j++;
145 }
146
147 $i++;
148 }
149 } else {
150 dol_syslog($this->db->error());
151 $this->error = $this->db->error();
152 return -1;
153 }
154
155 // You must fill the $target array with record like this
156 // $target[0]=array('email'=>'email_0','name'=>'name_0','firstname'=>'firstname_0');
157 // ...
158 // $target[n]=array('email'=>'email_n','name'=>'name_n','firstname'=>'firstname_n');
159
160 // Example: $target[0]=array('email'=>'myemail@mydomain.com','name'=>'Doe','firstname'=>'John');
161
162 // ----- Your code ends here -----
163
164 return parent::addTargetsToDatabase($mailing_id, $target);
165 }
166
167
176 public function getSqlArrayForStats()
177 {
178 // CHANGE THIS: Optional
179
180 //var $statssql=array();
181 //$this->statssql[0]="SELECT field1 as label, count(distinct(email)) as nb FROM mytable WHERE email IS NOT NULL";
182
183 return array();
184 }
185
186
195 public function getNbOfRecipients($sql = '')
196 {
197 $sql = "SELECT COUNT(DISTINCT(email)) as nb";
198 $sql .= " FROM ".MAIN_DB_PREFIX."myobject as p";
199 $sql .= " WHERE email IS NOT NULL AND email <> ''";
200
201 $a = parent::getNbOfRecipients($sql);
202
203 if ($a < 0) {
204 return -1;
205 }
206 return $a;
207 }
208}
$id
Definition account.php:39
Parent class of emailing target selectors modules.
getNbOfRecipients($sql='')
Return the number of distinct emails returned by your selector.
formFilter()
Display 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)
Return 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.