]> git.agnieray.net Git - galette.git/blob - galette/cron/reminder.php
9907a6f9eea38c557a04bd29dd4482b78a7be6cb
[galette.git] / galette / cron / reminder.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Galette cron reminders
7 *
8 * PHP version 5
9 *
10 * Copyright © 2013-2023 The Galette Team
11 *
12 * This file is part of Galette (http://galette.tuxfamily.org).
13 *
14 * Galette is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
18 *
19 * Galette is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
26 *
27 * @category Main
28 * @package Galette
29 *
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2013-2022 The Galette Team
32 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
33 * @link http://galette.tuxfamily.org
34 * @since Available since 0.7.5dev - 2013-02-08
35 */
36
37 use Galette\Entity\Texts;
38 use Galette\Repository\Members;
39 use Galette\Repository\Reminders;
40 use Galette\Filters\MembersList;
41
42 /** @ignore */
43 require_once __DIR__ . '/../includes/galette.inc.php';
44
45 $gapp = new \Galette\Core\LightSlimApp('CRON');
46 $app = $gapp->getApp();
47
48 session_start();
49 require_once __DIR__ . '/../includes/dependencies.php';
50
51 if (isset($needs_update) && $needs_update === true) {
52 echo _T("Your Galette database is not present, or not up to date.");
53 die(1);
54 }
55
56 /**
57 * Authentication middleware
58 */
59 $authenticate = new \Galette\Middleware\Authenticate($container);
60
61 require_once GALETTE_ROOT . 'includes/routes/main.routes.php';
62 require_once GALETTE_ROOT . 'includes/routes/authentication.routes.php';
63 require_once GALETTE_ROOT . 'includes/routes/management.routes.php';
64 require_once GALETTE_ROOT . 'includes/routes/members.routes.php';
65 require_once GALETTE_ROOT . 'includes/routes/groups.routes.php';
66 require_once GALETTE_ROOT . 'includes/routes/contributions.routes.php';
67 if ($cron) {
68 $container->get('login')->logCron(basename($argv[0], '.php'));
69 define('GALETTE_CRON', true);
70 }
71
72 if (!$container->get('login')->isCron()) {
73 die(1);
74 }
75
76 if ($cron && !defined('GALETTE_URI')) {
77 echo _T('Please define constant "GALETTE_URI" with the path to your instance.') . "\n";
78 die(1);
79 }
80
81 $texts = new Texts(
82 $container->get('preferences')
83 );
84 $reminders = new Reminders();
85 $success_detected = [];
86 $error_detected = [];
87
88 $list_reminders = $reminders->getList($container->get('zdb'), false);
89 if (count($list_reminders) > 0) {
90 foreach ($list_reminders as $reminder) {
91 //send reminders by email
92 $sent = $reminder->send($texts, $container->get('history'), $container->get('zdb'));
93
94 if ($sent === true) {
95 $success_detected[] = $reminder->getMessage();
96 } else {
97 $error_detected[] = $reminder->getMessage();
98 }
99 }
100
101 if (count($error_detected) > 0) {
102 array_unshift(
103 $error_detected,
104 _T("Reminder has not been sent:")
105 );
106 }
107
108 if (count($success_detected) > 0) {
109 array_unshift(
110 $success_detected,
111 _T("Sent reminders:")
112 );
113 }
114 }
115
116 //called from a cron. warning and errors has been stored into history
117 //and probably logged
118 if (count($error_detected) > 0) {
119 //if there are errors, we print them
120 echo "\n";
121 $count = 0;
122 foreach ($error_detected as $e) {
123 if ($count > 0) {
124 echo ' ';
125 }
126 echo $e . "\n";
127 $count++;
128 }
129 //we can also print additional information.
130 if (count($success_detected) > 0) {
131 echo "\n";
132 echo str_replace(
133 '%i',
134 count($success_detected),
135 _T("%i emails have been sent successfully.")
136 );
137 }
138 exit(1);
139 } else {
140 //if there were no errors, we just exit properly for cron to be quiet.
141 exit(0);
142 }