Commit 8ee3030b authored by Jack'lul's avatar Jack'lul Committed by GitHub

Merge pull request #9 from noplanman/selectchats_channels_extended

Use array for select-parameters, instead of individual parameters.
parents 9f66ae0e 5b92dbdf
...@@ -35,7 +35,7 @@ class ChatsCommand extends AdminCommand ...@@ -35,7 +35,7 @@ class ChatsCommand extends AdminCommand
/** /**
* @var string * @var string
*/ */
protected $version = '1.1.0'; protected $version = '1.2.0';
/** /**
* @var bool * @var bool
...@@ -45,7 +45,7 @@ class ChatsCommand extends AdminCommand ...@@ -45,7 +45,7 @@ class ChatsCommand extends AdminCommand
/** /**
* Command execute method * Command execute method
* *
* @return mixed * @return \Longman\TelegramBot\Entities\ServerResponse
* @throws \Longman\TelegramBot\Exception\TelegramException * @throws \Longman\TelegramBot\Exception\TelegramException
*/ */
public function execute() public function execute()
...@@ -55,21 +55,18 @@ class ChatsCommand extends AdminCommand ...@@ -55,21 +55,18 @@ class ChatsCommand extends AdminCommand
$chat_id = $message->getChat()->getId(); $chat_id = $message->getChat()->getId();
$text = trim($message->getText(true)); $text = trim($message->getText(true));
$results = DB::selectChats( $results = DB::selectChats([
true, //Select groups (group chat) 'groups' => true,
true, //Select supergroups (super group chat) 'supergroups' => true,
true, //Select channels 'channels' => true,
true, //Select users (single chat) 'users' => true,
null, //'yyyy-mm-dd hh:mm:ss' date range from 'text' => ($text === '' || $text === '*') ? null : $text //Text to search in user/group name
null, //'yyyy-mm-dd hh:mm:ss' date range to ]);
null, //Specific chat_id to select
($text === '' || $text === '*') ? null : $text //Text to search in user/group name $user_chats = 0;
); $group_chats = 0;
$supergroup_chats = 0;
$user_chats = 0; $channel_chats = 0;
$group_chats = 0;
$super_group_chats = 0;
$channel_chats = 0;
if ($text === '') { if ($text === '') {
$text_back = ''; $text_back = '';
...@@ -102,7 +99,7 @@ class ChatsCommand extends AdminCommand ...@@ -102,7 +99,7 @@ class ChatsCommand extends AdminCommand
$text_back .= '- S ' . $chat->getTitle() . ' [' . $whois . ']' . PHP_EOL; $text_back .= '- S ' . $chat->getTitle() . ' [' . $whois . ']' . PHP_EOL;
} }
++$super_group_chats; ++$supergroup_chats;
} elseif ($chat->isGroupChat()) { } elseif ($chat->isGroupChat()) {
if ($text !== '') { if ($text !== '') {
$text_back .= '- G ' . $chat->getTitle() . ' [' . $whois . ']' . PHP_EOL; $text_back .= '- G ' . $chat->getTitle() . ' [' . $whois . ']' . PHP_EOL;
...@@ -119,14 +116,14 @@ class ChatsCommand extends AdminCommand ...@@ -119,14 +116,14 @@ class ChatsCommand extends AdminCommand
} }
} }
if (($user_chats + $group_chats + $super_group_chats) === 0) { if (($user_chats + $group_chats + $supergroup_chats) === 0) {
$text_back = 'No chats found..'; $text_back = 'No chats found..';
} else { } else {
$text_back .= PHP_EOL . 'Private Chats: ' . $user_chats; $text_back .= PHP_EOL . 'Private Chats: ' . $user_chats;
$text_back .= PHP_EOL . 'Groups: ' . $group_chats; $text_back .= PHP_EOL . 'Groups: ' . $group_chats;
$text_back .= PHP_EOL . 'Super Groups: ' . $super_group_chats; $text_back .= PHP_EOL . 'Super Groups: ' . $supergroup_chats;
$text_back .= PHP_EOL . 'Channels: ' . $channel_chats; $text_back .= PHP_EOL . 'Channels: ' . $channel_chats;
$text_back .= PHP_EOL . 'Total: ' . ($user_chats + $group_chats + $super_group_chats); $text_back .= PHP_EOL . 'Total: ' . ($user_chats + $group_chats + $supergroup_chats);
if ($text === '') { if ($text === '') {
$text_back .= PHP_EOL . PHP_EOL . 'List all chats: /' . $this->name . ' *' . PHP_EOL . 'Search for chats: /' . $this->name . ' <search string>'; $text_back .= PHP_EOL . PHP_EOL . 'List all chats: /' . $this->name . ' *' . PHP_EOL . 'Search for chats: /' . $this->name . ' <search string>';
......
...@@ -38,7 +38,7 @@ class SendtoallCommand extends AdminCommand ...@@ -38,7 +38,7 @@ class SendtoallCommand extends AdminCommand
/** /**
* @var string * @var string
*/ */
protected $version = '1.3.0'; protected $version = '1.4.0';
/** /**
* @var bool * @var bool
...@@ -48,7 +48,7 @@ class SendtoallCommand extends AdminCommand ...@@ -48,7 +48,7 @@ class SendtoallCommand extends AdminCommand
/** /**
* Execute command * Execute command
* *
* @return boolean * @return \Longman\TelegramBot\Entities\ServerResponse
* @throws \Longman\TelegramBot\Exception\TelegramException * @throws \Longman\TelegramBot\Exception\TelegramException
*/ */
public function execute() public function execute()
...@@ -64,12 +64,12 @@ class SendtoallCommand extends AdminCommand ...@@ -64,12 +64,12 @@ class SendtoallCommand extends AdminCommand
$results = Request::sendToActiveChats( $results = Request::sendToActiveChats(
'sendMessage', //callback function to execute (see Request.php methods) 'sendMessage', //callback function to execute (see Request.php methods)
['text' => $text], //Param to evaluate the request ['text' => $text], //Param to evaluate the request
true, //Send to groups (group chat) [
true, //Send to super groups chats (super group chat) 'groups' => true,
false, //Select channels 'supergroups' => true,
true, //Send to users (single chat) 'channels' => false,
null, //'yyyy-mm-dd hh:mm:ss' date range from 'users' => true,
null //'yyyy-mm-dd hh:mm:ss' date range to ]
); );
$total = 0; $total = 0;
......
...@@ -42,7 +42,7 @@ class WhoisCommand extends AdminCommand ...@@ -42,7 +42,7 @@ class WhoisCommand extends AdminCommand
/** /**
* @var string * @var string
*/ */
protected $version = '1.2.0'; protected $version = '1.3.0';
/** /**
* @var bool * @var bool
...@@ -52,7 +52,7 @@ class WhoisCommand extends AdminCommand ...@@ -52,7 +52,7 @@ class WhoisCommand extends AdminCommand
/** /**
* Command execute method * Command execute method
* *
* @return mixed * @return \Longman\TelegramBot\Entities\ServerResponse
* @throws \Longman\TelegramBot\Exception\TelegramException * @throws \Longman\TelegramBot\Exception\TelegramException
*/ */
public function execute() public function execute()
...@@ -89,30 +89,25 @@ class WhoisCommand extends AdminCommand ...@@ -89,30 +89,25 @@ class WhoisCommand extends AdminCommand
$result = null; $result = null;
if (is_numeric($text)) { if (is_numeric($text)) {
$results = DB::selectChats( $results = DB::selectChats([
true, //Select groups 'groups' => true,
true, //Select super groups 'supergroups' => true,
true, //Select channels 'channels' => true,
true, //Select users 'users' => true,
null, //'yyyy-mm-dd hh:mm:ss' date range from 'chat_id' => $user_id, //Specific chat_id to select
null, //'yyyy-mm-dd hh:mm:ss' date range to ]);
$user_id //Specific chat_id to select
);
if (!empty($results)) { if (!empty($results)) {
$result = reset($results); $result = reset($results);
} }
} else { } else {
$results = DB::selectChats( $results = DB::selectChats([
true, //Select groups 'groups' => true,
true, //Select super groups 'supergroups' => true,
true, //Select channels 'channels' => true,
true, //Select users 'users' => true,
null, //'yyyy-mm-dd hh:mm:ss' date range from 'text' => $text //Text to search in user/group name
null, //'yyyy-mm-dd hh:mm:ss' date range to ]);
null, //Specific chat_id to select
$text //Text to search in user/group name
);
if (is_array($results) && count($results) === 1) { if (is_array($results) && count($results) === 1) {
$result = reset($results); $result = reset($results);
......
...@@ -979,35 +979,32 @@ class DB ...@@ -979,35 +979,32 @@ class DB
} }
/** /**
* Select Group and/or single Chats * Select Groups, Supergroups, Channels and/or single user Chats (also by ID or text)
* *
* @param bool $select_groups * @param $select_chats_params
* @param bool $select_super_groups
* @param bool $select_channels
* @param bool $select_users
* @param string $date_from
* @param string $date_to
* @param int $chat_id
* @param string $text
* *
* @return array|bool (Selected chats or false if invalid arguments) * @return array|bool
* @throws \Longman\TelegramBot\Exception\TelegramException * @throws TelegramException
*/ */
public static function selectChats( public static function selectChats($select_chats_params)
$select_groups = true, {
$select_super_groups = true,
$select_channels = true,
$select_users = true,
$date_from = null,
$date_to = null,
$chat_id = null,
$text = null
) {
if (!self::isDbConnected()) { if (!self::isDbConnected()) {
return false; return false;
} }
if (!$select_groups && !$select_users && !$select_super_groups) { // Set defaults for omitted values.
$select = array_merge([
'groups' => true,
'supergroups' => true,
'channels' => true,
'users' => true,
'date_from' => null,
'date_to' => null,
'chat_id' => null,
'text' => null,
], $select_chats_params);
if (!$select['groups'] && !$select['users'] && !$select['supergroups']) {
return false; return false;
} }
...@@ -1019,7 +1016,7 @@ class DB ...@@ -1019,7 +1016,7 @@ class DB
' . 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`
'; ';
if ($select_users) { if ($select['users']) {
$query .= ' $query .= '
, ' . TB_USER . '.`id` AS `user_id` , ' . TB_USER . '.`id` AS `user_id`
FROM `' . TB_CHAT . '` FROM `' . TB_CHAT . '`
...@@ -1034,34 +1031,34 @@ class DB ...@@ -1034,34 +1031,34 @@ class DB
$where = []; $where = [];
$tokens = []; $tokens = [];
if (!$select_groups || !$select_users || !$select_super_groups) { if (!$select['groups'] || !$select['users'] || !$select['supergroups']) {
$chat_or_user = []; $chat_or_user = [];
$select_groups && $chat_or_user[] = TB_CHAT . '.`type` = "group"'; $select['groups'] && $chat_or_user[] = TB_CHAT . '.`type` = "group"';
$select_super_groups && $chat_or_user[] = TB_CHAT . '.`type` = "supergroup"'; $select['supergroups'] && $chat_or_user[] = TB_CHAT . '.`type` = "supergroup"';
$select_channels && $chat_or_user[] = TB_CHAT . '.`type` = "channel"'; $select['channels'] && $chat_or_user[] = TB_CHAT . '.`type` = "channel"';
$select_users && $chat_or_user[] = TB_CHAT . '.`type` = "private"'; $select['users'] && $chat_or_user[] = TB_CHAT . '.`type` = "private"';
$where[] = '(' . implode(' OR ', $chat_or_user) . ')'; $where[] = '(' . implode(' OR ', $chat_or_user) . ')';
} }
if (null !== $date_from) { if (null !== $select['date_from']) {
$where[] = TB_CHAT . '.`updated_at` >= :date_from'; $where[] = TB_CHAT . '.`updated_at` >= :date_from';
$tokens[':date_from'] = $date_from; $tokens[':date_from'] = $select['date_from'];
} }
if (null !== $date_to) { if (null !== $select['date_to']) {
$where[] = TB_CHAT . '.`updated_at` <= :date_to'; $where[] = TB_CHAT . '.`updated_at` <= :date_to';
$tokens[':date_to'] = $date_to; $tokens[':date_to'] = $select['date_to'];
} }
if (null !== $chat_id) { if (null !== $select['chat_id']) {
$where[] = TB_CHAT . '.`id` = :chat_id'; $where[] = TB_CHAT . '.`id` = :chat_id';
$tokens[':chat_id'] = $chat_id; $tokens[':chat_id'] = $select['chat_id'];
} }
if (null !== $text) { if (null !== $select['text']) {
if ($select_users) { if ($select['users']) {
$where[] = '( $where[] = '(
LOWER(' . TB_CHAT . '.`title`) LIKE :text LOWER(' . TB_CHAT . '.`title`) LIKE :text
OR LOWER(' . TB_USER . '.`first_name`) LIKE :text OR LOWER(' . TB_USER . '.`first_name`) LIKE :text
...@@ -1071,7 +1068,7 @@ class DB ...@@ -1071,7 +1068,7 @@ class DB
} else { } else {
$where[] = 'LOWER(' . TB_CHAT . '.`title`) LIKE :text'; $where[] = 'LOWER(' . TB_CHAT . '.`title`) LIKE :text';
} }
$tokens[':text'] = '%' . strtolower($text) . '%'; $tokens[':text'] = '%' . strtolower($select['text']) . '%';
} }
if (!empty($where)) { if (!empty($where)) {
......
...@@ -961,40 +961,30 @@ class Request ...@@ -961,40 +961,30 @@ class Request
/** /**
* Send message to all active chats * Send message to all active chats
* *
* @param string $callback_function * @param string $callback_function
* @param array $data * @param array $data
* @param boolean $send_groups * @param array $select_chats_params
* @param boolean $send_super_groups
* @param boolean $send_channels
* @param boolean $send_users
* @param string $date_from
* @param string $date_to
* *
* @return array * @return array
* @throws \Longman\TelegramBot\Exception\TelegramException * @throws TelegramException
*/ */
public static function sendToActiveChats( public static function sendToActiveChats(
$callback_function, $callback_function,
array $data, array $data,
$send_groups = true, array $select_chats_params
$send_super_groups = true,
$send_channels = false,
$send_users = true,
$date_from = null,
$date_to = null
) { ) {
$callback_path = __NAMESPACE__ . '\Request'; $callback_path = __NAMESPACE__ . '\Request';
if (!method_exists($callback_path, $callback_function)) { if (!method_exists($callback_path, $callback_function)) {
throw new TelegramException('Method "' . $callback_function . '" not found in class Request.'); throw new TelegramException('Method "' . $callback_function . '" not found in class Request.');
} }
$chats = DB::selectChats($send_groups, $send_super_groups, $send_channels, $send_users, $date_from, $date_to); $chats = DB::selectChats($select_chats_params);
$results = []; $results = [];
if (is_array($chats)) { if (is_array($chats)) {
foreach ($chats as $row) { foreach ($chats as $row) {
$data['chat_id'] = $row['chat_id']; $data['chat_id'] = $row['chat_id'];
$results[] = call_user_func_array($callback_path . '::' . $callback_function, [$data]); $results[] = call_user_func($callback_path . '::' . $callback_function, $data);
} }
} }
......
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