BxBaseConnection.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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. /**
  10. * @see BxBaseConnection
  11. */
  12. class BxBaseConnection extends BxDolConnection
  13. {
  14. protected $_oTemplate;
  15. protected $_oFunctions;
  16. protected $_sStylePrefix;
  17. protected $_sJsObjName;
  18. protected $_sHp;
  19. protected $_aHtmlIds;
  20. protected $_aT;
  21. protected $_sTmplContentElement;
  22. protected $_sTmplContentCounter;
  23. protected $_aElementDefaults;
  24. public function __construct($aObject)
  25. {
  26. parent::__construct($aObject);
  27. $this->_oTemplate = BxDolTemplate::getInstance();
  28. $this->_oFunctions = BxTemplFunctions::getInstanceWithTemplate($this->_oTemplate);
  29. $this->_sStylePrefix = 'bx-conn';
  30. $this->_sJsObjName = 'oConn' . bx_gen_method_name($this->_sObject, ['_' , '-']) . '';
  31. $this->_sHp = str_replace('_', '-', $this->_sObject);
  32. $this->_aHtmlIds = [
  33. 'main' => 'bx-conn-' . $this->_sHp . '-',
  34. 'do_popup' => 'bx-conn-do-popup-' . $this->_sHp . '-',
  35. 'do_menu' => 'bx-conn-do-menu-' . $this->_sHp . '-',
  36. 'counter' => 'bx-conn-counter-' . $this->_sHp . '-',
  37. 'by_popup' => 'bx-conn-by-popup-' . $this->_sHp . '-',
  38. ];
  39. $this->_aT = [
  40. 'do_by' => '_sys_conn_do_by'
  41. ];
  42. $this->_sTmplContentElement = $this->_oTemplate->getHtml('connection_element.html');
  43. $this->_sTmplContentCounter = $this->_oTemplate->getHtml('connection_counter.html');
  44. $this->_aElementDefaults = [
  45. 'dynamic_mode' => true,
  46. 'show_do_as_button_small' => false,
  47. 'show_do_as_button' => false,
  48. 'show_do_icon' => true,
  49. 'show_do_label' => true,
  50. 'show_counter' => false,
  51. 'show_counter_label_with_profiles' => true,
  52. 'show_script' => true
  53. ];
  54. }
  55. public function actionGetConnected ($iStart = 0, $iPerPage = 0)
  56. {
  57. $sContentType = bx_process_input(bx_get('content_type'));
  58. $iProfileId = (int)bx_get('id');
  59. $bIsMutual = (bool)bx_get('mutual');
  60. return $this->_getConnected ($sContentType, $iProfileId, $bIsMutual, $iStart, $iPerPage);
  61. }
  62. public function actionGetUsers ($iStart = 0, $iPerPage = 0)
  63. {
  64. $sContentType = bx_process_input(bx_get('content_type'));
  65. $iProfileId = (int)bx_get('id');
  66. $bIsMutual = (bool)bx_get('mutual');
  67. $iStart = (int)bx_get('start');
  68. $iPerPage = (int)bx_get('per_page');
  69. return [
  70. 'content' => $this->_getConnected($sContentType, $iProfileId, $bIsMutual, $iStart, $iPerPage),
  71. 'eval' => $this->getJsObjectName($iProfileId) . '.onGetUsers(oData)'
  72. ];
  73. }
  74. public function getJsObjectName($iProfileId)
  75. {
  76. return $this->_sJsObjName . $iProfileId;
  77. }
  78. public function getElement($iContent, $iInitiator = false, $aParams = [])
  79. {
  80. $aParams = array_merge($this->_aElementDefaults, $aParams);
  81. $bDynamicMode = isset($aParams['dynamic_mode']) && $aParams['dynamic_mode'] === true;
  82. $bShowDo = !isset($aParams['show_do']) || (bool)$aParams['show_do'] === true;
  83. $bShowDoAsButtonSmall = isset($aParams['show_do_as_button_small']) && $aParams['show_do_as_button_small'] == true;
  84. $bShowDoAsButton = !$bShowDoAsButtonSmall && isset($aParams['show_do_as_button']) && $aParams['show_do_as_button'] == true;
  85. $bShowDoLabel = isset($aParams['show_do_label']) && $aParams['show_do_label'] === true;
  86. $bShowCounter = isset($aParams['show_counter']) && $aParams['show_counter'] === true;
  87. if(!$iInitiator)
  88. $iInitiator = bx_get_logged_profile_id();
  89. if($iInitiator && $iInitiator == $iContent)
  90. return '';
  91. $aActions = $this->_getActions($iInitiator, $iContent, $aParams);
  92. $iCount = $this->_getCounterCount($iContent, $this->_bMutual, $aParams);
  93. if((empty($aActions['items']) || !is_array($aActions['items'])) && (!$bShowCounter || !$iCount))
  94. return '';
  95. $sClassDo = '';
  96. if($bShowDoAsButton)
  97. $sClassDo = ' bx-btn';
  98. else if ($bShowDoAsButtonSmall)
  99. $sClassDo = ' bx-btn bx-btn-small';
  100. $aTmplVarsAction = $aTmplVarsActions = [];
  101. if(count($aActions['items']) == 1) {
  102. $aAction = reset($aActions['items']);
  103. $aTmplVarsAction = array_merge([
  104. 'style_prefix' => $this->_sStylePrefix,
  105. 'class' => $sClassDo,
  106. 'object' => $this->_sObject,
  107. 'action' => $aAction['name'],
  108. 'profile_id' => $iContent,
  109. 'title' => $bShowDoLabel && !empty($aAction['title']) ? _t($aAction['title']) : ''
  110. ], $this->_getTmplVarsAction($aAction));
  111. }
  112. else {
  113. $sHtmlIdDoPopup = $this->_aHtmlIds['do_popup'] . $iContent;
  114. $sHtmlIdDoMenu = $this->_aHtmlIds['do_menu'] . $iContent;
  115. $aMenuItems = [];
  116. foreach($aActions['items'] as $aItem)
  117. if(!empty($aItem) && is_array($aItem)) {
  118. if(!isset($aItem['link'], $aItem['onclick']))
  119. $aMenuItems[] = [
  120. 'id' => $aItem['name'],
  121. 'name' => $aItem['name'],
  122. 'icon' => $this->_getActionIcon($aItem, $aParams),
  123. 'link' => 'javascript:void(0)',
  124. 'onclick' => "javascript:bx_conn_action(this, '" . $this->_sObject . "', '" . $aItem['name'] . "', " . $iContent . ")",
  125. 'target' => '_self',
  126. 'title' => !empty($aItem['title']) ? _t($aItem['title']) : '',
  127. 'active' => 1
  128. ];
  129. else
  130. $aMenuItems[] = $aItem;
  131. }
  132. $oMenu = new BxTemplMenu(['template' => 'menu_vertical.html', 'menu_id'=> $sHtmlIdDoMenu, 'menu_items' => $aMenuItems]);
  133. $aTmplVarsActions = array_merge([
  134. 'style_prefix' => $this->_sStylePrefix,
  135. 'class' => $sClassDo,
  136. 'html_id_popup' => $sHtmlIdDoPopup,
  137. 'do_popup' => $this->_oFunctions->transBox($sHtmlIdDoPopup, $oMenu->getCode(), true),
  138. ], $this->_getTmplVarsAction([
  139. 'name' => $aActions['name'],
  140. 'title' => $bShowDoLabel && !empty($aActions['title']) ? $aActions['title'] : ''
  141. ]));
  142. }
  143. $bTmplVarsAction = !empty($aTmplVarsAction) && is_array($aTmplVarsAction);
  144. $bTmplVarsActions = !empty($aTmplVarsActions) && is_array($aTmplVarsActions);
  145. return $this->_oTemplate->parseHtmlByContent($this->_getTmplContentElement(), [
  146. 'style_prefix' => $this->_sStylePrefix,
  147. 'html_id' => $this->_aHtmlIds['main'] . $iContent,
  148. 'class' => $this->_sStylePrefix . ($bShowDoAsButton ? '-button' : '-link') . ($bShowDoAsButtonSmall ? '-button-small' : ''),
  149. 'bx_if:show_do' => [
  150. 'condition' => $bShowDo && ($bTmplVarsAction || $bTmplVarsActions),
  151. 'content' => [
  152. 'style_prefix' => $this->_sStylePrefix,
  153. 'bx_if:show_action' => [
  154. 'condition' => $bTmplVarsAction,
  155. 'content' => $aTmplVarsAction
  156. ],
  157. 'bx_if:show_actions' => [
  158. 'condition' => $bTmplVarsActions,
  159. 'content' => $aTmplVarsActions
  160. ],
  161. ]
  162. ],
  163. 'bx_if:show_counter' => [
  164. 'condition' => $bShowCounter && $iCount,
  165. 'content' => [
  166. 'style_prefix' => $this->_sStylePrefix,
  167. 'counter' => $this->getCounter($iContent, $this->_bMutual, array_merge($aParams, ['count' => $iCount]))
  168. ]
  169. ],
  170. 'script' => $this->_getJsScript($iContent, $this->_bMutual, $aParams)
  171. ]);
  172. }
  173. public function getCounter($iProfileId, $bIsMutual = false, $aParams = [])
  174. {
  175. if(!$iProfileId)
  176. $iProfileId = bx_get_logged_profile_id();
  177. if($this->_aObject['type'] != 'mutual')
  178. $bIsMutual = false;
  179. $aParams = array_merge($this->_aElementDefaults, (!empty($aParams) ? $aParams : []));
  180. $iCount = !empty($aParams['count']) ? $aParams['count'] : $this->_getCounterCount($iProfileId, $bIsMutual, $aParams);
  181. if(!$iCount)
  182. return '';
  183. $sJsObject = $this->getJsObjectName($iProfileId);
  184. $sHtmlIdCounter = $this->_aHtmlIds['counter'] . $iProfileId;
  185. $bDynamicMode = isset($aParams['dynamic_mode']) && $aParams['dynamic_mode'] === true;
  186. $bShowDoAsButtonSmall = isset($aParams['show_do_as_button_small']) && $aParams['show_do_as_button_small'] === true;
  187. $bShowDoAsButton = !$bShowDoAsButtonSmall && isset($aParams['show_do_as_button']) && $aParams['show_do_as_button'] === true;
  188. $bShowScript = !isset($aParams['show_script']) || $aParams['show_script'] == true;
  189. $sClass = $this->_sStylePrefix . '-counter';
  190. if($bShowDoAsButtonSmall)
  191. $sClass .= ' bx-btn-small-height';
  192. if($bShowDoAsButton)
  193. $sClass .= ' bx-btn-height';
  194. $sContent = $this->_getCounterLabel($iCount, $iProfileId, $bIsMutual, $aParams);
  195. $this->_oTemplate->addCss(['connection.css']);
  196. return $this->_oTemplate->parseHtmlByContent($this->_getTmplContentCounter(), array(
  197. 'html_id' => $sHtmlIdCounter,
  198. 'style_prefix' => $this->_sStylePrefix,
  199. 'bx_if:show_text' => array(
  200. 'condition' => false,
  201. 'content' => array(
  202. 'class' => $sClass,
  203. 'bx_repeat:attrs' => array(
  204. array('key' => 'id', 'value' => $sHtmlIdCounter),
  205. ),
  206. 'content' => $sContent
  207. )
  208. ),
  209. 'bx_if:show_link' => array(
  210. 'condition' => true,
  211. 'content' => array(
  212. 'class' => $sClass,
  213. 'bx_repeat:attrs' => array(
  214. array('key' => 'id', 'value' => $sHtmlIdCounter),
  215. array('key' => 'href', 'value' => 'javascript:void(0)'),
  216. array('key' => 'onclick', 'value' => 'javascript:' . $sJsObject . '.toggleByPopup(this)'),
  217. array('key' => 'title', 'value' => _t('_sys_conn_do_by'))
  218. ),
  219. 'content' => $sContent
  220. )
  221. ),
  222. 'script' => $bShowScript ? $this->_getJsScript($iProfileId, $bIsMutual, $aParams) : ''
  223. ));
  224. }
  225. public function getCounterAPI($iProfileId, $bIsMutual = false, $aParams = [])
  226. {
  227. $sContentType = ($sKey = 'content_type') && isset($aParams[$sKey]) ? $aParams[$sKey] : BX_CONNECTIONS_CONTENT_TYPE_INITIATORS;
  228. $iCount = $this->{'getConnected' . ($sContentType == BX_CONNECTIONS_CONTENT_TYPE_INITIATORS ? 'Initiators' : 'Content') . 'Count'}($iProfileId, $bIsMutual);
  229. return [
  230. 'count' => $iCount,
  231. 'countf' => _t(isset($aParams['caption']) ? $aParams['caption'] : $this->_aT['counter'], $iCount)
  232. ];
  233. }
  234. public function getConnectedListAPI($iProfileId, $bIsMutual, $sContentType, $iCount = BX_CONNECTIONS_LIST_COUNTER)
  235. {
  236. $aProfiles = [];
  237. $aIds = $this->{BX_CONNECTIONS_CONTENT_TYPE_INITIATORS == $sContentType ? 'getConnectedInitiators' : 'getConnectedContent'}($iProfileId, $bIsMutual, 0, $iCount);
  238. foreach($aIds as $iId)
  239. if(($oProfile = BxDolProfile::getInstanceMagic($iId)) !== false)
  240. $aProfiles[] = $oProfile->getUnitAPI(0, ['template' => 'unit_wo_info']);
  241. return $aProfiles;
  242. }
  243. public function getCommonListAPI($iProfileId1, $iProfileId2, $bIsMutual, $iCount = BX_CONNECTIONS_LIST_COUNTER)
  244. {
  245. $aProfiles = [];
  246. $aIds = $this->getCommonContent($iProfileId1, $iProfileId2, $bIsMutual, 0, $iCount);
  247. foreach($aIds as $iId)
  248. if(($oProfile = BxDolProfile::getInstanceMagic($iId)) !== false)
  249. $aProfiles[] = $oProfile->getUnitAPI(0, ['template' => 'unit_wo_info']);
  250. return $aProfiles;
  251. }
  252. protected function _getJsScript($iProfileId, $bIsMutual = false, $aParams = [])
  253. {
  254. $sJsObject = $this->getJsObjectName($iProfileId);
  255. $aParamsJs = [
  256. 'sSystem' => $this->_sObject,
  257. 'iObjId' => $iProfileId,
  258. 'bIsMutual' => $bIsMutual ? 1 : 0,
  259. 'sObjName' => $sJsObject,
  260. 'sRootUrl' => BX_DOL_URL_ROOT,
  261. 'sStylePrefix' => $this->_sStylePrefix,
  262. 'aHtmlIds' => $this->_aHtmlIds
  263. ];
  264. if(($sKey = 'content_type') && !empty($aParams[$sKey]))
  265. $aParamsJs['sContentType'] = $aParams[$sKey];
  266. $sCode = "if(window['" . $sJsObject . "'] == undefined) var " . $sJsObject . " = new BxDolConnection(" . json_encode($aParamsJs) . ");";
  267. return $this->_oTemplate->_wrapInTagJsCode($sCode);
  268. }
  269. protected function _getCounterCount($iProfileId, $bIsMutual, $aParams = [])
  270. {
  271. $sContentType = ($sKey = 'content_type') && isset($aParams[$sKey]) ? $aParams[$sKey] : BX_CONNECTIONS_CONTENT_TYPE_INITIATORS;
  272. return $this->{'getConnected' . ($sContentType == BX_CONNECTIONS_CONTENT_TYPE_INITIATORS ? 'Initiators' : 'Content') . 'Count'}($iProfileId, $bIsMutual);
  273. }
  274. protected function _getCounterLabel($iCount, $iProfileId, $bIsMutual, $aParams)
  275. {
  276. if ('mutual' != $this->_aObject['type'])
  277. $bIsMutual = false;
  278. $sContentType = ($sKey = 'content_type') && isset($aParams[$sKey]) ? $aParams[$sKey] : BX_CONNECTIONS_CONTENT_TYPE_INITIATORS;
  279. $bShowWithProfiles = isset($aParams['show_counter_label_with_profiles']) && $aParams['show_counter_label_with_profiles'] === true;
  280. $bShowWithIcon = (!isset($aParams['show_counter_label_icon']) || $aParams['show_counter_label_icon'] === true) && !$bShowWithProfiles;
  281. $aTmplVarsWithProfiles = [];
  282. if($bShowWithProfiles) {
  283. $aTmplVarsWithProfiles = [
  284. 'style_prefix' => $this->_sStylePrefix,
  285. 'bx_repeat:profiles' => []
  286. ];
  287. $aIds = $this->{'getConnected' . (BX_CONNECTIONS_CONTENT_TYPE_INITIATORS == $sContentType ? 'Initiators' : 'Content')}($iProfileId, $bIsMutual, 0, BX_CONNECTIONS_LIST_COUNTER);
  288. foreach($aIds as $iId)
  289. if(($oProfile = BxDolProfile::getInstanceMagic($iId)) !== false)
  290. $aTmplVarsWithProfiles['bx_repeat:profiles'][] = [
  291. 'style_prefix' => $this->_sStylePrefix,
  292. 'unit' => $oProfile->getUnit(0, ['template' => ['name' => 'unit_wo_info_links', 'size' => 'icon']])
  293. ];
  294. }
  295. return $this->_oTemplate->parseHtmlByName('connection_counter_label.html', array(
  296. 'style_prefix' => $this->_sStylePrefix,
  297. 'bx_if:show_icon' => array(
  298. 'condition' => $bShowWithIcon,
  299. 'content' => array(
  300. 'style_prefix' => $this->_sStylePrefix,
  301. 'name' => isset($aParams['custom_icon']) && $aParams['custom_icon'] != '' ? $aParams['custom_icon'] : $this->_oFunctions->getFontIconAsHtml($this->_getIconDo())
  302. )
  303. ),
  304. 'bx_if:show_profiles' => array(
  305. 'condition' => $bShowWithProfiles,
  306. 'content' => $aTmplVarsWithProfiles
  307. ),
  308. 'bx_if:show_text' => array(
  309. 'condition' => !isset($aParams['show_counter_label_text']) || $aParams['show_counter_label_text'] === true,
  310. 'content' => array(
  311. 'style_prefix' => $this->_sStylePrefix,
  312. 'text' => _t(isset($aParams['caption']) ? $aParams['caption'] : $this->_aT['counter'], $iCount)
  313. )
  314. )
  315. ));
  316. }
  317. protected function _getIconDo()
  318. {
  319. if ($this->_sObject == 'sys_profiles_subscriptions')
  320. return 'user-friends';
  321. if (strpos($this->_sObject, 'fan'))
  322. return 'users';
  323. }
  324. public function _getConnected ($sContentType, $iProfileId, $bIsMutual, $iStart = 0, $iPerPage = 0)
  325. {
  326. if ('mutual' != $this->_aObject['type'])
  327. $bIsMutual = false;
  328. if(empty($iPerPage))
  329. $iPerPage = $this->_aObject['per_page_default'];
  330. $aUsers = $this->getConnectionsAsArray($sContentType, $iProfileId, null, $bIsMutual, $iStart, $iPerPage + 1);
  331. $oPaginate = new BxTemplPaginate([
  332. 'on_change_page' => $this->getJsObjectName($iProfileId) . '.getUsers(this, {start}, {per_page})',
  333. 'start' => $iStart,
  334. 'per_page' => $iPerPage,
  335. ]);
  336. $oPaginate->setNumFromDataArray($aUsers);
  337. foreach($aUsers as $iProfile) {
  338. $oProfile = BxDolProfile::getInstanceMagic($iProfile);
  339. $aTmplUsers[] = [
  340. 'style_prefix' => $this->_sStylePrefix,
  341. 'user_unit' => $oProfile->getUnit(0, ['template' => 'unit_wo_info']),
  342. 'user_url' => $oProfile->getUrl(),
  343. 'user_title' => bx_html_attribute($oProfile->getDisplayName()),
  344. 'user_name' => $oProfile->getDisplayName()
  345. ];
  346. }
  347. if(empty($aTmplUsers))
  348. $aTmplUsers = MsgBox(_t('_Empty'));
  349. return $this->_oTemplate->parseHtmlByName('connected_by_list.html', [
  350. 'style_prefix' => $this->_sStylePrefix,
  351. 'bx_repeat:list' => $aTmplUsers,
  352. 'paginate' => $oPaginate->getSimplePaginate()
  353. ]);
  354. }
  355. protected function _getAuthorInfo($iAuthorId = 0)
  356. {
  357. $oProfile = $this->_getAuthorObject($iAuthorId);
  358. return array(
  359. $oProfile->getDisplayName(),
  360. $oProfile->getUrl(),
  361. $oProfile->getThumb(),
  362. $oProfile->getUnit(),
  363. $oProfile->getUnit(0, array('template' => 'unit_wo_info'))
  364. );
  365. }
  366. protected function _getActions($iInitiator, $iContent, $aParams = [])
  367. {
  368. return [];
  369. }
  370. protected function _getActionIcon($aAction, $aParams = [])
  371. {
  372. return !empty($aAction['name']) ? $this->{'_getActionIconAs' . bx_gen_method_name($this->_useIconAs($aParams))}($aAction['name']) : '';
  373. }
  374. protected function _useIconAs($aParams = [])
  375. {
  376. if(!empty($aParams['use_icon_as']) && in_array($aParams['use_icon_as'], ['icon', 'emoji', 'image']))
  377. return $aParams['use_icon_as'];
  378. return 'icon';
  379. }
  380. protected function _getTmplContentElement()
  381. {
  382. return $this->_sTmplContentElement;
  383. }
  384. protected function _getTmplContentCounter()
  385. {
  386. return $this->_sTmplContentCounter;
  387. }
  388. protected function _getTmplVarsAction($aAction, $aParams = [])
  389. {
  390. $sIcon = $this->_getActionIcon($aAction, $aParams);
  391. return [
  392. 'bx_if:show_icon' => [
  393. 'condition' => !empty($sIcon),
  394. 'content' => [
  395. 'style_prefix' => $this->_sStylePrefix,
  396. 'icon' => $this->_oTemplate->getImageAuto($sIcon)
  397. ]
  398. ],
  399. 'bx_if:show_text' => [
  400. 'condition' => !empty($aAction['title']),
  401. 'content' => [
  402. 'style_prefix' => $this->_sStylePrefix,
  403. 'text' => _t($aAction['title'])
  404. ]
  405. ]
  406. ];
  407. }
  408. }