PageRenderTime 61ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/components/bitrix/main.interface.grid/component.php

https://gitlab.com/alexprowars/bitrix
PHP | 358 lines | 255 code | 58 blank | 45 comment | 73 complexity | b28e5e8e59949f66d1b330c2685b5246 MD5 | raw file
  1. <?php
  2. /**
  3. * Bitrix Framework
  4. * @package bitrix
  5. * @subpackage main
  6. * @copyright 2001-2013 Bitrix
  7. */
  8. /**
  9. * Bitrix vars
  10. *
  11. * @var array $arParams
  12. * @var array $arResult
  13. * @var CBitrixComponent $this
  14. * @global CMain $APPLICATION
  15. * @global CUser $USER
  16. */
  17. if(!defined("B_PROLOG_INCLUDED") || B_PROLOG_INCLUDED !== true)
  18. die();
  19. if(!is_array($arParams["HEADERS"]))
  20. $arParams["HEADERS"] = array();
  21. if(!is_array($arParams["FOOTER"]))
  22. $arParams["FOOTER"] = array();
  23. if(!is_array($arParams["FILTER"]))
  24. $arParams["FILTER"] = array();
  25. if(!is_array($arParams["SORT"]))
  26. $arParams["SORT"] = array();
  27. if(!is_array($arParams["SORT_VARS"]))
  28. $arParams["SORT_VARS"] = array();
  29. if(!isset($arParams["SORT_VARS"]["by"]))
  30. $arParams["SORT_VARS"]["by"] = "by";
  31. if(!isset($arParams["SORT_VARS"]["order"]))
  32. $arParams["SORT_VARS"]["order"] = "order";
  33. if($arParams["SHOW_FORM_TAG"] !== 'N' && $arParams["SHOW_FORM_TAG"] !== false)
  34. $arParams["SHOW_FORM_TAG"] = true;
  35. else
  36. $arParams["SHOW_FORM_TAG"] = false;
  37. if($arParams["ACTION_ALL_ROWS"] === "Y" || $arParams["ACTION_ALL_ROWS"] === true)
  38. $arParams["ACTION_ALL_ROWS"] = true;
  39. else
  40. $arParams["ACTION_ALL_ROWS"] = false;
  41. if($arParams["EDITABLE"] === "N" || $arParams["EDITABLE"] === false)
  42. $arParams["EDITABLE"] = false;
  43. else
  44. $arParams["EDITABLE"] = true;
  45. if($arParams["USE_THEMES"] !== 'N' && $arParams["USE_THEMES"] !== false && CPageOption::GetOptionString("main.interface", "use_themes", "Y") !== "N")
  46. $arParams["USE_THEMES"] = true;
  47. else
  48. $arParams["USE_THEMES"] = false;
  49. $arParams["GRID_ID"] = preg_replace("/[^a-z0-9_]/i", "", $arParams["GRID_ID"]);
  50. if(!is_array($arParams["~NAV_PARAMS"]))
  51. {
  52. $arParams["~NAV_PARAMS"] = array();
  53. }
  54. $arResult["HEADERS"] = array();
  55. //*********************
  56. //get saved columns and sorting from user settings
  57. //*********************
  58. $grid_options = new CGridOptions($arParams["GRID_ID"]);
  59. $aOptions = $grid_options->GetOptions();
  60. if(!isset($aOptions["views"]["default"]["name"]))
  61. $aOptions["views"]["default"]["name"] = GetMessage("interface_grid_default_view");
  62. uasort(
  63. $aOptions["views"],
  64. function ($a, $b) {
  65. return strcmp($a["name"], $b["name"]);
  66. }
  67. );
  68. $arResult["OPTIONS"] = $aOptions;
  69. $arResult["GLOBAL_OPTIONS"] = CUserOptions::GetOption("main.interface", "global", array(), 0);
  70. if($arParams["USE_THEMES"])
  71. {
  72. if($arResult["GLOBAL_OPTIONS"]["theme_template"][SITE_TEMPLATE_ID] <> '')
  73. $arResult["GLOBAL_OPTIONS"]["theme"] = $arResult["GLOBAL_OPTIONS"]["theme_template"][SITE_TEMPLATE_ID];
  74. if($arResult["OPTIONS"]["theme"] == '')
  75. $arResult["OPTIONS"]["theme"] = $arResult["GLOBAL_OPTIONS"]["theme"];
  76. $arResult["OPTIONS"]["theme"] = preg_replace("/[^a-z0-9_.-]/i", "", $arResult["OPTIONS"]["theme"]);
  77. }
  78. else
  79. {
  80. $arResult["OPTIONS"]["theme"] = '';
  81. }
  82. //Admin can change common settings
  83. $arResult["IS_ADMIN"] = $USER->CanDoOperation('edit_other_settings');
  84. //*********************
  85. // Filter
  86. //*********************
  87. if(is_array($arParams["FILTER_PRESETS"]) && !empty($arParams["FILTER_PRESETS"]))
  88. $arResult["OPTIONS"]["filters"] = array_merge($arParams["FILTER_PRESETS"], $aOptions["filters"]);
  89. $arResult["FILTER"] = $grid_options->GetFilter($arParams["FILTER"]);
  90. $aVisRows = array();
  91. $aFilterTmp = explode(",", $aOptions["filter_rows"]);
  92. foreach($aFilterTmp as $field)
  93. {
  94. if(($f = trim($field)) <> "")
  95. $aVisRows[$f] = $f;
  96. }
  97. $arResult["FILTER_ROWS"] = array();
  98. foreach($arParams["FILTER"] as $field)
  99. {
  100. if($field["filtered"] == true)
  101. {
  102. $arResult["FILTER_ROWS"][$field["id"]] = true;
  103. continue;
  104. }
  105. if(isset($arResult["FILTER"][$field["id"]."_from"]))
  106. $flt = $arResult["FILTER"][$field["id"]."_from"];
  107. elseif(isset($arResult["FILTER"][$field["id"]."_to"]))
  108. $flt = $arResult["FILTER"][$field["id"]."_to"];
  109. else
  110. $flt = $arResult["FILTER"][$field["id"]];
  111. if(is_array($flt) && !empty($flt) || !is_array($flt) && $flt <> '')
  112. $arResult["FILTER_ROWS"][$field["id"]] = true;
  113. elseif(array_key_exists($field["id"], $aVisRows))
  114. $arResult["FILTER_ROWS"][$field["id"]] = true;
  115. elseif(!isset($aOptions["filter_rows"]))
  116. $arResult["FILTER_ROWS"][$field["id"]] = ($field["default"] == true);
  117. else
  118. $arResult["FILTER_ROWS"][$field["id"]] = false;
  119. }
  120. if(!in_array(true, $arResult["FILTER_ROWS"]))
  121. {
  122. foreach($arParams["FILTER"] as $field)
  123. {
  124. $arResult["FILTER_ROWS"][$field["id"]] = true;
  125. break;
  126. }
  127. }
  128. //*********************
  129. // Columns names
  130. //*********************
  131. $aCurView = $aOptions["views"][$aOptions["current_view"]];
  132. $arResult["COLS_NAMES"] = array();
  133. foreach($arParams["HEADERS"] as $i => $header)
  134. {
  135. $arResult["COLS_NAMES"][$header["id"]] = $header["name"];
  136. if(isset($aCurView["custom_names"][$header["id"]]))
  137. {
  138. $arParams["HEADERS"][$i]["original_name"] = $header["name"];
  139. $arParams["HEADERS"][$i]["name"] = htmlspecialcharsbx($aCurView["custom_names"][$header["id"]]);
  140. }
  141. }
  142. //*********************
  143. // Columns
  144. //*********************
  145. $aColsTmp = explode(",", $aCurView["columns"]);
  146. $aCols = array();
  147. foreach($aColsTmp as $col)
  148. {
  149. if(trim($col)<>"")
  150. $aCols[] = trim($col);
  151. }
  152. $bEmptyCols = empty($aCols);
  153. foreach($arParams["HEADERS"] as $param)
  154. {
  155. if(($bEmptyCols && $param["default"]==true) || in_array($param["id"], $aCols))
  156. $arResult["HEADERS"][$param["id"]] = $param;
  157. }
  158. if(!$bEmptyCols)
  159. {
  160. foreach($aCols as $i=>$col)
  161. $arResult["HEADERS"][$col]["__sort"] = $i;
  162. uasort(
  163. $arResult["HEADERS"],
  164. function ($a, $b) {
  165. if ($a["__sort"] == $b["__sort"])
  166. {
  167. return 0;
  168. }
  169. return ($a["__sort"] < $b["__sort"] ? -1 : 1);
  170. }
  171. );
  172. }
  173. //*********************
  174. // Sorting and URL
  175. //*********************
  176. $uri = new \Bitrix\Main\Web\Uri($this->request->getRequestUri());
  177. $uri->deleteParams(\Bitrix\Main\HttpRequest::getSystemParameters());
  178. $uri->deleteParams(array("bxajaxid", "AJAX_CALL", $arParams["SORT_VARS"]["by"], $arParams["SORT_VARS"]["order"]));
  179. if($arParams["FORM_ID"] <> '' && $arParams["TAB_ID"] <> '')
  180. {
  181. $uri->addParams(array($arParams["FORM_ID"].'_active_tab' => $arParams["TAB_ID"]));
  182. }
  183. $arResult["CURRENT_URL"] = $uri->getUri();
  184. $sep = (mb_strpos($arResult["CURRENT_URL"], "?") !== false? "&":"?");
  185. $sortBy = key($arParams["SORT"]);
  186. $sortOrder = current($arParams["SORT"]);
  187. foreach($arResult["HEADERS"] as $id=>$header)
  188. {
  189. if($header["sort"] <> '')
  190. {
  191. $arResult["HEADERS"][$id]["sort_state"] = "";
  192. if(mb_strtolower($header["sort"]) == mb_strtolower($sortBy))
  193. {
  194. if(mb_strtolower($sortOrder) == "desc")
  195. $arResult["HEADERS"][$id]["sort_state"] = "desc";
  196. else
  197. $arResult["HEADERS"][$id]["sort_state"] = "asc";
  198. }
  199. $arResult["HEADERS"][$id]["sort_url"] = htmlspecialcharsbx($arResult["CURRENT_URL"].$sep.$arParams["SORT_VARS"]["by"]."=".$header["sort"]."&".$arParams["SORT_VARS"]["order"]."=");
  200. $arResult["HEADERS"][$id]["order"] = ($header["order"] == 'desc'? 'desc':'asc');
  201. }
  202. }
  203. //*********************
  204. // Editable columns detection
  205. //*********************
  206. $arResult["EDIT_DATE"] = false;
  207. $arResult["ALLOW_EDIT"] = false;
  208. $arResult["ALLOW_INLINE_EDIT"] = false;
  209. $arResult["COLS_EDIT_META"] = array();
  210. foreach($arResult["HEADERS"] as $header)
  211. {
  212. if($arParams["EDITABLE"] && isset($header["editable"]) && $header["editable"] !== false)
  213. {
  214. $arResult["ALLOW_EDIT"] = true;
  215. $arResult["ALLOW_INLINE_EDIT"] = true;
  216. if($header["type"] == "date")
  217. $arResult["EDIT_DATE"] = true;
  218. }
  219. $arResult["COLS_EDIT_META"][$header["id"]] = array(
  220. "editable" => (isset($header["editable"]) && $header["editable"] !== false),
  221. "type"=>($header["type"] <> ''? $header["type"] : "text"),
  222. );
  223. if($arParams["EDITABLE"] && is_array($header["editable"]))
  224. foreach($header["editable"] as $attr => $val)
  225. $arResult["COLS_EDIT_META"][$header["id"]][$attr] = $val;
  226. }
  227. //*********************
  228. // Editable Data
  229. //*********************
  230. $arResult["DATA_FOR_EDIT"] = array();
  231. if($arResult["ALLOW_EDIT"])
  232. {
  233. $arResult["ALLOW_EDIT"] = false;
  234. foreach($arParams["ROWS"] as $row)
  235. {
  236. if($row["editable"] !== false)
  237. {
  238. $arResult["ALLOW_EDIT"] = true;
  239. $id = ($row["id"] <> ''? $row["id"] : $row["data"]["ID"]);
  240. foreach($arResult["HEADERS"] as $header)
  241. {
  242. if(isset($header["editable"]) && $header["editable"] !== false)
  243. {
  244. if(isset($row["editable"][$header["id"]]) && $row["editable"][$header["id"]] === false)
  245. $arResult["DATA_FOR_EDIT"][$id][$header["id"]] = false;
  246. else
  247. $arResult["DATA_FOR_EDIT"][$id][$header["id"]] = $row["data"]['~'.$header["id"]];
  248. }
  249. }
  250. }
  251. }
  252. }
  253. if($arParams["EDITABLE"] && is_array($arParams["ACTIONS"]) && count($arParams["ACTIONS"]) > 0)
  254. $arResult["ALLOW_EDIT"] = true;
  255. //*********************
  256. // Navigation
  257. //*********************
  258. if($arParams["NAV_STRING"] <> '')
  259. {
  260. $arResult["NAV_STRING"] = $arParams["~NAV_STRING"];
  261. }
  262. elseif(is_object($arParams["NAV_OBJECT"]))
  263. {
  264. if(($nav = $arParams["NAV_OBJECT"]) instanceof \Bitrix\Main\UI\PageNavigation)
  265. {
  266. $params = array_merge(
  267. array(
  268. "NAV_OBJECT" => $nav,
  269. "PAGE_WINDOW" => 5,
  270. "SHOW_ALWAYS" => true,
  271. ),
  272. $arParams["~NAV_PARAMS"]
  273. );
  274. ob_start();
  275. $APPLICATION->IncludeComponent(
  276. "bitrix:main.pagenavigation",
  277. "modern",
  278. $params,
  279. false,
  280. array(
  281. "HIDE_ICONS" => "Y",
  282. )
  283. );
  284. $arResult["NAV_STRING"] = ob_get_clean();
  285. }
  286. else
  287. {
  288. /** @var CDBResult $nav */
  289. $nav = $arParams["NAV_OBJECT"];
  290. $nav->nPageWindow = 5;
  291. //dirty hack
  292. if($arParams["FORM_ID"] <> '' && $arParams["TAB_ID"] <> '')
  293. $_GET[$arParams["FORM_ID"].'_active_tab'] = $arParams["TAB_ID"];
  294. $arResult["NAV_STRING"] = $nav->GetPageNavStringEx($dummy, "", "modern", true, null, $arParams["~NAV_PARAMS"]);
  295. }
  296. }
  297. //*********************
  298. // Self-explaining
  299. //*********************
  300. $this->IncludeComponentTemplate();