BxBaseGrid.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  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. $sClassHeader = '';
  158. if($this->_aOptions['show_total_count'] == 0)
  159. $sClassHeader .= ' bx-gh-no-counter';
  160. $aVars = array (
  161. 'object' => $this->_sObject,
  162. 'id_table' => $sIdTable,
  163. 'id_cont' => $sIdContainer,
  164. 'id_wrap' => $sIdWrapper,
  165. 'class_table_wrapper' => $this->_aOptions['responsive'] ? 'bx-grid-table-wrapper-responsive' : '',
  166. 'sortable' => empty($this->_aOptions['field_order']) ? 0 : 1,
  167. 'sorting' => empty($this->_aOptions['sorting_fields']) ? 0 : 1,
  168. 'sorting_field' => $sOrderField,
  169. 'sorting_dir' => $sOrderDir,
  170. 'bx_if:display_head' => array(
  171. 'condition' => !empty($aVarsHead),
  172. 'content' => array(
  173. 'bx_repeat:row_header' => $aVarsHead,
  174. 'columns' => $iColumns,
  175. )
  176. ),
  177. 'bx_repeat:rows_data' => $this->_getRowsDataDesign ($aData),
  178. 'paginate_get_start' => $this->_aOptions['paginate_get_start'],
  179. 'paginate_get_per_page' => $this->_aOptions['paginate_get_per_page'],
  180. 'start' => $iStart,
  181. 'per_page' => $iPerPage,
  182. 'filter' => bx_js_string($sFilter, BX_ESCAPE_STR_APOS),
  183. 'filter_get' => bx_js_string($this->_aOptions['filter_get']),
  184. 'order_field' => bx_js_string($sOrderField, BX_ESCAPE_STR_APOS),
  185. 'order_dir' => bx_js_string($sOrderDir, BX_ESCAPE_STR_APOS),
  186. 'order_get_field' => bx_js_string($this->_aOptions['order_get_field']),
  187. 'order_get_dir' => bx_js_string($this->_aOptions['order_get_dir']),
  188. 'popup_options' => $sPopupOptions,
  189. 'query_append' => $sQueryAppend,
  190. 'confirm_messages' => $sConfirmMessages,
  191. 'columns' => $iColumns,
  192. 'bx_if:display_footer' => array(
  193. 'condition' => !empty($this->_aOptions['actions_bulk']) || !empty($sPaginate),
  194. 'content' => array(
  195. 'bx_if:actions_bulk' => array (
  196. 'condition' => !empty($this->_aOptions['actions_bulk']),
  197. 'content' => array(
  198. 'actions_bulk' => $this->_getActions ('bulk'),
  199. ),
  200. ),
  201. 'paginate' => $sPaginate,
  202. )
  203. ),
  204. 'bx_if:display_header' => array (
  205. 'condition' => $isDisplayHeader,
  206. 'content' => array (
  207. 'class' => $sClassHeader,
  208. 'bx_if:actions_independent' => array (
  209. 'condition' => !empty($this->_aOptions['actions_independent']),
  210. 'content' => array(
  211. 'actions_independent' => $this->_getActions ('independent'),
  212. ),
  213. ),
  214. 'bx_if:filter' => array (
  215. 'condition' => !empty($this->_aOptions['filter_fields']) || !empty($this->_aOptions['filter_fields_translatable']),
  216. 'content' => array(
  217. 'controls' => $this->_getFilterControls(),
  218. ),
  219. ),
  220. 'bx_if:counter' => array (
  221. 'condition' => $this->_aOptions['show_total_count'] == 1,
  222. 'content' => array(
  223. 'counter' => $this->_getCounter(),
  224. ),
  225. ),
  226. ),
  227. ),
  228. );
  229. $this->_addJsCss();
  230. return $this->_oTemplate->parseHtmlByName('grid.html', $aVars);
  231. }
  232. /**
  233. * Reset query params, like filter and page number
  234. */
  235. public function resetQueryParams()
  236. {
  237. foreach ($this->_aQueryReset as $sKey) {
  238. unset($_GET[$sKey]);
  239. unset($_POST[$sKey]);
  240. }
  241. }
  242. protected function _getRowHead ()
  243. {
  244. $aRet = array();
  245. foreach ($this->_aOptions['fields'] as $sKey => $a) {
  246. $sMethod = '_getCellHeaderDefault';
  247. $sCustomMethod = '_getCellHeader' . $this->_genMethodName($sKey);
  248. if (method_exists($this, $sCustomMethod))
  249. $sMethod = $sCustomMethod;
  250. $aRet[] = array('header_cell' => $this->$sMethod($sKey, $a));
  251. }
  252. return $aRet;
  253. }
  254. protected function _getCellHeaderDefault ($sKey, $aField)
  255. {
  256. $sHeader = bx_process_output($aField['title']);
  257. if (!empty($this->_aOptions['sorting_fields']) && is_array($this->_aOptions['sorting_fields']) && in_array($sKey, $this->_aOptions['sorting_fields'])) {
  258. $sHeader = '<a href="javascript:void(0);" class="bx-grid-sort-handle">' . $sHeader . '</a><span class="bx-grid-sort-indi"></span>';
  259. $aField['attr_head']['bx_grid_sort_head'] = $sKey;
  260. }
  261. $sAttr = $this->_convertAttrs(
  262. $aField, 'attr_head',
  263. 'bx-def-padding-sec-bottom bx-def-padding-sec-top bx-grid-header-' . $sKey, // add default classes
  264. isset($aField['width']) ? 'width:' . $aField['width'] : false // add default styles
  265. );
  266. return $this->_getCellHeaderWrapper ($sKey, $aField, $sHeader, $sAttr);
  267. }
  268. protected function _getCellHeaderCheckbox ($sKey, $aField)
  269. {
  270. $aAttr = array(
  271. 'type' => 'checkbox',
  272. 'id' => $this->_sObject . '_check_all',
  273. 'name' => $this->_sObject . '_check_all',
  274. 'onclick' => "$('input[name=" . $this->_sObject . "_check]:not([disabled])').attr('checked', this.checked)"
  275. );
  276. if($this->_bSelectAll)
  277. $aAttr['checked'] = 'checked';
  278. $aField['attr'] = isset($aField['attr']) && is_array($aField['attr']) ? array_merge($aAttr, $aField['attr']) : $aAttr;
  279. $sAttrHead = $this->_convertAttrs(
  280. $aField, 'attr_head',
  281. 'bx-def-padding-sec-bottom bx-def-padding-sec-top bx-grid-header-' . $sKey, // add default classes
  282. isset($aField['width']) ? 'width:' . $aField['width'] : false // add default styles
  283. );
  284. return $this->_getCellHeaderWrapper ($sKey, $aField, ' <input ' . $this->_convertAttrs($aField, 'attr') . ' /> ', $sAttrHead);
  285. }
  286. protected function _getCellHeaderWrapper ($sKey, $aField, $sHeader, $sAttr)
  287. {
  288. return '<th ' . $sAttr . '>' . $sHeader . '</th>';
  289. }
  290. /**
  291. * Check if the whole row is disabled.
  292. * When row is disabled - checkbox is not selectable, actions aren't clickable and text is grayed out.
  293. * By default all rows aren't disabled.
  294. * @param $aRow row array
  295. * @return boolean
  296. */
  297. protected function _isRowDisabled($aRow)
  298. {
  299. if (isset($aRow[$this->_aOptions['field_active']]) && !$this->_switcherState2Checked($aRow[$this->_aOptions['field_active']]))
  300. return true;
  301. return false;
  302. }
  303. /**
  304. * Determine how actions are disabled when whole row is disabled.
  305. * @param $aRow row array
  306. * @return null - disable/enable actions when row is disabled/enabled, true - actions are always disabled, false - actions are always enabled
  307. */
  308. protected function _getActionsDisabledBehavior($aRow)
  309. {
  310. return null;
  311. }
  312. /**
  313. * Check if the checkbox is disabled.
  314. * @param $aRow row array
  315. * @return boolean
  316. */
  317. protected function _isCheckboxDisabled($aRow)
  318. {
  319. return $this->_isRowDisabled($aRow);
  320. }
  321. /**
  322. * Is checkbox checked by default ?
  323. * By default no one checkbox is selected.
  324. * @return boolean
  325. */
  326. protected function _isCheckboxSelected($mixedValue, $sKey, $aField, $aRow)
  327. {
  328. return $this->_bSelectAll;
  329. }
  330. /**
  331. * Is switcher on by default ?
  332. * By default no one switcher is on.
  333. * @return boolean
  334. */
  335. protected function _isSwitcherOn($mixedValue, $sKey, $aField, $aRow)
  336. {
  337. return $this->_switcherState2Checked($mixedValue);
  338. }
  339. /**
  340. * Convert switcher checked status to the actual value ?
  341. * @return boolean
  342. */
  343. protected function _switcherChecked2State($isChecked)
  344. {
  345. return $isChecked ? 1 : 0;
  346. }
  347. /**
  348. * Convert switcher value to checked(boolean) value ?
  349. * @return boolean
  350. */
  351. protected function _switcherState2Checked($mixedState)
  352. {
  353. return $mixedState ? true : false;
  354. }
  355. protected function _getRowsDataDesign (&$aData)
  356. {
  357. $aGrid = array();
  358. if(empty($aData))
  359. $aGrid[] = array(
  360. 'id_row' => 0,
  361. 'row_class' => 'bx-grid-table-row-empty',
  362. 'row' => '<td class="bx-def-padding-sec-topbottom" colspan="' . count($this->_aOptions['fields']) . '">' . MsgBox(_t('_Empty')) . '</td>'
  363. );
  364. else
  365. foreach ($aData as $aRow)
  366. $aGrid[] = array(
  367. 'id_row' => $this->_getRowId($aRow),
  368. 'row_class' => $this->_isRowDisabled($aRow) ? 'bx-grid-table-row-disabled bx-def-font-grayed' : '',
  369. 'row' => $this->_getRowDesign($aRow)
  370. );
  371. return $aGrid;
  372. }
  373. protected function _getRowId($mixedRow)
  374. {
  375. if(is_string($mixedRow))
  376. $sId = rand(1, getrandmax());
  377. else
  378. $sId = $mixedRow[$this->_aOptions['field_id']];
  379. return $this->_sObject . '_row_' . $sId;
  380. }
  381. protected function _getRowDesign($mixedRow)
  382. {
  383. $sRow = '';
  384. if(is_string($mixedRow))
  385. $sRow = '<td class="bx-def-padding-sec-topbottom bx-def-font-bold" colspan="' . count($this->_aOptions['fields']) . '">' . $mixedRow . '</td>';
  386. else
  387. foreach($this->_aOptions['fields'] as $sKey => $aField)
  388. $sRow .= $this->_getCellDesign($sKey, $aField, $mixedRow);
  389. return $sRow;
  390. }
  391. protected function _getCellDesign($sKey, $aField, $aRow)
  392. {
  393. $sMethod = '_getCellDefault';
  394. if ($this->_aOptions['field_order'] == $sKey)
  395. $sMethod = '_getCellOrder';
  396. $sCustomMethod = '_getCell' . $this->_genMethodName($sKey);
  397. if (method_exists($this, $sCustomMethod))
  398. $sMethod = $sCustomMethod;
  399. $mixedValue = $this->_getCellData($sKey, $aField, $aRow);
  400. if ($aField['translatable'])
  401. $mixedValue = _t($mixedValue);
  402. $mixedValue = $this->_limitMaxLength($mixedValue, $sKey, $aField, $aRow, $this->_isDisplayPopupOnTextOverflow);
  403. return $this->$sMethod($mixedValue, $sKey, $aField, $aRow);
  404. }
  405. protected function _getCellCheckbox ($mixedValue, $sKey, $aField, $aRow)
  406. {
  407. $sAttr = $this->_convertAttrs(
  408. $aField, 'attr_cell',
  409. 'bx-def-padding-sec-bottom bx-def-padding-sec-top', // add default classes
  410. isset($aField['width']) ? 'width:' . $aField['width'] : false // add default styles
  411. );
  412. $sDisabled = ($this->_isCheckboxDisabled($aRow) ? 'disabled="disabled"' : '');
  413. $sSelected = ($this->_isCheckboxSelected($mixedValue, $sKey, $aField, $aRow) ? 'checked="checked"' : '');
  414. $sVal = $aRow[$this->_aOptions['field_id']];
  415. return '<td ' . $sAttr . '><input type="checkbox" name="'. $this->_sObject . '_check" value="' . $sVal . '" ' . $sDisabled . ' ' . $sSelected . '/></td>';
  416. }
  417. protected function _getCellSwitcher ($mixedValue, $sKey, $aField, $aRow)
  418. {
  419. $sAttr = $this->_convertAttrs(
  420. $aField, 'attr_cell',
  421. 'bx-def-padding-sec-bottom bx-def-padding-sec-top', // add default classes
  422. isset($aField['width']) ? 'width:' . $aField['width'] : false // add default styles
  423. );
  424. $oForm = new BxTemplFormView(array(), $this->_oTemplate);
  425. $oForm->addCssJs();
  426. $aInput = array(
  427. 'type' => 'switcher',
  428. 'name' => $this->_sObject . '_switch_' . $aRow[$this->_aOptions['field_id']],
  429. 'caption' => '',
  430. 'attrs' => array (
  431. 'bx_grid_action_single' => 'enable',
  432. 'bx_grid_action_confirm' => '',
  433. 'bx_grid_action_data' => $aRow[$this->_aOptions['field_id']],
  434. ),
  435. 'value' => $aRow[$this->_aOptions['field_id']],
  436. 'checked' => $this->_isSwitcherOn(isset($aRow[$this->_aOptions['field_active']]) ? $aRow[$this->_aOptions['field_active']] : false, $sKey, $aField, $aRow),
  437. );
  438. $sSwitcher = $oForm->genInput($aInput);
  439. return '<td ' . $sAttr . '>' . $sSwitcher . '</td>';
  440. }
  441. protected function _getCellOrder ($mixedValue, $sKey, $aField, $aRow)
  442. {
  443. $sAttr = $this->_convertAttrs(
  444. $aField, 'attr_cell',
  445. 'bx-def-padding-sec-bottom bx-def-padding-sec-top', // add default classes
  446. isset($aField['width']) ? 'width:' . $aField['width'] : false // add default styles
  447. );
  448. 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>';
  449. }
  450. protected function _getCellActions ($mixedValue, $sKey, $aField, $aRow)
  451. {
  452. $sAttr = $this->_convertAttrs(
  453. $aField, 'attr_cell',
  454. 'bx-def-padding-sec-bottom bx-def-padding-sec-top', // add default classes
  455. isset($aField['width']) ? 'width:' . $aField['width'] : false // add default styles
  456. );
  457. $mixedDisabledBehavior = $this->_getActionsDisabledBehavior($aRow);
  458. $sActions = $this->_getActions('single', $aRow[$this->_aOptions['field_id']], false, null === $mixedDisabledBehavior ? $this->_isRowDisabled($aRow) : $mixedDisabledBehavior, null !== $mixedDisabledBehavior, $aRow);
  459. return '<td ' . $sAttr . '><div class="bx-grid-cell-single-actions-wrapper bx-def-margin-thd-neg">' . $sActions . '</div></td>';
  460. }
  461. protected function _getCellDefault ($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. return '<td ' . $sAttr . '>' . $mixedValue . '</td>';
  469. }
  470. protected function _getActions ($sType, $sActionData = false, $isSmall = false, $isDisabled = false, $isPermanentState = false, $aRow = array())
  471. {
  472. $sActionsType = 'actions_' . $sType;
  473. if (empty($this->_aOptions[$sActionsType]) || !is_array($this->_aOptions[$sActionsType]))
  474. return '';
  475. $sRet = '';
  476. foreach ($this->_aOptions[$sActionsType] as $sKey => $a) {
  477. $sFunc = '_getAction' . $this->_genMethodName($sKey);
  478. if (!method_exists($this, $sFunc))
  479. $sFunc = empty($a) ? '_getActionDivider' : '_getActionDefault';
  480. if (!isset($a['attr']['bx_grid_action'])) {
  481. $a['attr']['bx_grid_action_' . $sType] = $sKey;
  482. if (!empty($sActionData))
  483. $a['attr']['bx_grid_action_data'] = $sActionData;
  484. if (isset($a['confirm']))
  485. $a['attr']['bx_grid_action_confirm'] = $a['confirm'] ? 1 : 0;
  486. $a['attr']['bx_grid_action_reset_paginate'] = false === strpos($sKey, 'delete') ? 0 : 1; // reset paginate after deleting row
  487. }
  488. if ($isPermanentState && !isset($a['attr']['bx_grid_permanent_state']))
  489. $a['attr']['bx_grid_permanent_state'] = 1;
  490. $sRet .= $this->$sFunc($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  491. }
  492. return $sRet;
  493. }
  494. protected function _getActionDefault ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  495. {
  496. if ($a['icon_only'] && empty($a['attr']['title']) && !empty($a['title']))
  497. $a['attr']['title'] = $a['title'];
  498. $sAttr = $this->_convertAttrs(
  499. $a, 'attr',
  500. 'bx-btn bx-def-margin-thd' . ($isSmall ? ' bx-btn-small' : '') . ($isDisabled ? ' bx-btn-disabled' : '') // add default classes
  501. );
  502. $sIcon = '';
  503. $sImage = '';
  504. if (!empty($a['icon'])) {
  505. if (false === strpos($a['icon'], '.'))
  506. $sIcon = '<i class="sys-icon ' . $a['icon'] . '"></i>';
  507. elseif ($sIconUrl = $this->_oTemplate->getIconUrl($a['icon']))
  508. $sImage = '<img style="background-image:url(' . $sIconUrl . ');" src="' . $this->_oTemplate->getIconUrl('spacer.gif') .'" />';
  509. }
  510. return '<button ' . $sAttr . '>' . $sIcon . $sImage . ($a['icon_only'] || empty($a['title']) ? '' : '<u>' . $a['title'] . '</u>') . '</button>';
  511. }
  512. protected function _getActionDivider ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  513. {
  514. return '<div class="bx-grid-actions-divider bx-def-margin-sec-left"> | </div>';
  515. }
  516. protected function _getFilterControls ()
  517. {
  518. $oForm = new BxTemplFormView(array());
  519. $aInput = array(
  520. 'type' => 'text',
  521. 'name' => 'keyword',
  522. 'attrs' => array(
  523. 'id' => 'bx-grid-search-' . $this->_sObject
  524. )
  525. );
  526. $this->_oTemplate->addCss('forms.css');
  527. return $oForm->genRow($aInput);
  528. }
  529. protected function _getCounter()
  530. {
  531. return _t('_sys_grid_total_count', $this->_iTotalCount);
  532. }
  533. protected function _limitMaxLength ($mixedValue, $sKey, $aField, $aRow, $isDisplayPopupOnTextOverflow, $bReturnString = true)
  534. {
  535. if ($aField['chars_limit'] > 0)
  536. $mixedValue = BxTemplFunctions::getInstance()->getStringWithLimitedLength($mixedValue, $aField['chars_limit'], $isDisplayPopupOnTextOverflow, $bReturnString);
  537. return $mixedValue;
  538. }
  539. protected function _convertAttrs ($aField, $sAttrName, $sClasses = false, $sStyles = false)
  540. {
  541. if (!empty($aField['hidden_on'])) {
  542. $aHiddenOn = array(
  543. pow(2, BX_DB_HIDDEN_PHONE - 1) => 'bx-def-media-phone-hide',
  544. pow(2, BX_DB_HIDDEN_TABLET - 1) => 'bx-def-media-tablet-hide',
  545. pow(2, BX_DB_HIDDEN_DESKTOP - 1) => 'bx-def-media-desktop-hide',
  546. pow(2, BX_DB_HIDDEN_MOBILE - 1) => 'bx-def-mobile-app-hide'
  547. );
  548. foreach ($aHiddenOn as $iHiddenOn => $sClass)
  549. if ((int)$aField['hidden_on'] & $iHiddenOn)
  550. $sClasses .= ' ' . $sClass;
  551. }
  552. return bx_convert_array2attrs(
  553. isset($aField[$sAttrName]) && is_array($aField[$sAttrName]) ? $aField[$sAttrName] : array(),
  554. $sClasses,
  555. $sStyles
  556. );
  557. }
  558. protected function _updateOrder($mixedId, $iOrder)
  559. {
  560. $oDb = BxDolDb::getInstance();
  561. $sTable = $this->_aOptions['table'];
  562. $sFieldId = $this->_aOptions['field_id'];
  563. $sFieldOrder = $this->_aOptions['field_order'];
  564. $sQuery = $oDb->prepare("UPDATE `{$sTable}` SET `{$sFieldOrder}` = ? WHERE `{$sFieldId}` = ?", $iOrder, $mixedId);
  565. return $oDb->query($sQuery);
  566. }
  567. protected function _delete ($mixedId)
  568. {
  569. $oDb = BxDolDb::getInstance();
  570. $sTable = $this->_aOptions['table'];
  571. $sFieldId = $this->_aOptions['field_id'];
  572. $sQuery = $oDb->prepare("DELETE FROM `{$sTable}` WHERE `{$sFieldId}` = ?", $mixedId);
  573. return $oDb->query($sQuery);
  574. }
  575. protected function _enable ($mixedId, $isChecked)
  576. {
  577. $oDb = BxDolDb::getInstance();
  578. $sTable = $this->_aOptions['table'];
  579. $sFieldId = $this->_aOptions['field_id'];
  580. $sFieldActive = $this->_aOptions['field_active'];
  581. $sQuery = $oDb->prepare("UPDATE `{$sTable}` SET `$sFieldActive` = ? WHERE `{$sFieldId}` = ?", $this->_switcherChecked2State($isChecked), $mixedId);
  582. return $oDb->query($sQuery);
  583. }
  584. protected function _addJsCss()
  585. {
  586. if ($this->_aOptions['field_order']) {
  587. $aUiLangs = BxBaseFormView::getJsUiLangs ();
  588. $sUiLang = BxDolLanguages::getInstance()->detectLanguageFromArray ($aUiLangs);
  589. $this->_oTemplate->addJs(array(
  590. 'jquery-ui/jquery-ui.custom.min.js',
  591. 'jquery-ui/i18n/jquery.ui.datepicker-' . $sUiLang . '.min.js',
  592. 'URI.js',
  593. ));
  594. $this->_oTemplate->addCss('jquery-ui/jquery-ui.css');
  595. }
  596. $this->_oTemplate->addJs('BxDolGrid.js');
  597. $this->_oTemplate->addCss('grid.css');
  598. $this->_oTemplate->addJsTranslation('_sys_grid_confirmation');
  599. }
  600. }
  601. /** @} */