PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/components/bitrix/forum.admin.forums/class.php

https://gitlab.com/alexprowars/bitrix
PHP | 353 lines | 319 code | 26 blank | 8 comment | 38 complexity | 99dad43d29946cf4bc0a7a96cb118513 MD5 | raw file
  1. <?if(!defined("B_PROLOG_INCLUDED") || B_PROLOG_INCLUDED!==true)die();
  2. use \Bitrix\Main\Localization\Loc;
  3. use \Bitrix\Main\Application;
  4. use \Bitrix\Main\Error;
  5. use \Bitrix\Main\ErrorCollection;
  6. use \Bitrix\Main\Config;
  7. use Bitrix\Main\ORM\Query\Result;
  8. use \Bitrix\Vote\Base\Diag;
  9. use Bitrix\Main\Grid\Options;
  10. Loc::loadMessages(__FILE__);
  11. class CForumAdminMessages extends \CBitrixComponent
  12. {
  13. /** @var ErrorCollection */
  14. protected $errorCollection;
  15. /** @var \Bitrix\Forum\User */
  16. protected $user;
  17. protected $rights = "D";
  18. /** @var string */
  19. protected $gridId = "grid_admin_forum_forums";
  20. public function __construct($component = null)
  21. {
  22. global $APPLICATION, $USER;
  23. parent::__construct($component);
  24. $this->rights = $APPLICATION->GetGroupRight("forum");
  25. $this->errorCollection = new \Bitrix\Main\ErrorCollection();
  26. \Bitrix\Main\Loader::includeModule("forum");
  27. $this->user = \Bitrix\Forum\User::getById($USER->getId());
  28. }
  29. public function executeComponent()
  30. {
  31. try
  32. {
  33. if ($this->rights <= "D")
  34. throw new \Bitrix\Main\AccessDeniedException();
  35. $this->arResult["ERRORS"] = $this->processAction();
  36. $nav = new Bitrix\Main\UI\PageNavigation($this->gridId);
  37. $nav->initFromUri();
  38. $this->arParams["GRID_ID"] = $this->gridId;
  39. $this->arResult["FILTER"] = $this->initFilter();
  40. $this->arResult["DATA"] = [];
  41. $dbRes = \Bitrix\Forum\ForumTable::getList(array(
  42. "order" => $this->initOrder(),
  43. "filter" => $this->prepareFilter($this->arResult["FILTER"]),
  44. "limit" => $nav->getLimit(),
  45. "offset" => $nav->getOffset(),
  46. "count_total" => true,
  47. ));
  48. /*?><pre><b>SQL: </b><? print_r(\Bitrix\Main\Entity\Query::getLastQuery()) ?></pre><?*/
  49. $nav->setRecordCount($dbRes->getCount());
  50. $this->arResult["NAV_OBJECT"] = $nav;
  51. $this->arResult["TOTAL_ROWS_COUNT"] = $dbRes->getCount();
  52. if ($res = $dbRes->fetch())
  53. {
  54. do
  55. {
  56. $this->arResult["DATA"][$res["ID"]] = $res + [
  57. "SITES" => [],
  58. "PERMISSIONS" => []
  59. ];
  60. } while ($res = $dbRes->fetch());
  61. if ($dbRes = \Bitrix\Forum\ForumSiteTable::getList([
  62. "order" => ["FORUM_ID" => "ASC"],
  63. "filter" => [
  64. "FORUM_ID" => array_keys($this->arResult["DATA"])
  65. ]
  66. ]))
  67. {
  68. while ($res = $dbRes->fetch())
  69. {
  70. $this->arResult["DATA"][$res["FORUM_ID"]]["SITES"][$res["SITE_ID"]] = $res["PATH2FORUM_MESSAGES"];
  71. }
  72. }
  73. if ($dbRes = \Bitrix\Forum\PermissionTable::getList([
  74. "order" => [
  75. "FORUM_ID" => "ASC",
  76. "GROUP_ID" => "ASC"
  77. ],
  78. "filter" => [
  79. "FORUM_ID" => array_keys($this->arResult["DATA"]),
  80. ]
  81. ]))
  82. {
  83. while ($res = $dbRes->fetch())
  84. {
  85. $this->arResult["DATA"][$res["FORUM_ID"]]["PERMISSIONS"][$res["GROUP_ID"]] = $res["PERMISSION"];
  86. }
  87. }
  88. }
  89. $this->includeComponentTemplate();
  90. }
  91. catch(Exception $e)
  92. {
  93. $exceptionHandling = Config\Configuration::getValue("exception_handling");
  94. if ($exceptionHandling["debug"])
  95. {
  96. throw $e;
  97. }
  98. else
  99. {
  100. ShowError($e->getMessage());
  101. }
  102. }
  103. }
  104. public function onPrepareComponentParams($arParams)
  105. {
  106. $arParams["SITES"] = [];
  107. if ($dbRes = \CSite::GetList())
  108. {
  109. while ($res = $dbRes->GetNext())
  110. {
  111. $arParams["SITES"][$res["ID"]] = "[{$res["ID"]}] " . $res["NAME"];
  112. }
  113. }
  114. $arParams["FORUM_GROUPS"] = [];
  115. $arParams["FORUM_GROUP_IDS"] = [];
  116. foreach (\CForumGroup::GetByLang(LANGUAGE_ID) as $res)
  117. {
  118. $arParams["FORUM_GROUP_IDS"][$res["ID"]] = str_repeat(".", ($res["DEPTH_LEVEL"]-1)).$res["NAME"];
  119. $arParams["FORUM_GROUPS"][$res["ID"]] = $res;
  120. }
  121. $arParams["FORUM_PERMISSIONS"] = \Bitrix\Forum\Permission::getTitledList();
  122. $arParams["USER_GROUPS"] = [];
  123. if ($dbRes = \Bitrix\Main\GroupTable::getList([
  124. "select" => ["ID", "NAME"],
  125. "cache" => ["ttl" => 3600]
  126. ]))
  127. {
  128. while ($res = $dbRes->fetch())
  129. {
  130. $arParams["USER_GROUPS"][$res["ID"]] = $res["NAME"];
  131. }
  132. }
  133. return parent::onPrepareComponentParams($arParams);
  134. }
  135. /**
  136. * @return array
  137. */
  138. protected function processAction()
  139. {
  140. if (
  141. $this->rights <= "R" ||
  142. !check_bitrix_sessid() ||
  143. !$this->request->isPost() ||
  144. !\Bitrix\Main\Grid\Context::isInternalRequest() ||
  145. $this->request->get("grid_id") !== $this->gridId
  146. )
  147. {
  148. return [];
  149. }
  150. $this->errorCollection->clear();
  151. $this->request->addFilter(new \Bitrix\Main\Web\PostDecodeFilter());
  152. global $DB, $APPLICATION;
  153. @set_time_limit(0);
  154. $DB->StartTransaction();
  155. $APPLICATION->ResetException();
  156. if ($this->request->getPost("action") == \Bitrix\Main\Grid\Actions::GRID_DELETE_ROW)
  157. {
  158. $forum = \Bitrix\Forum\Forum::getById($this->request->getPost("id"));
  159. if (!$this->user->canDeleteForum($forum))
  160. {
  161. $this->errorCollection->add([new \Bitrix\Main\Error(Loc::getMessage("FORUM_ERROR_DELETE_PERMISSION", ["#id#" => $forum->getId(), "#name#" => $forum["NAME"]]))]);
  162. }
  163. else if (!\CForumNew::Delete($forum->getId()))
  164. {
  165. $this->errorCollection->add([new \Bitrix\Main\Error(($ex = $APPLICATION->GetException()) ? $ex->GetString() : Loc::getMessage("FORUM_ERROR_DELETE_UNKNOWN", ["#id#" => $forum->getId(), "#name#" => $forum["NAME"]]))]);
  166. }
  167. }
  168. else if ($this->request->getPost("action_button_" . $this->gridId) === "edit")
  169. {
  170. foreach ($this->request->getPost("FIELDS") as $id => $fields)
  171. {
  172. $forum = \Bitrix\Forum\Forum::getById($id);
  173. if (!$this->user->canEditForum($forum))
  174. {
  175. $this->errorCollection->add([new \Bitrix\Main\Error(Loc::getMessage("FORUM_ERROR_EDIT_PERMISSION", ["#id#" => $forum->getId(), "#name#" => $forum["NAME"]]))]);
  176. }
  177. else if (!\CForumNew::Update($forum->getId(), $fields))
  178. {
  179. $this->errorCollection->add([new \Bitrix\Main\Error(($ex = $APPLICATION->GetException()) ? $ex->GetString() : Loc::getMessage("FORUM_ERROR_EDIT_UNKNOWN", ["#id#" => $forum->getId(), "#name#" => $forum["NAME"]]))]);
  180. }
  181. }
  182. }
  183. else
  184. {
  185. $ids = $this->request->getPost("rows") ?: $this->request->getPost("ID");
  186. $action = $this->request->getPost("action_button_" . $this->gridId);
  187. if ($controls = $this->request->getPost("controls"))
  188. $action = $controls["action_button_" . $this->gridId];
  189. switch ($action)
  190. {
  191. case "delete":
  192. foreach ($ids as $id)
  193. {
  194. $forum = \Bitrix\Forum\Forum::getById($id);
  195. if (!$this->user->canDeleteForum($forum))
  196. {
  197. $this->errorCollection->add([new \Bitrix\Main\Error(Loc::getMessage("FORUM_ERROR_DELETE_PERMISSION", ["#id#" => $forum->getId(), "#name#" => $forum["NAME"]]))]);
  198. }
  199. else if (!\CForumNew::Delete($forum->getId()))
  200. {
  201. $this->errorCollection->add([new \Bitrix\Main\Error(($ex = $APPLICATION->GetException()) ? $ex->GetString() : Loc::getMessage("FORUM_ERROR_DELETE_UNKNOWN", ["#id#" => $forum->getId(), "#name#" => $forum["NAME"]]))]);
  202. }
  203. }
  204. break;
  205. case "activate":
  206. case "deactivate":
  207. foreach ($ids as $id)
  208. {
  209. $forum = \Bitrix\Forum\Forum::getById($id);
  210. if (!$this->user->canEditForum($forum))
  211. {
  212. $this->errorCollection->add([new \Bitrix\Main\Error(Loc::getMessage("FORUM_ERROR_EDIT_PERMISSION", ["#id#" => $forum->getId(), "#name#" => $forum["NAME"]]))]);
  213. }
  214. else if (!\CForumNew::Update($forum->getId(), ["ACTIVE" => $action == "deactivate" ? "N" : "Y"]))
  215. {
  216. $this->errorCollection->add([new \Bitrix\Main\Error(($ex = $APPLICATION->GetException()) ? $ex->GetString() : Loc::getMessage("FORUM_ERROR_EDIT_UNKNOWN", ["#id#" => $forum->getId(), "#name#" => $forum["NAME"]]))]);
  217. $APPLICATION->ResetException();
  218. }
  219. }
  220. break;
  221. case "clear_html":
  222. foreach ($ids as $id)
  223. {
  224. \CForumNew::ClearHTML($id);
  225. }
  226. break;
  227. }
  228. }
  229. $DB->Commit();
  230. $errors = array();
  231. if (!$this->errorCollection->isEmpty())
  232. {
  233. /** @var $error Error */
  234. foreach($this->errorCollection->toArray() as $error)
  235. {
  236. $errors[] = array(
  237. "TYPE" => \Bitrix\Main\Grid\MessageType::ERROR,
  238. "TEXT" => Loc::getMessage("FORUM_PROCESS_ERRORS").$error->getMessage(),
  239. "TITLE" => Loc::getMessage("FORUM_PROCESS_ERRORS_TITLE")
  240. );
  241. }
  242. }
  243. return $errors;
  244. }
  245. protected function initFilter()
  246. {
  247. $this->arResult["FILTER_FIELDS"] = [
  248. [
  249. "id" => "ID",
  250. "name" => "ID",
  251. "type" => "number",
  252. "filterable" => "",
  253. "default" => true
  254. ],
  255. [
  256. "id" => "ACTIVE",
  257. "name" => Loc::getMessage("FORUM_FILTER_ACTIVE"),
  258. "type" => "list",
  259. "items" => ["Y" => GetMessage("admin_lib_list_yes"), "N" => GetMessage("admin_lib_list_no")],
  260. "filterable" => ""
  261. ],
  262. [
  263. "id" => "SITE.SITE_ID",
  264. "name" => Loc::getMessage("FORUM_FILTER_SITE"),
  265. "filterable" => "",
  266. "type" => "list",
  267. "items" => $this->arParams["SITES"]
  268. ],
  269. [
  270. "id" => "FORUM_GROUP_ID",
  271. "name" => Loc::getMessage("FORUM_FILTER_FORUM_GROUP_ID"),
  272. "type" => "list",
  273. "items" => $this->arParams["FORUM_GROUP_IDS"],
  274. "filterable" => ""
  275. ],
  276. [
  277. "id" => "CAN_READ",
  278. "name" => Loc::getMessage("FORUM_FILTER_CAN_READ", ["#permission#" => \Bitrix\Forum\Permission::CAN_READ]),
  279. "type" => "list",
  280. "items" => $this->arParams["USER_GROUPS"],
  281. "filterable" => ""
  282. ],
  283. ];
  284. $this->arResult["FILTER_ID"] = $this->gridId."_filter";
  285. return (new \Bitrix\Main\UI\Filter\Options($this->arResult["FILTER_ID"]))->getFilterLogic($this->arResult["FILTER_FIELDS"]);
  286. }
  287. protected function prepareFilter($filter)
  288. {
  289. $result = $filter;
  290. if (is_array($filter))
  291. {
  292. if (array_key_exists("CAN_READ", $filter))
  293. {
  294. unset($result["CAN_READ"]);
  295. $result[] = [
  296. "PERMISSION.GROUP_ID" => [2, $filter["CAN_READ"]],
  297. ">=PERMISSION.PERMISSION" => \Bitrix\Forum\Permission::CAN_READ
  298. ];
  299. }
  300. if (
  301. array_key_exists("FORUM_GROUP_ID", $filter) &&
  302. array_key_exists($filter["FORUM_GROUP_ID"], $this->arParams["FORUM_GROUPS"]) &&
  303. ($group = $this->arParams["FORUM_GROUPS"][$filter["FORUM_GROUP_ID"]])
  304. )
  305. {
  306. unset($result["FORUM_GROUP_ID"]);
  307. $result[] = [
  308. ">=GROUP.LEFT_MARGIN" => $group["LEFT_MARGIN"],
  309. "<=GROUP.RIGHT_MARGIN" => $group["RIGHT_MARGIN"]
  310. ];
  311. }
  312. }
  313. return $result;
  314. }
  315. protected function initOrder()
  316. {
  317. $order = (new Bitrix\Main\Grid\Options($this->gridId))->GetSorting(["sort" => ["ID" => "ASC"]]);
  318. $order = $order["sort"];
  319. if (!is_array($order) ||
  320. !empty(array_diff($order, ["DESC", "ASC", "desc", "asc"]))
  321. )
  322. {
  323. $order = ["ID" => "ASC"];
  324. }
  325. return $order;
  326. }
  327. }