BxEventsGridSessionsManage.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 Events Events
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxEventsGridSessionsManage extends BxTemplGrid
  12. {
  13. protected $_sModule;
  14. protected $_oModule;
  15. protected $_sParamsDivider = '#-#';
  16. protected $_iEventProfileId;
  17. protected $_iEventContentId;
  18. protected $_aEventContentInfo;
  19. public function __construct ($aOptions, $oTemplate = false)
  20. {
  21. $this->_sModule = 'bx_events';
  22. $this->_oModule = BxDolModule::getInstance($this->_sModule);
  23. parent::__construct ($aOptions, $oTemplate);
  24. if($this->_bIsApi)
  25. $this->_aOptions['field_order'] = 'date_start';
  26. $this->_iEventProfileId = 0;
  27. $this->_iEventContentId = 0;
  28. $this->_aEventContentInfo = [];
  29. if(($iEventProfileId = bx_get('profile_id')) !== false)
  30. $this->setProfileId($iEventProfileId);
  31. }
  32. public function getFormBlockTitleAPI($sAction, $iId = 0)
  33. {
  34. $CNF = &$this->_oModule->_oConfig->CNF;
  35. $sResult = '';
  36. switch($sAction) {
  37. case 'add':
  38. $sResult = _t('_bx_events_popup_title_sn_add');
  39. break;
  40. case 'edit':
  41. $sResult = _t('_bx_events_popup_title_sn_edit');
  42. break;
  43. }
  44. return $sResult;
  45. }
  46. public function getFormCallBackUrlAPI($sAction, $iId = 0)
  47. {
  48. return '/api.php?r=system/perfom_action_api/TemplServiceGrid/&params[]=&o=' . $this->_sObject . '&profile_id=' . $this->_iEventProfileId . '&a=' . $sAction . '&id=' . $iId;
  49. }
  50. public function setProfileId($iProfileId)
  51. {
  52. $this->_iEventProfileId = (int)$iProfileId;
  53. $this->_aQueryAppend['profile_id'] = $this->_iEventProfileId;
  54. if(($oEventProfile = BxDolProfile::getInstance($this->_iEventProfileId)) !== false) {
  55. $this->_iEventContentId = (int)$oEventProfile->getContentId();
  56. $this->_aEventContentInfo = $this->_oModule->_oDb->getContentInfoById($this->_iEventContentId);
  57. }
  58. }
  59. public function performActionAdd()
  60. {
  61. $CNF = &$this->_oModule->_oConfig->CNF;
  62. $sAction = 'add';
  63. if(($mixedResult = $this->_oModule->checkAllowedEdit($this->_aEventContentInfo)) !== CHECK_ACTION_RESULT_ALLOWED)
  64. return bx_is_api() ? [bx_api_get_msg($mixedResult)] : echoJson(['msg' => $mixedResult]);
  65. $sForm = $CNF['OBJECT_FORM_SESSION_DISPLAY_ADD'];
  66. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_SESSION'], $CNF['OBJECT_FORM_SESSION_DISPLAY_ADD']);
  67. $oForm->setId($sForm);
  68. $oForm->setName($sForm);
  69. $oForm->setAction(BX_DOL_URL_ROOT . bx_append_url_params('grid.php', ['o' => $this->_sObject, 'a' => $sAction, 'profile_id' => $this->_iEventProfileId]));
  70. $oForm->initChecker();
  71. if($oForm->isSubmittedAndValid()) {
  72. $iId = (int)$oForm->insert(['event_id' => $this->_iEventContentId, 'added' => time(), 'order' => $this->_oModule->_oDb->getSessionOrderMax($this->_iEventContentId) + 1]);
  73. if($iId != 0)
  74. $aResult = ['grid' => $this->getCode(false), 'blink' => $iId];
  75. else
  76. $aResult = ['msg' => _t($CNF['T']['err_cannot_perform'])];
  77. return bx_is_api() ? [] : echoJson($aResult);
  78. }
  79. if (bx_is_api())
  80. return $this->getFormBlockAPI($oForm, $sAction);
  81. bx_import('BxTemplFunctions');
  82. $sContent = BxTemplFunctions::getInstance()->popupBox($this->_oModule->_oConfig->getHtmlIds('popup_session'), _t('_bx_events_popup_title_sn_add'), $this->_oModule->_oTemplate->parseHtmlByName('popup_session.html', [
  83. 'form_id' => $oForm->getId(),
  84. 'form' => $oForm->getCode(true),
  85. 'object' => $this->_sObject,
  86. 'action' => $sAction
  87. ]));
  88. echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
  89. }
  90. public function performActionEdit()
  91. {
  92. $CNF = &$this->_oModule->_oConfig->CNF;
  93. $sAction = 'edit';
  94. if(($mixedResult = $this->_oModule->checkAllowedEdit($this->_aEventContentInfo)) !== CHECK_ACTION_RESULT_ALLOWED)
  95. return bx_is_api() ? [bx_api_get_msg($mixedResult)] : echoJson(['msg' => $mixedResult]);
  96. $aIds = $this->_getIds();
  97. if($aIds === false)
  98. return echoJson([]);
  99. $iItem = array_shift($aIds);
  100. $aItem = $this->_oModule->_oDb->getSessions(['sample' => 'id', 'id' => $iItem]);
  101. if(!is_array($aItem) || empty($aItem))
  102. return echoJson([]);
  103. $sForm = $CNF['OBJECT_FORM_SESSION_DISPLAY_EDIT'];
  104. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_SESSION'], $CNF['OBJECT_FORM_SESSION_DISPLAY_EDIT']);
  105. $oForm->setId($sForm);
  106. $oForm->setName($sForm);
  107. $oForm->setAction(BX_DOL_URL_ROOT . bx_append_url_params('grid.php', ['o' => $this->_sObject, 'a' => $sAction, 'profile_id' => $this->_iEventProfileId, 'id' => $iItem]));
  108. $oForm->initChecker($aItem);
  109. if($oForm->isSubmittedAndValid()) {
  110. if($oForm->update($aItem['id']) !== false)
  111. $aResult = ['grid' => $this->getCode(false), 'blink' => $aItem['id']];
  112. else
  113. $aResult = ['msg' => _t($CNF['T']['err_cannot_perform'])];
  114. return bx_is_api() ? [] : echoJson($aResult);
  115. }
  116. if (bx_is_api())
  117. return $this->getFormBlockAPI($oForm, $sAction, $iItem);
  118. bx_import('BxTemplFunctions');
  119. $sContent = BxTemplFunctions::getInstance()->popupBox($this->_oModule->_oConfig->getHtmlIds('popup_session'), _t('_bx_events_popup_title_sn_edit'), $this->_oModule->_oTemplate->parseHtmlByName('popup_session.html', [
  120. 'form_id' => $oForm->getId(),
  121. 'form' => $oForm->getCode(true),
  122. 'object' => $this->_sObject,
  123. 'action' => $sAction
  124. ]));
  125. return echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
  126. }
  127. protected function _getCellDateStart($mixedValue, $sKey, $aField, $aRow)
  128. {
  129. return $this->_getCellDate($mixedValue, $sKey, $aField, $aRow);
  130. }
  131. protected function _getCellDateEnd($mixedValue, $sKey, $aField, $aRow)
  132. {
  133. return $this->_getCellDate($mixedValue, $sKey, $aField, $aRow);
  134. }
  135. protected function _getCellDate($mixedValue, $sKey, $aField, $aRow)
  136. {
  137. if(bx_is_api()){
  138. return ['type' => 'datetime', 'data'=> $mixedValue];
  139. }
  140. return parent::_getCellDefault(bx_time_js($mixedValue, BX_FORMAT_DATE_TIME, true), $sKey, $aField, $aRow);
  141. }
  142. protected function _getCellData($sKey, $aField, $aRow)
  143. {
  144. if($sKey == 'description')
  145. $aRow[$sKey] = strip_tags($aRow[$sKey]);
  146. return parent::_getCellData($sKey, $aField, $aRow);
  147. }
  148. protected function _getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage)
  149. {
  150. $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString("AND `event_id`=? ", $this->_iEventContentId);
  151. return parent::_getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);
  152. }
  153. protected function _isVisibleGrid ($a)
  154. {
  155. return $this->_oModule->checkAllowedEdit($this->_aEventContentInfo) == CHECK_ACTION_RESULT_ALLOWED;
  156. }
  157. protected function _getIds()
  158. {
  159. $aIds = bx_get('ids');
  160. if(!$aIds || !is_array($aIds)) {
  161. $iId = (int)bx_get('id');
  162. if(!$iId)
  163. return false;
  164. $aIds = [$iId];
  165. }
  166. return $aIds;
  167. }
  168. }
  169. /** @} */