BxBaseModGroupsConnectionFans.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 Groups Groups
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxBaseModGroupsConnectionFans extends BxTemplConnection
  12. {
  13. protected $_sModule;
  14. protected $_oModule;
  15. protected $_bQuestionnaire;
  16. public function __construct($aObject)
  17. {
  18. parent::__construct($aObject);
  19. $this->_oModule = BxDolModule::getInstance($this->_sModule);
  20. $this->_bQuestionnaire = false;
  21. }
  22. public function getModule()
  23. {
  24. return $this->_sModule;
  25. }
  26. public function actionRemove($iContent = 0, $iInitiator = false)
  27. {
  28. $CNF = &$this->_oModule->_oConfig->CNF;
  29. $aResult = parent::actionRemove($iContent, $iInitiator);
  30. if($aResult['err'] == false) {
  31. if(!$iContent)
  32. $iContent = bx_process_input($_POST['id'], BX_DATA_INT);
  33. $aContentInfo = $this->_oModule->_oDb->getContentInfoByProfileId($iContent);
  34. if(!empty($aContentInfo) && is_array($aContentInfo))
  35. $this->_oModule->_oDb->deleteAnswersProfileId($aContentInfo[$CNF['FIELD_ID']], $iInitiator ? $iInitiator : bx_get_logged_profile_id());
  36. }
  37. return $aResult;
  38. }
  39. public function getActionTitle($sAction, $iInitiatorId, $iContentId, $bFlip = false)
  40. {
  41. $aResult = [];
  42. if($this->isConnectedNotMutual($iInitiatorId, $iContentId))
  43. $aResult = [
  44. 'add' => '',
  45. 'remove' => _t('_sys_menu_item_title_sm_leave_cancel'),
  46. ];
  47. else if($this->isConnectedNotMutual($iContentId, $iInitiatorId))
  48. $aResult = [
  49. 'add' => _t('_sys_menu_item_title_sm_join_confirm'),
  50. 'remove' => _t('_sys_menu_item_title_sm_leave_reject'),
  51. ];
  52. else if($this->isConnected($iInitiatorId, $iContentId, true))
  53. $aResult = [
  54. 'add' => '',
  55. 'remove' => _t('_sys_menu_item_title_sm_leave'),
  56. ];
  57. else
  58. $aResult = [
  59. 'add' => _t('_sys_menu_item_title_sm_join'),
  60. 'remove' => '',
  61. ];
  62. $aFlip = ['add' => 'remove', 'remove' => 'add'];
  63. if($bFlip)
  64. $sAction = $aFlip[$sAction];
  65. return !empty($aResult[$sAction]) ? _t($aResult[$sAction]) : '';
  66. }
  67. public function hasQuestionnaire($iContentProfileId)
  68. {
  69. $CNF = &$this->_oModule->_oConfig->CNF;
  70. if(!$this->_bQuestionnaire || empty($CNF['FIELD_JOIN_CONFIRMATION']))
  71. return false;
  72. $aContentInfo = $this->_oModule->_oDb->getContentInfoByProfileId($iContentProfileId);
  73. if(empty($aContentInfo) || !is_array($aContentInfo))
  74. return false;
  75. return (int)$aContentInfo[$CNF['FIELD_JOIN_CONFIRMATION']] != 0 && $this->_oModule->_oDb->hasQuestions($aContentInfo[$CNF['FIELD_ID']]);
  76. }
  77. public function isQuestionnaireAnswered($iContentProfileId, $iProfileId = 0)
  78. {
  79. $CNF = &$this->_oModule->_oConfig->CNF;
  80. if(!$iProfileId)
  81. $iProfileId = bx_get_logged_profile_id();
  82. if(!$iProfileId)
  83. return false;
  84. if(!$this->_bQuestionnaire || empty($CNF['FIELD_JOIN_CONFIRMATION']))
  85. return false;
  86. $aContentInfo = $this->_oModule->_oDb->getContentInfoByProfileId($iContentProfileId);
  87. if(empty($aContentInfo) || !is_array($aContentInfo))
  88. return false;
  89. return (int)$aContentInfo[$CNF['FIELD_JOIN_CONFIRMATION']] != 0 && $this->_oModule->_oDb->areQuestionsAnswered($aContentInfo[$CNF['FIELD_ID']], $iProfileId);
  90. }
  91. public function getQuestionnaireForm($sAction, $iContentProfileId, $aParams = [])
  92. {
  93. $CNF = &$this->_oModule->_oConfig->CNF;
  94. if(!$this->_bQuestionnaire)
  95. return false;
  96. if(empty($aParams['request']) || !is_array($aParams['request']))
  97. $aParams['request'] = [];
  98. $aForm = [
  99. 'form_attrs' => [
  100. 'id' => $this->_oModule->getName() . '_questionnaire',
  101. 'action' => BX_DOL_URL_ROOT . bx_append_url_params($this->_oModule->_oConfig->getBaseUri() . 'get_questionnaire', array_merge([
  102. 'o' => $this->_sObject,
  103. 'a' => $sAction,
  104. 'cpi' => $iContentProfileId
  105. ], $aParams['request']))
  106. ],
  107. 'params' => [
  108. 'db' => [
  109. 'table' => $CNF['TABLE_ANSWERS'],
  110. 'key' => '',
  111. 'uri' => '',
  112. 'uri_title' => '',
  113. 'submit_name' => 'do_submit'
  114. ],
  115. 'module' => $this->_sModule,
  116. 'object' => $this->_sModule . '_questionnaire',
  117. 'display' => $this->_sModule . '_questionnaire_answer',
  118. 'view_mode' => 0,
  119. ],
  120. 'inputs' => []
  121. ];
  122. $aQuestions = $this->_oModule->_oDb->getQuestions(['sample' => 'content_pid', 'content_pid' => $iContentProfileId]);
  123. if(empty($aQuestions) || !is_array($aQuestions))
  124. return false;
  125. foreach($aQuestions as $aQuestion) {
  126. $sName = 'question_' . $aQuestion['id'];
  127. $aForm['inputs'][$sName] = [
  128. 'type' => 'text',
  129. 'name' => $sName,
  130. 'caption' => $aQuestion['question'],
  131. 'value' => '',
  132. 'required' => '1',
  133. 'checker' => [
  134. 'func' => 'Avail',
  135. 'params' => [],
  136. 'error' => _t($CNF['T']['form_qnr_field_qn_err']),
  137. ],
  138. 'db' => [
  139. 'pass' => 'Xss',
  140. ],
  141. ];
  142. }
  143. $aForm['inputs']['controls'] = [
  144. 'name' => 'controls',
  145. 'type' => 'input_set', [
  146. 'type' => 'submit',
  147. 'name' => 'do_submit',
  148. 'value' => _t('_Submit'),
  149. ], [
  150. 'type' => 'reset',
  151. 'name' => 'close',
  152. 'value' => _t('_Cancel'),
  153. 'attrs' => [
  154. 'onclick' => "$('.bx-popup-applied:visible').dolPopupHide()",
  155. 'class' => 'bx-def-margin-sec-left',
  156. ],
  157. ]
  158. ];
  159. return new BxTemplFormView($aForm);
  160. }
  161. protected function _checkAllowedConnectInitiator ($oInitiator, $isPerformAction = false)
  162. {
  163. if(!bx_srv($oInitiator->getModule(), 'act_as_profile'))
  164. return CHECK_ACTION_RESULT_ALLOWED;
  165. return parent::_checkAllowedConnectInitiator($oInitiator, $isPerformAction);
  166. }
  167. public function _checkAllowedConnectContent ($oContent)
  168. {
  169. $CNF = &$this->_oModule->_oConfig->CNF;
  170. if(bx_srv($oContent->getModule(), 'act_as_profile'))
  171. return CHECK_ACTION_RESULT_ALLOWED;
  172. if(!empty($CNF['OBJECT_PRIVACY_VIEW']) && ($oPrivacy = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_VIEW'])) !== false) {
  173. $iContentId = $oContent->getContentId();
  174. $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
  175. if(in_array($aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']], array_merge($oPrivacy->getPartiallyVisiblePrivacyGroups(), ['s'])))
  176. return CHECK_ACTION_RESULT_ALLOWED;
  177. }
  178. return parent::_checkAllowedConnectContent($oContent);
  179. }
  180. }
  181. /** @} */