BxResourcesGridCommon.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 Resources Resources
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxResourcesGridCommon extends BxBaseModTextGridCommon
  12. {
  13. protected $_aCategories;
  14. protected $_iProfileId;
  15. public function __construct ($aOptions, $oTemplate = false)
  16. {
  17. $this->MODULE = 'bx_resources';
  18. parent::__construct ($aOptions, $oTemplate);
  19. $CNF = &$this->_oModule->_oConfig->CNF;
  20. $this->_aCategories = BxDolFormQuery::getDataItems($CNF['OBJECT_CATEGORY']);
  21. if(($iProfileId = bx_get('profile_id')) !== false)
  22. $this->setProfileId($iProfileId);
  23. }
  24. public function getFormBlockTitleAPI($sAction, $iId = 0)
  25. {
  26. $sResult = '';
  27. switch($sAction) {
  28. case 'add':
  29. $sResult = _t('_bx_resources_grid_popup_title_adm_add');
  30. break;
  31. case 'edit':
  32. $sResult = _t('_bx_resources_grid_popup_title_adm_edit');
  33. break;
  34. }
  35. return $sResult;
  36. }
  37. public function getFormCallBackUrlAPI($sAction, $iId = 0)
  38. {
  39. return '/api.php?r=system/perfom_action_api/TemplServiceGrid/&params[]=&o=' . $this->_sObject . '&profile_id=' . $this->_iProfileId . '&a=' . $sAction . '&id=' . $iId;
  40. }
  41. public function performActionAdd()
  42. {
  43. $sAction = 'add';
  44. if(!$this->_oModule->isAllowAdd($this->_iProfileId))
  45. return $this->_bIsApi ? [] : echoJson([]);
  46. $CNF = &$this->_oModule->_oConfig->CNF;
  47. $oForm = $this->_getFormObject($sAction);
  48. $oForm->initChecker();
  49. if($oForm->isSubmittedAndValid()) {
  50. $aResult = [];
  51. $iId = $oForm->insert([$CNF['FIELD_ALLOW_VIEW_TO'] => -$this->_iProfileId, $CNF['FIELD_LIST'] => $this->_oModule->getListDefault($this->_iProfileId)]);
  52. if($iId) {
  53. $this->_oModule->onPublished($iId);
  54. $aResult = ['grid' => $this->getCode(false), 'blink' => $iId];
  55. }
  56. else
  57. $aResult = ['msg' => _t('_bx_resources_txt_err_cannot_perform_action')];
  58. return $this->_bIsApi ? [] : echoJson($aResult);
  59. }
  60. if($this->_bIsApi)
  61. return $this->getFormBlockAPI($oForm, $sAction);
  62. $sContent = BxTemplFunctions::getInstance()->popupBox($this->_oModule->_oConfig->getHtmlIds('entry_popup_' . $sAction), _t('_bx_resources_grid_popup_title_adm_add'), $this->_oModule->_oTemplate->parseHtmlByName('popup_form.html', [
  63. 'form_id' => $oForm->aFormAttrs['id'],
  64. 'form' => $oForm->getCode(true),
  65. 'object' => $this->_sObject,
  66. 'action' => $sAction
  67. ]));
  68. return echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false, 'removeOnClose' => true]]]);
  69. }
  70. public function performActionEdit()
  71. {
  72. $sAction = 'edit';
  73. $aEntry = [];
  74. if(($aIds = $this->_getIds()) !== false) {
  75. $aEntry = $this->_oModule->_oDb->getContentInfoById(array_shift($aIds));
  76. if(empty($aEntry) || !is_array($aEntry) || !$this->_oModule->isAllowManage($aEntry))
  77. return $this->_bIsApi ? [] : echoJson([]);
  78. }
  79. else
  80. return $this->_bIsApi ? [] : echoJson([]);
  81. $CNF = &$this->_oModule->_oConfig->CNF;
  82. $iEntryId = (int)$aEntry[$CNF['FIELD_ID']];
  83. $oForm = $this->_getFormObject($sAction, $aEntry);
  84. $oForm->initChecker($aEntry);
  85. if($oForm->isSubmittedAndValid()) {
  86. $aResult = [];
  87. if($oForm->update($iEntryId) !== false)
  88. $aResult = ['grid' => $this->getCode(false), 'blink' => $iEntryId];
  89. else
  90. $aResult = ['msg' => _t('_bx_resources_txt_err_cannot_perform_action')];
  91. return $this->_bIsApi ? [] : echoJson($aResult);
  92. }
  93. if($this->_bIsApi)
  94. return $this->getFormBlockAPI($oForm, $sAction, $iEntryId);
  95. $sContent = BxTemplFunctions::getInstance()->popupBox($this->_oModule->_oConfig->getHtmlIds('entry_popup_' . $sAction), _t('_bx_resources_grid_popup_title_adm_' . $sAction), $this->_oModule->_oTemplate->parseHtmlByName('popup_form.html', [
  96. 'form_id' => $oForm->aFormAttrs['id'],
  97. 'form' => $oForm->getCode(true),
  98. 'object' => $this->_sObject,
  99. 'action' => $sAction
  100. ]));
  101. return echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false, 'removeOnClose' => true]]]);
  102. }
  103. public function setProfileId($iProfileId)
  104. {
  105. $this->_iProfileId = (int)$iProfileId;
  106. $this->_aQueryAppend['profile_id'] = $this->_iProfileId;
  107. }
  108. protected function _getCellCat($mixedValue, $sKey, $aField, $aRow)
  109. {
  110. $CNF = &$this->_oModule->_oConfig->CNF;
  111. return parent::_getCellDefault($this->_aCategories[$aRow[$CNF['FIELD_CATEGORY']]] ?? '', $sKey, $aField, $aRow);
  112. }
  113. protected function _getActionAdd($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = [])
  114. {
  115. if(!$this->_oModule->isAllowAdd($this->_iProfileId))
  116. return $this->_bIsApi ? [] : '';
  117. return $this->_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  118. }
  119. protected function _getActionEdit($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = [])
  120. {
  121. if(!$this->_oModule->isAllowManage($aRow['id']))
  122. return $this->_bIsApi ? [] : '';
  123. return $this->_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  124. }
  125. protected function _getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage)
  126. {
  127. if(!$this->_iProfileId)
  128. return [];
  129. $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString(" AND ABS(`allow_view_to`)=?", $this->_iProfileId);
  130. return $this->__getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);
  131. }
  132. protected function _getFormObject($sAction, $aEntry = [])
  133. {
  134. $CNF = &$this->_oModule->_oConfig->CNF;
  135. $sForm = $CNF['OBJECT_FORM_ENTRY_DISPLAY_' . strtoupper($sAction)];
  136. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $sForm);
  137. $oForm->setId($sForm);
  138. $aActionParams = ['o' => $this->_sObject, 'a' => $sAction, 'profile_id' => $this->_iProfileId];
  139. if(!empty($aEntry['id']))
  140. $aActionParams['id'] = $aEntry['id'];
  141. $oForm->setAction(BX_DOL_URL_ROOT . bx_append_url_params('grid.php', $aActionParams));
  142. return $oForm;
  143. }
  144. protected function _getIds()
  145. {
  146. $aIds = bx_get('ids');
  147. if(!$aIds || !is_array($aIds)) {
  148. $iId = (int)bx_get('id');
  149. if(!$iId)
  150. return false;
  151. $aIds = [$iId];
  152. }
  153. return $aIds;
  154. }
  155. }
  156. /** @} */