Instance.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Adminhtml Manage Widgets Instance Controller
  8. */
  9. namespace Magento\Widget\Controller\Adminhtml\Widget;
  10. abstract class Instance extends \Magento\Backend\App\Action
  11. {
  12. /**
  13. * Authorization level of a basic admin session
  14. *
  15. * @see _isAllowed()
  16. */
  17. const ADMIN_RESOURCE = 'Magento_Widget::widget_instance';
  18. /**
  19. * Core registry
  20. *
  21. * @var \Magento\Framework\Registry
  22. */
  23. protected $_coreRegistry;
  24. /**
  25. * @var \Magento\Widget\Model\Widget\InstanceFactory
  26. */
  27. protected $_widgetFactory;
  28. /**
  29. * @var \Psr\Log\LoggerInterface
  30. */
  31. protected $_logger;
  32. /**
  33. * @var \Magento\Framework\Math\Random
  34. */
  35. protected $mathRandom;
  36. /**
  37. * @var \Magento\Framework\Translate\InlineInterface
  38. */
  39. protected $_translateInline;
  40. /**
  41. * @param \Magento\Backend\App\Action\Context $context
  42. * @param \Magento\Framework\Registry $coreRegistry
  43. * @param \Magento\Widget\Model\Widget\InstanceFactory $widgetFactory
  44. * @param \Psr\Log\LoggerInterface $logger
  45. * @param \Magento\Framework\Math\Random $mathRandom
  46. * @param \Magento\Framework\Translate\InlineInterface $translateInline
  47. */
  48. public function __construct(
  49. \Magento\Backend\App\Action\Context $context,
  50. \Magento\Framework\Registry $coreRegistry,
  51. \Magento\Widget\Model\Widget\InstanceFactory $widgetFactory,
  52. \Psr\Log\LoggerInterface $logger,
  53. \Magento\Framework\Math\Random $mathRandom,
  54. \Magento\Framework\Translate\InlineInterface $translateInline
  55. ) {
  56. $this->_translateInline = $translateInline;
  57. $this->_coreRegistry = $coreRegistry;
  58. $this->_widgetFactory = $widgetFactory;
  59. $this->_logger = $logger;
  60. $this->mathRandom = $mathRandom;
  61. parent::__construct($context);
  62. }
  63. /**
  64. * Load layout, set active menu and breadcrumbs
  65. *
  66. * @return $this
  67. */
  68. protected function _initAction()
  69. {
  70. $this->_view->loadLayout();
  71. $this->_setActiveMenu(
  72. 'Magento_Widget::cms_widget_instance'
  73. )->_addBreadcrumb(
  74. __('CMS'),
  75. __('CMS')
  76. )->_addBreadcrumb(
  77. __('Manage Widget Instances'),
  78. __('Manage Widget Instances')
  79. );
  80. return $this;
  81. }
  82. /**
  83. * Init widget instance object and set it to registry
  84. *
  85. * @return \Magento\Widget\Model\Widget\Instance|boolean
  86. */
  87. protected function _initWidgetInstance()
  88. {
  89. /** @var $widgetInstance \Magento\Widget\Model\Widget\Instance */
  90. $widgetInstance = $this->_widgetFactory->create();
  91. $code = $this->getRequest()->getParam('code', null);
  92. $instanceId = $this->getRequest()->getParam('instance_id', null);
  93. if ($instanceId) {
  94. $widgetInstance->load($instanceId)->setCode($code);
  95. if (!$widgetInstance->getId()) {
  96. $this->messageManager->addError(__('Please specify a correct widget.'));
  97. return false;
  98. }
  99. } else {
  100. // Widget id was not provided on the query-string. Locate the widget instance
  101. // type (namespace\classname) based upon the widget code (aka, widget id).
  102. $themeId = $this->getRequest()->getParam('theme_id', null);
  103. $type = $code != null ? $widgetInstance->getWidgetReference('code', $code, 'type') : null;
  104. $widgetInstance->setType($type)->setCode($code)->setThemeId($themeId);
  105. }
  106. $this->_coreRegistry->register('current_widget_instance', $widgetInstance);
  107. return $widgetInstance;
  108. }
  109. /**
  110. * Set body to response
  111. *
  112. * @param string $body
  113. * @return void
  114. */
  115. protected function setBody($body)
  116. {
  117. $this->_translateInline->processResponseBody($body);
  118. $this->getResponse()->setBody($body);
  119. }
  120. }