<?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');
include_once('classes/core/types/TypesManager.class.php');
include_once('classes/core/types/Type.class.php');
include_once('classes/core/contents/ContentsManager.class.php');
include_once('interface/egManager.class.php');
include_once('interface/egContainer.class.php');
include_once('interface/egContextMenu.class.php');




/**
 * Content Class
 *
 * Generic content class. Holds fields value for a specified CMS type.
 *
 * @access     public
 * @author	   Cedric Francoys <cedric.francoys@easy-cms.org>
 * @version	   Content.class, Sun Nov 14 16:56:31 CET 2004
 * @package    Core
 * @subpackage Contents
 */
class Content extends CoreObject {
	/**
	 * @var integer Content Id
	 */
    var $id;


	/**
	 * @var string Content Type Name
	 */
    var $typeName;


	/**
	 * @var array Associative array of Easy-CMS types objects
	 */
    var $valuesTable;


	/**
	 * @var integer Deleted Content
	 */
    var $deleted;


	/**
	 * @var integer Published Content
	 */
    var $published;


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

	/**
	 * @var integer Content Author Id
	 */
    var $authorId;
    
	/**
	 * @var object ACL
	 */
    var $acl;




	/**
	 * Class constructor
	 *
	 * @param integer $id Content ID
	 * @param string  $typeName Content type

	 * @param array   $valuesTable associative array of Easy-CMS types objects
	 * @param integer $deleted Is Content Deleted
	 * @param integer $aclId Content's ACL ID
	 */
	function Content($id, $typeName, &$valuesTable, $deleted, $aclId) {
    parent::setClassName('Content');
    $this->debug('Content::Content, status : function invoked', 10, DBG_OPEN);
		$this->setContentId($id);
		$this->setContentTypeName($typeName);
		$this->setContentValuesTable($valuesTable);
		$this->setContentDeleted($deleted);
		$this->setContentAclId($aclId);
    $this->debug('Content::Content, status : function completed', 10, DBG_CLOSE);
	}


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


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


	/**
	 * Gets Content Type Name
	 *
	 * @return string
	 */
	function getContentTypeName() {
		return $this->typeName;
	}


	/**
	 * Sets Content Type Name
	 *
	 * @param string $typeName Content Type Name
	 */
	function setContentTypeName($typeName) {
		$this->typeName = $typeName;
	}


	/**
	 * Gets Content Values
	 *
	 * Returns associative array of Easy-CMS types objects
	 *
	 * @return array
	 */
	function &getContentValuesTable() {
		return $this->valuesTable;
	}


	/**
	 * Sets Content Values
	 *
	 * @param array $valuesTable Associative array of easy-CMS types objects
	 * values already set are overwritten
	 */
	function setContentValuesTable(&$valuesTable) {
    $this->debug('Content::setContentValuesTable, status : function invoked', 10, DBG_OPEN);
    foreach($valuesTable as $langId => $langValuesTable) {
      foreach($langValuesTable as $field => $value) {
		    $this->valuesTable[$langId][$field] = $value;
      }
    }
    $this->debug('Content::setContentValuesTable, status : function completed', 10, DBG_CLOSE);
	}


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


	/**
	 * Gets Content Publication
	 *
	 * Returns the publication status of the content
	 *
	 * @return integer
	 */
	function getContentPublished() {
		return $this->published;
	}


	function isPublished() {
		return (bool) $this->getContentPublished();
	}


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


	/**
	 * Sets Content Publication
	 *
	 * Tells if Content is published or not
	 *
	 * @param integer $published 0|1
	 */
	function setContentPublished($published) {
		$this->published = $published;
	}


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


  function setContentAuthorId($authorId) {
    $this->authorId = $authorId;
  }


  function getContentAuthorId() {
    return $this->authorId;
  }
  
	/**
	 * alias for getContentAclId
	 *
	 * @return integer
	 */
	function getAclId() {
		return $this->aclId;
	}


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


	/**
	 * Gets Content ACL
	 *
	 * Returns a reference to the ACL object used by Content
	 *
	 * @return object
	 */
	function &getContentAcl() {
    $this->debug('Content::getContentAcl, status : function invoked', 10, DBG_OPEN);
    $acl = null;
		if (!isset($this->acl)) {
      $this->debug('Content::getContentAcl, warning : acl not set', 4);
		}
    else {
      $acl = &$this->acl;
    }
    $this->debug('Content::getContentAcl, status : function completed', 10, DBG_CLOSE);
		return $acl;
	}



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


	/**
	 * Returns value for given field
	 *
	 * @param  string $field
	 * @return mixed  <0 if error
	 */
	function getContentFieldValue($langId, $field) {
    $this->debug('Content::getContentFieldValue, status : function invoked (lang '.$langId.', name '.$field.')', 10, DBG_OPEN);
    $value = null;
		if (!$this->contentFieldExists($langId, $field)) {
      $this->debug('Content::getContentFieldValue, warning : field does not exist', 4);
		}
		else {
      $value = &$this->valuesTable[$langId][$field];
		}
    $this->debug('Content::getContentFieldValue, status : function completed', 10, DBG_CLOSE);
		return $value;
	}


	/**
	 * Set value
	 *
	 * @param  string $field
	 * @param  object $valueObj
	 * @return mixed
	 */
	function setContentFieldValue($langId, $name, $value) {
    $this->debug('Content::setContentFieldValue, status : function invoked (lang '.$langId.', name '.$field.')', 10, DBG_OPEN);
    $result = true;
		if (!$this->contentFieldExists($langId, $name)) {
			$result = false;
      $this->debug('Content::setContentFieldValue, warning : field does not exist', 4);
		}
		else {
		  $this->valuesTable[$langId][$name] = $value;
		}
    $this->debug('Content::setContentFieldValue, status : function completed', 10, DBG_CLOSE);
		return $result;
	}


	/**
	 * Content Value Exists
	 *
	 * Tells if specific value is defined for Content
	 *
	 * @param  string $field value name
	 * @return boolean

	 */
	function contentFieldExists($langId, $field) {
		return is_object($this->valuesTable[$langId][$field]);
	}


	/**
	 * @return boolean
	 */
	function compareTo(&$contentObj, $field, $langId) {
    $this->debug('Content::compareTo, status : function invoked (lang '.$langId.', name '.$field.')', 10, DBG_OPEN);
    $result = 1;
		if ($field == '') {
      $this->debug('Content::compareTo, warning : no field specified', 3);
    }
    else {
      if(($subObj1 = $this->getContentFieldValue($langId, $field)) == null) {
        $this->debug('Content::compareTo, notice : specified field does not exist for current object', 4);
        $result = -1;
      }
      else {
     		if(($subObj2 = $contentObj->getContentFieldValue($langId, $field)) == null) {
          $this->debug('Content::compareTo, notice : specified field does not exist for specified object', 4);
     		}
     		else {
          $result = $subObj1->compareTo($subObj2);
     		}
      }
    }
    $this->debug('Content::compareTo, status : function completed', 10, DBG_CLOSE);
		return $result;
	}


	/**
 	 * @return string
 	 *
 	 */
	function toHTML($langId)	{
    $this->debug('Content::toHTML, status : function invoked (lang '.$langId.')', 10, DBG_OPEN);
		$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 />";
		}
    $this->debug('Content::toHTML, status : function completed', 10, DBG_CLOSE);
 		return $str;
 	}




	/**
	 * Exports Content 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) {
    $this->debug('Content::toXML, status : function invoked', 10, DBG_OPEN);
		$id          = $this->getContentId();
		$typeName    = $this->getContentTypeName();
		$valuesTable = &$this->getContentValuesTable();
    $author_id   = $this->getContentAuthorId();

    $site = &Site::getInstance();
    $usersManager = &$site->getUsersManager();

    if(($authorUser = &$usersManager->getUser($author_id))) {
      $author_firstname = $authorUser->getUserFirstName();
      $author_lastname  = $authorUser->getUserLastName();
    }
    else {
      $author_firstname = '';
      $author_lastname  = '';
    }
    
		$realContainerId = $containerId;
		$parentId        = $containerId;

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

    $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";
        $xml .= '  <parent-id>'.$parentId.'</parent-id>'."\n";
  			$xml .= '  <lang-id>'.$langId.'</lang-id>'."\n";
  			$xml .= '  <author-id>'.$authorId.'</author-id>'."\n";
  			$xml .= '  <author-name><![CDATA['.$author_firstname.' '.$author_lastname.']]></author-name>'."\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 .= '    <id>'.$id.'</id>'."\n";
  				$xml .= '    <container-id>'.$containerId.'</container-id>'."\n";
  				$xml .= '  </content_controls>'."\n";
  			}
  			$xml .= '</'.$typeName.'>'."\n";
      }
    }
    $this->debug('Content::toXML, status : function completed for content (id '.$id.', type '.$typeName.')', 10, DBG_CLOSE);
		return $xml;
	}


	/**
	 * Gets Content Controls
	 *
	 * Content 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) {
    $this->debug('Content::getControls, status : function invoked', 10, DBG_OPEN);
		$contentId  = $this->getContentId();
		$typeName   = $this->getContentTypeName();
		$acl        = &$this->getContentAcl();

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

		$html = '';

		if ($acl->userHasEditRight($user) && $mode == EDIT_MODE) {
			if(!isset($GLOBALS['contextMenuContent'])) {
				$menu = &new egContextMenu("menu_content", 0, 0, 150, 350);
				// move up
				$menu->addListItem(new egMenuItem("mnu_cn_1", LANG_CONTENT_MENU_MOVE_UP, "interface/icons/move_up.png", "contentMenuMoveUpOption();"));
				// move down
				$menu->addListItem(new egMenuItem("mnu_cn_2", LANG_CONTENT_MENU_MOVE_DOWN, "interface/icons/move_down.png", "contentMenuMoveDownOption();"));
				// move top
				$menu->addListItem(new egMenuItem("mnu_cn_3", LANG_CONTENT_MENU_MOVE_TOP, "interface/icons/move_top.png", "contentMenuMoveTopOption();"));
				// move bottom
				$menu->addListItem(new egMenuItem("mnu_cn_4", LANG_CONTENT_MENU_MOVE_BOTTOM, "interface/icons/move_bottom.png", "contentMenuMoveBottomOption();"));

				$menu->addSeparator();

				// remove content
				$menu->addListItem(new egMenuItem("mnu_cn_5", LANG_CONTENT_MENU_REMOVE, "interface/icons/remove.png", "contentMenuRemoveOption();"));
				
				$menu->addSeparator();

				// content properties
				$menu->addListItem(new egMenuItem("mnu_cn_6", LANG_CONTENT_MENU_EDIT, "interface/icons/edit.png", "contentMenuEditOption();"));

  	    $main = &new egContainer("eg_ctn_menu_content", 0, 0, 0, 0);
	      $main->addObject($menu);

  	    $manager = &egManager::getInstance();
	    	$manager->addContainer($main);
				$GLOBALS['contextMenuContent'] = &$menu;
			}
    }
    $this->debug('Content::getControls, status : function completed', 10, DBG_CLOSE);
		return $html;
	}
}

?>