BxFilesMenuViewActions.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 Files Files
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. /**
  12. * View entry social actions menu
  13. */
  14. class BxFilesMenuViewActions extends BxBaseModTextMenuViewActions
  15. {
  16. public function __construct($aObject, $oTemplate = false)
  17. {
  18. $this->_sModule = 'bx_files';
  19. parent::__construct($aObject, $oTemplate);
  20. }
  21. protected function _getMenuItemDownloadFile($aItem)
  22. {
  23. return $this->_getMenuItemByNameActions($aItem);
  24. }
  25. protected function _getMenuItemEditFile($aItem)
  26. {
  27. return $this->_getMenuItemByNameActions($aItem);
  28. }
  29. protected function _getMenuItemDeleteFile($aItem)
  30. {
  31. return $this->_getMenuItemByNameActions($aItem);
  32. }
  33. }
  34. class BxFilesMenuViewActionsInline extends BxFilesMenuViewActions {
  35. protected $_iMenuItemCounter;
  36. protected $_sJsObjectMenuTools;
  37. protected $_sBookmarkIcon;
  38. protected $_bAllowEditOptions;
  39. public function __construct($aObject, $oTemplate = false)
  40. {
  41. parent::__construct($aObject, $oTemplate);
  42. $this->_bShowTitle = false;
  43. $this->_iMenuItemCounter = 0;
  44. $this->_iMoreAutoItemsStatic = 2;
  45. $this->_bMoreAutoItemsStaticOnly = 1;
  46. $this->_sJsObjectMenuTools = 'oMenuTools' . bx_gen_method_name($this->_sObject);
  47. $this->addMarkers([
  48. 'js_object' => $this->_sJsObjectMenuTools,
  49. ]);
  50. $this->_sBookmarkIcon = 'far star';
  51. }
  52. public function setContentId($iContentId)
  53. {
  54. $iRandNumber = mt_rand();
  55. $this->_iContentId = $iContentId;
  56. $this->_sJsObjectMoreAuto = 'oMenuMoreAuto' . bx_gen_method_name($this->_sObject) . $this->_iContentId . $iRandNumber;
  57. $sPrefix = str_replace('_', '-', $this->_sObject);
  58. $this->_aHtmlIds = [
  59. 'main' => $sPrefix . $this->_iContentId . $iRandNumber,
  60. 'more_auto_popup' => $sPrefix . '-ma-popup' . $this->_iContentId . $iRandNumber,
  61. ];
  62. //erase cached data to force it to generate it again since this is going to be a menu for a new entry
  63. $this->_oMenuAction = false;
  64. $this->_oMenuActions = false;
  65. $this->_oMenuActionsMore = false;
  66. $this->_oMenuSocialSharing = false;
  67. $this->_bMoreAuto = null;
  68. unset($this->_aObject['menu_items']);
  69. parent::setContentId($iContentId);
  70. }
  71. public function setBookmarked($bBookmarked) {
  72. $this->_sBookmarkIcon = $bBookmarked ? 'fas star' : 'far star';
  73. }
  74. public function setAllowEditOptions($bAllow) {
  75. $this->_bAllowEditOptions = $bAllow;
  76. }
  77. protected function _getMenuItem ($aItem) {
  78. //first two items suppose to appear in a row without a title, the rest must go to more-auto along with a title
  79. $this->_bShowTitle = $this->_iMenuItemCounter > $this->_iMoreAutoItemsStatic;
  80. if ($aItem['name'] == 'bookmark')
  81. $aItem['icon'] = $this->_sBookmarkIcon;
  82. $bRes = parent::_getMenuItem ($aItem);
  83. if ($bRes) $this->_iMenuItemCounter++;
  84. return $bRes;
  85. }
  86. public function _getMenuItemBookmark($aItem) {
  87. if (!isLogged()) return false;
  88. return true;
  89. }
  90. public function _getMenuItemDeleteFileQuick($aItem) {
  91. if (!$this->_bAllowEditOptions) return false;
  92. return true;
  93. }
  94. public function _getMenuItemMoveTo($aItem) {
  95. if (!$this->_bAllowEditOptions) return false;
  96. return true;
  97. }
  98. public function _getMenuItemEditTitle($aItem) {
  99. if (!$this->_bAllowEditOptions) return false;
  100. return true;
  101. }
  102. public function getMenuItems () {
  103. //reset the counter here because at the very first call the getMenuItems calls twice (the first is in BxBaseMenuMoreAuto::_isMoreAuto
  104. $this->_iMenuItemCounter = 1;
  105. return parent::getMenuItems();
  106. }
  107. protected function _getJsCodeMoreAuto()
  108. {
  109. $sJsObject = $this->_getJsObjectMoreAuto();
  110. $aJsParams = array(
  111. 'sObject' => $this->_sObject,
  112. 'iItemsStatic' => $this->_iMoreAutoItemsStatic,
  113. 'bItemsStaticOnly' => $this->_bMoreAutoItemsStaticOnly ? 1 : 0,
  114. 'aHtmlIds' => $this->_getHtmlIds()
  115. );
  116. return $this->_oTemplate->_wrapInTagJsCode("if(!" . $sJsObject . ") {var " . $sJsObject . " = new BxDolMenuMoreAuto(" . json_encode($aJsParams) . "); " . $sJsObject . ".init();} else {" . $sJsObject . ".init();}");
  117. }
  118. protected function _addJsCss()
  119. {
  120. parent::_addJsCss();
  121. $this->_oTemplate->addJs('menu_tools.js');
  122. }
  123. protected function _getTemplateVars() {
  124. $aResult = parent::_getTemplateVars();
  125. $sJsObject = $this->_getJsObjectMenuTools();
  126. $aJsParams = [
  127. 'sActionUrl' => BX_DOL_URL_ROOT.$this->_oTemplate->getModule()->_oConfig->getBaseUri(),
  128. ];
  129. $aResult['js_code'] .= $this->_oTemplate->_wrapInTagJsCode("if(!" . $sJsObject . ") {var " . $sJsObject . " = new BxFilesMenuTools(" . json_encode($aJsParams) . ");}");
  130. return $aResult;
  131. }
  132. protected function _getJsObjectMenuTools() {
  133. return $this->_sJsObjectMenuTools;
  134. }
  135. }
  136. /** @} */