-
Notifications
You must be signed in to change notification settings - Fork 9
/
NewsletterSendedDB.php
169 lines (140 loc) · 4.33 KB
/
NewsletterSendedDB.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
158
159
160
161
162
163
164
165
166
167
168
169
<?php
// namespace FakeNPC\FlRuTelegramBot;
// namespace Longman\TelegramBot;
//use Exception;
use Longman\TelegramBot\Exception\TelegramException;
use Longman\TelegramBot\DB;
//use PDO;
class NewsletterSendedDB extends DB
{
/**
* Initialize newsletter_sended table
*/
public static function initializeNewsletterSended()
{
if (!defined('TB_NEWSLETTER_SENDED')) {
define('TB_NEWSLETTER_SENDED', self::$table_prefix . 'newsletter_sended');
}
}
/**
* Select a newsletter_sended from the DB
*
* @param int $id
* @param int $newsletter_id
* @param int $subscriber_id
* @param int|null $limit
*
* @return array|bool
* @throws TelegramException
*/
public static function selectNewsletterSended($id = null, $newsletter_id = null, $subscriber_id = null, $limit = null)
{
if (!self::isDbConnected()) {
return false;
}
try {
$sql = '
SELECT *
FROM `' . TB_NEWSLETTER_SENDED . '`
';
$where = array();
if($id !== null) {
if(is_array($id)) {
//
}
else {
$where[] = '`id` = :id';
}
}
if($newsletter_id !== null) {
$where[] = '`newsletter_id` = :newsletter_id';
}
if($subscriber_id !== null) {
$where[] = '`subscriber_id` = :subscriber_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_id !== null) {
$sth->bindValue(':newsletter_id', $newsletter_id, PDO::PARAM_INT);
}
if($subscriber_id !== null) {
$sth->bindValue(':subscriber_id', $subscriber_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 newsletter in the database
*
* @param string $name
* @param string $description
* @param int $sending_timestamp
* @param int $disabling_timestamp
* @param int $subscriber_id
*
* @return string last insert id
* @throws TelegramException
*/
public static function insertNewsletterSended($newsletter_id, $subscriber_id)
{
if (!self::isDbConnected()) {
return false;
}
try {
$sth = self::$pdo->prepare('INSERT INTO `' . TB_NEWSLETTER_SENDED . '`
(`newsletter_id`, `subscriber_id`)
VALUES
(:newsletter_id, :subscriber_id)
');
// $date = self::getTimestamp();
$sth->bindValue(':newsletter_id', $newsletter_id);
$sth->bindValue(':subscriber_id', $subscriber_id);
$sth->execute();
return self::$pdo->lastInsertId();
} 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 updateNewsletterSended(array $fields_values, array $where_fields_values)
{
return self::update(TB_NEWSLETTER_SENDED, $fields_values, $where_fields_values);
}
public function deleteNewsletter($id)
{
if (!self::isDbConnected()) {
return false;
}
try {
$sth = self::$pdo->prepare('DELETE FROM `' . TB_NEWSLETTER_SENDED . '`
WHERE `id` = :id
');
$sth->bindValue(':id', $id);
return $sth->execute();
} catch (Exception $e) {
throw new TelegramException($e->getMessage());
}
}
}