123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461 |
- <?php
- /**
- * Copyright © 2015-2017 Ihor Vansach (ihor@magefan.com). All rights reserved.
- * See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
- *
- * Glory to Ukraine! Glory to the heroes!
- */
- namespace Magefan\Blog\Controller\Adminhtml;
- /**
- * Abstract admin controller
- */
- abstract class Actions extends \Magento\Backend\App\Action
- {
- /**
- * Form session key
- * @var string
- */
- protected $_formSessionKey;
- /**
- * Allowed Key
- * @var string
- */
- protected $_allowedKey;
- /**
- * Model class name
- * @var string
- */
- protected $_modelClass;
- /**
- * Active menu key
- * @var string
- */
- protected $_activeMenu;
- /**
- * Store config section key
- * @var string
- */
- protected $_configSection;
- /**
- * Request id key
- * @var string
- */
- protected $_idKey = 'id';
- /**
- * Status field name
- * @var string
- */
- protected $_statusField = 'status';
- /**
- * Save request params key
- * @var string
- */
- protected $_paramsHolder;
- /**
- * Model Object
- * @var \Magento\Framework\Model\AbstractModel
- */
- protected $_model;
- /**
- * Core registry
- *
- * @var \Magento\Framework\Registry
- */
- protected $_coreRegistry = null;
- /**
- * Action execute
- * @return \Magento\Framework\Controller\ResultInterface
- */
- public function execute()
- {
- $_preparedActions = ['index', 'grid', 'new', 'edit', 'save', 'duplicate', 'delete', 'config', 'massStatus'];
- $_action = $this->getRequest()->getActionName();
- if (in_array($_action, $_preparedActions)) {
- $method = '_'.$_action.'Action';
- $this->_beforeAction();
- $this->$method();
- $this->_afterAction();
- }
- }
- /**
- * Index action
- * @return void
- */
- protected function _indexAction()
- {
- if ($this->getRequest()->getParam('ajax')) {
- $this->_forward('grid');
- return;
- }
- $this->_view->loadLayout();
- $this->_setActiveMenu($this->_activeMenu);
- $title = __('Manage %1', $this->_getModel(false)->getOwnTitle(true));
- $this->_view->getPage()->getConfig()->getTitle()->prepend($title);
- $this->_addBreadcrumb($title, $title);
- $this->_view->renderLayout();
- }
- /**
- * Grid action
- * @return void
- */
- protected function _gridAction()
- {
- $this->_view->loadLayout(false);
- $this->_view->renderLayout();
- }
- /**
- * New action
- * @return void
- */
- protected function _newAction()
- {
- $this->_forward('edit');
- }
- /**
- * Edit action
- * @return void
- */
- public function _editAction()
- {
- $model = $this->_getModel();
- $this->_getRegistry()->register('current_model', $model);
- $this->_view->loadLayout();
- $this->_setActiveMenu($this->_activeMenu);
- $title = $model->getOwnTitle();
- if ($model->getId()) {
- $breadcrumbTitle = __('Edit %1', $title);
- $breadcrumbLabel = $breadcrumbTitle;
- } else {
- $breadcrumbTitle = __('New %1', $title);
- $breadcrumbLabel = __('Create %1', $title);
- }
- $this->_view->getPage()->getConfig()->getTitle()->prepend(__($title));
- $this->_view->getPage()->getConfig()->getTitle()->prepend(
- $model->getId() ? $this->_getModelName($model) : __('New %1', $title)
- );
- $this->_addBreadcrumb($breadcrumbLabel, $breadcrumbTitle);
- // restore data
- $values = $this->_getSession()->getData($this->_formSessionKey, true);
- if ($this->_paramsHolder) {
- $values = isset($values[$this->_paramsHolder]) ? $values[$this->_paramsHolder] : null;
- }
- if ($values) {
- $model->addData($values);
- }
- $this->_view->renderLayout();
- }
- /**
- * Retrieve model name
- * @param boolean $plural
- * @return string
- */
- protected function _getModelName(\Magento\Framework\Model\AbstractModel $model)
- {
- return $model->getName() ?: $model->getTitle();
- }
- /**
- * Save action
- * @return void
- */
- public function _saveAction()
- {
- $request = $this->getRequest();
- if (!$request->isPost()) {
- $this->getResponse()->setRedirect($this->getUrl('*/*'));
- }
- $model = $this->_getModel();
- try {
- $params = $this->_paramsHolder ? $request->getParam($this->_paramsHolder) : $request->getParams();
- $idFieldName = $model->getResource()->getIdFieldName();
- if (isset($params[$idFieldName]) && empty($params[$idFieldName])) {
- unset($params[$idFieldName]);
- }
- $model->addData($params);
- $this->_beforeSave($model, $request);
- $model->save();
- $this->_afterSave($model, $request);
- $this->messageManager->addSuccess(__('%1 has been saved.', $model->getOwnTitle()));
- $this->_setFormData(false);
- } catch (\Magento\Framework\Exception\LocalizedException $e) {
- $this->messageManager->addError(nl2br($e->getMessage()));
- $this->_setFormData();
- } catch (\Exception $e) {
- $this->messageManager->addException(
- $e,
- __('Something went wrong while saving this %1. %2',
- strtolower($model->getOwnTitle()),
- $e->getMessage()
- )
- );
- $this->_setFormData();
- }
- $hasError = (bool)$this->messageManager->getMessages()->getCountByType(
- \Magento\Framework\Message\MessageInterface::TYPE_ERROR
- );
- if ($request->getParam('isAjax')) {
- $block = $this->_objectManager->create('Magento\Framework\View\Layout')->getMessagesBlock();
- $block->setMessages($this->messageManager->getMessages(true));
- $this->getResponse()->setBody(json_encode(
- [
- 'messages' => $block->getGroupedHtml(),
- 'error' => $hasError,
- 'model' => $model->toArray(),
- ]
- ));
- } else {
- if ($hasError || $request->getParam('back')) {
- $this->_redirect('*/*/edit', [$this->_idKey => $model->getId()]);
- } else {
- $this->_redirect('*/*');
- }
- }
- }
- /**
- * Duplicat action
- * @return void
- */
- protected function _duplicateAction()
- {
- try {
- $originModel = $this->_getModel();
- if (!$originModel->getId()) {
- throw new \Exception("Item is not longer exist.", 1);
- }
- $model = $originModel->duplicate();
- $this->messageManager->addSuccess(__('%1 has been duplicated.', $model->getOwnTitle()));
- $this->_redirect('*/*/edit', [$this->_idKey => $model->getId()]);
- } catch (Exception $e) {
- $this->messageManager->addException(
- $e,
- __('Something went wrong while saving this %1. %2',
- strtolower($model->getOwnTitle()),
- $e->getMessage()
- )
- );
- $this->_redirect('*/*/edit', [$this->_idKey => $originModel->getId()]);
- }
- }
- /**
- * Before model Save action
- * @return void
- */
- protected function _beforeSave($model, $request) {}
- /**
- * After model action
- * @return void
- */
- protected function _afterSave($model, $request) {}
- /**
- * Before action
- * @return void
- */
- protected function _beforeAction() {}
- /**
- * After action
- * @return void
- */
- protected function _afterAction() {}
- /**
- * Delete action
- * @return void
- */
- protected function _deleteAction()
- {
- $ids = $this->getRequest()->getParam($this->_idKey);
- if (!is_array($ids)) {
- $ids = [$ids];
- }
- $error = false;
- try {
- foreach($ids as $id) {
- $this->_objectManager->create($this->_modelClass)->setId($id)->delete();
- }
- } catch (\Magento\Framework\Exception\LocalizedException $e) {
- $error = true;
- $this->messageManager->addError($e->getMessage());
- } catch (\Exception $e) {
- $error = true;
- $this->messageManager->addException(
- $e,
- __("We can't delete %1 right now. %2",
- strtolower($this->_getModel(false)->getOwnTitle()),
- $e->getMessage()
- )
- );
- }
- if (!$error) {
- $this->messageManager->addSuccess(
- __('%1 have been deleted.', $this->_getModel(false)->getOwnTitle(count($ids) > 1))
- );
- }
- $this->_redirect('*/*');
- }
- /**
- * Change status action
- * @return void
- */
- protected function _massStatusAction()
- {
- $ids = $this->getRequest()->getParam($this->_idKey);
- if (!is_array($ids)) {
- $ids = [$ids];
- }
- $model = $this->_getModel(false);
- $error = false;
- try {
- $status = $this->getRequest()->getParam('status');
- $statusFieldName = $this->_statusField;
- if (is_null($status)) {
- throw new \Exception(__('Parameter "Status" missing in request data.'));
- }
- if (is_null($statusFieldName)) {
- throw new \Exception(__('Status Field Name is not specified.'));
- }
- foreach($ids as $id) {
- $this->_objectManager->create($this->_modelClass)
- ->load($id)
- ->setData($this->_statusField, $status)
- ->save();
- }
- } catch (\Magento\Framework\Exception\LocalizedException $e) {
- $error = true;
- $this->messageManager->addError($e->getMessage());
- } catch (\Exception $e) {
- $error = true;
- $this->messageManager->addException(
- $e,
- __("We can't change status of %1 right now. %2",
- strtolower($model->getOwnTitle()),
- $e->getMessage()
- )
- );
- }
- if (!$error) {
- $this->messageManager->addSuccess(
- __('%1 status have been changed.', $model->getOwnTitle(count($ids) > 1))
- );
- }
- $this->_redirect('*/*');
- }
- /**
- * Go to config section action
- * @return void
- */
- protected function _configAction()
- {
- $this->_redirect('admin/system_config/edit', ['section' => $this->_configSection()]);
- }
- /**
- * Set form data
- * @return $this
- */
- protected function _setFormData($data = null)
- {
- $this->_getSession()->setData($this->_formSessionKey,
- is_null($data) ? $this->getRequest()->getParams() : $data);
- return $this;
- }
- /**
- * Get core registry
- * @return void
- */
- protected function _getRegistry()
- {
- if (is_null($this->_coreRegistry)) {
- $this->_coreRegistry = $this->_objectManager->get('\Magento\Framework\Registry');
- }
- return $this->_coreRegistry;
- }
- /**
- * Check is allowed access
- *
- * @return bool
- */
- protected function _isAllowed()
- {
- return $this->_authorization->isAllowed($this->_allowedKey);
- }
- /**
- * Retrieve model object
- * @return \Magento\Framework\Model\AbstractModel
- */
- protected function _getModel($load = true)
- {
- if (is_null($this->_model)) {
- $this->_model = $this->_objectManager->create($this->_modelClass);
- $id = (int)$this->getRequest()->getParam($this->_idKey);
- if ($id && $load) {
- $this->_model->load($id);
- }
- }
- return $this->_model;
- }
- }
|