BxBaseConnection.php 19 KB

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