1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the TelegramBot package. |
5
|
|
|
* |
6
|
|
|
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Longman\TelegramBot; |
13
|
|
|
|
14
|
|
|
use Exception; |
15
|
|
|
use Longman\TelegramBot\Exception\TelegramException; |
16
|
|
|
use PDO; |
17
|
|
|
|
18
|
|
|
class ConversationDB extends DB |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Initialize conversation table |
22
|
|
|
*/ |
23
|
9 |
|
public static function initializeConversation(): void |
24
|
|
|
{ |
25
|
9 |
|
if (!defined('TB_CONVERSATION')) { |
26
|
1 |
|
define('TB_CONVERSATION', self::$table_prefix . 'conversation'); |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Select a conversation from the DB |
32
|
|
|
* |
33
|
|
|
* @param int $user_id |
34
|
|
|
* @param int $chat_id |
35
|
|
|
* @param int $limit |
36
|
|
|
* |
37
|
|
|
* @return array|bool |
38
|
|
|
* @throws TelegramException |
39
|
|
|
*/ |
40
|
9 |
|
public static function selectConversation(int $user_id, int $chat_id, $limit = 0) |
41
|
|
|
{ |
42
|
9 |
|
if (!self::isDbConnected()) { |
43
|
|
|
return false; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
try { |
47
|
|
|
$sql = ' |
48
|
|
|
SELECT * |
49
|
9 |
|
FROM `' . TB_CONVERSATION . '` |
50
|
|
|
WHERE `status` = :status |
51
|
|
|
AND `chat_id` = :chat_id |
52
|
|
|
AND `user_id` = :user_id |
53
|
|
|
'; |
54
|
|
|
|
55
|
9 |
|
if ($limit > 0) { |
56
|
9 |
|
$sql .= ' LIMIT :limit'; |
57
|
|
|
} |
58
|
|
|
|
59
|
9 |
|
$sth = self::$pdo->prepare($sql); |
60
|
|
|
|
61
|
9 |
|
$sth->bindValue(':status', 'active'); |
62
|
9 |
|
$sth->bindValue(':user_id', $user_id); |
63
|
9 |
|
$sth->bindValue(':chat_id', $chat_id); |
64
|
|
|
|
65
|
9 |
|
if ($limit > 0) { |
66
|
9 |
|
$sth->bindValue(':limit', $limit, PDO::PARAM_INT); |
67
|
|
|
} |
68
|
|
|
|
69
|
9 |
|
$sth->execute(); |
70
|
|
|
|
71
|
9 |
|
return $sth->fetchAll(PDO::FETCH_ASSOC); |
72
|
|
|
} catch (Exception $e) { |
73
|
|
|
throw new TelegramException($e->getMessage()); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Insert the conversation in the database |
79
|
|
|
* |
80
|
|
|
* @param int $user_id |
81
|
|
|
* @param int $chat_id |
82
|
|
|
* @param string $command |
83
|
|
|
* |
84
|
|
|
* @return bool |
85
|
|
|
* @throws TelegramException |
86
|
|
|
*/ |
87
|
6 |
|
public static function insertConversation(int $user_id, int $chat_id, string $command): bool |
88
|
|
|
{ |
89
|
6 |
|
if (!self::isDbConnected()) { |
90
|
|
|
return false; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
try { |
94
|
6 |
|
$sth = self::$pdo->prepare('INSERT INTO `' . TB_CONVERSATION . '` |
95
|
|
|
(`status`, `user_id`, `chat_id`, `command`, `notes`, `created_at`, `updated_at`) |
96
|
|
|
VALUES |
97
|
|
|
(:status, :user_id, :chat_id, :command, :notes, :created_at, :updated_at) |
98
|
|
|
'); |
99
|
|
|
|
100
|
6 |
|
$date = self::getTimestamp(); |
101
|
|
|
|
102
|
6 |
|
$sth->bindValue(':status', 'active'); |
103
|
6 |
|
$sth->bindValue(':command', $command); |
104
|
6 |
|
$sth->bindValue(':user_id', $user_id); |
105
|
6 |
|
$sth->bindValue(':chat_id', $chat_id); |
106
|
6 |
|
$sth->bindValue(':notes', '[]'); |
107
|
6 |
|
$sth->bindValue(':created_at', $date); |
108
|
6 |
|
$sth->bindValue(':updated_at', $date); |
109
|
|
|
|
110
|
6 |
|
return $sth->execute(); |
111
|
1 |
|
} catch (Exception $e) { |
112
|
1 |
|
throw new TelegramException($e->getMessage()); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Update a specific conversation |
118
|
|
|
* |
119
|
|
|
* @param array $fields_values |
120
|
|
|
* @param array $where_fields_values |
121
|
|
|
* |
122
|
|
|
* @return bool |
123
|
|
|
* @throws TelegramException |
124
|
|
|
*/ |
125
|
3 |
|
public static function updateConversation(array $fields_values, array $where_fields_values): bool |
126
|
|
|
{ |
127
|
|
|
// Auto update the update_at field. |
128
|
3 |
|
$fields_values['updated_at'] = self::getTimestamp(); |
129
|
|
|
|
130
|
3 |
|
return self::update(TB_CONVERSATION, $fields_values, $where_fields_values); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|