BxBaseGrid.php 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297
  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 $_oFunctions;
  17. protected $_aPopupOptions = false;
  18. protected $_aQueryAppend = [];
  19. protected $_aQueryAppendExclude = false; // an array of keys which shouldn't be pathed in http requests, but can be stored (used) in 'Query Append' array.
  20. protected $_aQueryAppendExcludeApi = false;
  21. protected $_aQueryReset = false;
  22. protected $_aConfirmMessages = false;
  23. protected $_bSelectAll = false;
  24. protected $_isDisplayPopupOnTextOverflow = true;
  25. public function __construct ($aOptions, $oTemplate)
  26. {
  27. parent::__construct ($aOptions);
  28. if ($oTemplate)
  29. $this->_oTemplate = $oTemplate;
  30. else
  31. $this->_oTemplate = BxDolTemplate::getInstance();
  32. $this->_oFunctions = BxTemplFunctions::getInstanceWithTemplate($this->_oTemplate);
  33. $this->_aPopupOptions = [];
  34. $this->_aQueryAppend = [
  35. $this->_oTemplate->getCodeKey() => $this->_oTemplate->getCode()
  36. ];
  37. $this->_aQueryAppendExclude = [];
  38. $this->_aQueryAppendExcludeApi = [$this->_oTemplate->getCodeKey()];
  39. $this->_aQueryReset = [
  40. $this->_aOptions['filter_get'],
  41. $this->_aOptions['order_get_field'],
  42. $this->_aOptions['order_get_dir'],
  43. $this->_aOptions['paginate_get_start'],
  44. $this->_aOptions['paginate_get_per_page']
  45. ];
  46. $this->_aConfirmMessages = [];
  47. }
  48. public function performActionDisplay()
  49. {
  50. if($this->_bIsApi)
  51. return $this->getCodeAPI();
  52. require_once(BX_DIRECTORY_PATH_INC . "design.inc.php");
  53. echoJson(array(
  54. 'grid' => $this->getCode(false),
  55. 'total_count' => $this->_iTotalCount,
  56. 'total_count_f' => $this->_getCounter()
  57. ));
  58. }
  59. public function performActionReorder()
  60. {
  61. $this->_replaceMarkers ();
  62. $aOrder = bx_get($this->_sObject . '_row');
  63. $iOrder = 0;
  64. foreach ($aOrder as $mixedId)
  65. $this->_updateOrder($mixedId, ++$iOrder);
  66. return $this->_getActionResult([]);
  67. }
  68. public function performActionDelete()
  69. {
  70. $this->_replaceMarkers ();
  71. $aResult = [];
  72. $iAffected = 0;
  73. $aIds = bx_get('ids');
  74. if (!$aIds || !is_array($aIds))
  75. return $this->_getActionResult($aResult);
  76. foreach ($aIds as $mixedId)
  77. $iAffected += $this->_delete($mixedId) ? 1 : 0;
  78. if($iAffected)
  79. $aResult = !$this->_bIsApi ? ['grid' => $this->getCode(false)] : [];
  80. else
  81. $aResult = ['msg' => _t("_sys_grid_delete_failed")];
  82. return $this->_getActionResult($aResult);
  83. }
  84. public function performActionEnable($mixedChecked = null)
  85. {
  86. $this->_replaceMarkers ();
  87. $aIds = bx_get('ids');
  88. if (!$aIds || !is_array($aIds)) {
  89. echoJson(array());
  90. exit;
  91. }
  92. $iChecked = (int)($mixedChecked !== null ? $mixedChecked : bx_get('checked'));
  93. $aAffectedIds = array ();
  94. foreach ($aIds as $mixedId)
  95. if ($this->_enable($mixedId, $iChecked))
  96. $aAffectedIds[] = preg_match("/^[\d\w]+$/", $mixedId) ? $mixedId : (int)$mixedId;
  97. $sAction = $iChecked ? 'enable' : 'disable';
  98. if($this->_bIsApi)
  99. return $aAffectedIds;
  100. echo echoJson(array($sAction => $aAffectedIds));
  101. }
  102. public function performActionRevealSecret()
  103. {
  104. $this->_replaceMarkers();
  105. if (!isLogged())
  106. return $this->_getActionResult(['msg' => _t('_Access denied')]);
  107. if (!$this->_isCurrentPasswordValid(bx_get('password')))
  108. return $this->_getActionResult(['msg' => _t('_sys_form_account_input_password_current_error')]);
  109. $aIds = bx_get('ids');
  110. $sKey = bx_process_input(bx_get('field'));
  111. $mixedId = is_array($aIds) && $aIds ? reset($aIds) : false;
  112. if ($mixedId === false || $sKey === false || $sKey === '' || empty($this->_aOptions['fields'][$sKey]))
  113. return $this->_getActionResult(['msg' => _t('_sys_txt_error_occured')]);
  114. $aRow = $this->_getSecretRow($mixedId);
  115. if (!$aRow)
  116. return $this->_getActionResult(['msg' => _t('_sys_txt_error_occured')]);
  117. return $this->_getActionResult([
  118. 'secret' => [
  119. 'id' => $mixedId,
  120. 'field' => $sKey,
  121. 'value' => $this->_getSecretValue($mixedId, $sKey, $aRow),
  122. ]
  123. ]);
  124. }
  125. public function getCode ($isDisplayHeader = true)
  126. {
  127. $this->_replaceMarkers ();
  128. if ($isDisplayHeader && empty($this->_aOptions['paginate_url'])) {
  129. // reset page query params if grid is just initialized and it uses AJAX paginate
  130. $this->resetQueryParams();
  131. }
  132. $sPaginate = '';
  133. $aData = array();
  134. $sIdWrapper = 'bx-grid-wrap-' . $this->_sObject;
  135. $sIdContainer = 'bx-grid-cont-' . $this->_sObject;
  136. $sIdTable = 'bx-grid-table-' . $this->_sObject;
  137. $sFilter = $this->_getFilterValue();
  138. $sOrderField = $this->_getOrderValue();
  139. $sOrderDir = 0 === strcasecmp('desc', bx_get($this->_aOptions['order_get_dir'])) ? 'DESC' : 'ASC';
  140. $iStart = 0;
  141. if($this->_aOptions['paginate_get_start'] && ($iStartGet = (int)bx_get($this->_aOptions['paginate_get_start'])) >= 0)
  142. $iStart = $iStartGet;
  143. $iPerPage = 10;
  144. if($this->_aOptions['paginate_get_per_page'] && ($iPerPageGet = (int)bx_get($this->_aOptions['paginate_get_per_page'])) > 0)
  145. $iPerPage = $iPerPageGet;
  146. else if($this->_aOptions['paginate_per_page'])
  147. $iPerPage = (int)$this->_aOptions['paginate_per_page'];
  148. if ($this->_aOptions['paginate_get_start']) {
  149. $aData = $this->_getData ($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage + 1);
  150. $sPageUrl = false;
  151. if (!empty($this->_aOptions['paginate_url'])) {
  152. $sPageUrl = $this->_aOptions['paginate_url'];
  153. $aParamsAppend = array();
  154. if ($sFilter) {
  155. $aParamsAppend['filter'] = bx_process_input(bx_get($this->_aOptions['filter_get']));
  156. }
  157. if ($sOrderField) {
  158. $aParamsAppend['order_field'] = bx_process_input(bx_get($this->_aOptions['order_get_field']));
  159. $aParamsAppend['order_dir'] = bx_process_input(bx_get($this->_aOptions['order_get_dir']));
  160. }
  161. if ($aParamsAppend)
  162. $sPageUrl = bx_append_url_params($sPageUrl, $aParamsAppend);
  163. }
  164. $aPaginateParams = array(
  165. 'start' => $iStart,
  166. 'per_page' => $iPerPage,
  167. 'page_url' => $sPageUrl ? $sPageUrl : "javascript:glGrids." . $this->_sObject . ".reload('{start}'); void(0);",
  168. );
  169. $oPaginate = new BxTemplPaginate($aPaginateParams, $this->_oTemplate);
  170. $oPaginate->setNumFromDataArray($aData);
  171. if (isset($this->_aOptions['paginate_simple']) && false !== $this->_aOptions['paginate_simple'])
  172. $sPaginate = $oPaginate->getSimplePaginate($this->_aOptions['paginate_simple']);
  173. else
  174. $sPaginate = $oPaginate->getPaginate();
  175. } else {
  176. $aData = $this->_getData ($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);
  177. }
  178. if((empty($aData) || !is_array($aData)) && isset($this->_aBrowseParams['empty_message']) && !(bool)$this->_aBrowseParams['empty_message'])
  179. return '';
  180. $sPopupOptions = '{}';
  181. if (!empty($this->_aPopupOptions) && is_array($this->_aPopupOptions))
  182. $sPopupOptions = json_encode($this->_aPopupOptions);
  183. $aQueryAppend = array_merge(is_array($this->_aQueryAppend) ? $this->_aQueryAppend : array(), is_array($this->_aMarkers) ? $this->_aMarkers : array());
  184. if(!empty($this->_aQueryAppendExclude) && is_array($this->_aQueryAppendExclude))
  185. $aQueryAppend = array_diff_key($aQueryAppend, array_flip($this->_aQueryAppendExclude));
  186. $sQueryAppend = '{}';
  187. if (!empty($aQueryAppend) && is_array($aQueryAppend))
  188. $sQueryAppend = json_encode($aQueryAppend);
  189. $sConfirmMessages = '{}';
  190. if (!empty($this->_aConfirmMessages) && is_array($this->_aConfirmMessages))
  191. $sConfirmMessages = json_encode($this->_aConfirmMessages);
  192. $iColumns = count($this->_aOptions['fields']);
  193. $aVarsHead = $this->_getRowHead();
  194. $sClassHeader = '';
  195. if($this->_aOptions['show_total_count'] == 0)
  196. $sClassHeader .= ' bx-gh-no-counter';
  197. $aVars = array (
  198. 'object' => $this->_sObject,
  199. 'csrf_token' => BxDolForm::getCsrfToken(),
  200. 'id_table' => $sIdTable,
  201. 'id_cont' => $sIdContainer,
  202. 'id_wrap' => $sIdWrapper,
  203. 'class_table_wrapper' => $this->_aOptions['responsive'] ? 'bx-grid-table-wrapper-responsive' : '',
  204. 'sortable' => empty($this->_aOptions['field_order']) ? 0 : 1,
  205. 'sorting' => empty($this->_getOrderFields()) ? 0 : 1,
  206. 'sorting_field' => $sOrderField,
  207. 'sorting_dir' => $sOrderDir,
  208. 'bx_if:display_head' => array(
  209. 'condition' => !empty($aVarsHead),
  210. 'content' => array(
  211. 'bx_repeat:row_header' => $aVarsHead,
  212. 'columns' => $iColumns,
  213. )
  214. ),
  215. 'bx_repeat:rows_data' => $this->_getRowsDataDesign ($aData),
  216. 'paginate_get_start' => $this->_aOptions['paginate_get_start'],
  217. 'paginate_get_per_page' => $this->_aOptions['paginate_get_per_page'],
  218. 'start' => $iStart,
  219. 'per_page' => $iPerPage,
  220. 'filter' => bx_js_string($sFilter, BX_ESCAPE_STR_APOS),
  221. 'filter_get' => bx_js_string($this->_aOptions['filter_get']),
  222. 'order_field' => bx_js_string($sOrderField, BX_ESCAPE_STR_APOS),
  223. 'order_dir' => bx_js_string($sOrderDir, BX_ESCAPE_STR_APOS),
  224. 'order_get_field' => bx_js_string($this->_aOptions['order_get_field']),
  225. 'order_get_dir' => bx_js_string($this->_aOptions['order_get_dir']),
  226. 'popup_options' => $sPopupOptions,
  227. 'query_append' => $sQueryAppend,
  228. 'confirm_messages' => $sConfirmMessages,
  229. 'columns' => $iColumns,
  230. 'bx_if:display_footer' => array(
  231. 'condition' => !empty($this->_aOptions['actions_bulk']) || !empty($sPaginate),
  232. 'content' => array(
  233. 'bx_if:actions_bulk' => array (
  234. 'condition' => !empty($this->_aOptions['actions_bulk']),
  235. 'content' => array(
  236. 'actions_bulk' => $this->_getActions ('bulk'),
  237. ),
  238. ),
  239. 'paginate' => $sPaginate,
  240. )
  241. ),
  242. 'bx_if:display_header' => array (
  243. 'condition' => $isDisplayHeader,
  244. 'content' => array (
  245. 'class' => $sClassHeader,
  246. 'bx_if:actions_independent' => array (
  247. 'condition' => !empty($this->_aOptions['actions_independent']),
  248. 'content' => array(
  249. 'actions_independent' => $this->_getActions ('independent'),
  250. ),
  251. ),
  252. 'bx_if:filter' => array (
  253. 'condition' => !empty($this->_aOptions['filter_fields']) || !empty($this->_aOptions['filter_fields_translatable']),
  254. 'content' => array(
  255. 'controls' => $this->_getFilterControls(),
  256. ),
  257. ),
  258. 'bx_if:counter' => array (
  259. 'condition' => $this->_aOptions['show_total_count'] == 1,
  260. 'content' => array(
  261. 'counter' => $this->_getCounter(),
  262. ),
  263. ),
  264. ),
  265. ),
  266. );
  267. $this->_addJsCss();
  268. return $this->_oTemplate->parseHtmlByName('grid.html', $aVars);
  269. }
  270. protected function _getJsCodeVars()
  271. {
  272. $sFilter = $this->_getFilterValue();
  273. $sOrderField = $this->_getOrderValue();
  274. $sOrderDir = 0 === strcasecmp('desc', bx_get($this->_aOptions['order_get_dir'])) ? 'DESC' : 'ASC';
  275. $iStart = 0;
  276. if($this->_aOptions['paginate_get_start'] && ($iStartGet = (int)bx_get($this->_aOptions['paginate_get_start'])) >= 0)
  277. $iStart = $iStartGet;
  278. $iPerPage = 10;
  279. if($this->_aOptions['paginate_get_per_page'] && ($iPerPageGet = (int)bx_get($this->_aOptions['paginate_get_per_page'])) > 0)
  280. $iPerPage = $iPerPageGet;
  281. else if($this->_aOptions['paginate_per_page'])
  282. $iPerPage = (int)$this->_aOptions['paginate_per_page'];
  283. $sPopupOptions = '{}';
  284. if (!empty($this->_aPopupOptions) && is_array($this->_aPopupOptions))
  285. $sPopupOptions = json_encode($this->_aPopupOptions);
  286. $aQueryAppend = array_merge(is_array($this->_aQueryAppend) ? $this->_aQueryAppend : array(), is_array($this->_aMarkers) ? $this->_aMarkers : array());
  287. if(!empty($this->_aQueryAppendExclude) && is_array($this->_aQueryAppendExclude))
  288. $aQueryAppend = array_diff_key($aQueryAppend, array_flip($this->_aQueryAppendExclude));
  289. $sQueryAppend = '{}';
  290. if (!empty($aQueryAppend) && is_array($aQueryAppend))
  291. $sQueryAppend = json_encode($aQueryAppend);
  292. $sConfirmMessages = '{}';
  293. if (!empty($this->_aConfirmMessages) && is_array($this->_aConfirmMessages))
  294. $sConfirmMessages = json_encode($this->_aConfirmMessages);
  295. return [
  296. 'object' => $this->_sObject,
  297. 'csrf_token' => BxDolForm::getCsrfToken(),
  298. 'sortable' => empty($this->_aOptions['field_order']) ? 0 : 1,
  299. 'sorting' => empty($this->_getOrderFields()) ? 0 : 1,
  300. 'paginate_get_start' => $this->_aOptions['paginate_get_start'],
  301. 'paginate_get_per_page' => $this->_aOptions['paginate_get_per_page'],
  302. 'start' => $iStart,
  303. 'per_page' => $iPerPage,
  304. 'filter' => bx_js_string($sFilter, BX_ESCAPE_STR_APOS),
  305. 'filter_get' => bx_js_string($this->_aOptions['filter_get']),
  306. 'order_field' => bx_js_string($sOrderField, BX_ESCAPE_STR_APOS),
  307. 'order_dir' => bx_js_string($sOrderDir, BX_ESCAPE_STR_APOS),
  308. 'order_get_field' => bx_js_string($this->_aOptions['order_get_field']),
  309. 'order_get_dir' => bx_js_string($this->_aOptions['order_get_dir']),
  310. 'popup_options' => $sPopupOptions,
  311. 'query_append' => $sQueryAppend,
  312. 'confirm_messages' => $sConfirmMessages,
  313. 'columns' => count($this->_aOptions['fields']),
  314. ];
  315. }
  316. /**
  317. * Init grid JS class without rendering grid HTML.
  318. * @return string
  319. */
  320. public function getCodeJs()
  321. {
  322. $this->_replaceMarkers();
  323. $this->_addJsCss();
  324. return $this->_oTemplate->parseHtmlByName('grid_js.html', $this->_getJsCodeVars());
  325. }
  326. /**
  327. * Get raw actions list (name, title, icon, confirm) without HTML.
  328. * Custom `_getAction[Name]` methods that return empty hide the action, same as in the grid.
  329. * @param string $sType action type: single, bulk, independent
  330. * @param mixed $sActionData row id for single actions
  331. * @param array $aRow row data, used by custom action methods
  332. * @return array
  333. */
  334. public function getActionsRaw($sType, $sActionData = false, $aRow = [])
  335. {
  336. $sActionsType = 'actions_' . $sType;
  337. if (empty($this->_aOptions[$sActionsType]) || !is_array($this->_aOptions[$sActionsType]))
  338. return [];
  339. $aResult = [];
  340. foreach ($this->_aOptions[$sActionsType] as $sKey => $a) {
  341. if(!$a['active'] || (!$a['title'] && !$a['icon']))
  342. continue;
  343. $sFunc = '_getAction' . $this->_genMethodName($sKey);
  344. if (method_exists($this, $sFunc)) {
  345. if (!isset($a['attr']))
  346. $a['attr'] = [];
  347. $mixed = $this->$sFunc($sType, $sKey, $a, false, false, $aRow);
  348. if ($mixed === '' || $mixed === false)
  349. continue;
  350. }
  351. $aResult[] = [
  352. 'name' => $sKey,
  353. 'title' => $a['title'],
  354. 'icon' => !empty($a['icon']) ? $a['icon'] : '',
  355. 'confirm' => !empty($a['confirm']) ? 1 : 0,
  356. 'reset_paginate' => false === strpos($sKey, 'delete') ? 0 : 1,
  357. ];
  358. }
  359. return $aResult;
  360. }
  361. /**
  362. * Get grid code API.
  363. * @return array
  364. */
  365. public function getCodeAPI($bForceReturn = false)
  366. {
  367. $this->_replaceMarkers();
  368. $sFilter = bx_unicode_urldecode(bx_process_input(bx_get($this->_aOptions['filter_get'])));
  369. $sOrderField = bx_unicode_urldecode(bx_process_input(bx_get($this->_aOptions['order_get_field'])));
  370. $sOrderDir = 0 === strcasecmp('desc', bx_get($this->_aOptions['order_get_dir'])) ? 'DESC' : 'ASC';
  371. $iStart = 0;
  372. if($this->_aOptions['paginate_get_start'] && ($iStartGet = (int)bx_get($this->_aOptions['paginate_get_start'])) >= 0)
  373. $iStart = $iStartGet;
  374. $iPerPage = 10;
  375. if($this->_aOptions['paginate_get_per_page'] && ($iPerPageGet = (int)bx_get($this->_aOptions['paginate_get_per_page'])) > 0)
  376. $iPerPage = $iPerPageGet;
  377. else if($this->_aOptions['paginate_per_page'])
  378. $iPerPage = (int)$this->_aOptions['paginate_per_page'];
  379. $aData = $this->_getData($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);
  380. if(!empty($aData) && is_array($aData))
  381. $aData = $this->decodeDataAPI($aData);
  382. $aQueryAppend = [];
  383. if(!empty($this->_aQueryAppend) && is_array($this->_aQueryAppend))
  384. $aQueryAppend = array_merge($aQueryAppend, $this->_aQueryAppend);
  385. if(!empty($this->_aMarkers) && is_array($this->_aMarkers))
  386. $aQueryAppend = array_merge($aQueryAppend, $this->_aMarkers);
  387. if(!empty($this->_aQueryAppendExclude) && is_array($this->_aQueryAppendExclude))
  388. $aQueryAppend = array_diff_key($aQueryAppend, array_flip($this->_aQueryAppendExclude));
  389. if(!empty($this->_aQueryAppendExcludeApi) && is_array($this->_aQueryAppendExcludeApi))
  390. $aQueryAppend = array_diff_key($aQueryAppend, array_flip($this->_aQueryAppendExcludeApi));
  391. return [
  392. 'settings' => [
  393. 'object' => $this->_aOptions['object'],
  394. 'field_id' => $this->_aOptions['field_id'],
  395. 'start' => $iStart,
  396. 'per_page' => $iPerPage,
  397. 'filters' => $this->_getFilterControlsAPI(),
  398. 'query_append' => $aQueryAppend
  399. ],
  400. 'header' => $this->_getRowHeadAPI(),
  401. 'data' => defined('BX_API_PAGE') && !$bForceReturn ? [] : $aData,
  402. 'actions' => [
  403. 'independent' => $this->_getActionsAPI('independent'),
  404. 'bulk' => $this->_getActionsAPI('bulk')
  405. ]
  406. ];
  407. }
  408. public function decodeDataAPI($aData)
  409. {
  410. $sKeyId = $this->_aOptions['field_id'];
  411. $sMethodDefault = '_getCellDefault';
  412. $aDataRv = [];
  413. foreach($aData as $iKey => $aRow) {
  414. $aDataRv[$iKey] = [$sKeyId => $aData[$iKey][$sKeyId]];
  415. foreach($this->_aOptions['fields'] as $sKey => $aField) {
  416. $sMethod = '_getCell' . $this->_genMethodName($sKey);
  417. if(!method_exists($this, $sMethod))
  418. $sMethod = !empty($aField['secret']) ? '_getCellSecret' : $sMethodDefault;
  419. if(($aCell = $this->$sMethod(isset($aData[$iKey][$sKey]) ? $aData[$iKey][$sKey] : _t('_undefined'), $sKey, $aField, $aRow)))
  420. $aDataRv[$iKey][$sKey] = $aCell;
  421. }
  422. }
  423. return $aDataRv;
  424. }
  425. public function getFormBlockAPI($oForm, $sAction, $iId = 0)
  426. {
  427. return [
  428. bx_api_get_block('form', $oForm->getCodeAPI(), [
  429. 'ext' => [
  430. 'name' => $this->_oModule->getName(),
  431. 'title' => $this->getFormBlockTitleAPI($sAction, $iId),
  432. 'request' => ['url' => $this->getFormCallBackUrlAPI($sAction, $iId), 'immutable' => true]]
  433. ]
  434. )
  435. ];
  436. }
  437. public function getFormBlockTitleAPI($sAction, $iId)
  438. {
  439. return '';
  440. }
  441. public function getFormCallBackUrlAPI($sAction, $iId)
  442. {
  443. return '';
  444. }
  445. /**
  446. * Reset query params, like filter and page number
  447. */
  448. public function resetQueryParams()
  449. {
  450. foreach ($this->_aQueryReset as $sKey) {
  451. unset($_GET[$sKey]);
  452. unset($_POST[$sKey]);
  453. }
  454. }
  455. protected function _getRowHead()
  456. {
  457. $aRet = array();
  458. foreach ($this->_aOptions['fields'] as $sKey => $a) {
  459. $sMethod = '_getCellHeaderDefault';
  460. $sCustomMethod = '_getCellHeader' . $this->_genMethodName($sKey);
  461. if (method_exists($this, $sCustomMethod))
  462. $sMethod = $sCustomMethod;
  463. $aRet[] = array('header_cell' => $this->$sMethod($sKey, $a));
  464. }
  465. return $aRet;
  466. }
  467. protected function _getRowHeadAPI()
  468. {
  469. $bResize = false;
  470. $aHeader = [];
  471. foreach($this->_aOptions['fields'] as $sKey => $aField) {
  472. if($sKey == 'checkbox' && (int)$aField['width'] < 8)
  473. $bResize = true;
  474. $sMethod = '_getCellHeader' . $this->_genMethodName($sKey);
  475. if(($aHeaderItem = $this->{method_exists($this, $sMethod) ? $sMethod : '_getCellHeaderDefault'}($sKey, $aField)))
  476. $aHeader[] = $aHeaderItem;
  477. }
  478. if(($sKey = 'checkbox') && $bResize)
  479. $this->_resizeCellAPI($sKey, $this->_aOptions['fields'][$sKey], $aHeader);
  480. return $aHeader;
  481. }
  482. protected function _resizeCellAPI($sKey, $aField, &$aHeader)
  483. {
  484. $iMin = 8;
  485. $iAdd = $iMin - (int)$aField['width'];
  486. $iThreshold = $iMin + $iAdd;
  487. $iReduce = 0;
  488. array_walk($aHeader, function($aItem) use ($iThreshold, &$iReduce) {
  489. if((int)$aItem['width'] >= $iThreshold)
  490. $iReduce += 1;
  491. });
  492. $fSub = round($iAdd / $iReduce, 2);
  493. array_walk($aHeader, function(&$aItem) use ($sKey, $iThreshold, $iAdd, $fSub) {
  494. if($aItem['name'] == $sKey)
  495. $aItem['width'] = (float)$aItem['width'] + $iAdd;
  496. else if((int)$aItem['width'] >= $iThreshold)
  497. $aItem['width'] = (float)$aItem['width'] - $fSub;
  498. else
  499. return;
  500. $aItem['width'] .= '%';
  501. });
  502. }
  503. protected function _getCellHeaderDefault ($sKey, $aField)
  504. {
  505. if($this->_bIsApi)
  506. return [
  507. 'name' => bx_process_output($aField['name']),
  508. 'title' => bx_process_output($aField['title']),
  509. 'width' => $aField['width']
  510. ];
  511. $sHeader = bx_process_output($aField['title']);
  512. if (($aSortingFields = $this->_getOrderFields()) && in_array($sKey, $aSortingFields)) {
  513. $sHeader = '<a href="javascript:void(0);" class="bx-grid-sort-handle">' . $sHeader . '</a><span class="bx-grid-sort-indi"></span>';
  514. $aField['attr_head']['bx_grid_sort_head'] = $sKey;
  515. }
  516. $sAttr = $this->_convertAttrs(
  517. $aField, 'attr_head',
  518. 'bx-def-padding-sec-bottom bx-def-padding-sec-top bx-grid-header-' . $sKey, // add default classes
  519. isset($aField['width']) ? 'width:' . $aField['width'] : false // add default styles
  520. );
  521. return $this->_getCellHeaderWrapper ($sKey, $aField, $sHeader, $sAttr);
  522. }
  523. protected function _getCellHeaderCheckbox ($sKey, $aField)
  524. {
  525. if($this->_bIsApi)
  526. return $this->_getCellHeaderDefault($sKey, $aField);
  527. $aAttr = array(
  528. 'type' => 'checkbox',
  529. 'id' => $this->_sObject . '_check_all',
  530. 'name' => $this->_sObject . '_check_all',
  531. 'onclick' => "$('input[name=" . $this->_sObject . "_check]:not([disabled])').attr('checked', this.checked)"
  532. );
  533. if($this->_bSelectAll)
  534. $aAttr['checked'] = 'checked';
  535. $aField['attr'] = isset($aField['attr']) && is_array($aField['attr']) ? array_merge($aAttr, $aField['attr']) : $aAttr;
  536. $sAttrHead = $this->_convertAttrs(
  537. $aField, 'attr_head',
  538. 'bx-def-padding-sec-bottom bx-def-padding-sec-top bx-grid-header-' . $sKey, // add default classes
  539. isset($aField['width']) ? 'width:' . $aField['width'] : false // add default styles
  540. );
  541. return $this->_getCellHeaderWrapper ($sKey, $aField, ' <input ' . $this->_convertAttrs($aField, 'attr') . ' /> ', $sAttrHead);
  542. }
  543. protected function _getCellHeaderWrapper ($sKey, $aField, $sHeader, $sAttr)
  544. {
  545. return '<th ' . $sAttr . '>' . $sHeader . '</th>';
  546. }
  547. /**
  548. * Check if the whole row is disabled.
  549. * When row is disabled - checkbox is not selectable, actions aren't clickable and text is grayed out.
  550. * By default all rows aren't disabled.
  551. * @param $aRow row array
  552. * @return boolean
  553. */
  554. protected function _isRowDisabled($aRow)
  555. {
  556. if (isset($aRow[$this->_aOptions['field_active']]) && !$this->_switcherState2Checked($aRow[$this->_aOptions['field_active']]))
  557. return true;
  558. return false;
  559. }
  560. /**
  561. * Determine how actions are disabled when whole row is disabled.
  562. * @param $aRow row array
  563. * @return null - disable/enable actions when row is disabled/enabled, true - actions are always disabled, false - actions are always enabled
  564. */
  565. protected function _getActionsDisabledBehavior($aRow)
  566. {
  567. return null;
  568. }
  569. /**
  570. * Check if the checkbox is disabled.
  571. * @param $aRow row array
  572. * @return boolean
  573. */
  574. protected function _isCheckboxDisabled($aRow)
  575. {
  576. return $this->_isRowDisabled($aRow);
  577. }
  578. /**
  579. * Is checkbox checked by default ?
  580. * By default no one checkbox is selected.
  581. * @return boolean
  582. */
  583. protected function _isCheckboxSelected($mixedValue, $sKey, $aField, $aRow)
  584. {
  585. return $this->_bSelectAll;
  586. }
  587. /**
  588. * Is switcher on by default ?
  589. * By default no one switcher is on.
  590. * @return boolean
  591. */
  592. protected function _isSwitcherOn($mixedValue, $sKey, $aField, $aRow)
  593. {
  594. return $this->_switcherState2Checked($mixedValue);
  595. }
  596. /**
  597. * Convert switcher checked status to the actual value ?
  598. * @return boolean
  599. */
  600. protected function _switcherChecked2State($isChecked)
  601. {
  602. return $isChecked ? 1 : 0;
  603. }
  604. /**
  605. * Convert switcher value to checked(boolean) value ?
  606. * @return boolean
  607. */
  608. protected function _switcherState2Checked($mixedState)
  609. {
  610. return $mixedState ? true : false;
  611. }
  612. protected function _getRowsDataDesign (&$aData)
  613. {
  614. $aGrid = array();
  615. if(empty($aData))
  616. $aGrid[] = array(
  617. 'id_row' => 0,
  618. 'row_class' => 'bx-grid-table-row-empty',
  619. 'row' => '<td class="bx-def-padding-sec-topbottom" colspan="' . count($this->_aOptions['fields']) . '">' . MsgBox(_t('_Empty')) . '</td>'
  620. );
  621. else
  622. foreach ($aData as $aRow)
  623. $aGrid[] = array(
  624. 'id_row' => $this->_getRowId($aRow),
  625. 'row_class' => $this->_isRowDisabled($aRow) ? 'bx-grid-table-row-disabled bx-def-font-grayed' : '',
  626. 'row' => $this->_getRowDesign($aRow)
  627. );
  628. return $aGrid;
  629. }
  630. protected function _getRowId($mixedRow)
  631. {
  632. if(is_string($mixedRow))
  633. $sId = rand(1, getrandmax());
  634. else
  635. $sId = $mixedRow[$this->_aOptions['field_id']];
  636. return $this->_sObject . '_row_' . $sId;
  637. }
  638. protected function _getRowDesign($mixedRow)
  639. {
  640. $sRow = '';
  641. if(is_string($mixedRow))
  642. $sRow = '<td class="bx-grid-cell bx-gc-string bx-def-padding-sec-topbottom bx-def-font-bold" colspan="' . count($this->_aOptions['fields']) . '">' . $mixedRow . '</td>';
  643. else
  644. foreach($this->_aOptions['fields'] as $sKey => $aField)
  645. $sRow .= $this->_getCellDesign($sKey, $aField, $mixedRow);
  646. return $sRow;
  647. }
  648. protected function _getCellDesign($sKey, $aField, $aRow)
  649. {
  650. $sMethod = '_getCellDefault';
  651. if ($this->_aOptions['field_order'] == $sKey)
  652. $sMethod = '_getCellOrder';
  653. $sCustomMethod = '_getCell' . $this->_genMethodName($sKey);
  654. if (method_exists($this, $sCustomMethod))
  655. $sMethod = $sCustomMethod;
  656. else if (!empty($aField['secret']))
  657. $sMethod = '_getCellSecret';
  658. $mixedValue = $this->_getCellData($sKey, $aField, $aRow);
  659. if ($aField['translatable'])
  660. $mixedValue = _t($mixedValue);
  661. if ($sMethod != '_getCellSecret')
  662. $mixedValue = $this->_limitMaxLength($mixedValue, $sKey, $aField, $aRow, $this->_isDisplayPopupOnTextOverflow);
  663. return $this->$sMethod($mixedValue, $sKey, $aField, $aRow);
  664. }
  665. protected function _getCellCheckbox ($mixedValue, $sKey, $aField, $aRow)
  666. {
  667. if($this->_bIsApi)
  668. return ['type' => 'checkbox', 'data' => $aRow[$this->_aOptions['field_id']]];
  669. $sAttr = $this->_convertAttrs(
  670. $aField, 'attr_cell',
  671. 'bx-def-padding-sec-bottom bx-def-padding-sec-top', // add default classes
  672. isset($aField['width']) ? 'width:' . $aField['width'] : false // add default styles
  673. );
  674. $sDisabled = ($this->_isCheckboxDisabled($aRow) ? 'disabled="disabled"' : '');
  675. $sSelected = ($this->_isCheckboxSelected($mixedValue, $sKey, $aField, $aRow) ? 'checked="checked"' : '');
  676. $sVal = $aRow[$this->_aOptions['field_id']];
  677. return '<td ' . $sAttr . '><input type="checkbox" name="'. $this->_sObject . '_check" value="' . $sVal . '" ' . $sDisabled . ' ' . $sSelected . '/></td>';
  678. }
  679. protected function _getCellSwitcher ($mixedValue, $sKey, $aField, $aRow)
  680. {
  681. if($this->_bIsApi)
  682. return ['type' => 'switcher', 'data' => $aRow[$this->_aOptions['field_active']], 'fld' => $this->_aOptions['field_active']];
  683. $sAttr = $this->_convertAttrs(
  684. $aField, 'attr_cell',
  685. 'bx-def-padding-sec-bottom bx-def-padding-sec-top', // add default classes
  686. isset($aField['width']) ? 'width:' . $aField['width'] : false // add default styles
  687. );
  688. $oForm = new BxTemplFormView(array(), $this->_oTemplate);
  689. $oForm->addCssJs();
  690. $aInput = array(
  691. 'type' => 'switcher',
  692. 'name' => $this->_sObject . '_switch_' . $aRow[$this->_aOptions['field_id']],
  693. 'caption' => '',
  694. 'attrs' => array (
  695. 'bx_grid_action_single' => 'enable',
  696. 'bx_grid_action_confirm' => '',
  697. 'bx_grid_action_data' => $aRow[$this->_aOptions['field_id']],
  698. ),
  699. 'value' => $aRow[$this->_aOptions['field_id']],
  700. 'checked' => $this->_isSwitcherOn(isset($aRow[$this->_aOptions['field_active']]) ? $aRow[$this->_aOptions['field_active']] : false, $sKey, $aField, $aRow),
  701. );
  702. $sSwitcher = $oForm->genInput($aInput);
  703. return '<td ' . $sAttr . '>' . $sSwitcher . '</td>';
  704. }
  705. protected function _getCellOrder ($mixedValue, $sKey, $aField, $aRow)
  706. {
  707. if($this->_bIsApi)
  708. return ['type' => 'order', 'data'=> $mixedValue];
  709. $sAttr = $this->_convertAttrs(
  710. $aField, 'attr_cell',
  711. 'bx-def-padding-sec-bottom bx-def-padding-sec-top', // add default classes
  712. isset($aField['width']) ? 'width:' . $aField['width'] : false // add default styles
  713. );
  714. 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>';
  715. }
  716. protected function _getCellActions ($mixedValue, $sKey, $aField, $aRow)
  717. {
  718. $sAttr = $this->_convertAttrs(
  719. $aField, 'attr_cell',
  720. 'bx-def-padding-sec-bottom bx-def-padding-sec-top', // add default classes
  721. isset($aField['width']) ? 'width:' . $aField['width'] : false // add default styles
  722. );
  723. $mixedDisabledBehavior = $this->_getActionsDisabledBehavior($aRow);
  724. $mixedActions = $this->_getActions('single', $aRow[$this->_aOptions['field_id']], false, null === $mixedDisabledBehavior ? $this->_isRowDisabled($aRow) : $mixedDisabledBehavior, null !== $mixedDisabledBehavior, $aRow);
  725. if($this->_bIsApi)
  726. return ['type' => 'actions', 'data' => $mixedActions];
  727. return '<td ' . $sAttr . '><div class="bx-grid-cell-single-actions-wrapper bx-def-margin-thd-neg">' . $mixedActions . '</div></td>';
  728. }
  729. protected function _getCellDefault ($mixedValue, $sKey, $aField, $aRow)
  730. {
  731. if($this->_bIsApi) {
  732. $sType = $mixedValue['type'] ?? 'text';
  733. if(isset($mixedValue['type']))
  734. unset($mixedValue['type']);
  735. return [
  736. 'type' => $sType,
  737. 'value'=> (int)$aField['translatable'] ? _t($mixedValue) : $mixedValue
  738. ];
  739. }
  740. $sAttr = $this->_convertAttrs(
  741. $aField, 'attr_cell',
  742. 'bx-grid-cell' . (!empty($aField['name']) ? ' bx-gc-' . $aField['name'] : '') . ' bx-def-padding-sec-bottom bx-def-padding-sec-top', // add default classes
  743. isset($aField['width']) ? 'width:' . $aField['width'] : false // add default styles
  744. );
  745. return '<td ' . $sAttr . '>' . $mixedValue . '</td>';
  746. }
  747. /**
  748. * Display a cell as a masked secret with an eye icon to reveal it after password confirmation.
  749. * @see BxDolGrid grid_display_secret_cell
  750. */
  751. protected function _getCellSecret ($mixedValue, $sKey, $aField, $aRow)
  752. {
  753. $sRaw = '';
  754. if (isset($aRow[$sKey]) && $aRow[$sKey] !== '' && $aRow[$sKey] !== null)
  755. $sRaw = (string)$aRow[$sKey];
  756. else if ($mixedValue !== '' && $mixedValue !== null && $mixedValue !== false)
  757. $sRaw = (string)$mixedValue;
  758. $sMasked = $sRaw !== '' ? bx_gen_secret($sRaw) : '';
  759. if ($this->_bIsApi)
  760. return ['type' => 'secret', 'value' => $sMasked];
  761. $sAttr = $this->_convertAttrs(
  762. $aField, 'attr_cell',
  763. 'bx-grid-cell' . (!empty($aField['name']) ? ' bx-gc-' . $aField['name'] : '') . ' bx-def-padding-sec-bottom bx-def-padding-sec-top',
  764. isset($aField['width']) ? 'width:' . $aField['width'] : false
  765. );
  766. $sToggle = '';
  767. if ($sRaw !== '' && isLogged()) {
  768. $mixedId = $aRow[$this->_aOptions['field_id']];
  769. $sToggle = '<a href="javascript:void(0);" class="bx-grid-secret-toggle" title="' . bx_html_attribute(_t('_sys_grid_secret_reveal')) . '" bx_grid_secret_id="' . bx_html_attribute($mixedId) . '" bx_grid_secret_field="' . bx_html_attribute($sKey) . '"><i class="sys-icon eye"></i></a>';
  770. }
  771. return '<td ' . $sAttr . '><div class="bx-grid-secret"><span class="bx-grid-secret-value" data-masked="' . bx_html_attribute($sMasked) . '">' . bx_process_output($sMasked) . '</span>' . $sToggle . '</div></td>';
  772. }
  773. protected function _getSecretRow($mixedId)
  774. {
  775. $aRows = $this->_getDataUpdated([$mixedId]);
  776. if (!$aRows || !is_array($aRows))
  777. return false;
  778. $sFieldId = $this->_aOptions['field_id'];
  779. foreach ($aRows as $aRow) {
  780. if (isset($aRow[$sFieldId]) && (string)$aRow[$sFieldId] === (string)$mixedId)
  781. return $aRow;
  782. }
  783. return reset($aRows);
  784. }
  785. /**
  786. * Return the secret value for a revealed cell. Override when the value is encrypted or computed.
  787. */
  788. protected function _getSecretValue($mixedId, $sKey, $aRow)
  789. {
  790. return isset($aRow[$sKey]) ? $aRow[$sKey] : '';
  791. }
  792. protected function _isCurrentPasswordValid($sPassword)
  793. {
  794. if ($sPassword === false || $sPassword === '')
  795. return false;
  796. if (!class_exists('BxFormAccountCheckerHelper'))
  797. bx_import('BxBaseFormAccount');
  798. $oChecker = new BxFormAccountCheckerHelper();
  799. return true === $oChecker->checkPasswordCurrent($sPassword);
  800. }
  801. protected function _getActions ($sType, $sActionData = false, $isSmall = false, $isDisabled = false, $isPermanentState = false, $aRow = array())
  802. {
  803. $sActionsType = 'actions_' . $sType;
  804. if (empty($this->_aOptions[$sActionsType]) || !is_array($this->_aOptions[$sActionsType]))
  805. return '';
  806. $mixedResult = $this->_bIsApi ? [] : '';
  807. foreach ($this->_aOptions[$sActionsType] as $sKey => $a) {
  808. if(!$a['active'] || (!$a['title'] && !$a['icon']))
  809. continue;
  810. $sFunc = '_getAction' . $this->_genMethodName($sKey);
  811. if (!method_exists($this, $sFunc))
  812. $sFunc = empty($a) ? '_getActionDivider' : '_getActionDefault';
  813. if (!isset($a['attr']['bx_grid_action'])) {
  814. $a['attr']['bx_grid_action_' . $sType] = $sKey;
  815. if (!empty($sActionData))
  816. $a['attr']['bx_grid_action_data'] = $sActionData;
  817. if (isset($a['confirm']))
  818. $a['attr']['bx_grid_action_confirm'] = $a['confirm'] ? 1 : 0;
  819. $a['attr']['bx_grid_action_reset_paginate'] = false === strpos($sKey, 'delete') ? 0 : 1; // reset paginate after deleting row
  820. }
  821. if ($isPermanentState && !isset($a['attr']['bx_grid_permanent_state']))
  822. $a['attr']['bx_grid_permanent_state'] = 1;
  823. if ($this->_bIsApi)
  824. $mixedResult[] = $this->$sFunc($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  825. else
  826. $mixedResult .= $this->$sFunc($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  827. }
  828. return $mixedResult;
  829. }
  830. protected function _getActionsAPI ($sType)
  831. {
  832. $sActionsType = 'actions_' . $sType;
  833. if(empty($this->_aOptions[$sActionsType]) || !is_array($this->_aOptions[$sActionsType]))
  834. return [];
  835. foreach ($this->_aOptions[$sActionsType] as $sKey => $aAction) {
  836. $sMethod = '_getAction' . $this->_genMethodName($sKey);
  837. if(!method_exists($this, $sMethod))
  838. $sMethod = '_getActionDefault';
  839. if(!isset($aAction['attr']))
  840. $aAction['attr'] = [];
  841. if(($_aAction = $this->$sMethod($sType, $sKey, $aAction)))
  842. $this->_aOptions[$sActionsType][$sKey] = $_aAction;
  843. }
  844. return $this->_aOptions[$sActionsType];
  845. }
  846. protected function _getActionDelete($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  847. {
  848. if($this->_bIsApi)
  849. return array_merge($a, ['name' => $sKey, 'type' => 'callback', 'on_callback' => 'hide_row']);
  850. return $this->_getActionDefault($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  851. }
  852. protected function _getActionDefault ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  853. {
  854. if ($a['icon_only'] && empty($a['attr']['title']) && !empty($a['title']))
  855. $a['attr']['title'] = $a['title'];
  856. if ($a['icon_only'] && empty($a['attr']['aria-label']) && !empty($a['title']))
  857. $a['attr']['aria-label'] = $a['title'];
  858. if ($this->_bIsApi) {
  859. $sParams = '';
  860. if($sType == 'single' && ($sFieldId = $this->_aOptions['field_id']) && isset($aRow[$sFieldId]))
  861. $sParams = '&id=' . $aRow[$sFieldId];
  862. return array_merge($a, ['name' => $sKey, 'type' => 'modal', 'action' => $sKey, 'params' => $sParams]);
  863. }
  864. $sAttr = $this->_convertAttrs(
  865. $a, 'attr',
  866. 'bx-btn bx-def-margin-thd' . ($isSmall ? ' bx-btn-small' : '') . ($isDisabled ? ' bx-btn-disabled' : '') // add default classes
  867. );
  868. $sIcon = '';
  869. $sImage = '';
  870. if (!empty($a['icon'])) {
  871. list($sIconFont, $sIconUrl, $sIconA, $sIconHtml) = $this->_oFunctions->getIcon($a['icon']);
  872. if ($sIconFont)
  873. $sIcon = '<i class="sys-icon ' . $sIconFont . '"></i>';
  874. else if($sIconUrl)
  875. $sImage = '<img style="background-image:url(' . $sIconUrl . ');" src="' . $this->_oTemplate->getIconUrl('spacer.gif') .'" />';
  876. else if($sIconHtml)
  877. $sImage = $sIconHtml;
  878. }
  879. return '<button ' . $sAttr . '>' . $sIcon . $sImage . ($a['icon_only'] || empty($a['title']) ? '' : '<u>' . $a['title'] . '</u>') . '</button>';
  880. }
  881. protected function _getActionDivider ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  882. {
  883. return '<div class="bx-grid-actions-divider bx-def-margin-sec-left"> | </div>';
  884. }
  885. protected function _getActionResult($aResult)
  886. {
  887. return $this->_bIsApi ? $aResult : echoJson($aResult);
  888. }
  889. protected function _getFilterControls ()
  890. {
  891. $oForm = new BxTemplFormView([]);
  892. $aInput = array(
  893. 'type' => 'text',
  894. 'name' => 'keyword',
  895. 'attrs' => array(
  896. 'id' => 'bx-grid-search-' . $this->_sObject
  897. )
  898. );
  899. $this->_oTemplate->addCss('forms.css');
  900. return $oForm->genRow($aInput);
  901. }
  902. protected function _getFilterControlsAPI($aFilters = [])
  903. {
  904. if(!empty($this->_aOptions['filter_fields']) || !empty($this->_aOptions['filter_fields_translatable']))
  905. $aFilters['search'] = [];
  906. return $aFilters;
  907. }
  908. protected function _getFilterOnChange()
  909. {
  910. return '';
  911. }
  912. protected function _getFilterSelectOne($sFilterName, $sFilterValue, $aFilterValues, $mixedAddSelectOne = true, $bAsArray = false)
  913. {
  914. if(empty($sFilterName))
  915. return '';
  916. $aInputValues = [];
  917. if(($mixedAddSelectOne === true && ($sKey = '_Select_one')) || (is_string($mixedAddSelectOne) && ($sKey = $mixedAddSelectOne)))
  918. $aInputValues[''] = _t($sKey);
  919. foreach($aFilterValues as $mixedKey => $mixedValue)
  920. if(is_array($mixedValue) && isset($mixedValue['key'], $mixedValue['value']))
  921. $aInputValues[$mixedValue['key']] = _t($mixedValue['value']);
  922. else
  923. $aInputValues[$mixedKey] = _t($mixedValue);
  924. $aInputSelect = [
  925. 'type' => 'select',
  926. 'name' => $sFilterName,
  927. 'attrs' => [
  928. 'id' => 'bx-grid-' . $sFilterName . '-' . $this->_sObject,
  929. 'onChange' => ($sOnChange = $this->_getFilterOnChange()) ? 'javascript:' . $sOnChange : '',
  930. ],
  931. 'value' => $sFilterValue,
  932. 'values' => $aInputValues
  933. ];
  934. if($bAsArray)
  935. return $aInputSelect;
  936. $oForm = new BxTemplFormView([]);
  937. return $oForm->genRow($aInputSelect);
  938. }
  939. protected function _getFilterDatePicker($sFilterName, $sFilterValue, $bAsArray = false)
  940. {
  941. if(empty($sFilterName))
  942. return '';
  943. $aInputDatePicker = [
  944. 'type' => 'datepicker',
  945. 'name' => $sFilterName,
  946. 'attrs' => [
  947. 'id' => 'bx-grid-' . $sFilterName . '-' . $this->_sObject,
  948. 'onChange' => ($sOnChange = $this->_getFilterOnChange()) ? 'javascript:' . $sOnChange : '',
  949. ],
  950. 'value' => $sFilterValue,
  951. ];
  952. if($bAsArray)
  953. return $aInputDatePicker;
  954. $oForm = new BxTemplFormView([]);
  955. return $oForm->genRow($aInputDatePicker);
  956. }
  957. protected function _getFilterButton($bAsArray = false)
  958. {
  959. $sOnChange = $this->_getFilterOnChange();
  960. $aInputButton = [
  961. 'type' => 'button',
  962. 'name' => 'button',
  963. 'attrs' => [
  964. 'id' => 'bx-grid-button-' . $this->_sObject,
  965. 'onClick' => 'javascript:' . $sOnChange,
  966. ],
  967. 'value' => _t('_Search'),
  968. ];
  969. if($bAsArray)
  970. return $aInputButton;
  971. $oForm = new BxTemplFormView([]);
  972. return $oForm->genRow($aInputButton);
  973. }
  974. protected function _getFilterLabel($sFilterValue, $bAsArray = false)
  975. {
  976. $aInputLabel = [
  977. 'type' => 'value',
  978. 'value' => $sFilterValue,
  979. 'tr_attrs' => ['class' => 'bx-grid-controls-filter-label'],
  980. ];
  981. if($bAsArray)
  982. return $aInputLabel;
  983. $oForm = new BxTemplFormView([]);
  984. return $oForm->genRow($aInputLabel);
  985. }
  986. protected function _getSearchInput($bAsArray = false)
  987. {
  988. $sOnChange = $this->_getFilterOnChange();
  989. $aInputSearch = [
  990. 'type' => 'text',
  991. 'name' => 'search',
  992. 'attrs' => [
  993. 'id' => 'bx-grid-search-' . $this->_sObject,
  994. 'onKeyup' => 'javascript:$(this).off(\'keyup focusout\'); ' . $sOnChange,
  995. 'onBlur' => 'javascript:' . $sOnChange,
  996. ]
  997. ];
  998. if($bAsArray)
  999. return $aInputSearch;
  1000. $oForm = new BxTemplFormView([]);
  1001. return $oForm->genRow($aInputSearch);
  1002. }
  1003. protected function _getCounter()
  1004. {
  1005. return _t('_sys_grid_total_count', $this->_iTotalCount);
  1006. }
  1007. protected function _limitMaxLength ($mixedValue, $sKey, $aField, $aRow, $isDisplayPopupOnTextOverflow, $bReturnString = true)
  1008. {
  1009. if((int)$aField['chars_limit'] <= 0)
  1010. return $mixedValue;
  1011. return $this->_oFunctions->getStringWithLimitedLength([strip_tags($mixedValue), $mixedValue], $aField['chars_limit'], $isDisplayPopupOnTextOverflow, $bReturnString);
  1012. }
  1013. protected function _convertAttrs ($aField, $sAttrName, $sClasses = false, $sStyles = false)
  1014. {
  1015. if (!empty($aField['hidden_on'])) {
  1016. $aHiddenOn = array(
  1017. pow(2, BX_DB_HIDDEN_PHONE - 1) => 'bx-def-media-phone-hide',
  1018. pow(2, BX_DB_HIDDEN_TABLET - 1) => 'bx-def-media-tablet-hide',
  1019. pow(2, BX_DB_HIDDEN_DESKTOP - 1) => 'bx-def-media-desktop-hide',
  1020. pow(2, BX_DB_HIDDEN_MOBILE - 1) => 'bx-def-mobile-app-hide'
  1021. );
  1022. foreach ($aHiddenOn as $iHiddenOn => $sClass)
  1023. if ((int)$aField['hidden_on'] & $iHiddenOn)
  1024. $sClasses .= ' ' . $sClass;
  1025. }
  1026. return bx_convert_array2attrs(
  1027. isset($aField[$sAttrName]) && is_array($aField[$sAttrName]) ? $aField[$sAttrName] : array(),
  1028. $sClasses,
  1029. $sStyles
  1030. );
  1031. }
  1032. protected function _updateOrder($mixedId, $iOrder)
  1033. {
  1034. $oDb = BxDolDb::getInstance();
  1035. $sTable = $this->_aOptions['table'];
  1036. $sFieldId = $this->_aOptions['field_id'];
  1037. $sFieldOrder = $this->_aOptions['field_order'];
  1038. $sQuery = $oDb->prepare("UPDATE `{$sTable}` SET `{$sFieldOrder}` = ? WHERE `{$sFieldId}` = ?", $iOrder, $mixedId);
  1039. return $oDb->query($sQuery);
  1040. }
  1041. protected function _delete ($mixedId)
  1042. {
  1043. $oDb = BxDolDb::getInstance();
  1044. $sTable = $this->_aOptions['table'];
  1045. $sFieldId = $this->_aOptions['field_id'];
  1046. $sQuery = $oDb->prepare("DELETE FROM `{$sTable}` WHERE `{$sFieldId}` = ?", $mixedId);
  1047. return $oDb->query($sQuery);
  1048. }
  1049. protected function _enable ($mixedId, $isChecked)
  1050. {
  1051. $oDb = BxDolDb::getInstance();
  1052. $sTable = $this->_aOptions['table'];
  1053. $sFieldId = $this->_aOptions['field_id'];
  1054. $sFieldActive = $this->_aOptions['field_active'];
  1055. $sQuery = $oDb->prepare("UPDATE `{$sTable}` SET `$sFieldActive` = ? WHERE `{$sFieldId}` = ?", $this->_switcherChecked2State($isChecked), $mixedId);
  1056. return $oDb->query($sQuery);
  1057. }
  1058. protected function _addJsCss()
  1059. {
  1060. if ($this->_aOptions['field_order']) {
  1061. $this->_oTemplate->addJs(array(
  1062. 'jquery-ui/jquery-ui.custom.min.js',
  1063. 'URI.min.js',
  1064. ));
  1065. }
  1066. $this->_oTemplate->addJs('BxDolGrid.js');
  1067. $this->_oTemplate->addCss('grid.css');
  1068. $this->_oTemplate->addJsTranslation([
  1069. '_sys_grid_confirmation',
  1070. '_sys_grid_secret_reveal',
  1071. '_sys_grid_secret_hide',
  1072. '_sys_grid_secret_password_prompt',
  1073. ]);
  1074. }
  1075. }
  1076. /** @} */