BxInvGridRequests.php 12 KB

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