<?php
/**
* @author moufer<moufer@163.com>
* @copyright www.modoer.com
* @version $Id 4 2008-08-31 11:27 $
*/
!defined('IN_MUDDER') && exit('Access Denied');

if($mod_article['disable']) cpmsg($mod_article['name'].'模块已禁用。');
if(!$mod_article['ownernews']) cpmsg($mod_article['name'].'模块未启用商家发布资讯的功能。');

if(!check_submit('mysubmit')) {
    $isEdit = false;
    $query = $db->query("SELECT sid,shopname,subname FROM {$dbpre}shops WHERE ownerid='$user->uid'");
    $ids = '';
    $items = array();
    while($row = $db->fetch_array($query)) {
        $ids[] .= "'".$row['sid']."'";
        $items[$row['sid']] = $row['shopname'].$row['subname'];
    }
    // 加载 FCK 编辑器
    require_once(MUDDER_ROOT.'./include/editor/fckeditor.php');
    $fck = new FCKeditor('content');
    $fck->ToolbarSet = 'Basic';
    $fck->Height = 300;
    if($articleid = intval($articleid)) {
        $article = $db->get_one("SELECT * FROM {$dbpre}articles a LEFT JOIN {$dbpre}articledata ad ON(a.articleid=ad.articleid) WHERE a.articleid='$articleid'");
        empty($article) and redirect('您编辑的文章不存在。','index.php?ac=itemarticles');
        $shopid = $article['shopid'];
        $access = !empty($items[$shopid]);
        $access or redirect('您没有足够的权限来完成本次操作。','index.php?ac=itemarticles');
        $fck->Value = $article['content'];
        $article['introduce'] = str_replace("<br />","", $article['introduce']);
        $isEdit = true;
    }
    $fckhtml = $fck->CreateHtml();
} else {
    require_once(MUDDER_ROOT.$mod_article['directory'].'/include/fun_common.php');
    if($isEdit) {
        $articleid = intval($articleid);
        empty($articleid) and redirect('无效的文章ID。');
    }
    $post = array();
    $post['shopid'] = intval($shopid);
    $post['subject'] = html_special_chars_ex($subject);
    if(!$isEdit) {
        $post['classid'] = $mod_article['ownernews_classid'];
        $post['status'] = $mod_article['ownernews_check'] ? '2' : '1';
    }
    $post['author'] = $user->username;
    $post['keywords'] = html_special_chars_ex($keywords);
    $post['copyfrom'] = '商家发布';
    $post['introduce'] = nl2br(unite_textarea($introduce));
    $post['content'] = strip_safe($content);
    if(!$isEdit || ($isEdit && $updateDateline)) {
        $post['dateline'] = $timestamp;
    }

    //empty($classid) && redirect('分类为选择，请返回选择。');
    empty($post['shopid']) and redirect('未选择文章所属对象，请返回选择。');
    empty($post['subject']) and redirect('标题未填写，请返回填写。');
    empty($post['content']) and redirect('内容未填写，请返回填写。');
    if(!$item = $db->get_one("SELECT sid FROM {$dbpre}shops WHERE sid='{$post['shopid']}'")) {
        redirect('商铺不存在，请返回刷新。');
    }

    if($isEdit) {
        article_edit($articleid, $post);
        $msg = '更新完毕！';
    } else {
        $articleid = article_add($post);
        $msg = '发布成功！';
    }

    $urls = array(
        array(
            'name' => '浏览文章',
            'url' => $mod_article['url'].'article.php?articleid='.$articleid,
        ),
        array(
            'name' => '继续添加',
            'url' => 'index.php?ac=postarticle&shopid='.$post['shopid'],
        ),
        array(
            'name' => '返回管理页',
            'url' => 'index.php?ac=itemarticles',
        ),
    );

    redirect($msg, $urls);
}
?>