<?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 Pages
 */
 
/**
 */
include_once('config.inc.php');
include_once('classes/core/CoreObject.class.php');
include_once('classes/core/frames/Frame.class.php');
include_once('interface/egManager.class.php');
include_once('interface/egContainer.class.php');
include_once('interface/egContextMenu.class.php');

/**
 * Page Class
 *
 * A page holds infos about template and associations between frame 
 * and containers to be display on this page.
 *
 * @access     public
 * @author	   Cedric Francoys <cedric.francoys@easy-cms.org>
 * @version    Page.class,v 0.1
 * @package	   Core
 * @subpackage Pages
 */
class Page extends CoreObject {	
	/**
	 * @var integer
	 */
	var $id;
	
	/**
	 * @var integer
	 */
	var $parentId;
	
	/**
	 * @var integer
	 */
	var $nameId;
	
	/**
	 * @var integer
	 */
	var $aliasId;
	
	/**
	 * @var integer
	 */
	var $titleId;
	
	/**
	 * @var integer
	 */
	var $descriptionId;
	
	/**
	 * @var integer
	 */
	var $keywordsId;
	
	/**
	 * @var integer
	 */
	var $frameId;
	
	/**
	 * @var string
	 */
	var $link;

	/**
	 * @var integer
	 */
	var $cache;
	
	/**
	 * @var integer
	 */
	var $deleted;
	
	/**
	 * @var integer
	 */
	var $published;

	/**
	 * @var integer
	 */
	var $aclId;	
	
	/**
	 * @var object
	 */
	var $acl;

	/**
	 * @var object
	 */
	var $frame;

	/**
	 * @var array
	 */
	var $titlesTable;

	/**
	 * @var array
	 */
	var $namesTable;
	
	/**
	 * @var array
	 */
	var $aliasesTable;
	
	/**
	 * @var array
	 */
	var $descriptionsTable;
	
	/**
	 * @var array
	 */
	var $keywordsTable;

	
	/**
	 * Constructor
	 *
	 * @param integer $id Page Id
	 * @param integer $parentId Parent Page Id
	 * @param integer $nameId Page Name Id
	 * @param integer $aliasId Page Alias Id
	 * @param integer $titleId Page Title Id
	 * @param integer $descriptionId Page Description Id
	 * @param integer $keywordsId Page Keywords Id
	 * @param integer $frameId Page Frame Id
	 * @param integer $cache Is Page Cached
	 * @param integer $deleted Is Page Deleted
	 * @param integer $aclId Page ACL Id
	 */
	function Page($id=0, $parentId=0, $nameId=0, $aliasId=0, $titleId=0, $descriptionId=0, $keywordsId=0, $frameId=0, $cache=0, $deleted=0, $aclId=0) {
		$this->namesTable = array();
    $this->aliasesTable = array();
    $this->titlesTable = array();
    $this->descriptionsTable = array();
    $this->keywordsTable = array();
		$this->setPageId($id);
		$this->setPageParentId($parentId);
		$this->setPageNameId($nameId);
		$this->setPageAliasId($aliasId);
		$this->setPageTitleId($titleId);
		$this->setPageDescriptionId($descriptionId);
		$this->setPageKeywordsId($keywordsId);
		$this->setPageFrameId($frameId);
		$this->setPageCache($cache);
		$this->setPageDeleted($deleted);
		$this->setPageAclId($aclId);
	}
	
	function getPageURL() {
		// relative link or absolute link
		if(!strncasecmp($this->getPageLink(), 'http://', 7)) {
			$url = $this->getPageLink();
		}
		else {
			$scriptPath = substr(dirname($_SERVER["PHP_SELF"]), 1);
			$url = "http://".$_SERVER["SERVER_NAME"];
			if(strlen($scriptPath)) {
				$url .= "/".$scriptPath;
			}
			$pageLink = $this->getPageLink();
			if(strlen($pageLink) && $pageLink[0] != '/') {
				$pageLink = '/'.$pageLink;
			}
			$url .= $pageLink;
			// prevent from redirecting to the current page !
			$params = substr(strstr($this->getPageLink(), '?'), 1);
			$paramArray = explode('&', $params);
			foreach($paramArray as $param) {
				list($paramName, $paramValue) = explode('=', $param);
				if(!(strncmp($paramName, 'pid', 3)) && ($paramValue == $this->getPageId())) {
					return false;
				}
			}
		}
		return $url;
	}


	function getPageLinkOrAlias($langId) {
	  if(strlen($this->getPageAlias($langId)) > 0) {
	    $alias = $this->getPageAlias($langId);
	    if($alias[0] != '/') {
	      $alias = '/'.$alias;
	    }
	    return $alias;
	  }
    return '/index.php?pid='.$this->getPageId();
	}

	/**
	 * Gets Page Id
	 *
	 * @return integer
	 */
	function getPageId(){
		return $this->id;
	}
	
	/**
	 * Sets Page Id
	 *
	 * @param integer $id
	 */
	function setPageId($id){
		$this->id = $id;
	}
	
	/**
	 * Gets Parent Page Id
	 *
	 * @return integer
	 */
	function getPageParentId(){
		return $this->parentId;
	}
	
	/**
	 * Sets Parent Page Id
	 *
	 * @param integer $parentId
	 */
	function setPageParentId($parentId){
		$this->parentId = $parentId;
	}
	
	/**
	 * Gets Page Name Id
	 *
	 * @return integer
	 */
	function getPageNameId(){
		return $this->nameId;
	}
	
	/**
	 * Gets Page Redirection Link
	 *
	 * @return string
	 */
	function getPageLink(){
		if(!isset($this->link)) {
			return '';
		}
		return $this->link;
	}

	/**
	 * Sets Redirection Link
	 *
	 * @param string
	 */
	function setPageLink($link){
		$this->link = $link;
	}

	/**
	 * Sets Page
	 *
	 * @param integer
	 */
	function setPageNameId($nameId){
		$this->nameId = $nameId;
	}
	
	/**
	 * Gets Page Name
	 *
	 * @param  integer $langId
	 * @return string
	 */
	function getPageName($langId){
		if(!isset($this->namesTable[$langId])) {
			return "";
		}
		return $this->namesTable[$langId];
	}
	
	/**
	 * Sets Page Name
	 *
	 * @param string  $name
	 * @param integer $langId
	 */
	function setPageName($name, $langId){
		$this->namesTable[$langId] = stripslashes($name);
	}
	
	/**
	 * Gets Page Alias Id
	 *
	 * @return integer
	 */
	function getPageAliasId(){
		return $this->aliasId;
	}
	
	/**
	 * Sets Page Alias Id
	 *
	 * @param integer $aliasId
	 */
	function setPageAliasId($aliasId){
		$this->aliasId = $aliasId;
	}
	
	/**
	 * Gets Page Alias
	 *
	 * @param  integer $langId
	 * @return string
	 */
	function getPageAlias($langId){
		if(!isset($this->aliasesTable[$langId])) {
			return "";
		}
		return $this->aliasesTable[$langId];
	}

	/**
	 * Sets Page Alias
	 *
	 * @param string  $alias
	 * @param integer $langId
	 */
	function setPageAlias($alias, $langId){
		$this->aliasesTable[$langId] = stripslashes($alias);
	}

	/**
	 * Gets Page Title Id
	 *
	 * @return integer
	 */
	function getPageTitleId(){
		return $this->titleId;
	}
	
	/**
	 * Sets Page Title Id
	 *
	 * @param integer $id
	 */
	function setPageTitleId($titleId){
		$this->titleId = $titleId;
	}
	
	/**
	 * Gets Page Title
	 *
	 * @param  integer $langId
	 * @return string
	 */
	function getPageTitle($langId){
		if(!isset($this->titlesTable[$langId])) {
			return "";
		}
		return $this->titlesTable[$langId];
	}

	/**
	 * Sets Page Title
	 *
	 * @param string $title
	 * @param integer $langId
	 */
	function setPageTitle($title, $langId){
		$this->titlesTable[$langId] = stripslashes($title);
	}

	/**
	 * Gets Page Description Id
	 *
	 * @return integer
	 */
	function getPageDescriptionId(){
		return $this->descriptionId;
	}
	
	/**
	 * Sets Page Description Id
	 *
	 * @param integer $descriptionId
	 */
	function setPageDescriptionId($descriptionId){
		$this->descriptionId = $descriptionId;
	}
	
	/**
	 * Gets Page Description
	 *
	 * @param  integer $langId
	 * @return string
	 */
	function getPageDescription($langId){
		if(!isset($this->descriptionsTable[$langId])) {
			return "";
		}
		return $this->descriptionsTable[$langId];
	}

	/**
	 * Sets Page Description
	 *
	 * @param string $description
	 * @param integer $langId
	 */
	function setPageDescription($description, $langId){
		$this->descriptionsTable[$langId] = stripslashes($description);
	}
	
	/**
	 * Gets Page Keywords Id
	 *
	 * @return integer
	 */
	function getPageKeywordsId(){
		return $this->keywordsId;
	}
	
	/**
	 * Sets Page Keywords Id
	 *
	 * @param integer $keywordsId
	 */
	function setPageKeywordsId($keywordsId){
		$this->keywordsId = $keywordsId;
	}
	
	/**
	 * Gets Page Keywords
	 *
	 * @param  integer $langId
	 * @return string
	 */
	function getPageKeywords($langId){
		if(!isset($this->keywordsTable[$langId])) {
			return "";
		}
		return $this->keywordsTable[$langId];
	}

	/**
	 * Sets Page Keywords
	 *
	 * @param string $keywords
	 * @param integer $langId
	 */
	function setPageKeywords($keywords, $langId){
		$this->keywordsTable[$langId] = stripslashes($keywords);
	}
	
	/**
	 * Gets Page Frame Id
	 *
	 * @return integer
	 */
	function getPageFrameId(){
		return $this->frameId;
	}
	
	/**
	 * Sets Page Frame Id
	 *
	 * @param integer $frameId
	 */
	function setPageFrameId($frameId){
		$this->frameId = $frameId;
	}

	/**
	 * Gets Page Published
	 *
	 * @return integer
	 */
	function getPagePublished(){
		return $this->published;
	}

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

	/**
	 * Sets Page Published
	 *
	 * @param integer $published
	 */
	function setPagePublished($published){
		$this->published = $published;
	}

	/**
	 * Gets Page Frame
	 *
	 * @return object
	 */
	function &getPageFrame() {
		if (!is_object($this->frame)){
			return null;
		}
		return $this->frame;
	}
	
	/**
	 * Sets Page Frame
	 *
	 * @param object $frame
	 */
	function setPageFrame(&$frame){
		$this->frame = &$frame;
	}

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

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

	/**
	 * Sets Page Acl Id
	 *
	 * @param integer $aclId
	 */
	function setPageAclId($aclId){
		$this->aclId = $aclId;
	}
	
	/**
	 * Gets Page ACL
	 *
	 * @return object
	 */
	function &getPageAcl(){
		if (!isset($this->acl))
			return null;
		return $this->acl;
	}
	
	/**
	 * Sets Page ACL
	 *
	 * @param object $acl
	 */
	function setPageAcl(&$acl){
		$this->acl = &$acl;
	}

	/**
	 * Gets Page Cache
	 *
	 * Returns 1 if page cache is available
	 *
	 * @return integer
	 */
	function getPageCache(){
		return $this->cache;
	}
	
	/**
	 * Sets Page Cache
	 *
	 * @param integer $cache
	 */
	function setPageCache($cache){
		$this->cache = $cache;
	}
	
	/**
	 * Gets Page Deleted
	 *
	 * Returns 1 if page is deleted
	 *
	 * @return integer
	 */
	function getPageDeleted(){
		return $this->deleted;
	}
	
	/**
	 * Sets Page Deleted
	 *
	 * @param integer $deleted
	 */
	function setPageDeleted($deleted){
		$this->deleted = $deleted;
	}
	
	/**
	 * Gets Type Name
	 *
	 * Compatibility in templates management with 
	 * other contents (for menus display)
	 *
	 * @return string Always returns 'easy_cms_page'
	 */
	//not correct : function getPageTypeName() {
	function getTypeName(){
		return 'easy_cms_page';
	}
	
	/**
	 * Gets Page Value
	 *
	 * @param  string $fieldName
	 * @return mixed
	 */
	function getPageValue($fieldName, $langId) {
		if($fieldName == 'name')
			return $this->getPageName($langId);
		if($fieldName == 'alias')
			return $this->getPageAlias($langId);
		if($fieldName == 'title')
			return $this->getPageTitle($langId);
		if($fieldName == 'description')
			return $this->getPageDescription($langId);
		if($fieldName == 'keywords')
			return $this->getPageKeywords($langId);

		if($fieldName == 'id') {
			return $this->getPageId();
		}
		if($fieldName == 'name_id') {
			return $this->getPageNameId();
		}
		if($fieldName == 'alias_id') {
			return $this->getPageAliasId();
		}
		if($fieldName == 'title_id') {
			return $this->getPageTitleId();
		}
		if($fieldName == 'description_id') {
			return $this->getPageDescriptionId();
		}
		if($fieldName == 'keywords_id') {
			return $this->getPageKeywordsId();
		}
		if($fieldName == 'parent_id') {
			return $this->getPageParentId();
		}
		if($fieldName == 'frame_id') {
			return $this->getPageFrameId();
		}
		if($fieldName == 'cache') {
			return $this->getPageCache();
		}
		if($fieldName == 'deleted') {
			return $this->getPageDeleted();
		}
		if($fieldName == 'published') {
			return $this->getPagePublished();
		}
		$this->debug('Page::getValue, warning : non-exitent "'.$fieldName.'" value : 1 returned', 3);
		return 1;
	}


	function getPageValuesTable() {
    $valuesTable = array();

    foreach($this->namesTable as $langId => $name) {
      $valuesTable[$langId]['name'] = $name;
    }
    foreach($this->aliasesTable as $langId => $alias) {
      $valuesTable[$langId]['alias'] = $alias;
    }
    foreach($this->titlesTable as $langId => $title) {
      $valuesTable[$langId]['title'] = $title;
    }
    foreach($this->descriptionsTable as $langId => $description) {
      $valuesTable[$langId]['description'] = $description;
    }
    foreach($this->keywordsTable as $langId => $keywords) {
      $valuesTable[$langId]['keywords'] = $keywords;
    }

		$valuesTable['id'] = $this->getPageId();
		$valuesTable['name_id'] = $this->getPageNameId();
		$valuesTable['alias_id'] = $this->getPageAliasId();
		$valuesTable['title_id'] = $this->getPageTitleId();
		$valuesTable['description_id'] = $this->getPageDescriptionId();
		$valuesTable['keywords_id'] = $this->getPageKeywordsId();
		$valuesTable['parent_id'] = $this->getPageParentId();
		$valuesTable['frame_id'] = $this->getPageFrameId();
		$valuesTable['cache'] = $this->getPageCache();
		$valuesTable['deleted'] = $this->getPageDeleted();
		$valuesTable['published'] = $this->getPagePublished();
		$valuesTable['link'] = $this->getPageLink();
		$valuesTable['acl_id'] = $this->getPageAclId();

    return $valuesTable;
  }

	/**
	 * Sets Page Values
	 *
	 * @param array $array
	 */
	function setPageValues($array) {
		if(!is_array($array)) {
			return;
		}
    foreach($array as $key => $valuesTable) {
			if(is_int($key)) {
				$langId = $key;
				if(isset($valuesTable['name'])) {
					$this->setPageName($valuesTable['name'], $langId);
				}
				if(isset($valuesTable['alias'])) {
					$this->setPageAlias($valuesTable['alias'], $langId);
				}
				if(isset($valuesTable['title'])) {
					$this->setPageTitle($valuesTable['title'], $langId);
				}
				if(isset($valuesTable['description'])) {
					$this->setPageDescription($valuesTable['description'], $langId);
				}
				if(isset($valuesTable['keywords'])) {
					$this->setPageKeywords($valuesTable['keywords'], $langId);
				}
			}
    }

		if(isset($array['id'])) {
			$this->setPageId($array['id']);
		}
		if(isset($array['name_id'])) {
			$this->setPageNameId($array['name_id']);
		}
		if(isset($array['alias_id'])) {
			$this->setPageAliasId($array['alias_id']);
		}
		if(isset($array['title_id'])) {
			$this->setPageTitleId($array['title_id']);
		}
		if(isset($array['description_id'])) {
			$this->setPageDescriptionId($array['description_id']);
		}
		if(isset($array['keywords_id'])) {
			$this->setPageKeywordsId($array['keywords_id']);
		}
		if(isset($array['parent_id'])) {
			$this->setPageParentId($array['parent_id']);
		}
		if(isset($array['frame_id'])) {
			$this->setPageFrameId($array['frame_id']);
		}
		if(isset($array['cache'])) {
			$this->setPageCache($array['cache']);
		}
		if(isset($array['deleted'])) {
			$this->setPageDeleted($array['deleted']);
		}
		if(isset($array['published'])) {
			$this->setPagePublished($array['published']);
		}
		if(isset($array['link'])) {
			$this->setPageLink($array['link']);
		}
		if (isset($array['acl_id'])) {
			$this->setPageAclId($array['acl_id']);
		}
	}
	
	/**
	 * Compares To Page Object
	 *
	 * Compares current Page object to given Page object
	 *
	 * @param  object $page
	 * @param  string $field
	 * @return integer
	 * @todo check code or deprecation
	 */
	function compareTo(&$page, $field) {
	// todo : test with language param
/*
		if ($field == "") {
			return 1;
		}
		$value1 = $this->getPageValue($field, $langId);
		$value2 = $page->getPageValue($field, $langId);
		if(is_numeric($value1) && is_numeric($value2)){
			$value1 = sprintf("%05d", $value1);
			$value2 = sprintf("%05d", $value2);
		}
		return strcmp($value1, $value2);
*/
	}
	
	/**
	 * Gets Page Controls
	 *
	 * Page controls pop-up menu
	 *
	 * @param  object $user User Object
	 * @param  integer $menuId Menu Id
	 * @param  string $mode
	 * @return string html
	 */
	function getControls(&$user, $menuId=0, $mode=VIEW_MODE) {
		$pageId  = $this->getPageId();
		$acl     = &$this->getPageAcl();

		$html = '';

    if ($acl->userHasEditRight($user) && $mode == EDIT_MODE) {

			if(!isset($GLOBALS['contextMenuPage'])) {
	      $menu = &new egContextMenu("menu_page", 0, 0, 150, 350);
				if(ENABLE_PAGES_CREATION == 1) {
		      // add sub page
		      $menu->addListItem(new egMenuItem("mnu_pg_1", LANG_PAGE_MENU_ADD, "/interface/icons/add.png", "pageMenuAddOption();"));
			    $menu->addSeparator();
				}
				// remove page
				if ($this->acl->userHasRemoveRight($user)) {
	        $menu->addListItem(new egMenuItem("mnu_pg_2", LANG_PAGE_MENU_REMOVE, "/interface/icons/remove.png", "pageMenuRemoveOption();"));
				}
		    $menu->addSeparator();
				// page properties
		    $menu->addListItem(new egMenuItem("mnu_pg_3", LANG_PAGE_MENU_EDIT, "/interface/icons/edit.png", "pageMenuEditOption();"));

	      $main = &new egContainer("eg_ctn_menu_page", 0, 0, 0, 0);

	      $main->addObject($menu);

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

				$GLOBALS['contextMenuPage'] = &$menu;
			}
 			$html .= "<a class=\"easy-CMS_UI_propertiesIcon\" href=\"javascript:selectPage('$pageId');egShowContextMenu(contextMenu_menu_page);\"><img src=\"/interface/icons/properties.png\" /></a>";
    }
		return $html;
	}

	/**
	 * Gets HTML Body
	 *
	 * Content of the page
	 *
	 * @return string
	 */
	function getHTMLBody($langId, &$user, $mode=VIEW_MODE) {
    $body = '';
		$frame = &$this->getPageFrame();
		if (!$frame) {
			$this->debug('Page::getHTMLBody, error : Frame(id='.$this->getPageFrameId().') not loaded', 3);
			return '';
		}
		if ($this->acl->userHasViewRight($user) && ($mode == EDIT_MODE || $this->isPublished())) {
  		$body .= $frame->getHTML($langId);
    }
		return $body;
	}

	/**
	 * Gets HTML Styles
	 *
	 * Styles used within page
	 *
	 * @return array
	 */
	function getHTMLStyles() {
		$frame = &$this->getPageFrame();
		if (!$frame) {
			$this->debug('Page::getHTMLStyles, error : Frame(id='.$this->getPageFrameId().') not loaded', 3);
			return null;
		}
		$stylesArray = $frame->getFrameStyles();
		return $stylesArray;
		/*
    if(!is_object($this->frame)) {
		  $this->debug("Page.getHTMLStyles, error : no frame object.", 2);
      return null;  
    }
		return $this->frame->getFrameStyles();
		*/
	}

	/**
	 * Gets HTML Scripts
	 *
	 * Scripts used within page
	 *
	 * @return array
	 */
	function getHTMLScripts() {
		$frame = &$this->getPageFrame();
		if (!$frame) {
			$frameId = $this->getPageFrameId();
			$this->debug('Page::getHTMLScripts, error : Frame(id='.$frameId.') not loaded', 3);
			return null;
		}
		$scriptsArray = $frame->getFrameScripts();
		return $scriptsArray;
		/*
    if(!is_object($this->frame)) {
		  $this->debug("Page.getHTMLStcripts, error : no frame object.", 2);
      return null;
    }
    return $this->frame->getFrameScripts();
		*/
	}
	
	/**
	 * Exports Page Into XML
	 *
	 * @param  string $mode
	 * @return string
	 */
	function toXML($langId, $mode=VIEW_MODE, $menuId=0) {
    $xml  = '';
		$xml .= '<page>'."\n";
		$xml .= $this->toInnerXML($langId, $mode, $menuId);
    $xml .= '</page>'."\n";

		return $xml;
	}

	function toInnerXML($langId, $mode=VIEW_MODE, $menuId=0) {
		$xml  = '';
		$xml .= '   <id>'.$this->getPageId().'</id>'."\n";
		//$xml .= '   <link><![CDATA[/index.php?pid='.$this->getPageId().']]></link>'."\n";
		$xml .= '   <link><![CDATA['.$this->getPageLinkOrAlias($langId).']]></link>'."\n";
		$xml .= '   <name><![CDATA['.$this->getPageName($langId).']]></name>'."\n";
		$xml .= '   <title><![CDATA['.$this->getPageTitle($langId).']]></title>'."\n";
		$xml .= '   <alias><![CDATA['.$this->getPageAlias($langId).']]></alias>'."\n";
		$xml .= '   <description><![CDATA['.$this->getPageDescription($langId).']]></description>'."\n";
		$xml .= '   <keywords><![CDATA['.$this->getPageKeywords($langId).']]></keywords>'."\n";
		if ($mode == EDIT_MODE){
			$xml .= '<page_controls>'."\n";
			$xml .= '  <type>'.$this->getTypeName().'</type>'."\n";
			$xml .= '  <id>'.$this->getPageId().'</id>'."\n";
			$xml .= '  <menu-id>'.$menuId.'</menu-id>'."\n";
			$xml .= '</page_controls>'."\n";
		}
		return $xml;
	} 
}
?>