1
0

BxBaseConnection.php 10 KB

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