BxInvGridRequests.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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', 'new');
  12. define('BX_INV_FILTER_STATUS_INVITED', 'invited');
  13. define('BX_INV_FILTER_STATUS_SEEN', 'seen');
  14. define('BX_INV_FILTER_STATUS_JOINED', 'joined');
  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. $sTableInvites = $CNF['TABLE_INVITES'];
  48. if(strpos($sFilter, $this->_sParamsDivider) !== false)
  49. list($this->_sFilter1Value, $sFilter) = explode($this->_sParamsDivider, $sFilter);
  50. $sFilterSql = "";
  51. if(!empty($this->_sFilter1Value)){
  52. switch ($this->_sFilter1Value) {
  53. case BX_INV_FILTER_STATUS_NEW:
  54. $sFilterSql = " AND " . $sTableRequests . ".invite_id IS NULL ";
  55. break;
  56. case BX_INV_FILTER_STATUS_INVITED:
  57. $sFilterSql = " AND " . $sTableRequests . ".invite_id IS NOT NULL AND " . $sTableInvites . ".date_seen IS NULL AND " . $sTableInvites . ".joined_account_id IS NULL ";
  58. break;
  59. case BX_INV_FILTER_STATUS_SEEN:
  60. $sFilterSql = " AND " . $sTableInvites . ".date_seen IS NOT NULL AND " . $sTableInvites . ".joined_account_id IS NULL";
  61. break;
  62. case BX_INV_FILTER_STATUS_JOINED:
  63. $sFilterSql = " AND " . $sTableInvites . ".joined_account_id IS NOT NULL ";
  64. break;
  65. }
  66. }
  67. $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString($sFilterSql);
  68. return parent::_getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);
  69. }
  70. public function performActionAdd()
  71. {
  72. $sAction = 'add';
  73. $oForm = $this->_oModule->getFormObjectInvite();
  74. if (!$oForm)
  75. return '';
  76. $oForm->aInputs['text']['value'] = _t('_bx_invites_msg_invitation');
  77. $oForm->aFormAttrs['action'] = BX_DOL_URL_ROOT . 'grid.php?' . bx_encode_url_params($_GET, array('ids', '_r'));
  78. $oForm->initChecker();
  79. if($oForm->isSubmittedAndValid()) {
  80. $sResult = $this->_oModule->processFormObjectInvite($oForm);
  81. $aRes = array('msg' => $sResult);
  82. echoJson($aRes);
  83. }
  84. else {
  85. $sContent = BxTemplStudioFunctions::getInstance()->popupBox('_bx_invites_form_invite', _t('_bx_invites_form_invite'), $this->_oModule->_oTemplate->parseHtmlByName('popup_invite.html', array(
  86. 'form_id' => $oForm->id,
  87. 'form' => $oForm->getCode(true),
  88. 'object' => $this->_sObject,
  89. 'action' => $sAction
  90. )));
  91. echoJson(array('popup' => array('html' => $sContent, 'options' => array('closeOnOuterClick' => true))));
  92. }
  93. }
  94. public function performActionInfo()
  95. {
  96. $aIds = bx_get('ids');
  97. if(!$aIds || !is_array($aIds)) {
  98. echoJson(array());
  99. exit;
  100. }
  101. $aRequest = $this->_oModule->_oDb->getRequests(array('type' => 'by_id', 'value' => (int)array_shift($aIds)));
  102. if(empty($aRequest) || !is_array($aRequest)){
  103. echoJson(array());
  104. exit;
  105. }
  106. $sContent = BxTemplFunctions::getInstance()->transBox('bx-invites-info-popup', $this->_oModule->_oTemplate->getBlockRequestText($aRequest));
  107. echoJson(array('popup' => array('html' => $sContent)));
  108. }
  109. public function performActionInvite($aParams = array())
  110. {
  111. $iProfileId = $this->_oModule->getProfileId();
  112. $mixedAllowed = $this->_oModule->isAllowedInvite($iProfileId);
  113. if($mixedAllowed !== true) {
  114. echoJson(array('msg' => $mixedAllowed));
  115. exit;
  116. }
  117. $iAffected = 0;
  118. $aIds = bx_get('ids');
  119. if(!$aIds || !is_array($aIds)) {
  120. echoJson(array());
  121. exit;
  122. }
  123. $sText = _t('_bx_invites_msg_invitation');
  124. $aIdsAffected = array ();
  125. foreach($aIds as $iId) {
  126. $aRequest = $this->_oModule->_oDb->getRequests(array('type' => 'by_id', 'value' => $iId));
  127. if(empty($aRequest) || !is_array($aRequest))
  128. continue;
  129. $iInviteId = -1;
  130. $mixedResult = $this->_oModule->invite(BX_INV_TYPE_FROM_SYSTEM, $aRequest['email'], $sText);
  131. if($mixedResult === false)
  132. continue;
  133. else
  134. $iInviteId = (int)$mixedResult[0];
  135. $this->_oModule->isAllowedInvite($iProfileId, true);
  136. $this->_oModule->_oDb->attachInviteToRequest($iId, $iInviteId);
  137. $aIdsAffected[] = $iId;
  138. $iAffected++;
  139. }
  140. echoJson($iAffected ? array('grid' => $this->getCode(false), 'blink' => $aIdsAffected, 'msg' => _t('_bx_invites_msg_invitation_sent', $iAffected)) : array('msg' => _t('_bx_invites_err_invite')));
  141. }
  142. public function performActionDelete($aParams = array())
  143. {
  144. $iProfileId = $this->_oModule->getProfileId();
  145. $mixedAllowed = $this->_oModule->isAllowedDeleteRequest($iProfileId);
  146. if($mixedAllowed !== true) {
  147. echoJson(array('msg' => $mixedAllowed));
  148. exit;
  149. }
  150. $iAffected = 0;
  151. $aIds = bx_get('ids');
  152. if(!$aIds || !is_array($aIds)) {
  153. echoJson(array());
  154. exit;
  155. }
  156. $oForm = BxDolForm::getObjectInstance($this->_oModule->_oConfig->getObject('form_request'), $this->_oModule->_oConfig->getObject('form_display_request_send'));
  157. $aIdsAffected = array ();
  158. foreach($aIds as $iId) {
  159. if(!$oForm->delete($iId))
  160. continue;
  161. $this->_oModule->isAllowedDeleteRequest($iProfileId, true);
  162. $aIdsAffected[] = $iId;
  163. $iAffected++;
  164. }
  165. echoJson($iAffected ? array('grid' => $this->getCode(false), 'blink' => $aIdsAffected) : array('msg' => _t('_bx_invites_err_delete_request')));
  166. }
  167. protected function _getFilterControls ()
  168. {
  169. parent::_getFilterControls();
  170. return $this->_getFilterSelectOne($this->_sFilter1Name, $this->_sFilter1Value, $this->_aFilter1Values) . $this->_getSearchInput();
  171. }
  172. protected function _getSearchInput()
  173. {
  174. $sJsObject = $this->_oModule->_oConfig->getJsObject('main');
  175. $aInputSearch = array(
  176. 'type' => 'text',
  177. 'name' => 'search',
  178. 'attrs' => array(
  179. 'id' => 'bx-grid-search-' . $this->_sObject,
  180. 'onKeyup' => 'javascript:$(this).off(\'keyup focusout\'); ' . $sJsObject . '.onChangeFilter(this)',
  181. 'onBlur' => 'javascript:' . $sJsObject . '.onChangeFilter(this)',
  182. )
  183. );
  184. $oForm = new BxTemplFormView(array());
  185. return $oForm->genRow($aInputSearch);
  186. }
  187. protected function _getFilterSelectOne($sFilterName, $sFilterValue, $aFilterValues)
  188. {
  189. if(empty($sFilterName) || empty($aFilterValues))
  190. return '';
  191. $CNF = &$this->_oModule->_oConfig->CNF;
  192. $sJsObject = $this->_oModule->_oConfig->getJsObject('main');
  193. foreach($aFilterValues as $sKey => $sValue)
  194. $aFilterValues[$sKey] = _t($sValue);
  195. $aInputModules = array(
  196. 'type' => 'select',
  197. 'name' => $sFilterName,
  198. 'attrs' => array(
  199. 'id' => 'bx-grid-' . $sFilterName . '-' . $this->_sObject,
  200. 'onChange' => 'javascript:' . $sJsObject . '.onChangeFilter(this)'
  201. ),
  202. 'value' => $sFilterValue,
  203. 'values' => $aFilterValues
  204. );
  205. $oForm = new BxTemplFormView(array());
  206. return $oForm->genRow($aInputModules);
  207. }
  208. protected function _getCellNip($mixedValue, $sKey, $aField, $aRow)
  209. {
  210. return parent::_getCellDefault(long2ip($mixedValue), $sKey, $aField, $aRow);
  211. }
  212. protected function _getCellDate($mixedValue, $sKey, $aField, $aRow)
  213. {
  214. return parent::_getCellDefault(bx_time_js($mixedValue), $sKey, $aField, $aRow);
  215. }
  216. protected function _getCellStatus($mixedValue, $sKey, $aField, $aRow)
  217. {
  218. $sStatus = _t('_bx_invites_request_status_new');
  219. if($aRow["invite_id"] != "")
  220. $sStatus = _t('_bx_invites_request_status_invited');
  221. if($aRow["date_seen"] != "")
  222. $sStatus = _t('_bx_invites_request_status_seen');
  223. if($aRow["joined_account"] != "")
  224. $sStatus = _t('_bx_invites_request_status_joined');
  225. return parent::_getCellDefault($sStatus, $sKey, $aField, $aRow);
  226. }
  227. protected function _getCellDateSeen($mixedValue, $sKey, $aField, $aRow)
  228. {
  229. return parent::_getCellDefault($this->getDateValueOrEmpty($mixedValue), $sKey, $aField, $aRow);
  230. }
  231. protected function _getCellDateJoined($mixedValue, $sKey, $aField, $aRow)
  232. {
  233. return parent::_getCellDefault($this->getDateValueOrEmpty($mixedValue), $sKey, $aField, $aRow);
  234. }
  235. protected function _getCellJoinedAccount($mixedValue, $sKey, $aField, $aRow)
  236. {
  237. $sAccountInfo = "";
  238. if ($mixedValue != 'undefined') {
  239. $iAccountId = (int)$mixedValue;
  240. $oJoinedProfile = BxDolProfile::getInstanceByAccount($iAccountId);
  241. if ($oJoinedProfile->getModule() != 'system') {
  242. $sAccountInfo = $this->_oModule->_oTemplate->parseHtmlByName('member_link.html', array(
  243. 'href' => $oJoinedProfile->getUrl(),
  244. 'content' => $oJoinedProfile->getDisplayName()
  245. )
  246. );
  247. }
  248. else {
  249. $sAccountInfo = BxDolAccount::getInstance($iAccountId)->getDisplayName();
  250. }
  251. }
  252. return parent::_getCellDefault($sAccountInfo, $sKey, $aField, $aRow);
  253. }
  254. protected function _getCellDateInvite($mixedValue, $sKey, $aField, $aRow)
  255. {
  256. return parent::_getCellDefault($this->getDateValueOrEmpty($mixedValue), $sKey, $aField, $aRow);
  257. }
  258. protected function _getActionInvite($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  259. {
  260. if(isset($aRow["invite_id"]) && $aRow["invite_id"] != "")
  261. return '';
  262. return parent::_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  263. }
  264. private function getDateValueOrEmpty($sValue)
  265. {
  266. return $sValue != 'undefined' ? bx_time_js($sValue) : '';
  267. }
  268. }
  269. /** @} */