BxBaseFavorite.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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 BxDolFavorite
  11. */
  12. class BxBaseFavorite extends BxDolFavorite
  13. {
  14. protected $_bCssJsAdded;
  15. protected $_sJsObjClass;
  16. protected $_sJsObjName;
  17. protected $_sStylePrefix;
  18. protected $_aHtmlIds;
  19. protected $_aElementDefaults;
  20. public function __construct($sSystem, $iId, $iInit = 1, $oTemplate = false)
  21. {
  22. parent::__construct($sSystem, $iId, $iInit, $oTemplate);
  23. $this->_bCssJsAdded = false;
  24. $this->_sJsObjClass = 'BxDolFavorite';
  25. $this->_sJsObjName = 'oFavorite' . bx_gen_method_name($sSystem, array('_' , '-')) . $iId;
  26. $this->_sStylePrefix = 'bx-favorite';
  27. $sHtmlId = str_replace(array('_' , ' '), array('-', '-'), $sSystem) . '-' . $iId;
  28. $this->_aHtmlIds = array(
  29. 'main' => 'bx-favorite-' . $sHtmlId,
  30. 'counter' => 'bx-favorite-counter-' . $sHtmlId,
  31. 'do_link' => 'bx-favorite-do-link-' . $sHtmlId,
  32. 'by_popup' => 'bx-favorite-by-popup-' . $sHtmlId,
  33. 'do_popup' => 'bx-favorite-do-popup-' . $sHtmlId,
  34. 'do_form' => 'bx-favorite-do-form-' . $sHtmlId,
  35. );
  36. $this->_aElementDefaults = array(
  37. 'show_do_favorite_as_button' => false,
  38. 'show_do_favorite_as_button_small' => false,
  39. 'show_do_favorite_icon' => true,
  40. 'show_do_favorite_label' => false,
  41. 'show_counter' => true
  42. );
  43. $this->_sTmplContentElementBlock = $this->_oTemplate->getHtml('favorite_element_block.html');
  44. $this->_sTmplContentElementInline = $this->_oTemplate->getHtml('favorite_element_inline.html');
  45. $this->_sTmplContentDoActionLabel = $this->_oTemplate->getHtml('favorite_do_favorite_label.html');
  46. }
  47. public function getJsObjectName()
  48. {
  49. return $this->_sJsObjName;
  50. }
  51. public function getJsScript($bDynamicMode = false)
  52. {
  53. $aParams = array(
  54. 'sObjName' => $this->_sJsObjName,
  55. 'sSystem' => $this->getSystemName(),
  56. 'iAuthorId' => $this->_getAuthorId(),
  57. 'iObjId' => $this->getId(),
  58. 'sRootUrl' => BX_DOL_URL_ROOT,
  59. 'sStylePrefix' => $this->_sStylePrefix,
  60. 'aHtmlIds' => $this->_aHtmlIds
  61. );
  62. $sCode = "var " . $this->_sJsObjName . " = new " . $this->_sJsObjClass . "(" . json_encode($aParams) . ");";
  63. return $this->_oTemplate->_wrapInTagJsCode($sCode);
  64. }
  65. public function getJsClick()
  66. {
  67. return $this->getJsObjectName() . '.favorite(this)';
  68. }
  69. public function getCounter($aParams = array())
  70. {
  71. $bShowDoFavoriteAsButtonSmall = isset($aParams['show_do_favorite_as_button_small']) && $aParams['show_do_favorite_as_button_small'] == true;
  72. $bShowDoFavoriteAsButton = !$bShowDoFavoriteAsButtonSmall && isset($aParams['show_do_favorite_as_button']) && $aParams['show_do_favorite_as_button'] == true;
  73. $aFavorite = $this->_oQuery->getFavorite($this->getId());
  74. $sClass = $this->_sStylePrefix . '-counter';
  75. if($bShowDoFavoriteAsButtonSmall)
  76. $sClass .= ' bx-btn-small-height';
  77. if($bShowDoFavoriteAsButton)
  78. $sClass .= ' bx-btn-height';
  79. return $this->_oTemplate->parseLink('javascript:void(0)', (int)$aFavorite['count'] > 0 ? $this->_getCounterLabel($aFavorite['count']) : '', array(
  80. 'title' => _t('_favorite_do_favorite_by'),
  81. 'id' => $this->_aHtmlIds['counter'],
  82. 'class' => $sClass,
  83. 'onclick' => 'javascript:' . $this->getJsObjectName() . '.toggleByPopup(this)'
  84. ));
  85. }
  86. public function getElementBlock($aParams = array())
  87. {
  88. $aParams['usage'] = BX_DOL_FAVORITE_USAGE_BLOCK;
  89. return $this->getElement($aParams);
  90. }
  91. public function getElementInline($aParams = array())
  92. {
  93. $aParams['usage'] = BX_DOL_FAVORITE_USAGE_INLINE;
  94. return $this->getElement($aParams);
  95. }
  96. public function getElement($aParams = array())
  97. {
  98. $aParams = array_merge($this->_aElementDefaults, $aParams);
  99. $bDynamicMode = isset($aParams['dynamic_mode']) && $aParams['dynamic_mode'] === true;
  100. $bShowDoFavoriteAsButtonSmall = isset($aParams['show_do_favorite_as_button_small']) && $aParams['show_do_favorite_as_button_small'] == true;
  101. $bShowDoFavoriteAsButton = !$bShowDoFavoriteAsButtonSmall && isset($aParams['show_do_favorite_as_button']) && $aParams['show_do_favorite_as_button'] == true;
  102. $bShowCounter = isset($aParams['show_counter']) && $aParams['show_counter'] === true && $this->isAllowedFavoriteView();
  103. $iObjectId = $this->getId();
  104. $iAuthorId = $this->_getAuthorId();
  105. $aFavorite = $this->_oQuery->getFavorite($iObjectId);
  106. $bCount = (int)$aFavorite['count'] != 0;
  107. $bAllowedFavorite = $this->isAllowedFavorite();
  108. if(!$bAllowedFavorite && (!$this->isAllowedFavoriteView() || !$bCount))
  109. return '';
  110. $aParams['is_favorited'] = $this->isPerformed($iObjectId, $iAuthorId) ? true : false;
  111. $sTmplName = $this->{'_getTmplContentElement' . bx_gen_method_name(!empty($aParams['usage']) ? $aParams['usage'] : BX_DOL_FAVORITE_USAGE_DEFAULT)}();
  112. return $this->_oTemplate->parseHtmlByContent($sTmplName, array(
  113. 'style_prefix' => $this->_sStylePrefix,
  114. 'html_id' => $this->_aHtmlIds['main'],
  115. 'class' => $this->_sStylePrefix . ($bShowDoFavoriteAsButton ? '-button' : '-link') . ($bShowDoFavoriteAsButtonSmall ? '-button-small' : ''),
  116. 'count' => $aFavorite['count'],
  117. 'do_favorite' => $this->_getDoFavorite($aParams, $bAllowedFavorite),
  118. 'bx_if:show_counter' => array(
  119. 'condition' => $bShowCounter,
  120. 'content' => array(
  121. 'style_prefix' => $this->_sStylePrefix,
  122. 'bx_if:show_hidden' => array(
  123. 'condition' => !$bCount,
  124. 'content' => array()
  125. ),
  126. 'counter' => $this->getCounter($aParams)
  127. )
  128. ),
  129. 'script' => $this->getJsScript($bDynamicMode)
  130. ));
  131. }
  132. protected function _getDoFavorite($aParams = array(), $bAllowedFavorite = true)
  133. {
  134. $bFavorited = isset($aParams['is_favorited']) && $aParams['is_favorited'] === true;
  135. $bShowDoFavoriteAsButtonSmall = isset($aParams['show_do_favorite_as_button_small']) && $aParams['show_do_favorite_as_button_small'] == true;
  136. $bShowDoFavoriteAsButton = !$bShowDoFavoriteAsButtonSmall && isset($aParams['show_do_favorite_as_button']) && $aParams['show_do_favorite_as_button'] == true;
  137. $bDisabled = !$bAllowedFavorite || ($bFavorited && !$this->isUndo());
  138. $sClass = '';
  139. if($bShowDoFavoriteAsButton)
  140. $sClass = 'bx-btn';
  141. else if ($bShowDoFavoriteAsButtonSmall)
  142. $sClass = 'bx-btn bx-btn-small';
  143. if($bDisabled)
  144. $sClass .= $bShowDoFavoriteAsButton || $bShowDoFavoriteAsButtonSmall ? ' bx-btn-disabled' : 'bx-favorite-disabled';
  145. return $this->_oTemplate->parseLink('javascript:void(0)', $this->_getLabelDoFavorite($aParams), array(
  146. 'id' => $this->_aHtmlIds['do_link'],
  147. 'class' => $this->_sStylePrefix . '-do-favorite ' . $sClass,
  148. 'title' => _t($this->_getTitleDoFavorite($bFavorited)),
  149. 'onclick' => !$bDisabled ? $this->getJsClick() : ''
  150. ));
  151. }
  152. protected function _getCounterLabel($iCount)
  153. {
  154. return _t('_favorite_counter', $iCount);
  155. }
  156. protected function _getLabelDoFavorite($aParams = array())
  157. {
  158. $bFavorited = isset($aParams['is_favorited']) && $aParams['is_favorited'] === true;
  159. return $this->_oTemplate->parseHtmlByContent($this->_getTmplContentDoActionLabel(), array(
  160. 'style_prefix' => $this->_sStylePrefix,
  161. 'bx_if:show_icon' => array(
  162. 'condition' => isset($aParams['show_do_favorite_icon']) && $aParams['show_do_favorite_icon'] == true,
  163. 'content' => array(
  164. 'style_prefix' => $this->_sStylePrefix,
  165. 'name' => $this->_getIconDoFavorite($bFavorited)
  166. )
  167. ),
  168. 'bx_if:show_text' => array(
  169. 'condition' => isset($aParams['show_do_favorite_label']) && $aParams['show_do_favorite_label'] == true,
  170. 'content' => array(
  171. 'style_prefix' => $this->_sStylePrefix,
  172. 'text' => _t($this->_getTitleDoFavorite($bFavorited))
  173. )
  174. )
  175. ));
  176. }
  177. protected function _getFavorite()
  178. {
  179. if (!$this->isEnabled())
  180. return array('code' => 1, 'message' => _t('_favorite_err_not_enabled'));
  181. $iAuthorId = $this->_getAuthorId();
  182. $iObjectId = $this->getId();
  183. $iObjectAuthorId = $this->_oQuery->getObjectAuthorId($iObjectId);
  184. $oForm = $this->_getFormObject($this->_sFormDisplayAdd);
  185. $oForm->setId($this->_aHtmlIds['do_form']);
  186. $oForm->setName($this->_aHtmlIds['do_form']);
  187. $oForm->aParams['db']['table'] = $this->_aSystem['table_track'];
  188. $oForm->aInputs['sys']['value'] = $this->_sSystem;
  189. $oForm->aInputs['object_id']['value'] = $iObjectId;
  190. $oForm->aInputs['action']['value'] = 'Favorite';
  191. $aListsValues = array();
  192. $aLists = $this->_oQuery->getList(array('type' => 'all', 'author_id' => $iAuthorId, 'need_default' => true));
  193. foreach($aLists as $aList) {
  194. $aListsValues[$aList['id']] = $aList['title'] . $this->_oTemplate->parseHtmlByName('privacy_icon.html', array('icon' => BxDolPrivacy::getIcon($aList['allow_view_favorite_list_to'])));
  195. }
  196. $oForm->aInputs['list']['values'] = $aListsValues;
  197. $oForm->aInputs['list']['value'] = $this->_oQuery->getList(array('type' => 'object_and_author', 'object_id' => $iObjectId, 'author_id' => $iAuthorId));
  198. $oForm->aInputs['list']['label_as_html'] = true;
  199. $oModule = BxDolModule::getInstance($this->_aSystem["name"]);
  200. $CNF = $oModule->_oConfig->CNF;
  201. $oPrivacy = BxDolPrivacy::getObjectInstance($CNF["OBJECT_PRIVACY_LIST_VIEW"]);
  202. if(!$oPrivacy)
  203. return;
  204. $aSave = array('db' => array('pass' => 'Xss'));
  205. $aGroupChooser = $oPrivacy->getGroupChooser($CNF["OBJECT_PRIVACY_LIST_VIEW"]);
  206. $oForm->aInputs["allow_view_favorite_list_to"] = array_merge($oForm->aInputs["allow_view_favorite_list_to"], $aGroupChooser, $aSave);
  207. if($oForm->isSubmittedAndValid()) {
  208. $mList = $oForm->getCleanValue('list');
  209. $aList = is_array($mList) ? $mList : array();
  210. $sTitle = $oForm->getCleanValue('title');
  211. if ($sTitle){
  212. $sAllowViewTo = $oForm->getCleanValue('allow_view_favorite_list_to');
  213. $iAddList = $this->_oQuery->addList($iAuthorId, $sTitle, $sAllowViewTo);
  214. array_push($aList, $iAddList);
  215. }
  216. $this->_oQuery->clearFavorite($iObjectId, $iAuthorId);
  217. $bPerformed = count($aList) > 0 ? true: false;
  218. foreach($aList as $iList) {
  219. $this->_oQuery->doFavorite($iObjectId, $iAuthorId, $iList);
  220. }
  221. bx_alert($this->_sSystem, 'favorite', $iObjectId, $iAuthorId, array('favorite_author_id' => $iAuthorId, 'object_author_id' => $iObjectAuthorId, 'list_ids' => $aList));
  222. bx_alert('favorite', 'do', 0, $iAuthorId, array('object_system' => $this->_sSystem, 'object_id' => $iObjectId, 'object_author_id' => $iObjectAuthorId, 'list_ids' => $aList));
  223. $aFavorite = $this->_oQuery->getFavorite($iObjectId);
  224. return array(
  225. 'eval' => $this->getJsObjectName() . '.onFavorite(oData, oElement)',
  226. 'code' => 0,
  227. 'count' => $aFavorite['count'],
  228. 'countf' => (int)$aFavorite['count'] > 0 ? $this->_getCounterLabel($aFavorite['count']) : '',
  229. 'label_icon' => $this->_getIconDoFavorite($bPerformed),
  230. 'label_title' => _t($this->_getTitleDoFavorite($bPerformed)),
  231. 'disabled' => false
  232. );
  233. }
  234. $sPopupId = $this->_aHtmlIds['do_popup'];
  235. $sPopupContent = BxTemplFunctions::getInstance()->transBox($sPopupId, $this->_oTemplate->parseHtmlByName('favorite_do_favorite_popup.html', array(
  236. 'style_prefix' => $this->_sStylePrefix,
  237. 'js_object' => $this->getJsObjectName(),
  238. 'form' => str_replace('{js_object}', $this->getJsObjectName(), $oForm->getCode()),
  239. 'form_id' => $oForm->id,
  240. )));
  241. return array('popup' => $sPopupContent, 'popup_id' => $sPopupId);
  242. }
  243. protected function _getEditList($aData)
  244. {
  245. if (!$this->isEnabled())
  246. return array('code' => 1, 'message' => _t('_favorite_err_not_enabled'));
  247. $oForm = $this->_getFormObject($this->_sFormDisplayListEdit);
  248. $oForm->setId($this->_aHtmlIds['do_form']);
  249. $oForm->setName($this->_aHtmlIds['do_form']);
  250. $oForm->aParams['db']['table'] = $this->_aSystem['table_track'];
  251. $oForm->aInputs['sys']['value'] = $this->_sSystem;
  252. $oForm->aInputs['list_id']['value'] = $aData['id'];
  253. $oForm->aInputs['object_id']['value'] = 0;
  254. $oForm->aInputs['action']['value'] = 'EditList';
  255. $oModule = BxDolModule::getInstance($this->_aSystem["name"]);
  256. $CNF = $oModule->_oConfig->CNF;
  257. $oPrivacy = BxDolPrivacy::getObjectInstance($CNF["OBJECT_PRIVACY_LIST_VIEW"]);
  258. if(!$oPrivacy)
  259. return;
  260. $aSave = array('db' => array('pass' => 'Xss'));
  261. $aGroupChooser = $oPrivacy->getGroupChooser($CNF["OBJECT_PRIVACY_LIST_VIEW"]);
  262. $oForm->aInputs["allow_view_favorite_list_to"] = array_merge($oForm->aInputs["allow_view_favorite_list_to"], $aGroupChooser, $aSave);
  263. $oForm->aInputs['title']['value'] = $aData['title'];
  264. $oForm->aInputs['allow_view_favorite_list_to']['value'] = $aData['allow_view_favorite_list_to'];
  265. //echo 'yyy';
  266. if($oForm->isSubmittedAndValid()) {
  267. $sTitle = $oForm->getCleanValue('title');
  268. if ($sTitle){
  269. $sAllowViewTo = $oForm->getCleanValue('allow_view_favorite_list_to');
  270. $this->_oQuery->editList($aData['id'], $sTitle, $sAllowViewTo);
  271. }
  272. return array(
  273. 'eval' => $this->getJsObjectName() . '.onEditFavoriteList(oData, oElement)',
  274. 'code' => 0,
  275. );
  276. }
  277. $sPopupId = $this->_aHtmlIds['do_popup'];
  278. $sPopupContent = BxTemplFunctions::getInstance()->transBox($sPopupId, $this->_oTemplate->parseHtmlByName('favorite_edit_favorite_list.html', array(
  279. 'style_prefix' => $this->_sStylePrefix,
  280. 'js_object' => $this->getJsObjectName(),
  281. 'form' => str_replace('{js_object}', $this->getJsObjectName(), $oForm->getCode()),
  282. 'form_id' => $oForm->id,
  283. )));
  284. return array('popup' => $sPopupContent, 'popup_id' => $sPopupId);
  285. }
  286. protected function _getFavoritedBy()
  287. {
  288. $aTmplFavorites = array();
  289. $aFavorites = $this->_oQuery->getPerformedBy($this->getId());
  290. foreach($aFavorites as $aFavorite) {
  291. list($sUserName, $sUserUrl, $sUserIcon, $sUserUnit) = $this->_getAuthorInfo($aFavorite['author_id']);
  292. $aTmplFavorites[] = array(
  293. 'style_prefix' => $this->_sStylePrefix,
  294. 'user_unit' => $sUserUnit,
  295. );
  296. }
  297. if(empty($aTmplFavorites))
  298. $aTmplFavorites = MsgBox(_t('_Empty'));
  299. return $this->_oTemplate->parseHtmlByName('favorite_by_list.html', array(
  300. 'style_prefix' => $this->_sStylePrefix,
  301. 'bx_repeat:list' => $aTmplFavorites
  302. ));
  303. }
  304. }
  305. /** @} */