DonationFinder.php 2.57 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
<?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;
26
        return $this->connection->executeQuery("SELECT * FROM dons WHERE user_id=:userId", ["userId" => $userId])->fetchAll(\PDO::FETCH_ASSOC);
27 28
    }

Okhin's avatar
Okhin committed
29 30 31 32 33 34 35
    /**
     * findById
     *
     * @param int $donationId
     *
     * @return []
     */
36
    public function findById($did)
Okhin's avatar
Okhin committed
37
    {
38
        return $this->connection->executeQuery("SELECT * FROM dons WHERE id=:did", ["did" => $did])->fetch(\PDO::FETCH_ASSOC);
Okhin's avatar
Okhin committed
39
    }
40 41 42 43 44 45 46 47 48 49 50
    /**
     * 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
51
    public function adminSearch($text, $sum, $public, $status, $date1, $date2 = "2999-12-31 23:59:59", $limit = 50, $page = 1)
52 53 54 55 56 57 58 59 60
    {
        $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,
61
    u.pseudo AS pseudo,
62 63 64 65 66 67 68
    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
69
LEFT JOIN users u ON u.id = d.user_id
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
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
94

Okhin's avatar
Okhin committed
95 96 97 98 99
        if ('' !== $date1) {
            $query .= ' AND d.datec BETWEEN (:date1) AND (:date2) ';
            $params['date1'] = $date1;
            $params['date2'] = $date2;
        }
Okhin's avatar
Okhin committed
100 101 102
        $first = ($page - 1) * $limit;
        $last = ($page) * $limit;
        $query .= " ORDER BY datec DESC LIMIT $first, $last";
103 104 105 106

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