<?php
// ------------------------------------------------------------------------- //
//                XOOPS - PHP Content Management System                      //
//                       <http://www.xoops.org/>                             //
// ------------------------------------------------------------------------- //
//  This program is free software; you can redistribute it and/or modify     //
//  it under the terms of the GNU General Public License as published by     //
//  the Free Software Foundation; either version 2 of the License, or        //
//  (at your option) any later version.                                      //
//                                                                           //
//  This program is distributed in the hope that it will be useful,          //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
//  GNU General Public License for more details.                             //
//                                                                           //
//  You should have received a copy of the GNU General Public License        //
//  along with this program; if not, write to the Free Software              //
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
// ------------------------------------------------------------------------- //
// Author of File: Kazumi Ono (http://www.mywebaddons.com/)                  //
// ------------------------------------------------------------------------- //

include_once(XOOPS_ROOT_PATH."/class/xoopscomments.php");
include_once(XOOPS_ROOT_PATH."/class/xoopstopic.php");
include_once(XOOPS_ROOT_PATH."/class/xoopsuser.php");

class XoopsStory {
	var $table;
	var $storyid;
	var $topicid;
	var $uid;
	var $title;
	var $hometext;
	var $bodytext="";
	var $counter;
	var $created;
	var $published;
	var $hostname;
	var $nohtml=0;
	var $nosmiley=0;
	var $ihome=0;
	var $notifypub=0;
	var $type;
	var $approved;
	var $topicdisplay;
	var $topicalign;
	var $db;
	var $commentstable;
	var $topicstable;

	function Story($storyid=-1){
		$this->db =& Database::getInstance();
		$this->table = "";
		$this->commentstable = "";
		$this->topicstable = "";
		if(is_array($storyid)){
			$this->makeStory($storyid);
		}elseif($storyid != -1){
			$this->getStory(intval($storyid));
		}
	}

	function setStoryId($value){
		$this->storyid = $value;
	}

	function setTopicId($value){
		$this->topicid = $value;
	}

	function setUid($value){
		$this->uid=$value;
	}

	function setTitle($value){
		$this->title=$value;
	}

	function setHometext($value){
		$this->hometext=$value;
	}

	function setBodytext($value){
		$this->bodytext=$value;
	}

	function setPublished($value){
		$this->published=$value;
	}

	function setHostname($value){
		$this->hostname=$value;
	}

	function setNohtml($value=0){
		$this->nohtml=$value;
	}

	function setNosmiley($value=0){
		$this->nosmiley=$value;
	}

	function setIhome($value){
		$this->ihome=$value;
	}

	function setNotifyPub($value){
		$this->notifypub=$value;
	}

	function setType($value){
		$this->type = $value;
	}

	function setApproved($value){
		$this->approved = $value;
	}

	function setTopicdisplay($value){
		$this->topicdisplay = $value;
	}

	function setTopicalign($value){
		$this->topicalign = $value;
	}

	function store($approved=false){
		//$newpost = 0;
		$myts =& MyTextSanitizer::getInstance();
		$title =$myts->censorString($this->title);
		$hometext =$myts->censorString($this->hometext);
		$bodytext =$myts->censorString($this->bodytext);
		$title = $myts->makeTboxData4Save($title);
		$hometext = $myts->makeTareaData4Save($hometext);
		$bodytext = $myts->makeTareaData4Save($bodytext);
		if(!isset($this->nohtml) || $this->nohtml != 1){
			$this->nohtml = 0;
		}
		if(!isset($this->nosmiley) || $this->nosmiley != 1){
			$this->nosmiley = 0;
		}
		if(!isset($this->notifypub) || $this->notifypub != 1){
			$this->notifypub = 0;
		}
		if( !isset($this->topicdisplay) || $this->topicdisplay != 0 ) {
			$this->topicdisplay = 1;
		}
		if(!isset($this->storyid)){
			//$newpost = 1;
			$newstoryid = $this->db->genId($this->table."_storyid_seq");
			$created = time();
			if($this->approved){
				$sql = "INSERT INTO ".$this->table." (storyid, uid, title, created, published, hostname, nohtml, nosmiley, hometext, bodytext, counter, topicid, ihome, notifypub, type, topicdisplay, topicalign) VALUES ($newstoryid,".$this->uid.",'".$title."',".$created.",".$this->published.",'".$this->hostname."',".$this->nohtml.",".$this->nosmiley.",'".$hometext."','".$bodytext."',0,".$this->topicid.",".$this->ihome.",".$this->notifypub.",'".$this->type."',$this->topicdisplay,'".$this->topicalign."')";
			} else {
				$sql = "INSERT INTO ".$this->table." (storyid, uid, title, created, published, hostname, nohtml, nosmiley, hometext, bodytext, counter, topicid, ihome, notifypub, type, topicdisplay, topicalign) VALUES ($newstoryid,".$this->uid.",'".$title."',".$created.",0,'".$this->hostname."',".$this->nohtml.",".$this->nosmiley.",'".$hometext."','".$bodytext."',0,".$this->topicid.",".$this->ihome.",".$this->notifypub.",'".$this->type."',$this->topicdisplay,'".$this->topicalign."')";
			}
		}else{
			if($this->approved){
				$sql = "UPDATE ".$this->table." SET title='".$title."',published=".$this->published.",nohtml=".$this->nohtml.",nosmiley=".$this->nosmiley.",hometext='".$hometext."',bodytext='".$bodytext."',topicid=".$this->topicid.",ihome=".$this->ihome.",topicdisplay=".$this->topicdisplay.",topicalign='".$this->topicalign."' WHERE storyid=".$this->storyid."";
			} else {
				$sql = "UPDATE ".$this->table." SET title='".$title."',nohtml=".$this->nohtml.",nosmiley=".$this->nosmiley.",hometext='".$hometext."',bodytext='".$bodytext."',topicid=".$this->topicid.",ihome=".$this->ihome.",topicdisplay=".$this->topicdisplay.",topicalign='".$this->topicalign."' WHERE storyid=".$this->storyid."";
			}
		}
		//echo $sql;
		if(!$result = $this->db->query($sql)){
			return false;
		}
		return true;
	}


	function getStory($storyid){
		$sql = "SELECT * FROM ".$this->table." WHERE storyid=".$storyid."";
		$array = $this->db->fetchArray($this->db->query($sql));
		$this->makeStory($array);
	}

	function makeStory($array){
		foreach($array as $key=>$value){
			$this->$key = $value;
		}
	}

	function delete(){
		$sql = "DELETE FROM ".$this->table." WHERE storyid=".$this->storyid."";
		if(!$result = $this->db->query($sql)){
			return false;
		}
		if ( isset($this->commentstable) && $this->commentstable != "" ) {
			$commentsarray = array();
			$com = new XoopsComments($this->commentstable,"storyid");
			$criteria = array("item_id=".$this->storyid."", "pid=0");
			$commentsarray = $com->getAllComments($criteria);
			foreach($commentsarray as $comment){
				$comment->delete();
			}
		}
		return true;
	}

	function updateCounter(){
		$sql = "UPDATE ".$this->table." SET counter=counter+1 WHERE storyid=".$this->storyid."";
		if(!$result = $this->db->queryF($sql)){
			return false;
		}
		return true;
	}

	function topicid(){
		return $this->topicid;
	}

	function topic(){
		return new XoopsTopic($this->topicstable, $this->topicid);
	}

	function uid(){
		return $this->uid;
	}

	function uname(){
		return XoopsUser::getUnameFromId($this->uid);
	}

	function title($format="Show"){
		$myts =& MyTextSanitizer::getInstance();
		$smiley = 1;
		if($this->nosmiley()){
			$smiley = 0;
		}
		switch($format){
			case "Show":
				$title = $myts->makeTboxData4Show($this->title,$smiley);
				break;
			case "Edit":
				$title = $myts->makeTboxData4Edit($this->title);
				break;
			case "Preview":
				$title = $myts->makeTboxData4Preview($this->title,$smiley);
				break;
			case "InForm":
				$title = $myts->makeTboxData4PreviewInForm($this->title);
				break;
		}
		return $title;
	}

	function hometext($format="Show"){
		$myts =& MyTextSanitizer::getInstance();
		$html = 1;
		$smiley = 1;
		$xcodes = 1;
		if ( $this->nohtml() ) {
			$html = 0;
		}
		if ( $this->nosmiley() ) {
			$smiley = 0;
		}
		switch($format){
			case "Show":
				$hometext = $myts->makeTareaData4Show($this->hometext,$html,$smiley,$xcodes);
				break;
			case "Edit":
				$hometext = $myts->makeTareaData4Edit($this->hometext);
				break;
			case "Preview":
				$hometext = $myts->makeTareaData4Preview($this->hometext,$html,$smiley,$xcodes);
				break;
			case "InForm":
				$hometext = $myts->makeTareaData4PreviewInForm($this->hometext);
				break;
		}
		return $hometext;
	}

	function bodytext($format="Show"){
		$myts =& MyTextSanitizer::getInstance();
		$html = 1;
		$smiley = 1;
		$xcodes = 1;
		if ( $this->nohtml() ) {
			$html = 0;
		}
		if ( $this->nosmiley() ) {
			$smiley = 0;
		}
		switch($format){
			case "Show":
				$bodytext = $myts->makeTareaData4Show($this->bodytext,$html,$smiley,$xcodes);
				break;
			case "Edit":
				$bodytext = $myts->makeTareaData4Edit($this->bodytext);
				break;
			case "Preview":
				$bodytext = $myts->makeTareaData4Preview($this->bodytext,$html,$smiley, $xcodes);
				break;
			case "InForm":
				$bodytext = $myts->makeTareaData4PreviewInForm($this->bodytext);
				break;
		}
		return $bodytext;
	}

	function counter(){
		return $this->counter;
	}

	function created(){
		return $this->created;
	}

	function published(){
		return $this->published;
	}

	function hostname(){
		return $this->hostname;
	}

	function storyid(){
		return $this->storyid;
	}

	function nohtml(){
		return $this->nohtml;
	}

	function nosmiley(){
		return $this->nosmiley;
	}

	function notifypub(){
		return $this->notifypub;
	}

	function type(){
		return $this->type;
	}

	function ihome(){
		return $this->ihome;
	}

	function topicdisplay(){
		return $this->topicdisplay;
	}

	function topicalign($astext=true){
		if ( $astext ) {
			if ( $this->topicalign == "R" ) {
				$ret = "right";
			} else {
				$ret = "left";
			}
			return $ret;
		}
		return $this->topicalign;
	}

	function getCommentsCount(){
		$result = $this->db->query("SELECT COUNT(*) FROM ".$this->commentstable." WHERE item_id=".$this->storyid."");
        	list($count) = $this->db->fetchRow($result);
		return $count;
	}
}
?>