DonationFinder.php 2.1 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 46 47 48 49 50 51 52 53 54 55
<?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
     */
    public function adminSearch($text, $sum, $public, $status, $limit = 50)
    {
        $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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
    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
JOIN users u ON u.id = d.user_id
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;
        }
        $query .= " ORDER BY datec DESC LIMIT 0,$limit";

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