dolibarr 19.0.4
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
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 = '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 = 'isModEnabled("mymodule")';
28
29 public $require_module = array();
30
34 public $picto = 'fa-file';
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 $form = new Form($this->db);
65
66 $arraystatus = array(1=>'Option 1', 2=>'Option 2');
67
68 $s = '';
69 $s .= $langs->trans("Status").': ';
70 $s .= '<select name="filter" class="flat">';
71 $s .= '<option value="none">&nbsp;</option>';
72 foreach ($arraystatus as $status) {
73 $s .= '<option value="'.$status.'">'.$status.'</option>';
74 }
75 $s .= '</select>';
76 $s .= '<br>';
77
78 return $s;
79 }
80
81
88 public function url($id)
89 {
90 return '<a href="'.dol_buildpath('/mymodule/myobject_card.php', 1).'?id='.$id.'">'.img_object('', "generic").'</a>';
91 }
92
93
94 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
101 public function add_to_target($mailing_id)
102 {
103 // phpcs:enable
104 $target = array();
105 $j = 0;
106
107 $sql = " select rowid as id, label, firstname, lastname";
108 $sql .= " from ".MAIN_DB_PREFIX."myobject";
109 $sql .= " where email IS NOT NULL AND email <> ''";
110 if (GETPOSTISSET('filter') && GETPOST('filter', 'alphanohtml') != 'none') {
111 $sql .= " AND status = '".$this->db->escape(GETPOST('filter', 'alphanohtml'))."'";
112 }
113 $sql .= " ORDER BY email";
114
115 // Store recipients in target
116 $result = $this->db->query($sql);
117 if ($result) {
118 $num = $this->db->num_rows($result);
119 $i = 0;
120
121 dol_syslog("mailinglist_mymodule_myobject.modules.php: mailing ".$num." targets found");
122
123 $old = '';
124 while ($i < $num) {
125 $obj = $this->db->fetch_object($result);
126 if ($old != $obj->email) {
127 $target[$j] = array(
128 'email' => $obj->email,
129 'name' => $obj->lastname,
130 'id' => $obj->id,
131 'firstname' => $obj->firstname,
132 'other' => $obj->label,
133 'source_url' => $this->url($obj->id),
134 'source_id' => $obj->id,
135 'source_type' => 'myobject@mymodule'
136 );
137 $old = $obj->email;
138 $j++;
139 }
140
141 $i++;
142 }
143 } else {
144 dol_syslog($this->db->error());
145 $this->error = $this->db->error();
146 return -1;
147 }
148
149 // You must fill the $target array with record like this
150 // $target[0]=array('email'=>'email_0','name'=>'name_0','firstname'=>'firstname_0');
151 // ...
152 // $target[n]=array('email'=>'email_n','name'=>'name_n','firstname'=>'firstname_n');
153
154 // Example: $target[0]=array('email'=>'myemail@mydomain.com','name'=>'Doe','firstname'=>'John');
155
156 // ----- Your code end here -----
157
158 return parent::addTargetsToDatabase($mailing_id, $target);
159 }
160
161
170 public function getSqlArrayForStats()
171 {
172 // CHANGE THIS: Optionnal
173
174 //var $statssql=array();
175 //$this->statssql[0]="SELECT field1 as label, count(distinct(email)) as nb FROM mytable WHERE email IS NOT NULL";
176
177 return array();
178 }
179
180
189 public function getNbOfRecipients($sql = '')
190 {
191 $sql = "select count(distinct(email)) as nb from ".MAIN_DB_PREFIX."myobject as p 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}
Class to manage generation of HTML components Only common components must be here.
Parent class of emailing target selectors modules.
formFilter()
Displays the filter form that appears in the mailing recipient selection page.
getNbOfRecipients($sql='')
Return here number of distinct emails returned by your selector.
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=false, $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.