BxFilesMenuViewActions.php 5.2 KB

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