PageRenderTime 23ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/vote/include.php

https://gitlab.com/alexprowars/bitrix
PHP | 349 lines | 277 code | 18 blank | 54 comment | 60 complexity | c97c5045c0040c80853889ee85be9aaa MD5 | raw file
  1. <?
  2. global $DB, $MESS, $APPLICATION, $voteCache;
  3. require_once ($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/admin_tools.php");
  4. require_once ($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/filter_tools.php");
  5. require_once ($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/vote/vote_tools.php");
  6. require_once ($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/vote/classes/".mb_strtolower($DB->type)."/channel.php");
  7. IncludeModuleLangFile(__FILE__);
  8. if (!defined("VOTE_CACHE_TIME"))
  9. define("VOTE_CACHE_TIME", 3600);
  10. define("VOTE_DEFAULT_DIAGRAM_TYPE", "histogram");
  11. $GLOBALS["VOTE_CACHE"] = array(
  12. "CHANNEL" => array(),
  13. "VOTE" => array(),
  14. "QUESTION" => array());
  15. $GLOBALS["VOTE_CACHE_VOTING"] = array();
  16. $GLOBALS["aVotePermissions"] = array(
  17. "reference_id" => array(0, 1, 2, /*3, */4),
  18. "reference" => array(GetMessage("VOTE_DENIED"), GetMessage("VOTE_READ"), GetMessage("VOTE_WRITE"), /*GetMessage("VOTE_EDIT_MY_OWN"), */GetMessage("VOTE_EDIT")));
  19. CModule::AddAutoloadClasses("vote", array(
  20. "CVoteAnswer" => "classes/".mb_strtolower($DB->type)."/answer.php",
  21. "CVoteEvent" => "classes/".mb_strtolower($DB->type)."/event.php",
  22. "CVoteQuestion" => "classes/".mb_strtolower($DB->type)."/question.php",
  23. "CVoteUser" => "classes/".mb_strtolower($DB->type)."/user.php",
  24. "CVote" => "classes/".mb_strtolower($DB->type)."/vote.php",
  25. "CVoteCacheManager" => "classes/general/functions.php",
  26. "CVoteNotifySchema" => "classes/general/im.php",
  27. "bitrix\\vote\\answertable" => "lib/answer.php",
  28. "bitrix\\vote\\answer" => "lib/answer.php",
  29. "bitrix\\vote\\attachtable" => "lib/attach.php",
  30. "bitrix\\vote\\attach" => "lib/attach.php",
  31. "bitrix\\vote\\attachment\\attach" => "lib/attachment/attach.php",
  32. "bitrix\\vote\\attachment\\blogpostconnector" => "lib/attachment/blogpostconnector.php",
  33. "bitrix\\vote\\attachment\\connector" => "lib/attachment/connector.php",
  34. "bitrix\\vote\\attachment\\controller" => "lib/attachment/controller.php",
  35. "bitrix\\vote\\attachment\\defaultconnector" => "lib/attachment/defaultconnector.php",
  36. "bitrix\\vote\\attachment\\forummessageconnector" => "lib/attachment/forummessageconnector.php",
  37. "bitrix\\vote\\attachment\\storable" => "lib/attachment/storable.php",
  38. "bitrix\\vote\\base\\baseobject" => "lib/base/baseobject.php",
  39. "bitrix\\vote\\base\\controller" => "lib/base/controller.php",
  40. "bitrix\\vote\\base\\diag" => "lib/base/diag.php",
  41. "bitrix\\vote\\channeltable" => "lib/channel.php",
  42. "bitrix\\vote\\channelgrouptable" => "lib/channel.php",
  43. "bitrix\\vote\\channelsitetable" => "lib/channel.php",
  44. "bitrix\\vote\\channel" => "lib/channel.php",
  45. "bitrix\\vote\\dbresult" => "lib/dbresult.php",
  46. "bitrix\\vote\\voteeventtable" => "lib/event.php",
  47. "bitrix\\vote\\eventtable" => "lib/event.php",
  48. "bitrix\\vote\\eventquestiontable" => "lib/event.php",
  49. "bitrix\\vote\\eventanswertable" => "lib/event.php",
  50. "bitrix\\vote\\event" => "lib/event.php",
  51. "bitrix\\vote\\questiontable" => "lib/question.php",
  52. "bitrix\\vote\\question" => "lib/question.php",
  53. "bitrix\\vote\\uf\\manager" => "lib/uf/manager.php",
  54. "bitrix\\vote\\uf\\voteusertype" => "lib/uf/voteusertype.php",
  55. "bitrix\\vote\\usertable" => "lib/user.php",
  56. "bitrix\\vote\\user" => "lib/user.php",
  57. "bitrix\\vote\\votetable" => "lib/vote.php",
  58. "bitrix\\vote\\vote" => "lib/vote.php"
  59. ));
  60. $voteCache = new CVoteCacheManager();
  61. function VoteVoteEditFromArray($CHANNEL_ID, $VOTE_ID = false, $arFields = array(), $params = array())
  62. {
  63. $CHANNEL_ID = intval($CHANNEL_ID);
  64. if ($CHANNEL_ID <= 0 || empty($arFields)):
  65. return false;
  66. elseif (CVote::UserGroupPermission($CHANNEL_ID) <= 0):
  67. return false;
  68. endif;
  69. $aMsg = array();
  70. $params = (is_array($params) ? $params : array());
  71. $params["UNIQUE_TYPE"] = (is_set($params, "UNIQUE_TYPE") ? intval($params["UNIQUE_TYPE"]) : 20);
  72. $arVote = array();
  73. $arQuestions = array();
  74. $arFieldsQuestions = array();
  75. $arFieldsVote = array(
  76. "CHANNEL_ID" => $CHANNEL_ID,
  77. "AUTHOR_ID" => $GLOBALS["USER"]->GetID(),
  78. "UNIQUE_TYPE" => $params["UNIQUE_TYPE"],
  79. "DELAY" => $params["DELAY"] ?: 10,
  80. "DELAY_TYPE" => $params['DELAY_TYPE'] ?: "D");
  81. if (!empty($arFields["DATE_START"]))
  82. $arFieldsVote["DATE_START"] = $arFields["DATE_START"];
  83. if (!empty($arFields["DATE_END"]))
  84. $arFieldsVote["DATE_END"] = $arFields["DATE_END"];
  85. if (!empty($arFields["TITLE"]))
  86. $arFieldsVote["TITLE"] = $arFields["TITLE"];
  87. if (isset($arFields["ACTIVE"]))
  88. $arFieldsVote["ACTIVE"] = $arFields["ACTIVE"];
  89. if (isset($arFields["NOTIFY"]))
  90. $arFieldsVote["NOTIFY"] = $arFields["NOTIFY"];
  91. if (isset($arFields["URL"]))
  92. $arFieldsVote["URL"] = $arFields["URL"];
  93. /************** Fatal errors ***************************************/
  94. if (!CVote::CheckFields("UPDATE", $arFieldsVote)):
  95. $e = $GLOBALS['APPLICATION']->GetException();
  96. $aMsg[] = array(
  97. "id" => "VOTE_ID",
  98. "text" => $e->GetString());
  99. elseif (intval($VOTE_ID) > 0):
  100. $db_res = CVote::GetByID($VOTE_ID);
  101. if (!($db_res && $res = $db_res->Fetch())):
  102. $aMsg[] = array(
  103. "id" => "VOTE_ID",
  104. "text" => GetMessage("VOTE_VOTE_NOT_FOUND", array("#ID#", $VOTE_ID)));
  105. elseif ($res["CHANNEL_ID"] != $CHANNEL_ID):
  106. $aMsg[] = array(
  107. "id" => "CHANNEL_ID",
  108. "text" => GetMessage("VOTE_CHANNEL_ID_ERR"));
  109. else:
  110. $arVote = $res;
  111. $db_res = CVoteQuestion::GetList($arVote["ID"], "s_id");
  112. if ($db_res && $res = $db_res->Fetch()):
  113. do { $arQuestions[$res["ID"]] = $res + array("ANSWERS" => array()); } while ($res = $db_res->Fetch());
  114. endif;
  115. $db_res = CVoteAnswer::GetListEx(array("ID" => "ASC"), array("VOTE_ID" => $arVote["ID"]));
  116. if ($db_res && $res = $db_res->Fetch()):
  117. do { $arQuestions[$res["QUESTION_ID"]]["ANSWERS"][$res["ID"]] = $res; } while ($res = $db_res->Fetch());
  118. endif;
  119. endif;
  120. endif;
  121. if (!empty($aMsg)):
  122. $e = new CAdminException(array_reverse($aMsg));
  123. $GLOBALS["APPLICATION"]->ThrowException($e);
  124. return false;
  125. endif;
  126. /************** Fatal errors/***************************************/
  127. if (!empty($arFieldsVote["TITLE"]) && !empty($arVote["TITLE"]))
  128. {
  129. $q = reset($arQuestions);
  130. if ($arVote["TITLE"] == mb_substr($q["QUESTION"], 0, mb_strlen($arVote["TITLE"])))
  131. unset($arFieldsVote["TITLE"]);
  132. }
  133. /************** Check Data *****************************************/
  134. // Questions
  135. $arFields["QUESTIONS"] = (is_array($arFields["QUESTIONS"]) ? $arFields["QUESTIONS"] : array());
  136. $iQuestions = 0;
  137. foreach ($arFields["QUESTIONS"] as $key => $arQuestion)
  138. {
  139. if ($arQuestion["DEL"] != "Y")
  140. {
  141. $arQuestion["ID"] = intval($arQuestion["ID"]);
  142. $arQuestion = array(
  143. "ID" => $arQuestion["ID"] > 0 && is_set($arQuestions, $arQuestion["ID"]) ? $arQuestion["ID"] : false,
  144. "QUESTION" => trim($arQuestion["QUESTION"]),
  145. "QUESTION_TYPE" => trim($arQuestion["QUESTION_TYPE"]),
  146. "ANSWERS" => (is_array($arQuestion["ANSWERS"]) ? $arQuestion["ANSWERS"] : array()));
  147. $arAnswers = ($arQuestion["ID"] > 0 ? $arQuestions[$arQuestion["ID"]]["ANSWERS"] : array());
  148. foreach ($arQuestion["ANSWERS"] as $keya => $arAnswer)
  149. {
  150. $arAnswer["ID"] = intval($arAnswer["ID"]);
  151. $arAnswer["MESSAGE"] = trim($arAnswer["MESSAGE"]);
  152. if (!empty($arAnswer["MESSAGE"]) && $arAnswer["DEL"] != "Y")
  153. {
  154. $arQuestion["ANSWERS"][$keya] = array(
  155. "MESSAGE" => $arAnswer["MESSAGE"],
  156. "MESSAGE_TYPE" => $arAnswer["MESSAGE_TYPE"],
  157. "FIELD_TYPE" => $arAnswer["FIELD_TYPE"]);
  158. if ($arAnswer["ID"] > 0 && is_set($arAnswers, $arAnswer["ID"]))
  159. {
  160. $arQuestion["ANSWERS"][$keya]["ID"] = $arAnswer["ID"];
  161. unset($arAnswers[$arAnswer["ID"]]);
  162. }
  163. }
  164. }
  165. }
  166. if ($arQuestion["DEL"] == "Y" || empty($arQuestion["QUESTION"]) || empty($arQuestion["ANSWERS"]))
  167. {
  168. if ($arQuestion["DEL"] != "Y" && !(empty($arQuestion["QUESTION"]) && empty($arQuestion["ANSWERS"])))
  169. {
  170. $aMsg[] = array(
  171. "id" => "QUESTION_".$key,
  172. "text" => (empty($arQuestion["QUESTION"]) ?
  173. GetMessage("VOTE_QUESTION_EMPTY", array("#NUMBER#" => $key)) :
  174. GetMessage("VOTE_ANSWERS_EMPTY", array("#QUESTION#" => htmlspecialcharsbx($arQuestion["QUESTION"])))));
  175. }
  176. continue;
  177. }
  178. if ($arQuestion["ID"] > 0)
  179. {
  180. unset($arQuestions[$arQuestion["ID"]]);
  181. foreach($arAnswers as $arAnswer)
  182. {
  183. $arQuestion["ANSWERS"][] = ($arAnswer + array("DEL" => "Y"));
  184. }
  185. }
  186. $iQuestions++;
  187. $arFieldsQuestions[$key] = $arQuestion;
  188. }
  189. foreach ($arQuestions as $arQuestion)
  190. {
  191. $arFieldsQuestions[] = ($arQuestion + array("DEL" => "Y"));
  192. }
  193. if (!empty($aMsg)):
  194. $e = new CAdminException(array_reverse($aMsg));
  195. $GLOBALS["APPLICATION"]->ThrowException($e);
  196. return false;
  197. elseif (empty($arFieldsQuestions) && $VOTE_ID <= 0):
  198. return true;
  199. elseif ($params["bOnlyCheck"] == "Y"):
  200. return true;
  201. endif;
  202. /************** Check Data/*****************************************/
  203. /************** Main actions with return ***************************/
  204. if (empty($arFieldsVote["TITLE"]))
  205. {
  206. $q = reset($arFieldsQuestions);
  207. $arFieldsVote["TITLE"] = null;
  208. do {
  209. if ($q["DEL"] != "Y")
  210. {
  211. $arFieldsVote["TITLE"] = $q["QUESTION"];
  212. break;
  213. }
  214. } while ($q = next($arFieldsQuestions));
  215. reset($arFieldsQuestions);
  216. }
  217. if (empty($arVote))
  218. {
  219. $arVote["ID"] = intval(CVote::Add($arFieldsVote));
  220. }
  221. else
  222. {
  223. CVote::Update($VOTE_ID, $arFieldsVote);
  224. }
  225. if ($iQuestions > 0 && $arVote["ID"] > 0)
  226. {
  227. $iQuestions = 0;
  228. foreach ($arFieldsQuestions as $arQuestion)
  229. {
  230. if ($arQuestion["DEL"] == "Y"):
  231. CVoteQuestion::Delete($arQuestion["ID"]);
  232. continue;
  233. elseif ($arQuestion["ID"] > 0):
  234. $arQuestion["C_SORT"] = ($iQuestions + 1) * 10;
  235. CVoteQuestion::Update($arQuestion["ID"], $arQuestion);
  236. else:
  237. $arQuestion["C_SORT"] = ($iQuestions + 1) * 10;
  238. $arQuestion["VOTE_ID"] = $arVote["ID"];
  239. $arQuestion["ID"] = intval(CVoteQuestion::Add($arQuestion));
  240. if ($arQuestion["ID"] <= 0):
  241. continue;
  242. endif;
  243. endif;
  244. $iQuestions++;
  245. $iAnswers = 0;
  246. foreach ($arQuestion["ANSWERS"] as $arAnswer)
  247. {
  248. if ($arAnswer["DEL"] == "Y"):
  249. CVoteAnswer::Delete($arAnswer["ID"]);
  250. continue;
  251. endif;
  252. if ($arAnswer["ID"] > 0):
  253. $arAnswer["C_SORT"] = ($iAnswers + 1)* 10;
  254. CVoteAnswer::Update($arAnswer["ID"], $arAnswer);
  255. else:
  256. $arAnswer["QUESTION_ID"] = $arQuestion["ID"];
  257. $arAnswer["C_SORT"] = ($iAnswers + 1)* 10;
  258. $arAnswer["ID"] = intval(CVoteAnswer::Add($arAnswer));
  259. if ($arAnswer["ID"] <= 0):
  260. continue;
  261. endif;
  262. endif;
  263. $iAnswers++;
  264. }
  265. if ($iAnswers <= 0)
  266. {
  267. CVoteQuestion::Delete($arQuestion["ID"]);
  268. $iQuestions--;
  269. }
  270. }
  271. }
  272. if (intval($arVote["ID"]) <= 0)
  273. {
  274. return false;
  275. }
  276. elseif ($iQuestions <= 0)
  277. {
  278. CVote::Delete($arVote["ID"]);
  279. return 0;
  280. }
  281. return $arVote["ID"];
  282. /************** Actions/********************************************/
  283. /* $arFields = array(
  284. "ID" => 345,
  285. "TITLE" => "test",
  286. "...",
  287. "QUESTIONS" => array(
  288. array(
  289. "ID" => 348,
  290. "QUESTION" => "test",
  291. "ANSWERS" => array(
  292. array(
  293. "ID" => 340,
  294. "MESSAGE" => "test"),
  295. array(
  296. "ID" => 0,
  297. "MESSAGE" => "test"),
  298. array(
  299. "ID" => 350,
  300. "DEL" => "Y",
  301. "MESSAGE" => "test")
  302. )
  303. ),
  304. array(
  305. "ID" => 351,
  306. "DEL" => "Y",
  307. "QUESTION" => "test",
  308. "ANSWERS" => array(
  309. array(
  310. "ID" => 0,
  311. "MESSAGE" => "test"),
  312. array(
  313. "ID" => 478,
  314. "DEL" => "Y",
  315. "MESSAGE" => "test")
  316. )
  317. ),
  318. array(
  319. "ID" => 0,
  320. "QUESTION" => "test",
  321. "ANSWERS" => array(
  322. array(
  323. "ID" => 0,
  324. "MESSAGE" => "test"),
  325. )
  326. ),
  327. )
  328. );
  329. */
  330. }
  331. ?>