<?php
/**
 * Netgo_News extension
 * 
 * NOTICE OF LICENSE
 * 
 * This source file is subject to the MIT License
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/mit-license.php
 * 
 * @category       Netgo
 * @package        Netgo_News
 * @copyright      Copyright (c) 2015
 * @license        http://opensource.org/licenses/mit-license.php MIT License
 */
/**
 * News admin controller
 *
 * @category    Netgo
 * @package     Netgo_News
 * @author      Ultimate Module Creator
 */
class Netgo_News_Adminhtml_News_NewsController
    extends Netgo_News_Controller_Adminhtml_News {
    /**
     * init the news
     * @access protected
     * @return Netgo_News_Model_News
     */
    protected function _initNews(){
        $newsId  = (int) $this->getRequest()->getParam('id');
        $news    = Mage::getModel('netgo_news/news');
        if ($newsId) {
            $news->load($newsId);
        }
        Mage::register('current_news', $news);
        return $news;
    }
     /**
     * default action
     * @access public
     * @return void
     * @author Ultimate Module Creator
     */
    public function indexAction() {
        $this->loadLayout();
        $this->_title(Mage::helper('netgo_news')->__('Manage News'))
             ->_title(Mage::helper('netgo_news')->__('News'));
        $this->renderLayout();
    }
    /**
     * grid action
     * @access public
     * @return void
     * @author Ultimate Module Creator
     */
    public function gridAction() {
        $this->loadLayout()->renderLayout();
    }
    /**
     * edit news - action
     * @access public
     * @return void
     * @author Ultimate Module Creator
     */
    public function editAction() {
        $newsId    = $this->getRequest()->getParam('id');
        $news      = $this->_initNews();
        if ($newsId && !$news->getId()) {
            $this->_getSession()->addError(Mage::helper('netgo_news')->__('This news no longer exists.'));
            $this->_redirect('*/*/');
            return;
        }
        $data = Mage::getSingleton('adminhtml/session')->getNewsData(true);
        if (!empty($data)) {
            $news->setData($data);
        }
        Mage::register('news_data', $news);
        $this->loadLayout();
        $this->_title(Mage::helper('netgo_news')->__('Manage News'))
             ->_title(Mage::helper('netgo_news')->__('News'));
        if ($news->getId()){
            $this->_title($news->getTitle());
        }
        else{
            $this->_title(Mage::helper('netgo_news')->__('Add news'));
        }
        if (Mage::getSingleton('cms/wysiwyg_config')->isEnabled()) {
            $this->getLayout()->getBlock('head')->setCanLoadTinyMce(true);
        }
        $this->renderLayout();
    }
    /**
     * new news action
     * @access public
     * @return void
     * @author Ultimate Module Creator
     */
    public function newAction() {
        $this->_forward('edit');
    }
    /**
     * save news - action
     * @access public
     * @return void
     * @author Ultimate Module Creator
     */
    public function saveAction() {
        if ($data = $this->getRequest()->getPost('news')) {
            try {
                $news = $this->_initNews();
                $news->addData($data);
                $imageName = $this->_uploadAndGetName('image', Mage::helper('netgo_news/news_image')->getImageBaseDir(), $data);
                $news->setData('image', $imageName);
                $news->save();
                Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('netgo_news')->__('News was successfully saved'));
                Mage::getSingleton('adminhtml/session')->setFormData(false);
                if ($this->getRequest()->getParam('back')) {
                    $this->_redirect('*/*/edit', array('id' => $news->getId()));
                    return;
                }
                $this->_redirect('*/*/');
                return;
            }
            catch (Mage_Core_Exception $e){
                if (isset($data['image']['value'])){
                    $data['image'] = $data['image']['value'];
                }
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
                Mage::getSingleton('adminhtml/session')->setNewsData($data);
                $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
                return;
            }
            catch (Exception $e) {
                Mage::logException($e);
                if (isset($data['image']['value'])){
                    $data['image'] = $data['image']['value'];
                }
                Mage::getSingleton('adminhtml/session')->addError(Mage::helper('netgo_news')->__('There was a problem saving the news.'));
                Mage::getSingleton('adminhtml/session')->setNewsData($data);
                $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
                return;
            }
        }
        Mage::getSingleton('adminhtml/session')->addError(Mage::helper('netgo_news')->__('Unable to find news to save.'));
        $this->_redirect('*/*/');
    }
    /**
     * delete news - action
     * @access public
     * @return void
     * @author Ultimate Module Creator
     */
    public function deleteAction() {
        if( $this->getRequest()->getParam('id') > 0) {
            try {
                $news = Mage::getModel('netgo_news/news');
                $news->setId($this->getRequest()->getParam('id'))->delete();
                Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('netgo_news')->__('News was successfully deleted.'));
                $this->_redirect('*/*/');
                return;
            }
            catch (Mage_Core_Exception $e){
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
                $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
            }
            catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError(Mage::helper('netgo_news')->__('There was an error deleting news.'));
                $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
                Mage::logException($e);
                return;
            }
        }
        Mage::getSingleton('adminhtml/session')->addError(Mage::helper('netgo_news')->__('Could not find news to delete.'));
        $this->_redirect('*/*/');
    }
    /**
     * mass delete news - action
     * @access public
     * @return void
     * @author Ultimate Module Creator
     */
    public function massDeleteAction() {
        $newsIds = $this->getRequest()->getParam('news');
        if(!is_array($newsIds)) {
            Mage::getSingleton('adminhtml/session')->addError(Mage::helper('netgo_news')->__('Please select news to delete.'));
        }
        else {
            try {
                foreach ($newsIds as $newsId) {
                    $news = Mage::getModel('netgo_news/news');
                    $news->setId($newsId)->delete();
                }
                Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('netgo_news')->__('Total of %d news were successfully deleted.', count($newsIds)));
            }
            catch (Mage_Core_Exception $e){
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
            }
            catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError(Mage::helper('netgo_news')->__('There was an error deleting news.'));
                Mage::logException($e);
            }
        }
        $this->_redirect('*/*/index');
    }
    /**
     * mass status change - action
     * @access public
     * @return void
     * @author Ultimate Module Creator
     */
    public function massStatusAction(){
        $newsIds = $this->getRequest()->getParam('news');
        if(!is_array($newsIds)) {
            Mage::getSingleton('adminhtml/session')->addError(Mage::helper('netgo_news')->__('Please select news.'));
        }
        else {
            try {
                foreach ($newsIds as $newsId) {
                $news = Mage::getSingleton('netgo_news/news')->load($newsId)
                            ->setStatus($this->getRequest()->getParam('status'))
                            ->setIsMassupdate(true)
                            ->save();
                }
                $this->_getSession()->addSuccess($this->__('Total of %d news were successfully updated.', count($newsIds)));
            }
            catch (Mage_Core_Exception $e){
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
            }
            catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError(Mage::helper('netgo_news')->__('There was an error updating news.'));
                Mage::logException($e);
            }
        }
        $this->_redirect('*/*/index');
    }
    /**
     * export as csv - action
     * @access public
     * @return void
     * @author Ultimate Module Creator
     */
    public function exportCsvAction(){
        $fileName   = 'news.csv';
        $content    = $this->getLayout()->createBlock('netgo_news/adminhtml_news_grid')->getCsv();
        $this->_prepareDownloadResponse($fileName, $content);
    }
    /**
     * export as MsExcel - action
     * @access public
     * @return void
     * @author Ultimate Module Creator
     */
    public function exportExcelAction(){
        $fileName   = 'news.xls';
        $content    = $this->getLayout()->createBlock('netgo_news/adminhtml_news_grid')->getExcelFile();
        $this->_prepareDownloadResponse($fileName, $content);
    }
    /**
     * export as xml - action
     * @access public
     * @return void
     * @author Ultimate Module Creator
     */
    public function exportXmlAction(){
        $fileName   = 'news.xml';
        $content    = $this->getLayout()->createBlock('netgo_news/adminhtml_news_grid')->getXml();
        $this->_prepareDownloadResponse($fileName, $content);
    }
    /**
     * Check if admin has permissions to visit related pages
     * @access protected
     * @return boolean
     * @author Ultimate Module Creator
     */
    protected function _isAllowed() {
        return Mage::getSingleton('admin/session')->isAllowed('cms/netgo_news/news');
    }
}
