<?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/PhotoBlogBar.class.php");
require_once(dirname(__FILE__)."/../common/utils.func.php");
require_once(dirname(__FILE__)."/../common/WriteFile.class.php");


define ('PHOTOBLOG_ID','photoBlogID');
define ('PHOTOBLOG_TEXT','photoBlogText');
define ('PHOTOBLOG_COMMENT','photoBlogComment');
define ('PHOTOBLOG_POSITION','photoBlogPosition');
define ('PHOTOBLOG_COLNUMBA','photoBlogColNumba');
define ('PHOTOBLOG_THUMBSIZE','photoBlogThumbSize');


/**
 * @title			: PhotoBlog
 * @description		: Manages photoalbum   
 * @date			: 15-Dec-2005   
 * @author			: Alberto Sfolcini  <a.sfolcini@gmail.com>
 */
class PhotoBlog {

	var $photoBlogText 		= "";
	var $photoBlogID		= "";
	var $photoBlogComment	= "";
	var $photoBlogPosition	= "";
	var $photoBlogColNumba	= "";
	var $photoBlogThumbSize	= "";
	var $blogID;
	var $ini;

	/**
	 * Constructor 1
	 */
	function PhotoBlog($blogID){
		$this->blogID = $blogID;
		$fn = BLOG_PATH.$this->blogID."/".PHOTOBLOG_INI;
		if (file_exists($fn)){ 
			$this->ini = new Ini($fn);
		}else{
			$tmp = new WriteFile($fn);
			$tmp->closeFile();
		}
	}

	/**
	 * Creates PhotoBlog
	 * @param $photoBlogID string
	 * @param $photoBlogText string
	 * @param $photoBlogPosition int
	 * @param $photoBlogColNumba int
	 * @param $photoBlogThumbSize int
	 * @return boolean
	 */
	function createPhotoBlog($photoBlogID,$photoBlogText,$photoBlogComment,$photoBlogPosition,$photoBlogColNumba,$photoBlogThumbSize){
		$this->photoBlogText		= $photoBlogText;
		$this->photoBlogID			= $photoBlogID;
		$this->photoBlogComment		= $photoBlogComment;
		$this->photoBlogPosition	= $photoBlogPosition;
		$this->photoBlogColNumba	= $photoBlogColNumba;
		$this->photoBlogThumbSize	= $photoBlogThumbSize;
		
		$ret = true;
		
 		// create fs structure
		if (!file_exists(BLOG_PATH.$this->blogID.PHOTOBLOG_UPLOADS))
			$ret = (mkdir(BLOG_PATH.$this->blogID.PHOTOBLOG_UPLOADS,0755)) and $ret;
		if (!file_exists(BLOG_PATH.$this->blogID.PHOTOBLOG_UPLOADS.$this->photoBlogID))
			$ret = (mkdir(BLOG_PATH.$this->blogID.PHOTOBLOG_UPLOADS.$this->photoBlogID,0755)) and $ret;
		if (!file_exists(BLOG_PATH.$this->blogID.PHOTOBLOG_UPLOADS.$this->photoBlogID.PHOTOBLOG_THUMBS))
			$ret = (mkdir(BLOG_PATH.$this->blogID.PHOTOBLOG_UPLOADS.$this->photoBlogID.PHOTOBLOG_THUMBS,0755)) and $ret;
		 
		// write INI
		$pb = $photoBlogID;
		$this->ini->setSectionValue($pb,PHOTOBLOG_ID,$photoBlogID);
		$this->ini->setSectionValue($pb,PHOTOBLOG_TEXT,$photoBlogText);
		$this->ini->setSectionValue($pb,PHOTOBLOG_COMMENT,$photoBlogComment);
		$this->ini->setSectionValue($pb,PHOTOBLOG_POSITION,$photoBlogPosition);
		$this->ini->setSectionValue($pb,PHOTOBLOG_COLNUMBA,$photoBlogColNumba);
		$this->ini->setSectionValue($pb,PHOTOBLOG_THUMBSIZE,$photoBlogThumbSize);
		// create PhotoBlogBar
		$pbb = new PhotoBlogBar($this->blogID);
		$pbb->generate();		
		// check if all steps above are ok, if yes return true
		return $ret;
	}
	
	/**
	 * delete given photoBlogID section
	 */
	function deletePhotoBlog($photoBlogID){
		$pb = new PhotoBlogBar($this->blogID);
		if (file_exists(BLOG_PATH.$this->blogID.PHOTOBLOG_UPLOADS.$photoBlogID)){
			rm_dir(BLOG_PATH.$this->blogID.PHOTOBLOG_UPLOADS.$photoBlogID,false);
			// 	delete INI
		}			
		$this->ini->deleteSection($photoBlogID);
		// create blogsBar
		$pb->generate();					
	}

	/**
	 * Delete all photoblogs and its content
	 */
	function deleteAllPhotoBlogs(){
		$a = array_values($this->ini->getIniAsArray());
		for($i=0;$i<$this->getPhotoBlogCount();$i++){
			$id = $a[$i][PHOTOBLOG_ID];
			rm_dir(BLOG_PATH.$this->blogID.PHOTOBLOG_UPLOADS.$id,false);			
		}	
	}

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

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

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

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


	/**
	 * Gets photoBlog text
	 * @return text string
	 */
	function getPhotoBlogText($photoBlogID){
		$tmp = $this->getAllPhotoBlog();		
		foreach($tmp as $value){
			if(strcasecmp($photoBlogID,$value[PHOTOBLOG_ID])==0){
				return $value[PHOTOBLOG_TEXT];
			}
		}
		return "";
	}

	/**
	 * Gets photoBlog Comment
	 * @return text string
	 */
	function getPhotoBlogComment($photoBlogID){
		$tmp = $this->getAllPhotoBlog();		
		foreach($tmp as $value){
			if(strcasecmp($photoBlogID,$value[PHOTOBLOG_ID])==0){
				return $value[PHOTOBLOG_COMMENT];
			}
		}
		return "";
	}

	
	/**
	 * Gets photoBlog position
	 * @return position int
	 */
	function getPhotoBlogPosition($photoBlogID){
		$tmp = $this->getAllPhotoBlog();		
		foreach($tmp as $value){
			if(strcasecmp($photoBlogID,$value[PHOTOBLOG_ID])==0){
				return $value[PHOTOBLOG_POSITION];
			}
		}
		return "";
	}

	/**
	 * Gets photoBlog colnumba
	 * @return cols int
	 */
	function getPhotoBlogColNumba($photoBlogID){
		$tmp = $this->getAllPhotoBlog();		
		foreach($tmp as $value){
			if(strcasecmp($photoBlogID,$value[PHOTOBLOG_ID])==0){
				return $value[PHOTOBLOG_COLNUMBA];
			}
		}
		return "";
	}


	/**
	 * Gets photoBlog ThumbSize
	 * @return size int
	 */
	function getPhotoBlogThumbSize($photoBlogID){
		$tmp = $this->getAllPhotoBlog();		
		foreach($tmp as $value){
			if(strcasecmp($photoBlogID,$value[PHOTOBLOG_ID])==0){
				return $value[PHOTOBLOG_THUMBSIZE];
			}
		}
		return "";
	}



} // end class
?>