BxBaseConnection.php 8.8 KB

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