1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package Polls |
4
|
|
|
* @category modules |
5
|
|
|
* @author Nazar Mokrynskyi <[email protected]> |
6
|
|
|
* @license 0BSD |
7
|
|
|
*/ |
8
|
|
|
namespace cs\modules\Polls; |
9
|
|
|
use |
10
|
|
|
cs\Cache\Prefix, |
11
|
|
|
cs\Config, |
12
|
|
|
cs\CRUD_helpers, |
13
|
|
|
cs\Singleton; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @method static $this instance($check = false) |
17
|
|
|
*/ |
18
|
|
|
class Polls { |
19
|
|
|
use |
20
|
|
|
Common_actions, |
21
|
|
|
CRUD_helpers, |
22
|
|
|
Singleton; |
23
|
|
|
/** |
24
|
|
|
* @var Prefix |
25
|
|
|
*/ |
26
|
|
|
protected $cache; |
27
|
|
|
protected $data_model = [ |
28
|
|
|
'id' => 'int', |
29
|
|
|
'title' => 'ml:string' |
30
|
|
|
]; |
31
|
|
|
protected $table = '[prefix]polls'; |
32
|
|
|
protected $data_model_ml_group = 'Polls/polls'; |
33
|
|
|
|
34
|
|
|
protected function construct () { |
35
|
|
|
$this->cache = new Prefix('Polls'); |
36
|
|
|
} |
37
|
|
|
protected function cdb () { |
38
|
|
|
return Config::instance()->module('Polls')->db('polls'); |
39
|
|
|
} |
40
|
|
|
/** |
41
|
|
|
* Add new poll |
42
|
|
|
* |
43
|
|
|
* @param string $title |
44
|
|
|
* |
45
|
|
|
* @return false|int |
46
|
|
|
*/ |
47
|
|
|
public function add ($title) { |
48
|
|
|
$id = $this->create($title); |
|
|
|
|
49
|
|
|
if ($id) { |
50
|
|
|
unset($this->cache->all); |
|
|
|
|
51
|
|
|
return $id; |
52
|
|
|
} |
53
|
|
|
return false; |
54
|
|
|
} |
55
|
|
|
/** |
56
|
|
|
* Get poll |
57
|
|
|
* |
58
|
|
|
* @param int|int[] $id |
59
|
|
|
* |
60
|
|
|
* @return array|array[]|false |
61
|
|
|
*/ |
62
|
|
|
public function get ($id) { |
63
|
|
|
return $this->get_common($id); |
64
|
|
|
} |
65
|
|
|
/** |
66
|
|
|
* Get ids of add polls |
67
|
|
|
* |
68
|
|
|
* @return false|int[] |
69
|
|
|
*/ |
70
|
|
|
public function get_all () { |
71
|
|
|
return $this->cache->get( |
72
|
|
|
'all', |
73
|
|
|
function () { |
74
|
|
|
return $this->search([], 1, PHP_INT_MAX, 'id', false); |
75
|
|
|
} |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
/** |
79
|
|
|
* Set poll |
80
|
|
|
* |
81
|
|
|
* @param int $id |
82
|
|
|
* @param string $title |
83
|
|
|
* |
84
|
|
|
* @return bool |
85
|
|
|
*/ |
86
|
|
|
public function set ($id, $title) { |
87
|
|
|
$id = (int)$id; |
88
|
|
|
$result = $this->update($id, $title); |
|
|
|
|
89
|
|
|
if ($result) { |
90
|
|
|
unset( |
91
|
|
|
$this->cache->$id, |
92
|
|
|
$this->cache->all |
|
|
|
|
93
|
|
|
); |
94
|
|
|
return true; |
95
|
|
|
} |
96
|
|
|
return false; |
97
|
|
|
} |
98
|
|
|
/** |
99
|
|
|
* Del poll |
100
|
|
|
* |
101
|
|
|
* @param int $id |
102
|
|
|
* |
103
|
|
|
* @return bool |
104
|
|
|
*/ |
105
|
|
|
public function del ($id) { |
106
|
|
|
$id = (int)$id; |
107
|
|
|
if (!$this->delete($id)) { |
108
|
|
|
return false; |
109
|
|
|
} |
110
|
|
|
$Options = Options::instance(); |
111
|
|
|
$Options->del( |
112
|
|
|
$Options->get_all_for_poll($id) ?: [] |
113
|
|
|
); |
114
|
|
|
if (!$this->db_prime()->q( |
115
|
|
|
"DELETE FROM `[prefix]polls_options_answers` |
116
|
|
|
WHERE `id` = $id" |
117
|
|
|
) |
118
|
|
|
) { |
119
|
|
|
return false; |
120
|
|
|
} |
121
|
|
|
unset( |
122
|
|
|
$this->cache->all, |
|
|
|
|
123
|
|
|
$this->cache->$id, |
124
|
|
|
$this->cache->{"options/poll/$id"} |
125
|
|
|
); |
126
|
|
|
return true; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|