ConversationDB.php 5.25 KB
Newer Older
MBoretto's avatar
MBoretto committed
1 2 3 4 5 6 7 8 9 10 11 12 13
<?php
/**
 * This file is part of the TelegramBot package.
 *
 * (c) Avtandil Kikabidze aka LONGMAN <akalongman@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Longman\TelegramBot;

use Longman\TelegramBot\DB;
14
use Longman\TelegramBot\Exception\TelegramException;
MBoretto's avatar
MBoretto committed
15 16

/**
17
 * Class ConversationDB
MBoretto's avatar
MBoretto committed
18
 */
19
class ConversationDB extends DB
MBoretto's avatar
MBoretto committed
20 21
{
    /**
22
     * Initilize conversation table
MBoretto's avatar
MBoretto committed
23
     */
24
    public static function initializeConversation()
MBoretto's avatar
MBoretto committed
25
    {
26 27
        if (!defined('TB_CONVERSATION')) {
            define('TB_CONVERSATION', self::$table_prefix . 'conversation');
MBoretto's avatar
MBoretto committed
28 29
        }
    }
30

MBoretto's avatar
MBoretto committed
31
    /**
32
     * Select a conversation from the DB
MBoretto's avatar
MBoretto committed
33 34 35 36 37
     *
     * @param int  $user_id
     * @param int  $chat_id
     * @param bool $limit
     *
38
     * @return array|bool
MBoretto's avatar
MBoretto committed
39
     */
40
    public static function selectConversation($user_id, $chat_id, $limit = null)
MBoretto's avatar
MBoretto committed
41 42 43 44
    {
        if (!self::isDbConnected()) {
            return false;
        }
45

MBoretto's avatar
MBoretto committed
46
        try {
47
            $query = 'SELECT * FROM `' . TB_CONVERSATION . '` ';
48
            $query .= 'WHERE `status` = :status ';
MBoretto's avatar
MBoretto committed
49 50
            $query .= 'AND `chat_id` = :chat_id ';
            $query .= 'AND `user_id` = :user_id ';
51

MBoretto's avatar
MBoretto committed
52 53
            $tokens = [':chat_id' => $chat_id, ':user_id' => $user_id];
            if (!is_null($limit)) {
54
                $query .= ' LIMIT :limit';
MBoretto's avatar
MBoretto committed
55 56 57
            }
            $sth = self::$pdo->prepare($query);

58 59
            $active = 'active';
            $sth->bindParam(':status', $active, \PDO::PARAM_STR);
MBoretto's avatar
MBoretto committed
60 61 62 63 64 65 66
            $sth->bindParam(':user_id', $user_id, \PDO::PARAM_INT);
            $sth->bindParam(':chat_id', $chat_id, \PDO::PARAM_INT);
            $sth->bindParam(':limit', $limit, \PDO::PARAM_INT);
            $sth->execute();

            $results = $sth->fetchAll(\PDO::FETCH_ASSOC);

67
        } catch (\Exception $e) {
MBoretto's avatar
MBoretto committed
68 69 70 71 72 73
            throw new TelegramException($e->getMessage());
        }
        return $results;
    }

    /**
74
     * Insert the conversation in the database
MBoretto's avatar
MBoretto committed
75 76 77
     *
     * @param int    $user_id
     * @param int    $chat_id
78
     * @param string $command
MBoretto's avatar
MBoretto committed
79 80 81
     *
     * @return bool
     */
82
    public static function insertConversation($user_id, $chat_id, $command)
MBoretto's avatar
MBoretto committed
83 84 85 86 87 88
    {
        if (!self::isDbConnected()) {
            return false;
        }

        try {
89
            $sth = self::$pdo->prepare('INSERT INTO `' . TB_CONVERSATION . '`
MBoretto's avatar
MBoretto committed
90
                (
MBoretto's avatar
MBoretto committed
91
                `status`, `user_id`, `chat_id`, `command`, `notes`, `created_at`, `updated_at`
MBoretto's avatar
MBoretto committed
92 93
                )
                VALUES (
MBoretto's avatar
MBoretto committed
94
                :status, :user_id, :chat_id, :command, :notes, :date, :date
MBoretto's avatar
MBoretto committed
95 96
                )
               ');
97
            $active = 'active';
MBoretto's avatar
MBoretto committed
98 99
            //$notes = json_encode('');
            $notes = '""';
MBoretto's avatar
MBoretto committed
100 101
            $created_at = self::getTimestamp();

102
            $sth->bindParam(':status', $active);
103
            $sth->bindParam(':command', $command);
MBoretto's avatar
MBoretto committed
104 105
            $sth->bindParam(':user_id', $user_id);
            $sth->bindParam(':chat_id', $chat_id);
MBoretto's avatar
MBoretto committed
106
            $sth->bindParam(':notes', $notes);
MBoretto's avatar
MBoretto committed
107 108 109
            $sth->bindParam(':date', $created_at);

            $status = $sth->execute();
110
        } catch (\Exception $e) {
MBoretto's avatar
MBoretto committed
111 112 113 114 115 116
            throw new TelegramException($e->getMessage());
        }
        return $status;
    }

    /**
117
     * Update a specific conversation
MBoretto's avatar
MBoretto committed
118 119 120 121 122 123
     *
     * @param array $fields_values
     * @param array $where_fields_values
     *
     * @return bool
     */
124
    public static function updateConversation(array $fields_values, array $where_fields_values)
MBoretto's avatar
MBoretto committed
125
    {
126
        return self::update(TB_CONVERSATION, $fields_values, $where_fields_values);
MBoretto's avatar
MBoretto committed
127 128 129
    }

    /**
130
     * Update the conversation in the database
MBoretto's avatar
MBoretto committed
131
     *
132 133 134
     * @param string $table
     * @param array  $fields_values
     * @param array  $where_fields_values
MBoretto's avatar
MBoretto committed
135
     *
136
     * @todo This function is generic should be moved in DB.php
MBoretto's avatar
MBoretto committed
137 138 139 140 141 142 143 144 145 146 147 148 149 150
     *
     * @return bool
     */
    public static function update($table, array $fields_values, array $where_fields_values)
    {
        if (!self::isDbConnected()) {
            return false;
        }
        //Auto update the field update_at
        $fields_values['updated_at'] = self::getTimestamp();

        //Values
        $update = '';
        $tokens = [];
151
        $tokens_counter = 0;
MBoretto's avatar
MBoretto committed
152 153 154 155 156 157
        $a = 0;
        foreach ($fields_values as $field => $value) {
            if ($a) {
                $update .= ', ';
            }
            ++$a;
158
            ++$tokens_counter;
159 160
            $update .= '`' . $field . '` = :' . $tokens_counter;
            $tokens[':' . $tokens_counter] = $value;
MBoretto's avatar
MBoretto committed
161 162 163 164 165 166 167
        }

        //Where
        $a = 0;
        $where  = '';
        foreach ($where_fields_values as $field => $value) {
            if ($a) {
168
                $where .= ' AND ';
MBoretto's avatar
MBoretto committed
169 170
            } else {
                ++$a;
171
                $where .= 'WHERE ';
MBoretto's avatar
MBoretto committed
172
            }
173
            ++$tokens_counter;
LONGMAN's avatar
LONGMAN committed
174
            $where .= '`' . $field .'`= :' . $tokens_counter;
175
            $tokens[':' . $tokens_counter] = $value;
MBoretto's avatar
MBoretto committed
176 177
        }

178
        $query = 'UPDATE `' . $table . '` SET ' . $update . ' ' . $where;
MBoretto's avatar
MBoretto committed
179 180 181
        try {
            $sth = self::$pdo->prepare($query);
            $status = $sth->execute($tokens);
182
        } catch (\Exception $e) {
MBoretto's avatar
MBoretto committed
183 184 185 186 187
            throw new TelegramException($e->getMessage());
        }
        return $status;
    }
}