BxBaseGrid.php 26 KB

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