DonationFinder.php 2.41 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 35 36 37 38 39 40 41 42 43 44 45
<?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;
    }

    /**
     * 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
46
    public function adminSearch($text, $sum, $public, $status, $date1, $date2 = "2999-12-31 23:59:59", $limit = 50, $page = 1)
47 48 49 50 51 52 53 54 55
    {
        $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,
56
    u.pseudo AS pseudo,
57 58 59 60 61 62 63
    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
64
LEFT JOIN users u ON u.id = d.user_id
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
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
89

Okhin's avatar
Okhin committed
90 91 92 93 94
        if ('' !== $date1) {
            $query .= ' AND d.datec BETWEEN (:date1) AND (:date2) ';
            $params['date1'] = $date1;
            $params['date2'] = $date2;
        }
Okhin's avatar
Okhin committed
95

Okhin's avatar
Okhin committed
96 97 98
        $first = ($page - 1) * $limit;
        $last = ($page) * $limit;
        $query .= " ORDER BY datec DESC LIMIT $first, $last";
99 100 101 102

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