I have a form in my module, and now I want to add the new button to it i.e. Save and Continue Edit
button.
I added this button but it is working partially, means it is saving the values in the database but not redirecting to the current page.
Here is my file code.
public function __construct()
{
parent::__construct();
$this->_objectId = 'id';
$this->_blockGroup = 'blog';
$this->_controller = 'adminhtml_article';
$this->_updateButton('save', 'label','Save Article');
$this->_updateButton('delete', 'label', 'Delete Article');
$this->_addButton('save_and_continue', array(
'label' => Mage::helper('blog')->__('Save And Continue Edit'),
'onclick' => 'saveAndContinueEdit()',
'class' => 'save'
), -100);
$this->_formScripts[] = "
function saveAndContinueEdit(){
editForm.submit($('edit_form').action + 'back/edit/');
}
";
$this->setId('blog_edit');
}
Now what should I need to do it properly.
Here is my controller file.
<?php
class Vertax_Blog_Adminhtml_ArticleController extends Mage_Adminhtml_Controller_Action
{
protected function _initAction()
{
$this->loadLayout()->_setActiveMenu('blog/article')
->_addBreadcrumb('Article Manager','Article Manager');
return $this;
}
public function indexAction()
{
$this->_initAction();
$this->_title($this->__("Vertax_Article"));
$this->renderLayout();
}
public function editAction()
{
$articleId = $this->getRequest()->getParam('id');
$articleModel = Mage::getModel('blog/article')->load($articleId);
$categoryModel= Mage::getModel('blog/category')
->getCollection()
->addFieldToFilter('article_id', $articleId);
if ($articleModel->getId() || $articleId == 0)
{
Mage::register('article_data', $articleModel);
$this->loadLayout();
$this->_setActiveMenu('blog/article');
$this->_addBreadcrumb('article Manager', 'article Manager');
$this->_addBreadcrumb('Article Description', 'Article Description');
$this->getLayout()->getBlock('head')->setCanLoadExtJs(true);
$this->_addContent($this->getLayout()->createBlock('blog/adminhtml_article_edit'))
->_addLeft($this->getLayout()->createBlock('blog/adminhtml_article_edit_tabs'));
$this->renderLayout();
}
else
{
Mage::getSingleton('adminhtml/session')
->addError('Article does not exist');
$this->_redirect('*/*/');
}
}
public function newAction()
{
$this->_forward('edit');
}
public function saveAction()
{
if ($this->getRequest()->getPost())
{
try {
$postData = $this->getRequest()->getPost();
//echo "<pre>";print_r($postData); exit;
$articleModel = Mage::getModel('blog/article');
$imgFilename = NULL;
if($_FILES['image']['name'] != '')
{
try {
$uploader = new Varien_File_Uploader('image');
$uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
$uploader->setAllowRenameFiles(false);
$uploader->setFilesDispersion(false);
$uploader->setAllowCreateFolders(true);
// Set media as the upload dir
//$media_path = Mage::getBaseDir('media') . DS;
$media_path = Mage::getBaseDir('media') . DS . 'blog' . DS;
$imgFilename = $media_path . $postData['image'];
// Upload the image
$uploader->save($media_path, $_FILES['image']['name']);
}
catch (Exception $e)
{
Mage::log($e);
$this->_redirectError(502);
}
$data['image'] = $_FILES['image']['name'];
}
else
{
if(isset($data['image']['delete']) && $data['image']['delete'] == 1)
$data['image'] = '';
else
unset($data['image']);
}
$imageModel=Mage::getModel('blog/image');
if( $this->getRequest()->getParam('id') <= 0 )
$articleModel->setCreatedTime(
Mage::getSingleton('core/date')
->gmtDate());
$articleModel
->addData($postData)
->setUpdatedTime(
Mage::getSingleton('core/date')
->gmtDate())
->setId($this->getRequest()->getParam('id'))
->save();
$lastid = $articleModel->getId();
$imageModel->setArticleId($lastid)->setImage($data['image'])->save();
foreach($postData['category_id'] as $cat_id)
{
$categoryModel = Mage::getModel('blog/category');//print_r($categoryModel); exit;
if( $this->getRequest()->getParam('id') <= 0 )
$categoryModel->setCategoryId($cat_id)->setArticleId($lastid)->save();
$categoryModel->setCategoryId($cat_id)->setArticleId($this->getRequest()->getParam('id'))->save();
}
Mage::getSingleton('adminhtml/session')
->addSuccess('successfully saved');
Mage::getSingleton('adminhtml/session')
->setarticleData(false);
$this->_redirect('*/*/');
return;
}
catch (Exception $e)
{
Mage::getSingleton('adminhtml/session')
->addError($e->getMessage());
Mage::getSingleton('adminhtml/session')
->setarticleData($this->getRequest()
->getPost());
$this->_redirect('*/*/edit',
array('id' => $this->getRequest()
->getParam('id')));
return;
}
}
$this->_redirect('*/*/');
}
public function deleteAction()
{
if($this->getRequest()->getParam('id') > 0)
{ $articleId=$this->getRequest()->getParam('id');
try
{
$articleModel = Mage::getModel('blog/article');
$articleModel->setId($this->getRequest()
->getParam('id'))
->delete();
$categoryModel= Mage::getModel('blog/category')
->getCollection()
->addFieldToFilter('article_id', array('eq'=>$articleId));
foreach ($categoryModel as $cat) {
$cat->delete(); }
Mage::getSingleton('adminhtml/session')
->addSuccess('successfully deleted');
$this->_redirect('*/*/');
}
catch (Exception $e)
{
Mage::getSingleton('adminhtml/session')
->addError($e->getMessage());
$this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
}
}
$this->_redirect('*/*/');
}
public function massDeleteAction()
{
$catIds = $this->getRequest()->getParam('id');
if (!is_array($catIds)) {
Mage::getSingleton('adminhtml/session')->addError(Mage::helper('adminhtml')->__('Please select item(s)'));
} else {
try {
foreach ($catIds as $catId) {
$cat = Mage::getModel('blog/article')->load($catId);
$cat->delete();
$categoryModel= Mage::getModel('blog/category')
->getCollection()
->addFieldToFilter('article_id', array('eq'=>$catId));
foreach ($categoryModel as $cat) {
$cat->delete();
}
}
Mage::getSingleton('adminhtml/session')->addSuccess(
Mage::helper('adminhtml')->__('Total of %d record(s) were successfully deleted',
count($catIds))
);
} catch (Exception $e) {
Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
}
}
$this->_redirect('*/*/index');
}
public function massActiveAction()
{
$catIds = $this->getRequest()->getParam('id');
if (!is_array($catIds)) {
Mage::getSingleton('adminhtml/session')->addError($this->__('Please select item(s)'));
} else {
try {
foreach ($catIds as $catId) {
Mage::getSingleton('blog/article')
->load($catId)
->setStatus('Active')
->setIsMassupdate(true)
->save();
}
$this->_getSession()->addSuccess(
$this->__('Total of %d record(s) were successfully updated', count($catIds))
);
} catch (Exception $e) {
$this->_getSession()->addError($e->getMessage());
}
}
$this->_redirect('*/*/index');
}
public function massInactiveAction()
{
$catIds = $this->getRequest()->getParam('id');
if (!is_array($catIds)) {
Mage::getSingleton('adminhtml/session')->addError($this->__('Please select item(s)'));
} else {
try {
foreach ($catIds as $catId) {
Mage::getSingleton('blog/article')
->load($catId)
->setStatus('Inactive')
->setIsMassupdate(true)
->save();
}
$this->_getSession()->addSuccess(
$this->__('Total of %d record(s) were successfully updated', count($catIds))
);
} catch (Exception $e) {
$this->_getSession()->addError($e->getMessage());
}
}
$this->_redirect('*/*/index');
}
}
?>
Here is my form.php
<?php
class Vertax_Blog_Block_Adminhtml_Article_Edit_Tab_Form extends Mage_Adminhtml_Block_Widget_Form
{
protected function _prepareForm()
{
$form = new Varien_Data_Form();
$this->setForm($form);
$form->setDataObject(Mage::registry('article_data'));
$fieldset = $form->addFieldset('article_form',
array('legend'=>'Article Configuration'));
$fieldset->addField('name', 'text',
array(
'label' => 'Name',
'class' => 'required-entry',
'required' => true,
'name' => 'name',
));
$fieldset->addField('status', 'select', array(
'label' => 'Status',
'name' => 'status',
'values' => array(
array(
'value' => 'Active',
'label' => 'Active',
),
array(
'value' => 'Inactive',
'label' => 'Inactive',
),
),
));
$isElementDisabled = false;
if (!Mage::app()->isSingleStoreMode()) {
$fieldset->addField('store_ids', 'multiselect', array(
'name' => 'store_ids[]',
'label' => Mage::helper('cms')->__('Store View'),
'title' => Mage::helper('cms')->__('Store View'),
'required' => true,
'values' => Mage::getSingleton('adminhtml/system_store')->getStoreValuesForForm(false, true),
'disabled' => $isElementDisabled,
'note' => 'Hold CTRL to select multiple'
));
} else {
$fieldset->addField('store_ids', 'hidden', array(
'name' => 'store_ids[]',
'value' => Mage::app()->getStore(true)->getId()
));
}
//TODO: replace the categories
$fieldset->addField('category_ids', 'multiselect', array(
'name' => 'category_ids[]',
'label' => Mage::helper('blog')->__('Categories'),
'title' => Mage::helper('blog')->__('Categories'),
'required' => true,
'class' => 'required-entry',
'values' => Mage::helper('blog')->getCategoryOptionValues(true),
'disabled' => $isElementDisabled,
'index' => 'category_ids',
'note' => 'Hold CTRL to select multiple'
));
/*$wysiwygConfig = Mage::getSingleton('cms/wysiwyg_config')->getConfig(
array('tab_id' => $this->getTabId())
);*/
$configSettings = array( 'add_widgets' => false, 'add_variables' => false, 'add_images' => false, 'files_browser_window_url'=> $this->getBaseUrl().'admin/cms_wysiwyg_images/index/');
$fieldset->addField('summary', 'editor', array(
'name' => 'summary',
'style' => 'min-width:615px;',
'required' => false,
'label' => Mage::helper('cms')->__('Summary'),
'title' => Mage::helper('cms')->__('Summary'),
'config' => $configSettings,
'wysiwyg' => true
));
$fieldset->addField('content_html', 'editor',
array(
'label' => 'Content',
'title' => Mage::helper('cms')->__('Content'),
'style' => 'min-width:615px;',
'class' => 'required-entry',
'required' => true,
'name' => 'content_html',
'config' => $configSettings,
'wysiwyg' => true
));
if (Mage::getSingleton('adminhtml/session')->getBlogPostData()) {
$form->setValues(Mage::getSingleton('adminhtml/session')->getBlogPostData());
Mage::getSingleton('adminhtml/session')->setBlogPostData(null);
} elseif (Mage::registry('article_data')) {
$data = (!Mage::registry('article_data')->getId())? $this->getDefaultValues() : Mage::registry('article_data')->getData();
$form->setValues($data);
}
return parent::_prepareForm();
}
/*if ( Mage::registry('article_data') )
{
$form->setValues(Mage::registry('article_data')->getData());
}
return parent::_prepareForm();
}*/
}
?>
here is the code for the form.php
in edit
<?php
class Vertax_Blog_Block_Adminhtml_Article_Edit_Form extends
Mage_Adminhtml_Block_Widget_Form
{
protected function _prepareForm()
{
$form = new Varien_Data_Form(
array(
'id' => 'edit_form',
'action' => $this->getUrl('*/*/save', array('id' => $this->getRequest()->getParam('id'))),
'method' => 'post',
'enctype' => 'multipart/form-data',
)
);
$form->setUseContainer(true);
$this->setForm($form);
return parent::_prepareForm();
}
protected function _prepareLayout()
{
parent::_prepareLayout();
if (Mage::getSingleton('cms/wysiwyg_config')->isEnabled()) {
$this->getLayout()->getBlock('head')->setCanLoadTinyMce(true);
$this->getLayout()->getBlock('head')->setCanLoadExtJs(true);
}
}
}
?>