3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
10 * Copyright © 2014 The Galette Team
12 * This file is part of Galette (http://galette.tuxfamily.org).
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.
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.
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/>.
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
34 * @link http://galette.tuxfamily.org
35 * @since 0.8.2dev 2014-11-11
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
;
44 $app->group('/ajax', function () use ($authenticate) {
47 function ($request, $response) {
54 )->setName('ajaxMessages');
58 function ($request, $response) {
59 $post = $request->getParsedBody();
60 $ret = ['result' => false];
62 if (!isset($post['member_id'])
63 ||
!isset($post['file'])
64 ||
!isset($post['filename'])
65 ||
!isset($post['filesize'])
67 $this->flash
->addMessage(
69 _T("Required argument not present!")
71 return $response->withJson($ret);
74 $mid = $post['member_id'];
75 $fsize = $post['filesize'];
76 $fname = $post['filename'];
77 $tmpname = GALETTE_TEMPIMAGES_PATH
. 'ajax_upload_' . $fname;
79 $temp = explode('base64,', $post['file']);
80 $mime = str_replace('data:', '', trim($temp[0], ';'));
81 $raw_file = base64_decode($temp[1]);
83 //write temporary file
84 $fp = fopen($tmpname, 'w');
85 fwrite($fp, $raw_file);
88 $adh = new Adherent($this->zdb
, (int)$mid);
90 $res = $adh->picture
->store(
93 'tmp_name' => $tmpname,
100 $ret['message'] = $adh->picture
->getErrorMessage($res);
101 $this->flash
->addMessage(
106 $ret['result'] = true;
107 $this->flash
->addMessage(
109 _T('Member photo has been changed.')
113 return $response->withJson($ret);
115 )->setName('photoDnd');
119 function ($request, $response) {
120 $post = $request->getParsedBody();
125 $select1 = $this->zdb
->select(Adherent
::TABLE
);
126 $select1->columns(['ville_adh']);
127 $select1->where
->like('ville_adh', '%' . html_entity_decode($post['term']) . '%');
129 $select2 = $this->zdb
->select(Adherent
::TABLE
);
130 $select2->columns(['lieu_naissance']);
131 $select2->where
->like('lieu_naissance', '%' . html_entity_decode($post['term']) . '%');
133 $select1->combine($select2);
135 $select = $this->zdb
->sql
->select();
136 $select->from(['sub' => $select1])
137 ->order('ville_adh ASCC')
140 $towns = $this->zdb
->execute($select);
142 foreach ($towns as $town) {
144 'id' => $town->ville_adh
,
145 'label' => $town->ville_adh
148 } catch (\Exception
$e) {
150 'Something went wrong is towns suggestion: ' . $e->getMessage(),
156 return $response->withJson($ret);
158 )->setName('suggestTown');
161 '/suggest/countries',
162 function ($request, $response) {
163 $post = $request->getParsedBody();
168 $select = $this->zdb
->select(Adherent
::TABLE
);
169 $select->columns(['pays_adh']);
170 $select->where
->like('pays_adh', '%' . html_entity_decode($post['term']) . '%');
172 $select->order(['pays_adh ASC']);
174 $towns = $this->zdb
->execute($select);
176 foreach ($towns as $town) {
178 'id' => $town->pays_adh
,
179 'label' => $town->pays_adh
182 } catch (\Exception
$e) {
184 'Something went wrong is countries suggestion: ' . $e->getMessage(),
190 return $response->withJson($ret);
192 )->setName('suggestCountry');
196 function ($request, $response) {
197 $telemetry = new \Galette\Util\
Telemetry(
202 $body = $response->getBody();
203 $body->write('<pre>' . json_encode($telemetry->getTelemetryInfos(), JSON_PRETTY_PRINT
) . '</pre>');
206 )->setName('telemetryInfos')->add($authenticate);
210 function ($request, $response) {
211 $telemetry = new \Galette\Util\
Telemetry(
217 $result = $telemetry->send();
218 $message = _T('Telemetry information has been sent. Thank you!');
221 'message' => $message
223 } catch (\Exception
$e) {
226 'message' => $e->getMessage()
229 return $response->withJson($result);
231 )->setName('telemetrySend')->add($authenticate);
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!')]);
240 )->setName('setRegistered')->add($authenticate);
243 '/contribution/dates',
244 function ($request, $response) {
245 $post = $request->getParsedBody();
247 $contrib = new Contribution(
251 'type' => (int)$post['fee_id'],
252 'adh' => (int)$post['member_id']
255 $contribution['duree_mois_cotis'] = $this->preferences
->pref_membership_ext
;
257 return $response->withJson([
258 'date_debut_cotis' => $contrib->begin_date
,
259 'date_fin_cotis' => $contrib->end_date
262 )->setName('contributionDates')->add($authenticate);
265 '/contribution/members[/{page:\d+}[/{search}]]',
266 function ($request, $response, $args) {
267 $post = $request->getParsedBody();
268 $filters = new MembersList();
269 if (isset($post['page'])) {
270 $filters->current_page
= (int)$post['page'];
271 } elseif (isset($args['page'])) {
272 $filters->current_page
= (int)$args['page'];
276 if (isset($args['search'])) {
277 $term = $args['search'];
279 if (isset($post['search'])) {
280 $term = $post['search'];
282 if ($term !== null) {
283 $filters->filter_str
= $term;
284 if (is_numeric($term)) {
285 $filters->field_filter
= Members
::FILTER_NUMBER
;
289 $m = new Members($filters);
290 $list_members = $m->getSelectizedMembers($this->zdb
);
293 if (count($list_members) > 0) {
294 foreach ($list_members as $pk => $member) {
302 return $response->withJson([
303 'members' => $members,
304 'count' => count($members)
307 )->setName('contributionMembers')->add($authenticate);
310 '/password/strength',
311 function ($request, $response) {
312 //post params may be passed from security tab test password
313 $post = $request->getParsedBody();
315 if (isset($post['pref_password_length'])) {
316 $this->preferences
->pref_password_length
= $post['pref_password_length'];
319 if (isset($post['pref_password_strength'])) {
320 $this->preferences
->pref_password_strength
= $post['pref_password_strength'];
323 if (isset($post['pref_password_blacklist'])) {
324 $this->preferences
->pref_password_blacklist
= $post['pref_password_blacklist'];
327 $pass = new \Galette\Util\
Password($this->preferences
);
328 $valid = $pass->isValid($post['value']);
330 return $response->withJson(
333 'score' => $pass->getStrenght(),
334 'errors' => $pass->getErrors(),
335 'warnings' => ($valid ?
$pass->getStrenghtErrors() : null)
339 )->setName('checkPassword');