Tidy up selectChats method.

parent b1cceae8
...@@ -958,77 +958,68 @@ class DB ...@@ -958,77 +958,68 @@ class DB
} }
try { try {
$query = 'SELECT * , $query = '
SELECT * ,
' . TB_CHAT . '.`id` AS `chat_id`, ' . TB_CHAT . '.`id` AS `chat_id`,
' . TB_CHAT . '.`created_at` AS `chat_created_at`, ' . TB_CHAT . '.`created_at` AS `chat_created_at`,
' . TB_CHAT . '.`updated_at` AS `chat_updated_at` ' . TB_CHAT . '.`updated_at` AS `chat_updated_at`
' . ';
(($select_users) ? ', ' . TB_USER . '.`id` AS `user_id` FROM `' . TB_CHAT . '` LEFT JOIN `' . TB_USER . '` if ($select_users) {
ON ' . TB_CHAT . '.`id`=' . TB_USER . '.`id`' : 'FROM `' . TB_CHAT . '`'); $query .= '
, ' . TB_USER . '.`id` AS `user_id`
FROM `' . TB_CHAT . '`
LEFT JOIN `' . TB_USER . '`
ON ' . TB_CHAT . '.`id`=' . TB_USER . '.`id`
';
} else {
$query .= 'FROM `' . TB_CHAT . '`';
}
//Building parts of query //Building parts of query
$where = []; $where = [];
$tokens = []; $tokens = [];
if (!$select_groups || !$select_users || !$select_super_groups) { if (!$select_groups || !$select_users || !$select_super_groups) {
$chat_or_user = ''; $chat_or_user = [];
if ($select_groups) {
$chat_or_user .= TB_CHAT . '.`type` = "group"';
}
if ($select_super_groups) {
if (!empty($chat_or_user)) {
$chat_or_user .= ' OR ';
}
$chat_or_user .= TB_CHAT . '.`type` = "supergroup"';
}
if ($select_users) { $select_groups && $chat_or_user[] = TB_CHAT . '.`type` = "group"';
if (!empty($chat_or_user)) { $select_super_groups && $chat_or_user[] = TB_CHAT . '.`type` = "supergroup"';
$chat_or_user .= ' OR '; $select_users && $chat_or_user[] = TB_CHAT . '.`type` = "private"';
}
$chat_or_user .= TB_CHAT . '.`type` = "private"';
}
$where[] = '(' . $chat_or_user . ')'; $where[] = '(' . implode(' OR ', $chat_or_user) . ')';
} }
if (!is_null($date_from)) { if (null !== $date_from) {
$where[] = TB_CHAT . '.`updated_at` >= :date_from'; $where[] = TB_CHAT . '.`updated_at` >= :date_from';
$tokens[':date_from'] = $date_from; $tokens[':date_from'] = $date_from;
} }
if (!is_null($date_to)) { if (null !== $date_to) {
$where[] = TB_CHAT . '.`updated_at` <= :date_to'; $where[] = TB_CHAT . '.`updated_at` <= :date_to';
$tokens[':date_to'] = $date_to; $tokens[':date_to'] = $date_to;
} }
if (!is_null($chat_id)) { if (null !== $chat_id) {
$where[] = TB_CHAT . '.`id` = :chat_id'; $where[] = TB_CHAT . '.`id` = :chat_id';
$tokens[':chat_id'] = $chat_id; $tokens[':chat_id'] = $chat_id;
} }
if (!is_null($text)) { if (null !== $text) {
if ($select_users) { if ($select_users) {
$where[] = '(LOWER(' . TB_CHAT . '.`title`) LIKE :text OR LOWER(' . TB_USER . '.`first_name`) LIKE :text OR LOWER(' . TB_USER . '.`last_name`) LIKE :text OR LOWER(' . TB_USER . '.`username`) LIKE :text)'; $where[] = '(
LOWER(' . TB_CHAT . '.`title`) LIKE :text
OR LOWER(' . TB_USER . '.`first_name`) LIKE :text
OR LOWER(' . TB_USER . '.`last_name`) LIKE :text
OR LOWER(' . TB_USER . '.`username`) LIKE :text
)';
} else { } else {
$where[] = 'LOWER(' . TB_CHAT . '.`title`) LIKE :text'; $where[] = 'LOWER(' . TB_CHAT . '.`title`) LIKE :text';
} }
$tokens[':text'] = '%' . strtolower($text) . '%'; $tokens[':text'] = '%' . strtolower($text) . '%';
} }
$a = 0; if ($where) {
foreach ($where as $part) { $query .= ' WHERE ' . implode(' AND ', $where);
if ($a) {
$query .= ' AND ' . $part;
} else {
$query .= ' WHERE ' . $part;
++$a;
}
} }
$query .= ' ORDER BY ' . TB_CHAT . '.`updated_at` ASC'; $query .= ' ORDER BY ' . TB_CHAT . '.`updated_at` ASC';
...@@ -1036,11 +1027,9 @@ class DB ...@@ -1036,11 +1027,9 @@ class DB
$sth = self::$pdo->prepare($query); $sth = self::$pdo->prepare($query);
$sth->execute($tokens); $sth->execute($tokens);
$result = $sth->fetchAll(PDO::FETCH_ASSOC); return $sth->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) { } catch (PDOException $e) {
throw new TelegramException($e->getMessage()); throw new TelegramException($e->getMessage());
} }
return $result;
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment