<?php

// no direct access
defined('_JEXEC') or die;

jimport('joomla.application.component.controller');

class SPStgSrvControllerInstaller extends JControllerLegacy {

    public function install() {
        //login
        $app = JFactory::getApplication();
        
        error_reporting(0);

        $model = $this->getModel('installer');

        $params = &JComponentHelper::getParams('com_spstgsrv');
        $data['package_path'] = $params->get('package_path').'components/com_spstgmng/packages/';
        $data['package_name'] = JRequest::getString('package_name');

        $ip = $model->getIP();
        $message = $model->install($data);
        $data['package_name'] = $model->package_name;
        $data['version'] = $model->version;
        if ($message != 'success') {
            $data['result'] = 0;
            $data['message'] = $message;
            $data['ip'] = $ip;
            $model->save($data);            
            $app->logout();            
            $app->redirect(JURI::root().'index.php?option=com_spstgsrv&tmpl=component&view=fail&message='.$data['message']);            
            return false;
        }

        $data['ip'] = $ip;
        $data['result'] = 1;
        $data['message'] = JText::_('COM_SPSTGSRV_INSTALL_SUCCESS');
        $model->save($data);      
        $app->logout();
        $app->redirect(JURI::root().'index.php?option=com_spstgsrv&tmpl=component&view=success');        
        return true;
    }
    
    public function update() {
        //login
        $app = JFactory::getApplication();

        error_reporting(0);

        $model = $this->getModel('installer');

        $params = &JComponentHelper::getParams('com_spstgsrv');
        $data['package_path'] = $params->get('package_path').'components/com_spstgmng/packages/';
        $data['extension_id'] = JRequest::getString('extension_id');        

        $ip = $model->getIP();
        $message = $model->update($data);
        $data['package_name'] = $model->package_name;
        $data['version'] = $model->version;
        if ($message != 'success') {
            $data['result'] = 0;
            $data['message'] = $message;
            $data['ip'] = $ip;
            $model->save($data);            
            $app->logout();            
            $app->redirect(JURI::root().'index.php?option=com_spstgsrv&tmpl=component&view=fail&message='.$data['message']);            
            return false;
        }

        $data['ip'] = $ip;
        $data['result'] = 1;
        $data['message'] = JText::_('COM_SPSTGSRV_UPDATE_SUCCESS');
        $model->save($data);          
        $app->logout();
        $app->redirect(JURI::root().'index.php?option=com_spstgsrv&tmpl=component&view=success');        
        return true;
    }

    public function getModel($name = '', $prefix = '', $config = array('ignore_request' => true)) {
        return parent::getModel($name, $prefix, array('ignore_request' => false));
    }

}

?>
