<?php
/**
* @Copyright Copyright (C) 2010 dhtmlx.com
* @license GNU/GPL http://www.gnu.org/copyleft/gpl.html
**/

/**
 This file is part of dhtmlxScheduler for Joomla.

    dhtmlxScheduler for Joomla is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    dhtmlxScheduler for Joomla is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with dhtmlxScheduler for Joomla.  If not, see <http://www.gnu.org/licenses/>.
**/

defined( '_JEXEC' ) or die( 'Restricted access' );

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

class SchedulersController extends JController
{
	private $scheduler_log = false;

	function __construct() {
		parent::__construct();
		$this->registerTask( 'save' , 'save' );
		$this->registerTask( 'cancel' , 'cancel' );
		$config =& JFactory::getConfig();
	}


	function save() {
		$link = 'index.php?option=com_scheduler';
		$model = $this->getModel('schedulers');
		$data_post = JRequest::get('post');
		$data['name'] = 'scheduler_xml';
		$data['value'] = $data_post['scheduler_xml'];
		$model->store($data);
		$data['name'] = 'scheduler_xml_version';
		$data['value'] = $data_post['scheduler_xml_version'];
		$model->store($data);
		$this->setRedirect($link);
	}

	function cancel() {
		$this->setRedirect( 'index.php');
	}

	function default_xml() {
		$model = $this->getModel('schedulers');
		$data_post = JRequest::get('post');
		$data['name'] = 'scheduler_xml';
		$data['value'] = '&ltesc;config&gtesc;&ltesc;active_tab&gtesc;a1&ltesc;/active_tab&gtesc;&ltesc;settings&gtesc;&ltesc;settings_width&gtesc;680px&ltesc;/settings_width&gtesc;&ltesc;settings_height&gtesc;600px&ltesc;/settings_height&gtesc;&ltesc;settings_eventnumber&gtesc;2&ltesc;/settings_eventnumber&gtesc;&ltesc;settings_link&gtesc;&ltesc;/settings_link&gtesc;&ltesc;settings_posts&gtesc;false&ltesc;/settings_posts&gtesc;&ltesc;settings_repeat&gtesc;true&ltesc;/settings_repeat&gtesc;&ltesc;settings_firstday&gtesc;false&ltesc;/settings_firstday&gtesc;&ltesc;settings_multiday&gtesc;true&ltesc;/settings_multiday&gtesc;&ltesc;settings_singleclick&gtesc;false&ltesc;/settings_singleclick&gtesc;&ltesc;settings_day&gtesc;true&ltesc;/settings_day&gtesc;&ltesc;settings_week&gtesc;true&ltesc;/settings_week&gtesc;&ltesc;settings_month&gtesc;true&ltesc;/settings_month&gtesc;&ltesc;settings_agenda&gtesc;false&ltesc;/settings_agenda&gtesc;&ltesc;settings_year&gtesc;false&ltesc;/settings_year&gtesc;&ltesc;settings_defaultmode&gtesc;month&ltesc;/settings_defaultmode&gtesc;&ltesc;settings_debug&gtesc;false&ltesc;/settings_debug&gtesc;&ltesc;settings_eventnumber&gtesc;2&ltesc;/settings_eventnumber&gtesc;&ltesc;settings_collision&gtesc;false&ltesc;/settings_collision&gtesc;&ltesc;settings_expand&gtesc;true&ltesc;/settings_expand&gtesc;&ltesc;settings_print&gtesc;false&ltesc;/settings_print&gtesc;&ltesc;settings_minical&gtesc;false&ltesc;/settings_minical&gtesc;&ltesc;/settings&gtesc;&ltesc;access&gtesc;&ltesc;access_guestView_j&gtesc;true&ltesc;/access_guestView_j&gtesc;&ltesc;access_guestAdd_j&gtesc;false&ltesc;/access_guestAdd_j&gtesc;&ltesc;access_guestEdit_j&gtesc;false&ltesc;/access_guestEdit_j&gtesc;&ltesc;access_registeredView_j&gtesc;true&ltesc;/access_registeredView_j&gtesc;&ltesc;access_registeredAdd_j&gtesc;false&ltesc;/access_registeredAdd_j&gtesc;&ltesc;access_registeredEdit_j&gtesc;false&ltesc;/access_registeredEdit_j&gtesc;&ltesc;access_authorView_j&gtesc;true&ltesc;/access_authorView_j&gtesc;&ltesc;access_authorAdd_j&gtesc;false&ltesc;/access_authorAdd_j&gtesc;&ltesc;access_authorEdit_j&gtesc;false&ltesc;/access_authorEdit_j&gtesc;&ltesc;access_editorView_j&gtesc;true&ltesc;/access_editorView_j&gtesc;&ltesc;access_editorAdd_j&gtesc;false&ltesc;/access_editorAdd_j&gtesc;&ltesc;access_editorEdit_j&gtesc;false&ltesc;/access_editorEdit_j&gtesc;&ltesc;access_publisherView_j&gtesc;true&ltesc;/access_publisherView_j&gtesc;&ltesc;access_publisherAdd_j&gtesc;false&ltesc;/access_publisherAdd_j&gtesc;&ltesc;access_publisherEdit_j&gtesc;false&ltesc;/access_publisherEdit_j&gtesc;&ltesc;access_managerView_j&gtesc;true&ltesc;/access_managerView_j&gtesc;&ltesc;access_managerAdd_j&gtesc;true&ltesc;/access_managerAdd_j&gtesc;&ltesc;access_managerEdit_j&gtesc;true&ltesc;/access_managerEdit_j&gtesc;&ltesc;access_administratorView_j&gtesc;true&ltesc;/access_administratorView_j&gtesc;&ltesc;access_administratorAdd_j&gtesc;true&ltesc;/access_administratorAdd_j&gtesc;&ltesc;access_administratorEdit_j&gtesc;true&ltesc;/access_administratorEdit_j&gtesc;&ltesc;access_superadministratorView_j&gtesc;true&ltesc;/access_superadministratorView_j&gtesc;&ltesc;access_superadministratorAdd_j&gtesc;true&ltesc;/access_superadministratorAdd_j&gtesc;&ltesc;access_superadministratorEdit_j&gtesc;true&ltesc;/access_superadministratorEdit_j&gtesc;&ltesc;privatemode&gtesc;off&ltesc;/privatemode&gtesc;&ltesc;/access&gtesc;&ltesc;templates&gtesc;&ltesc;templates_defaultdate&gtesc;&ltesc;![CDATA[%d %M %Y]]&gtesc;&ltesc;/templates_defaultdate&gtesc;&ltesc;templates_monthdate&gtesc;&ltesc;![CDATA[%F %Y]]&gtesc;&ltesc;/templates_monthdate&gtesc;&ltesc;templates_weekdate&gtesc;&ltesc;![CDATA[%l]]&gtesc;&ltesc;/templates_weekdate&gtesc;&ltesc;templates_daydate&gtesc;&ltesc;![CDATA[%d/%m/%Y]]&gtesc;&ltesc;/templates_daydate&gtesc;&ltesc;templates_hourdate&gtesc;&ltesc;![CDATA[%H:%i]]&gtesc;&ltesc;/templates_hourdate&gtesc;&ltesc;templates_monthday&gtesc;&ltesc;![CDATA[%d]]&gtesc;&ltesc;/templates_monthday&gtesc;&ltesc;templates_minmin&gtesc;&ltesc;![CDATA[5]]&gtesc;&ltesc;/templates_minmin&gtesc;&ltesc;templates_hourheight&gtesc;&ltesc;![CDATA[40]]&gtesc;&ltesc;/templates_hourheight&gtesc;&ltesc;templates_starthour&gtesc;&ltesc;![CDATA[0]]&gtesc;&ltesc;/templates_starthour&gtesc;&ltesc;templates_endhour&gtesc;&ltesc;![CDATA[24]]&gtesc;&ltesc;/templates_endhour&gtesc;&ltesc;templates_agendatime&gtesc;&ltesc;![CDATA[30]]&gtesc;&ltesc;/templates_agendatime&gtesc;&ltesc;templates_eventtext&gtesc;&ltesc;![CDATA[return event.text;]]&gtesc;&ltesc;/templates_eventtext&gtesc;&ltesc;templates_eventheader&gtesc;&ltesc;![CDATA[return scheduler.templates.hour_scale(start) + " - " + scheduler.templates.hour_scale(end);]]&gtesc;&ltesc;/templates_eventheader&gtesc;&ltesc;templates_eventbartext&gtesc;&ltesc;![CDATA[return "&ltesc;span title=\'"+event.text+"\'&gtesc;" + event.text + "&ltesc;/span&gtesc;";]]&gtesc;&ltesc;/templates_eventbartext&gtesc;&ltesc;/templates&gtesc;&ltesc;customfields&gtesc;&ltesc;customfield name="Text" dsc="Description" type="textarea" old_name="Text" use_colors="false" units="false" height="150" /&gtesc;&ltesc;/customfields&gtesc;&ltesc;/config&gtesc;';
		$model->store($data);
		$data['name'] = 'scheduler_xml_version';
		$data['value'] = $data_post['scheduler_xml_version'];
		$model->store($data);
		$link = 'index.php?option=com_scheduler';
		$this->setRedirect($link);
	}


	function loadxml() {
		require_once(JPATH_ROOT.DS.'components'.DS.'com_scheduler'.DS.'codebase'.DS.'dhtmlxSchedulerConfigurator.php');
		$table = 'scheduler_options';
		$tableEvents = 'events_rec';
		$fieldName = 'name';
		$fieldValue = 'value';
		$userIdField = 'id';
		$userLoginField = 'username';
		$tableUsers = 'users';

		$user =& JFactory::getUser();
		$usertype = $this->getUser();
		$userid = $user->id;

		$cfg = new JConfig;
		$prefix = $cfg->dbprefix;
		$res = mysql_connect($cfg->host, $cfg->user, $cfg->password);
		if (!$res) {
			die('Incorrect db configuration');
		}
		mysql_select_db($cfg->db, $res);

		$cfg = new SchedulerConfig('scheduler_config_xml', $res, $table, $fieldName, $fieldValue, $tableEvents, $userIdField, $userLoginField, $tableUsers, $prefix, $userid, true);
		if (isset($_GET['config_xml'])) {
			header('Content-type: text/xml');
			echo $cfg->getXML();
			die();
		} else {
			if (isset($_GET['grid_events'])) {
				$cfg->getEventsRecGrid(true);
			} else {
				if (isset($_GET['scheduler_events'])) {
					$cfg->getEventsRec($usertype, true);
					return "";
				}
			}
		}
	}

	function getUser() {
		$user =& JFactory::getUser();
		switch($user->usertype) {
			case '':
				$usertype = 'guest';
				break;
			case 'Super Administrator':
				$usertype = 'superadministrator';
				break;
			default:
				$usertype = strtolower($user->usertype);
			}
		return $usertype;
	}

}