BxBaseProfileSearchResult.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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 UnaBaseView UNA Base Representation Classes
  7. * @{
  8. */
  9. class BxBaseProfileSearchResult extends BxTemplSearchResult
  10. {
  11. protected $_bIsApi;
  12. protected $aUnitViews;
  13. protected $sUnitViewDefault;
  14. public function __construct($sMode = '', $aParams = [])
  15. {
  16. $this->_sMode = $sMode;
  17. $this->_aParams = $aParams;
  18. parent::__construct();
  19. $this->_bIsApi = bx_is_api();
  20. $this->aUnitViews = [
  21. 'unit' => 'unit_wo_cover.html',
  22. 'gallery' => 'unit_with_cover.html'
  23. ];
  24. if(!empty($aParams['unit_views']) && is_array($aParams['unit_views']))
  25. $this->aUnitViews = array_merge($this->aUnitViews, $aParams['unit_views']);
  26. $this->sUnitViewDefault = 'gallery';
  27. if(!empty($aParams['unit_view']))
  28. $this->sUnitViewDefault = $aParams['unit_view'];
  29. $this->sUnitTemplate = $this->aUnitViews[$this->sUnitViewDefault];
  30. $this->addContainerClass (array(
  31. 'bx-base-pofile-units-wrapper',
  32. 'bx-base-puw-' . str_replace('_' , '-', $this->sUnitViewDefault),
  33. 'bx-def-margin-sec-neg'
  34. ));
  35. $aProfileModules = bx_srv('system', 'get_modules_by_type', ['profile']);
  36. $aProfileModulesNames = array_map(function($item) {
  37. return $item['name'];
  38. }, $aProfileModules);
  39. $this->aCurrent = [
  40. 'name' => 'sys_search',
  41. 'module_name' => 'system',
  42. 'title' => '',
  43. 'table' => 'sys_profiles',
  44. 'ownFields' => ['id', 'account_id', 'type', 'content_id'],
  45. 'searchFields' => [],
  46. 'restriction' => [
  47. 'account_id' => ['value' => '', 'field' => 'account_id', 'operator' => '='],
  48. 'perofileStatus' => ['value' => 'active', 'field' => 'status', 'operator' => '='],
  49. 'perofileType' => ['value' => $aProfileModulesNames, 'field' => 'type', 'operator' => 'in'],
  50. ],
  51. 'join' => [
  52. 'account' => [
  53. 'type' => 'INNER',
  54. 'table' => 'sys_accounts',
  55. 'mainField' => 'account_id',
  56. 'onField' => 'id',
  57. 'joinFields' => [],
  58. ],
  59. ],
  60. 'paginate' => ['start' => 0, 'perPage' => BX_DOL_SEARCH_RESULTS_PER_PAGE_DEFAULT],
  61. 'sorting' => 'last',
  62. 'ident' => 'id'
  63. ];
  64. if(!empty($this->_aParams['start']))
  65. $this->aCurrent['paginate']['forceStart'] = (int)$this->_aParams['start'];
  66. if(!empty($this->_aParams['per_page']))
  67. $this->aCurrent['paginate']['perPage'] = (int)$this->_aParams['per_page'];
  68. $this->sBrowseUrl = '';
  69. switch ($sMode) {
  70. case 'friends':
  71. $aParams = array_merge($aParams, [
  72. 'object' => 'sys_profiles_friends',
  73. 'type' => 'content',
  74. 'mutual' => true,
  75. 'profile2' => 0
  76. ]);
  77. if(!$this->_setConnectionsConditions($aParams)) {
  78. $this->isError = true;
  79. break;
  80. }
  81. $this->aCurrent = array_merge($this->aCurrent, [
  82. 'title' => _t('_sys_page_title_browse_connections', BxDolProfile::getInstanceMagic($aParams['profile'])->getDisplayName()),
  83. 'api_request_url' => '/api.php?r=system/browse_friends/TemplServiceProfiles&params[]=' . $aParams['profile'] . '&params[]='
  84. ]);
  85. break;
  86. case 'friend_requests':
  87. $aParams = array_merge($aParams, [
  88. 'object' => 'sys_profiles_friends',
  89. 'type' => 'initiators',
  90. 'mutual' => 0,
  91. 'profile2' => 0
  92. ]);
  93. if(!$this->_setConnectionsConditions($aParams)) {
  94. $this->isError = true;
  95. break;
  96. }
  97. $this->aCurrent = array_merge($this->aCurrent, [
  98. 'title' => _t('_sys_page_title_browse_connections', BxDolProfile::getInstanceMagic($aParams['profile'])->getDisplayName()),
  99. 'api_request_url' => '/api.php?r=system/browse_friend_requests/TemplServiceProfiles&params[]=' . $aParams['profile'] . '&params[]='
  100. ]);
  101. break;
  102. case 'friend_requested':
  103. $aParams = array_merge($aParams, [
  104. 'object' => 'sys_profiles_friends',
  105. 'type' => 'content',
  106. 'mutual' => 0,
  107. 'profile2' => 0
  108. ]);
  109. if(!$this->_setConnectionsConditions($aParams)) {
  110. $this->isError = true;
  111. break;
  112. }
  113. $this->aCurrent = array_merge($this->aCurrent, [
  114. 'title' => _t('_sys_page_title_browse_connections', BxDolProfile::getInstanceMagic($aParams['profile'])->getDisplayName()),
  115. 'api_request_url' => '/api.php?r=system/browse_friend_requested/TemplServiceProfiles&params[]=' . $aParams['profile'] . '&params[]='
  116. ]);
  117. break;
  118. case 'subscriptions':
  119. $aParams = array_merge($aParams, [
  120. 'object' => 'sys_profiles_subscriptions',
  121. 'type' => 'content',
  122. 'mutual' => false,
  123. 'profile2' => 0
  124. ]);
  125. if(!$this->_setConnectionsConditions($aParams)) {
  126. $this->isError = true;
  127. break;
  128. }
  129. $this->aCurrent = array_merge($this->aCurrent, [
  130. 'title' => _t('_sys_page_title_browse_connections', BxDolProfile::getInstanceMagic($aParams['profile'])->getDisplayName()),
  131. 'api_request_url' => '/api.php?r=system/browse_subscriptions/TemplServiceProfiles&params[]=' . $aParams['profile'] . '&params[]='
  132. ]);
  133. break;
  134. case 'subscribed_me':
  135. $aParams = array_merge($aParams, [
  136. 'object' => 'sys_profiles_subscriptions',
  137. 'type' => 'initiators',
  138. 'mutual' => false,
  139. 'profile2' => 0
  140. ]);
  141. if(!$this->_setConnectionsConditions($aParams)) {
  142. $this->isError = true;
  143. break;
  144. }
  145. $this->aCurrent = array_merge($this->aCurrent, [
  146. 'title' => _t('_sys_page_title_browse_connections', BxDolProfile::getInstanceMagic($aParams['profile'])->getDisplayName()),
  147. 'api_request_url' => '/api.php?r=system/browse_subscribed_me/TemplServiceProfiles&params[]=' . $aParams['profile'] . '&params[]='
  148. ]);
  149. break;
  150. case 'members':
  151. $aParams = array_merge($aParams, [
  152. 'type' => 'content',
  153. 'mutual' => true,
  154. 'profile2' => 0
  155. ]);
  156. if(!$this->_setConnectionsConditions($aParams)) {
  157. $this->isError = true;
  158. break;
  159. }
  160. $this->aCurrent = array_merge($this->aCurrent, [
  161. 'title' => _t('_sys_page_title_browse_connections', BxDolProfile::getInstanceMagic($aParams['profile'])->getDisplayName()),
  162. 'api_request_url' => '/api.php?r=system/browse_members/TemplServiceProfiles&params[]=' . $aParams['profile'] . '&params[]=' . $aParams['object'] . '&params[]='
  163. ]);
  164. break;
  165. case 'connections':
  166. if(!$this->_setConnectionsConditions($aParams)) {
  167. $this->isError = true;
  168. break;
  169. }
  170. $oProfile = BxDolProfile::getInstance($aParams['profile']);
  171. $oProfile2 = isset($aParams['profile2']) ? BxDolProfile::getInstance($aParams['profile2']) : null;
  172. $bCommon = isset($aParams['type']) && $aParams['type'] == 'common';
  173. if($bCommon && $oProfile && $oProfile2)
  174. $this->aCurrent['title'] = _t('_sys_page_title_browse_connections_mutual', $oProfile->getDisplayName(), $oProfile2->getDisplayName());
  175. else if(!$bCommon && $oProfile)
  176. $this->aCurrent['title'] = _t('_sys_page_title_browse_connections', $oProfile->getDisplayName());
  177. $this->aCurrent['api_request_url'] = '/api.php?r=system/browse_connections/TemplServiceProfiles&params[]=' . $aParams['profile'] . '&params[]=';
  178. break;
  179. case 'invitations':
  180. if(!$this->_setInvitationsConditions($aParams)) {
  181. $this->isError = true;
  182. break;
  183. }
  184. if(($oProfile = BxDolProfile::getInstance($aParams['profile'])) !== false)
  185. $this->aCurrent['title'] = _t('_sys_page_title_browse_invitations', $oProfile->getDisplayName());
  186. $this->aCurrent['api_request_url'] = '/api.php?r=system/browse_invitations/TemplServiceProfiles&params[]=' . $aParams['profile'] . '&params[]=';
  187. break;
  188. }
  189. }
  190. function displaySearchUnit ($aData)
  191. {
  192. $oProfile = BxDolProfile::getInstance($aData['id']);
  193. if(!$oProfile)
  194. return '';
  195. $aParams = $this->aUnitParams;
  196. if($this->_bIsApi)
  197. return $oProfile->getUnitAPI(0, $aParams);
  198. $aParams['template'] = substr($this->sUnitTemplate, 0, -5);
  199. return $oProfile->getUnit(0, $aParams);
  200. }
  201. function getAlterOrder()
  202. {
  203. $sType = is_array($this->aCurrent['sorting']) ? $this->aCurrent['sorting']['type'] : $this->aCurrent['sorting'];
  204. switch($sType) {
  205. case 'none':
  206. return [];
  207. case 'active':
  208. return ['order' => ' ORDER BY `sys_accounts`.`logged` DESC '];
  209. case 'last_connected':
  210. case 'last_invited':
  211. return ['order' => ' ORDER BY `' . $this->aCurrent['sorting']['table'] . '`.`added` DESC '];
  212. case 'last':
  213. default:
  214. return ['order' => ' ORDER BY `sys_accounts`.`added` DESC '];
  215. }
  216. }
  217. function decodeDataAPI($a)
  218. {
  219. if(!is_array($a))
  220. return $a;
  221. $aResult = [];
  222. foreach ($a as $index => $aItem)
  223. if(($oProfile = BxDolProfile::getInstance($aItem['id'])) !== false) {
  224. $aUnitApi = $oProfile->getUnitAPI();
  225. $aUnitApi['id'] = $aItem['id'];
  226. $aResult[] = $aUnitApi;
  227. }
  228. return $aResult;
  229. }
  230. protected function _setConnectionsConditions ($aParams)
  231. {
  232. $oConnection = isset($aParams['object']) ? BxDolConnection::getObjectInstance($aParams['object']) : false;
  233. if(!$oConnection || empty($aParams['profile']))
  234. return false;
  235. $sContentType = isset($aParams['type']) ? $aParams['type'] : BX_CONNECTIONS_CONTENT_TYPE_CONTENT;
  236. $isMutual = isset($aParams['mutual']) ? $aParams['mutual'] : false;
  237. $aConnection = $oConnection->getConnectionsAsCondition($sContentType, 'id', (int)$aParams['profile'], (int)$aParams['profile2'], $isMutual);
  238. $this->aCurrent['restriction'] = array_merge($this->aCurrent['restriction'], $aConnection['restriction']);
  239. $this->aCurrent['join'] = array_merge($this->aCurrent['join'], $aConnection['join']);
  240. $this->aCurrent['sorting'] = [
  241. 'type' => 'last_connected',
  242. 'table' => current($aConnection['join'])['table']
  243. ];
  244. return true;
  245. }
  246. protected function _setInvitationsConditions ($aParams)
  247. {
  248. if(empty($aParams['profile']) || empty($aParams['module']))
  249. return false;
  250. $oModule = BxDolModule::getInstance($aParams['module']);
  251. if(!$oModule || empty($oModule->_oConfig->CNF['TABLE_INVITES']))
  252. return false;
  253. $sKey = 'invitations_' . $aParams['module'];
  254. $sTable = $oModule->_oConfig->CNF['TABLE_INVITES'];
  255. $this->aCurrent['tableSearch'] = $sTable;
  256. $this->aCurrent['ident'] = 'group_profile_id';
  257. $this->aCurrent['restriction']['perofileType']['value'] = '';
  258. $this->aCurrent['restriction'][$sKey] = [
  259. 'value' => (int)$aParams['profile'],
  260. 'table' => $sTable,
  261. 'field' => 'invited_profile_id',
  262. 'operator' => '='
  263. ];
  264. $this->aCurrent['join'][$sKey] = [
  265. 'type' => 'INNER',
  266. 'table' => $oModule->_oConfig->CNF['TABLE_INVITES'],
  267. 'mainField' => 'id',
  268. 'onField' => 'group_profile_id',
  269. 'joinFields' => []
  270. ];
  271. $this->aCurrent['sorting'] = [
  272. 'type' => 'last_invited',
  273. 'table' => $sTable
  274. ];
  275. return true;
  276. }
  277. }
  278. /** @} */