BxBaseGrid.php 27 KB

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