PageRenderTime 71ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/components/bitrix/forum.topic.reviews/action.php

https://gitlab.com/alexprowars/bitrix
PHP | 344 lines | 308 code | 17 blank | 19 comment | 76 complexity | ee8b3babf470fc26df6038afe2d85c36 MD5 | raw file
  1. <?if(!defined("B_PROLOG_INCLUDED") || B_PROLOG_INCLUDED!==true)die();
  2. /**
  3. * Bitrix vars
  4. *
  5. * @var array $arParams
  6. * @var array $arResult
  7. * @var CForumAutosave $arParams["AUTOSAVE"]
  8. * @var CBitrixComponent $this
  9. * @var CMain $APPLICATION
  10. * @var CUser $USER
  11. */
  12. $request = \Bitrix\Main\Context::getCurrent()->getRequest();
  13. $post = $this->request->getPostList()->toArray();
  14. if ($post["AJAX_POST"] == "Y")
  15. CUtil::decodeURIComponent($post);
  16. $this->includeComponentLang("action.php");
  17. // 1.3 Check Permission
  18. if ((empty($request["preview_comment"]) || $request["preview_comment"] == "N"))
  19. {
  20. $strErrorMessage = "";
  21. // 1.5 Create Property
  22. $needProperty = array();
  23. $PRODUCT_IBLOCK_ID = intval($arResult["ELEMENT"]["IBLOCK_ID"]);
  24. $PRODUCT_NAME = trim($arResult["ELEMENT"]["~NAME"]);
  25. $FORUM_TOPIC_ID = intval($arResult["ELEMENT"]["PROPERTY_FORUM_TOPIC_ID_VALUE"]);
  26. $FORUM_MESSAGE_CNT = intval($arResult["ELEMENT"]["PROPERTY_FORUM_MESSAGE_CNT_VALUE"]);
  27. if ($FORUM_TOPIC_ID <= 0 && !($res = CIBlockElement::GetProperty($arResult["ELEMENT"]["IBLOCK_ID"], $arResult["ELEMENT"]["ID"], false, false, array("CODE" => "FORUM_TOPIC_ID"))->fetch()))
  28. {
  29. $needProperty[] = "FORUM_TOPIC_ID";
  30. }
  31. if ($FORUM_MESSAGE_CNT <= 0 && !($res = CIBlockElement::GetProperty($arResult["ELEMENT"]["IBLOCK_ID"], $arResult["ELEMENT"]["ID"], false, false, array("CODE" => "FORUM_MESSAGE_CNT"))->fetch()))
  32. {
  33. $needProperty[] = "FORUM_MESSAGE_CNT";
  34. }
  35. if (!empty($needProperty))
  36. {
  37. $obProperty = new CIBlockProperty;
  38. foreach ($needProperty as $nameProperty)
  39. {
  40. $sName = trim($nameProperty == "FORUM_TOPIC_ID" ? GetMessage("F_FORUM_TOPIC_ID") : GetMessage("F_FORUM_MESSAGE_CNT"));
  41. $sName = (empty($sName) ? $nameProperty : $sName);
  42. if($obProperty->Add(array(
  43. "IBLOCK_ID" => $PRODUCT_IBLOCK_ID,
  44. "ACTIVE" => "Y",
  45. "PROPERTY_TYPE" => "N",
  46. "MULTIPLE" => "N",
  47. "NAME" => $sName,
  48. "CODE" => $nameProperty)))
  49. ${mb_strtoupper($nameProperty)} = 0;
  50. }
  51. }
  52. // 1.5 Set NULL for topic_id if it was deleted
  53. if ($FORUM_TOPIC_ID > 0):
  54. $arTopic = CForumTopic::GetByID($FORUM_TOPIC_ID);
  55. if (!is_array($arTopic) || $arTopic["FORUM_ID"] != $arParams["FORUM_ID"]):
  56. CIBlockElement::SetPropertyValues($arParams["ELEMENT_ID"], $PRODUCT_IBLOCK_ID, 0, "FORUM_TOPIC_ID");
  57. CIBlockElement::SetPropertyValues($arParams["ELEMENT_ID"], $PRODUCT_IBLOCK_ID, 0, "FORUM_MESSAGE_CNT");
  58. $FORUM_TOPIC_ID = 0;
  59. elseif ($arTopic["XML_ID"] !== "IBLOCK_".$arParams["ELEMENT_ID"]):
  60. CForumTopic::Update($FORUM_TOPIC_ID, array("XML_ID" => "IBLOCK_".$arParams["ELEMENT_ID"]));
  61. endif;
  62. elseif (($arTopic = CForumTopic::GetList(array(), array("XML_ID" => "IBLOCK_".$arParams["ELEMENT_ID"]))->fetch()) && $arTopic):
  63. $FORUM_TOPIC_ID = intval($arTopic["ID"]);
  64. CIBlockElement::SetPropertyValues($arParams["ELEMENT_ID"], $PRODUCT_IBLOCK_ID, $arTopic["ID"], "FORUM_TOPIC_ID");
  65. CIBlockElement::SetPropertyValues($arParams["ELEMENT_ID"], $PRODUCT_IBLOCK_ID, $arTopic["POSTS"], "FORUM_MESSAGE_CNT");
  66. endif;
  67. // 1.6 Create New topic and add messages
  68. if ($FORUM_TOPIC_ID <= 0)
  69. {
  70. $arUserStart["NAME"] = (empty($arUserStart["NAME"]) ? $GLOBALS["FORUM_STATUS_NAME"]["guest"] : $arUserStart["NAME"]);
  71. // 1.6.a.1 Add Topic
  72. $DB->StartTransaction();
  73. $arFields = Array(
  74. "TITLE" => $arResult["ELEMENT"]["~NAME"],
  75. "TAGS" => $arResult["ELEMENT"]["~TAGS"],
  76. "FORUM_ID" => $arParams["FORUM_ID"],
  77. "USER_START_ID" => $arUserStart["ID"],
  78. "USER_START_NAME" => $arUserStart["NAME"],
  79. "LAST_POSTER_NAME" => $arUserStart["NAME"],
  80. "APPROVED" => "Y",
  81. "XML_ID" => "IBLOCK_".$arParams["ELEMENT_ID"]
  82. );
  83. $TID = CForumTopic::Add($arFields);
  84. if ($TID <= 0)
  85. {
  86. $arError[] = array(
  87. "code" => "topic is not created",
  88. "title" => GetMessage("F_ERR_ADD_TOPIC"));
  89. }
  90. else
  91. {
  92. // 1.6.b Add post as new message
  93. $sImage = ""; $arSection = array();
  94. $url = (empty($arParams["URL_TEMPLATES_DETAIL"]) ? $arResult["ELEMENT"]["DETAIL_PAGE_URL"] : $arParams["URL_TEMPLATES_DETAIL"]);
  95. $SECTION_CODE_PATH = "";
  96. if (mb_strpos($arParams["URL_TEMPLATES_DETAIL"], "#SECTION_CODE#") !== false && intval($arResult["ELEMENT"]["IBLOCK_SECTION_ID"]) > 0):
  97. $db_res = CIBlockSection::GetList(array(), array("ID" => $arResult["ELEMENT"]["IBLOCK_SECTION_ID"]), false, array("ID", "NAME", "CODE"));
  98. if ($db_res && $res = $db_res->Fetch()):
  99. $arSection = $res;
  100. endif;
  101. endif;
  102. if (mb_strpos($arParams["URL_TEMPLATES_DETAIL"], "#SECTION_CODE_PATH#") !== false && intval($arResult["ELEMENT"]["IBLOCK_SECTION_ID"]) > 0):
  103. $db_res = CIBlockSection::GetNavChain(0, $arResult["ELEMENT"]["IBLOCK_SECTION_ID"], array("ID", "IBLOCK_SECTION_ID", "CODE"));
  104. while ($a = $db_res->Fetch())
  105. $SECTION_CODE_PATH .= urlencode($a["CODE"])."/";
  106. $SECTION_CODE_PATH = rtrim($SECTION_CODE_PATH, "/");
  107. endif;
  108. $url = str_replace(
  109. array("#ELEMENT_ID#", "#ID#", "#ELEMENT_CODE#", "#SECTION_ID#", "#SECTION_CODE#", "#SECTION_CODE_PATH#"),
  110. array($arResult["ELEMENT"]["ID"], $arResult["ELEMENT"]["ID"], $arResult["ELEMENT"]["CODE"],
  111. $arResult["ELEMENT"]["IBLOCK_SECTION_ID"], $arSection["CODE"], $SECTION_CODE_PATH), $url);
  112. if (intval($arResult["ELEMENT"]["PREVIEW_PICTURE"]) > 0):
  113. $arImage = CFile::GetFileArray($arResult["ELEMENT"]["PREVIEW_PICTURE"]);
  114. if (!empty($arImage)):
  115. $sImage = ($arResult["FORUM"]["ALLOW_IMG"] == "Y" ? "[IMG]".$arImage["SRC"]."[/IMG]" : '');
  116. endif;
  117. endif;
  118. $sElementPreview = $arResult["ELEMENT"]["~PREVIEW_TEXT"];
  119. if ($arAllow["HTML"] != "Y")
  120. $sElementPreview = strip_tags($sElementPreview);
  121. $arFields = Array(
  122. "POST_MESSAGE" => str_replace(array("#IMAGE#", "#TITLE#", "#BODY#", "#LINK#"),
  123. array($sImage, $arResult["ELEMENT"]["~NAME"], $sElementPreview, $url),
  124. $arParams["POST_FIRST_MESSAGE_TEMPLATE"]),
  125. "AUTHOR_ID" => $arUserStart["ID"],
  126. "AUTHOR_NAME" => $arUserStart["NAME"],
  127. "FORUM_ID" => $arParams["FORUM_ID"],
  128. "TOPIC_ID" => $TID,
  129. "APPROVED" => "Y",
  130. "NEW_TOPIC" => "Y",
  131. "PARAM1" => "IB",
  132. "PARAM2" => $arParams["ELEMENT_ID"]);
  133. $MID = CForumMessage::Add($arFields, false, array("SKIP_INDEXING" => "Y", "SKIP_STATISTIC" => "N"));
  134. if ($MID <= 0)
  135. {
  136. $arError[] = array(
  137. "code" => "message is not added 1",
  138. "title" => GetMessage("F_ERR_ADD_MESSAGE"));
  139. CForumTopic::Delete($TID);
  140. $TID = 0;
  141. }
  142. else
  143. {
  144. CIBlockElement::SetPropertyValues($arParams["ELEMENT_ID"], $PRODUCT_IBLOCK_ID, intval($TID), "FORUM_TOPIC_ID");
  145. if ($arParams["SUBSCRIBE_AUTHOR_ELEMENT"] == "Y" && intval($arResult["ELEMENT"]["~CREATED_BY"]) > 0)
  146. {
  147. if ($arUserStart["USER_PROFILE"] == "N"):
  148. $arUserStart["FORUM_USER_ID"] = CForumUser::Add(array("USER_ID" => $arResult["ELEMENT"]["~CREATED_BY"]));
  149. endif;
  150. if (intval($arUserStart["FORUM_USER_ID"]) > 0):
  151. CForumSubscribe::Add(array(
  152. "USER_ID" => $arResult["ELEMENT"]["~CREATED_BY"],
  153. "FORUM_ID" => $arParams["FORUM_ID"],
  154. "SITE_ID" => SITE_ID,
  155. "TOPIC_ID" => $TID,
  156. "NEW_TOPIC_ONLY" => "N"));
  157. BXClearCache(true, "/bitrix/forum/user/".$arResult["ELEMENT"]["~CREATED_BY"]."/subscribe/"); // Sorry, Max.
  158. endif;
  159. }
  160. }
  161. }
  162. // Second exit point
  163. if (!empty($arError)):
  164. $DB->Rollback();
  165. return false;
  166. else:
  167. $DB->Commit();
  168. endif;
  169. $FORUM_TOPIC_ID = $TID;
  170. }
  171. // 1.6.1 Add post comment
  172. $arFieldsG = array(
  173. "POST_MESSAGE" => $post["REVIEW_TEXT"],
  174. "AUTHOR_NAME" => trim($post["REVIEW_AUTHOR"]),
  175. "AUTHOR_EMAIL" => $post["REVIEW_EMAIL"],
  176. "USE_SMILES" => $post["REVIEW_USE_SMILES"],
  177. "PARAM2" => intval($arParams["ELEMENT_ID"]),
  178. "TITLE" => $PRODUCT_NAME);
  179. if (!empty($_FILES["REVIEW_ATTACH_IMG"]))
  180. {
  181. $arFieldsG["ATTACH_IMG"] = $_FILES["REVIEW_ATTACH_IMG"];
  182. }
  183. else
  184. {
  185. $arFiles = array();
  186. if (!empty($post["FILES"])):
  187. foreach ($post["FILES"] as $key):
  188. $arFiles[$key] = array("FILE_ID" => $key);
  189. if (!in_array($key, $post["FILES_TO_UPLOAD"]))
  190. $arFiles[$key]["del"] = "Y";
  191. endforeach;
  192. endif;
  193. if (!empty($_FILES)):
  194. foreach ($_FILES as $key => $val):
  195. if (mb_substr($key, 0, mb_strlen("FILE_NEW")) == "FILE_NEW" && !empty($val["name"])):
  196. if ($post["AJAX_POST"] == "Y")
  197. $val["name"] = $APPLICATION->ConvertCharset($val["name"], "UTF-8", LANG_CHARSET);
  198. $arFiles[] = $val;
  199. endif;
  200. endforeach;
  201. endif;
  202. if (!empty($arFiles))
  203. $arFieldsG["FILES"] = $arFiles;
  204. }
  205. $MID = ForumAddMessage(($FORUM_TOPIC_ID > 0 ? "REPLY" : "NEW"), $arParams["FORUM_ID"], $FORUM_TOPIC_ID, 0, $arFieldsG, $strErrorMessage, $arNote, false,
  206. $post["captcha_word"], 0, $post["captcha_code"]);
  207. if ($MID <= 0 || !empty($strErrorMessage)):
  208. $arError[] = array(
  209. "code" => "message is not added 2",
  210. "title" => (empty($strErrorMessage) ? GetMessage("F_ERR_ADD_MESSAGE") : $strErrorMessage));
  211. $arResult['RESULT'] = false;
  212. $arResult["OK_MESSAGE"] = '';
  213. else:
  214. if ($FORUM_TOPIC_ID <= 0):
  215. $res = CForumMessage::GetByID($MID);
  216. $FORUM_TOPIC_ID = intval($res["TOPIC_ID"]);
  217. endif;
  218. if ($arParams["AUTOSAVE"])
  219. $arParams["AUTOSAVE"]->Reset();
  220. $arResult["FORUM_TOPIC_ID"] = intval($FORUM_TOPIC_ID);
  221. ForumClearComponentCache($componentName);
  222. // SUBSCRIBE
  223. if ($post["TOPIC_SUBSCRIBE"] == "Y"):
  224. ForumSubscribeNewMessagesEx($arParams["FORUM_ID"], $FORUM_TOPIC_ID, "N", $strErrorMessage, $strOKMessage);
  225. BXClearCache(true, "/bitrix/forum/user/".$USER->GetID()."/subscribe/");
  226. endif;
  227. $strURL = (!empty($post["back_page"]) ? $post["back_page"] : $APPLICATION->GetCurPageParam("",
  228. array("MID", "SEF_APPLICATION_CUR_PAGE_URL", BX_AJAX_PARAM_ID, "result", "AJAX_CALL", "bxajaxid")));
  229. $bNotModerated = ($arResult["FORUM"]["MODERATION"] != "Y" || CForumNew::CanUserModerateForum($arParams["FORUM_ID"], $USER->GetUserGroupArray()));
  230. $strURL = ForumAddPageParams($strURL, array("MID" => $MID, "result" => ($bNotModerated ? "reply" : "not_approved")), true, false);
  231. $strURL .= ($bNotModerated ? "#message".$MID : "#reviewnote");
  232. if ($arParams["NO_REDIRECT_AFTER_SUBMIT"] != "Y")
  233. LocalRedirect($strURL);
  234. else
  235. $arResult['RESULT'] = $MID;
  236. endif;
  237. }
  238. elseif ($post["save_product_review"] == "Y") // preview
  239. {
  240. $arAllow["SMILES"] = ($post["REVIEW_USE_SMILES"] !="Y" ? "N" : $arResult["FORUM"]["ALLOW_SMILES"]);
  241. $arResult["MESSAGE_VIEW"] = array(
  242. "POST_MESSAGE_TEXT" => $post["REVIEW_TEXT"],
  243. "AUTHOR_NAME" => htmlspecialcharsbx($arResult["USER"]["SHOWED_NAME"]),
  244. "AUTHOR_ID" => intval($USER->GetID()),
  245. "AUTHOR_URL" => CComponentEngine::MakePathFromTemplate($arParams["URL_TEMPLATES_PROFILE_VIEW"], array("UID" => $USER->GetID())),
  246. "POST_DATE" => CForumFormat::DateFormat($arParams["DATE_TIME_FORMAT"], time()+CTimeZone::GetOffset()),
  247. "FILES" => array());
  248. $arFields = array(
  249. "FORUM_ID" => intval($arParams["FORUM_ID"]),
  250. "TOPIC_ID" => 0,
  251. "MESSAGE_ID" => 0,
  252. "USER_ID" => intval($USER->GetID()));
  253. $arFiles = array();
  254. $arFilesExists = array();
  255. $res = array();
  256. foreach ($_FILES as $key => $val):
  257. if ((mb_substr($key, 0, mb_strlen("FILE_NEW")) == "FILE_NEW") && !empty($val["name"])):
  258. if ($post["AJAX_POST"] == "Y")
  259. $val["name"] = $APPLICATION->ConvertCharset($val["name"], "UTF-8", LANG_CHARSET);
  260. $arFiles[] = $val;
  261. endif;
  262. endforeach;
  263. if (is_array($post["FILES"]))
  264. {
  265. $post["FILES_TO_UPLOAD"] = is_array($post["FILES_TO_UPLOAD"]) ? $post["FILES_TO_UPLOAD"] : array();
  266. foreach ($post["FILES"] as $key => $val)
  267. {
  268. if (in_array($val, $post["FILES_TO_UPLOAD"]))
  269. $arFilesExists[$val] = array("FILE_ID" => $val);
  270. else
  271. {
  272. $arFiles[$val] = array("FILE_ID" => $val, "del" => "Y");
  273. unset($post["FILES"][$key]);
  274. unset($post["FILES_TO_UPLOAD"][$key]);
  275. }
  276. }
  277. }
  278. if (!empty($arFiles)):
  279. $res = CForumFiles::Save($arFiles, $arFields);
  280. $res1 = $APPLICATION->GetException();
  281. if ($res1):
  282. $arError[] = array(
  283. "code" => "file upload error",
  284. "title" => $res1->GetString());
  285. endif;
  286. endif;
  287. $res = is_array($res) ? $res : array();
  288. foreach ($res as $key => $val):
  289. $arFilesExists[$key] = $val;
  290. endforeach;
  291. $arFilesExists = array_keys($arFilesExists);
  292. sort($arFilesExists);
  293. $arResult["MESSAGE_VIEW"]["FILES"] = $post["FILES"] = $arFilesExists;
  294. $arResult["MESSAGE_VIEW"]["POST_MESSAGE_TEXT"] = $parser->convert($post["REVIEW_TEXT"], $arAllow, "html", $arFilesExists);
  295. }
  296. if (isset($request['REVIEW_ACTION']))
  297. {
  298. $arFields = array();
  299. if (empty($arError))
  300. {
  301. if (isset($request['MID']) && intval($request['MID']) > 0)
  302. $arFields = array("MID" => intval($request['MID']));
  303. if (($result = ForumActions($request['REVIEW_ACTION'], $arFields, $strErrorMessage, $strOKMessage)) && $result)
  304. {
  305. ForumClearComponentCache($componentName);
  306. }
  307. }
  308. if (isset($request['AJAX_CALL']))
  309. {
  310. $APPLICATION->RestartBuffer();
  311. if (empty($arError))
  312. {
  313. $arRes = array('status' => $result, 'message' => ($result ? (!empty($arNote) ? $arNote[0]["text"] : $strOKMessage) : $strErrorMessage));
  314. }
  315. else
  316. {
  317. $arRes = array('status' => false, 'message' => $arError[0]['title']);
  318. }
  319. echo CUtil::PhpToJSObject($arRes);
  320. die();
  321. }
  322. else
  323. {
  324. LocalRedirect($APPLICATION->GetCurPageParam("", array("REVIEW_ACTION", "sessid", "MID")));
  325. }
  326. }
  327. ?>