BxBaseFavorite.php 18 KB

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