Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
channel.php
1<?php
8namespace Bitrix\Vote;
9use \Bitrix\Main\Entity;
10use \Bitrix\Main\Localization\Loc;
11use \Bitrix\Vote\Base\BaseObject;
12Loc::loadMessages(__FILE__);
13
29class ChannelTable extends Entity\DataManager
30{
36 public static function getTableName()
37 {
38 return 'b_vote_channel';
39 }
40
46 public static function getMap()
47 {
48 return array(
49 'ID' => array(
50 'data_type' => 'integer',
51 'primary' => true,
52 'autocomplete' => true,
53 'title' => Loc::getMessage('V_TABLE_FIELD_ID'),
54 ),
55 'SYMBOLIC_NAME' => array(
56 'data_type' => 'string',
57 'size' => 255,
58 'title' => Loc::getMessage('V_TABLE_FIELD_SYMBOLIC_NAME'),
59 ),
60 'TITLE' => array(
61 'data_type' => 'string',
62 'size' => 255,
63 'title' => Loc::getMessage('V_TABLE_FIELD_TITLE'),
64 ),
65 'C_SORT' => array(
66 'data_type' => 'integer',
67 'title' => Loc::getMessage('V_TABLE_FIELD_C_SORT'),
68 ),
69 'ACTIVE' => array(
70 'data_type' => 'boolean',
71 'values' => array('N', 'Y'),
72 'default_value' => 'Y',
73 'title' => Loc::getMessage('V_TABLE_FIELD_ACTIVE')
74 ),
75 'HIDDEN' => array(
76 'data_type' => 'boolean',
77 'values' => array('N', 'Y'),
78 'default_value' => 'N',
79 'title' => Loc::getMessage('V_TABLE_FIELD_HIDDEN')
80 ),
81 'TIMESTAMP_X' => array(
82 'data_type' => 'datetime',
83 'title' => Loc::getMessage('V_TABLE_FIELD_TIMESTAMP_X'),
84 ),
85 'VOTE_SINGLE' => array(
86 'data_type' => 'boolean',
87 'values' => array('N', 'Y'),
88 'default_value' => 'Y',
89 'title' => Loc::getMessage('V_TABLE_FIELD_ACTIVE')
90 ),
91 'USE_CAPTCHA' => array(
92 'data_type' => 'boolean',
93 'values' => array('N', 'Y'),
94 'default_value' => 'N',
95 'title' => Loc::getMessage('V_TABLE_FIELD_HIDDEN')
96 ),
97 'PERMISSION' => array(
98 'data_type' => '\Bitrix\Vote\ChannelGroupTable',
99 'reference' => array(
100 '=this.ID' => 'ref.CHANNEL_ID',
101 ),
102 'join_type' => 'INNER',
103 ),
104 'SITE' => array(
105 'data_type' => '\Bitrix\Vote\ChannelSiteTable',
106 'reference' => array(
107 '=this.ID' => 'ref.CHANNEL_ID',
108 ),
109 'join_type' => 'LEFT',
110 )
111 );
112 }
113}
114
125class ChannelGroupTable extends Entity\DataManager
126{
132 public static function getTableName()
133 {
134 return 'b_vote_channel_2_group';
135 }
136
142 public static function getMap()
143 {
144 return array(
145 'ID' => array(
146 'data_type' => 'integer',
147 'primary' => true,
148 'autocomplete' => true,
149 'title' => Loc::getMessage('V_TABLE_FIELD_ID'),
150 ),
151 'CHANNEL_ID' => array(
152 'data_type' => 'integer',
153 'title' => Loc::getMessage('V_TABLE_CHANNEL_ID'),
154 ),
155 'GROUP_ID' => array(
156 'data_type' => 'integer',
157 'title' => Loc::getMessage('V_TABLE_GROUP_ID'),
158 ),
159 'PERMISSION' => array(
160 'data_type' => 'enum',
161 'values' => array(1, 2, 3, 4),
162 'default_value' => 4,
163 'title' => Loc::getMessage('V_TABLE_PERMISSION')
164 )
165 );
166 }
167}
189class ChannelSiteTable extends Entity\DataManager
190{
196 public static function getTableName()
197 {
198 return 'b_vote_channel_2_site';
199 }
200
206 public static function getMap()
207 {
208 return array(
209 'CHANNEL_ID' => array(
210 'data_type' => 'integer',
211 'primary' => true,
212 'title' => Loc::getMessage('V_TABLE_FIELD_CHANNEL_ID'),
213 ),
214 'SITE_ID' => array(
215 'data_type' => 'string',
216 'primary' => true,
217 'size' => 2,
218 'title' => Loc::getMessage('V_TABLE_FIELD_SITE_ID'),
219 )
220 );
221 }
222}
223
224class Channel extends BaseObject implements \ArrayAccess
225{
226 static $channels = array();
227 private $data = array();
228
234 public function __construct($id)
235 {
236 if (!($id > 0))
237 throw new \Bitrix\Main\ArgumentNullException("id");
238 parent::__construct($id);
239 }
243 public function init()
244 {
245 if (($data = self::getById($this->id)->fetch()) && !empty($data))
246 $this->data = $data;
247 else
248 throw new \Bitrix\Main\ObjectNotFoundException(GetMessage("V_CHANNEL_IS_NOT_FOUND", "channel is not found"));
249 }
255 public static function getList(array $parameters)
256 {
257 global $CACHE_MANAGER;
258 $md5 = md5(serialize($parameters));
259 if (!array_key_exists($md5, self::$channels))
260 {
261 $data = array();
262 if (defined("VOTE_CACHE_TIME") && $CACHE_MANAGER->read(VOTE_CACHE_TIME, "b_vote_channel_".$md5, "b_vote_channel"))
263 $data = $CACHE_MANAGER->get("b_vote_channel_".$md5);
264 else
265 {
266 $db = ChannelTable::getList($parameters);
267 while ($r = $db->fetch())
268 $data[$r["ID"]] = $r;
269 if (defined("VOTE_CACHE_TIME"))
270 $CACHE_MANAGER->set("b_vote_channel_".$md5, $data);
271 }
272 self::$channels[$md5] = $data;
273 }
274
275 $db = new \CDBResult();
276 $db->initFromArray(self::$channels[$md5]);
277 return $db;
278 }
279
284 public static function getById($id)
285 {
286 return self::getList(array(
287 'select' => array("*"),
288 'filter' => array("ID" => $id),
289 ));
290 }
291
296 public function canRead($userId)
297 {
298 if (!parent::canEdit($userId))
299 {
300 $groups = parent::loadUserGroups($userId);
301 $dbRes = \Bitrix\Vote\Channel::getList(array(
302 'select' => array("*"),
303 'filter' => array(
304 "ACTIVE" => "Y",
305 "HIDDEN" => "N",
306 ">=PERMISSION.PERMISSION" => 1,
307 "PERMISSION.GROUP_ID" => $groups
308 ),
309 'order' => array(
310 'TITLE' => 'ASC'
311 ),
312 'group' => array("ID")
313 ));
314 while ($res = $dbRes->fetch())
315 {
316 if ($res["ID"] == $this->id)
317 return true;
318 }
319 return false;
320 }
321 return true;
322 }
323
328 public function canEdit($userId)
329 {
330 return parent::canEdit($userId);
331 }
336 public function canEditVote($userId)
337 {
338 if (!parent::canEdit($userId))
339 {
340 $groups = parent::loadUserGroups($userId);
341 $dbRes = \Bitrix\Vote\Channel::getList(array(
342 'select' => array("*"),
343 'filter' => array(
344 "ACTIVE" => "Y",
345 "HIDDEN" => "N",
346 ">=PERMISSION.PERMISSION" => 4,
347 "PERMISSION.GROUP_ID" => $groups
348 ),
349 'order' => array(
350 'TITLE' => 'ASC'
351 ),
352 'group' => array("ID")
353 ));
354 while ($res = $dbRes->fetch())
355 {
356 if ($res["ID"] == $this->id)
357 return true;
358 }
359 return false;
360 }
361 return true;
362 }
367 public function get($key)
368 {
369 return array_key_exists($key, $this->data) ? $this->data[$key] : null;
370 }
376 public function offsetExists($offset)
377 {
378 return array_key_exists($offset, $this->data);
379 }
384 public function offsetGet($offset)
385 {
386 return $this->data[$offset];
387 }
388
395 public function offsetSet($offset, $value)
396 {
397 throw new \Bitrix\Main\NotSupportedException('Model provide ArrayAccess only for reading');
398 }
403 public function offsetUnset($offset)
404 {
405 throw new \Bitrix\Main\NotSupportedException('Model provide ArrayAccess only for reading');
406 }
407}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
offsetUnset($offset)
Definition channel.php:403
offsetExists($offset)
Definition channel.php:376
static getById($id)
Definition channel.php:284
static getList(array $parameters)
Definition channel.php:255
canEditVote($userId)
Definition channel.php:336
offsetSet($offset, $value)
Definition channel.php:395