BxBaseGrid.php 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295
  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 ($this->_bIsApi) {
  857. $sParams = '';
  858. if($sType == 'single' && ($sFieldId = $this->_aOptions['field_id']) && isset($aRow[$sFieldId]))
  859. $sParams = '&id=' . $aRow[$sFieldId];
  860. return array_merge($a, ['name' => $sKey, 'type' => 'modal', 'action' => $sKey, 'params' => $sParams]);
  861. }
  862. $sAttr = $this->_convertAttrs(
  863. $a, 'attr',
  864. 'bx-btn bx-def-margin-thd' . ($isSmall ? ' bx-btn-small' : '') . ($isDisabled ? ' bx-btn-disabled' : '') // add default classes
  865. );
  866. $sIcon = '';
  867. $sImage = '';
  868. if (!empty($a['icon'])) {
  869. list($sIconFont, $sIconUrl, $sIconA, $sIconHtml) = $this->_oFunctions->getIcon($a['icon']);
  870. if ($sIconFont)
  871. $sIcon = '<i class="sys-icon ' . $sIconFont . '"></i>';
  872. else if($sIconUrl)
  873. $sImage = '<img style="background-image:url(' . $sIconUrl . ');" src="' . $this->_oTemplate->getIconUrl('spacer.gif') .'" />';
  874. else if($sIconHtml)
  875. $sImage = $sIconHtml;
  876. }
  877. return '<button ' . $sAttr . '>' . $sIcon . $sImage . ($a['icon_only'] || empty($a['title']) ? '' : '<u>' . $a['title'] . '</u>') . '</button>';
  878. }
  879. protected function _getActionDivider ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  880. {
  881. return '<div class="bx-grid-actions-divider bx-def-margin-sec-left"> | </div>';
  882. }
  883. protected function _getActionResult($aResult)
  884. {
  885. return $this->_bIsApi ? $aResult : echoJson($aResult);
  886. }
  887. protected function _getFilterControls ()
  888. {
  889. $oForm = new BxTemplFormView([]);
  890. $aInput = array(
  891. 'type' => 'text',
  892. 'name' => 'keyword',
  893. 'attrs' => array(
  894. 'id' => 'bx-grid-search-' . $this->_sObject
  895. )
  896. );
  897. $this->_oTemplate->addCss('forms.css');
  898. return $oForm->genRow($aInput);
  899. }
  900. protected function _getFilterControlsAPI($aFilters = [])
  901. {
  902. if(!empty($this->_aOptions['filter_fields']) || !empty($this->_aOptions['filter_fields_translatable']))
  903. $aFilters['search'] = [];
  904. return $aFilters;
  905. }
  906. protected function _getFilterOnChange()
  907. {
  908. return '';
  909. }
  910. protected function _getFilterSelectOne($sFilterName, $sFilterValue, $aFilterValues, $mixedAddSelectOne = true, $bAsArray = false)
  911. {
  912. if(empty($sFilterName))
  913. return '';
  914. $aInputValues = [];
  915. if(($mixedAddSelectOne === true && ($sKey = '_Select_one')) || (is_string($mixedAddSelectOne) && ($sKey = $mixedAddSelectOne)))
  916. $aInputValues[''] = _t($sKey);
  917. foreach($aFilterValues as $mixedKey => $mixedValue)
  918. if(is_array($mixedValue) && isset($mixedValue['key'], $mixedValue['value']))
  919. $aInputValues[$mixedValue['key']] = _t($mixedValue['value']);
  920. else
  921. $aInputValues[$mixedKey] = _t($mixedValue);
  922. $aInputSelect = [
  923. 'type' => 'select',
  924. 'name' => $sFilterName,
  925. 'attrs' => [
  926. 'id' => 'bx-grid-' . $sFilterName . '-' . $this->_sObject,
  927. 'onChange' => ($sOnChange = $this->_getFilterOnChange()) ? 'javascript:' . $sOnChange : '',
  928. ],
  929. 'value' => $sFilterValue,
  930. 'values' => $aInputValues
  931. ];
  932. if($bAsArray)
  933. return $aInputSelect;
  934. $oForm = new BxTemplFormView([]);
  935. return $oForm->genRow($aInputSelect);
  936. }
  937. protected function _getFilterDatePicker($sFilterName, $sFilterValue, $bAsArray = false)
  938. {
  939. if(empty($sFilterName))
  940. return '';
  941. $aInputDatePicker = [
  942. 'type' => 'datepicker',
  943. 'name' => $sFilterName,
  944. 'attrs' => [
  945. 'id' => 'bx-grid-' . $sFilterName . '-' . $this->_sObject,
  946. 'onChange' => ($sOnChange = $this->_getFilterOnChange()) ? 'javascript:' . $sOnChange : '',
  947. ],
  948. 'value' => $sFilterValue,
  949. ];
  950. if($bAsArray)
  951. return $aInputDatePicker;
  952. $oForm = new BxTemplFormView([]);
  953. return $oForm->genRow($aInputDatePicker);
  954. }
  955. protected function _getFilterButton($bAsArray = false)
  956. {
  957. $sOnChange = $this->_getFilterOnChange();
  958. $aInputButton = [
  959. 'type' => 'button',
  960. 'name' => 'button',
  961. 'attrs' => [
  962. 'id' => 'bx-grid-button-' . $this->_sObject,
  963. 'onClick' => 'javascript:' . $sOnChange,
  964. ],
  965. 'value' => _t('_Search'),
  966. ];
  967. if($bAsArray)
  968. return $aInputButton;
  969. $oForm = new BxTemplFormView([]);
  970. return $oForm->genRow($aInputButton);
  971. }
  972. protected function _getFilterLabel($sFilterValue, $bAsArray = false)
  973. {
  974. $aInputLabel = [
  975. 'type' => 'value',
  976. 'value' => $sFilterValue,
  977. 'tr_attrs' => ['class' => 'bx-grid-controls-filter-label'],
  978. ];
  979. if($bAsArray)
  980. return $aInputLabel;
  981. $oForm = new BxTemplFormView([]);
  982. return $oForm->genRow($aInputLabel);
  983. }
  984. protected function _getSearchInput($bAsArray = false)
  985. {
  986. $sOnChange = $this->_getFilterOnChange();
  987. $aInputSearch = [
  988. 'type' => 'text',
  989. 'name' => 'search',
  990. 'attrs' => [
  991. 'id' => 'bx-grid-search-' . $this->_sObject,
  992. 'onKeyup' => 'javascript:$(this).off(\'keyup focusout\'); ' . $sOnChange,
  993. 'onBlur' => 'javascript:' . $sOnChange,
  994. ]
  995. ];
  996. if($bAsArray)
  997. return $aInputSearch;
  998. $oForm = new BxTemplFormView([]);
  999. return $oForm->genRow($aInputSearch);
  1000. }
  1001. protected function _getCounter()
  1002. {
  1003. return _t('_sys_grid_total_count', $this->_iTotalCount);
  1004. }
  1005. protected function _limitMaxLength ($mixedValue, $sKey, $aField, $aRow, $isDisplayPopupOnTextOverflow, $bReturnString = true)
  1006. {
  1007. if((int)$aField['chars_limit'] <= 0)
  1008. return $mixedValue;
  1009. return $this->_oFunctions->getStringWithLimitedLength([strip_tags($mixedValue), $mixedValue], $aField['chars_limit'], $isDisplayPopupOnTextOverflow, $bReturnString);
  1010. }
  1011. protected function _convertAttrs ($aField, $sAttrName, $sClasses = false, $sStyles = false)
  1012. {
  1013. if (!empty($aField['hidden_on'])) {
  1014. $aHiddenOn = array(
  1015. pow(2, BX_DB_HIDDEN_PHONE - 1) => 'bx-def-media-phone-hide',
  1016. pow(2, BX_DB_HIDDEN_TABLET - 1) => 'bx-def-media-tablet-hide',
  1017. pow(2, BX_DB_HIDDEN_DESKTOP - 1) => 'bx-def-media-desktop-hide',
  1018. pow(2, BX_DB_HIDDEN_MOBILE - 1) => 'bx-def-mobile-app-hide'
  1019. );
  1020. foreach ($aHiddenOn as $iHiddenOn => $sClass)
  1021. if ((int)$aField['hidden_on'] & $iHiddenOn)
  1022. $sClasses .= ' ' . $sClass;
  1023. }
  1024. return bx_convert_array2attrs(
  1025. isset($aField[$sAttrName]) && is_array($aField[$sAttrName]) ? $aField[$sAttrName] : array(),
  1026. $sClasses,
  1027. $sStyles
  1028. );
  1029. }
  1030. protected function _updateOrder($mixedId, $iOrder)
  1031. {
  1032. $oDb = BxDolDb::getInstance();
  1033. $sTable = $this->_aOptions['table'];
  1034. $sFieldId = $this->_aOptions['field_id'];
  1035. $sFieldOrder = $this->_aOptions['field_order'];
  1036. $sQuery = $oDb->prepare("UPDATE `{$sTable}` SET `{$sFieldOrder}` = ? WHERE `{$sFieldId}` = ?", $iOrder, $mixedId);
  1037. return $oDb->query($sQuery);
  1038. }
  1039. protected function _delete ($mixedId)
  1040. {
  1041. $oDb = BxDolDb::getInstance();
  1042. $sTable = $this->_aOptions['table'];
  1043. $sFieldId = $this->_aOptions['field_id'];
  1044. $sQuery = $oDb->prepare("DELETE FROM `{$sTable}` WHERE `{$sFieldId}` = ?", $mixedId);
  1045. return $oDb->query($sQuery);
  1046. }
  1047. protected function _enable ($mixedId, $isChecked)
  1048. {
  1049. $oDb = BxDolDb::getInstance();
  1050. $sTable = $this->_aOptions['table'];
  1051. $sFieldId = $this->_aOptions['field_id'];
  1052. $sFieldActive = $this->_aOptions['field_active'];
  1053. $sQuery = $oDb->prepare("UPDATE `{$sTable}` SET `$sFieldActive` = ? WHERE `{$sFieldId}` = ?", $this->_switcherChecked2State($isChecked), $mixedId);
  1054. return $oDb->query($sQuery);
  1055. }
  1056. protected function _addJsCss()
  1057. {
  1058. if ($this->_aOptions['field_order']) {
  1059. $this->_oTemplate->addJs(array(
  1060. 'jquery-ui/jquery-ui.custom.min.js',
  1061. 'URI.min.js',
  1062. ));
  1063. }
  1064. $this->_oTemplate->addJs('BxDolGrid.js');
  1065. $this->_oTemplate->addCss('grid.css');
  1066. $this->_oTemplate->addJsTranslation([
  1067. '_sys_grid_confirmation',
  1068. '_sys_grid_secret_reveal',
  1069. '_sys_grid_secret_hide',
  1070. '_sys_grid_secret_password_prompt',
  1071. ]);
  1072. }
  1073. }
  1074. /** @} */