BxInvGridRequests.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 Invites Invites
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. require_once('BxInvGrid.php');
  12. define('BX_INV_FILTER_STATUS_NEW', 0);
  13. define('BX_INV_FILTER_STATUS_INVITED', 1);
  14. define('BX_INV_FILTER_STATUS_SEEN', 2);
  15. define('BX_INV_FILTER_STATUS_JOINED', 3);
  16. class BxInvGridRequests extends BxInvGrid
  17. {
  18. public function __construct ($aOptions, $oTemplate = false)
  19. {
  20. parent::__construct ($aOptions, $oTemplate);
  21. $this->_aQueryReset = array('order_field', 'order_dir', $this->_aOptions['paginate_get_start'], $this->_aOptions['paginate_get_per_page']);
  22. $this->_sFilter1Name = 'filter1';
  23. $this->_aFilter1Values = array(
  24. '' => _t('_bx_invites_request_status_for_filter_all'),
  25. BX_INV_FILTER_STATUS_NEW => _t('_bx_invites_request_status_for_filter_new'),
  26. BX_INV_FILTER_STATUS_INVITED => _t('_bx_invites_request_status_for_filter_invited'),
  27. BX_INV_FILTER_STATUS_SEEN => _t('_bx_invites_request_status_for_filter_seen'),
  28. BX_INV_FILTER_STATUS_JOINED => _t('_bx_invites_request_status_for_filter_joined')
  29. );
  30. $sFilter1 = bx_get($this->_sFilter1Name);
  31. if(!empty($sFilter1)) {
  32. $this->_sFilter1Value = bx_process_input($sFilter1);
  33. $this->_aQueryAppend['filter1'] = $this->_sFilter1Value;
  34. }
  35. }
  36. public function performActionInfo()
  37. {
  38. $iId = $this->_getId();
  39. if(!$iId)
  40. return $this->_getActionResult([]);
  41. $aRequest = $this->_oModule->_oDb->getRequests(['type' => 'by_id', 'value' => $iId]);
  42. if(empty($aRequest) || !is_array($aRequest))
  43. return $this->_getActionResult([]);
  44. $sContent = $this->_oModule->_oTemplate->getBlockRequestText($aRequest);
  45. $aResult = [];
  46. if($this->_bIsApi)
  47. $aResult = [bx_api_get_block('simple_list', $sContent)];
  48. else
  49. $aResult = ['popup' => [
  50. 'html' => BxTemplFunctions::getInstance()->transBox('bx-invites-info-popup', $sContent)
  51. ]];
  52. return $this->_getActionResult($aResult);
  53. }
  54. public function performActionInviteInfo()
  55. {
  56. $iId = $this->_getId();
  57. if(!$iId)
  58. return $this->_getActionResult([]);
  59. $aRequest = $this->_oModule->_oDb->getInvites(['type' => 'all', 'value' => $iId]);
  60. if(empty($aRequest) || !is_array($aRequest))
  61. return $this->_getActionResult([]);
  62. $sContent = $this->_oModule->_oTemplate->getBlockInviteInfo($aRequest);
  63. $aResult = [];
  64. if($this->_bIsApi)
  65. $aResult = [bx_api_get_block('simple_list', $sContent)];
  66. else
  67. $aResult = ['popup' => [
  68. 'html' => BxTemplFunctions::getInstance()->transBox('bx-invites-info-popup', $sContent)
  69. ]];
  70. return $this->_getActionResult($aResult);
  71. }
  72. public function performActionInvite($aParams = [])
  73. {
  74. $iProfileId = $this->_oModule->getProfileId();
  75. if(($mixedAllowed = $this->_oModule->isAllowedInvite($iProfileId)) !== true)
  76. return echoJson(['msg' => $mixedAllowed]);
  77. $aIds = $this->_getIds();
  78. if(!$aIds)
  79. return echoJson([]);
  80. $sText = _t('_bx_invites_msg_invitation');
  81. $iAffected = 0;
  82. $aIdsAffected = [];
  83. foreach($aIds as $iId) {
  84. $aRequest = $this->_oModule->_oDb->getRequests(['type' => 'by_id', 'value' => $iId]);
  85. if(empty($aRequest) || !is_array($aRequest))
  86. continue;
  87. $mixedResult = $this->_oModule->invite(BX_INV_TYPE_FROM_SYSTEM, [$aRequest['email']], $sText);
  88. if(empty($mixedResult) || !is_array($mixedResult))
  89. continue;
  90. $aInviteIds = array_keys($mixedResult);
  91. $this->_oModule->_oDb->attachInviteToRequest($iId, (int)array_shift($aInviteIds));
  92. $aIdsAffected[] = $iId;
  93. $iAffected++;
  94. }
  95. $sResult = $iAffected ? _t('_bx_invites_msg_invitation_sent', $iAffected) : _t('_bx_invites_err_invite');
  96. $aResult = [];
  97. if($this->_bIsApi)
  98. $aResult = [bx_api_get_block('simple_list', [['text' => $sResult]])];
  99. else
  100. $aResult = $iAffected ? ['grid' => $this->getCode(false), 'blink' => $aIdsAffected, 'msg' => $sResult] : ['msg' => $sResult];
  101. return $this->_getActionResult($aResult);
  102. }
  103. public function performActionDelete($aParams = [])
  104. {
  105. $iProfileId = $this->_oModule->getProfileId();
  106. if(($mixedAllowed = $this->_oModule->isAllowedDeleteRequest($iProfileId)) !== true)
  107. return echoJson(['msg' => $mixedAllowed]);
  108. $aIds = $this->_getIds();
  109. if(!$aIds)
  110. return echoJson([]);
  111. $oForm = BxDolForm::getObjectInstance($this->_oModule->_oConfig->getObject('form_request'), $this->_oModule->_oConfig->getObject('form_display_request_send'));
  112. $iAffected = 0;
  113. $aIdsAffected = [];
  114. foreach($aIds as $iId) {
  115. if(!$oForm->delete($iId))
  116. continue;
  117. $this->_oModule->isAllowedDeleteRequest($iProfileId, true);
  118. $aIdsAffected[] = $iId;
  119. $iAffected++;
  120. }
  121. $aResult = [];
  122. if($iAffected)
  123. $aResult = !$this->_bIsApi ? ['grid' => $this->getCode(false), 'blink' => $aIdsAffected] : [];
  124. else
  125. $aResult = ['msg' => _t('_bx_invites_err_delete_request')];
  126. return $this->_getActionResult($aResult);
  127. }
  128. protected function _getCellNip($mixedValue, $sKey, $aField, $aRow)
  129. {
  130. return parent::_getCellDefault(long2ip($mixedValue), $sKey, $aField, $aRow);
  131. }
  132. protected function _getCellDate($mixedValue, $sKey, $aField, $aRow)
  133. {
  134. if($this->_bIsApi)
  135. return ['type' => 'time', 'data' => $mixedValue];
  136. return parent::_getCellDefault(bx_time_js($mixedValue), $sKey, $aField, $aRow);
  137. }
  138. protected function _getCellStatus($mixedValue, $sKey, $aField, $aRow)
  139. {
  140. $sStatus = _t('_bx_invites_request_status_new');
  141. if(in_array($mixedValue, [1, 2, 3])) {
  142. switch ($mixedValue) {
  143. case 1:
  144. case 2:
  145. $iInvites = $this->_oModule->_oDb->getInvites(['type' => 'count_by_request', 'value' => $aRow['id']]);
  146. $sStatus = _t($mixedValue == 1 ? '_bx_invites_request_status_invited' : '_bx_invites_request_status_seen') . ' (' . $iInvites . ')';
  147. break;
  148. case 3:
  149. $sStatus = _t('_bx_invites_request_status_joined');
  150. break;
  151. }
  152. if(!$this->_bIsApi)
  153. $sStatus = $this->_oTemplate->parseLink('javascript:void(0)', $sStatus, [
  154. 'title' => bx_html_attribute(_t('_bx_invites_grid_action_title_adm_invite_info')),
  155. 'bx_grid_action_single' => 'invite_info',
  156. 'bx_grid_action_data' => $aRow['id']
  157. ]);
  158. }
  159. return parent::_getCellDefault($sStatus, $sKey, $aField, $aRow);
  160. }
  161. protected function _getCellJoinedAccount($mixedValue, $sKey, $aField, $aRow)
  162. {
  163. $sAccountInfo = "";
  164. if(isset($aRow["status"]) && $aRow["status"] == 3) {
  165. $iAccountId = $this->_oModule->_oDb->getInvites(['type' => 'account_by_request', 'value' => $aRow['id']]);
  166. if($this->_bIsApi) {
  167. $iProfileId = 0;
  168. if(($mixedProfileId = BxDolProfileQuery::getInstance()->getCurrentProfileByAccount($iAccountId, true)) !== false)
  169. $iProfileId = (int)$mixedProfileId;
  170. return ['type' => 'profile', 'data' => BxDolProfile::getData($iProfileId)];
  171. }
  172. $sAccountInfo = $this->_oModule->_oTemplate->getProfilesByAccount($iAccountId);
  173. }
  174. return parent::_getCellDefault($sAccountInfo, $sKey, $aField, $aRow);
  175. }
  176. protected function _getActionInvite($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  177. {
  178. if(isset($aRow["status"]) && $aRow["status"] == 3)
  179. return '';
  180. return parent::_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  181. }
  182. protected function _getActionInviteInfo ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  183. {
  184. return '';
  185. }
  186. protected function _getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage)
  187. {
  188. $CNF = $this->_oModule->_oConfig->CNF;
  189. $sTableRequests = $CNF['TABLE_REQUESTS'];
  190. if(strpos($sFilter, $this->_sParamsDivider) !== false)
  191. list($this->_sFilter1Value, $sFilter) = explode($this->_sParamsDivider, $sFilter);
  192. $sFilterSql = "";
  193. if(isset($this->_sFilter1Value) && $this->_sFilter1Value != ''){
  194. if ($this->_sFilter1Value == BX_INV_FILTER_STATUS_INVITED){
  195. $sFilterSql = " AND " . $sTableRequests . ".`status` IN (" . BX_INV_FILTER_STATUS_INVITED . ', ' . BX_INV_FILTER_STATUS_SEEN . ', ' . BX_INV_FILTER_STATUS_JOINED . ')';
  196. }
  197. else{
  198. $sFilterSql = " AND " . $sTableRequests . ".`status` = " . $this->_sFilter1Value;
  199. }
  200. }
  201. $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString($sFilterSql);
  202. return parent::_getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);
  203. }
  204. protected function _getDataSqlOrderClause ($sOrderByFilter, $sOrderField, $sOrderDir, $bFieldsOnly = false)
  205. {
  206. return " ORDER BY `status` ASC, `date` DESC";
  207. }
  208. }
  209. /** @} */