1
0

BxWorkspacesModule.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 Workspaces Workspaces
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. /**
  12. * Workspace profiles module.
  13. */
  14. class BxWorkspacesModule extends BxBaseModProfileModule
  15. {
  16. function __construct(&$aModule)
  17. {
  18. parent::__construct($aModule);
  19. $this->_aSearchableNamesExcept[] = $this->_oConfig->CNF['FIELD_AUTHOR'];
  20. }
  21. /**
  22. * @page service Service Calls
  23. * @section bx_workspaces Workspaces
  24. * @subsection bx_workspaces-other Other
  25. * @subsubsection bx_workspaces-get_search_options
  26. *
  27. * @code bx_srv('bx_workspaces', 'get_search_options', [...]); @endcode
  28. *
  29. * Get options for searchable fields
  30. *
  31. * @return array - params or false.
  32. *
  33. * @see BxWorkspacesModule::serviceGetSearchOptions
  34. */
  35. /**
  36. * @ref bx_workspaces-get_search_options "get_search_options"
  37. */
  38. public function serviceGetSearchOptions ($sField, $sFieldType, $sSearchType)
  39. {
  40. $CNF = $this->_oConfig->CNF;
  41. if (isset($CNF['OBJECT_FORM_ENTRY']) && isset($CNF['OBJECT_FORM_ENTRY_DISPLAY_ADD'])){
  42. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $CNF['OBJECT_FORM_ENTRY_DISPLAY_ADD'], $this->_oTemplate);
  43. foreach ($oForm->aInputs as $aFld) {
  44. if ($aFld['name'] == $sField && $aFld['type'] == $sFieldType && isset($aFld['checker']) && isset($aFld['checker']['params'])){
  45. return $aFld['checker']['params'];
  46. }
  47. }
  48. }
  49. if (isset($CNF['OBJECT_FORM_ENTRY']) && isset($CNF['OBJECT_FORM_ENTRY_DISPLAY_EDIT'])){
  50. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $CNF['OBJECT_FORM_ENTRY_DISPLAY_EDIT'], $this->_oTemplate);
  51. foreach ($oForm->aInputs as $aFld) {
  52. if ($aFld['name'] == $sField && $aFld['type'] == $sFieldType && isset($aFld['checker']) && isset($aFld['checker']['params'])){
  53. return $aFld['checker']['params'];
  54. }
  55. }
  56. }
  57. return false;
  58. }
  59. public function serviceGetContextModules ()
  60. {
  61. $a = bx_srv('system', 'get_modules_by_subtype_simple', ['context']);
  62. return array_merge(['' => _t('_sys_please_select')], $a);
  63. }
  64. public function getSubtypes()
  65. {
  66. return pow(2, BX_DOL_MODULE_SUBTYPE_PROFILE);
  67. }
  68. public function getProfileName ($aContentInfo)
  69. {
  70. return bx_process_output('User' . $aContentInfo[$this->_oConfig->CNF['FIELD_ID']]);
  71. }
  72. // autoswitch currently active user profiule to the ws profile if user already has ws profile in given group context
  73. public function autoSwitchProfile($oProfileGroup)
  74. {
  75. $oAccount = BxDolAccount::getInstance();
  76. $aProfiles = $oAccount->getProfilesIds();
  77. $aFans = bx_srv($oProfileGroup->getModule(), 'fans', [$oProfileGroup->getContentId(), true]);
  78. $a = array_intersect($aProfiles, $aFans);
  79. if ($a) {
  80. foreach ($a as $iProfileId) {
  81. $oProfile = BxDolProfile::getInstance($iProfileId);
  82. if ($oProfile->getModule() == $this->getName()) {
  83. $oAccount->updateProfileContext($iProfileId);
  84. return true;
  85. }
  86. }
  87. }
  88. return false;
  89. }
  90. public function addConnection($sContextModule, $oAlert)
  91. {
  92. $iProfileIdSubstituteInAlert = 0;
  93. $iProfileIdCreated = 0;
  94. // identify group profile and workspace profile
  95. [$oProfileGroup, $oProfileWs] = $this->_identifyGroupAndWorkspaceProfiles($sContextModule, $oAlert);
  96. // check for existing connection switch to ws profile if exising connection is found
  97. // TODO: call this method on any group view page, also update global context is needed
  98. if ($this->autoSwitchProfile($oProfileGroup)) {
  99. $oAlert->aExtras['override_result'] = true;
  100. return;
  101. }
  102. // check for old connection with this group id, if old ws profile which was associated with this group is found then switch to this old ws profile id
  103. if (null === $oProfileWs) {
  104. $a = BxDolAccount::getInstance()->getProfiles();
  105. foreach ($a as $iProfileId => $aProfile) {
  106. if ($aProfile['type'] == $this->getName()) {
  107. $iAllowViewTo = bx_srv($aProfile['type'], 'get_privacy_view', [$aProfile['content_id']]);
  108. if (abs($iAllowViewTo) == $oProfileGroup->id()) {
  109. $iProfileIdSubstituteInAlert = $iProfileId;
  110. break;
  111. }
  112. }
  113. }
  114. }
  115. // create new workspace profile if needed, context is automatically siwtched to newly created profile
  116. if (!$iProfileIdSubstituteInAlert && null === $oProfileWs) {
  117. $aInfo = [
  118. 'allow_view_to' => -$oProfileGroup->id(),
  119. 'allow_post_to' => 5,
  120. 'allow_contact_to' => -$oProfileGroup->id(),
  121. ];
  122. $a = bx_srv('system', 'add', ['bx_workspaces', $aInfo], 'TemplServiceContent');
  123. if (0 === $a['code']) {
  124. $iProfileIdSubstituteInAlert = $a['content']['profile_id'];
  125. $iProfileIdCreated = $a['content']['profile_id'];
  126. }
  127. }
  128. // substitute profile id with ws profile id in alert
  129. if (null === $oProfileWs) {
  130. $oAlert->aExtras['initiator'] = $iProfileIdSubstituteInAlert;
  131. }
  132. // switch context to the profile in context (context is already automatically switched when new profile is created)
  133. if (!$iProfileIdCreated && $iProfileIdSubstituteInAlert) {
  134. BxDolAccount::getInstance()->updateProfileContext($iProfileIdSubstituteInAlert);
  135. }
  136. }
  137. public function removeConnection($sContextModule, $oAlert)
  138. {
  139. $oAccount = BxDolAccount::getInstance();
  140. // identify group profile and workspace profile
  141. [$oProfileGroup, $oProfileWs] = $this->_identifyGroupAndWorkspaceProfiles($sContextModule, $oAlert);
  142. if (!$oProfileWs) // no need to switch context if no workspace profile was used
  143. return;
  144. // switch profile id from ws profile id to real profile id, if previous profile was ws profile and if colled in proper context module
  145. $iProfileIdReal = null;
  146. $a = $oAccount->getProfilesIds();
  147. foreach ($a as $iProfileId) {
  148. $oProfile = BxDolProfile::getInstance($iProfileId);
  149. if ($oProfile->getModule() != $this->getName()) {
  150. $iProfileIdReal = $oProfile->id();
  151. }
  152. }
  153. // TODO: if no profile was found - use account profile id
  154. if (!$iProfileIdReal) {
  155. }
  156. // switch current context
  157. if ($iProfileIdReal)
  158. $oAccount->updateProfileContext($iProfileIdReal);
  159. }
  160. // identify group profile and workspace profile
  161. protected function _identifyGroupAndWorkspaceProfiles($sContextModule, $oAlert)
  162. {
  163. $oProfileGroup = null;
  164. $oProfileWs = null;
  165. $oProfileInitiator = BxDolProfile::getInstance($oAlert->aExtras['initiator']);
  166. $oProfileContent = BxDolProfile::getInstance($oAlert->aExtras['content']);
  167. $a = [$oProfileInitiator, $oProfileContent];
  168. foreach ($a as $o) {
  169. if ($sContextModule == $o->getModule()) // detect group based profile
  170. $oProfileGroup = $o;
  171. elseif ($this->getName() == $o->getModule()) // detect workspace profile
  172. $oProfileWs = $o;
  173. }
  174. return [$oProfileGroup, $oProfileWs];
  175. }
  176. }
  177. /** @} */