<?php
/**
 * Plans Management
 * @package     ci-hotel2
 * @author      Teruki Hosokawa <hosotk@gmail.com>
 * @license     New BSD License
 */
require_once (dirname(__FILE__) . "/ADMIN_Controller.php");

class Plans extends ADMIN_Controller {

    function Plans()
    {
        parent::ADMIN_Controller();
        $this->load->model('User_model');
        $this->load->model('Common_model');
        $this->load->model('Hotel_model');
    }
    
    /**
     * プラン一覧
     */
    public function index($hotelid)
    {
        $this->load->helper(array('form', 'url'));
        //$this->db->order_by('ordering ASC');
        $this->db->where('status >=', 0);
        $query = $this->db->get('plans');
        $data['plans'] = $query->result_array();
        $this->load->vars($data);
        
        $this->load->view('admin/plans/index');
    }
    
    /**
     * プランの新規作成
     */
    public function add($hotelid)
    {
        $this->load->helper(array('form', 'url'));
        
        $this->load->helper('ck');
        
        $data['summary'] = '';
        $data['description'] = '';
        $this->load->vars($data);
        
        $this->db->where('status >=', 0);
        $this->db->where('hotelid', $hotelid);
        $dbres = $this->db->get('roomtypes');
        $ddmenu = array();
        foreach ($dbres->result_array() as $tablerow) {
            $ddmenu[$tablerow['roomtypeid']] = $tablerow['roomtype_name'];
        }
        $data['options'] = $ddmenu;
        
        $this->load->vars($data);
        
        $this->load->library('form_validation');
        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
        
        $this->form_validation->set_rules('plan_name', 'プラン名', 'required|trim|xss_clean');
        $this->form_validation->set_rules('summary', '概要', 'required');
        $this->form_validation->set_rules('description', '説明', '');
        $this->form_validation->set_rules('start_date', '開始日付', 'required|trim|xss_clean');
        $this->form_validation->set_rules('end_date', '終了日時', 'required|trim|xss_clean');
        $this->form_validation->set_rules('default_num', 'デフォルト数', 'required|integer|trim|xss_clean');
        $this->form_validation->set_rules('max_rooms', '最大部屋数', 'required|integer|trim|xss_clean');
        $this->form_validation->set_rules('max_days', '最大連泊数', 'required|integer|trim|xss_clean');
        $this->form_validation->set_rules('guest_min', '最小人数', 'required|integer|trim|xss_clean');
        $this->form_validation->set_rules('guest_max', '最大人数', 'required|integer|trim|xss_clean');
        $this->form_validation->set_rules('roomtypeid', '部屋タイプ', 'required|integer|trim|xss_clean');
        $this->form_validation->set_rules('breakfast', '朝食', 'required|integer|trim|xss_clean');
        $this->form_validation->set_rules('dinner', '夕食', 'required|integer|trim|xss_clean');
        $this->form_validation->set_rules('status', 'ステータス', 'required|integer|trim|xss_clean');
        
        if (!$this->input->post('submit'))
        {
            $data['default_num'] = 1;
            $data['guest_min'] = 1;
            $data['guest_max'] = 1;
            $data['max_rooms'] = 5;
            $data['max_days'] = 9;
            $data['breakfast'] = 0;
            $data['dinner'] = 0;
            $data['status'] = 0;
            $this->load->vars($data);
        }
        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('admin/plans/add');
            return;
        }
        else
        {
            $start_date = set_value('start_date');
            $end_date = set_value('end_date');
            // 日付のチェック
            if ($this->Common_model->checkDate($start_date) == false)
            {
                $data['error'] = array();
                $data['error'][] = '開始日付の形式が不正です。';
                $this->load->vars($data);
                $this->load->view('admin/plans/add');
                return;
            }
            if ($this->Common_model->checkDate($end_date) == false)
            {
                $data['error'] = array();
                $data['error'][] = '終了日付の形式が不正です。';
                $this->load->vars($data);
                $this->load->view('admin/plans/add');
                return;
            }
            // 開始日付 < 終了日付
            if ($start_date >= $end_date)
            {
                $data['error'] = array();
                $data['error'][] = '終了日付は開始日付の翌日以降を指定します。';
                $this->load->vars($data);
                $this->load->view('admin/plans/add');
                return;
            }
            // 最小人数 <= 最大人数
            $guest_min = set_value('guest_min');
            $guest_max = set_value('guest_max');
            if ($guest_min > $guest_max)
            {
                $data['error'] = array();
                $data['error'][] = '最大人数は最小人数以上を指定します。';
                $this->load->vars($data);
                $this->load->view('admin/plans/add');
                return;
            }
            
            $planid = $this->Hotel_model->getPlanId($hotelid);
            
            $meal = 0;
            if (set_value('breakfast') == 1)
            {
                $meal = 1;
            }
            if (set_value('dinner') == 1)
            {
                $meal |= 2;
            }
            
            $data = array(
                           'hotelid' => $hotelid,
                           'planid' => $planid,
                           'plan_name' => set_value('plan_name'),
                           'summary' => $_POST['summary'],
                           'description' => $_POST['description'],
                           'start_date' => set_value('start_date'),
                           'end_date' => set_value('end_date'),
                           'max_rooms' => set_value('max_rooms'),
                           'max_days' => set_value('max_days'),
                           'default_num' => set_value('default_num'),
                           'guest_min' => set_value('guest_min'),
                           'guest_max' => set_value('guest_max'),
                           'roomtypeid' => set_value('roomtypeid'),
                           'meal' => $meal,
                           'status' => set_value('status')
                        );
            $this->db->insert('plans', $data);
            
            redirect('admin/plans/index/' . $hotelid);
        }
    }
    
    /**
     * プランの編集
     */
    public function edit($hotelid, $planid)
    {
        $this->load->helper(array('form', 'url'));
        $this->load->helper('ck');
        
        $data['planid'] = $planid;
        $data['summary'] = '';
        $data['description'] = '';
        $this->load->vars($data);
        
        $this->load->library('form_validation');
        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
        
        // ruleがないときも空でset_rules()しておく。
        // そうでないとset_value()で値が取れなくなる。
        $this->form_validation->set_rules('plan_name', 'プラン名', 'required|trim|xss_clean');
        $this->form_validation->set_rules('summary', '概要', 'required');
        $this->form_validation->set_rules('description', '説明', '');
        $this->form_validation->set_rules('start_date', '開始日付', 'required|trim|xss_clean');
        $this->form_validation->set_rules('end_date', '終了日付', 'required|trim|xss_clean');
        $this->form_validation->set_rules('max_rooms', '最大部屋数', 'required|trim|xss_clean');
        $this->form_validation->set_rules('max_days', '最大連泊数', 'required|trim|xss_clean');
        $this->form_validation->set_rules('default_num', 'デフォルト数', 'required|integer|trim|xss_clean');
        $this->form_validation->set_rules('guest_min', '最小人数', 'required|integer|trim|xss_clean');
        $this->form_validation->set_rules('guest_max', '最大人数', 'required|integer|trim|xss_clean');
        $this->form_validation->set_rules('roomtypeid', '部屋タイプ', 'required|integer|trim|xss_clean');
        $this->form_validation->set_rules('breakfast', '朝食', 'required|integer|trim|xss_clean');
        $this->form_validation->set_rules('dinner', '夕食', 'required|integer|trim|xss_clean');
        $this->form_validation->set_rules('status', 'ステータス', 'required|integer|trim|xss_clean');
        
        $dbres = $this->db->get('roomtypes');
        $ddmenu = array();
        foreach ($dbres->result_array() as $tablerow) {
          $ddmenu[$tablerow['roomtypeid']] = $tablerow['roomtype_name'];
        }
        
        // フォームの初期値設定
        if (!$this->input->post('submit'))
        {
            $this->db->where('planid', $planid);
            $this->db->where('status >', 0);
            $query = $this->db->get('plans', 1);
            if ($query->num_rows() <= 0)
            {
                die();
            }
            $row = $query->row_array();
            $data['plan_name'] = $row['plan_name'];
            $data['summary'] = $row['summary'];
            $data['description'] = $row['description'];
            $data['start_date'] = substr($row['start_date'], 0, 10);
            $data['end_date'] = substr($row['end_date'], 0, 10);
            $data['max_rooms'] = $row['max_rooms'];
            $data['max_days'] = $row['max_days'];
            $data['default_num'] = $row['default_num'];
            $data['guest_min'] = $row['guest_min'];
            $data['guest_max'] = $row['guest_max'];
            $data['roomtypeid'] = $row['roomtypeid'];
            $data['options'] = $ddmenu;
            $data['status'] = $row['status'];
            if ($row['meal'] == 1)
            {
                $data['breakfast'] = 1;
            }
            if (($row['meal'] & 2) == 1)
            {
                $data['dinner'] = 1;
            }
            $this->load->vars($data);
        }
        
        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('admin/plans/edit');
        }
        else
        {
            $start_date = set_value('start_date');
            $end_date = set_value('end_date');
            // 日付のチェック
            if ($this->Common_model->checkDate($start_date) == false)
            {
                $data['error'] = array();
                $data['error'][] = '開始日付の形式が不正です。';
                $this->load->vars($data);
                $this->load->view('admin/plans/edit');
                return;
            }
            if ($this->Common_model->checkDate($end_date) == false)
            {
                $data['error'] = array();
                $data['error'][] = '終了日付の形式が不正です。';
                $this->load->vars($data);
                $this->load->view('admin/plans/edit');
                return;
            }
            // 開始日付 < 終了日付
            if ($start_date >= $end_date)
            {
                $data['error'] = array();
                $data['error'][] = '終了日付は開始日付の翌日以降を指定します。';
                $this->load->vars($data);
                $this->load->view('admin/plans/edit');
                return;
            }
            // 最小人数 <= 最大人数
            $guest_min = set_value('guest_min');
            $guest_max = set_value('guest_max');
            if ($guest_min > $guest_max)
            {
                $data['error'] = array();
                $data['error'][] = '最大人数は最小人数以上を指定します。';
                $this->load->vars($data);
                $this->load->view('admin/plans/add');
                return;
            }
            
            $meal = 0;
            if (set_value('breakfast') == 1)
            {
                $meal = 1;
            }
            if (set_value('dinner') == 1)
            {
                $meal |= 2;
            }
            
            $data = array(
                           'plan_name' => set_value('plan_name'),
                           'summary' => $_POST['summary'],
                           'description' => $_POST['description'],
                           'start_date' => set_value('start_date'),
                           'end_date' => set_value('end_date'),
                           'default_num' => set_value('default_num'),
                           'max_rooms' => set_value('max_rooms'),
                           'max_days' => set_value('max_days'),
                           'guest_min' => set_value('guest_min'),
                           'guest_max' => set_value('guest_max'),
                           'roomtypeid' => set_value('roomtypeid'),
                           'meal' => $meal,
                           'status' => set_value('status')
                        );
            $this->db->where('planid', $planid);
            $this->db->update('plans', $data);
            
            redirect('admin/plans/index/' . $hotelid);
        }
    }
    
    /**
     * プランの削除
     */
    public function delete($hotelid, $planid)
    {
        // データを削除せずにstatusに-1をセットする。
        $data = array('status' => -1);
        $this->db->where('planid', $planid);
        $this->db->update('plans', $data);
        
        // 関連テーブルはそのままにしておく
        
        redirect('admin/plans/index/' . $hotelid);
    }
}

/* End of file plans.php */
