DonationFinder.php 2.84 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php

namespace LQDN\Finder;

use Doctrine\DBAL\Connection;

class DonationFinder
{
    private $connection;

    public function __construct(Connection $connection)
    {
        $this->connection = $connection;
    }

    /**
     * findByUserId
     *
     * @param int $userId
     *
     * @return []
     */
    public function findByUserId($userId)
    {
        $userId = (int) $userId;
        $donations = [];
        $stmt = $this->connection->query("SELECT * FROM dons WHERE user_id='$userId'");
        while ($donation = $stmt->fetch()) {
            $donations[$donation['id']] = $donation;
        }

        return $donations;
    }

Okhin's avatar
Okhin committed
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
    /**
     * findById
     *
     * @param int $donationId
     *
     * @return []
     */
    public function findById($donationId)
    {
        $donationId = (int) $donationId;
        $donations = [];
        $stmt = $this->connection->query("SELECT * FROM dons WHERE id='$donationId'");
        while ($donation = $stmt->fetch()) {
            $donations[$donationId] = $donation;
        }

        return $donations;
    }
53
54
55
56
57
58
59
60
61
62
63
    /**
     * Return dons for admins.
     *
     * @param string $text
     * @param int $sum
     * @param mixed $public
     * @param status $status
     * @param int $limit
     *
     * @return array
     */
Okhin's avatar
Okhin committed
64
    public function adminSearch($text, $sum, $public, $status, $date1, $date2 = "2999-12-31 23:59:59", $limit = 50, $page = 1)
65
66
67
68
69
70
71
72
73
    {
        $query = <<<EOQ
SELECT d.id as id,
    d.datec AS datec,
    d.somme AS somme,
    d.pdf AS pdf,
    d.status AS status,
    d.user_id AS user_id,
    u.email AS email,
74
    u.pseudo AS pseudo,
75
76
77
78
79
80
81
    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 dons d
Okhin's avatar
Okhin committed
82
LEFT JOIN users u ON u.id = d.user_id
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
LEFT JOIN adresses a ON d.adresse_id = a.id
WHERE 1=1
EOQ;

        $params = [];

        if ('' !== $text) {
            $query .= " AND (d.id like :text OR u.email like :text OR a.nom like :text OR a.adresse like :text OR a.adresse2 like :text)";
            $params['text'] = "%$text%";
        }

        if ('' !== $sum) {
            $query .= ' AND d.somme = :sum';
            $params['sum'] = $sum;
        }

        if ('' !== $public) {
            $query .= ' AND d.public = :public';
            $params['public'] = $public;
        }
        if ('' !== $status) {
            $query .= ' AND d.status IN (:status) ';
            $params['status'] = $status;
        }
Okhin's avatar
Okhin committed
107

Okhin's avatar
Okhin committed
108
109
110
111
112
        if ('' !== $date1) {
            $query .= ' AND d.datec BETWEEN (:date1) AND (:date2) ';
            $params['date1'] = $date1;
            $params['date2'] = $date2;
        }
Okhin's avatar
Okhin committed
113
114
115
        $first = ($page - 1) * $limit;
        $last = ($page) * $limit;
        $query .= " ORDER BY datec DESC LIMIT $first, $last";
116
117
118
119

        return $this->connection->fetchAll($query, $params);
    }
}