<?php
include_once(XOOPS_ROOT_PATH."/class/xoopsstory.php");

class NewsStory extends XoopsStory{

	var $newstopic;   // XoopsTopic object

	function NewsStory($storyid=-1){
		$this->db =& Database::getInstance();
		$this->table = $this->db->prefix("stories");
		$this->commentstable = $this->db->prefix("comments");
		$this->topicstable = $this->db->prefix("topics");
		if (is_array($storyid)) {
			$this->makeStory($storyid);
			$this->newstopic = $this->topic();
		} elseif($storyid != -1) {
			$this->getStory(intval($storyid));
			$this->newstopic = $this->topic();
		}
	}

	function getAllPublished($limit=0, $start=0, $topic=0, $ihome=0, $asobject=true){
		$db =& Database::getInstance();
		$myts =& MyTextSanitizer::getInstance();
		$ret = array();
		$sql = "SELECT * FROM ".$db->prefix("stories")." WHERE published > 0 AND published <= ".time()."";
		if ( !empty($topic) ) {
			$sql .= " AND topicid=".intval($topic)." AND (ihome=1 OR ihome=0)";
		} else {
			if ( $ihome == 0 ) {
				$sql .= " AND ihome=0";
			}
		}
 		$sql .= " ORDER BY published DESC";
		$result = $db->query($sql,intval($limit),intval($start));
		while ( $myrow = $db->fetchArray($result) ) {
			if ( $asobject ) {
				$ret[] = new NewsStory($myrow);
			} else {
				$ret[$myrow['storyid']] = $myts->makeTboxData4Show($myrow['title']);
			}
		}
		return $ret;
	}

	function getAllAutoStory($limit=0, $asobject=true){
		$db =& Database::getInstance();
		$myts =& MyTextSanitizer::getInstance();
		$ret = array();
		$sql = "SELECT * FROM ".$db->prefix("stories")." WHERE published > ".time()." ORDER BY published ASC";
		$result = $db->query($sql,$limit,0);
		while ( $myrow = $db->fetchArray($result) ) {
			if ( $asobject ) {
				$ret[] = new NewsStory($myrow);
			} else {
				$ret[$myrow['storyid']] = $myts->makeTboxData4Show($myrow['title']);
			}
		}
		return $ret;
	}

	function getAllSubmitted($limit=0, $asobject=true){
		$db =& Database::getInstance();
		$myts =& MyTextSanitizer::getInstance();
		$ret = array();
		$sql = "SELECT * FROM ".$db->prefix("stories")." WHERE published=0 ORDER BY created DESC";
		$result = $db->query($sql,$limit,0);
		while ( $myrow = $db->fetchArray($result) ) {
			if ( $asobject ) {
				$ret[] = new NewsStory($myrow);
			} else {
				$ret[$myrow['storyid']] = $myts->makeTboxData4Show($myrow['title']);
			}
		}
		return $ret;
	}

	function getByTopic($topicid){
		$ret = array();
		$db =& Database::getInstance();
		$result = $db->query("SELECT * FROM ".$db->prefix("stories")." WHERE topicid=".intval($topicid)."");
		while( $myrow = $db->fetchArray($result) ){
			$ret[] = new NewsStory($myrow);
		}
		return $ret;
	}

	function countByTopic($topicid=0){
		$db =& Database::getInstance();
		$sql = "SELECT COUNT(*) FROM ".$db->prefix("stories")."";
		if ( $topicid != 0 ) {
			$sql .= " WHERE topicid=".intval($topicid);
		}
		$result = $db->query($sql);
		list($count) = $db->fetchRow($result);
		return $count;
	}

	function topic_title(){
		return $this->newstopic->topic_title();
	}

	function adminlink(){
		$ret = "&nbsp;[ <a href='".XOOPS_URL."/modules/news/admin/index.php?op=edit&amp;storyid=".$this->storyid."'>"._EDIT."</a> | <a href='".XOOPS_URL."/modules/news/admin/index.php?op=delete&amp;storyid=".$this->storyid."'>"._DELETE."</a> ]&nbsp;";
		return $ret;
	}

	function imglink(){
		$ret = "<a href='".XOOPS_URL."/modules/news/index.php?storytopic=".$this->topicid."'><img src='".XOOPS_URL."/modules/news/images/topics/".$this->newstopic->topic_imgurl()."' alt='".$this->newstopic->topic_title()."' hspace='10' vspace='10' align='".$this->topicalign()."' /></a>";
		return $ret;
	}

	function textlink(){
		$ret = "<a href='".XOOPS_URL."/modules/news/index.php?storytopic=".$this->topicid()."'>".$this->newstopic->topic_title()."</a>";
		return $ret;
	}
}
?>