<?php

include_once("DB.php");

class Story
{

	private $id;
	private $html;
	private $title;
	private $pubDate;
	private $desc;
	private $link;
	private $content;
	private $language;
	private $channelId;
	private $author;
	private $category;
	private $fullArticle = true;
	
	private $db;
	
	public function Story($title = "", $pubDate = "", $desc = "", $link = "", $content = "", $category = "", $channelId = -1)
	{
		if ( strlen($title) > 0 )
		{
			$this->title = $title;
			$this->pubDate = $pubDate;
			$this->desc = $desc;
			$this->link = $link;
			$this->content = $content;
			$this->channelId = $channelId;
			$this->category = $category;
		}
		$this->db = DB::getInstance();
	}
	
	public function saveBulk($storyArray)
	{
		$this->db->connect();
		foreach($storyArray as $s)
		{
			$this->store($s);
		}
		$this->db->close();
	}
	
	public function save()
	{
		$this->db->connect();
		$this->store($this);
		$this->db->close();
	}
	
	public function endsWith( $str, $sub ) {
		return ( substr( $str, strlen( $str ) - strlen( $sub ) ) == $sub );
	}
	
	public function getIdentifier($story)
	{
		if($story->channelId == "ARIPAEV")
		{
			return "url='" . $story->link . "'";
		}
		else
		{
			return "channel_id='" . $story->channelId . "' AND  article_local_id ='" . $story->id . "'";
		}
	}
	
	public function update($story)
	{
		$this->db->query("UPDATE ekktt_article SET category_name = '" . $story->category . "', title='" . addslashes($story->title) . "', lead='" . addslashes($story->desc) . "',content='" . addslashes($story->content)  . "',author='" .  $story->author . "',publish_date='" . $story->pubDate . "',html='" . $story->html . "' WHERE " . $this->getIdentifier($story) . ";");
	}
	
	public function store($story)
	{
		if($story->category == "Eesti Päevaleht")
		{
			$story->channelId = "PAEVALEHT";
		}
		if($story->category == "Eesti Ekspress")
		{
			$story->channelId = "EESTIEKSPRESS";
		}
		
		if($this->endsWith($story->author, ";")){
			 $story->author = substr($story->author, 0, (strlen($story->author)-1));
		}
		
		$this->db->query("SELECT COUNT(article_id) as cnt FROM ekktt_article WHERE " . $this->getIdentifier($story) . ";");
		$rows = $this->db->getRows();
		
		if ($rows[0]['cnt'] == 0)
		{
			if( !($story->pubDate == '' and ($story->desc == '' or $story->content == '') ) )
			{
				$queryString = "INSERT INTO ekktt_article (article_local_id, url, category_name, channel_id, lang_id, title, lead, content, author, publish_date, is_article_full, html) VALUES( '" . $story->id . "','". $story->link . "','" . $story->category . "', '" . $story->channelId . "', '0', '" . addslashes($story->title) . "', '" . addslashes($story->desc) . "', '" . addslashes($story->content)  . "','" .  $story->author . "','" . $story->pubDate . "','" . $story->fullArticle . "','" . $story->html . "' )";
				$this->db->query($queryString);
			}
			
			$this->db->query("SELECT COUNT(id) as cnt FROM ekktt_visit_url WHERE " . $this->getIdentifier($story) . ";");
			$rows = $this->db->getRows();
			
			// If the news story was firstly encountered and not found from the DB.
			if ( $rows[0]['cnt'] == 0 )
			{
				$queryString = "INSERT INTO ekktt_visit_url(article_local_id, url, visited, channel_id, last_visit_date) VALUES('" . $story->id . "', '" . $story->link . "', 1, '" . $story->channelId . "', NOW())";
				$this->db->query($queryString);
			}
			// Otherwise just update the stuff
			else
			{
				$queryString = "UPDATE ekktt_visit_url SET visited = 1, last_visit_date = NOW() WHERE " . $this->getIdentifier($story) . ";";
				$this->db->query($queryString);
			}
		}
	}
	
	public function title($title = NULL)
	{
		if ( is_null($title) )
		{
			return $this->title;
		}
		else
		{
			$this->title = $title;
		}
	}
	
	public function language($language = NULL)
	{
		if ( is_null($language) )
		{
			return $this->language;
		}
		else
		{
			$this->language = $language;
		}
	}
	
	public function html($html = NULL)
	{
		if ( is_null($html) )
		{
			return $this->html;
		}
		else
		{
			$this->db->connect();
			$this->html = mysql_real_escape_string($html, $this->db->getResource());
			$this->db->close();
		}
	}
	
	public function channelId($channelId = NULL)
	{
		if ( is_null($channelId) )
		{
			return $this->channelId;
		}
		else
		{
			$this->channelId = $channelId;
		}
	}
	
	public function pubDate($pubDate = NULL)
	{
		if ( is_null($pubDate) )
		{
			return $this->pubDate;
		}
		else
		{
			$this->pubDate = $pubDate;
		}
	}
	
	public function desc($desc = NULL)
	{
		if ( is_null($desc) )
		{
			return $this->desc;
		}
		else
		{
			$this->db->connect();
			$this->desc = mysql_real_escape_string($desc, $this->db->getResource());
			$this->db->close();
		}
	}
	
	public function link($link = NULL)
	{
		if ( is_null($link) )
		{
			return $this->link;
		}
		else
		{
			$this->link = $link;
		}
	}
	
	public function author($author = NULL)
	{
		if ( is_null($author) )
		{
			return $this->author;
		}
		else
		{
			$this->db->connect();
			$this->author = mysql_real_escape_string($author, $this->db->getResource());
			$this->db->close();
		}
	}
	
	public function content($content = NULL)
	{
		if ( is_null($content) )
		{
			return $this->content;
		}
		else
		{
			$this->db->connect();
			$this->content = mysql_real_escape_string($content, $this->db->getResource());
			$this->db->close();
		}
	}
	
	public function category($category = NULL)
	{
		if ( is_null($category) )
		{
			return $this->category;
		}
		else
		{
			$this->category = $category;
		}
	}
	
	public function id($id = NULL)
	{
		if ( is_null($id) )
		{
			return $this->id;
		}
		else
		{
			$this->id = $id;
		}
	}
	
	public function fullArticle($fullArticle = NULL)
	{
		if ( is_null($fullArticle) )
		{
			return $this->fullArticle;
		}
		else
		{
			$this->fullArticle = $fullArticle;
		}
	}
	
	public function toString($show_html = false)
	{
		$html = $this->html();
		if ( $show_html == false )
		{
			$this->html = 'Not shown for convenience. Use $story->toString(true) if you want otherwise.';
		}
		$output = print_r($this, true);
		$this->html = $html;
		return $output;
	}
	
	public function setChannelId($channelId) {
		$this->channelId = $channelId;
	}
	
	public function setAuthor($author) {
		$this->author = $author;
	}

}


?>
