BxInvGridRequests.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. define('BX_INV_FILTER_STATUS_NEW', 0);
  12. define('BX_INV_FILTER_STATUS_INVITED', 1);
  13. define('BX_INV_FILTER_STATUS_SEEN', 2);
  14. define('BX_INV_FILTER_STATUS_JOINED', 3);
  15. class BxInvGridRequests extends BxTemplGrid
  16. {
  17. protected $_sModule;
  18. protected $_oModule;
  19. protected $_sFilter1Name;
  20. protected $_sFilter1Value;
  21. protected $_aFilter1Values;
  22. protected $_sParamsDivider;
  23. public function __construct ($aOptions, $oTemplate = false)
  24. {
  25. parent::__construct ($aOptions, $oTemplate);
  26. $this->_sModule = 'bx_invites';
  27. $this->_oModule = BxDolModule::getInstance($this->_sModule);
  28. $this->_sFilter1Name = 'filter1';
  29. $this->_aFilter1Values = array(
  30. '' => _t('_bx_invites_request_status_for_filter_all'),
  31. BX_INV_FILTER_STATUS_NEW => _t('_bx_invites_request_status_for_filter_new'),
  32. BX_INV_FILTER_STATUS_INVITED => _t('_bx_invites_request_status_for_filter_invited'),
  33. BX_INV_FILTER_STATUS_SEEN => _t('_bx_invites_request_status_for_filter_seen'),
  34. BX_INV_FILTER_STATUS_JOINED => _t('_bx_invites_request_status_for_filter_joined')
  35. );
  36. $sFilter1 = bx_get($this->_sFilter1Name);
  37. if(!empty($sFilter1)) {
  38. $this->_sFilter1Value = bx_process_input($sFilter1);
  39. $this->_aQueryAppend['filter1'] = $this->_sFilter1Value;
  40. }
  41. $this->_sParamsDivider = '#-#';
  42. }
  43. protected function _getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage)
  44. {
  45. $CNF = $this->_oModule->_oConfig->CNF;
  46. $sTableRequests = $CNF['TABLE_REQUESTS'];
  47. if(strpos($sFilter, $this->_sParamsDivider) !== false)
  48. list($this->_sFilter1Value, $sFilter) = explode($this->_sParamsDivider, $sFilter);
  49. $sFilterSql = "";
  50. if(isset($this->_sFilter1Value) && $this->_sFilter1Value != ''){
  51. if ($this->_sFilter1Value == BX_INV_FILTER_STATUS_INVITED){
  52. $sFilterSql = " AND " . $sTableRequests . ".`status` IN (" . BX_INV_FILTER_STATUS_INVITED . ', ' . BX_INV_FILTER_STATUS_SEEN . ', ' . BX_INV_FILTER_STATUS_JOINED . ')';
  53. }
  54. else{
  55. $sFilterSql = " AND " . $sTableRequests . ".`status` = " . $this->_sFilter1Value;
  56. }
  57. }
  58. $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString($sFilterSql);
  59. return parent::_getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);
  60. }
  61. public function performActionAdd()
  62. {
  63. $sAction = 'add';
  64. $oForm = $this->_oModule->getFormObjectInvite();
  65. if (!$oForm)
  66. return '';
  67. $oForm->aInputs['text']['value'] = _t('_bx_invites_msg_invitation');
  68. $oForm->aFormAttrs['action'] = BX_DOL_URL_ROOT . 'grid.php?' . bx_encode_url_params($_GET, array('ids', '_r'));
  69. $oForm->initChecker();
  70. if($oForm->isSubmittedAndValid()) {
  71. $sResult = $this->_oModule->processFormObjectInvite($oForm);
  72. $aRes = array('msg' => $sResult);
  73. echoJson($aRes);
  74. }
  75. else {
  76. $sContent = BxTemplStudioFunctions::getInstance()->popupBox('_bx_invites_form_invite', _t('_bx_invites_form_invite'), $this->_oModule->_oTemplate->parseHtmlByName('popup_invite.html', array(
  77. 'form_id' => $oForm->id,
  78. 'form' => $oForm->getCode(true),
  79. 'object' => $this->_sObject,
  80. 'action' => $sAction
  81. )));
  82. echoJson(array('popup' => array('html' => $sContent, 'options' => array('closeOnOuterClick' => true))));
  83. }
  84. }
  85. public function performActionInfo()
  86. {
  87. $aIds = bx_get('ids');
  88. if(!$aIds || !is_array($aIds)) {
  89. echoJson(array());
  90. exit;
  91. }
  92. $aRequest = $this->_oModule->_oDb->getRequests(array('type' => 'by_id', 'value' => (int)array_shift($aIds)));
  93. if(empty($aRequest) || !is_array($aRequest)){
  94. echoJson(array());
  95. exit;
  96. }
  97. $sContent = BxTemplFunctions::getInstance()->transBox('bx-invites-info-popup', $this->_oModule->_oTemplate->getBlockRequestText($aRequest));
  98. echoJson(array('popup' => array('html' => $sContent)));
  99. }
  100. public function performActionInviteInfo()
  101. {
  102. $aIds = bx_get('ids');
  103. if(!$aIds || !is_array($aIds)) {
  104. echoJson(array());
  105. exit;
  106. }
  107. $aRequest = $this->_oModule->_oDb->getInvites(array('type' => 'all', 'value' => (int)array_shift($aIds)));
  108. if(empty($aRequest) || !is_array($aRequest)){
  109. echoJson(array());
  110. exit;
  111. }
  112. $sContent = BxTemplFunctions::getInstance()->transBox('bx-invites-info-popup', $this->_oModule->_oTemplate->getBlockInviteInfo($aRequest));
  113. echoJson(array('popup' => array('html' => $sContent)));
  114. }
  115. public function performActionInvite($aParams = array())
  116. {
  117. $iProfileId = $this->_oModule->getProfileId();
  118. $mixedAllowed = $this->_oModule->isAllowedInvite($iProfileId);
  119. if($mixedAllowed !== true) {
  120. echoJson(array('msg' => $mixedAllowed));
  121. exit;
  122. }
  123. $iAffected = 0;
  124. $aIds = bx_get('ids');
  125. if(!$aIds || !is_array($aIds)) {
  126. echoJson(array());
  127. exit;
  128. }
  129. $sText = _t('_bx_invites_msg_invitation');
  130. $aIdsAffected = array ();
  131. foreach($aIds as $iId) {
  132. $aRequest = $this->_oModule->_oDb->getRequests(array('type' => 'by_id', 'value' => $iId));
  133. if(empty($aRequest) || !is_array($aRequest))
  134. continue;
  135. $iInviteId = -1;
  136. $mixedResult = $this->_oModule->invite(BX_INV_TYPE_FROM_SYSTEM, $aRequest['email'], $sText);
  137. if($mixedResult === false)
  138. continue;
  139. else
  140. $iInviteId = (int)$mixedResult[0];
  141. $this->_oModule->isAllowedInvite($iProfileId, true);
  142. $this->_oModule->_oDb->attachInviteToRequest($iId, $iInviteId);
  143. $aIdsAffected[] = $iId;
  144. $iAffected++;
  145. }
  146. echoJson($iAffected ? array('grid' => $this->getCode(false), 'blink' => $aIdsAffected, 'msg' => _t('_bx_invites_msg_invitation_sent', $iAffected)) : array('msg' => _t('_bx_invites_err_invite')));
  147. }
  148. public function performActionDelete($aParams = array())
  149. {
  150. $iProfileId = $this->_oModule->getProfileId();
  151. $mixedAllowed = $this->_oModule->isAllowedDeleteRequest($iProfileId);
  152. if($mixedAllowed !== true) {
  153. echoJson(array('msg' => $mixedAllowed));
  154. exit;
  155. }
  156. $iAffected = 0;
  157. $aIds = bx_get('ids');
  158. if(!$aIds || !is_array($aIds)) {
  159. echoJson(array());
  160. exit;
  161. }
  162. $oForm = BxDolForm::getObjectInstance($this->_oModule->_oConfig->getObject('form_request'), $this->_oModule->_oConfig->getObject('form_display_request_send'));
  163. $aIdsAffected = array ();
  164. foreach($aIds as $iId) {
  165. if(!$oForm->delete($iId))
  166. continue;
  167. $this->_oModule->isAllowedDeleteRequest($iProfileId, true);
  168. $aIdsAffected[] = $iId;
  169. $iAffected++;
  170. }
  171. echoJson($iAffected ? array('grid' => $this->getCode(false), 'blink' => $aIdsAffected) : array('msg' => _t('_bx_invites_err_delete_request')));
  172. }
  173. protected function _getFilterControls ()
  174. {
  175. parent::_getFilterControls();
  176. return $this->_getFilterSelectOne($this->_sFilter1Name, $this->_sFilter1Value, $this->_aFilter1Values) . $this->_getSearchInput();
  177. }
  178. protected function _getSearchInput()
  179. {
  180. $sJsObject = $this->_oModule->_oConfig->getJsObject('main');
  181. $aInputSearch = array(
  182. 'type' => 'text',
  183. 'name' => 'search',
  184. 'attrs' => array(
  185. 'id' => 'bx-grid-search-' . $this->_sObject,
  186. 'onKeyup' => 'javascript:$(this).off(\'keyup focusout\'); ' . $sJsObject . '.onChangeFilter(this)',
  187. 'onBlur' => 'javascript:' . $sJsObject . '.onChangeFilter(this)',
  188. )
  189. );
  190. $oForm = new BxTemplFormView(array());
  191. return $oForm->genRow($aInputSearch);
  192. }
  193. protected function _getFilterSelectOne($sFilterName, $sFilterValue, $aFilterValues)
  194. {
  195. if(empty($sFilterName) || empty($aFilterValues))
  196. return '';
  197. $CNF = &$this->_oModule->_oConfig->CNF;
  198. $sJsObject = $this->_oModule->_oConfig->getJsObject('main');
  199. foreach($aFilterValues as $sKey => $sValue)
  200. $aFilterValues[$sKey] = _t($sValue);
  201. $aInputModules = array(
  202. 'type' => 'select',
  203. 'name' => $sFilterName,
  204. 'attrs' => array(
  205. 'id' => 'bx-grid-' . $sFilterName . '-' . $this->_sObject,
  206. 'onChange' => 'javascript:' . $sJsObject . '.onChangeFilter(this)'
  207. ),
  208. 'value' => $sFilterValue,
  209. 'values' => $aFilterValues
  210. );
  211. $oForm = new BxTemplFormView(array());
  212. return $oForm->genRow($aInputModules);
  213. }
  214. protected function _getCellNip($mixedValue, $sKey, $aField, $aRow)
  215. {
  216. return parent::_getCellDefault(long2ip($mixedValue), $sKey, $aField, $aRow);
  217. }
  218. protected function _getCellDate($mixedValue, $sKey, $aField, $aRow)
  219. {
  220. return parent::_getCellDefault(bx_time_js($mixedValue), $sKey, $aField, $aRow);
  221. }
  222. protected function _getCellStatus($mixedValue, $sKey, $aField, $aRow)
  223. {
  224. $sStatus = _t('_bx_invites_request_status_new');
  225. switch ($mixedValue) {
  226. case 1:
  227. case 2:
  228. $sLinkHtml = $this->_oTemplate->parseLink('javascript:void(0)', $this->_oModule->_oDb->getInvites(array('type' => 'count_by_request', 'value' => $aRow['id'])), array(
  229. 'title' => _t('_bx_invites_grid_action_title_adm_invite_info'),
  230. 'bx_grid_action_single' => 'invite_info',
  231. 'bx_grid_action_data' => $aRow['id']
  232. ));
  233. $sStatus = ($mixedValue == 1 ? _t('_bx_invites_request_status_invited') : _t('_bx_invites_request_status_seen')) . ' (' . $sLinkHtml . ')';
  234. break;
  235. case 3:
  236. $sStatus = _t('_bx_invites_request_status_joined') . ' (' . bx_time_js($this->_oModule->_oDb->getInvites(array('type' => 'date_joined_by_request', 'value' => $aRow['id']))) . ')';
  237. break;
  238. }
  239. return parent::_getCellDefault($sStatus, $sKey, $aField, $aRow);
  240. }
  241. protected function _getCellJoinedAccount($mixedValue, $sKey, $aField, $aRow)
  242. {
  243. $sAccountInfo = "";
  244. if(isset($aRow["status"]) && $aRow["status"] == 3){
  245. $sAccountInfo = $this->_oModule->_oTemplate->getProfilesByAccount($this->_oModule->_oDb->getInvites(array('type' => 'account_by_request', 'value' => $aRow['id'])));
  246. }
  247. return parent::_getCellDefault($sAccountInfo, $sKey, $aField, $aRow);
  248. }
  249. protected function _getActionInvite($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  250. {
  251. if(isset($aRow["status"]) && $aRow["status"] == 3)
  252. return '';
  253. return parent::_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  254. }
  255. protected function _getActionInviteInfo ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  256. {
  257. return '';
  258. }
  259. }
  260. /** @} */