<?php
/*
 *
 * sflog!
 * Copyright (C) 2005 Alberto Sfolcini <a.sfolcini@gmail.com>
 *
 * This file is a part of sflog!.
 *
 * sflog! 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.
 *
 * sflog! 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */

require_once(dirname(__FILE__)."/../common/Ini.class.php");
require_once(dirname(__FILE__)."/../cms/BlogBar.class.php");
require_once(dirname(__FILE__)."/../cms/Constants.inc.php");
require_once(dirname(__FILE__)."/../common/utils.func.php");
require_once(dirname(__FILE__)."/../common/WriteFile.class.php");


define ('BLOG_ID','blogID');
define ('BLOG_NAME','blogName');
define ('BLOG_POSITION','blogPosition');
define ('BLOG_VISIBLE','blogVisible');
define ('BLOG_POSTPERPAGE','blogPostPerPage');
	
/**
 * @title			: Blog
 * @description		: Manages Blog   
 * @date			: 17-Oct-2005   
 * @author			: Alberto Sfolcini  <a.sfolcini@gmail.com>
 */
class Blog {

	var $blogName 	= "";
	var $blogID		= "";
	var $blogPosition	= "";
	var $blogVisible	= "";
	var $blogPostPerPage= "";

	var $ini;

	/**
	 * Constructor 1
	 */
	function Blog(){
		if (file_exists(FILE_INI))
			$this->ini = new Ini(FILE_INI);
		else{
			$tmp = new WriteFile(FILE_INI);
			$tmp->closeFile();			
		}
	}

	/**
	 * Creates blog
	 * @param $blogID string
	 * @param $blogName string
	 * @param $blogPosition int
	 * @param $blogVisible boolean
	 * @param $blogPostPerPage int
	 * @return boolean
	 */
	function createBlog($blogID,$blogName,$blogPosition,$blogVisible,$blogPostPerPage){
		$this->blogName = $blogName;
		$this->blogID	= $blogID;
		$this->blogPosition	= $blogPosition;
		$this->blogVisible = $blogVisible;
		$this->blogPostPerPage = $blogPostPerPage;
		
		$ret = true;
		
		// create fs structure
		if (!file_exists(BLOG_PATH.$blogID))
			$ret = (mkdir(BLOG_PATH.$blogID,0755)) and $ret;
		if (!file_exists(BLOG_PATH.$blogID.BLOG_DATA))
			$ret = (mkdir(BLOG_PATH.$blogID.BLOG_DATA,0755)) and $ret;
		if (!file_exists(BLOG_PATH.$blogID.BLOG_HTML))
			$ret = (mkdir(BLOG_PATH.$blogID.BLOG_HTML,0755)) and $ret;
		if (!file_exists(BLOG_PATH.$blogID.PHOTOBLOG_UPLOADS))
			$ret = (mkdir(BLOG_PATH.$blogID.PHOTOBLOG_UPLOADS,0755)) and $ret;
		if (!file_exists(BLOG_PATH.$blogID.BLOG_UPLOADS))
			$ret = (mkdir(BLOG_PATH.$blogID.BLOG_UPLOADS,0755)) and $ret;
		// write INI
		$section = $blogID;
		$this->ini->setSectionValue($section,BLOG_ID,$blogID);
		$this->ini->setSectionValue($section,BLOG_NAME,$blogName);
		$this->ini->setSectionValue($section,BLOG_POSITION,$blogPosition);
		$this->ini->setSectionValue($section,BLOG_VISIBLE,$blogVisible);
		$this->ini->setSectionValue($section,BLOG_POSTPERPAGE,$blogPostPerPage);
		// create blogsBar
		$bb = new BlogBar();
		$bb->generate();		
		// check if all steps above were ok, if yes return true
		return $ret;
	}
	
	/**
	 * delete given blogID blog
	 */
	function deleteBlog($blogID){
		$bb = new BlogBar();
		// delete filesystem
		if (file_exists(BLOG_PATH.$blogID)){
			if (rm_dir(BLOG_PATH.$blogID,false)){
				// 	delete INI
				$this->ini->deleteSection($blogID);
				$bb->generate();				
			}
		}else{
			// 	delete INI
			$this->ini->deleteSection($blogID);
			// create blogsBar
			$bb->generate();					
		}
		
	}

	/**
	 * Gets array of strings composed of blogID
	 * @return array of blogID
	 */
	function getBlogsID(){
		$barray = array();
		$b = array_values($this->ini->getIniAsArray());
		for($i=0;$i<$this->getBlogsCount();$i++){
			$barray[$i] = $b[$i][BLOG_ID];
		}
		return $barray;				
	}

	/**
	 * Gets number of blogs
	 * @return number of blogs
	 */
	function getBlogsCount(){
		$tmp = $this->ini->getSectionsCount();
		return $tmp;
	}

	/**
	 * Gets first visible blog (alpha order)
	 * @return blogID string
	 */
	function getFirstVisible(){
		$tmp = $this->getAllBlogs();		
		foreach($tmp as $value){
			if(($value[BLOG_VISIBLE]==1)&&($value[BLOG_POSITION]==1)){
				return $value[BLOG_ID];
			}
		}	
	}

	/**
	 * Gets Post per Page
	 * @return int
	 */
	function getPostPerPage($blogID){
		$tmp = $this->getAllBlogs();		
		foreach($tmp as $value){
			if(strcasecmp($blogID,$value[BLOG_ID])==0){
				return $value[BLOG_POSTPERPAGE];
			}
		}
		return "99";	
	}

	/**
	 * Gets first blog (alpha order)
	 * @return first blogID
	 */
	function getFirst(){
		$tmp = $this->getBlogsID();
		return $tmp[0];
	}
	
	/**
	 * Returns true if given blogID exists
	 * @return boolean
	 */
	function  exists($blogID){
		$ret = false;
		$tmp = $this->getBlogsID();
		foreach ($tmp as $value) {
   			if (strcasecmp($blogID,$value)==0){
   				$ret = true;
   			}
		}
		return $ret;
	}

	/**
	 * Gets all blogs
	 * @return array of blogs
	 */
	function getAllBlogs(){
		return $this->ini->getIniAsArray();
	}


	/**
	 * Gets blog's fullname
	 * @return fullname string
	 */
	function getBlogFullName($blogID){
		$tmp = $this->getAllBlogs();		
		foreach($tmp as $value){
			if(strcasecmp($blogID,$value[BLOG_ID])==0){
				return $value[BLOG_NAME];
			}
		}
		return "";	
	}
	
	/**
	 * Gets blog's position
	 * @return position int
	 */
	function getBlogPosition($blogID){
		$tmp = $this->getAllBlogs();		
		foreach($tmp as $value){
			if(strcasecmp($blogID,$value[BLOG_ID])==0){
				return $value[BLOG_POSITION];
			}
		}
		return "";
	
	}


	/**
	 * Is blog visible?
	 * @return fullname string
	 */
	function isBlogVisible($blogID){
		$tmp = $this->getAllBlogs();		
		foreach($tmp as $value){
			if(strcasecmp($blogID,$value[BLOG_ID])==0){
				return $value[BLOG_VISIBLE];
			}
		}
		return false;
	
	}


} // end dclass
?>