Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
notifyemail.php
1<?php
2namespace Bitrix\Idea;
3
6
7Loc::loadMessages(__FILE__);
8
9class NotifyEmailTable extends Entity\DataManager
10{
11 const SUBSCRIBE_TYPE_ALL = 'ALL';
12 const SUBSCRIBE_TYPE_NEW_IDEAS = 'NEW IDEAS';
13 const ENTITY_TYPE_IDEA = 'IDEA';
14 const ENTITY_TYPE_CATEGORY = 'CATEGORY';
20 public static function getFilePath()
21 {
22 return __FILE__;
23 }
24
30 public static function getTableName()
31 {
32 return 'b_idea_email_subscribe';
33 }
34
40 public static function getMap()
41 {
42 return array(
43 'USER_ID' => array(
44 'data_type' => 'integer',
45 'primary' => true,
46 'title' => Loc::getMessage('IDEA_NOTIFY_EMAIL_USER_ID'),
47 ),
48 'SUBSCRIBE_TYPE' => array(
49 'data_type' => 'enum',
50 'required' => true,
51 'values' => array(self::SUBSCRIBE_TYPE_ALL, self::SUBSCRIBE_TYPE_NEW_IDEAS),
52 'title' => Loc::getMessage('IDEA_NOTIFY_EMAIL_SUBSCRIBE_TYPE')
53 ),
54 'ENTITY_TYPE' => array(
55 'data_type' => 'enum',
56 'primary' => true,
57 'values' => array(self::ENTITY_TYPE_IDEA, self::ENTITY_TYPE_CATEGORY),
58 'title' => Loc::getMessage('IDEA_NOTIFY_EMAIL_ENTITY_TYPE')
59 ),
60 'ENTITY_CODE' => array(
61 'data_type' => 'string',
62 'primary' => true,
63
64 'title' => Loc::getMessage('IDEA_NOTIFY_EMAIL_ENTITY_CODE')
65 ),
66 'USER' => array(
67 'data_type' => 'Bitrix\Main\User',
68 'reference' => array(
69 '=this.USER_ID' => 'ref.ID'
70 ),
71 ),
72 'ASCENDED_CATEGORIES' => array(
73 'data_type' => 'Bitrix\Iblock\Section',
74 'reference' => array(
75 '=this.ENTITY_TYPE' => array('?', 'CATEGORY'),
76 '=this.ENTITY_CODE' => 'ref.CODE'
77 ),
78 )
79 );
80 }
81}
82
84{
85 protected static $cache = array();
86 protected $IblockID = null;
87 protected $userID = null;
88
89 function __construct($IblockID = null)
90 {
91 if ($IblockID > 0)
92 {
93 $this->IblockID = $IblockID;
94 \CIdeaManagment::getInstance()->idea()->setCategoryListId($IblockID);
95 }
96 else
97 $this->IblockID = \CIdeaManagment::getInstance()->idea()->getCategoryListID();
98 global $USER;
99 $this->userID = $USER->getID();
100 }
101
102 protected function getCacheId($params = array())
103 {
104 if (array_key_exists("IDEA", $params))
105 {
106 $id = "IDEA_".$params["IDEA"];
107 }
108 else
109 {
110 $id = "CATEGORY_".$this->IblockID.(empty($params["CATEGORY"]) ? "" : "_".$params["CATEGORY"]);
111 }
112
113 return $id;
114 }
115
116 public function addCategory($category, $subscribeType = NotifyEmailTable::SUBSCRIBE_TYPE_NEW_IDEAS)
117 {
118 if ($this->IblockID > 0 && $this->userID > 0)
119 {
120 $db_res = NotifyEmailTable::getList(array(
121 "filter" => array(
122 "USER_ID" => $this->userID,
124 "ENTITY_CODE" => (empty($category) ? "" : $category)
125 )));
126 if ($db_res->getSelectedRowsCount() <= 0)
127 {
128 $db_res = NotifyEmailTable::add(array(
129 "USER_ID" => $this->userID,
132 "ENTITY_CODE" => (empty($category) ? "" : $category)
133 ));
134 }
135 return $db_res;
136 }
137 return false;
138 }
139
140 public function deleteCategory($category)
141 {
142 if ($this->userID > 0)
143 {
144 return NotifyEmailTable::delete(array(
145 "USER_ID" => $this->userID,
147 "ENTITY_CODE" => (empty($category) ? "" : $category)
148 ));
149 }
150 return false;
151 }
152
153 public function addIdea($id)
154 {
155 if ($this->userID > 0)
156 {
157 $db_res = NotifyEmailTable::getList(array(
158 "filter" => array(
159 "USER_ID" => $this->userID,
160 "ENTITY_TYPE" => NotifyEmailTable::ENTITY_TYPE_IDEA,
161 "ENTITY_CODE" => $id.""
162 )));
163 if (!(!!$db_res && ($res = $db_res->fetch()) && !empty($res)))
164 {
165 $db_res = NotifyEmailTable::add(array(
166 "USER_ID" => $this->userID,
167 "SUBSCRIBE_TYPE" => NotifyEmailTable::SUBSCRIBE_TYPE_ALL,
168 "ENTITY_TYPE" => NotifyEmailTable::ENTITY_TYPE_IDEA,
169 "ENTITY_CODE" => $id.""
170 ));
171 return $db_res;
172 }
173 return $res;
174 }
175 return false;
176 }
177
178 public function deleteIdea($id)
179 {
180
181 if ($this->userID > 0)
182 {
183 return NotifyEmailTable::delete(array(
184 "USER_ID" => $this->userID,
185 "ENTITY_TYPE" => NotifyEmailTable::ENTITY_TYPE_IDEA,
186 "ENTITY_CODE" => $id
187 ));
188 }
189 return false;
190 }
191
192 protected function checkCache($userId, $params = array())
193 {
194 if (!array_key_exists($userId, self::$cache))
195 self::$cache[$userId] = array();
196 $id = $this->getCacheId($params);
197 return (array_key_exists($id, self::$cache[$userId]) ? self::$cache[$userId][$id] : false);
198 }
199
200 protected function setCache($userId, $params = array(), $data = array())
201 {
202 if (!array_key_exists($userId, self::$cache))
203 self::$cache[$userId] = array();
204 $id = $this->getCacheId($params);
205 self::$cache[$userId][$id] = $data;
206 return true;
207 }
208
209 public function getAscendedCategories($category = null, $userId = null)
210 {
211 $return = false;
212 $userId = ($userId === null ? $this->userID : $userId);
213 if ($this->IblockID > 0 && $userId > 0)
214 {
215 $cache = $this->checkCache($userId, array("CATEGORY" => $category));
216 if (!!$cache)
217 {
218 $return = $cache;
219 }
220 else if (empty($category))
221 {
222 $return = array();
223 $db_res = NotifyEmailTable::getList(array(
224 "filter" => array(
225 "USER_ID" => $userId,
227 "=ENTITY_CODE" => NULL
228 )
229 ));
230 while ($res = $db_res->fetch())
231 array_push($return, $res);
232 }
233 else if (is_string($category) && ($categories = \CIdeaManagment::getInstance()->idea()->getCategoryList()) && !empty($categories))
234 {
235 $category = ToUpper($category);
236 if (array_key_exists($category, $categories))
237 {
238 $return = array();
239 $category = $categories[$category];
240 $db_res = NotifyEmailTable::getList(array(
241 "filter" => array(
242 "=USER_ID" => $userId,
244 "=ASCENDED_CATEGORIES.IBLOCK_ID" => \CIdeaManagment::getInstance()->idea()->getCategoryListID(),
245 "<=ASCENDED_CATEGORIES.DEPTH_LEVEL" => $category["DEPTH_LEVEL"],
246 "<=ASCENDED_CATEGORIES.LEFT_MARGIN" => $category["LEFT_MARGIN"],
247 ">=ASCENDED_CATEGORIES.RIGHT_MARGIN" => $category["RIGHT_MARGIN"]
248 )
249 ));
250 while ($res = $db_res->fetch())
251 array_push($return, $res);
252 }
253 }
254 $this->setCache($userId, array("CATEGORY" => $category), $return);
255 }
256 return $return;
257 }
258
259}
260?>
getCacheId($params=array())
setCache($userId, $params=array(), $data=array())
__construct($IblockID=null)
addCategory($category, $subscribeType=NotifyEmailTable::SUBSCRIBE_TYPE_NEW_IDEAS)
getAscendedCategories($category=null, $userId=null)
checkCache($userId, $params=array())
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29