<?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    Core
 * @subpackage Contents
 */

/**
 */
include_once('config.inc.php');
include_once('classes/core/CoreObject.class.php');

/**
 * VirtualFile Class
 *
 * Generic VirtualFile class. Holds values for a file of the virtual fiel system.
 *
 * @access     public
 * @author	   Cedric Francoys <cedric.francoys@easy-cms.org>
 * @version	   VirtualFile.class, Sun Nov 14 16:56:31 CET 2004
 * @package    Core
 * @subpackage Contents
 */
class VirtualFile extends CoreObject {
	/**
	 * @var integer Content Id
	 */
    var $id;

	/**
	 * @var integer
	 */
    var $nameId;

	/**
	 * @var date Creation Date
	 */
    var $creationDate;

	/**
	 * @var integer Creator User Id
	 */
    var $creatorId;

	/**
	 * @var integer Parent Virtual Directory Id
	 */
    var $directoryId;

	/**
	 * @var string Upload Name
	 */
    var $originalName;

	/**
	 * @var array Names
	 */
    var $namesTable;

	/**
	 * @var integer File ACL Id
	 */
    var $aclId;

	/**
	 * @var object ACL
	 */
    var $acl;


	/**
	 * Class constructor
	 *
	 * @param integer $id VirtualFile ID
	 */
	function VirtualFile($id) {
		$this->id = $id;
		$this->namesTable = array();
	}

	function getClassName() {
		return 'VirtualFile';
	}

	/**
	 * Gets VirtualFile Id
	 *
	 * @return integer
	 */
	function getVirtualFileId() {
		return $this->id;
	}

	/**
	 * Sets VirtualFile Id
	 *
	 * @param integer $id VirtualFile Id
	 */
    function setVirtualFileId($id) {
		$this->id = $id;
	}

	/**
	 * Sets VirtualFile Creator Id
	 *
	 * @param integer $userId Creator Id
	 */
	function setVirtualFileCreatorId($userId) {
		$this->creatorId = $userId;
	}

	/**
	 * Gets VirtualFile Creator Id
	 *
	 * @return integer
	 */
	function getVirtualFileCreatorId() {
		return $this->creatorId;
	}

	/**
	 * Gets VirtualFile Creation Date
	 *
	 * @return date
	 */
	function getVirtualFileCreationDate() {
		return $this->creationDate;
	}

	/**
	 * Sets VirtualFile Creation Date
	 *
	 * @param date $creationDate VirtualFile Creation Date
	 */
	function setVirtualFileCreationDate($creationDate) {
		$this->creationDate = $creationDate;
	}

	/**
	 * Gets VirtualFile Deletion
	 *
	 * Returns the deletion status of the VirtualFile
	 *
	 * @return integer
	 */
	function getVirtualFileDeleted() {
		return $this->deleted;
	}

	/**
	 * Sets VirtualFile Deletion
	 *
	 * Tells if VirtualFile is deleted or not
	 *
	 * @param integer $deleted 0|1
	 */
	function setVirtualFileDeleted($deleted) {
		$this->deleted = $deleted;
	}

	/**
	 * Gets VirtualFile ACL Id
	 *
	 * @return integer
	 */
	function getVirtualFileAclId() {
		return $this->aclId;
	}

	/**
	 * Sets VirtualFile ACL Id
	 *
	 * @param integer $aclId ACL Id
	 */
	function setVirtualFileAclId($aclId) {
		$this->aclId = $aclId;
	}

	/**
	 * Gets VirtualFile ACL
	 *
	 * Returns a reference to the ACL object used by VirtualFile
	 *
	 * @return object
	 */
	function &getVirtualFileAcl() {
		if (!isset($this->acl)) {
			return null;
		}
		return $this->acl;
	}

	/**
	 * Sets VirtualFile ACL
	 *
	 * @param object $acl ACL object
	 */
	function setVirtualFileAcl(&$acl) {
		$this->acl = &$acl;
	}

	function getVirtualFileName($langId) {
		if(isset($this->namesTable[$langId])) {
			return $this->namesTable[$langId];
		}
		return '';
	}

	function setVirtualFileName($name, $langId) {
		$this->namesTable[$langId] = $name;
	}

	/**
	 * @return boolean
	 * @todo check code
	 */
	function compareTo(&$virtualFileObj, $langId) {
		$subObj1 = $this->getVirtualFileName($langId);
		$subObj2 = $virtualFileObj->getVirtualFileName($langId);
		return $subObj1->compareTo($subObj2);
	}

	/**
 	 * @return string
 	 *
 	 */
	function toHTML($langId)	{
		$valuesTable = &$this->getContentValuesTable();
 		$str = "";
		foreach ($valuesTable[$langId] as $key => $value) {
			$valueSummary = htmlentities(String::summarize($value->toString(), 40));
			$cr = array("\n", "\r", "\r\n");
			$valueSummary = str_replace($cr, '', $valueSummary);
			$str .= "<b>$key : </b>&nbsp;&nbsp;".$valueSummary."<br />";
		}
 		return $str;
 	}

	/**
	 * Exports VirtualFile Into XML
	 *
	 * @param  object $user User object
	 * @param  integer $containerId Container ID
	 * @param  string $mode
	 * @return string XML
	 */
  function toXML(&$user, $langId, $containerId=0, $cloneId=0, $mode=VIEW_MODE) {
		$id          = $this->getContentId();
		$typeName    = $this->getContentTypeName();
		$valuesTable = &$this->getContentValuesTable();

		$realContainerId = $containerId;

		if($cloneId > 0) {
			$realContainerId = $cloneId;
		}

		$this->debug('Content::toXML, status : method invoked for Content(id='.$id.' type_name="'.$typeName.'")', 10);

    $xml = '';
		if ($this->acl->userHasViewRight($user) && ($mode == EDIT_MODE || $this->isPublished())) {
      if(isset($valuesTable[$langId])) {
        $xml .= '<'.$typeName.'>'."\n";
  			$xml .= '  <id>'.$id.'</id>'."\n";
  			$xml .= '  <container-id>'.$realContainerId.'</container-id>'."\n";
        foreach($valuesTable[$langId] as $key => $value) {
          $xml .= '  <'.$key.'>'.$value->toXML().'</'.$key.'>'."\n";
        }
        if ($this->acl->userHasEditRight($user) && $mode == EDIT_MODE) {
  				$xml .= '  <content_controls>'."\n";
  				$xml .= '    <type>'.$typeName.'</type>'."\n";
  				$xml .= '    <id>'.$id.'</id>'."\n";
  				$xml .= '    <container-id>'.$containerId.'</container-id>'."\n";
  				$xml .= '    <clone-id>'.$cloneId.'</clone-id>'."\n";
  				$xml .= '  </content_controls>'."\n";
  			}
  			$xml .= '</'.$typeName.'>'."\n";
      }
    }
		return $xml;
	}

	/**
	 * Gets VirtualFile Controls
	 *
	 * VirtualFile controls pop-up menu
	 *
	 * @param  object $user User Object
	 * @param  integer $containerId Container Id
	 * @param  string $mode
	 * @return string html
	 */
	function getControls(&$user, $containerId=0, $cloneId=0, $mode=VIEW_MODE) {

		$contentId  = $this->getContentId();
		$typeName   = $this->getContentTypeName();
		$acl        = &$this->getContentAcl();

		$realContainerId = $containerId;
		if($cloneId > 0) {
			$realContainerId = $cloneId;
		}

		$html = '';

		if ($acl->userHasEditRight($user) && $mode == EDIT_MODE) {
      $menu = &new egContextMenu("menu_content_".$containerId."_".$cloneId."_".$contentId, 0, 0, 150, 350);

			// move up
      $menu->addListItem(new egMenuItem("mnu_cn_".$containerId."_".$cloneId."_".$contentId."_2", LANG_CONTENT_MENU_MOVE_UP, "interface/icons/move_up.png", "moveUpCn_".$containerId."_".$cloneId."_".$contentId."();"));
			// move down
      $menu->addListItem(new egMenuItem("mnu_cn_".$containerId."_".$cloneId."_".$contentId."_3", LANG_CONTENT_MENU_MOVE_DOWN, "interface/icons/move_down.png", "moveDownCn_".$containerId."_".$cloneId."_".$contentId."();"));
	    $menu->addSeparator();
			// remove content
			if ($this->acl->userHasRemoveRight($user)) {
        $menu->addListItem(new egMenuItem("mnu_cn_".$containerId."_".$cloneId."_".$contentId."_4", LANG_CONTENT_MENU_REMOVE, "interface/icons/remove.png", "launchContentDeletionWindow(\'container_id=$realContainerId&amp;content_id=$contentId&amp;type_name=$typeName\');"));
			}
	    $menu->addSeparator();
			// content properties
	    $menu->addListItem(new egMenuItem("mnu_cn_".$containerId."_".$cloneId."_".$contentId."_5", LANG_CONTENT_MENU_EDIT, "interface/icons/edit.png", "launchContentEditionWindow(\'container_id=$realContainerId&amp;content_id=$contentId&amp;type_name=$typeName\');"));

      $main = &new egContainer("eg_ctn_cn_".$containerId."_".$cloneId."_".$contentId, 0, 0, 0, 0);

      $main->addObject($menu);

      $manager = &egManager::getInstance();
    	$manager->addContainer($main);
      $scripts = new egScript();

      $line = "function moveUpCn_".$containerId."_".$cloneId."_".$contentId."() {
window.location.href = 'index.php?display=".EDIT_CONTAINER."&action=".MOVE_CONTENT_UP."&container_id=$realContainerId&content_id=$contentId';
}
function moveDownCn_".$containerId."_".$cloneId."_".$contentId."() {
window.location.href = 'index.php?display=".EDIT_CONTAINER."&action=".MOVE_CONTENT_DOWN."&container_id=$realContainerId&content_id=$contentId';
}";
      $scripts->addLine($line);

      $manager->addScripts($scripts);
    }
		return $html;
	}
}
?>