<?php

/*
 *  배너 등록 저장
 */
include_once './_common.php';
include_once './index.php';
include_once G5_PLUGIN_PATH . "/asktheme/lib/upload.php";
$asktheme_upload_dir = "data/asktheme_attach";

if (!defined('_GNUBOARD_')) {
    exit;
}

if (!$_POST) {
    exit;
}


//텍스트배너
if ($ab_type == 'text') {
    if (!$w) {
        //신규등록
        $sql = "insert into ask_plugin_banner set ab_banner_image = '', ab_html = '', ab_mime = '', ab_subject = '{$ab_subject}', ab_link = '{$ab_link}', ab_tag = '{$ab_tag}', ab_status = '{$ab_status}', ab_type = '{$ab_type}' ";
        sql_query($sql);
        $idx = sql_insert_id();
    } elseif ($w == 'u' && $ab_idx) {
        //수정
        $sql = "update ask_plugin_banner set ab_banner_image = '', ab_html = '', ab_mime = '', ab_subject = '{$ab_subject}', ab_link = '{$ab_link}', ab_tag = '{$ab_tag}', ab_status = '{$ab_status}', ab_type = '{$ab_type}' where ab_idx = '{$ab_idx}' limit 1";
        sql_query($sql);
        $idx = $ab_idx;
    }
    goto_url('./banner_add.php?w=u&idx=' . $idx);
}

//이미지배너
if ($ab_type == 'image' && $_FILES) {
    if (!$w) {
        //신규등록    
        if (!empty($_FILES['ab_banner_image']['tmp_name'])) {
            $upload = Upload::factory($asktheme_upload_dir);
            $upload->file($_FILES['ab_banner_image']);
            $upload->set_max_file_size(5);
            //set allowed mime types
            $upload->set_allowed_mime_types(array('image/gif', 'image/jpeg', 'image/png', 'image/bmp'));
            $results = $upload->upload();
            //에러 발생시
            if ($results['errors']) {
                alert($results['errors']['0']);
                exit;
            }
            //업로드 되면 DB 등록.
            if ($results['filename']) {
                $sql = "insert into ask_plugin_banner set ab_subject = '', ab_html = '', ab_banner = '{$results['filename']}', ab_link = '{$ab_link}', ab_tag = '{$ab_tag}', ab_status = '{$ab_status}', ab_type = '{$ab_type}'";
                sql_query($sql);
                $idx = sql_insert_id();
            } else {
                alert('error');
                exit;
            }
        }
    } elseif ($w == 'u' && $ab_idx) {
        //수정
        if (!empty($_FILES['ab_banner_image']['tmp_name'])) {
            $upload = Upload::factory($asktheme_upload_dir);
            $upload->file($_FILES['ab_banner_image']);
            $upload->set_max_file_size(5);
            //set allowed mime types
            $upload->set_allowed_mime_types(array('image/gif', 'image/jpeg', 'image/png', 'image/bmp'));
            $results = $upload->upload();
            if ($results['errors']) {
                alert($results['errors']['0']);
                exit;
            }
        }
        //DB 등록.
        if ($results['filename']) {
            //기존 이미지는 삭제.
            //첨부이미지 삭제
            $sql = "select * from ask_plugin_banner where ab_idx = '{$ab_idx}' limit 1";
            $data = sql_fetch($sql);
            if ($data['ab_banner']) {
                $filepath = G5_PATH . "/data/asktheme_attach";
                unlink($filepath . '/' . $data['ab_banner']);
            }
            $img_update = " ab_banner = '{$results['filename']}' , ";
        } else {
            $img_update = "";
        }
        $sql = "update ask_plugin_banner set {$img_update}  ab_subject = '', ab_html = '', ab_link = '{$ab_link}', ab_tag = '{$ab_tag}', ab_status = '{$ab_status}', ab_type = '{$ab_type}' where ab_idx = '{$ab_idx}' limit 1";
        sql_query($sql);
        $idx = $ab_idx;
    }


    goto_url('./banner_add.php?w=u&idx=' . $idx);
}

//html배너
if ($ab_type == 'html') {
    if (!$w) {
        //신규등록
        $sql = "insert into ask_plugin_banner set ab_subject = '', ab_link = '', ab_banner_image = '', ab_mime = '', ab_html = '{$ab_html}', ab_tag = '{$ab_tag}', ab_status = '{$ab_status}', ab_type = '{$ab_type}' ";
        sql_query($sql);
        $idx = sql_insert_id();
    } elseif ($w == 'u' && $ab_idx) {
        //수정
        $sql = "update ask_plugin_banner set ab_subject = '', ab_link = '', ab_banner_image = '', ab_mime = '', ab_html = '{$ab_html}', ab_tag = '{$ab_tag}', ab_status = '{$ab_status}', ab_type = '{$ab_type}' where ab_idx = '{$ab_idx}' limit 1";
        sql_query($sql);
        $idx = $ab_idx;
    }
    goto_url('./banner_add.php?w=u&idx=' . $idx);
}
