From d1870e516eea5ced3cb9edb2d255b88e5633343b Mon Sep 17 00:00:00 2001 From: m Date: Mon, 2 Apr 2018 09:12:51 +0200 Subject: [PATCH] =?UTF-8?q?Adresse=20e-mail=20d'un=20sondage=20avec=20SMTP?= =?UTF-8?q?=20d=C3=A9sactiv=C3=A9=20https://framagit.org/framasoft/framada?= =?UTF-8?q?te/issues/301?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit retour pour recherche d'une autre solution Adresse e-mail d'un sondage avec SMTP désactivé https://framagit.org/framasoft/framadate/issues/301 CS Signed-off-by: Thomas Citharel --- .../Framadate/Repositories/PollRepository.php | 62 ++++++++++++------- 1 file changed, 41 insertions(+), 21 deletions(-) diff --git a/app/classes/Framadate/Repositories/PollRepository.php b/app/classes/Framadate/Repositories/PollRepository.php index 0b8167b..5a11c0b 100644 --- a/app/classes/Framadate/Repositories/PollRepository.php +++ b/app/classes/Framadate/Repositories/PollRepository.php @@ -79,7 +79,7 @@ class PollRepository extends AbstractRepository { } /** - * Search polls in databse. + * Search polls in database. * * @param array $search Array of search : ['id'=>..., 'title'=>..., 'name'=>..., 'mail'=>...] * @param int $start The number of first entry to select @@ -88,28 +88,48 @@ class PollRepository extends AbstractRepository { */ public function findAll($search, $start, $limit) { // Polls - $prepared = $this->prepare(' -SELECT p.*, - (SELECT count(1) FROM `' . Utils::table('vote') . '` v WHERE p.id=v.poll_id) votes - FROM `' . Utils::table('poll') . '` p - WHERE (:id = "" OR p.id LIKE :id) - AND (:title = "" OR p.title LIKE :title) - AND (:name = "" OR p.admin_name LIKE :name) - AND (:mail = "" OR p.admin_mail LIKE :mail) - ORDER BY p.title ASC - LIMIT :start, :limit - '); - - $poll = $search['poll'] . '%'; - $title = '%' . $search['title'] . '%'; - $name = '%' . $search['name'] . '%'; - $mail = '%' . $search['mail'] . '%'; - $prepared->bindParam(':id', $poll, PDO::PARAM_STR); - $prepared->bindParam(':title', $title, PDO::PARAM_STR); - $prepared->bindParam(':name', $name, PDO::PARAM_STR); - $prepared->bindParam(':mail', $mail, PDO::PARAM_STR); + + $request = ""; + $request .= "SELECT p.*,"; + $request .= " (SELECT count(1) FROM `" . Utils::table('vote') . "` v WHERE p.id=v.poll_id) votes"; + $request .= " FROM `" . Utils::table('poll') . "` p"; + $request .= " WHERE 1"; + + $values = []; + + if (!empty($search["poll"])) { + $request .= " AND p.id LIKE :poll"; + $values["poll"] = "{$search["poll"]}%"; + } + + $fields = [ + // key of $search => column name + "title" => "title", + "name" => "admin_name", + "mail" => "admin_mail", + ]; + + foreach ($fields as $searchKey => $columnName) { + if (empty($search[$searchKey])) { + continue; + } + + $request .= " AND p.$columnName LIKE :$searchKey"; + $values[$searchKey] = "%{$search[$searchKey]}%"; + } + + $request .= " ORDER BY p.title ASC"; + $request .= " LIMIT :start, :limit"; + + $prepared = $this->prepare($request); + + foreach ($values as $searchKey => $value) { + $prepared->bindParam(":$searchKey", $value, PDO::PARAM_STR); + } + $prepared->bindParam(':start', $start, PDO::PARAM_INT); $prepared->bindParam(':limit', $limit, PDO::PARAM_INT); + $prepared->execute(); return $prepared->fetchAll();