]> git.agnieray.net Git - galette.git/blob - galette/includes/routes/ajax.routes.php
a502395e0d73dc4a22e53657813622e46e6f10bb
[galette.git] / galette / includes / routes / ajax.routes.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Ajax routes
7 *
8 * PHP version 5
9 *
10 * Copyright © 2014-2020 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 Routes
28 * @package Galette
29 *
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2014-2020 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 0.8.2dev 2014-11-11
35 */
36
37 use Galette\Entity\Adherent;
38 use Galette\Entity\Contribution;
39 use Galette\Entity\ContributionsTypes;
40 use Galette\Repository\Members;
41 use Galette\Filters\MembersList;
42
43 $app->group('/ajax', function () use ($authenticate) {
44 $this->get(
45 '/messages',
46 function ($request, $response) {
47 $this->get('view')->render(
48 $response,
49 'ajax_messages.tpl'
50 );
51 return $response;
52 }
53 )->setName('ajaxMessages');
54
55 $this->post(
56 'photo',
57 function ($request, $response) {
58 $post = $request->getParsedBody();
59 $ret = ['result' => false];
60
61 if (
62 !isset($post['member_id'])
63 || !isset($post['file'])
64 || !isset($post['filename'])
65 || !isset($post['filesize'])
66 ) {
67 $this->get('flash')->addMessage(
68 'error_detected',
69 _T("Required argument not present!")
70 );
71 return $response->withJson($ret);
72 }
73
74 $mid = $post['member_id'];
75 $fsize = $post['filesize'];
76 $fname = $post['filename'];
77 $tmpname = GALETTE_TEMPIMAGES_PATH . 'ajax_upload_' . $fname;
78
79 $temp = explode('base64,', $post['file']);
80 $raw_file = base64_decode($temp[1]);
81
82 //write temporary file
83 $fp = fopen($tmpname, 'w');
84 fwrite($fp, $raw_file);
85 fclose($fp);
86
87 $adh = new Adherent($this->get('zdb'), (int)$mid);
88
89 $res = $adh->picture->store(
90 array(
91 'name' => $fname,
92 'tmp_name' => $tmpname,
93 'size' => $fsize
94 ),
95 true
96 );
97
98 if ($res < 0) {
99 $ret['message'] = $adh->picture->getErrorMessage($res);
100 $this->get('flash')->addMessage(
101 'error_detected',
102 $ret['message']
103 );
104 } else {
105 $ret['result'] = true;
106 $this->get('flash')->addMessage(
107 'success_detected',
108 _T('Member photo has been changed.')
109 );
110 }
111
112 return $response->withJson($ret);
113 }
114 )->setName('photoDnd');
115
116 $this->post(
117 '/suggest/towns',
118 function ($request, $response) {
119 $post = $request->getParsedBody();
120
121 $ret = [];
122
123 try {
124 $select1 = $this->get('zdb')->select(Adherent::TABLE);
125 $select1->columns(['ville_adh']);
126 $select1->where->like('ville_adh', '%' . html_entity_decode($post['term']) . '%');
127
128 $select2 = $this->get('zdb')->select(Adherent::TABLE);
129 $select2->columns(['lieu_naissance']);
130 $select2->where->like('lieu_naissance', '%' . html_entity_decode($post['term']) . '%');
131
132 $select1->combine($select2);
133
134 $select = $this->get('zdb')->sql->select();
135 $select->from(['sub' => $select1])
136 ->order('ville_adh ASCC')
137 ->limit(10);
138
139 $towns = $this->get('zdb')->execute($select);
140
141 foreach ($towns as $town) {
142 $ret[] = [
143 'id' => $town->ville_adh,
144 'label' => $town->ville_adh
145 ];
146 }
147 } catch (Throwable $e) {
148 Analog::log(
149 'Something went wrong is towns suggestion: ' . $e->getMessage(),
150 Analog::WARNING
151 );
152 throw $e;
153 }
154
155 return $response->withJson($ret);
156 }
157 )->setName('suggestTown');
158
159 $this->post(
160 '/suggest/countries',
161 function ($request, $response) {
162 $post = $request->getParsedBody();
163
164 $ret = [];
165
166 try {
167 $select = $this->get('zdb')->select(Adherent::TABLE);
168 $select->columns(['pays_adh']);
169 $select->where->like('pays_adh', '%' . html_entity_decode($post['term']) . '%');
170 $select->limit(10);
171 $select->order(['pays_adh ASC']);
172
173 $towns = $this->get('zdb')->execute($select);
174
175 foreach ($towns as $town) {
176 $ret[] = [
177 'id' => $town->pays_adh,
178 'label' => $town->pays_adh
179 ];
180 }
181 } catch (Throwable $e) {
182 Analog::log(
183 'Something went wrong is countries suggestion: ' . $e->getMessage(),
184 Analog::WARNING
185 );
186 throw $e;
187 }
188
189 return $response->withJson($ret);
190 }
191 )->setName('suggestCountry');
192
193 $this->get(
194 '/telemetry/infos',
195 function ($request, $response) {
196 $telemetry = new \Galette\Util\Telemetry(
197 $this->get('zdb'),
198 $this->get('preferences'),
199 $this->get('plugins')
200 );
201 $body = $response->getBody();
202 $body->write('<pre>' . json_encode($telemetry->getTelemetryInfos(), JSON_PRETTY_PRINT) . '</pre>');
203 return $response;
204 }
205 )->setName('telemetryInfos')->add($authenticate);
206
207 $this->post(
208 '/telemetry/send',
209 function ($request, $response) {
210 $telemetry = new \Galette\Util\Telemetry(
211 $this->get('zdb'),
212 $this->get('preferences'),
213 $this->get('plugins')
214 );
215 try {
216 $telemetry->send();
217 $message = _T('Telemetry information has been sent. Thank you!');
218 $result = [
219 'success' => true,
220 'message' => $message
221 ];
222 } catch (Throwable $e) {
223 $result = [
224 'success' => false,
225 'message' => $e->getMessage()
226 ];
227 }
228 return $response->withJson($result);
229 }
230 )->setName('telemetrySend')->add($authenticate);
231
232 $this->get(
233 '/telemetry/registered',
234 function ($request, $response) {
235 $this->get('preferences')->pref_registration_date = date('Y-m-d H:i:s');
236 $this->get('preferences')->store();
237 return $response->withJson(['message' => _T('Thank you for registering!')]);
238 }
239 )->setName('setRegistered')->add($authenticate);
240
241 $this->post(
242 '/contribution/dates',
243 function ($request, $response) {
244 $post = $request->getParsedBody();
245
246 $contrib = new Contribution(
247 $this->get('zdb'),
248 $this->get('login'),
249 [
250 'type' => (int)$post['fee_id'],
251 'adh' => (int)$post['member_id']
252 ]
253 );
254 $contribution['duree_mois_cotis'] = $this->get('preferences')->pref_membership_ext;
255
256 return $response->withJson([
257 'date_debut_cotis' => $contrib->begin_date,
258 'date_fin_cotis' => $contrib->end_date
259 ]);
260 }
261 )->setName('contributionDates')->add($authenticate);
262
263 $this->post(
264 '/contribution/members[/{page:\d+}[/{search}]]',
265 function ($request, $response, int $page = null, $search = null) {
266 $post = $request->getParsedBody();
267 $filters = new MembersList();
268 if (isset($post['page'])) {
269 $filters->current_page = (int)$post['page'];
270 } elseif ($page !== null) {
271 $filters->current_page = $page;
272 }
273
274 if (isset($post['search'])) {
275 $search = $post['search'];
276 }
277 if ($search !== null) {
278 $filters->filter_str = $search;
279 if (is_numeric($search)) {
280 $filters->field_filter = Members::FILTER_NUMBER;
281 }
282 }
283
284 $m = new Members($filters);
285 $list_members = $m->getSelectizedMembers($this->get('zdb'));
286
287 $members = [];
288 if (count($list_members) > 0) {
289 foreach ($list_members as $pk => $member) {
290 $members[] = [
291 'value' => $pk,
292 'text' => $member
293 ];
294 }
295 }
296
297 return $response->withJson([
298 'members' => $members,
299 'count' => count($members)
300 ]);
301 }
302 )->setName('contributionMembers')->add($authenticate);
303
304 $this->post(
305 '/password/strength',
306 function ($request, $response) {
307 //post params may be passed from security tab test password
308 $post = $request->getParsedBody();
309
310 if (isset($post['pref_password_length'])) {
311 $this->get('preferences')->pref_password_length = $post['pref_password_length'];
312 }
313
314 if (isset($post['pref_password_strength'])) {
315 $this->get('preferences')->pref_password_strength = $post['pref_password_strength'];
316 }
317
318 if (isset($post['pref_password_blacklist'])) {
319 $this->get('preferences')->pref_password_blacklist = $post['pref_password_blacklist'];
320 }
321
322 $pass = new \Galette\Util\Password($this->get('preferences'));
323 $valid = $pass->isValid($post['value']);
324
325 return $response->withJson(
326 [
327 'valid' => $valid,
328 'score' => $pass->getStrenght(),
329 'errors' => $pass->getErrors(),
330 'warnings' => ($valid ? $pass->getStrenghtErrors() : null)
331 ]
332 );
333 }
334 )->setName('checkPassword');
335 });