<?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/Constants.inc.php");
require_once(dirname(__FILE__)."/../cms/SectionBar.class.php");
require_once(dirname(__FILE__)."/../common/utils.func.php");


define ('BSECTION_ID','bsectionID');
define ('BSECTION_NAME','bsectionName');
define ('BSECTION_POSITION','bsectionPosition');
define ('BSECTION_VISIBLE','bsectionVisible');

	
/**
 * @title			: SectionBlog
 * @description		: Manages SectionBlogs   
 * @date			: 28-Oct-2005   
 * @author			: Alberto Sfolcini  <a.sfolcini@gmail.com>
 */
class SectionBlog {

	var $bsectionName 	= "";
	var $bsectionID		= "";
	var $bsectionPosition	= "";
	var $bsectionVisible	= "";
	var $blogID;

	var $ini;

	/**
	 * Constructor 1
	 */
	function SectionBlog($blogID){
		$this->blogID = $blogID;
		$this->ini = new Ini(BLOG_PATH.$this->blogID."/".BLOG_SECTION_INI);
	}

	/**
	 * Creates sectionBlog
	 * @param $bsectionID string
	 * @param $bsectionName string
	 * @param $bsectionPosition int
	 * @param $bsectionVisible boolean
	 * @return boolean
	 */
	function createBlogSection($bsectionID,$bsectionName,$bsectionPosition,$bsectionVisible){
		$this->bsectionName	= $bsectionName;
		$this->bsectionID	= $bsectionID;
		$this->bsectionPosition	= $bsectionPosition;
		$this->bsectionVisible 	= $bsectionVisible;
		
		$ret = true;
		// create fs structure
		if (!file_exists(BLOG_PATH.$this->blogID."/".BLOG_DATA.$bsectionID))
			$ret = (mkdir(BLOG_PATH.$this->blogID."/".BLOG_DATA.$bsectionID,0775)) and $ret;
		
		// write INI
		$sec = $bsectionID;
		$this->ini->setSectionValue($sec,BSECTION_ID,$bsectionID);
		$this->ini->setSectionValue($sec,BSECTION_NAME,$bsectionName);
		$this->ini->setSectionValue($sec,BSECTION_POSITION,$bsectionPosition);
		$this->ini->setSectionValue($sec,BSECTION_VISIBLE,$bsectionVisible);
		// create SectionBar
		$sb = new SectionBar($this->blogID);
		$sb->generate();		
		// check if all steps above are ok, if yes return true
		return $ret;
	}
	
	/**
	 * delete given bsectionID section
	 */
	function deleteBlogSection($bsectionID){
		$sb = new SectionBar($this->blogID);
		// delete filesystem
		if (file_exists(BLOG_PATH.$this->blogID."/".BLOG_DATA.$bsectionID)){
			if (rm_dir(BLOG_PATH.$this->blogID."/".BLOG_DATA.$bsectionID,false)){
				// 	delete INI
				$this->ini->deleteSection($bsectionID);
				$sb->generate();				
			}
		}else{
			// 	delete INI
			$this->ini->deleteSection($bsectionID);
			// create blogsBar
			$sb->generate();					
		}		
	}

	/**
	 * Delete all blog sections
	 */
	function deleteAllSections(){
		$b = array_values($this->ini->getIniAsArray());
		for($i=0;$i<$this->getBlogSectionsCount();$i++){
			$id = $b[$i][BSECTION_ID];
			if (strcmp(trim($id),'')!=0)
				rm_dir(BLOG_PATH.$this->blogID."/".BLOG_DATA.$id,false);
		}		
	}

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

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

	/**
	 * Gets first Blogsection (alpha order)
	 * @return first bsectionID
	 */
	function getFirst(){
		$tmp = $this->getAllBlogSections();
		foreach($tmp as $value){
			if(($value[BSECTION_VISIBLE]==1)&&($value[BSECTION_POSITION]==1)){
				return $value[BSECTION_ID];
			}
		}	
	}
	
	/**
	 * Returns true if given bsectionID exists
	 * @return boolean
	 */
	function  exists($bsectionID){
		$ret = false;
		$tmp = $this->getBlogSectionsID();
		foreach ($tmp as $value) {
   			if (strcasecmp($bsectionID,$value)==0){
   				$ret = true;
   			}
		}
		return $ret;
	}

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


	/**
	 * Gets bsection's fullname
	 * @return fullname string
	 */
	function getBlogSectionFullName($bsectionID){
		$tmp = $this->getAllBlogSections();		
		foreach($tmp as $value){
			if(strcasecmp($bsectionID,$value[BSECTION_ID])==0){
				return $value[BSECTION_NAME];
			}
		}
		return "";
	
	}
	/**
	 * Gets bsection's position
	 * @return position int
	 */
	function getBlogSectionPosition($bsectionID){
		$tmp = $this->getAllBlogSections();		
		foreach($tmp as $value){
			if(strcasecmp($bsectionID,$value[BSECTION_ID])==0){
				return $value[BSECTION_POSITION];
			}
		}
		return "";
	
	}


	/**
	 * Is bsection visible?
	 * @return fullname string
	 */
	function isBlogSectionVisible($bsectionID){
		$tmp = $this->getAllBlogSections();		
		foreach($tmp as $value){
			if(strcasecmp($bsectionID,$value[BSECTION_ID])==0){
				return $value[BSECTION_VISIBLE];
			}
		}
		return false;
	
	}


} // end class
?>