3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
10 * Copyright © 2014-2020 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-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
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
;
43 $app->group('/ajax', function () use ($authenticate) {
46 function ($request, $response) {
47 $this->get('view')->render(
53 )->setName('ajaxMessages');
57 function ($request, $response) {
58 $post = $request->getParsedBody();
59 $ret = ['result' => false];
62 !isset($post['member_id'])
63 ||
!isset($post['file'])
64 ||
!isset($post['filename'])
65 ||
!isset($post['filesize'])
67 $this->get('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 $raw_file = base64_decode($temp[1]);
82 //write temporary file
83 $fp = fopen($tmpname, 'w');
84 fwrite($fp, $raw_file);
87 $adh = new Adherent($this->get('zdb'), (int)$mid);
89 $res = $adh->picture
->store(
92 'tmp_name' => $tmpname,
99 $ret['message'] = $adh->picture
->getErrorMessage($res);
100 $this->get('flash')->addMessage(
105 $ret['result'] = true;
106 $this->get('flash')->addMessage(
108 _T('Member photo has been changed.')
112 return $response->withJson($ret);
114 )->setName('photoDnd');
118 function ($request, $response) {
119 $post = $request->getParsedBody();
124 $select1 = $this->get('zdb')->select(Adherent
::TABLE
);
125 $select1->columns(['ville_adh']);
126 $select1->where
->like('ville_adh', '%' . html_entity_decode($post['term']) . '%');
128 $select2 = $this->get('zdb')->select(Adherent
::TABLE
);
129 $select2->columns(['lieu_naissance']);
130 $select2->where
->like('lieu_naissance', '%' . html_entity_decode($post['term']) . '%');
132 $select1->combine($select2);
134 $select = $this->get('zdb')->sql
->select();
135 $select->from(['sub' => $select1])
136 ->order('ville_adh ASCC')
139 $towns = $this->get('zdb')->execute($select);
141 foreach ($towns as $town) {
143 'id' => $town->ville_adh
,
144 'label' => $town->ville_adh
147 } catch (Throwable
$e) {
149 'Something went wrong is towns suggestion: ' . $e->getMessage(),
155 return $response->withJson($ret);
157 )->setName('suggestTown');
160 '/suggest/countries',
161 function ($request, $response) {
162 $post = $request->getParsedBody();
167 $select = $this->get('zdb')->select(Adherent
::TABLE
);
168 $select->columns(['pays_adh']);
169 $select->where
->like('pays_adh', '%' . html_entity_decode($post['term']) . '%');
171 $select->order(['pays_adh ASC']);
173 $towns = $this->get('zdb')->execute($select);
175 foreach ($towns as $town) {
177 'id' => $town->pays_adh
,
178 'label' => $town->pays_adh
181 } catch (Throwable
$e) {
183 'Something went wrong is countries suggestion: ' . $e->getMessage(),
189 return $response->withJson($ret);
191 )->setName('suggestCountry');
195 function ($request, $response) {
196 $telemetry = new \Galette\Util\
Telemetry(
198 $this->get('preferences'),
199 $this->get('plugins')
201 $body = $response->getBody();
202 $body->write('<pre>' . json_encode($telemetry->getTelemetryInfos(), JSON_PRETTY_PRINT
) . '</pre>');
205 )->setName('telemetryInfos')->add($authenticate);
209 function ($request, $response) {
210 $telemetry = new \Galette\Util\
Telemetry(
212 $this->get('preferences'),
213 $this->get('plugins')
217 $message = _T('Telemetry information has been sent. Thank you!');
220 'message' => $message
222 } catch (Throwable
$e) {
225 'message' => $e->getMessage()
228 return $response->withJson($result);
230 )->setName('telemetrySend')->add($authenticate);
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!')]);
239 )->setName('setRegistered')->add($authenticate);
242 '/contribution/dates',
243 function ($request, $response) {
244 $post = $request->getParsedBody();
246 $contrib = new Contribution(
250 'type' => (int)$post['fee_id'],
251 'adh' => (int)$post['member_id']
254 $contribution['duree_mois_cotis'] = $this->get('preferences')->pref_membership_ext
;
256 return $response->withJson([
257 'date_debut_cotis' => $contrib->begin_date
,
258 'date_fin_cotis' => $contrib->end_date
261 )->setName('contributionDates')->add($authenticate);
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;
274 if (isset($post['search'])) {
275 $search = $post['search'];
277 if ($search !== null) {
278 $filters->filter_str
= $search;
279 if (is_numeric($search)) {
280 $filters->field_filter
= Members
::FILTER_NUMBER
;
284 $m = new Members($filters);
285 $list_members = $m->getSelectizedMembers($this->get('zdb'));
288 if (count($list_members) > 0) {
289 foreach ($list_members as $pk => $member) {
297 return $response->withJson([
298 'members' => $members,
299 'count' => count($members)
302 )->setName('contributionMembers')->add($authenticate);
305 '/password/strength',
306 function ($request, $response) {
307 //post params may be passed from security tab test password
308 $post = $request->getParsedBody();
310 if (isset($post['pref_password_length'])) {
311 $this->get('preferences')->pref_password_length
= $post['pref_password_length'];
314 if (isset($post['pref_password_strength'])) {
315 $this->get('preferences')->pref_password_strength
= $post['pref_password_strength'];
318 if (isset($post['pref_password_blacklist'])) {
319 $this->get('preferences')->pref_password_blacklist
= $post['pref_password_blacklist'];
322 $pass = new \Galette\Util\
Password($this->get('preferences'));
323 $valid = $pass->isValid($post['value']);
325 return $response->withJson(
328 'score' => $pass->getStrenght(),
329 'errors' => $pass->getErrors(),
330 'warnings' => ($valid ?
$pass->getStrenghtErrors() : null)
334 )->setName('checkPassword');