<?php
/**
 * 圖集編輯
 * 
 * @version        $Id: album_edit.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);
if($cfg_mb_lit=='Y')
{
    ShowMsg("由於系統開啟了精簡版會員空間，你訪問的功能不可用！","-1");
    exit();
}
if($cfg_mb_album=='N')
{
    ShowMsg("對不起，由於系統關閉了圖集功能，你訪問的功能不可用！","-1");
    exit();
}
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 : 2;
$aid = isset($aid) && is_numeric($aid) ? $aid : 0;
$menutype = 'content';
if(empty($formhtml)) $formhtml = 0;

/*-------------
function _ShowForm(){  }
--------------*/
if(empty($dopost))
{
    //讀取歸檔信息
    $arcQuery = "SELECT arc.*,ch.addtable,ch.fieldset,ch.arcsta
       FROM `#@__archives` arc LEFT JOIN `#@__channeltype` ch ON ch.id=arc.channel
       WHERE arc.id='$aid' AND arc.mid='".$cfg_ml->M_ID."'; ";
    $row = $dsql->GetOne($arcQuery);
    if(!is_array($row))
    {
        ShowMsg("讀取文檔信息出錯!","-1");
        exit();
    }
    else if($row['arcrank']>=0)
    {
        $dtime = time();
        $maxtime = $cfg_mb_editday * 24 *3600;
        if($dtime - $row['senddate'] > $maxtime)
        {
            ShowMsg("這篇文檔已經鎖定，你不能再修改它！","-1");
            exit();
        }
    }
    $addRow = $dsql->GetOne("SELECT * FROM `{$row['addtable']}` WHERE aid='$aid'; ");
    $dtp = new DedeTagParse();
    $dtp->LoadSource($addRow['imgurls']);
    $abinfo = $dtp->GetTagByName('pagestyle');
    include(DEDEMEMBER."/templets/album_edit.htm");
    exit();
}
/*------------------------------
function _Save(){  }
------------------------------*/
else if($dopost=='save')
{
    $svali = GetCkVdValue();
    if(preg_match("/1/",$safe_gdopen)){
        if(strtolower($vdcode)!=$svali || $svali=='')
        {
            ResetVdValue();
            ShowMsg('驗證碼錯誤！', '-1');
            exit();
        }
    }
    $cInfos = $dsql->GetOne("Select * From `#@__channeltype`  where id='$channelid'; ");
    $maxwidth = isset($maxwidth) && is_numeric($maxwidth) ? $maxwidth : 800;
    $pagepicnum = isset($pagepicnum) && is_numeric($pagepicnum) ? $pagepicnum : 12;
    $ddmaxwidth = isset($ddmaxwidth) && is_numeric($ddmaxwidth) ? $ddmaxwidth : 200;
    $prow = isset($prow) && is_numeric($prow) ? $prow : 3;
    $pcol = isset($pcol) && is_numeric($pcol) ? $pcol : 3;
    $pagestyle = in_array($pagestyle,array('1','2','3')) ? $pagestyle : 2;

    include(DEDEMEMBER.'/inc/archives_check_edit.php');
    $imgurls = "{dede:pagestyle maxwidth='$maxwidth' pagepicnum='$pagepicnum'
    ddmaxwidth='$ddmaxwidth' row='$prow' col='$pcol' value='$pagestyle'/}\r\n";
    $hasone = false;
    $ddisfirst=1;

    //處理並保存所指定的圖片從網上複製
    if($formhtml==1)
    {
        $imagebody = stripslashes($imagebody);
        $imgurls .= GetCurContentAlbum($imagebody,$copysource,$litpicname);
        if($ddisfirst==1 && $litpic=='' && !empty($litpicname))
        {
            $litpic = $litpicname;
            $hasone = true;
        }
    }
    $info = '';

    //檢查已上傳或直接上傳的圖片
    for($i=1;$i<=120;$i++)
    {
        //含有圖片的條件
        if(isset(${'imgurl'.$i}) || (isset($_FILES['imgfile'.$i]['tmp_name']) && is_uploaded_file($_FILES['imgfile'.$i]['tmp_name'])))
        {
            $iinfo = str_replace("'","`",stripslashes(${'imgmsg'.$i}));
            if(!is_uploaded_file($_FILES['imgfile'.$i]['tmp_name']))
            {
                $iurl = stripslashes(${'imgurl'.$i});

                //如果有舊圖
                if(isset(${'imgurl'.$i}))
                {
                    $litpicname = $iurl;
                    $filename = $iurl;

                    //縮圖
                    if($pagestyle > 2)
                    {
                        $litpicname = GetImageMapDD($filename,$ddmaxwidth);
                        if($litpicname != '')
                        {
                            SaveUploadInfo($title.' 小圖',$litpicname,1);
                        }
                    }
                }
                else
                {
                    continue;
                }
            }
            else
            {
                $sparr = Array("image/pjpeg","image/jpeg","image/gif","image/png","image/xpng","image/wbmp");
                if(!in_array($_FILES['imgfile'.$i]['type'],$sparr))
                {
                    continue;
                }
                if(isset(${'imgurl'.$i}))
                {
                    $filename = ${'imgurl'.$i};
                }
                else
                {
                    $filename = '';
                }
                $filename = MemberUploads('imgfile'.$i,$filename,$cfg_ml->M_ID,'image','',0,0,false);
                if($filename!='')
                {
                    SaveUploadInfo($title,$filename,1);
                }
                $litpicname = $filename;

                //縮圖
                if($pagestyle > 2)
                {
                    $litpicname = GetImageMapDD($filename,$ddmaxwidth);
                    if($litpicname != '')
                    {
                        SaveUploadInfo($title.' 小圖',$litpicname,1);
                    }
                }
            }
            $imgfile = $cfg_basedir.$filename;
            if(is_file($imgfile))
            {
                $iurl = $filename;
                $info = '';
                $imginfos = @getimagesize($imgfile,$info);
                $imgurls .= "{dede:img ddimg='$litpicname' text='$iinfo' width='".$imginfos[0]."' height='".$imginfos[1]."'} $iurl {/dede:img}\r\n";
            }
            if(!$hasone && $litpic=='' && !empty($litpicname))
            {
                $litpic = $litpicname;
                $hasone = true;
            }
        }
    }//循環結束
    $imgurls = addslashes($imgurls);

    //分析處理附加表數據
    $inadd_f = '';
    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]} = '';
                }
                ${$vs[0]} = GetFieldValueA(${$vs[0]},$vs[1],$aid);
                $inadd_f .= ','.$vs[0]." ='".${$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_f)
        {
            ShowMsg("提交表單同系統配置不相符,請重新提交！", "-1");
            exit();
        }
    }

    //處理圖片文檔的自定義屬性
    if($litpic!='') $flag = 'p';

    //更新數據庫的SQL語句
    //更新數據庫的SQL語句
    $upQuery = "UPDATE `#@__archives` SET
             ismake='$ismake',
             arcrank='$arcrank',
             typeid='$typeid',
             title='$title',
             litpic='$litpic',
             description='$description',
             keywords='$keywords',
             mtype='$mtypesid',            
             flag='$flag'
        WHERE id='$aid' AND mid='$mid'; ";
    if(!$dsql->ExecuteNoneQuery($upQuery))
    {
        ShowMsg("把數據保存到數據庫主表時出錯，請聯繫管理員！".$dsql->GetError(),"-1");
        exit();
    }

    $isrm = 0;

    if($addtable!='')
    {
        $query = "UPDATE `$addtable`
      set typeid='$typeid',
      pagestyle='$pagestyle',
      maxwidth = '$maxwidth',
      ddmaxwidth = '$ddmaxwidth',
      pagepicnum = '$pagepicnum',
      imgurls='$imgurls',
      row='$prow',
      col='$pcol',
       userip='$userip',
      isrm='$isrm'{$inadd_f}
    WHERE aid='$aid'; ";
        if(!$dsql->ExecuteNoneQuery($query))
        {
            ShowMsg("更新附加表 `$addtable`  時出錯，請聯繫管理員！".$dsql->GetError(),"javascript:;");
            exit();
        }
    }

    UpIndexKey($aid, $arcrank, $typeid, $sortrank, $tags);
    $artUrl = MakeArt($aid, TRUE);
    if($artUrl=='') $artUrl = $cfg_phpurl."/view.php?aid=$aid";

    //---------------------------------
    //返回成功信息
    //----------------------------------
    $msg = "　　請選擇你的後續操作：
<a href='album_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_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();
}