<?php
/**
 * templatedate.php - Controller for template date.
 *
 * @author $Author: dtong $
 * @version $Id: templatedate.php,v 1.1 2011/04/19 04:04:47 dtong Exp $
 * @copyright Copyright (c) 2011, Tiller Software Co., Ltd.
*/

class Templatedate extends CI_Controller{
	function __construct() {
		parent::__construct();
	 	/* if ( !$this->login_model->isAuthenticated() ) {
         echo osa_ajaxmsg_redirecthome(lang('gen_js_ajaxsessiontimeoutmsg'));
         exit;
      } */
      $this->access_model->checkaccess_any('unit_read');
		$this->load->model('unit/uph/template_date_model');
		osa_load_lang('template');
	}

	function update($type, $extid) {
		$data = new stdClass();
		$data->date = FALSE;

		foreach($_REQUEST as $key => $value) {
			if ( strpos($key,'templatedatetext') === 0 ) {
				//Always expect one date value wth the key beining as templatedatetext
				$tmp = trim($value);
				if (osa_valid_date_str($tmp)) {
					$data->date = $tmp;
					break;
				}
			}
		}

		if ( !$data->date ) {
			echo osa_ajaxmsg(lang('template_date_invalid_date_msg'));
			exit;
		}

		if( isset($extid) && $extid != FALSE ) {
			$data->extid = $extid;
		}

		if( isset($type) && $type !== FALSE ) {
			$data->type = $type;
		}

		if ( !$this->template_date_model->add_template_date($data) ) {
			echo osa_ajaxmsg(lang('template_date_failed_update_msg'));
			exit;
		}
	}
}