<?php
/**
 * Copyright (C) LJCMS All rights reserved.
 * <E-Mail：aspcms@qq.com>
 * Url http://www.liangjing.org
 *     http://www.8CMS.com
 * Update 2016.03.19
 */

class quetopicAModel extends X {
    private $tbname = "question_topic";
    private $pkid = "topicid";

    
    public function getList($items) {
        $where = " WHERE 1=1".$items['searchsql'];
        $start = ($items['page']-1)*$items['pagesize'];
        $countsql = "SELECT COUNT(*) AS my_count".
                    " FROM ".DB_PREFIX."{$this->tbname} AS v".$where;
        $total = parent::$obj->fetch_count($countsql);
        if ($total > 0) {
            $sql = "SELECT v.* FROM ".DB_PREFIX."{$this->tbname} AS v".
                    $where." ORDER BY v.{$this->pkid} DESC LIMIT {$start}, {$items['pagesize']}";
            $_data = parent::$obj->getall($sql);
        }
        return array($total, $_data);
    }

    
    public function getData($id) {
        $sql = "SELECT * FROM ".DB_PREFIX."{$this->tbname} WHERE {$this->pkid}='{$id}'";
        return parent::$obj->fetch_first($sql);
    }

    
    public function doAdd($args) {
        $topicid = parent::$obj->fetch_newid("SELECT MAX({$this->pkid}) FROM ".DB_PREFIX."{$this->tbname}", 1);
        $args = array_merge(array(
                "topicid"=>$topicid,
                "addtime"=>time(),
            ), $args);
        return parent::$obj->insert(DB_PREFIX."{$this->tbname}", $args);
    }

    
    public function doEdit($id, $args) {
        return parent::$obj->update(DB_PREFIX."{$this->tbname}", $args, "{$this->pkid}='{$id}'");
    }

    
    public function doDel($id) {
        $res = false;
        $_question = parent::$obj->getall("SELECT * FROM ".DB_PREFIX."question WHERE topicid='{$id}'");
        if (!empty($_question)) {
            foreach ($_question as $key=>$value) {
                $_queid = intval($value['queid']);
                
                parent::$obj->query("DELETE FROM ".DB_PREFIX."question_option WHERE queid='{$_queid}'");
                parent::$obj->query("DELETE FROM ".DB_PREFIX."question WHERE queid='{$_queid}'");
            }
        }
        $res = parent::$obj->query("DELETE FROM ".DB_PREFIX."{$this->tbname} WHERE {$this->pkid}='{$id}'");
        return $res;
    }

    
    public function getOptions($inid) {
        $temps = "";
        $sql = "SELECT `topicid`, `topicname` FROM ".DB_PREFIX."{$this->tbname} ORDER BY `{$this->pkid}` DESC";
        $data = parent::$obj->getall($sql);
        foreach($data as $key=>$value) {
            $temps .= "<option value='".$value['topicid']."'";
            if (intval($inid) == $value['topicid']) {
                $temps .= " selected";
            }
            $temps .= ">".$value['topicname']."</option>";
        }
        return $temps;
    }

    
    public function getVolist($where="", $orderby="", $num=0, $limit="") {
        $sql = "SELECT v.* FROM ".DB_PREFIX."{$this->tbname} AS v".
                " WHERE 1=1";
        $sql .= !empty($where) ? " AND ".$where : "";
        $sql .= !empty($orderby) ? " ".$orderby : " ORDER BY v.{$this->pkid} DESC";
        if (!empty($limit)) {
            $sql .= " LIMIT {$limit}";
        }
        else {
            $num = intval($num)<1 ? 10 : intval($num);
            $sql .= " LIMIT 0, {$num}";
        }
        $data = parent::$obj->getall($sql);
        $i = 1;
        foreach ($data as $key=>$value) {
            $data[$key]['url'] = PATH_URL."index.php?c=question&a=detail&id=".$value['topicid'];
            $data[$key]['i'] = $i;
        }
        return $data;
    }
}
?>
