2

IndexController.php

<?php
class Dcgpac_Sitemaps_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    {

      $this->loadLayout()
            ->_addContent($this->getLayout()->createBlock('sitemaps/admin_main'))
            ->renderLayout();                   
    }

   public function newAction()
    {
        $this->loadLayout()
        ->_addContent($this->getLayout()->createBlock('sitemaps/admin_new'))
        ->renderLayout();
    }

    public function editAction()
    {
        $this->loadLayout();

        $this->_addContent($this->getLayout()->createBlock('sitemaps/admin_edit'));

        $this->renderLayout();
    }

    public function saveAction()
    {
        $params = $this->getRequest()->getParams();

        if(!$params)
        {
            Mage::getSingleton('adminhtml/session')->addNotice($this->__('None specified parameter'));
            $this->getResponse()->setRedirect($this->getUrl('*/*/'));
        } 
        else
        {
            if(Mage::getModel('sitemaps/sitemap')->genSitemap($params, 'edit'))
            {
                Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('sitemaps')->__('
XML successfully updated.'));
                $this->getResponse()->setRedirect($this->getUrl('*/*/'));
            }
            else
            {
                Mage::getSingleton('adminhtml/session')->addNotice($this->__('Error generating file.'));
                $this->getResponse()->setRedirect($this->getUrl('*/*/'));
            }
        }
    }

    public function updateAction()
    {
        $sitemapId = $this->getRequest()->getParam('id', false);

        $sitemap = Mage::getModel('sitemaps/sitemap')->load($sitemapId);

        $type = Mage::getModel('sitemaps/type')->load($sitemap->getType());
        $fields = json_decode($type->getFields());

        $params['sitemap_id'] = $sitemapId;
        $params['type'] = $type->getName();
        $params['filename'] = $sitemap->getFilename();
        $params['path'] = $sitemap->getPath();

        foreach ($fields as $key => $value)
        {
            if($key === 'master')
            {
                foreach($value as $k => $ff)
                {
                    if($k !== 'product')
                    {
                        $params['field']['master']['code'] = $value->code;
                    }
                    else
                    {
                        $params['field']['master']['product']['code'] = $ff->code;
                    }
                }
            }

            if($key === 'default')
            {
                foreach ($value as $k => $v)
                {
                    $params['field']['default'][$k]['code'] = $v->code;
                    $params['field']['default'][$k]['value'] = $v->value;
                }
            }

            if($key === 'custom')
            {
                foreach ($value as $k => $v)
                {
                    $params['field']['custom'][$k]['code'] = $v->code;
                    $params['field']['custom'][$k]['value'] = $v->value;
                }
            }
        }

        if(Mage::getModel('sitemaps/sitemap')->genSitemap($params, 'edit'))
        {
            Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('sitemaps')->__('XML successfully updated.'));
            $this->getResponse()->setRedirect($this->getUrl('*/*/'));
        }
        else
        {
            Mage::getSingleton('adminhtml/session')->addNotice($this->__('Error generating file.'));
            $this->getResponse()->setRedirect($this->getUrl('*/*/'));
        }
    }

    public function genAction()
    {
        $params = $this->getRequest()->getParams();

        if(!$params)
        {
            Mage::getSingleton('adminhtml/session')->addNotice($this->__('Nenhum parâmetro especificado'));
            $this->getResponse()->setRedirect($this->getUrl('*/*/'));
        }
        else
        {
            if(Mage::getModel('sitemaps/sitemap')->genSitemap($params))
            {
                Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('sitemaps')->__('XML successfully generated.'));
                $this->getResponse()->setRedirect($this->getUrl('*/*/'));
            }
            else
            {
                Mage::getSingleton('adminhtml/session')->addNotice($this->__('Error generating file.'));
                $this->getResponse()->setRedirect($this->getUrl('*/*/'));
            }
        }
    }

    public function deleteAction()
    {       
        $sitemapId = $this->getRequest()->getParam('id', false);

        try {

            $sitemap = Mage::getModel('sitemaps/sitemap')->load($sitemapId);

            $data = $sitemap->getData();

            $io = new Varien_Io_File();

            $io->open(array('path' => Mage::getBaseDir().'/'.$data["path"]));

            if($io->fileExists($data["filename"]))
            {
                $io->rm($data["filename"]);
            }

            $io->close();

            Mage::getModel('sitemaps/sitemap')->setId($sitemapId)->delete();

            Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('sitemaps')->__('XML deleted successfully.'));

            $this->getResponse()->setRedirect($this->getUrl('*/*/'));

        } 
        catch (Exception $e) {
            Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
        }

    }
}

Can any one please help.

7
  • 1
    what's the name of the undefined method?
    – Marius
    Jul 12, 2016 at 7:28
  • _IndexController::_addContent() Jul 12, 2016 at 7:30
  • can you post the full error message?
    – Marius
    Jul 12, 2016 at 7:32
  • Fatal error: Call to undefined method Namespace_Modulename_IndexController::_addContent() Jul 12, 2016 at 7:33
  • 3
    _addContent() method exists only in Mage_Adminhtml_Controller_Action Jul 12, 2016 at 7:36

1 Answer 1

6

Method you are trying to call does not exist in the class you are extending. _addContent() exists only in admin controller Mage_Adminhtml_Controller_Action class. Method itself is pretty simple.

protected function _addContent(Mage_Core_Block_Abstract $block)
{
    $this->getLayout()->getBlock('content')->append($block);
    return $this;
}

So you can either call:

$this->loadLayout();
$this->getLayout()->getBlock('content')->append($block);

instead of _addContent() or create your own layout update xml file and do everything there.

2
  • I have applied your solution, but this is giving me this Fatal error: Call to a member function append() on boolean Jul 12, 2016 at 8:36
  • You have to call loadLayout() first. I've updated the answer. Jul 12, 2016 at 9:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.