BxBaseProfileSearchResult.php 11 KB

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