<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class ADMIN_Controller extends Controller {
    function ADMIN_Controller()
    {
        parent::Controller();
        
        $this->load->helper('MY_url');
        
        // 共通の処理をここに書く
        // ...
        
        $this->load->library('phpsession');
        
        // 設定をロードし、セッションに保存する。
        //$system = $this->phpsession->get('system');
        //if ($system != null)
        //{
            $system = array();
            $query = $this->db->get('system');
            foreach ($query->result() as $row)
            {
                $system[$row->name] = $row->value;
            }
            $this->phpsession->save('system', $system);
        //}
        
        $admin = $this->phpsession->get('admin');
        if (is_null($admin))
        {
            // 管理者ログインページへ
            if (isset($system['ssl']) && $system['ssl'] == 'true')
            {
                redirect('admin/login', 'location', 302, true);
            }
            else
            {
                redirect('admin/login');
            }
        }
        
        $base_url = base_url();
        $current_url = current_url();
        //log_message('debug', 'base_url=' . $base_url);
        //log_message('debug', 'current_url=' . $current_url);
        
        $url = substr($current_url, strlen($base_url));
        //log_message('debug', 'url=' . $url);
        $a = explode('/', $url);
        $hotelid = $a[3];
        //log_message('debug', 'hotelid=' . $hotelid);
        
        // ckfinder 設定
        $ckfinder_BaseUrl = CIHOTEL_BASE . 'userfiles/' . $hotelid . '/';
        log_message('debug', 'ADMIN_Controller:ckfinder_BaseUrl=' . $ckfinder_BaseUrl);
        $this->phpsession->save('ckfinder_BaseUrl', $ckfinder_BaseUrl);
        
        if ($admin['hotelid'] != $hotelid)
        {
            die('oops! invalid hotelid');
        }
        
        $this->db->where('hotelid', $hotelid);
        $query = $this->db->get('hotels', 1);
        if ($query->num_rows() > 0)
        {
            $data['hotel_spec'] = $query->row_array();
            $this->load->vars($data);
        }
        
        $data['hotelid'] = $hotelid;
        
        $this->load->vars($data);
        
        $box['loginbox'][1] = '<a href="' . site_url('admin/login/logout/') . '">ログアウト</a>';
        $this->load->vars($box);
    }
    
    public function logout()
    {
        $this->load->helper(array('form', 'url'));
        $this->load->library('phpsession');
        
        $this->phpsession->clear('admin');
        redirect('admin/index/index/');
    }
}
?>