<?php
/*
 * This file is part of the easy-CMS project.
 * easy-CMS 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.
 * 
 * easy-CMS 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 easy-CMS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
 
/**
 * @package Types
 */
 
include_once('config.inc.php');
include_once('classes/system/File.class.php');

/**
 * Holds document informations.
 *
 * @access   private
 * @author	Cedric Francoys <cedric.francoys@easy-cms.org>
 * @version	Document.class, Sat Nov 13 13:13:28 CET 2004
 * @package	Types
 */
class Document {
	/**
	 * File Name
	 *
	 * @access private
	 * @var    string
	 */
  var $value;

	/**
	 * Constructor
	 *
	 * @param string $value
	 */
  function Document($value = '') {
    $this->setValue($value);
  }

  function getType() {
    return EASY_CMS_TYPE_DOCUMENT;
  }

  function compareTo(&$documentObj){
    return strcmp($this->getValue(), $documentObj->getValue());
  }
  
	/**
	 * Sets File Name
	 *
	 * @param string $value
	 */
  function setValue($value) {
    $this->value = $value;
  }

	/**
	 * Gets File Name
	 *
	 * @return string
	 */
  function getValue() {
    return $this->value;
  }
    
	/**
	 * @deprecated
	 * Gets Document Path
	 *
	 * @return string
	 */
  function getPath() {
    return REPOSITORY_FOLDER.'/'.DOCUMENTS_FOLDER.'/'.$this->getValue();
  }

	/**
	 * Gets File Name
	 *
	 * @return string
	 */
  function getFileName() {
		/*
        $value = $this->getValue();
		$array = explode('/', $value);
		$name = '';
		foreach($array as $node){
			$name = $node;
		}
		return $name;
		*/
		return basename($this->getValue());
  }
    
	/**
	 * Gets Icon
	 *
	 * @param  string $extension
	 * @return string
	 */
	function getIconName($extension) {
    switch (strtolower($extension)) {
    	case 'rtf':
    	case 'doc':
    		$icon = 'icon_doc_doc.png';
    		break;
    	case 'pdf':
    		$icon = 'icon_doc_pdf.png';
    		break;
    	case 'xls':
    		$icon = 'icon_doc_xls.png';
    		break;
    	case 'exe':
    		$icon = 'icon_doc_exe.png';
    		break;
    	case 'zip':
    		$icon = 'icon_doc_zip.png';
        break;
    	case 'png':
    	case 'bmp':
    	case 'tif':
    	case 'tiff':
    	case 'jpg':
    	case 'jpeg':
    	case 'gif':
    		$icon = 'icon_doc_img.png';
    		break;
    	default:
    		$icon = 'icon_doc_txt.png';
		}
		return $icon;
	}

	function getDirName() {
		$value = &$this->getValue();
		$array = explode('/', $value);
		$name = '';
		$dir = '';
		foreach($array as $node){
			if($dir != '')
				$dir .= '/';
			$dir .= $name;
			$name = $node;
		}
		return $dir;
	}

	/**
	 * Exports Document Value Into String
	 *
	 * @return string
	 */
  function toString() {
    return $this->getValue();
  }

	/**
	 * Exports Document Into XML
	 *
	 * @return string
	 */
  function toXML() {
		//$path = $this->getPath();
		$path = $this->getValue();
		$dir = $this->getDirName();
		$file = &new File('', getcwd().'/'.$path);
		$xml  = "";
    $xml .= "<path>".$path."</path>\n";
		$xml .= "<dir>".$dir."</dir>\n";
		$xml .= "<name>".$file->getName()."</name>\n";
		$xml .= "<fullname><![CDATA[".$file->getNodeFullName()."]]></fullname>\n";
		$xml .= "<size>".$file->getSize()."</size>\n";
		$xml .= "<type>".$file->getExtension()."</type>\n";
		$xml .= "<icon><![CDATA[includes/images/".$this->getIconName($file->getExtension())."]]></icon>\n";
		return $xml;
  }

	/**
	 * Exports Document Into SQL
	 *
	 * @return string
	 */
  function toSQL() {
    return addslashes($this->getValue());
  }

}
?>