1
0

BxBaseFavorite.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. if (!isset($this->_aSystem['table_lists']) || $this->_aSystem['table_lists'] == ''){
  185. $bUndo = $this->isUndo();
  186. $bPerformed = $this->isPerformed($iObjectId, $iAuthorId);
  187. $bPerformUndo = $bPerformed && $bUndo ? true : false;
  188. if(!$bPerformUndo && !$this->isAllowedFavorite())
  189. return array('code' => 2, 'message' => $this->msgErrAllowedFavorite());
  190. if($bPerformed && !$bUndo)
  191. return array('code' => 3, 'message' => _t('_favorite_err_duplicate_favorite'));
  192. if(!$this->_oQuery->{($bPerformUndo ? 'un' : '') . 'doFavorite'}($iObjectId, $iAuthorId))
  193. return array('code' => 4, 'message' => _t('_favorite_err_cannot_perform_action'));
  194. if(!$bPerformUndo)
  195. $this->isAllowedFavorite(true);
  196. $this->_triggerValue($bPerformUndo ? -1 : 1);
  197. bx_alert($this->_sSystem, ($bPerformUndo ? 'un' : '') . 'favorite', $iObjectId, $iAuthorId, array('favorite_author_id' => $iAuthorId, 'object_author_id' => $iObjectAuthorId));
  198. bx_alert('favorite', ($bPerformUndo ? 'un' : '') . 'do', 0, $iAuthorId, array('object_system' => $this->_sSystem, 'object_id' => $iObjectId, 'object_author_id' => $iObjectAuthorId));
  199. $aFavorite = $this->_oQuery->getFavorite($iObjectId);
  200. return array(
  201. 'eval' => $this->getJsObjectName() . '.onFavorite(oData, oElement)',
  202. 'code' => 0,
  203. 'count' => $aFavorite['count'],
  204. 'countf' => (int)$aFavorite['count'] > 0 ? $this->_getCounterLabel($aFavorite['count']) : '',
  205. 'label_icon' => $this->_getIconDoFavorite(!$bPerformed),
  206. 'label_title' => _t($this->_getTitleDoFavorite(!$bPerformed)),
  207. 'disabled' => !$bPerformed && !$bUndo
  208. );
  209. }
  210. $oForm = $this->_getFormObject($this->_sFormDisplayAdd);
  211. $oForm->setId($this->_aHtmlIds['do_form']);
  212. $oForm->setName($this->_aHtmlIds['do_form']);
  213. $oForm->aParams['db']['table'] = $this->_aSystem['table_track'];
  214. $oForm->aInputs['sys']['value'] = $this->_sSystem;
  215. $oForm->aInputs['object_id']['value'] = $iObjectId;
  216. $oForm->aInputs['action']['value'] = 'Favorite';
  217. $aListsValues = array();
  218. $aLists = $this->_oQuery->getList(array('type' => 'all', 'author_id' => $iAuthorId, 'need_default' => true));
  219. foreach($aLists as $aList) {
  220. $aListsValues[$aList['id']] = $aList['title'] . $this->_oTemplate->parseHtmlByName('privacy_icon.html', array('icon' => BxDolPrivacy::getIcon($aList['allow_view_favorite_list_to'])));
  221. }
  222. $oForm->aInputs['list']['values'] = $aListsValues;
  223. $oForm->aInputs['list']['value'] = $this->_oQuery->getList(array('type' => 'object_and_author', 'object_id' => $iObjectId, 'author_id' => $iAuthorId));
  224. $oForm->aInputs['list']['label_as_html'] = true;
  225. $oModule = BxDolModule::getInstance($this->_aSystem["name"]);
  226. $CNF = $oModule->_oConfig->CNF;
  227. $oPrivacy = BxDolPrivacy::getObjectInstance($CNF["OBJECT_PRIVACY_LIST_VIEW"]);
  228. if (!$oPrivacy)
  229. return array('code' => 5, 'message' => _t('_favorite_err_cannot_perform_action'));
  230. $aSave = array('db' => array('pass' => 'Xss'));
  231. $aGroupChooser = $oPrivacy->getGroupChooser($CNF["OBJECT_PRIVACY_LIST_VIEW"]);
  232. $oForm->aInputs["allow_view_favorite_list_to"] = array_merge($oForm->aInputs["allow_view_favorite_list_to"], $aGroupChooser, $aSave);
  233. if ($oForm->isSubmittedAndValid()) {
  234. $mList = $oForm->getCleanValue('list');
  235. $aList = is_array($mList) ? $mList : array();
  236. $sTitle = $oForm->getCleanValue('title');
  237. if ($sTitle) {
  238. $sAllowViewTo = $oForm->getCleanValue('allow_view_favorite_list_to');
  239. $iAddList = $this->_oQuery->addList($iAuthorId, $sTitle, $sAllowViewTo);
  240. array_push($aList, $iAddList);
  241. }
  242. $this->_oQuery->clearFavorite($iObjectId, $iAuthorId);
  243. foreach ($aList as $iList) {
  244. $this->_oQuery->doFavorite($iObjectId, $iAuthorId, $iList);
  245. }
  246. $this->_trigger();
  247. bx_alert($this->_sSystem, 'favorite', $iObjectId, $iAuthorId, array('favorite_author_id' => $iAuthorId, 'object_author_id' => $iObjectAuthorId, 'list_ids' => $aList));
  248. bx_alert('favorite', 'do', 0, $iAuthorId, array('object_system' => $this->_sSystem, 'object_id' => $iObjectId, 'object_author_id' => $iObjectAuthorId, 'list_ids' => $aList));
  249. $bPerformed = count($aList) > 0;
  250. $aFavorite = $this->_oQuery->getFavorite($iObjectId);
  251. return array(
  252. 'eval' => $this->getJsObjectName() . '.onFavorite(oData, oElement)',
  253. 'code' => 0,
  254. 'count' => $aFavorite['count'],
  255. 'countf' => (int)$aFavorite['count'] > 0 ? $this->_getCounterLabel($aFavorite['count']) : '',
  256. 'label_icon' => $this->_getIconDoFavorite($bPerformed),
  257. 'label_title' => _t($this->_getTitleDoFavorite($bPerformed)),
  258. 'disabled' => false
  259. );
  260. }
  261. $sPopupId = $this->_aHtmlIds['do_popup'];
  262. $sPopupContent = BxTemplFunctions::getInstance()->transBox($sPopupId, $this->_oTemplate->parseHtmlByName('favorite_do_favorite_popup.html', array(
  263. 'style_prefix' => $this->_sStylePrefix,
  264. 'js_object' => $this->getJsObjectName(),
  265. 'form' => str_replace('{js_object}', $this->getJsObjectName(), $oForm->getCode()),
  266. 'form_id' => $oForm->id,
  267. )));
  268. return array('popup' => $sPopupContent, 'popup_id' => $sPopupId);
  269. }
  270. protected function _getEditList($aData)
  271. {
  272. if (!$this->isEnabled())
  273. return array('code' => 1, 'message' => _t('_favorite_err_not_enabled'));
  274. $oForm = $this->_getFormObject($this->_sFormDisplayListEdit);
  275. $oForm->setId($this->_aHtmlIds['do_form']);
  276. $oForm->setName($this->_aHtmlIds['do_form']);
  277. $oForm->aParams['db']['table'] = $this->_aSystem['table_track'];
  278. $oForm->aInputs['sys']['value'] = $this->_sSystem;
  279. $oForm->aInputs['list_id']['value'] = $aData['id'];
  280. $oForm->aInputs['object_id']['value'] = 0;
  281. $oForm->aInputs['action']['value'] = 'EditList';
  282. $oModule = BxDolModule::getInstance($this->_aSystem["name"]);
  283. $CNF = $oModule->_oConfig->CNF;
  284. $oPrivacy = BxDolPrivacy::getObjectInstance($CNF["OBJECT_PRIVACY_LIST_VIEW"]);
  285. if(!$oPrivacy)
  286. return;
  287. $aSave = array('db' => array('pass' => 'Xss'));
  288. $aGroupChooser = $oPrivacy->getGroupChooser($CNF["OBJECT_PRIVACY_LIST_VIEW"]);
  289. $oForm->aInputs["allow_view_favorite_list_to"] = array_merge($oForm->aInputs["allow_view_favorite_list_to"], $aGroupChooser, $aSave);
  290. $oForm->aInputs['title']['value'] = $aData['title'];
  291. $oForm->aInputs['allow_view_favorite_list_to']['value'] = $aData['allow_view_favorite_list_to'];
  292. //echo 'yyy';
  293. if($oForm->isSubmittedAndValid()) {
  294. $sTitle = $oForm->getCleanValue('title');
  295. if ($sTitle){
  296. $sAllowViewTo = $oForm->getCleanValue('allow_view_favorite_list_to');
  297. $this->_oQuery->editList($aData['id'], $sTitle, $sAllowViewTo);
  298. }
  299. return array(
  300. 'eval' => $this->getJsObjectName() . '.onEditFavoriteList(oData, oElement)',
  301. 'code' => 0,
  302. );
  303. }
  304. $sPopupId = $this->_aHtmlIds['do_popup'];
  305. $sPopupContent = BxTemplFunctions::getInstance()->transBox($sPopupId, $this->_oTemplate->parseHtmlByName('favorite_edit_favorite_list.html', array(
  306. 'style_prefix' => $this->_sStylePrefix,
  307. 'js_object' => $this->getJsObjectName(),
  308. 'form' => str_replace('{js_object}', $this->getJsObjectName(), $oForm->getCode()),
  309. 'form_id' => $oForm->id,
  310. )));
  311. return array('popup' => $sPopupContent, 'popup_id' => $sPopupId);
  312. }
  313. protected function _getFavoritedBy()
  314. {
  315. $aTmplFavorites = array();
  316. $aFavorites = $this->_oQuery->getPerformedBy($this->getId());
  317. foreach($aFavorites as $aFavorite) {
  318. list($sUserName, $sUserUrl, $sUserIcon, $sUserUnit) = $this->_getAuthorInfo($aFavorite['author_id']);
  319. $aTmplFavorites[] = array(
  320. 'style_prefix' => $this->_sStylePrefix,
  321. 'user_unit' => $sUserUnit,
  322. );
  323. }
  324. if(empty($aTmplFavorites))
  325. $aTmplFavorites = MsgBox(_t('_Empty'));
  326. return $this->_oTemplate->parseHtmlByName('favorite_by_list.html', array(
  327. 'style_prefix' => $this->_sStylePrefix,
  328. 'bx_repeat:list' => $aTmplFavorites
  329. ));
  330. }
  331. }
  332. /** @} */