BxBaseProfileSearchResult.php 13 KB

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