*/ class Mage_Adminhtml_Cms_BlockController extends Mage_Adminhtml_Controller_Action { /** * Init actions * * @return Mage_Adminhtml_Cms_BlockController */ protected function _initAction() { // load layout, set active menu and breadcrumbs $this->loadLayout() ->_setActiveMenu('cms/block') ->_addBreadcrumb(Mage::helper('cms')->__('CMS'), Mage::helper('cms')->__('CMS')) ->_addBreadcrumb(Mage::helper('cms')->__('Static Blocks'), Mage::helper('cms')->__('Static Blocks')) ; return $this; } /** * Index action */ public function indexAction() { $this->_title($this->__('CMS'))->_title($this->__('Static Blocks')); $this->_initAction(); $this->renderLayout(); } /** * Create new CMS block */ public function newAction() { // the same form is used to create and edit $this->_forward('edit'); } /** * Edit CMS block */ public function editAction() { $this->_title($this->__('CMS'))->_title($this->__('Static Blocks')); // 1. Get ID and create model $id = $this->getRequest()->getParam('block_id'); $model = Mage::getModel('cms/block'); // 2. Initial checking if ($id) { $model->load($id); if (! $model->getId()) { Mage::getSingleton('adminhtml/session')->addError(Mage::helper('cms')->__('This block no longer exists.')); $this->_redirect('*/*/'); return; } } $this->_title($model->getId() ? $model->getTitle() : $this->__('New Block')); // 3. Set entered data if was error when we do save $data = Mage::getSingleton('adminhtml/session')->getFormData(true); if (! empty($data)) { $model->setData($data); } // 4. Register model to use later in blocks Mage::register('cms_block', $model); // 5. Build edit form $this->_initAction() ->_addBreadcrumb($id ? Mage::helper('cms')->__('Edit Block') : Mage::helper('cms')->__('New Block'), $id ? Mage::helper('cms')->__('Edit Block') : Mage::helper('cms')->__('New Block')) ->renderLayout(); } /** * Save action */ public function saveAction() { // check if data sent if ($data = $this->getRequest()->getPost()) { $id = $this->getRequest()->getParam('block_id'); $model = Mage::getModel('cms/block')->load($id); if (!$model->getId() && $id) { Mage::getSingleton('adminhtml/session')->addError(Mage::helper('cms')->__('This block no longer exists.')); $this->_redirect('*/*/'); return; } // init model and set data $model->setData($data); // try to save it try { // save the data $model->save(); // display success message Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('cms')->__('The block has been saved.')); // clear previously saved data from session Mage::getSingleton('adminhtml/session')->setFormData(false); // check if 'Save and Continue' if ($this->getRequest()->getParam('back')) { $this->_redirect('*/*/edit', array('block_id' => $model->getId())); return; } // go to grid $this->_redirect('*/*/'); return; } catch (Exception $e) { // display error message Mage::getSingleton('adminhtml/session')->addError($e->getMessage()); // save data in session Mage::getSingleton('adminhtml/session')->setFormData($data); // redirect to edit form $this->_redirect('*/*/edit', array('block_id' => $this->getRequest()->getParam('block_id'))); return; } } $this->_redirect('*/*/'); } /** * Delete action */ public function deleteAction() { // check if we know what should be deleted if ($id = $this->getRequest()->getParam('block_id')) { $title = ""; try { // init model and delete $model = Mage::getModel('cms/block'); $model->load($id); $title = $model->getTitle(); $model->delete(); // display success message Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('cms')->__('The block has been deleted.')); // go to grid $this->_redirect('*/*/'); return; } catch (Exception $e) { // display error message Mage::getSingleton('adminhtml/session')->addError($e->getMessage()); // go back to edit form $this->_redirect('*/*/edit', array('block_id' => $id)); return; } } // display error message Mage::getSingleton('adminhtml/session')->addError(Mage::helper('cms')->__('Unable to find a block to delete.')); // go to grid $this->_redirect('*/*/'); } /** * Check the permission to run it * * @return boolean */ protected function _isAllowed() { return Mage::getSingleton('admin/session')->isAllowed('cms/block'); } }