BxBaseGrid.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaBaseView UNA Base Representation Classes
  7. * @{
  8. */
  9. /**
  10. * Grid representation.
  11. * @see BxDolGrid
  12. */
  13. class BxBaseGrid extends BxDolGrid
  14. {
  15. protected $_oTemplate;
  16. protected $_aPopupOptions = false;
  17. protected $_aQueryAppend = [];
  18. protected $_aQueryAppendExclude = false; // an array of keys which shouldn't be pathed in http requests, but can be stored (used) in 'Query Append' array.
  19. protected $_aQueryReset = false;
  20. protected $_aConfirmMessages = false;
  21. protected $_bSelectAll = false;
  22. protected $_isDisplayPopupOnTextOverflow = true;
  23. public function __construct ($aOptions, $oTemplate)
  24. {
  25. parent::__construct ($aOptions);
  26. if ($oTemplate)
  27. $this->_oTemplate = $oTemplate;
  28. else
  29. $this->_oTemplate = BxDolTemplate::getInstance();
  30. $this->_aPopupOptions = [];
  31. $this->_aQueryAppend = [
  32. $this->_oTemplate->getCodeKey() => $this->_oTemplate->getCode()
  33. ];
  34. $this->_aQueryAppendExclude = [];
  35. $this->_aQueryReset = [
  36. $this->_aOptions['filter_get'],
  37. $this->_aOptions['order_get_field'],
  38. $this->_aOptions['order_get_dir'],
  39. $this->_aOptions['paginate_get_start'],
  40. $this->_aOptions['paginate_get_per_page']
  41. ];
  42. $this->_aConfirmMessages = [];
  43. }
  44. public function performActionDisplay()
  45. {
  46. require_once(BX_DIRECTORY_PATH_INC . "design.inc.php");
  47. echoJson(array(
  48. 'grid' => $this->getCode(false),
  49. 'total_count' => $this->_iTotalCount,
  50. 'total_count_f' => $this->_getCounter()
  51. ));
  52. }
  53. public function performActionReorder()
  54. {
  55. $this->_replaceMarkers ();
  56. $aOrder = bx_get($this->_sObject . '_row');
  57. $iOrder = 0;
  58. foreach ($aOrder as $mixedId)
  59. $this->_updateOrder($mixedId, ++$iOrder);
  60. echoJson(array());
  61. }
  62. public function performActionDelete()
  63. {
  64. $this->_replaceMarkers ();
  65. $iAffected = 0;
  66. $aIds = bx_get('ids');
  67. if (!$aIds || !is_array($aIds)) {
  68. echoJson(array());
  69. exit;
  70. }
  71. foreach ($aIds as $mixedId)
  72. $iAffected += $this->_delete($mixedId) ? 1 : 0;
  73. echo echoJson(array_merge(
  74. array(
  75. 'grid' => $this->getCode(false),
  76. ),
  77. $iAffected ? array() : array('msg' => _t("_sys_grid_delete_failed"))
  78. ));
  79. }
  80. public function performActionEnable($mixedChecked = null)
  81. {
  82. $this->_replaceMarkers ();
  83. $aIds = bx_get('ids');
  84. if (!$aIds || !is_array($aIds)) {
  85. echoJson(array());
  86. exit;
  87. }
  88. $iChecked = (int)($mixedChecked !== null ? $mixedChecked : bx_get('checked'));
  89. $aAffectedIds = array ();
  90. foreach ($aIds as $mixedId)
  91. if ($this->_enable($mixedId, $iChecked))
  92. $aAffectedIds[] = preg_match("/^[\d\w]+$/", $mixedId) ? $mixedId : (int)$mixedId;
  93. $sAction = $iChecked ? 'enable' : 'disable';
  94. echo echoJson(array($sAction => $aAffectedIds));
  95. }
  96. public function getCode ($isDisplayHeader = true)
  97. {
  98. $this->_replaceMarkers ();
  99. if ($isDisplayHeader && empty($this->_aOptions['paginate_url'])) {
  100. // reset page query params if grid is just initialized and it uses AJAX paginate
  101. $this->resetQueryParams();
  102. }
  103. $sPaginate = '';
  104. $aData = array();
  105. $sIdWrapper = 'bx-grid-wrap-' . $this->_sObject;
  106. $sIdContainer = 'bx-grid-cont-' . $this->_sObject;
  107. $sIdTable = 'bx-grid-table-' . $this->_sObject;
  108. $sFilter = bx_unicode_urldecode(bx_process_input(bx_get($this->_aOptions['filter_get'])));
  109. $sOrderField = bx_unicode_urldecode(bx_process_input(bx_get($this->_aOptions['order_get_field'])));
  110. $sOrderDir = 0 === strcasecmp('desc', bx_get($this->_aOptions['order_get_dir'])) ? 'DESC' : 'ASC';
  111. $iStart = 0;
  112. if($this->_aOptions['paginate_get_start'] && ($iStartGet = (int)bx_get($this->_aOptions['paginate_get_start'])) >= 0)
  113. $iStart = $iStartGet;
  114. $iPerPage = 10;
  115. if($this->_aOptions['paginate_get_per_page'] && ($iPerPageGet = (int)bx_get($this->_aOptions['paginate_get_per_page'])) > 0)
  116. $iPerPage = $iPerPageGet;
  117. else if($this->_aOptions['paginate_per_page'])
  118. $iPerPage = (int)$this->_aOptions['paginate_per_page'];
  119. if ($this->_aOptions['paginate_get_start']) {
  120. $aData = $this->_getData ($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage + 1);
  121. $sPageUrl = false;
  122. if (!empty($this->_aOptions['paginate_url'])) {
  123. $sPageUrl = $this->_aOptions['paginate_url'];
  124. $aParamsAppend = array();
  125. if ($sFilter) {
  126. $aParamsAppend['filter'] = bx_process_input(bx_get($this->_aOptions['filter_get']));
  127. }
  128. if ($sOrderField) {
  129. $aParamsAppend['order_field'] = bx_process_input(bx_get($this->_aOptions['order_get_field']));
  130. $aParamsAppend['order_dir'] = bx_process_input(bx_get($this->_aOptions['order_get_dir']));
  131. }
  132. if ($aParamsAppend)
  133. $sPageUrl = bx_append_url_params($sPageUrl, $aParamsAppend);
  134. }
  135. $aPaginateParams = array(
  136. 'start' => $iStart,
  137. 'per_page' => $iPerPage,
  138. 'page_url' => $sPageUrl ? $sPageUrl : "javascript:glGrids." . $this->_sObject . ".reload('{start}'); void(0);",
  139. );
  140. $oPaginate = new BxTemplPaginate($aPaginateParams, $this->_oTemplate);
  141. $oPaginate->setNumFromDataArray($aData);
  142. if (isset($this->_aOptions['paginate_simple']) && false !== $this->_aOptions['paginate_simple'])
  143. $sPaginate = $oPaginate->getSimplePaginate($this->_aOptions['paginate_simple']);
  144. else
  145. $sPaginate = $oPaginate->getPaginate();
  146. } else {
  147. $aData = $this->_getData ($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);
  148. }
  149. if((empty($aData) || !is_array($aData)) && isset($this->_aBrowseParams['empty_message']) && !(bool)$this->_aBrowseParams['empty_message'])
  150. return '';
  151. $sPopupOptions = '{}';
  152. if (!empty($this->_aPopupOptions) && is_array($this->_aPopupOptions))
  153. $sPopupOptions = json_encode($this->_aPopupOptions);
  154. $aQueryAppend = array_merge(is_array($this->_aQueryAppend) ? $this->_aQueryAppend : array(), is_array($this->_aMarkers) ? $this->_aMarkers : array());
  155. if(!empty($this->_aQueryAppendExclude) && is_array($this->_aQueryAppendExclude))
  156. $aQueryAppend = array_diff_key($aQueryAppend, array_flip($this->_aQueryAppendExclude));
  157. $sQueryAppend = '{}';
  158. if (!empty($aQueryAppend) && is_array($aQueryAppend))
  159. $sQueryAppend = json_encode($aQueryAppend);
  160. $sConfirmMessages = '{}';
  161. if (!empty($this->_aConfirmMessages) && is_array($this->_aConfirmMessages))
  162. $sConfirmMessages = json_encode($this->_aConfirmMessages);
  163. $iColumns = count($this->_aOptions['fields']);
  164. $aVarsHead = $this->_getRowHead();
  165. $sClassHeader = '';
  166. if($this->_aOptions['show_total_count'] == 0)
  167. $sClassHeader .= ' bx-gh-no-counter';
  168. $aVars = array (
  169. 'object' => $this->_sObject,
  170. 'csrf_token' => BxDolForm::getCsrfToken(),
  171. 'id_table' => $sIdTable,
  172. 'id_cont' => $sIdContainer,
  173. 'id_wrap' => $sIdWrapper,
  174. 'class_table_wrapper' => $this->_aOptions['responsive'] ? 'bx-grid-table-wrapper-responsive' : '',
  175. 'sortable' => empty($this->_aOptions['field_order']) ? 0 : 1,
  176. 'sorting' => empty($this->_aOptions['sorting_fields']) ? 0 : 1,
  177. 'sorting_field' => $sOrderField,
  178. 'sorting_dir' => $sOrderDir,
  179. 'bx_if:display_head' => array(
  180. 'condition' => !empty($aVarsHead),
  181. 'content' => array(
  182. 'bx_repeat:row_header' => $aVarsHead,
  183. 'columns' => $iColumns,
  184. )
  185. ),
  186. 'bx_repeat:rows_data' => $this->_getRowsDataDesign ($aData),
  187. 'paginate_get_start' => $this->_aOptions['paginate_get_start'],
  188. 'paginate_get_per_page' => $this->_aOptions['paginate_get_per_page'],
  189. 'start' => $iStart,
  190. 'per_page' => $iPerPage,
  191. 'filter' => bx_js_string($sFilter, BX_ESCAPE_STR_APOS),
  192. 'filter_get' => bx_js_string($this->_aOptions['filter_get']),
  193. 'order_field' => bx_js_string($sOrderField, BX_ESCAPE_STR_APOS),
  194. 'order_dir' => bx_js_string($sOrderDir, BX_ESCAPE_STR_APOS),
  195. 'order_get_field' => bx_js_string($this->_aOptions['order_get_field']),
  196. 'order_get_dir' => bx_js_string($this->_aOptions['order_get_dir']),
  197. 'popup_options' => $sPopupOptions,
  198. 'query_append' => $sQueryAppend,
  199. 'confirm_messages' => $sConfirmMessages,
  200. 'columns' => $iColumns,
  201. 'bx_if:display_footer' => array(
  202. 'condition' => !empty($this->_aOptions['actions_bulk']) || !empty($sPaginate),
  203. 'content' => array(
  204. 'bx_if:actions_bulk' => array (
  205. 'condition' => !empty($this->_aOptions['actions_bulk']),
  206. 'content' => array(
  207. 'actions_bulk' => $this->_getActions ('bulk'),
  208. ),
  209. ),
  210. 'paginate' => $sPaginate,
  211. )
  212. ),
  213. 'bx_if:display_header' => array (
  214. 'condition' => $isDisplayHeader,
  215. 'content' => array (
  216. 'class' => $sClassHeader,
  217. 'bx_if:actions_independent' => array (
  218. 'condition' => !empty($this->_aOptions['actions_independent']),
  219. 'content' => array(
  220. 'actions_independent' => $this->_getActions ('independent'),
  221. ),
  222. ),
  223. 'bx_if:filter' => array (
  224. 'condition' => !empty($this->_aOptions['filter_fields']) || !empty($this->_aOptions['filter_fields_translatable']),
  225. 'content' => array(
  226. 'controls' => $this->_getFilterControls(),
  227. ),
  228. ),
  229. 'bx_if:counter' => array (
  230. 'condition' => $this->_aOptions['show_total_count'] == 1,
  231. 'content' => array(
  232. 'counter' => $this->_getCounter(),
  233. ),
  234. ),
  235. ),
  236. ),
  237. );
  238. $this->_addJsCss();
  239. return $this->_oTemplate->parseHtmlByName('grid.html', $aVars);
  240. }
  241. /**
  242. * Get grid code API.
  243. * @return array
  244. */
  245. public function getCodeAPI()
  246. {
  247. $sFilter = bx_unicode_urldecode(bx_process_input(bx_get($this->_aOptions['filter_get'])));
  248. $sOrderField = bx_unicode_urldecode(bx_process_input(bx_get($this->_aOptions['order_get_field'])));
  249. $sOrderDir = 0 === strcasecmp('desc', bx_get($this->_aOptions['order_get_dir'])) ? 'DESC' : 'ASC';
  250. $iStart = 0;
  251. if($this->_aOptions['paginate_get_start'] && ($iStartGet = (int)bx_get($this->_aOptions['paginate_get_start'])) >= 0)
  252. $iStart = $iStartGet;
  253. $iPerPage = 10;
  254. if($this->_aOptions['paginate_get_per_page'] && ($iPerPageGet = (int)bx_get($this->_aOptions['paginate_get_per_page'])) > 0)
  255. $iPerPage = $iPerPageGet;
  256. else if($this->_aOptions['paginate_per_page'])
  257. $iPerPage = (int)$this->_aOptions['paginate_per_page'];
  258. return [
  259. 'header' => $this->_getRowHeadAPI(),
  260. 'data' => $this->_getData($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage),
  261. 'actions' => [
  262. 'independent' => $this->_getActionsAPI('independent'),
  263. 'bulk' => $this->_getActionsAPI('bulk'),
  264. ]
  265. ];
  266. }
  267. /**
  268. * Reset query params, like filter and page number
  269. */
  270. public function resetQueryParams()
  271. {
  272. foreach ($this->_aQueryReset as $sKey) {
  273. unset($_GET[$sKey]);
  274. unset($_POST[$sKey]);
  275. }
  276. }
  277. protected function _getRowHead()
  278. {
  279. $aRet = array();
  280. foreach ($this->_aOptions['fields'] as $sKey => $a) {
  281. $sMethod = '_getCellHeaderDefault';
  282. $sCustomMethod = '_getCellHeader' . $this->_genMethodName($sKey);
  283. if (method_exists($this, $sCustomMethod))
  284. $sMethod = $sCustomMethod;
  285. $aRet[] = array('header_cell' => $this->$sMethod($sKey, $a));
  286. }
  287. return $aRet;
  288. }
  289. protected function _getRowHeadAPI()
  290. {
  291. $aHeader = [];
  292. foreach($this->_aOptions['fields'] as $sKey => $aField)
  293. $aHeader[] = [
  294. 'title' => bx_process_output($aField['title'])
  295. ];
  296. return $aHeader;
  297. }
  298. protected function _getCellHeaderDefault ($sKey, $aField)
  299. {
  300. $sHeader = bx_process_output($aField['title']);
  301. if (!empty($this->_aOptions['sorting_fields']) && is_array($this->_aOptions['sorting_fields']) && in_array($sKey, $this->_aOptions['sorting_fields'])) {
  302. $sHeader = '<a href="javascript:void(0);" class="bx-grid-sort-handle">' . $sHeader . '</a><span class="bx-grid-sort-indi"></span>';
  303. $aField['attr_head']['bx_grid_sort_head'] = $sKey;
  304. }
  305. $sAttr = $this->_convertAttrs(
  306. $aField, 'attr_head',
  307. 'bx-def-padding-sec-bottom bx-def-padding-sec-top bx-grid-header-' . $sKey, // add default classes
  308. isset($aField['width']) ? 'width:' . $aField['width'] : false // add default styles
  309. );
  310. return $this->_getCellHeaderWrapper ($sKey, $aField, $sHeader, $sAttr);
  311. }
  312. protected function _getCellHeaderCheckbox ($sKey, $aField)
  313. {
  314. $aAttr = array(
  315. 'type' => 'checkbox',
  316. 'id' => $this->_sObject . '_check_all',
  317. 'name' => $this->_sObject . '_check_all',
  318. 'onclick' => "$('input[name=" . $this->_sObject . "_check]:not([disabled])').attr('checked', this.checked)"
  319. );
  320. if($this->_bSelectAll)
  321. $aAttr['checked'] = 'checked';
  322. $aField['attr'] = isset($aField['attr']) && is_array($aField['attr']) ? array_merge($aAttr, $aField['attr']) : $aAttr;
  323. $sAttrHead = $this->_convertAttrs(
  324. $aField, 'attr_head',
  325. 'bx-def-padding-sec-bottom bx-def-padding-sec-top bx-grid-header-' . $sKey, // add default classes
  326. isset($aField['width']) ? 'width:' . $aField['width'] : false // add default styles
  327. );
  328. return $this->_getCellHeaderWrapper ($sKey, $aField, ' <input ' . $this->_convertAttrs($aField, 'attr') . ' /> ', $sAttrHead);
  329. }
  330. protected function _getCellHeaderWrapper ($sKey, $aField, $sHeader, $sAttr)
  331. {
  332. return '<th ' . $sAttr . '>' . $sHeader . '</th>';
  333. }
  334. /**
  335. * Check if the whole row is disabled.
  336. * When row is disabled - checkbox is not selectable, actions aren't clickable and text is grayed out.
  337. * By default all rows aren't disabled.
  338. * @param $aRow row array
  339. * @return boolean
  340. */
  341. protected function _isRowDisabled($aRow)
  342. {
  343. if (isset($aRow[$this->_aOptions['field_active']]) && !$this->_switcherState2Checked($aRow[$this->_aOptions['field_active']]))
  344. return true;
  345. return false;
  346. }
  347. /**
  348. * Determine how actions are disabled when whole row is disabled.
  349. * @param $aRow row array
  350. * @return null - disable/enable actions when row is disabled/enabled, true - actions are always disabled, false - actions are always enabled
  351. */
  352. protected function _getActionsDisabledBehavior($aRow)
  353. {
  354. return null;
  355. }
  356. /**
  357. * Check if the checkbox is disabled.
  358. * @param $aRow row array
  359. * @return boolean
  360. */
  361. protected function _isCheckboxDisabled($aRow)
  362. {
  363. return $this->_isRowDisabled($aRow);
  364. }
  365. /**
  366. * Is checkbox checked by default ?
  367. * By default no one checkbox is selected.
  368. * @return boolean
  369. */
  370. protected function _isCheckboxSelected($mixedValue, $sKey, $aField, $aRow)
  371. {
  372. return $this->_bSelectAll;
  373. }
  374. /**
  375. * Is switcher on by default ?
  376. * By default no one switcher is on.
  377. * @return boolean
  378. */
  379. protected function _isSwitcherOn($mixedValue, $sKey, $aField, $aRow)
  380. {
  381. return $this->_switcherState2Checked($mixedValue);
  382. }
  383. /**
  384. * Convert switcher checked status to the actual value ?
  385. * @return boolean
  386. */
  387. protected function _switcherChecked2State($isChecked)
  388. {
  389. return $isChecked ? 1 : 0;
  390. }
  391. /**
  392. * Convert switcher value to checked(boolean) value ?
  393. * @return boolean
  394. */
  395. protected function _switcherState2Checked($mixedState)
  396. {
  397. return $mixedState ? true : false;
  398. }
  399. protected function _getRowsDataDesign (&$aData)
  400. {
  401. $aGrid = array();
  402. if(empty($aData))
  403. $aGrid[] = array(
  404. 'id_row' => 0,
  405. 'row_class' => 'bx-grid-table-row-empty',
  406. 'row' => '<td class="bx-def-padding-sec-topbottom" colspan="' . count($this->_aOptions['fields']) . '">' . MsgBox(_t('_Empty')) . '</td>'
  407. );
  408. else
  409. foreach ($aData as $aRow)
  410. $aGrid[] = array(
  411. 'id_row' => $this->_getRowId($aRow),
  412. 'row_class' => $this->_isRowDisabled($aRow) ? 'bx-grid-table-row-disabled bx-def-font-grayed' : '',
  413. 'row' => $this->_getRowDesign($aRow)
  414. );
  415. return $aGrid;
  416. }
  417. protected function _getRowId($mixedRow)
  418. {
  419. if(is_string($mixedRow))
  420. $sId = rand(1, getrandmax());
  421. else
  422. $sId = $mixedRow[$this->_aOptions['field_id']];
  423. return $this->_sObject . '_row_' . $sId;
  424. }
  425. protected function _getRowDesign($mixedRow)
  426. {
  427. $sRow = '';
  428. if(is_string($mixedRow))
  429. $sRow = '<td class="bx-def-padding-sec-topbottom bx-def-font-bold" colspan="' . count($this->_aOptions['fields']) . '">' . $mixedRow . '</td>';
  430. else
  431. foreach($this->_aOptions['fields'] as $sKey => $aField)
  432. $sRow .= $this->_getCellDesign($sKey, $aField, $mixedRow);
  433. return $sRow;
  434. }
  435. protected function _getCellDesign($sKey, $aField, $aRow)
  436. {
  437. $sMethod = '_getCellDefault';
  438. if ($this->_aOptions['field_order'] == $sKey)
  439. $sMethod = '_getCellOrder';
  440. $sCustomMethod = '_getCell' . $this->_genMethodName($sKey);
  441. if (method_exists($this, $sCustomMethod))
  442. $sMethod = $sCustomMethod;
  443. $mixedValue = $this->_getCellData($sKey, $aField, $aRow);
  444. if ($aField['translatable'])
  445. $mixedValue = _t($mixedValue);
  446. $mixedValue = $this->_limitMaxLength($mixedValue, $sKey, $aField, $aRow, $this->_isDisplayPopupOnTextOverflow);
  447. return $this->$sMethod($mixedValue, $sKey, $aField, $aRow);
  448. }
  449. protected function _getCellCheckbox ($mixedValue, $sKey, $aField, $aRow)
  450. {
  451. $sAttr = $this->_convertAttrs(
  452. $aField, 'attr_cell',
  453. 'bx-def-padding-sec-bottom bx-def-padding-sec-top', // add default classes
  454. isset($aField['width']) ? 'width:' . $aField['width'] : false // add default styles
  455. );
  456. $sDisabled = ($this->_isCheckboxDisabled($aRow) ? 'disabled="disabled"' : '');
  457. $sSelected = ($this->_isCheckboxSelected($mixedValue, $sKey, $aField, $aRow) ? 'checked="checked"' : '');
  458. $sVal = $aRow[$this->_aOptions['field_id']];
  459. return '<td ' . $sAttr . '><input type="checkbox" name="'. $this->_sObject . '_check" value="' . $sVal . '" ' . $sDisabled . ' ' . $sSelected . '/></td>';
  460. }
  461. protected function _getCellSwitcher ($mixedValue, $sKey, $aField, $aRow)
  462. {
  463. $sAttr = $this->_convertAttrs(
  464. $aField, 'attr_cell',
  465. 'bx-def-padding-sec-bottom bx-def-padding-sec-top', // add default classes
  466. isset($aField['width']) ? 'width:' . $aField['width'] : false // add default styles
  467. );
  468. $oForm = new BxTemplFormView(array(), $this->_oTemplate);
  469. $oForm->addCssJs();
  470. $aInput = array(
  471. 'type' => 'switcher',
  472. 'name' => $this->_sObject . '_switch_' . $aRow[$this->_aOptions['field_id']],
  473. 'caption' => '',
  474. 'attrs' => array (
  475. 'bx_grid_action_single' => 'enable',
  476. 'bx_grid_action_confirm' => '',
  477. 'bx_grid_action_data' => $aRow[$this->_aOptions['field_id']],
  478. ),
  479. 'value' => $aRow[$this->_aOptions['field_id']],
  480. 'checked' => $this->_isSwitcherOn(isset($aRow[$this->_aOptions['field_active']]) ? $aRow[$this->_aOptions['field_active']] : false, $sKey, $aField, $aRow),
  481. );
  482. $sSwitcher = $oForm->genInput($aInput);
  483. return '<td ' . $sAttr . '>' . $sSwitcher . '</td>';
  484. }
  485. protected function _getCellOrder ($mixedValue, $sKey, $aField, $aRow)
  486. {
  487. $sAttr = $this->_convertAttrs(
  488. $aField, 'attr_cell',
  489. 'bx-def-padding-sec-bottom bx-def-padding-sec-top', // add default classes
  490. isset($aField['width']) ? 'width:' . $aField['width'] : false // add default styles
  491. );
  492. return '<td ' . $sAttr . '><div id="' . $this->_sObject . '_cell_' . $aRow[$this->_aOptions['field_id']] . '" class="bx-grid-drag-handle"><i class="sys-icon align-justify"></i></div></td>';
  493. }
  494. protected function _getCellActions ($mixedValue, $sKey, $aField, $aRow)
  495. {
  496. $sAttr = $this->_convertAttrs(
  497. $aField, 'attr_cell',
  498. 'bx-def-padding-sec-bottom bx-def-padding-sec-top', // add default classes
  499. isset($aField['width']) ? 'width:' . $aField['width'] : false // add default styles
  500. );
  501. $mixedDisabledBehavior = $this->_getActionsDisabledBehavior($aRow);
  502. $sActions = $this->_getActions('single', $aRow[$this->_aOptions['field_id']], false, null === $mixedDisabledBehavior ? $this->_isRowDisabled($aRow) : $mixedDisabledBehavior, null !== $mixedDisabledBehavior, $aRow);
  503. return '<td ' . $sAttr . '><div class="bx-grid-cell-single-actions-wrapper bx-def-margin-thd-neg">' . $sActions . '</div></td>';
  504. }
  505. protected function _getCellDefault ($mixedValue, $sKey, $aField, $aRow)
  506. {
  507. $sAttr = $this->_convertAttrs(
  508. $aField, 'attr_cell',
  509. 'bx-def-padding-sec-bottom bx-def-padding-sec-top', // add default classes
  510. isset($aField['width']) ? 'width:' . $aField['width'] : false // add default styles
  511. );
  512. return '<td ' . $sAttr . '>' . $mixedValue . '</td>';
  513. }
  514. protected function _getActions ($sType, $sActionData = false, $isSmall = false, $isDisabled = false, $isPermanentState = false, $aRow = array())
  515. {
  516. $sActionsType = 'actions_' . $sType;
  517. if (empty($this->_aOptions[$sActionsType]) || !is_array($this->_aOptions[$sActionsType]))
  518. return '';
  519. $sRet = '';
  520. foreach ($this->_aOptions[$sActionsType] as $sKey => $a) {
  521. if(!$a['active'] || (!$a['title'] && !$a['icon']))
  522. continue;
  523. $sFunc = '_getAction' . $this->_genMethodName($sKey);
  524. if (!method_exists($this, $sFunc))
  525. $sFunc = empty($a) ? '_getActionDivider' : '_getActionDefault';
  526. if (!isset($a['attr']['bx_grid_action'])) {
  527. $a['attr']['bx_grid_action_' . $sType] = $sKey;
  528. if (!empty($sActionData))
  529. $a['attr']['bx_grid_action_data'] = $sActionData;
  530. if (isset($a['confirm']))
  531. $a['attr']['bx_grid_action_confirm'] = $a['confirm'] ? 1 : 0;
  532. $a['attr']['bx_grid_action_reset_paginate'] = false === strpos($sKey, 'delete') ? 0 : 1; // reset paginate after deleting row
  533. }
  534. if ($isPermanentState && !isset($a['attr']['bx_grid_permanent_state']))
  535. $a['attr']['bx_grid_permanent_state'] = 1;
  536. $sRet .= $this->$sFunc($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  537. }
  538. return $sRet;
  539. }
  540. protected function _getActionsAPI ($sType)
  541. {
  542. $sActionsType = 'actions_' . $sType;
  543. if(empty($this->_aOptions[$sActionsType]) || !is_array($this->_aOptions[$sActionsType]))
  544. return [];
  545. return $this->_aOptions[$sActionsType];
  546. }
  547. protected function _getActionDefault ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  548. {
  549. if ($a['icon_only'] && empty($a['attr']['title']) && !empty($a['title']))
  550. $a['attr']['title'] = $a['title'];
  551. $sAttr = $this->_convertAttrs(
  552. $a, 'attr',
  553. 'bx-btn bx-def-margin-thd' . ($isSmall ? ' bx-btn-small' : '') . ($isDisabled ? ' bx-btn-disabled' : '') // add default classes
  554. );
  555. $sIcon = '';
  556. $sImage = '';
  557. if (!empty($a['icon'])) {
  558. if (false === strpos($a['icon'], '.'))
  559. $sIcon = '<i class="sys-icon ' . $a['icon'] . '"></i>';
  560. elseif ($sIconUrl = $this->_oTemplate->getIconUrl($a['icon']))
  561. $sImage = '<img style="background-image:url(' . $sIconUrl . ');" src="' . $this->_oTemplate->getIconUrl('spacer.gif') .'" />';
  562. }
  563. return '<button ' . $sAttr . '>' . $sIcon . $sImage . ($a['icon_only'] || empty($a['title']) ? '' : '<u>' . $a['title'] . '</u>') . '</button>';
  564. }
  565. protected function _getActionDivider ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  566. {
  567. return '<div class="bx-grid-actions-divider bx-def-margin-sec-left"> | </div>';
  568. }
  569. protected function _getFilterControls ()
  570. {
  571. $oForm = new BxTemplFormView(array());
  572. $aInput = array(
  573. 'type' => 'text',
  574. 'name' => 'keyword',
  575. 'attrs' => array(
  576. 'id' => 'bx-grid-search-' . $this->_sObject
  577. )
  578. );
  579. $this->_oTemplate->addCss('forms.css');
  580. return $oForm->genRow($aInput);
  581. }
  582. protected function _getCounter()
  583. {
  584. return _t('_sys_grid_total_count', $this->_iTotalCount);
  585. }
  586. protected function _limitMaxLength ($mixedValue, $sKey, $aField, $aRow, $isDisplayPopupOnTextOverflow, $bReturnString = true)
  587. {
  588. if ($aField['chars_limit'] > 0)
  589. $mixedValue = BxTemplFunctions::getInstance()->getStringWithLimitedLength($mixedValue, $aField['chars_limit'], $isDisplayPopupOnTextOverflow, $bReturnString);
  590. return $mixedValue;
  591. }
  592. protected function _convertAttrs ($aField, $sAttrName, $sClasses = false, $sStyles = false)
  593. {
  594. if (!empty($aField['hidden_on'])) {
  595. $aHiddenOn = array(
  596. pow(2, BX_DB_HIDDEN_PHONE - 1) => 'bx-def-media-phone-hide',
  597. pow(2, BX_DB_HIDDEN_TABLET - 1) => 'bx-def-media-tablet-hide',
  598. pow(2, BX_DB_HIDDEN_DESKTOP - 1) => 'bx-def-media-desktop-hide',
  599. pow(2, BX_DB_HIDDEN_MOBILE - 1) => 'bx-def-mobile-app-hide'
  600. );
  601. foreach ($aHiddenOn as $iHiddenOn => $sClass)
  602. if ((int)$aField['hidden_on'] & $iHiddenOn)
  603. $sClasses .= ' ' . $sClass;
  604. }
  605. return bx_convert_array2attrs(
  606. isset($aField[$sAttrName]) && is_array($aField[$sAttrName]) ? $aField[$sAttrName] : array(),
  607. $sClasses,
  608. $sStyles
  609. );
  610. }
  611. protected function _updateOrder($mixedId, $iOrder)
  612. {
  613. $oDb = BxDolDb::getInstance();
  614. $sTable = $this->_aOptions['table'];
  615. $sFieldId = $this->_aOptions['field_id'];
  616. $sFieldOrder = $this->_aOptions['field_order'];
  617. $sQuery = $oDb->prepare("UPDATE `{$sTable}` SET `{$sFieldOrder}` = ? WHERE `{$sFieldId}` = ?", $iOrder, $mixedId);
  618. return $oDb->query($sQuery);
  619. }
  620. protected function _delete ($mixedId)
  621. {
  622. $oDb = BxDolDb::getInstance();
  623. $sTable = $this->_aOptions['table'];
  624. $sFieldId = $this->_aOptions['field_id'];
  625. $sQuery = $oDb->prepare("DELETE FROM `{$sTable}` WHERE `{$sFieldId}` = ?", $mixedId);
  626. return $oDb->query($sQuery);
  627. }
  628. protected function _enable ($mixedId, $isChecked)
  629. {
  630. $oDb = BxDolDb::getInstance();
  631. $sTable = $this->_aOptions['table'];
  632. $sFieldId = $this->_aOptions['field_id'];
  633. $sFieldActive = $this->_aOptions['field_active'];
  634. $sQuery = $oDb->prepare("UPDATE `{$sTable}` SET `$sFieldActive` = ? WHERE `{$sFieldId}` = ?", $this->_switcherChecked2State($isChecked), $mixedId);
  635. return $oDb->query($sQuery);
  636. }
  637. protected function _addJsCss()
  638. {
  639. if ($this->_aOptions['field_order']) {
  640. $this->_oTemplate->addJs(array(
  641. 'jquery-ui/jquery-ui.custom.min.js',
  642. 'URI.min.js',
  643. ));
  644. }
  645. $this->_oTemplate->addJs('BxDolGrid.js');
  646. $this->_oTemplate->addCss('grid.css');
  647. $this->_oTemplate->addJsTranslation('_sys_grid_confirmation');
  648. }
  649. }
  650. /** @} */