<?php
ApplicationSettings::RegisterDefaultSetting("blog", "preview_length", 1000);

class BlogPost extends DBObject {
	private $_uri;

	public function __construct($id=0) {
		$field="post_id";
		if (!is_numeric($id))
			$field="post_url";
		parent::__construct("blog_posts", $field, $id);
		
		$this->_uri=new URI($this->PostTitle, $this->PostUrl);
	}
	
	public function Save() {
		if (!isset($this->PostUrl) || $this->PostUrl==null || $this->PostUrl=="")
			$this->PostUrl=Utils::MakeUniqueURL("blog_posts","post_url",$this->PostTitle);
		if (is_numeric($this->PostUrl))
			$this->PostUrl="_".$this->PostUrl;
		parent::Save();
	}
	
	public function Delete() {
		$this->PostDeleted=1;
		$this->Save();
	}
	
	public function GetPostPreview() {
		$previewLength=ApplicationSettings::GetSetting("blog", "preview_length");
		$content=$this->PostContent;
		if ($previewLength<strlen($content)){
			$newlinePos=strpos($content,"\n",$previewLength);
			$content=substr($content, 0, $newlinePos);
		}
		return $content;
	}
	
	public function GetURI() {
		return $this->_uri;
	}
	
	public function Publish() {
		$this->PostContent=$this->PostDraft;
		$this->Save();
	}
}
