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

/**
 */
class Plantooptions extends ADMIN_Controller {
    
    function Plantooptions()
    {
        parent::ADMIN_Controller();
        $this->load->model('User_model');
        $this->load->model('Hotel_model');
    }
    
    /**
     * オプション一覧
     */
    public function index($hotelid, $planid)
    {
        $this->load->library('session');
        $this->load->helper(array('form', 'url'));
        
        $data['hotelid'] = $hotelid;
        $data['planid'] = $planid;
        $this->db->where('hotelid', $hotelid);
        $query = $this->db->get('options');
        $data['options'] = $query->result_array();
        $this->load->vars($data);
        
        if (isset($_POST['submit']))
        {
            // 一旦削除する。
            $this->db->where('hotelid', $hotelid);
            $this->db->where('planid', $planid);
            $this->db->delete('plan_to_options');
            
            if (isset($_POST['options']) != null)
            {
                $now = date("Y-m-d H:i:s");
                $optionids = $_POST['options'];
                foreach ($optionids as $optionid)
                {
                    $data = array(
                       'hotelid' => $hotelid,
                       'planid' => $planid,
                       'optionid' => $optionid,
                       'created' => $now
                    );
                    $this->db->insert('plan_to_options', $data); 
                }
            }
        }
        
        $options = array();
        $this->db->where('hotelid', $hotelid);
        $this->db->where('planid', $planid);
        $query = $this->db->get('plan_to_options');
        foreach ($query->result() as $row)
        {
            $options[] = $row->optionid;
        }
        $data['items'] = $options;
        $this->load->vars($data);
        
        $this->load->view('admin/plantooptions/index');
    }
    
}
/* End of file plantooptions.php */
