admin.php 67 KB
Newer Older
1
<?php
2
namespace Controller;
3
4
5
6

use LQDN\Command\AdminCreateCommand;
use LQDN\Command\AdminDeleteCommand;
use LQDN\Command\AdminChangePasswordCommand;
7
use LQDN\Command\CounterpartCreateCommand;
8
9
10
use LQDN\Command\DonationValidateCommand;
use LQDN\Command\DonationInvalidateCommand;
use LQDN\Command\DonationResetPdfCommand;
Okhin's avatar
Okhin committed
11
use LQDN\Command\DonationCreateCommand;
12
use LQDN\Command\UserUpdateByAdminCommand;
13
use LQDN\Command\UserCreateCommand;
14
use LQDN\Command\UserUpdateCumulCommand;
Okhin's avatar
Okhin committed
15
use LQDN\Command\AdminUpdateParentCommand;
16
use LQDN\Command\AdminUpdateTotalUsersCommand;
17
18
19
20
21
22
23

class Admin extends Controller
{
    // Fonction appelée avant routage
    public function beforeRoute($f3, $args)
    {
        parent::beforeRoute($f3, $args);
24
25
26
27

        // Template de base par défaut
        $this->template = "backend/base.html";

28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
        // URL de base à utiliser sur le site
        $f3->set('DONS_STATUTS', array(
            0 => "Ponctuel non validé",
            1 => "Ponctuel validé",
            4 => "Récurrent validé",
            42 => "En cours de traitement",
            100 => "Récurrent non validé",
            101 => "Récurrent validé",
            102 => "Récurrent remis",
        ));

        $f3->set('CT_STATUTS', array(
            1 => "Demandé",
            2 => "Envoyé",
            3 => "NPAI",
            99 => "Annulé",
        ));

        $f3->set('CB_STATUTS', array(
            1 => "en cours",
            2 => "à relancer",
            3 => "relancé",
            99 => "résilié",
            100 => "non applicable",
        ));
53
        
54
        $f3->set('TAILLES', array(
55
            0 => _('PlaceHolder -- ne pas utiliser') . ' S',
56
57
58
59
60
61
62
63
64
65
66
67
            1 => _('Coupe Homme, Taille') . ' S',
            2 => _('Coupe Homme, Taille') . ' M',
            3 => _('Coupe Homme, Taille') . ' L',
            4 => _('Coupe Homme, Taille') . ' XL',
            5 => _('Coupe Femme, Taille') . ' S',
            6 => _('Coupe Femme, Taille') . ' M',
            7 => _('Coupe Femme, Taille') . ' L',
            8 => _('Coupe Femme, Taille') . ' XL',
            9 => _('Coupe Homme, Taille') . ' XXL',
            10 => _('Coupe Femme, Taille') . ' XXL',
        ));

68
69
70
        if (!$f3->get('SESSION.admin')) {
            $mapper = new\DB\SQL\Mapper($f3->get('DB'), 'admins');
            $auth = new \Auth($mapper, array('id'=>'user_id', 'pw'=>'password'));
71
            $success = $auth->basic('\Controller\Admin::hash_password');
72
            if (!$success) {
73
                $f3->push('SESSION.error', 'Accès non autorisé');
Okhin's avatar
Okhin committed
74
                $f3->push('SESSION.message', '');
75
76
77
78
79
80
81
82
83
                $f3->error(401);
            } else {
                $f3->set('SESSION.admin', true);
            }
        };

        $f3->set('PIPLOME_URL', PIPLOME_URL);
    }

84
85
    public function afterRoute($f3, $args)
    {
86
87
88
89
90
91
92
93
        // Test si il y a des messages
        if (!$f3->exists('SESSION.message')) {
            $f3->push('SESSION.message', '');
        }
        if (!$f3->exists('SESSION.error')) {
            $f3->push('SESSION.error', '');
        }

94
95
96
97
98
99
100
101
102
        // Rendu de la page
        if ($this->template!='') {
            echo \Template::instance()->render($this->template);
        }
        // Une fois que tout est affiché, on peut supprimer les notifications
        $f3->clear('SESSION.message');
        $f3->clear('SESSION.error');
    }

Guinness's avatar
Guinness committed
103
104
105
106
107
108
    public function logout($f3, $args)
    {
        $f3->clear('SESSION');
        $f3->reroute('/');
    }

109

110
111
112
113
114
115
116
117
118
    public static function hash_password($password)
    {
        return hash('sha256', $password);
    }

    public function show($f3, $args)
    {
        if ($f3->get('VERB')=='POST' && $f3->exists('POST.action')) {
            switch ($f3->get('POST.action')) {
Okhin's avatar
Okhin committed
119
120
121
122
123
124
125
126
127
            case 'valid':
                $f3->get('container')['command_handler']->handle(new DonationValidateCommand($f3->get('POST.id')));
                break;
            case 'invalid':
                $f3->get('container')['command_handler']->handle(new DonationInvalidateCommand($f3->get('POST.id')));
                break;
            case 'pdf':
                $f3->get('container')['command_handler']->handle(new DonationResetPdfCommand($f3->get('POST.id')));
                break;
128
129
130
131
132
133
134
135
            }
        }

        $text = $f3->exists('POST.texte') ? $f3->get('POST.texte') : '';
        $sum = $f3->exists('POST.somme') ? $f3->get('POST.somme') : '';
        $public = $f3->exists('POST.public') ? $f3->get('POST.public') : '';
        $status = $f3->exists('POST.status') ? $f3->get('POST.status') : '';
        $limit = $f3->exists('POST.limite') ? (int) $f3->get('POST.limite') : 50;
Okhin's avatar
Okhin committed
136
        $page = $f3->exists('POST.page') ? (int) $f3->get('POST.page') : 1;
137
138
        $date1 = $f3->exists('POST.date1') ? (string) $f3->get('POST.date1') . " 00:00:00" : '';
        $date2 = $f3->exists('POST.date2') ? (string) $f3->get('POST.date2') . " 00:00:00" : date("Y-m-d H:i:s");
139

140
        $results = $f3->get('container')['donation_finder']->adminSearch($text, $sum, $public, $status, $date1, $date2, $limit, $page);
141
142
143
144
145

        $f3->set('row_count', count($results));
        $f3->set('texte', $text);
        $f3->set('somme', $sum);
        $f3->set('limite', $limit);
Okhin's avatar
Okhin committed
146
        $f3->set('page', $page);
147
148
        $f3->set('public', $public);
        $f3->set('status', $status);
149
150
        $f3->set('date1', substr($date1, 0, 10));
        $f3->set('date2', substr($date2, 0, 10));
151
152
153
154
155
156
        $f3->set('data', $results);
        $f3->set('piplome_url', PIPLOME_URL);

        $f3->set('block_content', 'backend/dons.html');
    }

Okhin's avatar
Linting    
Okhin committed
157
158
    public function validate($f3, $args)
    {
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
        $id = '';
        $error = '';
        $db = $f3->get('DB');
        if ($f3->get('VERB')=='GET') {
            if ($f3->exists('PARAMS.id')) {
                $id = $f3->get('PARAMS.id');
            } else {
                $error= 'Pas d\'id associé à ce don';
            }
            if ($id != '') {
                $result = $db->query("SELECT status,
                    somme,
                    adresse_id,
                    user_id
                    FROM dons
                    WHERE id = ".\Utils::asl($id));
                $result = $result->fetch(\PDO::FETCH_ASSOC);
                $user_amount = $db->query("SELECT total, cumul FROM users
                    WHERE id = ".\Utils::asl($result['user_id']));
                $user_amount = $user_amount->fetch(\PDO::FETCH_ASSOC);
            }
            if ($result['status'] >= 100) {
                $status = 101;
            } else {
                $status = 1;
            }
            $total = $user_amount['total'] + $result['somme'];
186
            $cumul = $user_amount['cumul'] + $result['somme'];
187
188
        }
        if ($error != '') {
189
            $f3->push('SESSION.error', $error);
190
        } else {
Okhin's avatar
Linting    
Okhin committed
191
192
            $db->query(
                "UPDATE dons
193
194
                SET status = $status
                WHERE id = ".\Utils::asl($id)
Okhin's avatar
Okhin committed
195
            );
196
197
198
199
200
201
202
203
204
            $db->query("UPDATE users
                SET total = $total,
                cumul = $cumul
                WHERE id = ".\Utils::asl($result['user_id']));
        }
        $f3->reroute("/admin/users/".$result['user_id']);
    }


Okhin's avatar
Linting    
Okhin committed
205
206
    public function invalidate($f3, $args)
    {
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
        $id = '';
        $error = '';
        $db = $f3->get('DB');
        if ($f3->get('VERB')=='GET') {
            if ($f3->exists('PARAMS.id')) {
                $id = $f3->get('PARAMS.id');
            } else {
                $error= 'Pas d\'id associé à ce don';
            }
            if ($id != '') {
                $result = $db->query("SELECT status,
                    somme,
                    adresse_id,
                    user_id
                    FROM dons
                    WHERE id = ".\Utils::asl($id));
                $result = $result->fetch(\PDO::FETCH_ASSOC);
                $user_amount = $db->query("SELECT total, cumul FROM users
                    WHERE id = ".\Utils::asl($result['user_id']));
                $user_amount = $user_amount->fetch(\PDO::FETCH_ASSOC);
            }
            if ($result['status'] >= 100) {
                $status = 100;
            } else {
                $status = 0;
            }
            $total = $user_amount['total'] - $result['somme'];
234
            $cumul = $user_amount['cumul'] - $result['somme'];
235
236
237
238
239
            if ($cumul < 0) {
                $error = "Impossible d'invalider, une contrepartie a été demandée";
            }
        }
        if ($error != '') {
240
            $f3->push('SESSION.error', $error);
241
        } else {
Okhin's avatar
Linting    
Okhin committed
242
243
            $db->query(
                "UPDATE dons
244
245
                SET status = $status
                WHERE id = ".\Utils::asl($id)
Okhin's avatar
Okhin committed
246
            );
247
248
249
250
251
252
253
254
            $db->query("UPDATE users
                SET total = $total,
                cumul = $cumul
                WHERE id = ".\Utils::asl($result['user_id']));
        }
        $f3->reroute("/admin/users/".$result['user_id']);
    }

255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
    public function support($f3, $args)
    {
        $id = '';
        $pseudo = '';
        $email = '';
        $somme = '';
        $lang = '';
        $abo = '0';
        $public = '0';
        $user_id = '';
        $msg = '';
        $adresse_id = '';
        $db = $f3->get('DB');
        if ($f3->get('VERB')=='GET') {
            if ($f3->exists('PARAMS.id')) {
                $id = $f3->get('PARAMS.id');
            }
272
            $f3->set('block_content', 'backend/don.html');
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
        } else {
            $id = $f3->get('POST.id');
            $pseudo = $f3->get('POST.pseudo');
            $email = $f3->get('POST.email');
            $somme = $f3->get('POST.somme');
            $lang = $f3->get('POST.lang');
            if ($f3->exists('POST.abo')) {
                $abo = '1';
            }
            if ($f3->exists('POST.public')) {
                $public = '1';
            }

            // Champs minimaux : pseudo, somme, email
            if ($pseudo=='' or $somme=='' or $email=='') {
                $msg = "Erreur: Les champs 'Pseudo', 'Montant' et 'Email' sont obligatoires";
            } else {
                // Mise à jour ou ajout du don
                if ($id=='') {
                    // We need to check if there's already a user with this email
                    $result = $db->query("SELECT id
Okhin's avatar
Okhin committed
294
295
                        FROM users
                        WHERE email='".\Utils::asl($email)."'");
296
297
                    if ($result) {
                        // The user already exist, lets get the user_id
298
                        $user_id = $result->fetch(\PDO::FETCH_ASSOC);
299
300
301
302
                        $user_id = $user_id['id'];
                    } else {
                        // Let's create the account first
                        $db->query("INSERT INTO users
Okhin's avatar
Okhin committed
303
304
305
306
307
308
309
310
                            (email,
                            pseudo,
                            cumul,
                            total)
                            VALUES('".\Utils::asl($email)."',
                                '".\Utils::asl($pseudo)."',
                                '".\Utils::asl($somme)."',
                                '0')");
311
312
313
                        $user_id = $db->lastInsertId();
                    }
                    $db->query("INSERT INTO dons
Okhin's avatar
Okhin committed
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
                        (datec,
                        user_id,
                        somme,
                        lang,
                        abo,
                        public,
                        status)
                        VALUES
                        (now(),
                            '".\Utils::asl($user_id)."',
                            '".\Utils::asl($somme)."',
                            '".\Utils::asl($lang)."',
                            '".\Utils::asl($abo)."',
                            '".\Utils::asl($public)."',
                            1)");
329
330
331
332
333
                    // Récupération de l'id pour passage en mode édition
                    $id = $db->lastInsertId();
                } else {
                    // We will get the ids associated to the gift first
                    $result = $db->query("SELECT adresse_id,
Okhin's avatar
Okhin committed
334
335
336
                        user_id
                        FROM dons
                        WHERE id = ".\Utils::asl($f3->get('POST.id')));
337
                    $result = $result->fetch(\PDO::FETCH_ASSOC);
338
339
                    // Let's update the adresse first or create the adress
                    $db->query("UPDATE users
Okhin's avatar
Okhin committed
340
341
342
                        SET email='".\Utils::asl($email)."',
                        pseudo='".\Utils::asl($pseudo)."'
                        WHERE id={$result['user_id']}");
343
                    $db->query("UPDATE dons
Okhin's avatar
Okhin committed
344
345
346
347
348
349
                        SET somme='".\Utils::asl($somme)."',
                        lang='".\Utils::asl($lang)."',
                        abo='".\Utils::asl($abo)."',
                        public='".\Utils::asl($public)."', 
                        adresse_id='".$result['adresse_id']."'
                        WHERE id='".\Utils::asl($_POST['id'])."'");
350
351
352
353
354
355
356
                }
            }
        }

        if ($id!='') {
            // Récupération du don, de l'adresse et de l'utilisateur
            $result = $db->query("SELECT d.id AS id,
Okhin's avatar
Okhin committed
357
358
359
360
361
362
363
364
365
366
367
                u.email AS email,
                u.pseudo AS pseudo,
                d.somme AS somme,
                d.lang AS lang,
                d.abo AS abo,
                d.public AS public,
                d.adresse_id AS adresse_id,
                d.user_id AS user_id
                FROM dons d
                JOIN users u ON u.id = d.user_id
                WHERE d.id=".$id);
368
            $don = $result->fetch(\PDO::FETCH_ASSOC);
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
        } else {
            $don = array(
                'id'=>$id,
                'email'=>$email,
                'pseudo'=>$pseudo,
                'somme'=>$somme,
                'lang'=>$lang,
                'abo'=>$abo,
                'public'=>$public,
                'user_id'=>$user_id,
            );
        }

        $f3->set('id', $id);
        $f3->set('msg', $msg);
        $f3->set('don', $don);

        $f3->set('block_content', 'backend/don.html');
    }

    public function counterparts_dashboard($f3, $args)
    {
        $db = $f3->get('DB');
392
        $f3->set('counterparts_import', '');
Okhin's avatar
Okhin committed
393
        $total = array("hoodie" => 0, "pishirt" => 0, "pibag" => 0, "piplome" => 0);
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429

        if ($f3->get('VERB') == 'POST') {
            $separator = ';';
            // Si on a un POST sur cette page, c'est que l'on a un csv à parser
            if ($_FILES['file']['tmp_name'] == '') {
                $f3->push('SESSION.error', 'Veuillez uploader un fichier au format csv');
            } else {
                // Lecture du fichier
                if (($handle = fopen($_FILES['file']['tmp_name'], "r")) !== false) {
                    // D'abord le header : email / quoi
                    $fields = fgetcsv($handle, 1000, $separator);
                    $mail_idx = -1;
                    $quoi_idx = -1;
                    foreach ($fields as $key => $value) {
                        if ($value == "Quoi") {
                            $quoi_idx = $key;
                        }
                        if ($value == "Mail") {
                            $mail_idx = $key;
                        }
                    }

                    // On tourne sur le fichier maintenant
                    while (($data = fgetcsv($handle, 1000, $separator)) !== false) {
                        // Récupération des données
                        $mail = $data[$mail_idx];
                        $quoi = $data[$quoi_idx];

                        // On cherche l'utilisateur
                        $user = $f3->get('container')['user_finder']->findByEmail($mail);
                        // Et son addresse
                        $adresse = $f3->get('container')['address_finder']->findByUserId($user['id']);

                        if (count($user) >= 1 and is_array($user)) {
                            // On a un utilisateur, cool
                            // On crée une contrepartie, si son cumul est suffisant
Okhin's avatar
Okhin committed
430
431
                            $needed = 0;

432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
                            switch ($quoi) {
                            case 'hoodie':
                                $needed = 250;
                                break;
                            case 'pishirt':
                                $needed = 100;
                                break;
                            case 'pibag':
                                $needed = 50;
                                break;
                            case 'piplome':
                                $needed = 30;
                                break;
                            }
                            if ($user['cumul'] >= $needed) {
                                // Assez de point, on crée
448
                                // On récupère le dernier ID inséré
449
                                $parent = $f3->get('container')['counterpart_finder']->getNextInsertedId();
450
                                $f3->get('container')['command_handler']->handle(new CounterpartCreateCommand($adresse['id'], $user['id'], $quoi, 1, 2, date("Y-m-d H:i:s"), 'Imported from a file', $parent));
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
                                // On mets à jour le cumul de l'utilisateur
                                $f3->get('container')['command_handler']->handle(new UserUpdateCumulCommand($user['id'], $user['cumul'] - $needed));
                                $total[$quoi] +=1;
                            } else {
                                $f3->push('SESSION.error', 'Pas assez de cumul pour un '.$quoi.' avec l\'email'.$mail);
                            }
                        } else {
                            $f3->push('SESSION.error', 'Pas d\'utilisateur avec l\'adresse '.$mail);
                        }
                    }
                    fclose($handle);
                }
            } // Fin de l'import CSV
            $f3->set('counterparts_import', $total);
        }

467
468
469
470
        // On définit des valeurs par défaut
        $query = "SELECT DISTINCT quoi FROM contreparties;";
        $result = $db->query($query);
        if ($result) {
471
            foreach ($result->fetchAll(\PDO::FETCH_ASSOC) as $row) {
472
473
474
475
476
477
                $f3->set($row['quoi'].'_s1', 0);
                $f3->set($row['quoi'].'_s2', 0);
            }
        }
        // Calcul des quantités
        $query = "SELECT quoi,
Okhin's avatar
Okhin committed
478
479
480
481
            status,
            COUNT(status) AS qte
            FROM contreparties
            GROUP BY quoi, status;";
482
483
        $result = $db->query($query);
        if ($result) {
484
            foreach ($result->fetchAll(\PDO::FETCH_ASSOC) as $row) {
485
486
487
488
489
490
491
492
493
                $f3->set($row['quoi'].'_s'.$row['status'], $row['qte']);
            }
        }
        // Calcul des pishirts et hoodies par taille
        for ($i=1;$i<9;$i++) {
            $f3->set('pishirt_t'.$i, 0);
            $f3->set('hoodie_t'.$i, 0);
        }
        $query = "SELECT quoi,
Okhin's avatar
Okhin committed
494
495
496
497
            taille,
            COUNT(taille) AS qte
            FROM contreparties
            GROUP BY quoi, taille;";
498
499
        $result = $db->query($query);
        if ($result) {
500
            foreach ($result->fetchAll(\PDO::FETCH_ASSOC) as $row) {
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
                $f3->set($row['quoi'].'_t'.$row['taille'], $row['qte']);
            }
        }
        $f3->set('block_content', 'backend/contreparties_tableau.html');
    }

    public function counterparts($f3, $args)
    {
        $export = false;
        $texte = '';
        $pibag = '';
        $piplome = '';
        $pishirt = '';
        $hoodie = '';
        $pseudo = '';
        $quoi = 0;
Okhin's avatar
Okhin committed
517
518
        $limite = 50;
        $page = 1;
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
        $status = '';
        $db = $f3->get('DB');
        if ($f3->get('VERB')=='POST') {
            if ($f3->exists('POST.action')) {
                switch ($f3->get('POST.action')) {
                case 'asked':
                    $db->query("UPDATE contreparties SET status=1 WHERE id=".$f3->get('POST.id')."");
                    break;
                case 'sent':
                    $db->query("UPDATE contreparties SET status=2 WHERE id=".$f3->get('POST.id')."");
                    break;
                case 'npai':
                    $db->query("UPDATE contreparties SET status=3 WHERE id=".$f3->get('POST.id')."");
                    break;
                case 'canceled':
                    $db->query("UPDATE contreparties SET status=99 WHERE id=".$f3->get('POST.id')."");
                    break;
                case 'export':
                    $export = true;
                    break;
                case 'pdf':
                    $db->query("UPDATE dons SET pdf='' WHERE id=".$f3->get('POST.id')."");
                    break;
                }
            }
            $texte = $f3->get('POST.texte');
            $pibag = $f3->get('POST.pibag');
            $piplome = $f3->get('POST.piplome');
            $pishirt = $f3->get('POST.pishirt');
            $hoodie = $f3->get('POST.hoodie');
            $quoi = $quoi + $pibag + $piplome + $pishirt + $hoodie;
Okhin's avatar
Okhin committed
550
551
            $limite = $f3->exists('POST.limite') ? (int) $f3->get('POST.limite') : 50;
            $page = $f3->exists('POST.page') ? (int) $f3->get('POST.page') : 1;
552
553
554
555
            $status = $f3->get('POST.status');
        }

        $query = "SELECT c.id,
Okhin's avatar
Okhin committed
556
557
558
559
560
            c.datec as datec,
            c.quoi as quoi,
            c.taille as taille,
            c.status as status,
            c.user_id as user_id,
561
            c.parent as parent,
Okhin's avatar
Okhin committed
562
563
564
565
566
567
568
569
570
571
            u.email as email,
            u.pseudo as pseudo,
            u.commentaire as commentaire,
            a.nom as nom,
            a.adresse as adresse,
            a.adresse2 as adresse2,
            a.codepostal as codepostal,
            a.ville as ville,
            a.pays as pays
            FROM contreparties c
572
573
            LEFT OUTER JOIN users u ON c.user_id = u.id
            LEFT OUTER JOIN adresses a ON u.id = a.user_id
Okhin's avatar
Okhin committed
574
            WHERE 1=1 ";
575
576
        if ($texte) {
            $query .= " AND (c.id like '%".$texte."%'
Okhin's avatar
Okhin committed
577
578
579
580
581
582
583
584
                OR u.pseudo like '%".$texte."%'
                OR u.email like '%".$texte."%'
                OR a.nom like '%".$texte."%'
                OR a.ville like '%".$texte."%'
                OR a.pays like '%".$texte."%'
                OR a.codepostal like '%".$texte."%'
                OR a.adresse2 like '%".$texte."%'
                OR a.adresse like '%".$texte."%')";
585
586
587
588
589
        }
        switch ($quoi) {
        case 0:
            break;
        case 1:
590
            $query .= " AND c.quoi='".\Utils::asl('pibag')."' ";
591
592
            break;
        case 2:
593
            $query .= " AND c.quoi='".\Utils::asl('piplome')."' ";
594
595
            break;
        case 4:
596
            $query .= " AND c.quoi='".\Utils::asl('pishirt')."' ";
597
598
            break;
        case 8:
599
            $query .= " AND c.quoi='".\Utils::asl('hoodie')."' ";
600
601
602
            break;
        default:
            $query .= " AND datec IN (
Okhin's avatar
Okhin committed
603
604
605
606
607
608
609
610
611
612
613
614
615
                SELECT datec
                FROM contreparties
                WHERE status=1
                GROUP BY user_id, datec
                HAVING SUM(
                    CASE quoi
                    WHEN 'pibag' THEN 1
                    WHEN 'piplome' THEN 2
                    WHEN 'pishirt' THEN 4
                    WHEN 'hoodie' THEN 8
                    END
                )=$quoi)
";
616
617
        }
        if ($status) {
618
            $query .= " AND c.status IN (".\Utils::asl($status).") ";
619
        }
Okhin's avatar
Okhin committed
620
621
622
        $first = ($page - 1) * $limite;
        $last = $page * $limite;
        $query .= " ORDER BY c.datec DESC LIMIT $first, $last;";
623
624
625
626
        $result = $db->query($query);
        $data = array();
        if ($result) {
            $i = 0;
627
            foreach ($result->fetchAll(\PDO::FETCH_ASSOC) as $row) {
628
629
                if ($row['quoi']=='piplome') {
                    $pdf = $db->query("SELECT d.id AS id,
Okhin's avatar
Okhin committed
630
                        a.nom AS pseudo,
631
                        d.pdf AS pdf
Okhin's avatar
Okhin committed
632
633
634
                        FROM dons d
                        JOIN adresses a ON a.id = d.adresse_id
                        WHERE d.id='".$row['taille']."'");
635
                    if ($pdf) {
636
                        $pdf = $pdf->fetch(\PDO::FETCH_ASSOC);
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
                    }
                    $row['pdf_id'] = $pdf['id'];
                    $row['pdf_nom'] = $pdf['pseudo'];
                    $row['pdf_url'] = $pdf['pdf'];
                } else {
                    $row['pdf_id'] = '';
                    $row['pdf_nom'] = '';
                    $row['pdf_url'] = '';
                }
                $data[] = $row;
                $i++;
            }
            $f3->set('row_count', $i);
        } else {
            $f3->set('row_count', 0);
        }

        $f3->set('texte', $texte);
        $f3->set('quoi', $quoi);
        $f3->set('pibag', $pibag);
        $f3->set('piplome', $piplome);
        $f3->set('pishirt', $pishirt);
        $f3->set('hoodie', $hoodie);
        $f3->set('limite', $limite);
Okhin's avatar
Okhin committed
661
        $f3->set('page', $page);
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
        $f3->set('status', $status);
        $f3->set('query', $query);
        $f3->set('data', $data);
        $f3->set('pseudo', $pseudo);
        $f3->set('PIPLOME_URL', PIPLOME_URL);

        if ($export) {
            $rows = array();
            $count = 1;
            foreach ($data as $row) {
                // Affichage de la taille de manière courte
                $taille = "";
                $atailles = $f3->get('TAILLES');
                if ($row['quoi']!="piplome" and $row['quoi']!="pibag") {
                    $row['taille'] = preg_replace('(Coupe |Taille |omme,|emme,)', '', $atailles[$row['taille']]);
                }
                if ($row['quoi']=="pibag") {
                    $row['taille'] = "N/A";
                }
                $row['count'] = $count++;
682
683
684
685
                $row['email'] = \Utils::clean_encoding($row['email']);
                $row['pseudo'] = \Utils::clean_encoding($row['pseudo']);
                $row['nom'] = \Utils::clean_encoding($row['nom']);
                $adresse = \Utils::clean_encoding($row['adresse']);
686
687
688
                $adresse = preg_replace('/\n|\r/', ' ', $adresse);
                $adresse = preg_replace('/^'.$row['nom'].'/', '', $adresse);
                $row['adresse'] = $adresse;
689
                $adresse2 = \Utils::clean_encoding($row['adresse2']);
690
691
692
                $adresse2 = preg_replace('/\n|\r/', ' ', $adresse2);
                $adresse2 = preg_replace('/^'.$row['nom'].'/', '', $adresse2);
                $row['adresse2'] = $adresse2;
693
694
695
696
697
698
699
700
                $row['codepostal'] = \Utils::clean_encoding($row['codepostal']);
                $row['ville'] = \Utils::clean_encoding($row['ville']);
                $row['pays'] = \Utils::clean_encoding($row['pays']);
                $row['datec'] = \Utils::clean_encoding($row['datec']);
                $row['taille'] = \Utils::clean_encoding($row['taille']);
                $row['pdf_id'] = \Utils::clean_encoding($row['pdf_id']);
                $row['pdf_nom'] = \Utils::clean_encoding($row['pdf_nom']);
                $row['commentaire'] = \Utils::clean_encoding($row['commentaire']);
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
                $rows[] = $row;
            }
            $f3->set('headers', array(
                "email",
                "pseudo",
                "nom",
                "adresse",
                "adresse2",
                "code postal",
                "ville",
                "pays",
                "demande",
                "taille",
                "piplome_id",
                "nom sur le piplome",
                "commentaire",
            ));
            $f3->set('rows', $rows);
            echo Template::instance()->render('export.csv', 'text/csv');
            die();
        } else {
            $f3->set('block_content', 'backend/contreparties.html');
        }
    }

    public function counterpart($f3, $args)
    {
        $id = '';
        $user_id = '';
        $quoi = '';
        $taille = '';
        $nom = '';
        $adresse = '';
        $adresse2 = '';
        $codepostal = '';
        $ville = '';
        $pays = '';
        $msg = '';
        $comment = '';
        $db = $f3->get('DB');
        if ($f3->get('VERB')=='GET') {
            if ($f3->exists('PARAMS.id')) {
                $id = $f3->get('PARAMS.id');
            }
        } else {
            $id = $f3->get('POST.id');
            $user_id = $f3->get('POST.user_id');
            $quoi = $f3->get('POST.quoi');
            $taille = $f3->get('POST.taille');
            $nom = $f3->get('POST.nom');
            $email = $f3->get('POST.email');
            $adresse = $f3->Get('POST.adresse');
            $adresse2 = $f3->Get('POST.adresse2');
            $codepostal = $f3->get('POST.codepostal');
            $ville = $f3->get('POST.ville');
            $pays = $f3->get('POST.pays');
            $status = $f3->get('POST.status');
            $comment = $f3->get('POST.commentaire');

            // Mise à jour de la contrepartie
            if ($id!='') {
                // First, let's retrieve the adresse from the adresse_id if it exists
                $result = $db->query("SELECT adresse_id
Okhin's avatar
Okhin committed
764
765
766
                    FROM contreparties
                    WHERE id = '".\Utils::asl($f3->get('POST.id'))."'
                    AND adresse_id IS NOT NULL");
767
                $result = $result->fetch(\PDO::FETCH_ASSOC);
768
769
770
771
772
                $adresse_id = $result['adresse_id'];

                if (!$adresse_id or $adresse_id == 0) {
                    // We need to create an adresse_id
                    $db->query("INSERT INTO adresses
Okhin's avatar
Okhin committed
773
774
775
776
777
778
                        VALUES (nom='".\Utils::asl($nom)."',
                            adresse='".\Utils::asl($adresse)."',
                            adresse2='".\Utils::asl($adresse2)."',
                            codepostal='".\Utils::asl($codepostal)."',
                            ville='".\Utils::asl($ville)."',
                            pays='".\Utils::asl($pays)."')");
779
                    $adresse_id = $db->lastInsertId();
780
781
782
783
                } else {
                    // Updating the adresses first
                    $db->query("UPDATE adresses
                        SET nom='".\Utils::asl($nom)."',
Okhin's avatar
Okhin committed
784
785
786
787
788
                        adresse='".\Utils::asl($adresse)."',
                        adresse2='".\Utils::asl($adresse2)."',
                        codepostal='".\Utils::asl($codepostal)."',
                        ville='".\Utils::asl($ville)."',
                        pays='".\Utils::asl($pays)."'
789
790
                        WHERE id = $adresse_id");
                };
791
                $db->query("UPDATE contreparties
Okhin's avatar
Okhin committed
792
793
794
795
                    SET quoi='".\Utils::asl($quoi)."',
                    taille='".\Utils::asl($taille)."',
                    status='".\Utils::asl($status)."'
                    WHERE id='".\Utils::asl($_POST['id'])."'");
796
                // We will update the comment
797
                $db->query("UPDATE users SET commentaire='".\Utils::asl($comment)."' WHERE id='".\Utils::asl($user_id)."'");
798
799
800
801
            }
        }

        if ($id!='') {
802
            $contrepartie = $f3->get('container')['counterpart_finder']->findById((int) $id);
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
        } else {
            $contrepartie = array(
                'id'=>$id,
                'user_id'=>$user_id,
                'quoi'=>$quoi,
                'taille'=>$taille,
                'nom'=>$nom,
                'adresse'=>$adresse,
                'adresse2'=>$adresse2,
                'codepostal'=>$codepostal,
                'ville'=>$ville,
                'pays'=>$pays,
                'status'=>$status,
                'commentaire'=>$comment,
            );
        }
        $pdfs = array();
        if ($contrepartie['quoi']=='piplome') {
            $result = $db->query("SELECT d.id AS id,
Okhin's avatar
Okhin committed
822
823
824
825
826
827
828
                d.pdf AS pdf,
                a.nom AS nom
                FROM dons d 
                INNER JOIN adresses a ON d.adresse_id = a.id
                WHERE pdf<>'' 
                AND d.status IN (1,4,101)
                AND d.user_id='".$contrepartie['user_id']."'");
829
            if ($result) {
830
                foreach ($result->fetchAll(\PDO::FETCH_ASSOC) as $pdf) {
831
832
833
834
835
836
837
838
839
840
841
842
843
                    $pdfs[] = $pdf;
                }
            }
        }
        $f3->set('pdfs', $pdfs);

        $f3->set('id', $id);
        $f3->set('msg', $msg);
        $f3->set('contrepartie', $contrepartie);

        $f3->set('block_content', 'backend/contrepartie.html');
    }

Okhin's avatar
Linting    
Okhin committed
844
845
    public function adresse($f3, $args)
    {
846
        $id = '';
847
        $form_name = '';
848
849
850
851
        $user_id = '';
        $db = $f3->get('DB');
        if ($f3->get('VERB') == 'POST') {
            $id = \Utils::asl($f3->get('POST.id'));
852
            $form_name = $f3->get('POST.action');
853
        }
854
855
856
857
858
859
860
861
        if ($form_name == 'delete') {
            $result = $db->query("SELECT user_id FROM adresses WHERE id = $id");
            $result = $result->fetch(\PDO::FETCH_ASSOC);
            $user_id = \Utils::asl($result["user_id"]);
            $db->query("UPDATE dons SET adresse_id = NULL
                WHERE user_id = $user_id");
            $db->query("UPDATE contreparties SET adresse_id = NULL
                WHERE user_id = $user_id");
Okhin's avatar
Linting    
Okhin committed
862
863
            $db->query(
                "DELETE FROM adresses
864
                WHERE id = $id"
865
        );
866
867
868
869
870
871
872
873
874
875
        } else {
            $result = $db->query("SELECT user_id FROM adresses WHERE id = $id");
            $result = $result->fetch(\PDO::FETCH_ASSOC);
            $user_id = \Utils::asl($result["user_id"]);
            $nom = \Utils::asl($f3->get('POST.nom'));
            $adresse = \Utils::asl($f3->get('POST.adresse'));
            $adresse2 = \Utils::asl($f3->get('POST.adresse2'));
            $codepostal = \Utils::asl($f3->get('POST.codepostal'));
            $ville = \Utils::asl($f3->get('POST.ville'));
            $pays = \Utils::asl($f3->get('POST.pays'));
Okhin's avatar
Linting    
Okhin committed
876
877
            $db->query(
                "UPDATE adresses SET
878
879
880
881
882
883
884
885
886
                nom = '".$nom."',
                adresse = '".$adresse."',
                adresse2 = '".$adresse2."',
                codepostal = '".$codepostal."',
                ville = '".$ville."',
                pays = '".$pays."'
                WHERE id = $id"
            );
        }
887
888
889
        $f3->reroute("/admin/users/$user_id");
    }

Okhin's avatar
Okhin committed
890
891
892
893
894
    public function user_merge($f3, $args)
    {
        $id = array_key_exists('id', $args) ? \Utils::asl($args['id']) : '';
        $f3->set('id', $id);

Okhin's avatar
linting    
Okhin committed
895
        if ($f3->get('VERB') == 'GET') {
Okhin's avatar
Okhin committed
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
            $merger = $f3->get('container')['user_finder']->findById($id);
            $email_merged = \Utils::asl($f3->get('GET.merged'));
            $user = $f3->get('container')['user_finder']->findById($id);
            $merged = $f3->get('container')['user_finder']->findByEmail($email_merged);
            // Now, we're going to list all pledges and counterparts of merged user
            // to display them to the admin.
            $counterparts = $f3->get('container')['counterpart_finder']->findByUserId($merged['id']);
            $donations = $f3->get('container')['donation_finder']->findByUserId($merged['id']);

            // Augment the context
            $f3->set('merger', $merger);
            $f3->set('merged', $merged);
            $f3->set('total', (int)$merged['total'] + (int)$merger['total']);
            $f3->set('cumul', (int)$merged['cumul'] + (int)$merger['cumul']);
            $f3->set('counterparts', $counterparts);
            $f3->set('donations', $donations);

            $f3->set('block_content', 'backend/fusion_user.html');
Okhin's avatar
linting    
Okhin committed
914
        } elseif ($f3->get('VERB') == 'POST') {
Okhin's avatar
Okhin committed
915
916
917
918
919
920
921
922
            $merger_id = \Utils::asl($f3->get('POST.merger_id'));
            $merged_id = \Utils::asl($f3->get('POST.merged_id'));

            // Retrieving the associated users
            $merger = $f3->get('container')['user_finder']->findById($merger_id);
            $merged = $f3->get('container')['user_finder']->findById($merged_id);

            // Update total and cumul values
Okhin's avatar
linting    
Okhin committed
923
924
            $f3->get('container')['command_handler']->handle(new UserUpdateByAdminCommand(
                $merger_id,
Okhin's avatar
Okhin committed
925
926
927
928
                $merger['pseudo'],
                $merger['email'],
                $merger['commentaire'] + $merged['commentaire'],
                (int) $merger['total'] + (int) $merged['total'],
Okhin's avatar
linting    
Okhin committed
929
930
                (int) $merger['cumul'] + (int) $merged['cumul']
            ));
Okhin's avatar
Okhin committed
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960

            $db = $f3->get('DB');
            // Now, let's change all counterparts of merged
            $db->query("UPDATE contreparties
                SET user_id = '" . $merger_id ."'
                WHERE user_id = '" . $merged_id ."'");

            // The donations
            $db->query("UPDATE dons
                SET user_id = '" . $merger_id ."'
                WHERE user_id = '" . $merged_id ."'");

            // The identifiers
            $db->query("UPDATE identifiers
                SET user_id = '" . $merger_id . "'
                WHERE user_id = '" . $merged_id . "'");

            // The adresses
            $db->query("UPDATE adresses
                SET user_id = '" . $merger_id . "'
                WHERE user_id = '" . $merged_id . "'");

            // Remove the merged user
            $db->query("DELETE FROM users WHERE id = '" . $merged_id . "'");

            // Reroute to the merger
            $f3->reroute('/admin/users/'.$merger_id);
        };
    }

961
962
    public function user($f3, $args)
    {
963
        $id = array_key_exists('id', $args) ? \Utils::asl($args['id']) : '';
964
965
966
        $f3->set('id', $id);

        if ($f3->get('VERB') == 'POST') {
967
            $pseudo = \Utils::asl($f3->get('POST.pseudo'));
968
969
            $email = $f3->get('POST.email');
            $commentaire = $f3->get('POST.commentaire');
970
971
            $total = $f3->get('POST.total', 0);
            $cumul = $f3->get('POST.cumul', 0);
972
973

            if ($id > 0) {
974
                $f3->get('container')['command_handler']->handle(new UserUpdateByAdminCommand($id, $pseudo, $email, $commentaire, $total, $cumul));
975
            } else {
976
                $password = hash('sha256', \Utils::generate_password());
977
978
979
980
981
982
983
984
985
986
987
988
                $db = $f3->get('DB');
                $db->query("INSERT INTO users
                    VALUES pseudo = '".$pseudo."',
                    hash = '".$hash."',
                    commentaire = '".$commentaire."',
                    email = '".$email."'");
                $id = $db->lastInsertId();
            }
        }

        // Let's get everything we have related to our user
        $user = $f3->get('container')['user_finder']->findById($id);
989
        $user['adresse'] = $f3->get('container')['address_finder']->findByUserId($user['id']);
990
991
992
993
994
995
996
997
998
999
1000
        $user['dons'] = $f3->get('container')['donation_finder']->findByUserId($user['id']);
        $user['contreparties'] = $f3->get('container')['counterpart_finder']->findByUserId($user['id']);

        $f3->set('user', $user);
        $f3->set('block_content', 'backend/user.html');
    }

    public function users($f3, $args)
    {
        $text = $f3->exists('POST.texte') ? $f3->get('POST.texte') : '';
        $status = $f3->exists('POST.status') ? $f3->get('POST.status') : '';
For faster browsing, not all history is shown. View entire blame