Commit 1ca2887d authored by MBoretto's avatar MBoretto

bugfix conversation are not overwritten

parent 4a788a36
...@@ -101,7 +101,7 @@ class Conversation ...@@ -101,7 +101,7 @@ class Conversation
if ($this->is_fetched) { if ($this->is_fetched) {
return true; return true;
} }
//select an active conversation
$conversation = ConversationDB::selectConversation($this->user_id, $this->chat_id, 1); $conversation = ConversationDB::selectConversation($this->user_id, $this->chat_id, 1);
$this->is_fetched = true; $this->is_fetched = true;
...@@ -114,14 +114,14 @@ class Conversation ...@@ -114,14 +114,14 @@ class Conversation
return true; return true;
} }
//a track with the same name was already opened store the data //a track with the same name was already opened, store the data inside the class
if ($this->conversation['conversation_name'] == $this->group_name) { if ($this->conversation['conversation_name'] == $this->group_name) {
$this->data = json_decode($this->conversation['data'], true); $this->data = json_decode($this->conversation['data'], true);
return true; return true;
} }
//a with a differet name has been opened unsetting the DB one and reacreatea a new one //a conversation with a differet name has been opened unsetting the DB one and reacreatea a new one
ConversationDB::updateConversation(['is_active' => 0], ['chat_id' => $this->chat_id, 'user_id' => $this->user_id]); ConversationDB::updateConversation(['status' => 'cancelled'], ['chat_id' => $this->chat_id, 'user_id' => $this->user_id, 'status' => 'active']);
return false; return false;
} }
...@@ -156,7 +156,7 @@ class Conversation ...@@ -156,7 +156,7 @@ class Conversation
if ($this->conversationExist()) { if ($this->conversationExist()) {
$fields['data'] = json_encode($data); $fields['data'] = json_encode($data);
ConversationDB::updateConversation($fields, ['chat_id' => $this->chat_id, 'user_id' => $this->user_id]); ConversationDB::updateConversation($fields, ['chat_id' => $this->chat_id, 'user_id' => $this->user_id, 'status' => 'active']);
//TODO verify query success before convert the private var //TODO verify query success before convert the private var
$this->data = $data; $this->data = $data;
} }
...@@ -174,7 +174,7 @@ class Conversation ...@@ -174,7 +174,7 @@ class Conversation
public function stop() public function stop()
{ {
if ($this->conversationExist()) { if ($this->conversationExist()) {
ConversationDB::updateConversation(['is_active' => 0], ['chat_id' => $this->chat_id, 'user_id' => $this->user_id]); ConversationDB::updateConversation(['status' => 'stopped'], ['chat_id' => $this->chat_id, 'user_id' => $this->user_id, 'status' => 'active']);
} }
} }
......
...@@ -44,7 +44,7 @@ class ConversationDB extends DB ...@@ -44,7 +44,7 @@ class ConversationDB extends DB
try { try {
$query = 'SELECT * FROM `' . TB_CONVERSATION . '` '; $query = 'SELECT * FROM `' . TB_CONVERSATION . '` ';
$query .= 'WHERE `is_active` = 1 '; $query .= 'WHERE `status` = :status ';
$query .= 'AND `chat_id` = :chat_id '; $query .= 'AND `chat_id` = :chat_id ';
$query .= 'AND `user_id` = :user_id '; $query .= 'AND `user_id` = :user_id ';
...@@ -54,6 +54,8 @@ class ConversationDB extends DB ...@@ -54,6 +54,8 @@ class ConversationDB extends DB
} }
$sth = self::$pdo->prepare($query); $sth = self::$pdo->prepare($query);
$active = 'active';
$sth->bindParam(':status', $active, \PDO::PARAM_STR);
$sth->bindParam(':user_id', $user_id, \PDO::PARAM_INT); $sth->bindParam(':user_id', $user_id, \PDO::PARAM_INT);
$sth->bindParam(':chat_id', $chat_id, \PDO::PARAM_INT); $sth->bindParam(':chat_id', $chat_id, \PDO::PARAM_INT);
$sth->bindParam(':limit', $limit, \PDO::PARAM_INT); $sth->bindParam(':limit', $limit, \PDO::PARAM_INT);
...@@ -86,18 +88,17 @@ class ConversationDB extends DB ...@@ -86,18 +88,17 @@ class ConversationDB extends DB
try { try {
$sth = self::$pdo->prepare('INSERT INTO `' . TB_CONVERSATION . '` $sth = self::$pdo->prepare('INSERT INTO `' . TB_CONVERSATION . '`
( (
`is_active`, `conversation_command`, `conversation_name`, `user_id`, `chat_id`, `data`, `created_at`, `updated_at` `status`, `conversation_command`, `conversation_name`, `user_id`, `chat_id`, `data`, `created_at`, `updated_at`
) )
VALUES ( VALUES (
:is_active, :conversation_command, :conversation_name, :user_id, :chat_id, :data, :date, :date :status, :conversation_command, :conversation_name, :user_id, :chat_id, :data, :date, :date
) )
'); ');
$active = 'active';
$active = 1;
$data = json_encode(''); $data = json_encode('');
$created_at = self::getTimestamp(); $created_at = self::getTimestamp();
$sth->bindParam(':is_active', $active); $sth->bindParam(':status', $active);
$sth->bindParam(':conversation_command', $conversation_command); $sth->bindParam(':conversation_command', $conversation_command);
$sth->bindParam(':conversation_name', $conversation_group_name); $sth->bindParam(':conversation_name', $conversation_group_name);
$sth->bindParam(':user_id', $user_id); $sth->bindParam(':user_id', $user_id);
...@@ -147,14 +148,16 @@ class ConversationDB extends DB ...@@ -147,14 +148,16 @@ class ConversationDB extends DB
//Values //Values
$update = ''; $update = '';
$tokens = []; $tokens = [];
$tokens_counter = 0;
$a = 0; $a = 0;
foreach ($fields_values as $field => $value) { foreach ($fields_values as $field => $value) {
if ($a) { if ($a) {
$update .= ', '; $update .= ', ';
} }
++$a; ++$a;
$update .= '`'.$field.'` = :'.$a; ++$tokens_counter;
$tokens[':'.$a] = $value; $update .= '`'.$field.'` = :'.$tokens_counter;
$tokens[':'.$tokens_counter] = $value;
} }
//Where //Where
...@@ -167,7 +170,9 @@ class ConversationDB extends DB ...@@ -167,7 +170,9 @@ class ConversationDB extends DB
++$a; ++$a;
$where .= 'WHERE '; $where .= 'WHERE ';
} }
$where .= '`'.$field .'`='.$value ; ++$tokens_counter;
$where .= '`'.$field .'`= :'.$tokens_counter ;
$tokens[':'.$tokens_counter] = $value;
} }
$query = 'UPDATE `'.$table.'` SET '.$update.' '.$where; $query = 'UPDATE `'.$table.'` SET '.$update.' '.$where;
......
...@@ -30,7 +30,7 @@ class User extends Entity ...@@ -30,7 +30,7 @@ class User extends Entity
$this->first_name = isset($data['first_name']) ? $data['first_name'] : null; $this->first_name = isset($data['first_name']) ? $data['first_name'] : null;
if (empty($this->first_name)) { if (empty($this->first_name)) {
throw new TelegramException('first_name is empty!'); // throw new TelegramException('first_name is empty!');
} }
$this->last_name = isset($data['last_name']) ? $data['last_name'] : null; $this->last_name = isset($data['last_name']) ? $data['last_name'] : null;
......
...@@ -135,12 +135,11 @@ CREATE TABLE IF NOT EXISTS `telegram_update` ( ...@@ -135,12 +135,11 @@ CREATE TABLE IF NOT EXISTS `telegram_update` (
REFERENCES `chosen_inline_query` (`id`) REFERENCES `chosen_inline_query` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci; ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
CREATE TABLE IF NOT EXISTS `conversation` ( CREATE TABLE IF NOT EXISTS `conversation` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Row unique id', `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Row unique id',
`user_id` bigint NULL DEFAULT NULL COMMENT 'User id', `user_id` bigint NULL DEFAULT NULL COMMENT 'User id',
`chat_id` bigint NULL DEFAULT NULL COMMENT 'Telegram chat_id can be a the user id or the chat id ', `chat_id` bigint NULL DEFAULT NULL COMMENT 'Telegram chat_id can be a the user id or the chat id ',
`is_active` tinyint(1) NOT NULL DEFAULT '1' COMMENT '1 conversation is active 0 conversation has been deactivated', `status` ENUM('active', 'cancelled', 'stopped') NOT NULL DEFAULT 'active' COMMENT 'active conversation is active, cancelled conversation has been truncated before end, stopped conversation has end',
`conversation_command` varchar(160) DEFAULT '' COMMENT 'Default Command to execute', `conversation_command` varchar(160) DEFAULT '' COMMENT 'Default Command to execute',
`conversation_name` varchar(160) NOT NULL DEFAULT '' COMMENT 'Name of the conversation can be the command name or a generic name for conversation between multiple commands', `conversation_name` varchar(160) NOT NULL DEFAULT '' COMMENT 'Name of the conversation can be the command name or a generic name for conversation between multiple commands',
`data` varchar(1000) DEFAULT 'NULL' COMMENT 'Data stored from command', `data` varchar(1000) DEFAULT 'NULL' COMMENT 'Data stored from command',
...@@ -150,7 +149,7 @@ CREATE TABLE IF NOT EXISTS `conversation` ( ...@@ -150,7 +149,7 @@ CREATE TABLE IF NOT EXISTS `conversation` (
PRIMARY KEY (`id`), PRIMARY KEY (`id`),
KEY `user_id` (`user_id`), KEY `user_id` (`user_id`),
KEY `chat_id` (`chat_id`), KEY `chat_id` (`chat_id`),
KEY `is_active` (`is_active`), KEY `status` (`status`),
KEY `conversation_name` (`conversation_name`), KEY `conversation_name` (`conversation_name`),
FOREIGN KEY (`user_id`) FOREIGN KEY (`user_id`)
......
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