1
0

BxBaseConnection.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 $_sStylePrefix;
  15. protected $_sJsObjName;
  16. protected $_aHtmlIds;
  17. protected $_oTemplate;
  18. public function __construct($aObject)
  19. {
  20. parent::__construct($aObject);
  21. $this->_sStylePrefix = 'bx-conn';
  22. $this->_sJsObjName = 'oConn' . bx_gen_method_name($this->_sObject, array('_' , '-')) . '';
  23. $this->_oTemplate = BxDolTemplate::getInstance();
  24. }
  25. public function actionGetConnected ($iStart = 0, $iPerPage = 0)
  26. {
  27. $sContentType = bx_process_input(bx_get('content_type'));
  28. $iProfileId = (int)bx_get('id');
  29. $bIsMutual = (bool)bx_get('mutual');
  30. return $this->_getConnected ($sContentType, $iProfileId, $bIsMutual, $iStart, $iPerPage);
  31. }
  32. public function actionGetUsers ($iStart = 0, $iPerPage = 0)
  33. {
  34. $sContentType = bx_process_input(bx_get('content_type'));
  35. $iProfileId = (int)bx_get('id');
  36. $bIsMutual = (bool)bx_get('mutual');
  37. $iStart = (int)bx_get('start');
  38. $iPerPage = (int)bx_get('per_page');
  39. return [
  40. 'content' => $this->_getConnected($sContentType, $iProfileId, $bIsMutual, $iStart, $iPerPage),
  41. 'eval' => $this->getJsObjectName($iProfileId) . '.onGetUsers(oData)'
  42. ];
  43. }
  44. public function getJsObjectName($iProfileId)
  45. {
  46. return $this->_sJsObjName . $iProfileId;
  47. }
  48. public function getCounter ($iProfileId, $bIsMutual, $aParams, $sContentType)
  49. {
  50. if (!$iProfileId)
  51. $iProfileId = bx_get_logged_profile_id();
  52. if ('mutual' != $this->_aObject['type'])
  53. $bIsMutual = false;
  54. if(empty($aParams))
  55. $aParams = [];
  56. $this->_sStylePrefix = 'bx-conn';
  57. $aHtmlIds = array(
  58. 'main' => 'bx-conn-' . $this->_sObject . $iProfileId,
  59. 'counter' => 'bx-conn-counter-' . $this->_sObject . $iProfileId,
  60. 'by_popup' => 'bx-conn-by-popup-' . $this->_sObject . $iProfileId,
  61. );
  62. $aParamsDefault = [
  63. 'dynamic_mode' => true,
  64. 'show_do_as_button_small' => true,
  65. 'show_do_as_button' => true,
  66. 'show_counter_label_with_profiles' => true,
  67. 'show_script' => true,
  68. ];
  69. $aParams = array_merge($aParams, $aParamsDefault);
  70. $iCount = $sContentType == BX_CONNECTIONS_CONTENT_TYPE_INITIATORS ? $this->getConnectedInitiatorsCount($iProfileId, $bIsMutual) : $this->getConnectedContentCount($iProfileId, $bIsMutual);
  71. if ($iCount > 0){
  72. $sJsObject = $this->getJsObjectName($iProfileId);
  73. $bDynamicMode = isset($aParams['dynamic_mode']) && $aParams['dynamic_mode'] === true;
  74. $bShowDoAsButtonSmall = isset($aParams['show_do_as_button_small']) && $aParams['show_do_as_button_small'] == false;
  75. $bShowDoAsButton = !$bShowDoAsButtonSmall && isset($aParams['show_do_as_button']) && $aParams['show_do_as_button'] == true;
  76. $bShowScript = !isset($aParams['show_script']) || $aParams['show_script'] == true;
  77. $sClass = $this->_sStylePrefix . '-counter';
  78. if($bShowDoAsButtonSmall)
  79. $sClass .= ' bx-btn-small-height';
  80. if($bShowDoAsButton)
  81. $sClass .= ' bx-btn-height';
  82. $sContent = $this->_getCounterLabel($iCount, $iProfileId, $bIsMutual, $aParams, $sContentType);
  83. $this->_oTemplate->addCss(array('connection.css'));
  84. return $this->_oTemplate->parseHtmlByName('connection_counter.html', array(
  85. 'html_id' => $aHtmlIds['counter'],
  86. 'style_prefix' => $this->_sStylePrefix,
  87. 'bx_if:show_text' => array(
  88. 'condition' => false,
  89. 'content' => array(
  90. 'class' => $sClass,
  91. 'bx_repeat:attrs' => array(
  92. array('key' => 'id', 'value' => $aHtmlIds['counter']),
  93. ),
  94. 'content' => $sContent
  95. )
  96. ),
  97. 'bx_if:show_link' => array(
  98. 'condition' => true,
  99. 'content' => array(
  100. 'class' => $sClass,
  101. 'bx_repeat:attrs' => array(
  102. array('key' => 'id', 'value' => $aHtmlIds['counter']),
  103. array('key' => 'href', 'value' => 'javascript:void(0)'),
  104. array('key' => 'onclick', 'value' => 'javascript:' . $sJsObject . '.toggleByPopup(this)'),
  105. array('key' => 'title', 'value' => _t('_view_do_view_by'))
  106. ),
  107. 'content' => $sContent
  108. )
  109. ),
  110. 'script' => $bShowScript ? $this->_getJsScript($iProfileId, $sContentType, $bIsMutual, $aHtmlIds, $bDynamicMode) : ''
  111. ));
  112. }
  113. }
  114. protected function _getJsScript($iProfileId, $sContentType, $bIsMutual, $aHtmlIds, $bDynamicMode = false)
  115. {
  116. $sJsObject = $this->getJsObjectName($iProfileId);
  117. $aParamsJs = array(
  118. 'sObjName' => $sJsObject,
  119. 'sSystem' => $this->_sObject,
  120. 'iObjId' => $iProfileId,
  121. 'sContentType' => $sContentType,
  122. 'bIsMutual' => $bIsMutual ? 1 : 0,
  123. 'sRootUrl' => BX_DOL_URL_ROOT,
  124. 'sStylePrefix' => $this->_sStylePrefix,
  125. 'aHtmlIds' => $aHtmlIds
  126. );
  127. $sCode = "if(window['" . $sJsObject . "'] == undefined) var " . $sJsObject . " = new BxDolConnection(" . json_encode($aParamsJs) . ");";
  128. return $this->_oTemplate->_wrapInTagJsCode($sCode);
  129. }
  130. protected function _getCounterLabel($iCount, $iProfileId, $bIsMutual, $aParams, $sContentType)
  131. {
  132. if ('mutual' != $this->_aObject['type'])
  133. $bIsMutual = false;
  134. $bShowWithProfiles = isset($aParams['show_counter_label_with_profiles']) && $aParams['show_counter_label_with_profiles'] === true;
  135. $bShowWithIcon = (!isset($aParams['show_counter_label_icon']) || $aParams['show_counter_label_icon'] === true) && !$bShowWithProfiles;
  136. $aTmplVarsWithProfiles = array();
  137. if($bShowWithProfiles) {
  138. $aTmplVarsWithProfiles = [
  139. 'style_prefix' => $this->_sStylePrefix,
  140. 'bx_repeat:profiles' => []
  141. ];
  142. $aIds = $this->{BX_CONNECTIONS_CONTENT_TYPE_INITIATORS == $sContentType ? 'getConnectedInitiators' : 'getConnectedContent'}($iProfileId, $bIsMutual, 0, BX_CONNECTIONS_LIST_COUNTER);
  143. foreach($aIds as $iId)
  144. if(($oProfile = BxDolProfile::getInstanceMagic($iId)) !== false)
  145. $aTmplVarsWithProfiles['bx_repeat:profiles'][] = [
  146. 'style_prefix' => $this->_sStylePrefix,
  147. 'unit' => $oProfile->getUnit(0, ['template' => ['name' => 'unit_wo_info_links', 'size' => 'icon']])
  148. ];
  149. }
  150. return $this->_oTemplate->parseHtmlByName('connection_counter_label.html', array(
  151. 'style_prefix' => $this->_sStylePrefix,
  152. 'bx_if:show_icon' => array(
  153. 'condition' => $bShowWithIcon,
  154. 'content' => array(
  155. 'style_prefix' => $this->_sStylePrefix,
  156. 'name' => isset($aParams['custom_icon']) && $aParams['custom_icon'] != '' ? $aParams['custom_icon'] : BxTemplFunctions::getInstanceWithTemplate($this->_oTemplate)->getFontIconAsHtml($this->_getIconDo())
  157. )
  158. ),
  159. 'bx_if:show_profiles' => array(
  160. 'condition' => $bShowWithProfiles,
  161. 'content' => $aTmplVarsWithProfiles
  162. ),
  163. 'bx_if:show_text' => array(
  164. 'condition' => !isset($aParams['show_counter_label_text']) || $aParams['show_counter_label_text'] === true,
  165. 'content' => array(
  166. 'style_prefix' => $this->_sStylePrefix,
  167. 'text' => _t(isset($aParams['caption']) ? $aParams['caption'] : '_view_counter', $iCount)
  168. )
  169. )
  170. ));
  171. }
  172. protected function _getIconDo()
  173. {
  174. if ($this->_sObject == 'sys_profiles_subscriptions')
  175. return 'user-friends';
  176. if (strpos($this->_sObject, 'fan'))
  177. return 'users';
  178. }
  179. public function _getConnected ($sContentType, $iProfileId, $bIsMutual, $iStart = 0, $iPerPage = 0)
  180. {
  181. if ('mutual' != $this->_aObject['type'])
  182. $bIsMutual = false;
  183. if(empty($iPerPage))
  184. $iPerPage = $this->_aObject['per_page_default'];
  185. $aUsers = $this->getConnectionsAsArray($sContentType, $iProfileId, null, $bIsMutual, $iStart, $iPerPage + 1);
  186. $oPaginate = new BxTemplPaginate([
  187. 'on_change_page' => $this->getJsObjectName($iProfileId) . '.getUsers(this, {start}, {per_page})',
  188. 'start' => $iStart,
  189. 'per_page' => $iPerPage,
  190. ]);
  191. $oPaginate->setNumFromDataArray($aUsers);
  192. foreach($aUsers as $iProfile) {
  193. $oProfile = BxDolProfile::getInstanceMagic($iProfile);
  194. $aTmplUsers[] = [
  195. 'style_prefix' => $this->_sStylePrefix,
  196. 'user_unit' => $oProfile->getUnit(0, ['template' => 'unit_wo_info']),
  197. 'user_url' => $oProfile->getUrl(),
  198. 'user_title' => bx_html_attribute($oProfile->getDisplayName()),
  199. 'user_name' => $oProfile->getDisplayName()
  200. ];
  201. }
  202. if(empty($aTmplUsers))
  203. $aTmplUsers = MsgBox(_t('_Empty'));
  204. return $this->_oTemplate->parseHtmlByName('connected_by_list.html', [
  205. 'style_prefix' => $this->_sStylePrefix,
  206. 'bx_repeat:list' => $aTmplUsers,
  207. 'paginate' => $oPaginate->getSimplePaginate()
  208. ]);
  209. }
  210. protected function _getAuthorInfo($iAuthorId = 0)
  211. {
  212. $oProfile = $this->_getAuthorObject($iAuthorId);
  213. return array(
  214. $oProfile->getDisplayName(),
  215. $oProfile->getUrl(),
  216. $oProfile->getThumb(),
  217. $oProfile->getUnit(),
  218. $oProfile->getUnit(0, array('template' => 'unit_wo_info'))
  219. );
  220. }
  221. }