<?php
defined('_JEXEC') or die('Restricted access');
jimport('joomla.application.component.controller');
/**
 * Standard Joomla 1.5.x native component controller
 *
 * @version    1.0.0
 * @author 	   ARTIO s.r.o., http://www.artio.net
 * @package    Charts
 * @copyright  Copyright (C) 2009 ARTIO s.r.o.. All rights reserved.
 * @since      Version 1.0.0
 */
class ARTIOFusionChartsController extends JController
{
    /**
     * Display page by task type
     *
     */
    function display ()
    {
        switch ($this->getTask()) {
            case 'add':
                JRequest::setVar('hidemainmenu', 1);
                JRequest::setVar('layout', 'form');
                JRequest::setVar('view', 'artiofusionchart');
                JRequest::setVar('edit', false);
                $model = $this->getModel('artiofusionchart');
                $model->checkout();
                break;
            case 'edit':
                JRequest::setVar('hidemainmenu', 1);
                JRequest::setVar('layout', 'form');
                JRequest::setVar('view', 'artiofusionchart');
                JRequest::setVar('edit', true);
                $model = $this->getModel('artiofusionchart');
                $model->checkout();
                break;
            case 'create':
                JRequest::setVar('view', 'create');
                break;
        }
        parent::display();
    }
    /**
     * Cancel chart editing out of saving changes
     *
     */
    function cancel ()
    {
        $this->_checkin();
        $this->_homePage();
    }
    /**
     * Go to componet home page
     *
     * @param string $msg
     */
    function _homePage ($msg = '')
    {
        $mainframe = &JFactory::getApplication();
        $link = 'index.php?option=' . ARTIOFUSIONCHARTS_OPTION;
        $mainframe->redirect($link, $msg);
    }
    /**
     * Apply chart editing changes and stay on editing page 
     *
     */
    function apply ()
    {
        $this->save(true);
    }
    /**
     * Save editing changes
     *
     * @param boolean $apply true: stay on editing page, 
     *                       false: go to home page and check out chart
     */
    function save ($apply = false)
    {
        JRequest::checkToken() or jexit('Invalid Token');
        $post = JRequest::get('post');
        $cid = JRequest::getVar('cid', array(0), 'post', 'array');
        $post['id'] = (int) $cid[0];
        $model = $this->getModel('artiofusionchart');
        if (($id = $model->store($post)) != 0) {
            $msg = JText::_('Chart Saved');
        } else {
            $msg = JText::_('Error Saving Chart');
        }
        if (! $apply) {
            $this->_checkin();
            $this->_homePage($msg);
        } else {
            $link = ARTIOFusionChartsHelperRoute::getEditRoute($id);
            $mainframe = &JFactory::getApplication();
            $mainframe->redirect($link, $msg);
        }
    }
    /**
     * Remove chart from db
     *
     */
    function remove ()
    {
        $model = $this->getModel('artiofusionchart');
        $model->remove();
        $this->_homePage();
    }
    /**
     * Checkin chart on ending editing
     *
     */
    private function _checkin ()
    {
        $model = $this->getModel('artiofusionchart');
        $model->checkin();
    }
}
