<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * 인클루드 class
 *
 * cm_controller: 부모 Class(CI_Controller 확장한 custome class)
 */
include_once (APPPATH.'core/admin/cm_controller.php');

class advertisement extends cm_controller {

    var $ad_page_tb_name = 'ad_page'; // 광고 페이지 테이블 이름
    var $ad_code_tb_name = ''; // 광고 코드 테이블 이름

	/*
     title : 광고 페이지 관리 (목록)
     */
	public function fn_1($page=1) {
        $this->load->database();
        $query = $this->db->get($this->ad_page_tb_name);
        $i = 0;
        $return_data = array();
        foreach($query->result_array() as $row) {
            $return_data['row'][$i] = $row;
            $i++;
        }
        return $this->load->view('/admin/advertisement/ads', $return_data, true);

    }

    public function fn_ad_page_arr_del_proc() {
        $is_del = $this->input->post('is_del');
        foreach($is_del as $v) {
            if($v>0) {
                $where_in[] = $v;
            }
        }
        if(count($where_in)>0) {
            $this->load->database();
            $this->db->where_in('idx',$where_in);
            $ret = $this->db->delete($this->ad_page_tb_name);
            if($ret==TRUE) {
                $message = '삭제되었습니다.';
            } else {
                $message = '삭제되지 않았습니다.(디비 에러) 서버 관리자에게 문의해 주십시오.';
            }
            echo "<script>alert('".$message."'); parent.location.reload();</script>";
        }
        exit;
    }

    public function fn_ad_page_edit() {
        $idx = $this->input->get('idx');

        if(is_numeric($idx)==TRUE) {
            $this->load->database();
            $query = $this->db->get_where($this->ad_page_tb_name, array('idx'=>$idx));
            $row = $query->row_array();
            $row['result'] = 'success';
        } else {
            $row['result'] = 'fail';
        }
        echo json_encode($row);
        exit;
    }

    /**
     * [ad_page_proc register ad page]
     * @return [boolean] 
     */
    public function fn_ad_page_proc() {
        $page_key = $this->input->post('page_key');
        $page_name = $this->input->post('page_name');
        $ad_pos_key = $this->input->post('ad_pos_key');
        $ad_pos_name = $this->input->post('ad_pos_name');
        $mode = $this->input->post('mode');
        $idx = $this->input->post('idx');

        $this->load->library('form_validation');
        $this->form_validation->set_rules('page_key','페이지 키',array('required','regex_match[/^[a-zA-Z]+$/]'));
        $this->form_validation->set_rules('page_name','페이지 이름',array('required'));
        $this->form_validation->set_rules('ad_pos_key','광고 위치 키',array('required','regex_match[/^[a-zA-Z]+$/]'));
        $this->form_validation->set_rules('ad_pos_name','광고 위치 이름',array('required'));
        if ($this->form_validation->run() == TRUE) {
            $this->load->database();
            $data = ['page_key'=>$page_key,
                     'page_name'=>$page_name,
                     'ad_pos_key'=>$ad_pos_key,
                     'ad_pos_name'=>$ad_pos_name];
            if($mode=='upt' && $idx>0) {
                $this->db->where('idx',$idx);
                $this->db->update($this->ad_page_tb_name,$data);
                $message = '수정되었습니다';
            } else {
                $this->db->insert($this->ad_page_tb_name,$data);
                $message = '등록되었습니다';
            }
            echo "<script>alert('".$message."'); parent.location.reload();</script>";
        } else {
            echo "<script>alert('적합한 데이터를 입력해 주십시오.');exit;</script>";
        }
        exit;
    }
}














