1
0

BxBaseProfileSearchResult.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. break;
  79. $this->aCurrent = array_merge($this->aCurrent, [
  80. 'title' => _t('_sys_page_title_browse_connections', BxDolProfile::getInstanceMagic($aParams['profile'])->getDisplayName()),
  81. 'api_request_url' => '/api.php?r=system/browse_friends/TemplServiceProfiles&params[]=' . $aParams['profile'] . '&params[]='
  82. ]);
  83. break;
  84. case 'friend_requests':
  85. $aParams = array_merge($aParams, [
  86. 'object' => 'sys_profiles_friends',
  87. 'type' => 'initiators',
  88. 'mutual' => 0,
  89. 'profile2' => 0
  90. ]);
  91. if (!$this->_setConnectionsConditions($aParams))
  92. break;
  93. $this->aCurrent = array_merge($this->aCurrent, [
  94. 'title' => _t('_sys_page_title_browse_connections', BxDolProfile::getInstanceMagic($aParams['profile'])->getDisplayName()),
  95. 'api_request_url' => '/api.php?r=system/browse_friend_requests/TemplServiceProfiles&params[]=' . $aParams['profile'] . '&params[]='
  96. ]);
  97. break;
  98. case 'friend_requested':
  99. $aParams = array_merge($aParams, [
  100. 'object' => 'sys_profiles_friends',
  101. 'type' => 'content',
  102. 'mutual' => 0,
  103. 'profile2' => 0
  104. ]);
  105. if (!$this->_setConnectionsConditions($aParams))
  106. break;
  107. $this->aCurrent = array_merge($this->aCurrent, [
  108. 'title' => _t('_sys_page_title_browse_connections', BxDolProfile::getInstanceMagic($aParams['profile'])->getDisplayName()),
  109. 'api_request_url' => '/api.php?r=system/browse_friend_requested/TemplServiceProfiles&params[]=' . $aParams['profile'] . '&params[]='
  110. ]);
  111. break;
  112. case 'subscriptions':
  113. $aParams = array_merge($aParams, [
  114. 'object' => 'sys_profiles_subscriptions',
  115. 'type' => 'content',
  116. 'mutual' => false,
  117. 'profile2' => 0
  118. ]);
  119. if (!$this->_setConnectionsConditions($aParams))
  120. break;
  121. $this->aCurrent = array_merge($this->aCurrent, [
  122. 'title' => _t('_sys_page_title_browse_connections', BxDolProfile::getInstanceMagic($aParams['profile'])->getDisplayName()),
  123. 'api_request_url' => '/api.php?r=system/browse_subscriptions/TemplServiceProfiles&params[]=' . $aParams['profile'] . '&params[]='
  124. ]);
  125. break;
  126. case 'subscribed_me':
  127. $aParams = array_merge($aParams, [
  128. 'object' => 'sys_profiles_subscriptions',
  129. 'type' => 'initiators',
  130. 'mutual' => false,
  131. 'profile2' => 0
  132. ]);
  133. if (!$this->_setConnectionsConditions($aParams))
  134. break;
  135. $this->aCurrent = array_merge($this->aCurrent, [
  136. 'title' => _t('_sys_page_title_browse_connections', BxDolProfile::getInstanceMagic($aParams['profile'])->getDisplayName()),
  137. 'api_request_url' => '/api.php?r=system/browse_subscribed_me/TemplServiceProfiles&params[]=' . $aParams['profile'] . '&params[]='
  138. ]);
  139. break;
  140. case 'members':
  141. $aParams = array_merge($aParams, [
  142. 'type' => 'content',
  143. 'mutual' => true,
  144. 'profile2' => 0
  145. ]);
  146. if (!$this->_setConnectionsConditions($aParams))
  147. break;
  148. $this->aCurrent = array_merge($this->aCurrent, [
  149. 'title' => _t('_sys_page_title_browse_connections', BxDolProfile::getInstanceMagic($aParams['profile'])->getDisplayName()),
  150. 'api_request_url' => '/api.php?r=system/browse_members/TemplServiceProfiles&params[]=' . $aParams['profile'] . '&params[]=' . $aParams['object'] . '&params[]='
  151. ]);
  152. break;
  153. case 'connections':
  154. if (!$this->_setConnectionsConditions($aParams))
  155. break;
  156. $oProfile = BxDolProfile::getInstance($aParams['profile']);
  157. $oProfile2 = isset($aParams['profile2']) ? BxDolProfile::getInstance($aParams['profile2']) : null;
  158. $bCommon = isset($aParams['type']) && $aParams['type'] == 'common';
  159. if($bCommon && $oProfile && $oProfile2)
  160. $this->aCurrent['title'] = _t('_sys_page_title_browse_connections_mutual', $oProfile->getDisplayName(), $oProfile2->getDisplayName());
  161. else if(!$bCommon && $oProfile)
  162. $this->aCurrent['title'] = _t('_sys_page_title_browse_connections', $oProfile->getDisplayName());
  163. $this->aCurrent['api_request_url'] = '/api.php?r=system/browse_connections/TemplServiceProfiles&params[]=' . $aParams['profile'] . '&params[]=';
  164. break;
  165. }
  166. }
  167. function displaySearchUnit ($aData)
  168. {
  169. $oProfile = BxDolProfile::getInstance($aData['id']);
  170. if(!$oProfile)
  171. return '';
  172. $aParams = $this->aUnitParams;
  173. if($this->_bIsApi)
  174. return $oProfile->getUnitAPI(0, $aParams);
  175. $aParams['template'] = substr($this->sUnitTemplate, 0, -5);
  176. return $oProfile->getUnit(0, $aParams);
  177. }
  178. function getAlterOrder()
  179. {
  180. $sType = is_array($this->aCurrent['sorting']) ? $this->aCurrent['sorting']['type'] : $this->aCurrent['sorting'];
  181. switch($sType) {
  182. case 'none':
  183. return [];
  184. case 'active':
  185. return ['order' => ' ORDER BY `sys_accounts`.`logged` DESC '];
  186. case 'last_connected':
  187. return ['order' => ' ORDER BY `' . $this->aCurrent['sorting']['table'] . '`.`added` DESC '];
  188. case 'last':
  189. default:
  190. return ['order' => ' ORDER BY `sys_accounts`.`added` DESC '];
  191. }
  192. }
  193. function decodeDataAPI($a)
  194. {
  195. $aResult = [];
  196. foreach ($a as $index => $aItem)
  197. if(($oProfile = BxDolProfile::getInstance($aItem['id'])) !== false)
  198. $aResult[] = $oProfile->getUnitAPI();
  199. return $aResult;
  200. }
  201. protected function _setConnectionsConditions ($aParams)
  202. {
  203. $oConnection = isset($aParams['object']) ? BxDolConnection::getObjectInstance($aParams['object']) : false;
  204. if(!$oConnection || empty($aParams['profile']))
  205. return false;
  206. $sContentType = isset($aParams['type']) ? $aParams['type'] : BX_CONNECTIONS_CONTENT_TYPE_CONTENT;
  207. $isMutual = isset($aParams['mutual']) ? $aParams['mutual'] : false;
  208. $aConnection = $oConnection->getConnectionsAsCondition($sContentType, 'id', (int)$aParams['profile'], (int)$aParams['profile2'], $isMutual);
  209. $this->aCurrent['restriction'] = array_merge($this->aCurrent['restriction'], $aConnection['restriction']);
  210. $this->aCurrent['join'] = array_merge($this->aCurrent['join'], $aConnection['join']);
  211. $this->aCurrent['sorting'] = [
  212. 'type' => 'last_connected',
  213. 'table' => current($aConnection['join'])['table']
  214. ];
  215. return true;
  216. }
  217. }
  218. /** @} */