BxBaseGrid.php 31 KB

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