PageRenderTime 4452ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/components/bitrix/sender.contact.set.list/class.php

https://gitlab.com/alexprowars/bitrix
PHP | 309 lines | 252 code | 49 blank | 8 comment | 26 complexity | 3b85fe10631fa76033cf10935c020465 MD5 | raw file
  1. <?
  2. use Bitrix\Main\Error;
  3. use Bitrix\Main\ErrorCollection;
  4. use Bitrix\Main\Grid;
  5. use Bitrix\Main\Grid\Options as GridOptions;
  6. use Bitrix\Main\Localization\Loc;
  7. use Bitrix\Main\UI\Filter\Options as FilterOptions;
  8. use Bitrix\Sender\ListTable;
  9. use Bitrix\Sender\Security;
  10. use Bitrix\Sender\UI\PageNavigation;
  11. if (!defined("B_PROLOG_INCLUDED") || B_PROLOG_INCLUDED !== true)
  12. {
  13. die();
  14. }
  15. if (!Bitrix\Main\Loader::includeModule('sender'))
  16. {
  17. ShowError('Module `sender` not installed');
  18. die();
  19. }
  20. Loc::loadMessages(__FILE__);
  21. class SenderContactSetListComponent extends \CBitrixComponent
  22. {
  23. /** @var ErrorCollection $errors */
  24. protected $errors;
  25. protected function checkRequiredParams()
  26. {
  27. return true;
  28. }
  29. protected function initParams()
  30. {
  31. $this->arParams['PATH_TO_CONTACT_LIST'] = isset($this->arParams['PATH_TO_CONTACT_LIST']) ? $this->arParams['PATH_TO_CONTACT_LIST'] : '';
  32. $this->arParams['GRID_ID'] = isset($this->arParams['GRID_ID']) ? $this->arParams['GRID_ID'] : 'SENDER_CONTACT_SET_LIST_GRID';
  33. $this->arParams['FILTER_ID'] = isset($this->arParams['FILTER_ID']) ? $this->arParams['FILTER_ID'] : $this->arParams['GRID_ID'] . '_FILTER';
  34. $this->arParams['SET_TITLE'] = isset($this->arParams['SET_TITLE']) ? $this->arParams['SET_TITLE'] == 'Y' : true;
  35. $this->arParams['CAN_EDIT'] = isset($this->arParams['CAN_EDIT'])
  36. ?
  37. $this->arParams['CAN_EDIT']
  38. :
  39. Security\Access::getInstance()->canModifySegments();
  40. }
  41. protected function preparePost()
  42. {
  43. $action = $this->request->get('action_button_' . $this->arParams['GRID_ID']);
  44. switch ($action)
  45. {
  46. case 'edit':
  47. $editFields = $this->request->get('FIELDS');
  48. $editFields = \Bitrix\Main\Text\Encoding::convertEncoding($editFields, 'UTF-8', LANG_CHARSET);
  49. if (!is_array($editFields))
  50. {
  51. $editFields = [];
  52. }
  53. foreach ($editFields as $id => $fields)
  54. {
  55. $fields = $this->filterActionFieldsByGridColumns($fields);
  56. ListTable::update($id, $fields);
  57. }
  58. break;
  59. case 'delete':
  60. $ids = $this->request->get('ID');
  61. if (!is_array($ids))
  62. {
  63. $ids = array($ids);
  64. }
  65. foreach ($ids as $id)
  66. {
  67. ListTable::delete($id);
  68. }
  69. break;
  70. }
  71. }
  72. protected function prepareResult()
  73. {
  74. /* Set title */
  75. if ($this->arParams['SET_TITLE'])
  76. {
  77. /**@var CAllMain*/
  78. $GLOBALS['APPLICATION']->SetTitle(Loc::getMessage('SENDER_CONTACT_SET_LIST_TITLE'));
  79. }
  80. if (!Security\Access::getInstance()->canViewSegments())
  81. {
  82. Security\AccessChecker::addError($this->errors);
  83. return false;
  84. }
  85. $this->arResult['ERRORS'] = [];
  86. $this->arResult['ROWS'] = [];
  87. if ($this->request->isPost() && check_bitrix_sessid() && $this->arParams['CAN_EDIT'])
  88. {
  89. $this->preparePost();
  90. }
  91. // set ui filter
  92. $this->setUiFilter();
  93. $this->setUiFilterPresets();
  94. // set ui grid columns
  95. $this->setUiGridColumns();
  96. // create nav
  97. $nav = new PageNavigation("page-sender-set-list");
  98. $nav->allowAllRecords(false)->setPageSize(10)->initFromUri();
  99. // get rows
  100. $list = ListTable::getList(array(
  101. 'select' => array(
  102. 'ID', 'NAME', 'CODE',
  103. ),
  104. 'filter' => $this->getDataFilter(),
  105. 'offset' => $nav->getOffset(),
  106. 'limit' => $nav->getLimit(),
  107. 'count_total' => true,
  108. 'order' => $this->getGridOrder()
  109. ));
  110. foreach ($list as $item)
  111. {
  112. $this->arResult['ROWS'][] = $item;
  113. }
  114. $this->arResult['TOTAL_ROWS_COUNT'] = $list->getCount();
  115. // set rec count to nav
  116. $nav->setRecordCount($list->getCount());
  117. $this->arResult['NAV_OBJECT'] = $nav;
  118. return true;
  119. }
  120. protected function getDataFilter()
  121. {
  122. $filterOptions = new FilterOptions($this->arParams['FILTER_ID']);
  123. $requestFilter = $filterOptions->getFilter($this->arResult['FILTERS']);
  124. $searchString = trim($filterOptions->getSearchString());
  125. $filter = [];
  126. if ($searchString)
  127. {
  128. $filter['NAME'] = '%' . $searchString . '%';
  129. }
  130. if (isset($requestFilter['NAME']) && $requestFilter['NAME'])
  131. {
  132. $filter['NAME'] = '%' . $requestFilter['NAME'] . '%';
  133. }
  134. if (isset($requestFilter['CODE']) && $requestFilter['CODE'])
  135. {
  136. $filter['CODE'] = '%' . $requestFilter['CODE'] . '%';
  137. }
  138. return $filter;
  139. }
  140. protected function filterActionFieldsByGridColumns(array $fields)
  141. {
  142. $list = [];
  143. foreach ($this->getUiGridColumns() as $column)
  144. {
  145. if (!isset($column['editable']) || !$column['editable'])
  146. {
  147. continue;
  148. }
  149. if (!isset($fields[$column['id']]))
  150. {
  151. continue;
  152. }
  153. $list[$column['id']] = $fields[$column['id']];
  154. }
  155. return $list;
  156. }
  157. protected function getGridOrder()
  158. {
  159. $defaultSort = array('ID' => 'DESC');
  160. $gridOptions = new GridOptions($this->arParams['GRID_ID']);
  161. $sorting = $gridOptions->getSorting(array('sort' => $defaultSort));
  162. $by = key($sorting['sort']);
  163. $order = mb_strtoupper(current($sorting['sort'])) === 'ASC' ? 'ASC' : 'DESC';
  164. $list = array();
  165. foreach ($this->getUiGridColumns() as $column)
  166. {
  167. if (!isset($column['sort']) || !$column['sort'])
  168. {
  169. continue;
  170. }
  171. $list[] = $column['sort'];
  172. }
  173. if (!in_array($by, $list))
  174. {
  175. return $defaultSort;
  176. }
  177. return array($by => $order);
  178. }
  179. protected function setUiGridColumns()
  180. {
  181. $this->arResult['COLUMNS'] = $this->getUiGridColumns();
  182. }
  183. protected function getUiGridColumns()
  184. {
  185. return [
  186. [
  187. "id" => "ID",
  188. "name" => "ID",
  189. "sort" => "ID",
  190. "default" => false
  191. ],
  192. [
  193. "id" => "NAME",
  194. "name" => Loc::getMessage('SENDER_CONTACT_SET_LIST_UI_COLUMN_NAME'),
  195. "sort" => "NAME",
  196. "default" => true,
  197. "editable" => ["TYPE" => Grid\Editor\Types::TEXT]
  198. ],
  199. [
  200. "id" => "CODE",
  201. "name" => Loc::getMessage('SENDER_CONTACT_SET_LIST_UI_COLUMN_CODE'),
  202. "sort" => "CODE",
  203. "default" => true,
  204. "editable" => ["TYPE" => Grid\Editor\Types::TEXT]
  205. ],
  206. ];
  207. }
  208. protected function setUiFilter()
  209. {
  210. $this->arResult['FILTERS'] = array(
  211. array(
  212. "id" => "NAME",
  213. "name" => Loc::getMessage('SENDER_CONTACT_SET_LIST_UI_COLUMN_NAME'),
  214. "default" => true,
  215. ),
  216. array(
  217. "id" => "CODE",
  218. "name" => Loc::getMessage('SENDER_CONTACT_SET_LIST_UI_COLUMN_CODE'),
  219. "default" => true,
  220. ),
  221. );
  222. }
  223. protected function getUiFilterPresets()
  224. {
  225. return [];
  226. }
  227. protected function setUiFilterPresets()
  228. {
  229. $this->arResult['FILTER_PRESETS'] = $this->getUiFilterPresets();
  230. }
  231. protected function printErrors()
  232. {
  233. foreach ($this->errors as $error)
  234. {
  235. ShowError($error);
  236. }
  237. }
  238. public function executeComponent()
  239. {
  240. $this->errors = new \Bitrix\Main\ErrorCollection();
  241. if (!Bitrix\Main\Loader::includeModule('sender'))
  242. {
  243. $this->errors->setError(new Error('Module `sender` is not installed.'));
  244. $this->printErrors();
  245. return;
  246. }
  247. $this->initParams();
  248. if (!$this->checkRequiredParams())
  249. {
  250. $this->printErrors();
  251. return;
  252. }
  253. if (!$this->prepareResult())
  254. {
  255. $this->printErrors();
  256. return;
  257. }
  258. $this->includeComponentTemplate();
  259. }
  260. }