|
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 Options { |
|
19
|
|
|
use |
|
20
|
|
|
Common_actions, |
|
21
|
|
|
CRUD_helpers, |
|
22
|
|
|
Singleton; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var Prefix |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $cache; |
|
28
|
|
|
protected $data_model = [ |
|
29
|
|
|
'id' => 'int', |
|
30
|
|
|
'poll' => 'int', |
|
31
|
|
|
'title' => 'ml:string', |
|
32
|
|
|
'votes' => 'int' |
|
33
|
|
|
]; |
|
34
|
|
|
protected $table = '[prefix]polls_options'; |
|
35
|
|
|
protected $data_model_ml_group = 'Polls/options'; |
|
36
|
|
|
|
|
37
|
|
|
protected function construct () { |
|
38
|
|
|
$this->cache = new Prefix('Polls/options'); |
|
39
|
|
|
} |
|
40
|
|
|
protected function cdb () { |
|
41
|
|
|
return Config::instance()->module('Polls')->db('polls'); |
|
42
|
|
|
} |
|
43
|
|
|
/** |
|
44
|
|
|
* Add new option |
|
45
|
|
|
* |
|
46
|
|
|
* @param int $poll |
|
47
|
|
|
* @param string $title |
|
48
|
|
|
* |
|
49
|
|
|
* @return false|int |
|
50
|
|
|
*/ |
|
51
|
|
|
public function add ($poll, $title) { |
|
52
|
|
|
$id = $this->create($poll, $title, 0); |
|
|
|
|
|
|
53
|
|
|
if ($id) { |
|
54
|
|
|
unset($this->cache->{"poll/$poll"}); |
|
55
|
|
|
return $id; |
|
56
|
|
|
} |
|
57
|
|
|
return false; |
|
58
|
|
|
} |
|
59
|
|
|
/** |
|
60
|
|
|
* Get option |
|
61
|
|
|
* |
|
62
|
|
|
* @param int|int[] $id |
|
63
|
|
|
* |
|
64
|
|
|
* @return array|array[]|false |
|
65
|
|
|
*/ |
|
66
|
|
|
public function get ($id) { |
|
67
|
|
|
return $this->get_common($id); |
|
68
|
|
|
} |
|
69
|
|
|
/** |
|
70
|
|
|
* Set option |
|
71
|
|
|
* |
|
72
|
|
|
* @param $id |
|
73
|
|
|
* @param $poll |
|
74
|
|
|
* @param $title |
|
75
|
|
|
* |
|
76
|
|
|
* @return bool |
|
77
|
|
|
*/ |
|
78
|
|
|
public function set ($id, $poll, $title) { |
|
79
|
|
|
$id = (int)$id; |
|
80
|
|
|
$data = $this->get($id); |
|
81
|
|
|
if ($this->update($id, $poll, $title, $data['votes'])) { |
|
|
|
|
|
|
82
|
|
|
unset($this->cache->$id); |
|
83
|
|
|
return true; |
|
84
|
|
|
} |
|
85
|
|
|
return false; |
|
86
|
|
|
} |
|
87
|
|
|
/** |
|
88
|
|
|
* Del option |
|
89
|
|
|
* |
|
90
|
|
|
* @param int|int[] $id |
|
91
|
|
|
* |
|
92
|
|
|
* @return bool |
|
93
|
|
|
*/ |
|
94
|
|
|
public function del ($id) { |
|
95
|
|
|
$return = $this->delete($id); |
|
96
|
|
|
if ($return) { |
|
97
|
|
|
foreach (_int((array)$id) as $i) { |
|
98
|
|
|
unset($this->cache->$i); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
return $return; |
|
102
|
|
|
} |
|
103
|
|
|
/** |
|
104
|
|
|
* Update count of votes |
|
105
|
|
|
* |
|
106
|
|
|
* @param $id |
|
107
|
|
|
* |
|
108
|
|
|
* @return bool |
|
109
|
|
|
*/ |
|
110
|
|
|
public function update_votes ($id) { |
|
111
|
|
|
$id = (int)$id; |
|
112
|
|
|
$result = (bool)$this->db_prime()->q( |
|
113
|
|
|
"UPDATE `$this->table` |
|
114
|
|
|
SET `votes` = ( |
|
115
|
|
|
SELECT COUNT(`id`) |
|
116
|
|
|
FROM `[prefix]polls_options_answers` |
|
117
|
|
|
WHERE `option` = '%1\$d' |
|
118
|
|
|
) |
|
119
|
|
|
WHERE `id` = '%1\$d'", |
|
120
|
|
|
$id |
|
|
|
|
|
|
121
|
|
|
); |
|
122
|
|
|
if ($result) { |
|
123
|
|
|
unset($this->cache->$id); |
|
124
|
|
|
} |
|
125
|
|
|
return $result; |
|
126
|
|
|
} |
|
127
|
|
|
/** |
|
128
|
|
|
* Get id of all options for specified poll |
|
129
|
|
|
* |
|
130
|
|
|
* @param $poll |
|
131
|
|
|
* |
|
132
|
|
|
* @return false|int[] |
|
133
|
|
|
*/ |
|
134
|
|
|
public function get_all_for_poll ($poll) { |
|
135
|
|
|
$poll = (int)$poll; |
|
136
|
|
|
return $this->cache->get( |
|
137
|
|
|
"poll/$poll", |
|
138
|
|
|
function () use ($poll) { |
|
139
|
|
|
return $this->search(['poll' => $poll], 1, PHP_INT_MAX, 'id', true); |
|
140
|
|
|
} |
|
141
|
|
|
); |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|