<?php

/**
 * @author twttwt
 * 所有函数放置在本类名的类中.
 * 其它类,前缀加模块名;
 */
class article
{
    /**
     * 对数据进行处理
     * 供主函数show_post()调取.
     */
    function show_topic($pid, $row)
    {
        global $db;
        $items = unserialize($row['items']);
        $arr = array();
        $arr['title'] = xlang(unserialize($row['title']));
        $arr['doc'] = xlang(unserialize($row['doc']));
        $arr['content'] = xlang(unserialize($row['content']));
        $arr['keyword'] = $row['keyword'];
        apply('tpl_title', $arr['title']);
        apply('tpl_keywords', $arr['keyword']);
        apply('tpl_description', $arr['doc']);
        apply('tpl_user_nav', array($arr['title'], url(RPG . 'article/show/' . A . '/' .
            B . '/')));
        $arr['pid'] = $pid;
        $arr['authorid'] = $row['authorid'];
        $arr['name'] = htmlspecialchars($row['name']);
        $arr['post_time'] = showtime($row['post_time']);
        $arr['comment'] = $row['comment'];
        $arr['module'] = $row['module'];
        $arr['title'] = htmlspecialchars($arr['title']);
        $arr['doc'] = htmlspecialchars($arr['doc']);

        $arr['keyword'] = htmlspecialchars($arr['keyword']);

        $arr['thumb'] = $row['thumb'];

        $arr['items']['ychuang'] = !empty($items['ychuang']);
        $arr['hits'] = $row['hits'] + 1;
        return $arr;
    }
    /**
     * 编辑主题
     */
    function edit_topic($pid, $row)
    {
        if (IS_POST)
        {
            edittopic($pid, $row);
            yun_succeed(mlang('主题修改成功', 'article'), url(RPGM . 'show/' . MID . '/' . $pid .
                '/'), true);
        }
        return $row;
    }
    /**
     * 新建主题
     */
    function newtopic($mid, $pid, $arr)
    {
        global $db;
        $row = $db->query("select * from " . PF . "article where menu_id='" . $mid .
            "' && post_id='" . $pid . "' limit 0,1");
        if ($db->num_rows($row))
            return;
        $db->query("insert into " . PF . "article (menu_id,post_id,post_time) values('" .
            $mid . "','$pid','" . TIME . "')");
    }
    /**
     * 删除主题
     */
    function deltopic($pid, $mid, $mod, $row)
    {
        if ($mod <> 'article')
            return;
        global $db;
        $db->query("delete from " . PF . "article where menu_id='" . $mid .
            "' && post_id='$pid'");
    }
    /**
     * 主题项目处理,用于新建和编辑
     */
    function topicitem($mod)
    {
        if ($mod <> 'article')
            return;
        return array('ychuang' => !empty($_POST['items']['ychuang']));
    }
    function topicitem_html($mod, $arr = array())
    {
        if ($mod <> 'article')
            return;
        echo '<div class="fieldwrapper">
	<label for="" class="styled">', htmlmlang('文章选项', 'article'), '</label>
	<div class="thefield"><input name="items[ychuang]" ', empty($arr['items']['ychuang']) ?
            '' : 'checked="checked"', ' type="checkbox" value="1" /> ', htmlmlang('原创',
            'article'), '
    </div>
</div>';
    }
    /**
     * 更新主题
     */
    function updatetopic($pid, $arr)
    {
    }
    /**
     * 菜单域
     */
    function menu_option()
    {
        return '<option value="article">' . htmlspecialchars(modname('article')) .
            '</option>';
    }
    /**
     * 菜单域值
     */
    function menu_option_val($mid = 0, $mod = 'article')
    {
        global $db, $conf;
        if ($mod <> 'article')
            return;
        $pvs = $conf['sys_modules']['article']['pvs'];
        $pvnames = $pvs['name'];
        unset($pvs['name']);
        if ($mid)
        {
            $pvs = $conf['sys_menus'][$mid]['pvs'];
        }
        $str = '
        <div class="fieldwrapper">
    <label for="" class="styled">' . htmlmlang('权限分配', 'article') . '</label>
    <div class="thefield">
       <table width="100%" >
      ';
        foreach ($conf['sys_teams'] as $k => $v)
        {
            $str .= '<tr>
    <th scope="row">' . htmlxlang($v['name']) . '</th>';
            foreach ($pvnames as $k2 => $v2)
            {
                $str .= '<td><input type="checkbox" value="1" ' . (empty($pvs[$k][$k2]) ? '' :
                    'checked="checked"') . ' name="pvs[' . $k . '][' . $k2 . ']" /> ' . htmlxlang($v2) .
                    '</td> ';
            }
        }
        $str .= '
  </tr>
    
    </table>
    </div>
</div>';
        $str .= '
        <div class="fieldwrapper">
    <label for="" class="styled">' . htmlmlang('列表页模板', 'article') . '</label>
    <div class="thefield">';
        $str .= '<input type="text" value="' . ($mid ? $conf['sys_menus'][$mid]['tplpf_h'] :
            '') . '" name="tplpf_h" /> (' . htmlmlang('选填', 'article') . ')';
        $str .= '  </div>
</div>';
        $str .= '
        <div class="fieldwrapper">
    <label for="" class="styled">' . htmlmlang('内容页模板', 'article') . '</label>
    <div class="thefield">';
        $str .= '<input type="text" value="' . ($mid ? $conf['sys_menus'][$mid]['tplpf_c'] :
            '') . '" name="tplpf_c" /> (' . htmlmlang('选填', 'article') . ')';
        $str .= '  </div>
</div>';
        $str = str_replace("\n", '', $str);
        $str = str_replace("\r", '', $str);
        if ($mid)
            return $str;
        $jsstr = '<script type="text/javascript">';
        $jsstr .= 'var menu_article=\'';
        $jsstr .= $str;
        $jsstr .= '\';';
        $jsstr .= '</script>';
        return $jsstr;
    }

    function menu_post($mod)
    {
        global $conf;
        if ($mod <> 'article')
            return;
        $arr = array();
        $pvs = $conf['sys_modules']['article']['pvs'];
        $pvnames = $pvs['name'];
        unset($pvs['name']);
        foreach ($conf['sys_teams'] as $k => $v)
        {
            $arr2 = array();
            foreach ($pvnames as $k2 => $v2)
            {
                $arr2[$k2] = !empty($_POST['pvs'][$k][$k2]);
            }
            $arr['pvs'][$k] = $arr2;
        }
        $arr['tplpf_h'] = $_POST['tplpf_h'];
        $arr['tplpf_c'] = $_POST['tplpf_c'];
        return $arr;
    }
    function menu_del($mid, $mod)
    {
        global $conf, $db;
        if ($mod <> 'article')
            return;

        $rs = $db->query('select * from (' . PF . 'article as article left join ' . PF .
            'posts as post on article.menu_id=' . $mid .
            ' && article.post_id=post.id) left join ' . PF .
            'user as user on post.authorid=user.id');
        while ($row = $db->fetch_array($rs))
        {
            del_topic($pid, $row, $mid);
        }
        unset($conf['sys_menus'][$mid]);
    }
    function pv($mid, $user = false)
    {
        global $conf;
        $user === false && $user = $GLOBALS['user'];
        $pv = $conf['sys_menus'][$mid]['pvs'][$user['team']];
        return is_array($pv) ? $pv : array();
    }
}

?>