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