<?php
/**
 * 單表模型編輯器
 * 
 * @version        $Id: archives_sg_add.php 1 13:52 2010年7月9日Z tianya $
 * @package        DedeCMS.Member
 * @copyright      Copyright (c) 2007 - 2010, DesDev, Inc.
 * @license        http://help.dedecms.com/usersguide/license.html
 * @link           http://www.dedecms.com
 */
require_once(dirname(__FILE__)."/config.php");
CheckRank(0,0);
require_once(DEDEINC."/dedetag.class.php");
require_once(DEDEINC."/customfields.func.php");
require_once(DEDEMEMBER."/inc/inc_catalog_options.php");
require_once(DEDEMEMBER."/inc/inc_archives_functions.php");
$channelid = isset($channelid) && is_numeric($channelid) ? $channelid : 1;
$aid = isset($aid) && is_numeric($aid) ? $aid : 0;
$mtypesid = isset($mtypesid) && is_numeric($mtypesid) ? $mtypesid : 0;
$menutype = 'content';

/*-------------
function _ShowForm(){  }
--------------*/
if(empty($dopost))
{
    //讀取歸檔信息
    $arcQuery = "SELECT ch.*,arc.* FROM `#@__arctiny` arc
    LEFT JOIN `#@__channeltype` ch ON ch.id=arc.channel WHERE arc.id='$aid' ";
    $cInfos = $dsql->GetOne($arcQuery);
    if(!is_array($cInfos))
    {
        ShowMsg("讀取文檔信息出錯!","-1");
        exit();
    }
    $addRow = $dsql->GetOne("SELECT * FROM `{$cInfos['addtable']}` WHERE aid='$aid'; ");
    if($addRow['mid']!=$cfg_ml->M_ID)
    {
        ShowMsg("對不起，你沒權限操作此文檔！","-1");
        exit();
    }
    $addRow['id'] = $addRow['aid'];
    include(DEDEMEMBER."/templets/archives_sg_edit.htm");
    exit();
}

/*------------------------------
function _SaveArticle(){  }
------------------------------*/
else if($dopost=='save')
{
    require_once(DEDEINC."/image.func.php");
    require_once(DEDEINC."/oxwindow.class.php");

    $flag = '';
    $typeid = isset($typeid) && is_numeric($typeid) ? $typeid : 0;
    $userip = GetIP();
    
    $svali = GetCkVdValue();
    if(preg_match("/3/",$safe_gdopen)){
        if(strtolower($vdcode)!=$svali || $svali=='')
        {
            ResetVdValue();
            ShowMsg('驗證碼錯誤！', '-1');
            exit();
        }
    }

    if($typeid==0)
    {
        ShowMsg('請指定文檔隸屬的欄目！','-1');
        exit();
    }
    $query = "SELECT tp.ispart,tp.channeltype,tp.issend,ch.issend AS cissend,ch.sendrank,ch.arcsta,ch.addtable,ch.fieldset,ch.usertype
         FROM `#@__arctype` tp LEFT JOIN `#@__channeltype` ch ON ch.id=tp.channeltype WHERE tp.id='$typeid' ";
    $cInfos = $dsql->GetOne($query);
    $addtable = $cInfos['addtable'];

    //檢測欄目是否有投稿權限
    if($cInfos['issend']!=1 || $cInfos['ispart']!=0|| $cInfos['channeltype']!=$channelid || $cInfos['cissend']!=1)
    {
        ShowMsg("你所選擇的欄目不支持投稿！","-1");
        exit();
    }

    //文檔的默認狀態
    if($cInfos['arcsta']==0)
    {
        $arcrank = 0;
    }
    else if($cInfos['arcsta']==1)
    {
        $arcrank = 0;
    }
    else
    {
        $arcrank = -1;
    }

    //對保存的內容進行處理
    $title = cn_substrR(HtmlReplace($title, 1), $cfg_title_maxlen);
    $mid = $cfg_ml->M_ID;

    //處理上傳的縮略圖
    $litpic = MemberUploads('litpic', $oldlitpic, $mid, 'image', '', $cfg_ddimg_width, $cfg_ddimg_height, FALSE);
    if($litpic!='') SaveUploadInfo($title, $litpic, 1);
    else $litpic =$oldlitpic;

    //分析處理附加表數據
    $inadd_f = $inadd_m = '';
    if(!empty($dede_addonfields))
    {
        $addonfields = explode(';',$dede_addonfields);
        if(is_array($addonfields))
        {
            foreach($addonfields as $v)
            {
                if($v=='')
                {
                    continue;
                }
                $vs = explode(',',$v);
                if(!isset(${$vs[0]}))
                {
                    ${$vs[0]} = '';
                }

                //自動摘要和遠程圖片本地化
                if($vs[1]=='htmltext'||$vs[1]=='textdata')
                {
                    ${$vs[0]} = AnalyseHtmlBody(${$vs[0]},$description,$vs[1]);
                }

                ${$vs[0]} = GetFieldValueA(${$vs[0]},$vs[1],$aid);

                $inadd_f .= ',`'.$vs[0]."` ='".${$vs[0]}."' ";
                $inadd_m .= ','.$vs[0];
            }
        }
        
        if (empty($dede_fieldshash) || $dede_fieldshash != md5($dede_addonfields.$cfg_cookie_encode))
        {
            showMsg('數據校驗不對，程序返回', '-1');
            exit();
        }
        
        // 這裡對前台提交的附加數據進行一次校驗
        $fontiterm = PrintAutoFieldsAdd($cInfos['fieldset'],'autofield', FALSE);
        if ($fontiterm != $inadd_m)
        {
            ShowMsg("提交表單同系統配置不相符,請重新提交！", "-1");
            exit();
        }
    }

    if($addtable!='')
    {
        $upQuery = "UPDATE `$addtable` SET `title`='$title',`typeid`='$typeid',`arcrank`='$arcrank',litpic='$litpic',userip='$userip'{$inadd_f} WHERE aid='$aid' ";
        if(!$dsql->ExecuteNoneQuery($upQuery))
        {
            ShowMsg("更新附加表 `$addtable`  時出錯，請聯繫管理員！","javascript:;");
            exit();
        }
    }
    
    UpIndexKey($aid,0,$typeid,$sortrank,'');
    $artUrl = MakeArt($aid,true);
    
    if($artUrl=='') $artUrl = $cfg_phpurl."/view.php?aid=$aid";

    //返回成功信息
    $msg = "　　請選擇你的後續操作：
        <a href='archives_sg_add.php?cid=$typeid'><u>發佈新內容</u></a>
        &nbsp;&nbsp;
        <a href='archives_do.php?channelid=$channelid&aid=".$aid."&dopost=edit'><u>查看更改</u></a>
        &nbsp;&nbsp;
        <a href='$artUrl' target='_blank'><u>查看內容</u></a>
        &nbsp;&nbsp;
        <a href='content_sg_list.php?channelid=$channelid'><u>管理內容</u></a>
        ";
    $wintitle = "成功更改內容！";
    $wecome_info = "內容管理::更改內容";
    $win = new OxWindow();
    $win->AddTitle("成功更改內容：");
    $win->AddMsgItem($msg);
    $winform = $win->GetWindow("hand","&nbsp;",false);
    $win->Display();
}