PageRenderTime 46ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/components/bitrix/forum.search/component.php

https://gitlab.com/alexprowars/bitrix
PHP | 304 lines | 271 code | 7 blank | 26 comment | 61 complexity | 54522b8e03a3abaea51a1f74d75ca977 MD5 | raw file
  1. <?if(!defined("B_PROLOG_INCLUDED") || B_PROLOG_INCLUDED!==true)die();
  2. if (!CModule::IncludeModule("forum")):
  3. ShowError(GetMessage("F_NO_MODULE"));
  4. return 0;
  5. elseif (!CModule::IncludeModule("search")):
  6. ShowError(GetMessage("F_NO_SEARCH_MODULE"));
  7. return 0;
  8. endif;
  9. if (!function_exists("__array_merge"))
  10. {
  11. function __array_merge($arr1, $arr2)
  12. {
  13. $arResult = $arr1;
  14. foreach ($arr2 as $key2 => $val2)
  15. {
  16. if (!array_key_exists($key2, $arResult))
  17. {
  18. $arResult[$key2] = $val2;
  19. continue;
  20. }
  21. elseif ($val2 == $arResult[$key2])
  22. continue;
  23. elseif (!is_array($arResult[$key2]))
  24. $arResult[$key2] = array($arResult[$key2]);
  25. $arResult[$key2] = __array_merge($arResult[$key2], $val2);
  26. }
  27. return $arResult;
  28. }
  29. }
  30. /********************************************************************
  31. Input params
  32. ********************************************************************/
  33. /***************** BASE ********************************************/
  34. $q = trim($_REQUEST["q"]);
  35. $arResult["q"] = htmlspecialcharsbx($q);
  36. $arParams["FID"] = (!empty($_REQUEST["FID"]) ? $_REQUEST["FID"] : $_REQUEST["FORUM_ID"]);
  37. $arParams["FID"] = (!empty($arParams["FID"]) ? $arParams["FID"] : $_REQUEST["find_forum"]);
  38. $arParams["FID"] = is_array($arParams["FID"]) ? $arParams["FID"] : array($arParams["FID"]);
  39. /***************** URL *********************************************/
  40. $URL_NAME_DEFAULT = array(
  41. "index" => "",
  42. "read" => "PAGE_NAME=read&FID=#FID#&TID=#TID#",
  43. "message" => "PAGE_NAME=message&FID=#FID#&TID=#TID#&MID=#MID#");
  44. foreach ($URL_NAME_DEFAULT as $URL => $URL_VALUE)
  45. {
  46. if (trim($arParams["URL_TEMPLATES_".mb_strtoupper($URL)]) == '')
  47. $arParams["URL_TEMPLATES_".mb_strtoupper($URL)] = $APPLICATION->GetCurPage()."?".$URL_VALUE;
  48. $arParams["~URL_TEMPLATES_".mb_strtoupper($URL)] = $arParams["URL_TEMPLATES_".mb_strtoupper($URL)];
  49. $arParams["URL_TEMPLATES_".mb_strtoupper($URL)] = htmlspecialcharsbx($arParams["~URL_TEMPLATES_".mb_strtoupper($URL)]);
  50. }
  51. /***************** ADDITIONAL **************************************/
  52. $arParams["SHOW_FORUM_ANOTHER_SITE"] = ($arParams["SHOW_FORUM_ANOTHER_SITE"] == "Y" ? "Y" : "N");
  53. $arParams["FID_RANGE"] = (is_array($arParams["FID_RANGE"]) && !empty($arParams["FID_RANGE"]) ? $arParams["FID_RANGE"] : array());
  54. $arParams["PAGE_NAVIGATION_TEMPLATE"] = trim($arParams["PAGE_NAVIGATION_TEMPLATE"]);
  55. $arParams["PAGE_NAVIGATION_WINDOW"] = intval(intVal($arParams["PAGE_NAVIGATION_WINDOW"]) > 0 ? $arParams["PAGE_NAVIGATION_WINDOW"] : 11);
  56. $arParams["DATE_FORMAT"] = trim(empty($arParams["DATE_FORMAT"]) ? $DB->DateFormatToPHP(CSite::GetDateFormat("SHORT")) : $arParams["DATE_FORMAT"]);
  57. $arParams["TOPICS_PER_PAGE"] = intval(intVal($arParams["TOPICS_PER_PAGE"]) > 0 ? $arParams["TOPICS_PER_PAGE"] : COption::GetOptionString("forum", "TOPICS_PER_PAGE", "10"));
  58. /***************** STANDART ****************************************/
  59. $arParams["SET_TITLE"] = ($arParams["SET_TITLE"] == "N" ? "N" : "Y");
  60. $arParams["SET_NAVIGATION"] = ($arParams["SET_NAVIGATION"] == "N" ? "N" : "Y");
  61. if ($arParams["CACHE_TYPE"] == "Y" || ($arParams["CACHE_TYPE"] == "A" && COption::GetOptionString("main", "component_cache_on", "Y") == "Y"))
  62. $arParams["CACHE_TIME"] = intval($arParams["CACHE_TIME"]);
  63. else
  64. $arParams["CACHE_TIME"] = 0;
  65. /********************************************************************
  66. /Input params
  67. ********************************************************************/
  68. /********************************************************************
  69. Default values
  70. ********************************************************************/
  71. $arResult["FID"] = (is_array($arParams["FID"]) ? $arParams["FID"] : array($arParams["FID"]));
  72. $arResult["ERROR_MESSAGE"] = "";
  73. $arResult["SHOW_FORUMS"] = "Y";
  74. $arResult["SHOW_RESULT"] = ($q <> '' || !empty($_REQUEST["tags"]) ? "Y" : "N");
  75. $arResult["FORUMS"] = array();
  76. $arResult["GROUPS_FORUMS"] = array(); // declared in result_modifier.php
  77. $arResult["GROUPS"] = CForumGroup::GetByLang(LANGUAGE_ID);
  78. $arResult["order"] = array("active" => "relevance");
  79. $aSort = array("RANK"=>"DESC", "DATE_CHANGE"=>"DESC");
  80. $cache = new CPHPCache();
  81. $cache_path_main = str_replace(array(":", "//"), "/", "/".SITE_ID."/".$componentName."/");
  82. $arResult["URL"] = array(
  83. "INDEX" => CComponentEngine::MakePathFromTemplate($arParams["URL_TEMPLATES_INDEX"], array()),
  84. "~INDEX" => CComponentEngine::MakePathFromTemplate($arParams["~URL_TEMPLATES_INDEX"], array()));
  85. /********************************************************************
  86. /Default values
  87. ********************************************************************/
  88. /********************************************************************
  89. Data
  90. ********************************************************************/
  91. /************** Forums *********************************************/
  92. $arFilter = array("INDEXATION" => "Y");
  93. $arForums = array();
  94. $arGroups = array();
  95. if ($arParams["SHOW_FORUM_ANOTHER_SITE"] == "N" || !CForumUser::IsAdmin())
  96. $arFilter["LID"] = SITE_ID;
  97. if (!empty($arParams["FID_RANGE"]))
  98. $arFilter["@ID"] = $arParams["FID_RANGE"];
  99. if (!CForumUser::IsAdmin()):
  100. $arFilter["PERMS"] = array($USER->GetGroups(), 'A');
  101. $arFilter["ACTIVE"] = "Y";
  102. endif;
  103. $cache_id = "forum_forums_".serialize($arFilter);
  104. $cache_path = $cache_path_main."forums";
  105. if ($arParams["CACHE_TIME"] > 0 && $cache->InitCache($arParams["CACHE_TIME"], $cache_id, $cache_path))
  106. {
  107. $res = $cache->GetVars();
  108. $arForums = CForumCacheManager::Expand($res["arForums"]);
  109. }
  110. $arForums = (is_array($arForums) ? $arForums : array());
  111. if (empty($arForums))
  112. {
  113. $db_res = CForumNew::GetListEx(array("FORUM_GROUP_SORT"=>"ASC", "FORUM_GROUP_ID"=>"ASC", "SORT"=>"ASC", "NAME"=>"ASC"), $arFilter);
  114. if ($db_res && ($res = $db_res->GetNext()))
  115. {
  116. do
  117. {
  118. $arForums[$res["ID"]] = $res;
  119. } while ($res = $db_res->GetNext());
  120. }
  121. if ($arParams["CACHE_TIME"] > 0):
  122. $cache->StartDataCache($arParams["CACHE_TIME"], $cache_id, $cache_path);
  123. $cache->EndDataCache(array("arForums" => CForumCacheManager::Compress($arForums)));
  124. endif;
  125. }
  126. $arResult["FORUMS"] = $arForums;
  127. if (empty($arResult["FORUMS"])):
  128. ShowError(GetMessage("F_ERROR_NO_FORUMS"));
  129. return false;
  130. endif;
  131. $arForums = array();
  132. foreach ($arResult["FORUMS"] as $key => $res)
  133. {
  134. $arForums[$res["FORUM_GROUP_ID"]][$key] = $res;
  135. }
  136. foreach ($arForums as $PARENT_ID => $res)
  137. {
  138. $bResult = true;
  139. $res = array("FORUMS" => $res);
  140. while ($PARENT_ID > 0)
  141. {
  142. if (!array_key_exists($PARENT_ID, $arResult["GROUPS"]))
  143. {
  144. $bResult = false;
  145. $PARENT_ID = false;
  146. break;
  147. }
  148. $res = array($PARENT_ID => __array_merge($arResult["GROUPS"][$PARENT_ID], $res));
  149. $PARENT_ID = $arResult["GROUPS"][$PARENT_ID]["PARENT_ID"];
  150. $res = array("GROUPS" => $res);
  151. if ($PARENT_ID > 0)
  152. $res = __array_merge($arResult["GROUPS"][$PARENT_ID], $res);
  153. }
  154. if ($bResult == true)
  155. $arGroups = __array_merge($arGroups, $res);
  156. }
  157. $arResult["GROUPS_FORUMS"] = $arGroups;
  158. $arParams["FID"] = array_intersect($arParams["FID"], array_keys($arResult["FORUMS"]));
  159. /************** Search data ****************************************/
  160. if ($_REQUEST["q"] <> '' || !empty($_REQUEST["tags"])):
  161. if ($_REQUEST["order"] == "date"):
  162. $arResult["order"]["active"] = "date";
  163. $aSort = array("DATE_CHANGE"=>"DESC");
  164. elseif($_REQUEST["order"] == "topic"):
  165. $arResult["order"]["active"] = "topic";
  166. $aSort = array("PARAM2"=>"DESC", "DATE_CHANGE"=>"ASC");
  167. endif;
  168. $arFilter1 = array(
  169. "MODULE_ID" => "forum",
  170. "SITE_ID" => SITE_ID,
  171. "QUERY" => $q,
  172. "TAGS" => $_REQUEST["tags"] ? $_REQUEST["tags"] : "");
  173. if (intval($_REQUEST["DATE_CHANGE"]) > 0)
  174. {
  175. $arFilter1["DATE_CHANGE"] = Date(CDatabase::DateFormatToPHP(CLang::GetDateFormat("FULL", LANGUAGE_ID)),
  176. time()-(intval($_REQUEST["DATE_CHANGE"])*24*3600)+CTimeZone::GetOffset());
  177. }
  178. $arFilter2 = array();
  179. if (!empty($arParams["FID_RANGE"]) || !empty($arParams["FID"]))
  180. {
  181. $arFilter2["PARAM1"] = empty($arParams["FID_RANGE"]) ? array() : array_keys($arResult["FORUMS"]);
  182. $arFilter2["PARAM1"] = empty($arParams["FID"]) ? $arFilter2["PARAM1"] : $arParams["FID"];
  183. }
  184. $obSearch = new CSearch();
  185. //When restart option is set we will ignore error on query with only stop words
  186. $obSearch->SetOptions(array(
  187. "ERROR_ON_EMPTY_STEM" => $arParams["RESTART"] != "Y",
  188. "NO_WORD_LOGIC" => $arParams["NO_WORD_LOGIC"] == "Y",
  189. ));
  190. $obSearch->Search($arFilter1, $aSort, array($arFilter2));
  191. if ($obSearch->errorno != 0):
  192. $arResult["ERROR_MESSAGE"] = $obSearch->error;
  193. else:
  194. $obSearch->NavStart($arParams["TOPICS_PER_PAGE"], false);
  195. $obSearch->nPageWindow = $arParams["PAGE_NAVIGATION_WINDOW"];
  196. $arResult["NAV_RESULT"] = $obSearch;
  197. $arResult["NAV_STRING"] = $obSearch->GetPageNavStringEx($navComponentObject, GetMessage("FL_TOPIC_LIST"), $arParams["PAGE_NAVIGATION_TEMPLATE"]);
  198. $arResult["EMPTY"] = "Y";
  199. $topics = array();
  200. if ($res = $obSearch->GetNext())
  201. {
  202. $arResult["order"]["~relevance"] = $APPLICATION->GetCurPageParam(
  203. "q=".urlencode($q).(!empty($arParams["FID"]) ? "&FORUM_ID=".$arParams["FID"] : ""),
  204. array("FORUM_ID", "q", "order", "s", BX_AJAX_PARAM_ID));
  205. $arResult["order"]["~topic"] = $APPLICATION->GetCurPageParam(
  206. "q=".urlencode($q).
  207. (!empty($arParams["FID"]) ? "&FORUM_ID=".$arParams["FID"] : "").
  208. "&order=topic", array("FORUM_ID", "q", "order", "s", BX_AJAX_PARAM_ID));
  209. $arResult["order"]["~date"] = $APPLICATION->GetCurPageParam(
  210. "q=".urlencode($q).
  211. (!empty($arParams["FID"]) ? "&FORUM_ID=".$arParams["FID"] : "").
  212. "&order=date", array("FORUM_ID", "q", "order", "s", BX_AJAX_PARAM_ID));
  213. $arResult["order"]["relevance"] = htmlspecialcharsbx($arResult["order"]["~relevance"]);
  214. $arResult["order"]["topic"] = htmlspecialcharsbx($arResult["order"]["~topic"]);
  215. $arResult["order"]["date"] = htmlspecialcharsbx($arResult["order"]["~date"]);
  216. $arResult["EMPTY"] = "N";
  217. $arResult["TOPICS"] = [];
  218. do
  219. {
  220. if (intval($res["ITEM_ID"]) > 0)
  221. {
  222. $res["URL"] = CComponentEngine::MakePathFromTemplate($arParams["URL_TEMPLATES_MESSAGE"],
  223. array("FID" => $res["PARAM1"], "TID"=>$res["PARAM2"], "TITLE_SEO"=>$res["PARAM2"], "MID" => $res["ITEM_ID"]));
  224. $res["~URL"] = CComponentEngine::MakePathFromTemplate($arParams["~URL_TEMPLATES_MESSAGE"],
  225. array("FID" => $res["PARAM1"], "TID"=>$res["PARAM2"], "TITLE_SEO"=>$res["PARAM2"], "MID" => $res["ITEM_ID"]));
  226. }
  227. else
  228. {
  229. $res["URL"] = CComponentEngine::MakePathFromTemplate($arParams["URL_TEMPLATES_READ"],
  230. array("FID" => $res["PARAM1"], "TID"=>$res["PARAM2"], "TITLE_SEO"=>$res["PARAM2"], "MID" => "s"));
  231. $res["~URL"] = CComponentEngine::MakePathFromTemplate($arParams["~URL_TEMPLATES_READ"],
  232. array("FID" => $res["PARAM1"], "TID"=>$res["PARAM2"], "TITLE_SEO"=>$res["PARAM2"], "MID" => "s"));
  233. }
  234. $res["BODY_FORMATED"] = preg_replace("#\[/?(quote|b|i|u|code|url).*?\]#i", "", $res["BODY_FORMATED"]);
  235. $res["DATE_CHANGE"] = CForumFormat::DateFormat($arParams["DATE_FORMAT"], MakeTimeStamp($res["DATE_CHANGE"], CSite::GetDateFormat()));
  236. if (mb_strpos($res["SITE_URL"], "#message") !== false)
  237. $res["SITE_URL"] = mb_substr($res["SITE_URL"], 0, mb_strpos($res["SITE_URL"], "#message"));
  238. $res["TAGS"] = array();
  239. if (!empty($res["~TAGS_FORMATED"]))
  240. {
  241. foreach ($res["~TAGS_FORMATED"] as $name => $tag)
  242. {
  243. $tags = $tag;
  244. $res["TAGS"][] = array(
  245. "URL" => $APPLICATION->GetCurPageParam("tags=".urlencode($tags), array("tags")),
  246. "TAG_NAME" => htmlspecialcharsbx($name),
  247. );
  248. }
  249. }
  250. $topics[$res["PARAM2"]] = (!!$topics[$res["PARAM2"]] ? $topics[$res["PARAM2"]] : array());
  251. $topics[$res["PARAM2"]][] = count($arResult["TOPICS"]);
  252. $arResult["TOPICS"][] = $res;
  253. }
  254. while ($res = $obSearch->GetNext());
  255. }
  256. if (mb_strpos($arParams["URL_TEMPLATES_READ"], "#TITLE_SEO#") !== false && !empty($topics))
  257. {
  258. $db_res2 = CForumTopic::GetList(array(), array("@ID" => array_keys($topics)));
  259. while($db_res2 && ($res2 = $db_res2->Fetch()))
  260. {
  261. if (array_key_exists($res2["ID"], $topics))
  262. {
  263. foreach ($topics[$res2["ID"]] as $key)
  264. {
  265. $res = $arResult["TOPICS"][$key];
  266. if (intval($res["ITEM_ID"]) > 0)
  267. {
  268. $res["URL"] = CComponentEngine::MakePathFromTemplate($arParams["URL_TEMPLATES_MESSAGE"],
  269. array("FID" => $res["PARAM1"], "TID"=>$res["PARAM2"], "TITLE_SEO"=>$res2["TITLE_SEO"], "MID" => $res["ITEM_ID"]));
  270. $res["~URL"] = CComponentEngine::MakePathFromTemplate($arParams["~URL_TEMPLATES_MESSAGE"],
  271. array("FID" => $res["PARAM1"], "TID"=>$res["PARAM2"], "TITLE_SEO"=>$res2["TITLE_SEO"], "MID" => $res["ITEM_ID"]));
  272. }
  273. else
  274. {
  275. $res["URL"] = CComponentEngine::MakePathFromTemplate($arParams["URL_TEMPLATES_READ"],
  276. array("FID" => $res["PARAM1"], "TID"=>$res["PARAM2"], "TITLE_SEO"=>$res2["TITLE_SEO"], "MID" => "s"));
  277. $res["~URL"] = CComponentEngine::MakePathFromTemplate($arParams["~URL_TEMPLATES_READ"],
  278. array("FID" => $res["PARAM1"], "TID"=>$res["PARAM2"], "TITLE_SEO"=>$res2["TITLE_SEO"], "MID" => "s"));
  279. }
  280. $arResult["TOPICS"][$key] = $res;
  281. }
  282. }
  283. }
  284. }
  285. endif;
  286. endif;
  287. /************** For custom template *******************************/
  288. $arResult["index"] = $arResult["URL"]["INDEX"];
  289. /******************************************************************/
  290. $this->IncludeComponentTemplate();
  291. /******************************************************************/
  292. if ($arParams["SET_NAVIGATION"] != "N")
  293. $APPLICATION->AddChainItem(GetMessage("F_TITLE"));
  294. /******************************************************************/
  295. if ($arParams["SET_TITLE"] != "N")
  296. $APPLICATION->SetTitle(GetMessage("F_TITLE"));
  297. ?>