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


define ('LINK_ID','linkID');
define ('LINK_TEXT','linkText');
define ('LINK_URL','linkURL');
define ('LINK_POSITION','linkPosition');


	
/**
 * @title			: Link
 * @description		: Manages Links   
 * @date			: 16-Nov-2005   
 * @author			: Alberto Sfolcini  <a.sfolcini@gmail.com>
 */
class Link {

	var $linkText 	= "";
	var $linkURL 	= "";
	var $linkID		= "";
	var $linkPosition	= "";
	var $blogID;
	var $ini;

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

	/**
	 * Creates Link
	 * @param $linkID string
	 * @param $linkText string
	 * @param $linkURL string
	 * @param $linkPosition int
	 * @return boolean
	 */
	function createLink($linkID,$linkText,$linkURL,$linkPosition){
		$this->linkText	= $linkText;
		$this->linkURL	= $linkURL;
		$this->linkID	= $linkID;
		$this->linkPosition	= $linkPosition;
		
		$ret = true;
		
		// write INI
		$lnk = $linkID;
		$this->ini->setSectionValue($lnk,LINK_ID,$linkID);
		$this->ini->setSectionValue($lnk,LINK_TEXT,$linkText);
		$this->ini->setSectionValue($lnk,LINK_URL,$linkURL);
		$this->ini->setSectionValue($lnk,LINK_POSITION,$linkPosition);
		// create LinkBar
		$sb = new LinkBar($this->blogID);
		$sb->generate();		
		// check if all steps above are ok, if yes return true
		return $ret;
	}
	
	/**
	 * delete given linkID section
	 */
	function deleteLink($linkID){
		$lb = new LinkBar($this->blogID);
			// 	delete INI
			$this->ini->deleteSection($linkID);
			// create blogsBar
			$lb->generate();					
	}

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

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

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

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


	/**
	 * Gets link's text
	 * @return text string
	 */
	function getLinkText($linkID){
		$tmp = $this->getAllLink();		
		foreach($tmp as $value){
			if(strcasecmp($linkID,$value[LINK_ID])==0){
				return $value[LINK_TEXT];
			}
		}
		return "";
	}

	/**
	 * Gets link's URL
	 * @return URL string
	 */
	function getLinkURL($linkID){
		$tmp = $this->getAllLink();		
		foreach($tmp as $value){
			if(strcasecmp($linkID,$value[LINK_ID])==0){
				return $value[LINK_URL];
			}
		}
		return "";
	}
	
	/**
	 * Gets link's position
	 * @return position int
	 */
	function getLinkPosition($linkID){
		$tmp = $this->getAllLink();		
		foreach($tmp as $value){
			if(strcasecmp($linkID,$value[LINK_ID])==0){
				return $value[LINK_POSITION];
			}
		}
		return "";
	}


} // end class
?>