BxBaseModGroupsGridPricesManage.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 BaseGroups Base classes for groups modules
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. require_once('BxBaseModGroupsGridPrices.php');
  12. class BxBaseModGroupsGridPricesManage extends BxBaseModGroupsGridPrices
  13. {
  14. protected $_iRoleId;
  15. public function __construct ($aOptions, $oTemplate = false)
  16. {
  17. parent::__construct ($aOptions, $oTemplate);
  18. $CNF = &$this->_oModule->_oConfig->CNF;
  19. $this->_iRoleId = 0;
  20. if(($iRoleId = (int)bx_get('role_id')) !== false)
  21. $this->setRoleId($iRoleId);
  22. }
  23. public function getFormBlockTitleAPI($sAction, $iId = 0)
  24. {
  25. $CNF = &$this->_oModule->_oConfig->CNF;
  26. $sResult = '';
  27. switch($sAction) {
  28. case 'add':
  29. $sResult = _t($CNF['T']['popup_title_price_add']);
  30. break;
  31. case 'edit':
  32. $sResult = _t($CNF['T']['popup_title_price_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 . '&a=' . $sAction . '&profile_id=' . $this->_iGroupProfileId . '&role_id=' . $this->_iRoleId . '&id=' . $iId;
  40. }
  41. public function setRoleId($iRoleId)
  42. {
  43. $this->_iRoleId = (int)$iRoleId;
  44. $this->_aQueryAppend['role_id'] = $this->_iRoleId;
  45. }
  46. public function getCode($isDisplayHeader = true)
  47. {
  48. $sResult = parent::getCode($isDisplayHeader);
  49. if(empty($sResult))
  50. return $sResult;
  51. $sJsCode = '';
  52. if($isDisplayHeader) {
  53. $this->_oModule->_oTemplate->addJs([
  54. 'modules/base/groups/js/|prices.js',
  55. 'prices.js'
  56. ]);
  57. $sJsCode = $this->_oModule->_oTemplate->getJsCode('prices', array(
  58. 'sObjNameGrid' => $this->_sObject,
  59. 'aHtmlIds' => $this->_oModule->_oConfig->getHtmlIds()
  60. ));
  61. }
  62. return $sJsCode . $sResult;
  63. }
  64. public function performActionAdd()
  65. {
  66. $CNF = &$this->_oModule->_oConfig->CNF;
  67. $sAction = 'add';
  68. if(($mixedResult = $this->_oModule->checkAllowedEdit($this->_aGroupContentInfo)) !== CHECK_ACTION_RESULT_ALLOWED)
  69. return $this->_bIsApi ? [bx_api_get_msg($mixedResult)] : echoJson(['msg' => $mixedResult]);
  70. $sFilter = bx_get('filter');
  71. if(strpos($sFilter, $this->_sParamsDivider) !== false)
  72. list($this->_iRoleId, $sFilter) = explode($this->_sParamsDivider, $sFilter);
  73. if(!is_numeric($this->_iRoleId) && ($iRoleId = bx_get('role_id')) !== false)
  74. $this->_iRoleId = (int)$iRoleId;
  75. $sForm = $CNF['OBJECT_FORM_PRICE_DISPLAY_ADD'];
  76. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_PRICE'], $CNF['OBJECT_FORM_PRICE_DISPLAY_ADD']);
  77. $oForm->setId($sForm);
  78. $oForm->setName($sForm);
  79. $oForm->setAction(BX_DOL_URL_ROOT . bx_append_url_params('grid.php', array('o' => $this->_sObject, 'a' => $sAction, 'profile_id' => $this->_iGroupProfileId, 'role_id' => $this->_iRoleId)));
  80. $oForm->setRoleId($this->_iRoleId);
  81. $oForm->initChecker();
  82. if($oForm->isSubmittedAndValid()) {
  83. $iPeriod = $oForm->getCleanValue('period');
  84. $sPeriodUnit = $oForm->getCleanValue('period_unit');
  85. if(!empty($iPeriod) && empty($sPeriodUnit))
  86. return ($sMsg = _t($CNF['T']['err_period_unit'])) && $this->_bIsApi ? [bx_api_get_msg($sMsg)] : echoJson(['msg' => $sMsg]);
  87. $aPrice = $this->_oModule->_oDb->getPrices(['type' => 'by_prpp', 'profile_id' => $this->_iGroupProfileId, 'role_id' => $this->_iRoleId, 'period' => $iPeriod, 'period_unit' => $sPeriodUnit]);
  88. if(!empty($aPrice) && is_array($aPrice))
  89. return ($sMsg = _t($CNF['T']['err_price_duplicate'])) && $this->_bIsApi ? [bx_api_get_msg($sMsg)] : echoJson(['msg' => $sMsg]);
  90. $iId = (int)$oForm->insert(['profile_id' => $this->_iGroupProfileId, 'added' => time(), 'order' => $this->_oModule->_oDb->getPriceOrderMax($this->_iRoleId) + 1]);
  91. if($iId != 0)
  92. $aRes = ['grid' => $this->getCode(false), 'blink' => $iId];
  93. else
  94. $aRes = ['msg' => _t($CNF['T']['err_cannot_perform'])];
  95. return $this->_bIsApi ? [] : echoJson($aRes);
  96. }
  97. if($this->_bIsApi)
  98. return $this->getFormBlockAPI($oForm, $sAction);
  99. bx_import('BxTemplFunctions');
  100. $sContent = BxTemplFunctions::getInstance()->popupBox($this->_oModule->_oConfig->getHtmlIds('popup_price'), _t($CNF['T']['popup_title_price_add']), $this->_oModule->_oTemplate->parseHtmlByName('popup_price.html', [
  101. 'form_id' => $oForm->getId(),
  102. 'form' => $oForm->getCode(true),
  103. 'object' => $this->_sObject,
  104. 'action' => $sAction
  105. ]));
  106. echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
  107. }
  108. public function performActionEdit()
  109. {
  110. $CNF = &$this->_oModule->_oConfig->CNF;
  111. $sAction = 'edit';
  112. if(($mixedResult = $this->_oModule->checkAllowedEdit($this->_aGroupContentInfo)) !== CHECK_ACTION_RESULT_ALLOWED)
  113. return $this->_bIsApi ? [bx_api_get_msg($mixedResult)] : echoJson(['msg' => $mixedResult]);
  114. $aIds = $this->_getIds();
  115. if($aIds === false)
  116. return $this->_bIsApi ? [] : echoJson([]);
  117. $aItem = $this->_oModule->_oDb->getPrices(['type' => 'by_id', 'value' => array_shift($aIds)]);
  118. if(!is_array($aItem) || empty($aItem))
  119. return $this->_bIsApi ? [] : echoJson([]);
  120. $sForm = $CNF['OBJECT_FORM_PRICE_DISPLAY_EDIT'];
  121. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_PRICE'], $CNF['OBJECT_FORM_PRICE_DISPLAY_EDIT']);
  122. $oForm->setId($sForm);
  123. $oForm->setName($sForm);
  124. $oForm->setAction(BX_DOL_URL_ROOT . bx_append_url_params('grid.php', ['o' => $this->_sObject, 'a' => $sAction, 'profile_id' => $this->_iGroupProfileId, 'role_id' => $this->_iRoleId]));
  125. $oForm->initChecker($aItem);
  126. if($oForm->isSubmittedAndValid()) {
  127. if($oForm->update($aItem['id']) !== false)
  128. $aRes = ['grid' => $this->getCode(false), 'blink' => $aItem['id']];
  129. else
  130. $aRes = ['msg' => _t($CNF['T']['err_cannot_perform'])];
  131. return $this->_bIsApi ? [] : echoJson($aRes);
  132. }
  133. if($this->_bIsApi)
  134. return $this->getFormBlockAPI($oForm, $sAction, $iItem);
  135. bx_import('BxTemplFunctions');
  136. $sContent = BxTemplFunctions::getInstance()->popupBox($this->_oModule->_oConfig->getHtmlIds('popup_price'), _t($CNF['T']['popup_title_price_edit']), $this->_oModule->_oTemplate->parseHtmlByName('popup_price.html', [
  137. 'form_id' => $oForm->getId(),
  138. 'form' => $oForm->getCode(true),
  139. 'object' => $this->_sObject,
  140. 'action' => $sAction
  141. ]));
  142. return echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
  143. }
  144. public function performActionDelete()
  145. {
  146. $CNF = &$this->_oModule->_oConfig->CNF;
  147. $aIds = bx_get('ids');
  148. if(!$aIds || !is_array($aIds))
  149. return echoJson(array());
  150. $iAffected = 0;
  151. $aIdsAffected = array();
  152. foreach($aIds as $iId)
  153. if($this->_oModule->_oDb->deletePrices(array('id' => $iId))) {
  154. $aIdsAffected[] = $iId;
  155. $iAffected++;
  156. }
  157. return echoJson($iAffected ? array('grid' => $this->getCode(false), 'blink' => $aIdsAffected) : array('msg' => _t($CNF['T']['err_cannot_perform'])));
  158. }
  159. protected function _addJsCss()
  160. {
  161. parent::_addJsCss();
  162. $this->_oModule->_oTemplate->addJs([
  163. 'jquery.form.min.js',
  164. 'modules/base/groups/js/|prices.js',
  165. 'prices.js'
  166. ]);
  167. $this->_oModule->_oTemplate->addCss([
  168. 'prices.css'
  169. ]);
  170. }
  171. protected function _getFilterControls()
  172. {
  173. parent::_getFilterControls();
  174. $sContent = '';
  175. $oForm = new BxTemplFormView(array());
  176. $aInputRoles = array(
  177. 'type' => 'select',
  178. 'name' => 'role',
  179. 'attrs' => array(
  180. 'id' => 'bx-grid-level-' . $this->_sObject,
  181. 'onChange' => 'javascript:' . $this->_oModule->_oConfig->getJsObject('prices') . '.onChangeRole()'
  182. ),
  183. 'value' => $this->_iRoleId,
  184. 'values' => $this->_aRoles
  185. );
  186. $sContent .= $oForm->genRow($aInputRoles);
  187. $aInputSearch = array(
  188. 'type' => 'text',
  189. 'name' => 'keyword',
  190. 'attrs' => array(
  191. 'id' => 'bx-grid-search-' . $this->_sObject,
  192. ),
  193. );
  194. $sContent .= $oForm->genRow($aInputSearch);
  195. return $sContent;
  196. }
  197. protected function _getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage)
  198. {
  199. if(strpos($sFilter, $this->_sParamsDivider) !== false) {
  200. list($iRoleId, $sFilter) = explode($this->_sParamsDivider, $sFilter);
  201. if(!is_numeric($iRoleId))
  202. $iRoleId = 0;
  203. $this->setRoleId($iRoleId);
  204. }
  205. $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString("AND `profile_id`=? AND `role_id`=? ", $this->_iGroupProfileId, $this->_iRoleId);
  206. return parent::_getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);;
  207. }
  208. protected function _isVisibleGrid ($a)
  209. {
  210. return $this->_oModule->checkAllowedManageAdmins($this->_aGroupContentInfo) == CHECK_ACTION_RESULT_ALLOWED;
  211. }
  212. }
  213. /** @} */