1
0

BxBaseProfileSearchResult.php 11 KB

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