1
0

BxBaseConnection.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. /**
  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' => false,
  65. 'show_do_as_button' => false,
  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'] === true;
  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. public function getCounterAPI($iProfileId, $bIsMutual, $aParams, $sContentType)
  115. {
  116. $iCount = $this->{'getConnected' . ($sContentType == BX_CONNECTIONS_CONTENT_TYPE_INITIATORS ? 'Initiators' : 'Content') . 'Count'}($iProfileId, $bIsMutual);
  117. return [
  118. 'count' => $iCount,
  119. 'countf' => _t(isset($aParams['caption']) ? $aParams['caption'] : '_view_counter', $iCount)
  120. ];
  121. }
  122. protected function _getJsScript($iProfileId, $sContentType, $bIsMutual, $aHtmlIds, $bDynamicMode = false)
  123. {
  124. $sJsObject = $this->getJsObjectName($iProfileId);
  125. $aParamsJs = array(
  126. 'sObjName' => $sJsObject,
  127. 'sSystem' => $this->_sObject,
  128. 'iObjId' => $iProfileId,
  129. 'sContentType' => $sContentType,
  130. 'bIsMutual' => $bIsMutual ? 1 : 0,
  131. 'sRootUrl' => BX_DOL_URL_ROOT,
  132. 'sStylePrefix' => $this->_sStylePrefix,
  133. 'aHtmlIds' => $aHtmlIds
  134. );
  135. $sCode = "if(window['" . $sJsObject . "'] == undefined) var " . $sJsObject . " = new BxDolConnection(" . json_encode($aParamsJs) . ");";
  136. return $this->_oTemplate->_wrapInTagJsCode($sCode);
  137. }
  138. protected function _getCounterLabel($iCount, $iProfileId, $bIsMutual, $aParams, $sContentType)
  139. {
  140. if ('mutual' != $this->_aObject['type'])
  141. $bIsMutual = false;
  142. $bShowWithProfiles = isset($aParams['show_counter_label_with_profiles']) && $aParams['show_counter_label_with_profiles'] === true;
  143. $bShowWithIcon = (!isset($aParams['show_counter_label_icon']) || $aParams['show_counter_label_icon'] === true) && !$bShowWithProfiles;
  144. $aTmplVarsWithProfiles = array();
  145. if($bShowWithProfiles) {
  146. $aTmplVarsWithProfiles = [
  147. 'style_prefix' => $this->_sStylePrefix,
  148. 'bx_repeat:profiles' => []
  149. ];
  150. $aIds = $this->{BX_CONNECTIONS_CONTENT_TYPE_INITIATORS == $sContentType ? 'getConnectedInitiators' : 'getConnectedContent'}($iProfileId, $bIsMutual, 0, BX_CONNECTIONS_LIST_COUNTER);
  151. foreach($aIds as $iId)
  152. if(($oProfile = BxDolProfile::getInstanceMagic($iId)) !== false)
  153. $aTmplVarsWithProfiles['bx_repeat:profiles'][] = [
  154. 'style_prefix' => $this->_sStylePrefix,
  155. 'unit' => $oProfile->getUnit(0, ['template' => ['name' => 'unit_wo_info_links', 'size' => 'icon']])
  156. ];
  157. }
  158. return $this->_oTemplate->parseHtmlByName('connection_counter_label.html', array(
  159. 'style_prefix' => $this->_sStylePrefix,
  160. 'bx_if:show_icon' => array(
  161. 'condition' => $bShowWithIcon,
  162. 'content' => array(
  163. 'style_prefix' => $this->_sStylePrefix,
  164. 'name' => isset($aParams['custom_icon']) && $aParams['custom_icon'] != '' ? $aParams['custom_icon'] : BxTemplFunctions::getInstanceWithTemplate($this->_oTemplate)->getFontIconAsHtml($this->_getIconDo())
  165. )
  166. ),
  167. 'bx_if:show_profiles' => array(
  168. 'condition' => $bShowWithProfiles,
  169. 'content' => $aTmplVarsWithProfiles
  170. ),
  171. 'bx_if:show_text' => array(
  172. 'condition' => !isset($aParams['show_counter_label_text']) || $aParams['show_counter_label_text'] === true,
  173. 'content' => array(
  174. 'style_prefix' => $this->_sStylePrefix,
  175. 'text' => _t(isset($aParams['caption']) ? $aParams['caption'] : '_view_counter', $iCount)
  176. )
  177. )
  178. ));
  179. }
  180. protected function _getIconDo()
  181. {
  182. if ($this->_sObject == 'sys_profiles_subscriptions')
  183. return 'user-friends';
  184. if (strpos($this->_sObject, 'fan'))
  185. return 'users';
  186. }
  187. public function _getConnected ($sContentType, $iProfileId, $bIsMutual, $iStart = 0, $iPerPage = 0)
  188. {
  189. if ('mutual' != $this->_aObject['type'])
  190. $bIsMutual = false;
  191. if(empty($iPerPage))
  192. $iPerPage = $this->_aObject['per_page_default'];
  193. $aUsers = $this->getConnectionsAsArray($sContentType, $iProfileId, null, $bIsMutual, $iStart, $iPerPage + 1);
  194. $oPaginate = new BxTemplPaginate([
  195. 'on_change_page' => $this->getJsObjectName($iProfileId) . '.getUsers(this, {start}, {per_page})',
  196. 'start' => $iStart,
  197. 'per_page' => $iPerPage,
  198. ]);
  199. $oPaginate->setNumFromDataArray($aUsers);
  200. foreach($aUsers as $iProfile) {
  201. $oProfile = BxDolProfile::getInstanceMagic($iProfile);
  202. $aTmplUsers[] = [
  203. 'style_prefix' => $this->_sStylePrefix,
  204. 'user_unit' => $oProfile->getUnit(0, ['template' => 'unit_wo_info']),
  205. 'user_url' => $oProfile->getUrl(),
  206. 'user_title' => bx_html_attribute($oProfile->getDisplayName()),
  207. 'user_name' => $oProfile->getDisplayName()
  208. ];
  209. }
  210. if(empty($aTmplUsers))
  211. $aTmplUsers = MsgBox(_t('_Empty'));
  212. return $this->_oTemplate->parseHtmlByName('connected_by_list.html', [
  213. 'style_prefix' => $this->_sStylePrefix,
  214. 'bx_repeat:list' => $aTmplUsers,
  215. 'paginate' => $oPaginate->getSimplePaginate()
  216. ]);
  217. }
  218. protected function _getAuthorInfo($iAuthorId = 0)
  219. {
  220. $oProfile = $this->_getAuthorObject($iAuthorId);
  221. return array(
  222. $oProfile->getDisplayName(),
  223. $oProfile->getUrl(),
  224. $oProfile->getThumb(),
  225. $oProfile->getUnit(),
  226. $oProfile->getUnit(0, array('template' => 'unit_wo_info'))
  227. );
  228. }
  229. }