10 const MEDIA_EMAIL
= 1;
11 const MEDIA_COURRIER
= 2;
12 const MEDIA_TELEPHONE
= 3;
13 const MEDIA_AUTRE
= 4;
16 * Nombre d'items par page dans les listes
18 const ITEMS_PER_PAGE
= 50;
21 * Vérification des champs fournis pour la modification de donnée
22 * @param array $data Tableau contenant les champs à ajouter/modifier
25 protected function _checkFields(&$data)
27 $db = DB
::getInstance();
29 if (isset($data['id_cotisation']))
31 if (!$db->simpleQuerySingle('SELECT 1 FROM cotisations WHERE id = ?;', false, (int) $data['id_cotisation']))
33 throw new UserException('Cotisation inconnue.');
36 $data['id_cotisation'] = (int) $data['id_cotisation'];
39 if (empty($data['id_membre'])
40 ||
!$db->simpleQuerySingle('SELECT 1 FROM membres WHERE id = ?;', false, (int) $data['id_membre']))
42 throw new UserException('Membre inconnu.');
45 $data['id_membre'] = (int) $data['id_membre'];
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
]))
50 throw new UserException('Média invalide.');
53 $data['media'] = (int) $data['media'];
55 if (empty($data['date']) ||
!utils
::checkDate($data['date']))
57 throw new UserException('La date indiquée n\'est pas valide.');
62 * Enregistrer un rappel
63 * @param array $data Données du rappel
64 * @return integer Numéro ID du rappel créé
66 public function add($data)
68 $db = DB
::getInstance();
70 $this->_checkFields($data);
72 $db->simpleInsert('rappels_envoyes', $data);
74 return $db->lastInsertRowId();
78 * Supprimer un rappel enregistré
79 * @param integer $id Numéro du rappel
80 * @return boolean TRUE en cas de succès
82 public function delete($id)
84 $db = DB
::getInstance();
85 $db->simpleExec('DELETE FROM rappels_envoyes WHERE id = ?;', (int) $id);
90 * Renvoie les données sur un rappel
91 * @param integer $id Numéro du rappel
92 * @return array Données du rappel
94 public function get($id)
96 return DB
::getInstance()->simpleQuerySingle('SELECT * FROM rappels_envoyes WHERE id = ?;', true, (int)$id);
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
105 public function replaceTagsInContent($content, $data = null)
107 $config = Config
::getInstance();
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/',
118 if (!empty($data) && is_array($data))
120 foreach ($data as $key=>$value)
122 $key = '#' . strtoupper($key);
123 $tags[$key] = $value;
127 return strtr($content, $tags);
131 * Envoi de mail pour rappel automatisé
132 * @param array $data Données du rappel automatisé
133 * @return boolean TRUE
135 public function sendAuto($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']);
143 $subject = $this->replaceTagsInContent($data['sujet'], $replace);
144 $text = $this->replaceTagsInContent($data['texte'], $replace);
147 utils
::mail($data['email'], $subject, $text);
149 // Enregistrement en DB
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'],
165 * Liste des rappels envoyés à un membre
166 * @param integer $id Numéro du membre
167 * @return array Liste des rappels
169 public function listForMember($id)
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);
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
185 public function listForCotisation($id, $page = 1)
187 $begin = ($page - 1) * self
::ITEMS_PER_PAGE
;
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);
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
199 public function countForCotisation($id)
201 return DB
::getInstance()->simpleQuerySingle('SELECT COUNT(*) FROM rappels_envoyes
202 WHERE id_rappel IN (SELECT id FROM rappels WHERE id_cotisation = ?);',
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
212 public function listForRappel($id, $page = 1)
214 $begin = ($page - 1) * self
::ITEMS_PER_PAGE
;
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
);
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
226 public function countForRappel($id)
228 return DB
::getInstance()->simpleQuerySingle('SELECT COUNT(*) FROM rappels_envoyes
229 WHERE id_rappel = ?;', false, (int)$id);