Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
controller.php
1<?php
2
4
13
14
15Loc::loadMessages(__FILE__);
16
18{
23
24 protected function listActions()
25 {
26 return array(
27 "vote" => array(
28 "need_auth" => false,
29 "method" => array("POST")
30 ),
31 "getBallot" => array(
32 "method" => array("POST", "GET")
33 ),
34 "stop" => array(
35 "method" => array("POST", "GET")
36 ),
37 "resume" => array(
38 "method" => array("POST", "GET")
39 ),
40 "getvoted" => array(
41 "method" => array("POST", "GET")
42 ),
43 "getmobilevoted" => array(
44 "method" => array("POST", "GET"),
45 "check_sessid" => false
46 ),
47 "exportXls" => array(
48 "method" => array("POST", "GET")
49 )
50 );
51 }
52
53 protected function init()
54 {
55 if ($this->request->getQuery("attachId"))
56 $this->attach = Manager::loadFromAttachId(intval($this->request->getQuery("attachId")));
57 else if ($this->request->getQuery("voteId"))
58 $this->attach = Manager::loadFromVoteId(array(
59 "MODULE_ID" => "vote",
60 "ENTITY_TYPE" => "VOTE",
61 "ENTITY_ID" => $this->request->getQuery("voteId")
62 ), $this->request->getQuery("voteId"));
63 else
64 throw new ArgumentNullException("attach ID");
65
66 AddEventHandler("vote", "onVoteReset", array(&$this, "clearCache"));
67 AddEventHandler("vote", "onAfterVoting", array(&$this, "clearCache"));
68 }
69
70
71 protected function processActionVote()
72 {
73 if ($this->checkRequiredGetParams(array("attachId")))
74 {
75 if (!$this->attach->canRead($this->getUser()->getId()))
76 throw new AccessDeniedException();
77
78 $request = $this->request->getPostList()->toArray();
79 if ($this->isAjaxRequest())
80 \CUtil::decodeURIComponent($request);
81
82 //TODO decide what should we do with captcha in attaches
83 if ($this->attach->voteFor($request))
84 {
85 if (\Bitrix\Main\Loader::includeModule("pull"))
86 {
87 $result = array();
88 foreach ($this->attach["QUESTIONS"] as $question)
89 {
90 $result[$question["ID"]] = array(
91 "ANSWERS" => array()
92 );
93 foreach ($question["ANSWERS"] as $answer)
94 {
95 $result[$question["ID"]]["ANSWERS"][$answer["ID"]] = array(
96 "PERCENT" => $answer["PERCENT"],
97 "USERS" => [],
98 "COUNTER" => $answer["COUNTER"]
99 );
100 }
101 }
102 \CPullWatch::AddToStack("VOTE_".$this->attach["VOTE_ID"],
103 Array(
104 "module_id" => "vote",
105 "command" => "voting",
106 "params" => Array(
107 "VOTE_ID" => $this->attach["VOTE_ID"],
108 "AUTHOR_ID" => $this->getUser()->getId(),
109 "COUNTER" => $this->attach["COUNTER"],
110 "QUESTIONS" => $result
111 )
112 )
113 );
114 }
115
116 $this->sendJsonSuccessResponse(array(
117 "action" => $this->getAction(),
118 "data" => array(
119 "attach" => array(
120 "ID" => $this->attach["ID"],
121 "VOTE_ID" => $this->attach["VOTE_ID"],
122 "COUNTER" => $this->attach["COUNTER"],
123 "QUESTIONS" => $this->attach["QUESTIONS"]
124 )
125 )
126 ));
127 }
128 elseif (($errors = $this->attach->getErrors()) && !empty($errors))
129 $this->errorCollection->add($errors);
130 else
131 throw new ArgumentException(GetMessage("V_ERROR_4501"));
132 }
133 }
134
135 protected function processActionGetBallot()
136 {
138 $eventId = 0;
139 $userId = 0;
140 if ($this->getUser()->isAdmin() && $this->request->getQuery("eventId") > 0)
141 $eventId = $this->request->getQuery("eventId");
142 else
143 {
144 $userId = $this->getUser()->getId();
145 if ($attach->canRead($userId) && ($result = $attach->canRevote($userId)) && $result->isSuccess())
146 {
147 $event = reset($result->getData());
148 $eventId = $event["ID"];
149 }
150 }
151 $stat = array();
152 $extras = array();
153 if ($eventId > 0)
154 {
155 $dbRes = EventTable::getList(array(
156 "select" => array(
157 "V_" => "*",
158 "Q_" => "QUESTION.*",
159 "A_" => "QUESTION.ANSWER.*",
160 "U_ID" => "USER.USER.ID",
161 "U_NAME" => "USER.USER.NAME",
162 "U_LAST_NAME" => "USER.USER.LAST_NAME",
163 "U_SECOND_NAME" => "USER.USER.SECOND_NAME",
164 "U_LOGIN" => "USER.USER.LOGIN",
165 "U_PERSONAL_PHOTO" => "USER.USER.PERSONAL_PHOTO",
166 ),
167 "filter" => array(
168 "ID" => $eventId,
169 "VOTE_ID" => $attach["VOTE_ID"]
170 )
171 ));
172 $questions = $attach["QUESTIONS"];
173 if ($dbRes && ($res = $dbRes->fetch()))
174 {
175 $userId = $res["U_ID"];
176 $extras = array(
177 "VISIBLE" => $res["V_VISIBLE"],
178 "VALID" => $res["V_VALID"]
179 );
180 do
181 {
182 if (!array_key_exists($res["Q_QUESTION_ID"], $questions) ||
183 !array_key_exists($res["A_ANSWER_ID"], $questions[$res["Q_QUESTION_ID"]]["ANSWERS"]))
184 continue;
185 if (!array_key_exists($res["Q_QUESTION_ID"], $stat))
186 $stat[$res["Q_QUESTION_ID"]] = array();
187
188 $stat[$res["Q_QUESTION_ID"]][$res["A_ANSWER_ID"]] = array(
189 "EVENT_ID" => $res["A_ID"],
190 "EVENT_QUESTION_ID" => $res["Q_ID"],
191 "ANSWER_ID" => $res["ANSWER_ID"],
192 "ID" => $res["A_ID"], // delete this
193 "MESSAGE" => $res["A_MESSAGE"]
194 );
195 } while ($res = $dbRes->fetch());
196 }
197 }
198 $this->sendJsonSuccessResponse(array(
199 "action" => $this->getAction(),
200 "data" => array(
201 "attach" => array(
202 "ID" => $attach["ID"],
203 "VOTE_ID" => $attach["VOTE_ID"],
204 "FIELD_NAME" => $attach["FIELD_NAME"],
205 "QUESTIONS" => $attach["QUESTIONS"]
206 ),
207 "event" => array(
208 "id" => $eventId,
209 "userId" => $userId,
210 "ballot" => $stat,
211 "extras" => $extras
212 )
213 )
214 ));
215 }
216
217 protected function processActionStop()
218 {
220 $userId = $this->getUser()->getId();
221 if (!$attach->canEdit($userId))
222 throw new AccessDeniedException();
223 $attach->stop();
224 $this->sendJsonSuccessResponse(array(
225 "action" => $this->getAction(),
226 "data" => array(
227 "attach" => array(
228 "ID" => $this->attach["ID"],
229 "VOTE_ID" => $this->attach["VOTE_ID"],
230 "COUNTER" => $this->attach["COUNTER"],
231 "QUESTIONS" => $this->attach["QUESTIONS"]
232 )
233 )
234 ));
235 }
236
237 protected function processActionResume()
238 {
240 $userId = $this->getUser()->getId();
241 if (!$attach->canEdit($userId))
242 throw new AccessDeniedException();
243 $attach->resume();
244 $this->sendJsonSuccessResponse(array(
245 "action" => $this->getAction(),
246 "data" => array(
247 "attach" => array(
248 "ID" => $this->attach["ID"],
249 "VOTE_ID" => $this->attach["VOTE_ID"],
250 "COUNTER" => $this->attach["COUNTER"],
251 "QUESTIONS" => $this->attach["QUESTIONS"]
252 )
253 )
254 ));
255 }
256
263 protected static function getVoted(array $cacheParams, array $pageParams)
264 {
265 $iNumPage = intval($pageParams["iNumPage"]);
266 $nPageSize = intval($pageParams["nPageSize"]);
267 $showAll = $pageParams["bShowAll"] === true;
268
269 $cache = new \CPHPCache();
270 $result = array(
271 "statusPage" => "done",
272 "items" => array(),
273 "hiddenItems" => 0
274 );
275
276 $cacheTtl = defined("BX_COMP_MANAGED_CACHE") ? 3153600 : 3600*4;
277 $cacheId = "voted_".serialize(array($cacheParams["answerId"], $nPageSize, $iNumPage));
278 $cacheDir = "/vote/".$cacheParams["voteId"]."/voted/";
279
280 if (\Bitrix\Main\Config\Option::get("main", "component_cache_on", "Y") == "Y" && $cache->initCache($cacheTtl, $cacheId, $cacheDir))
281 {
282 $result = $cache->getVars();
283 }
284 else
285 {
286 if ($iNumPage <= 1)
287 {
288 $res = EventTable::getList(array(
289 "select" => array(
290 "CNT" => "CNT"
291 ),
292 "runtime" => array(
293 new ExpressionField("CNT", "COUNT(*)")
294 ),
295 "filter" => array(
296 "VOTE_ID" => $cacheParams["voteId"],
297 "!=VISIBLE" => "Y",
298 "VALID" => "Y",
299 "QUESTION.ANSWER.ANSWER_ID" => $cacheParams["answerId"],
300 )
301 ))->fetch();
302 $result["hiddenItems"] = $res["CNT"];
303 }
304
305 $dbRes = \CVoteEvent::getUserAnswerStat(array(),
306 array(
307 "ANSWER_ID" => $cacheParams["answerId"],
308 "VALID" => "Y",
309 "VISIBLE" => "Y",
310 "bGetVoters" => "Y",
311 "bGetMemoStat" => "N"
312 ),
313 array(
314 "nPageSize" => $nPageSize,
315 "bShowAll" => $showAll,
316 "iNumPage" => $iNumPage
317 )
318 );
319 $userIds = array();
320 $result["statusPage"]= (($dbRes->NavPageNomer >= $dbRes->NavPageCount || $nPageSize > $dbRes->NavRecordCount) ? "done" : "continue");
321 while ($res = $dbRes->fetch())
322 $userIds[] = $res["AUTH_USER_ID"];
323
324 if (empty($userIds))
325 $result["statusPage"] = "done";
326 else
327 {
328 $departments = array();
329 if (IsModuleInstalled("extranet") &&
330 ($iblockId = \COption::GetOptionInt("intranet", "iblock_structure", 0)) &&
331 $iblockId > 0)
332 {
333 $dbRes = \CIBlockSection::GetList(
334 array("LEFT_MARGIN" => "DESC"),
335 array("IBLOCK_ID" => $iblockId),
336 false,
337 array("ID", "NAME")
338 );
339
340 while ($res = $dbRes->fetch())
341 $departments[$res["ID"]] = $res["NAME"];
342 }
343
344 $dbRes = \CUser::getList(
345 "ID",
346 "ASC",
347 array("ID" => implode("|", $userIds)),
348 array(
349 "FIELDS" => array("ID", "NAME", "LAST_NAME", "SECOND_NAME", "LOGIN", "PERSONAL_PHOTO") +
350 (IsModuleInstalled("mail") ? array("EXTERNAL_AUTH_ID") : array()),
351 "SELECT" => (IsModuleInstalled("extranet") ? array("UF_DEPARTMENT") : array())
352 )
353 );
354 while ($res = $dbRes->fetch())
355 {
356 if (array_key_exists("PERSONAL_PHOTO", $res))
357 {
358 if (!empty($res["PERSONAL_PHOTO"]))
359 {
360 $f = \CFile::resizeImageGet(
361 $res["PERSONAL_PHOTO"],
362 array("width" => 64, "height" => 64),
363 BX_RESIZE_IMAGE_EXACT,
364 false
365 );
366 $res["PHOTO_SRC"] = ($f["src"] ? $f["src"] : "");
367 $res["PHOTO"] = \CFile::showImage($f["src"], 21, 21, "border=0");
368 }
369 else
370 {
371 $res["PHOTO"] = $res["PHOTO_SRC"] = "";
372 }
373 }
374 $res["TYPE"] = "";
375 if (array_key_exists("EXTERNAL_AUTH_ID", $res) && $res["EXTERNAL_AUTH_ID"] == "email")
376 $res["TYPE"] = "mail";
377 elseif (array_key_exists("UF_DEPARTMENT", $res))
378 {
379 if (empty($res["UF_DEPARTMENT"]) || intval($res["UF_DEPARTMENT"][0]) <= 0)
380 $res["TYPE"] = "extranet";
381 else
382 {
383 $ds = array();
384 foreach ($res["UF_DEPARTMENT"] as $departmentId)
385 {
386 if (array_key_exists($departmentId, $departments))
387 $ds[] = $departments[$departmentId];
388 }
389 $res["TAGS"] = empty($res["WORK_POSITION"]) ? array() : array($res["WORK_POSITION"]);
390 $res["TAGS"] = implode(",", array_merge($res["TAGS"], $ds));
391 $res["WORK_DEPARTMENTS"] = $ds;
392 }
393 }
394 $result["items"][$res["ID"]] = $res;
395 }
396 }
397
398 if (!empty($result["items"]) || !empty($result["hiddenItems"]))
399 {
400 $cache->startDataCache($cacheTtl, $cacheId, $cacheDir);
401 \CVoteCacheManager::setTag($cacheDir, "V", $cacheParams["voteId"]);
402 $cache->endDataCache($result);
403 }
404 }
405 return $result;
406 }
407 protected function processActionGetVoted()
408 {
409 if (!$this->checkRequiredGetParams(array("answerId")))
410 return;
411 $answerId = intval($this->request->getQuery("answerId"));
412 if ($answerId <= 0)
413 throw new ArgumentNullException("Answer ID is required.");
415 $userId = $this->getUser()->getId();
416 if (!$attach->canRead($userId))
417 throw new AccessDeniedException();
418
419 $belong = false;
420 foreach ($attach["QUESTIONS"] as $qID => $question)
421 {
422 if (array_key_exists($answerId, $question["ANSWERS"]))
423 {
424 $belong = true;
425 break;
426 }
427 }
428 if (!$belong)
429 throw new AccessDeniedException();
430
431 $nameTemplate = $this->request->getPost("nameTemplate") ?: \CSite::getNameFormat(null, $this->request->getPost("SITE_ID"));
432 $iNumPage = $this->request->getPost("iNumPage");
433 $nPageSize = 50;
434 $items = array();
435
436 $result = self::getVoted(
437 array(
438 "voteId" => $attach->getVoteId(),
439 "answerId" => $answerId),
440 array(
441 "iNumPage" => $iNumPage,
442 "nPageSize" => $nPageSize,
443 "bShowAll" => false)
444 );
445
446 if ($result["hiddenItems"] > 0)
447 {
448 $items[] = array(
449 "ID" => "HIDDEN",
450 "COUNT" => $result["hiddenItems"],
451 "FULL_NAME" => Loc::getMessage("VOTE_HIDDEN_VOTES_COUNT", ["#COUNT#" => $result["hiddenItems"]]),
452 );
453 }
454 foreach ($result["items"] as $k => $res)
455 {
456 $items[] = array(
457 "ID" => $res["ID"],
458 "TYPE" => $res["TYPE"],
459 "PHOTO" => $res["PHOTO"],
460 "PHOTO_SRC" => $res["PHOTO_SRC"],
461 "FULL_NAME" => \CUser::formatName($nameTemplate, $res),
462 );
463 }
464 $result["items"] = $items;
465 $result["pageSize"] = $nPageSize;
466 $result["iNumPage"] = $iNumPage;
467 $this->sendJsonSuccessResponse(array(
468 "action" => $this->getAction(),
469 "data" => $result
470 ));
471 }
472
473 protected function processActionGetMobileVoted()
474 {
475 if (!$this->checkRequiredGetParams(array("answerId")))
476 return;
477 $answerId = intval($this->request->getQuery("answerId"));
478 if ($answerId <= 0)
479 throw new ArgumentNullException("Answer ID is required.");
481 $userId = $this->getUser()->getId();
482 if (!$attach->canRead($userId))
483 throw new AccessDeniedException();
484
485 $belong = false;
486 foreach ($attach["QUESTIONS"] as $qID => $question)
487 {
488 if (array_key_exists($answerId, $question["ANSWERS"]))
489 {
490 $belong = true;
491 break;
492 }
493 }
494 if (!$belong)
495 throw new AccessDeniedException();
496
497 $nameTemplate = $this->request->getPost("nameTemplate") ?: \CSite::getNameFormat(null, $this->request->getPost("SITE_ID"));
498
499 $result = self::getVoted(
500 array(
501 "voteId" => $attach->getVoteId(),
502 "answerId" => $answerId),
503 array(
504 "iNumPage" => 1,
505 "nPageSize" => 50,
506 "bShowAll" => true)
507 );
508
509 $items = array();
510 foreach ($result["items"] as $k => $res)
511 {
512 $url = \CComponentEngine::MakePathFromTemplate(
513 "/mobile/users/?user_id=#user_id#",
514 array("UID" => $res["ID"], "user_id" => $res["ID"],
515 "USER_ID" => $res["ID"])
516 );
517 $items[] = array(
518 "ID" => $res["ID"],
519 "NAME" => \CUser::FormatName($nameTemplate, $res, true, false),
520 "IMAGE" => $res["PHOTO_SRC"],
521 "TAGS" => $res["TAGS"],
522 "WORK_POSITION" => $res["WORK_POSITION"],
523 "WORK_DEPARTMENTS" => $res["WORK_DEPARTMENTS"],
524 "URL" => $url,
525 'PAGE' => [
526 'bx24ModernStyle' => true,
527 'url' => $url,
528 'useTitle' => true,
529 ]
530 );
531 }
532 $result["items"] = $items;
533 $this->sendJsonSuccessResponse(array(
534 "action" => $this->getAction(),
535 "data" => array(
536 "voted" => $items
537 ),
538 "names" => array(
539 "voted" => "Voted users"
540 )
541 ));
542 }
543
549 protected function processActionExportXls()
550 {
552 $userId = $this->getUser()->getId();
553 if (!$attach->canRead($userId))
554 throw new AccessDeniedException();
555 $attach->exportExcel();
556 }
557
562 public function clearCache($voteId)
563 {
564 BXClearCache(true, "/vote/".$voteId."/voted/");
565 }
566}
static includeModule($moduleName)
Definition loader.php:69
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static getVoted(array $cacheParams, array $pageParams)
static loadFromVoteId(array $attach, $id)
Definition manager.php:31