<?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 ContainersList
 */

include_once('classes/core/CoreObject.class.php');
include_once('classes/core/DBManipulator.class.php');
include_once('classes/core/ACL/ACL.class.php');
include_once('classes/core/ACL/ACLManager.class.php');
include_once('classes/core/containers/ContainersManager.class.php');
include_once('classes/core/containers/Container.class.php');
include_once('classes/utils/SortedList.class.php');
include_once('classes/utils/SortedListIterator.class.php');
include_once('interface/egManager.class.php');
include_once('interface/egContainer.class.php');
include_once('interface/egContextMenu.class.php');


/**
 * ContainersList Class
 *
 * A ContainersList holds infos about sort order, number to be shown and
 * exhaustive list of contentainers that are to be displayed in this list.
 *
 * @access     public
 * @author	   Cedric Francoys <cedric.francoys@easy-cms.org>

 * @version    ContainersList.class,v 0.1
 * @package	   Core
 * @subpackage ContainersList
 */
class ContainersList extends CoreObject {
	/**
	 * ContainersList Id
	 *
	 * @var integer
	 */
  var $id;


	/**
	 * ContainersList's "SortingType" property
	 *
	 * @var integer
	 */
  var $sortingType;


	/**
	 * ContainersList's "SortField" property
	 *
	 * @var string
	 */
  var $sortField;


	/**
	 * ContainersList's "SortOrder" property
	 *
	 * @var integer
	 */
  var $sortOrder;


	/**
	 * ContainersList's "CountShown" property
	 *
	 * @var integer
	 */
  var $countShown;


	/**
	 * ContainersList's Clone Id
	 *
	 * @var integer
	 */
	var $cloneId;


	/**
	 * ContainersList's ACL Id
	 *
	 * @var integer
	 */
	var $aclId;


	/**
	 * ContainersList's "Deleted" Property
	 *
	 * @var integer
	 */
  var $deleted;


	/**
	 * ContainersList's "Published" Property
	 *
	 * @var integer
	 */
  var $published;


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


	/**
	 * @todo deprecated??
	 */
  var $objectsList;


	/**
	 * @var array
	 */
  var	$containersTable;


	/**
	 * Constructor
	 *
	 * @param integer $id ContainersList Id
	 * @param integer $sortingType ContainersList's "SortingType" property
	 * @param string  $sortField ContainersList's "SortField" property
	 * @param integer $sortOrder ContainersList's "SortOrder" property
	 * @param integer $countShown ContainersList's "CountShown" property
	 * @param integer $cloneId ContainersList's Clone Id
	 * @param integer $aclId ContainersList's ACL Id
	 * @param integer $deleted ContainersList "Deleted" property
	 */
	function ContainersList($id=0, $sortingType=SORTING_BY_LIST, $sortField='id', $sortOrder=SORT_ASCENDING, $countShown=0, $cloneId=0, $aclId=0, $deleted=0) {
    $this->debug('ContainersList::ContainersList, status : function invoked', 10, DBG_OPEN);
		$this->setContainersListId($id);
		$this->setContainersListSortingType($sortingType);
		$this->setContainersListSortField($sortField);
		$this->setContainersListSortOrder($sortOrder);
		$this->setContainersListCountShown($countShown);
		$this->setContainersListCloneId($cloneId);
		$this->setContainersListAclId($aclId);
		$this->setContainersListDeleted($deleted);
		$this->containersTable = array();
    $this->debug('ContainersList::ContainersList, status : function completed', 10, DBG_CLOSE);
	}

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


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


	/**
	 * Gets ContainersList's "SortingType" property
	 *
	 * @return integer
	 */
	function getContainersListSortingType(){
		return $this->sortingType;
	}


	/**
	 * Sets ContainersList's "SortingType" property
	 *
	 * @param integer $sortingType
	 */
	function setContainersListSortingType($sortingType){
		$this->sortingType = $sortingType;
	}


	/**
	 * Gets ContainersList's "SortField" property
	 *
	 * @return string
	 */
	function getContainersListSortField(){
		return $this->sortField;
	}


	/**
	 * Sets ContainersList's "SortField" property
	 *
	 * @param string $sortField
	 */
	function setContainersListSortField($sortField){

		$this->sortField = $sortField;
	}


	/**
	 * Gets ContainersList's "SortOrder" property
	 *
	 * @return integer
	 */
	function getContainersListSortOrder(){
		return $this->sortOrder;
	}


	/**
	 * Sets ContainersList's "SortOrder" property

	 *
	 * @param integer $sortOrder
	 */
	function setContainersListSortOrder($sortOrder){
		$this->sortOrder = $sortOrder;
	}


	/**
	 * Gets ContainersList's "CountShown" property
	 *
	 * @return integer
	 */
	function getContainersListCountShown(){
		return $this->countShown;
	}


	/**
	 * Sets ContainersList's "CountShown" property
	 *
	 * @param integer $countShown
	 */
	function setContainersListCountShown($countShown){
		$this->countShown = $countShown;
	}


	/**
	 * Gets ContainersList Clone Id
	 *
	 * @return integer
	 */
	function getContainersListCloneId(){
		return $this->cloneId;
	}


	/**
	 * Sets ContainersList Clone Id
	 *
	 * @param integer $cloneId
	 */
	function setContainersListCloneId($cloneId){
		$this->cloneId = $cloneId;
	}


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

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


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


	/**
	 * Gets ContainersList's "Deleted" property
	 *
	 * @return integer
	 */
	function getContainersListDeleted(){
		return $this->deleted;
	}


	/**
	 * Sets ContainersList's "Deleted" property
	 *
	 * @param integer $deleted
	 */
	function setContainersListDeleted($deleted){
		$this->deleted = $deleted;
	}


	/**
	 * Gets ContainersList's "Published" property
	 *
	 * @return integer
	 */
	function getContainersListPublished(){
		return $this->published;
	}


	/**
	 * Sets ContainersList's "Published" property
	 *
	 * @param integer $deleted
	 */
	function setContainersListPublished($published){
		$this->published = $published;
	}


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




	/**
	 * Gets ContainersList ACL
	 *
	 * @return object
	 */
	function &getContainersListAcl(){
		if (!isset($this->acl))
			return null;
		return $this->acl;
	}


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


	/**
	 * Gets ContainersTable
	 *
	 * @return array
	 */
	function &getContainersTable(){
		return $this->containersTable;
	}


	function getContainersListValuesTable() {
    $this->debug('ContainersList::getContainersListValuesTable, status : function invoked', 10, DBG_OPEN);
    $valuesTable = array();

		$valuesTable['id'] = $this->id ;
		$valuesTable['clone_id'] = $this->cloneId ;
		$valuesTable['sorting_type'] = 	$this->sortingType;
		$valuesTable['sort_field'] = 	$this->sortField;
		$valuesTable['sort_order'] = $this->sortOrder;
		$valuesTable['count_shown'] = $this->countShown;
		$valuesTable['acl_id'] = $this->aclId;
		$valuesTable['deleted'] = $this->deleted;
		$valuesTable['published'] = $this->published;

    $this->debug('ContainersList::getContainersListValuesTable, status : function completed', 10, DBG_CLOSE);
    return $valuesTable;
	}


	/**
	 * Adds Container Into ContainersList
	 *
	 * @param  object $container
	 * @return boolean
	 */
	function addContainer(&$container){
    $this->debug('ContainersList::addContainer, status : function invoked', 10, DBG_OPEN);
    $result = true;
		if(!is_object($container)){
			$result = false;
		}
		else {
  		$id = $container->getContainerId();
      $this->containersTable[$id] = &$container;
    }
    $this->debug('ContainersList::addContainer, status : function completed', 10, DBG_CLOSE);
		return $result;
	}


	/**
	 * Sets ContainersTable
	 *
	 * @param array $table
	 */
	function setContainersTable(&$table){
		$this->containersTable = &$table;
	}


	/**
	 * Number of Containers In ContainersList
	 *
	 * @return integer <0 if error
	 */
	function getContainersCount(){
		return count($this->containersTable);
	}


	function setContainersListValues(&$array) {
    $this->debug('ContainersList::setContainersListValues, status : function invoked', 10, DBG_OPEN);
		if(isset($array["id"])) {
			$this->id = &$array["id"];
		}
		if(isset($array["clone_id"])) {
			$this->cloneId = &$array["clone_id"];
		}
		if(isset($array["sorting_type"])) {
			$this->sortingType = &$array["sorting_type"];
		}
		if(isset($array["sort_field"])) {
			$this->sortField = &$array["sort_field"];
		}
		if(isset($array["sort_order"])) {
			$this->sortOrder = &$array["sort_order"];
		}
		if(isset($array["count_shown"])) {
			$this->countShown = &$array["count_shown"];
		}
		if(isset($array["published"])) {
			$this->published = &$array["published"];
		}
    $this->debug('ContainersList::setContainersListValues, status : function completed', 10, DBG_CLOSE);
	}


	function setValues(&$array) {
		$this->setContainersListValues($array);
	}


  function sortContainers($langId) {
    $this->debug('ContainersList::sortContainers, status : function invoked', 10, DBG_OPEN);
    if (is_array($this->containersTable)) {
			$count = $this->getContainersCount();
			$sortField = $this->getContainersListSortField();
			$sortOrder = $this->getContainersListSortOrder();
			$this->objectsList = array();
			foreach($this->containersTable as $key => $container) {
        if(($value = $container->getContainerValue($sortField, $langId)) != null) {
  				$this->objectsList[$key] = $value;
        }
			}
			if($sortOrder == SORT_ASCENDING) {
				asort($this->objectsList);
			}
			else {
				arsort($this->objectsList);
			}
    }
    $this->debug('ContainersList::sortContainers, status : function completed', 10, DBG_CLOSE);
  }


	/*
	* @deprecated
	*
	*/
	function loadContainers(&$containersTable) {
    $this->debug('ContainersList::loadContainers, status : function invoked', 10, DBG_OPEN);
		$this->objectsList = new SortedList($this->sortField, $this->sortOrder);
		for($i = 0; $i < count($containersTable); ++$i) {
			$this->objectsList->push($containersTable[$i]);
		}
    $this->debug('ContainersList::loadContainers, status : function completed', 10, DBG_CLOSE);
	}

	function &getControls(&$user, $mode=VIEW_MODE) {
    $this->debug('ContainersList::getControls, status : function invoked', 10, DBG_OPEN);
		$html = '';

		if ($this->acl->userHasEditRight($user) && $mode == EDIT_MODE) {
			$containersListId = $this->getContainersListId();
			if(!isset($GLOBALS['contextMenuContainersList'])) {
	      $menu = &new egContextMenu("menu_containersList", 0, 0, 150, 350);

	      $menu->addListItem(new egMenuItem("mnu_cl_1", LANG_CONTAINERSLIST_MENU_ADD, "/interface/icons/add.png", "containersListMenuAddOption();"));
		    $menu->addSeparator();
				if(ENABLE_GALLERIES == 1) {
	  	    $menu->addListItem(new egMenuItem("mnu_cl_2", LANG_CONTAINERSLIST_MENU_GALLERY_ADD, "/interface/icons/add.png", "containersListMenuAddGalleryOption();"));
			    $menu->addSeparator();
				}
		    $menu->addListItem(new egMenuItem("mnu_cl_3", LANG_CONTAINERSLIST_MENU_EDIT, "/interface/icons/edit.png", "containersListMenuEditOption();"));
	      $main = &new egContainer("eg_ctn_menu_containersList", 0, 0, 0, 0);

	      $main->addObject($menu);

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

				$GLOBALS['contextMenuContainersList'] = &$menu;
			}
			$html .= "<a class=\"easy-CMS_UI_propertiesIcon\" href=\"javascript:selectContainersList('$containersListId');egShowContextMenu(contextMenu_menu_containersList);\"><img src=\"/interface/icons/properties.png\" /></a>";
    }
    $this->debug('ContainersList::getControls, status : function completed', 10, DBG_CLOSE);
		return $html;
	}


  function &toHTML(&$user, $langId, $mode = VIEW_MODE, $drawBorder = true)	{
    $this->debug('ContainersList::toHTML, status : function invoked', 10, DBG_OPEN);
		$html = '';
		
		if($user == null) {
			$this->debug("ContainersList::toHTML, error : user not defined.", 2);
		}
    else {
  		if ($this->acl->userHasViewRight($user) && ($mode == EDIT_MODE || $this->isPublished())) {
  			$i = 0;
  			$max = &$this->getContainersListCountShown();
  			$count = $this->getContainersCount();

  			// sorting by list
  			if ($this->getContainersListSortingType() == SORTING_BY_LIST) {
  				$containersTable = &$this->getContainersTable();
  				for (; $i < $count && ($max == 0 || $i < $max); ++$i) {
  					$container = &$this->containersTable[$i];
  					$html .= $container->toHTML($user, $langId, $mode, true);
  				}
  			}
  			// sorting by field
  			else {
  				$this->sortContainers($langId);
  				$sortField = $this->getContainersListSortField();

  				if (is_array($this->objectsList)) {
  					reset($this->objectsList);

  					for ($j = 0; $j < $i; ++$j) {
  						next($this->objectsList);
  					}

  					for (; $i < $count && ($max == 0 || $i < $max); ++$i) {
  						$index = key($this->objectsList);
  						next($this->objectsList);
  						$container = &$this->containersTable[$index];
  						$html .= $container->toHTML($user, $langId, $mode, true);
  					}
  				}
  			}

  			if ($this->acl->userHasEditRight($user) && $mode == EDIT_MODE && $drawBorder) {
  				$head = "";
  				$tail = "";

  				$head .= "<div class=\"easy-CMS_UI_outer_frame\">\n";
  				$head .= "<div class=\"easy-CMS_UI_leftBorder\">\n";
  				$head .= "<div class=\"easy-CMS_UI_rightBorder\">\n";
  				$head .= "	<div class=\"easy-CMS_UI_header\" style=\"background-color: #DADADA;\">\n";
  				$head .= "  <div class=\"easy-CMS_UI_headerInner\">\n";
  				$head .= "		<div class=\"easy-CMS_UI_left\"><div class=\"easy-CMS_UI_title\">list</div></div>\n";
  				$head .= "		<div class=\"easy-CMS_UI_right\">\n";
  				$head .= "			<div class=\"easy-CMS_UI_properties\">\n";
  				$head .= $this->getControls($user, EDIT_MODE);
  				$head .= "			</div>\n";
  				$head .= "		</div>\n";
  				$head .= "  </div>\n";
  				$head .= "	</div>\n";
  				$head .= "	<div class=\"easy-CMS_UI_inner_frame\">\n";
  				$head .= "	<table style=\"width: 100%; margin: 0; border: 0; padding: 0;\"><tr><td width=\"3\"></td><td>\n";

  				$tail .= "	</td><td width=\"3\"></td></tr></table>\n";
  				$tail .= "	</div>\n";
  				$tail .= "</div>\n";
  				$tail .= "</div>\n";
  				$tail .= "</div>\n";


  				$html = $head.$html.$tail;
  			}
  		}
    }
    $this->debug('ContainersList::toHTML, status : function completed', 10, DBG_CLOSE);
		return $html;
	}

}
?>