1
0

BxBaseGrid.php 27 KB

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