1
0

BxBaseModGroupsConnectionFans.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. protected $_bBan;
  17. protected $_oBanConnection;
  18. public function __construct($aObject)
  19. {
  20. parent::__construct($aObject);
  21. $this->_oModule = BxDolModule::getInstance($this->_sModule);
  22. $this->_bQuestionnaire = false;
  23. $this->_bBan = false;
  24. $this->_oBanConnection = null;
  25. $this->_aT = array_merge($this->_aT, [
  26. 'do_initiator' => $this->_getLKey('menu_item_title_sm_membership'),
  27. 'do_content' => $this->_getLKey('menu_item_title_sm_membership_respond'),
  28. 'counter' => $this->_getLKey('menu_item_title_sm_members')
  29. ]);
  30. }
  31. public function init()
  32. {
  33. if($this->_bBan)
  34. $this->_oBanConnection = BxDolConnection::getObjectInstance('sys_profiles_bans');
  35. }
  36. public function getModule()
  37. {
  38. return $this->_sModule;
  39. }
  40. public function checkAllowedAddConnection($iInitiator, $iContent, $isPerformAction = false, $isMutual = false, $isInvertResult = false, $isSwap = false, $isCheckExists = true)
  41. {
  42. if($this->_bBan && $this->_oBanConnection->isConnected($iContent, $iInitiator))
  43. return _t('_sys_txt_access_denied');
  44. return $this->checkAllowedConnect ($iInitiator, $iContent, $isPerformAction, $isMutual, $isInvertResult, $isSwap, $isCheckExists);
  45. }
  46. public function actionRemove($iContent = 0, $iInitiator = false)
  47. {
  48. $CNF = &$this->_oModule->_oConfig->CNF;
  49. $aResult = parent::actionRemove($iContent, $iInitiator);
  50. if($this->_bQuestionnaire && $aResult['err'] == false) {
  51. if(!$iContent)
  52. $iContent = bx_process_input($_POST['id'], BX_DATA_INT);
  53. $aContentInfo = $this->_oModule->_oDb->getContentInfoByProfileId($iContent);
  54. if(!empty($aContentInfo) && is_array($aContentInfo))
  55. $this->_oModule->_oDb->deleteAnswersProfileId($aContentInfo[$CNF['FIELD_ID']], $iInitiator ? $iInitiator : bx_get_logged_profile_id());
  56. }
  57. return $aResult;
  58. }
  59. public function actionReject ($iInitiator = 0, $iContent = false)
  60. {
  61. $CNF = &$this->_oModule->_oConfig->CNF;
  62. $aResult = parent::actionReject($iInitiator, $iContent);
  63. if($this->_bQuestionnaire && $aResult['err'] == false) {
  64. if(!$iContent)
  65. $iContent = bx_process_input($_POST['id'], BX_DATA_INT);
  66. $aContentInfo = $this->_oModule->_oDb->getContentInfoByProfileId($iContent);
  67. if(!empty($aContentInfo) && is_array($aContentInfo))
  68. $this->_oModule->_oDb->deleteAnswersProfileId($aContentInfo[$CNF['FIELD_ID']], $iInitiator ? $iInitiator : bx_get_logged_profile_id());
  69. }
  70. return $aResult;
  71. }
  72. public function hasQuestionnaire($iContentProfileId)
  73. {
  74. $CNF = &$this->_oModule->_oConfig->CNF;
  75. if(!$this->_bQuestionnaire || empty($CNF['FIELD_JOIN_CONFIRMATION']))
  76. return false;
  77. $aContentInfo = $this->_oModule->_oDb->getContentInfoByProfileId($iContentProfileId);
  78. if(empty($aContentInfo) || !is_array($aContentInfo))
  79. return false;
  80. return (int)$aContentInfo[$CNF['FIELD_JOIN_CONFIRMATION']] != 0 && $this->_oModule->_oDb->hasQuestions($aContentInfo[$CNF['FIELD_ID']]);
  81. }
  82. public function isQuestionnaireAnswered($iContentProfileId, $iProfileId = 0)
  83. {
  84. $CNF = &$this->_oModule->_oConfig->CNF;
  85. if(!$iProfileId)
  86. $iProfileId = bx_get_logged_profile_id();
  87. if(!$iProfileId)
  88. return false;
  89. if(!$this->_bQuestionnaire || empty($CNF['FIELD_JOIN_CONFIRMATION']))
  90. return false;
  91. $aContentInfo = $this->_oModule->_oDb->getContentInfoByProfileId($iContentProfileId);
  92. if(empty($aContentInfo) || !is_array($aContentInfo))
  93. return false;
  94. return (int)$aContentInfo[$CNF['FIELD_JOIN_CONFIRMATION']] != 0 && $this->_oModule->_oDb->areQuestionsAnswered($aContentInfo[$CNF['FIELD_ID']], $iProfileId);
  95. }
  96. public function getQuestionnaireForm($sAction, $iContentProfileId, $aParams = [])
  97. {
  98. $CNF = &$this->_oModule->_oConfig->CNF;
  99. if(!$this->_bQuestionnaire)
  100. return false;
  101. if(empty($aParams['request']) || !is_array($aParams['request']))
  102. $aParams['request'] = [];
  103. $aForm = [
  104. 'form_attrs' => [
  105. 'id' => $this->_oModule->getName() . '_questionnaire',
  106. 'action' => BX_DOL_URL_ROOT . bx_append_url_params($this->_oModule->_oConfig->getBaseUri() . 'get_questionnaire', array_merge([
  107. 'o' => $this->_sObject,
  108. 'a' => $sAction,
  109. 'cpi' => $iContentProfileId
  110. ], $aParams['request']))
  111. ],
  112. 'params' => [
  113. 'db' => [
  114. 'table' => $CNF['TABLE_ANSWERS'],
  115. 'key' => '',
  116. 'uri' => '',
  117. 'uri_title' => '',
  118. 'submit_name' => 'do_submit'
  119. ],
  120. 'module' => $this->_sModule,
  121. 'object' => $this->_sModule . '_questionnaire',
  122. 'display' => $this->_sModule . '_questionnaire_answer',
  123. 'view_mode' => 0,
  124. ],
  125. 'inputs' => []
  126. ];
  127. $aQuestions = $this->_oModule->_oDb->getQuestions(['sample' => 'content_pid', 'content_pid' => $iContentProfileId]);
  128. if(empty($aQuestions) || !is_array($aQuestions))
  129. return false;
  130. foreach($aQuestions as $aQuestion) {
  131. $sName = 'question_' . $aQuestion['id'];
  132. $aForm['inputs'][$sName] = [
  133. 'type' => 'text',
  134. 'name' => $sName,
  135. 'caption' => $aQuestion['question'],
  136. 'value' => '',
  137. 'required' => '1',
  138. 'checker' => [
  139. 'func' => 'Avail',
  140. 'params' => [],
  141. 'error' => _t($CNF['T']['form_qnr_field_qn_err']),
  142. ],
  143. 'db' => [
  144. 'pass' => 'Xss',
  145. ],
  146. ];
  147. }
  148. $aForm['inputs']['controls'] = [
  149. 'name' => 'controls',
  150. 'type' => 'input_set', [
  151. 'type' => 'submit',
  152. 'name' => 'do_submit',
  153. 'value' => _t('_Submit'),
  154. ], [
  155. 'type' => 'reset',
  156. 'name' => 'close',
  157. 'value' => _t('_Cancel'),
  158. 'attrs' => [
  159. 'onclick' => "$('.bx-popup-applied:visible').dolPopupHide()",
  160. 'class' => 'bx-def-margin-sec-left',
  161. ],
  162. ]
  163. ];
  164. return new BxTemplFormView($aForm);
  165. }
  166. protected function _checkAllowedConnectInitiator ($oInitiator, $isPerformAction = false)
  167. {
  168. if(!bx_srv($oInitiator->getModule(), 'act_as_profile'))
  169. return CHECK_ACTION_RESULT_ALLOWED;
  170. return parent::_checkAllowedConnectInitiator($oInitiator, $isPerformAction);
  171. }
  172. protected function _checkAllowedConnectContent ($oContent)
  173. {
  174. $CNF = &$this->_oModule->_oConfig->CNF;
  175. if(bx_srv($oContent->getModule(), 'act_as_profile'))
  176. return CHECK_ACTION_RESULT_ALLOWED;
  177. if(!empty($CNF['OBJECT_PRIVACY_VIEW']) && ($oPrivacy = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_VIEW'])) !== false) {
  178. $iContentId = $oContent->getContentId();
  179. $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
  180. if(in_array($aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']], array_merge($oPrivacy->getPartiallyVisiblePrivacyGroups(), ['s'])))
  181. return CHECK_ACTION_RESULT_ALLOWED;
  182. }
  183. return parent::_checkAllowedConnectContent($oContent);
  184. }
  185. protected function _getActions($iInitiator, $iContent, $aParams = [])
  186. {
  187. $CNF = &$this->_oModule->_oConfig->CNF;
  188. $sName = $sTitle = '';
  189. $aActions = [];
  190. if($this->isConnectedNotMutual($iInitiator, $iContent)) {
  191. $aActions = [[
  192. 'name' => 'remove',
  193. 'title' => $this->_getLKey('menu_item_title_sm_leave_cancel')
  194. ]];
  195. }
  196. else if($this->isConnectedNotMutual($iContent, $iInitiator)) {
  197. $sName = 'add';
  198. $sTitle = $this->_aT['do_content'];
  199. $aActions = [[
  200. 'name' => 'add',
  201. 'title' => $this->_getLKey('menu_item_title_sm_join_confirm')
  202. ], [
  203. 'name' => 'remove',
  204. 'title' => $this->_getLKey('menu_item_title_sm_leave_reject')
  205. ]];
  206. }
  207. else if($this->isConnected($iInitiator, $iContent, true)) {
  208. $sName = 'default';
  209. $sTitle = $this->_aT['do_initiator'];
  210. $aActions = [[
  211. 'name' => 'remove',
  212. 'title' => $this->_getLKey('menu_item_title_sm_leave')
  213. ], [
  214. /*
  215. * An empty array item to show all items in popup.
  216. * More actions will be added later.
  217. */
  218. ]];
  219. }
  220. else
  221. $aActions = [[
  222. 'name' => 'add',
  223. 'title' => $this->_getLKey('menu_item_title_sm_join')
  224. ]];
  225. return [
  226. 'name' => $sName,
  227. 'title' => $sTitle,
  228. 'items' => $aActions
  229. ];
  230. }
  231. protected function _getActionIconAsIcon($sAction)
  232. {
  233. $sDefault = 'users';
  234. $aA2I = [
  235. 'add' => 'sign-in-alt',
  236. 'remove' => 'sign-out-alt'
  237. ];
  238. return isset($aA2I[$sAction]) ? $aA2I[$sAction] : $sDefault;
  239. }
  240. protected function _getActionIconAsEmoji($sAction)
  241. {
  242. $sDefault = '🔄';
  243. $aA2I = [
  244. 'add' => '✅️️',
  245. 'remove' => '❎'
  246. ];
  247. return isset($aA2I[$sAction]) ? $aA2I[$sAction] : $sDefault;
  248. }
  249. protected function _getActionIconAsImage($sAction)
  250. {
  251. $sDefault = '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-users-round-icon lucide-users-round"><path d="M18 21a8 8 0 0 0-16 0"/><circle cx="10" cy="8" r="5"/><path d="M22 20c0-3.37-2-6.5-4-8a5 5 0 0 0-.45-8.3"/></svg>';
  252. $aA2I = [
  253. 'add' => '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-log-in-icon lucide-log-in"><path d="m10 17 5-5-5-5"/><path d="M15 12H3"/><path d="M15 3h4a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2h-4"/></svg>',
  254. 'remove' => '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-log-out-icon lucide-log-out"><path d="m16 17 5-5-5-5"/><path d="M21 12H9"/><path d="M9 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h4"/></svg>'
  255. ];
  256. return isset($aA2I[$sAction]) ? $aA2I[$sAction] : $sDefault;
  257. }
  258. protected function _getLKey($sKey, $bTranslate = false)
  259. {
  260. $CNF = &$this->_oModule->_oConfig->CNF;
  261. $sLKey = !empty($CNF['T'][$sKey]) ? $CNF['T'][$sKey] : '_sys_' . $sKey;
  262. return $bTranslate ? _t($sLKey) : $sLKey;
  263. }
  264. protected function _getTriggerObject($sType, $iInitiator, $iContent)
  265. {
  266. if(($oInitiator = BxDolProfile::getInstance($iInitiator)) !== false && ($sModule = $oInitiator->getModule()))
  267. if(($sModule == $this->_sModule && $sType == BX_CONNECTIONS_TRIGGER_TYPE_CONTENT) || ($sModule != $this->_sModule && $sType == BX_CONNECTIONS_TRIGGER_TYPE_INITIATOR))
  268. return $oInitiator->getContentId();
  269. if(($oContent = BxDolProfile::getInstance($iContent)) !== false && ($sModule = $oContent->getModule()) == $this->_sModule)
  270. if(($sModule == $this->_sModule && $sType == BX_CONNECTIONS_TRIGGER_TYPE_CONTENT) || ($sModule != $this->_sModule && $sType == BX_CONNECTIONS_TRIGGER_TYPE_INITIATOR))
  271. return $oContent->getContentId();
  272. return false;
  273. }
  274. }
  275. /** @} */