Ajout du support des écritures ventilées.
[garradin.git] / include / class.rappels_envoyes.php
1 <?php
2
3 namespace Garradin;
4
5 class Rappels_Envoyes
6 {
7 /**
8 * Types de médias
9 */
10 const MEDIA_EMAIL = 1;
11 const MEDIA_COURRIER = 2;
12 const MEDIA_TELEPHONE = 3;
13 const MEDIA_AUTRE = 4;
14
15 /**
16 * Nombre d'items par page dans les listes
17 */
18 const ITEMS_PER_PAGE = 50;
19
20 /**
21 * Vérification des champs fournis pour la modification de donnée
22 * @param array $data Tableau contenant les champs à ajouter/modifier
23 * @return void
24 */
25 protected function _checkFields(&$data)
26 {
27 $db = DB::getInstance();
28
29 if (isset($data['id_cotisation']))
30 {
31 if (!$db->simpleQuerySingle('SELECT 1 FROM cotisations WHERE id = ?;', false, (int) $data['id_cotisation']))
32 {
33 throw new UserException('Cotisation inconnue.');
34 }
35
36 $data['id_cotisation'] = (int) $data['id_cotisation'];
37 }
38
39 if (empty($data['id_membre'])
40 || !$db->simpleQuerySingle('SELECT 1 FROM membres WHERE id = ?;', false, (int) $data['id_membre']))
41 {
42 throw new UserException('Membre inconnu.');
43 }
44
45 $data['id_membre'] = (int) $data['id_membre'];
46
47 if (empty($data['media']) || !is_numeric($data['media'])
48 || !in_array((int)$data['media'], [self::MEDIA_EMAIL, self::MEDIA_COURRIER, self::MEDIA_TELEPHONE, self::MEDIA_AUTRE]))
49 {
50 throw new UserException('Média invalide.');
51 }
52
53 $data['media'] = (int) $data['media'];
54
55 if (empty($data['date']) || !utils::checkDate($data['date']))
56 {
57 throw new UserException('La date indiquée n\'est pas valide.');
58 }
59 }
60
61 /**
62 * Enregistrer un rappel
63 * @param array $data Données du rappel
64 * @return integer Numéro ID du rappel créé
65 */
66 public function add($data)
67 {
68 $db = DB::getInstance();
69
70 $this->_checkFields($data);
71
72 $db->simpleInsert('rappels_envoyes', $data);
73
74 return $db->lastInsertRowId();
75 }
76
77 /**
78 * Supprimer un rappel enregistré
79 * @param integer $id Numéro du rappel
80 * @return boolean TRUE en cas de succès
81 */
82 public function delete($id)
83 {
84 $db = DB::getInstance();
85 $db->simpleExec('DELETE FROM rappels_envoyes WHERE id = ?;', (int) $id);
86 return true;
87 }
88
89 /**
90 * Renvoie les données sur un rappel
91 * @param integer $id Numéro du rappel
92 * @return array Données du rappel
93 */
94 public function get($id)
95 {
96 return DB::getInstance()->simpleQuerySingle('SELECT * FROM rappels_envoyes WHERE id = ?;', true, (int)$id);
97 }
98
99 /**
100 * Remplacer les tags dans le contenu/sujet du mail
101 * @param string $content Chaîne à traiter
102 * @param array $data Données supplémentaires à utiliser comme tags (tableau associatif)
103 * @return string $content dont les tags ont été remplacés par le contenu correct
104 */
105 public function replaceTagsInContent($content, $data = null)
106 {
107 $config = Config::getInstance();
108 $tags = [
109 '#NOM_ASSO' => $config->get('nom_asso'),
110 '#ADRESSE_ASSO' => $config->get('adresse_asso'),
111 '#EMAIL_ASSO' => $config->get('email_asso'),
112 '#SITE_ASSO' => $config->get('site_asso'),
113 '#URL_RACINE' => WWW_URL,
114 '#URL_SITE' => WWW_URL,
115 '#URL_ADMIN' => WWW_URL . 'admin/',
116 ];
117
118 if (!empty($data) && is_array($data))
119 {
120 foreach ($data as $key=>$value)
121 {
122 $key = '#' . strtoupper($key);
123 $tags[$key] = $value;
124 }
125 }
126
127 return strtr($content, $tags);
128 }
129
130 /**
131 * Envoi de mail pour rappel automatisé
132 * @param array $data Données du rappel automatisé
133 * @return boolean TRUE
134 */
135 public function sendAuto($data)
136 {
137 $replace = $data;
138 $replace['date_rappel'] = utils::sqliteDateToFrench($replace['date_rappel']);
139 $replace['date_expiration'] = utils::sqliteDateToFrench($replace['expiration']);
140 $replace['nb_jours'] = abs($replace['nb_jours']);
141 $replace['delai'] = abs($replace['delai']);
142
143 $subject = $this->replaceTagsInContent($data['sujet'], $replace);
144 $text = $this->replaceTagsInContent($data['texte'], $replace);
145
146 // Envoi du mail
147 utils::mail($data['email'], $subject, $text);
148
149 // Enregistrement en DB
150 $this->add([
151 'id_cotisation' => $data['id_cotisation'],
152 'id_membre' => $data['id'],
153 'media' => Rappels_Envoyes::MEDIA_EMAIL,
154 // On enregistre la date de mise en œuvre du rappel
155 // et non pas la date d'envoi effective du rappel
156 // car l'envoi du rappel peut ne pas être effectué
157 // le jour où il aurait dû être envoyé (la magie des cron)
158 'date' => $data['date_rappel'],
159 ]);
160
161 return true;
162 }
163
164 /**
165 * Liste des rappels envoyés à un membre
166 * @param integer $id Numéro du membre
167 * @return array Liste des rappels
168 */
169 public function listForMember($id)
170 {
171 return DB::getInstance()->simpleStatementFetch('SELECT
172 re.*, c.intitule, c.montant
173 FROM rappels_envoyes AS re
174 INNER JOIN cotisations AS c ON c.id = re.id_cotisation
175 WHERE re.id_membre = ?
176 ORDER BY re.date DESC;', \SQLITE3_ASSOC, (int)$id);
177 }
178
179 /**
180 * Liste des rappels pour une cotisation donnée
181 * @param integer $id Numéro de la cotisation
182 * @param integer $page Numéro de page de liste
183 * @return array Liste des rappels
184 */
185 public function listForCotisation($id, $page = 1)
186 {
187 $begin = ($page - 1) * self::ITEMS_PER_PAGE;
188
189 return DB::getInstance()->simpleStatementFetch('SELECT * FROM rappels_envoyes
190 WHERE id_rappel IN (SELECT id FROM rappels WHERE id_cotisation = ?)
191 ORDER BY date DESC;', \SQLITE3_ASSOC, (int)$id);
192 }
193
194 /**
195 * Nombre de rappels pour une cotisation donnée
196 * @param integer $id Numéro de la cotisation
197 * @return integer Nombre de rappels envoyés
198 */
199 public function countForCotisation($id)
200 {
201 return DB::getInstance()->simpleQuerySingle('SELECT COUNT(*) FROM rappels_envoyes
202 WHERE id_rappel IN (SELECT id FROM rappels WHERE id_cotisation = ?);',
203 false, (int)$id);
204 }
205
206 /**
207 * Liste des rappels envoyés pour un rappel automatique
208 * @param integer $id Numéro du rappel
209 * @param integer $page Numéro de page de liste
210 * @return array Liste des rappels envoyés
211 */
212 public function listForRappel($id, $page = 1)
213 {
214 $begin = ($page - 1) * self::ITEMS_PER_PAGE;
215
216 return DB::getInstance()->simpleStatementFetch('SELECT * FROM rappels_envoyes
217 WHERE id_rappel = ? ORDER BY date DESC LIMIT ?,?;',
218 \SQLITE3_ASSOC, (int)$id, (int)$begin, self::ITEMS_PER_PAGE);
219 }
220
221 /**
222 * Nombre de rappels envoyés pour un rappel automatique
223 * @param integer $id Numéro du rappel
224 * @return integer Nombre de rappels envoyés pour ce rappel
225 */
226 public function countForRappel($id)
227 {
228 return DB::getInstance()->simpleQuerySingle('SELECT COUNT(*) FROM rappels_envoyes
229 WHERE id_rappel = ?;', false, (int)$id);
230 }
231 }