<?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 (!$parent_idx) {
    echo "부모값이 없음";
    exit;
}
if (!$w) {

    //신규등록    
    if (!empty($_FILES['ac_image']['tmp_name'])) {
        $upload = Upload::factory($asktheme_upload_dir);
        $upload->file($_FILES['ac_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_cslider set ac_parent_idx = '{$parent_idx}', ac_subject = '{$ac_subject}', ac_content = '{$ac_content}', ac_image_name = '{$results['filename']}', ac_sort = '{$ac_sort}', ac_link = '{$ac_link}', ac_status = '{$ac_status}'";
            sql_query($sql);
            $idx = sql_insert_id();
        } else {
            alert('error');
            exit;
        }
    }
} elseif ($w == 'u' && $ac_idx) {
    //수정
    /*
      if (is_uploaded_file($_FILES['ac_image']['tmp_name'])) {
      $imginfo = getimagesize($_FILES['ac_image']['tmp_name']);
      $filesize = filesize($_FILES['ac_image']['tmp_name']);
      $img = addslashes(file_get_contents($_FILES['ac_image']['tmp_name']));
      $img_query = ", ac_image = '{$img}',";
      $add_img = $img_query . " ac_mime = '{$imginfo['mime']}||{$filesize}'";
      } else {
      $add_img = '';
      }
     */
    if (!empty($_FILES['ac_image']['tmp_name'])) {
        $upload = Upload::factory($asktheme_upload_dir);
        $upload->file($_FILES['ac_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_cslider where ac_idx = '{$ac_idx}' limit 1";
        $data = sql_fetch($sql);
        if ($data['ac_image_name']) {
            $filepath = G5_PATH . "/data/asktheme_attach";
            unlink($filepath . '/' . $data['ac_image_name']);
        }
        $img_update = " ac_image_name = '{$results['filename']}' , ";
    } else {
        $img_update = "";
    }
    $sql = "update ask_plugin_cslider set {$img_update} ac_subject = '{$ac_subject}', ac_content = '{$ac_content}', ac_link = '{$ac_link}', ac_sort = '{$ac_sort}',  ac_status = '{$ac_status}' where ac_idx='{$ac_idx}' limit 1";
    sql_query($sql);
    $idx = $ac_idx;
}

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