-
Notifications
You must be signed in to change notification settings - Fork 9
/
SubscriptionDB.php
157 lines (131 loc) · 4 KB
/
SubscriptionDB.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
// namespace FakeNPC\FlRuTelegramBot;
// namespace Longman\TelegramBot;
//use Exception;
use Longman\TelegramBot\Exception\TelegramException;
use Longman\TelegramBot\DB;
//use PDO;
class SubscriptionDB extends DB
{
/**
* Initialize subscription table
*/
public static function initializeSubscription()
{
if (!defined('TB_SUBSCRIPTION')) {
define('TB_SUBSCRIPTION', self::$table_prefix . 'subscription');
}
}
/**
* Select a subscriptions from the DB
*
* @param int $id
* @param int $sended
* @param int|null $limit
*
* @return array|bool
* @throws TelegramException
*/
public static function selectSubscription($id = null, $newsletter_category_id = null, $limit = null)
{
if (!self::isDbConnected()) {
return false;
}
try {
$sql = '
SELECT *
FROM `' . TB_SUBSCRIPTION . '`
';
$where = array();
if($id !== null) {
if(is_array($id)) {
//
}
else {
$where[] = '`id` = :id';
}
}
if ($newsletter_category_id !== null) {
$where[] = '`newsletter_category_id` = :newsletter_category_id';
}
if(count($where)) {
$sql .= ' WHERE '.join(' AND ', $where);
}
if ($limit !== null) {
$sql .= ' LIMIT :limit';
}
$sth = self::$pdo->prepare($sql);
if($id !== null) {
$sth->bindValue(':id', $id, PDO::PARAM_INT);
}
if($newsletter_category_id !== null) {
$sth->bindValue(':newsletter_category_id', $newsletter_category_id, PDO::PARAM_INT);
}
if ($limit !== null) {
$sth->bindValue(':limit', $limit, PDO::PARAM_INT);
}
$sth->execute();
return $sth->fetchAll(PDO::FETCH_ASSOC);
} catch (Exception $e) {
throw new TelegramException($e->getMessage());
}
}
/**
* Insert the subscription in the database
*
* @param int $id
* @param int $sended
*
* @return bool
* @throws TelegramException
*/
public static function insertSubscription($name, $newsletter_category_id, $duration, $price)
{
if (!self::isDbConnected()) {
return false;
}
try {
$sth = self::$pdo->prepare('INSERT INTO `' . TB_SUBSCRIPTION . '`
(`name`, `newsletter_category_id`, `duration`, `price`)
VALUES
(:name, :newsletter_category_id, :duration, :price)
');
// $date = self::getTimestamp();
$sth->bindValue(':name', $name);
$sth->bindValue(':newsletter_category_id', $newsletter_category_id);
$sth->bindValue(':duration', $duration);
$sth->bindValue(':price', $price);
return $sth->execute();
} catch (Exception $e) {
throw new TelegramException($e->getMessage());
}
}
/**
* Update a specific run
*
* @param array $fields_values
* @param array $where_fields_values
*
* @return bool
* @throws TelegramException
*/
public static function updateSubscription(array $fields_values, array $where_fields_values)
{
return self::update(TB_SUBSCRIPTION, $fields_values, $where_fields_values);
}
public function deleteSubscription($id)
{
if (!self::isDbConnected()) {
return false;
}
try {
$sth = self::$pdo->prepare('DELETE FROM `' . TB_SUBSCRIPTION . '`
WHERE `id` = :id
');
$sth->bindValue(':id', $id);
return $sth->execute();
} catch (Exception $e) {
throw new TelegramException($e->getMessage());
}
}
}