<?php

class LayoutManager {
	static $instance;
	protected function __construct()
	{
	}
	/**
	 *
	 * @return LayoutManager
	 */
	static function instance()
	{
		if (empty(self::$instance)) {
			self::$instance = new LayoutManager();
		}
		return self::$instance;
	}

	protected $layouts = array();

	public function register($identifier, $name, $description, $isPublic = true, $catalyzIdentifier = null)
	{

		if(null == $catalyzIdentifier){
			$catalyzIdentifier = $identifier;
		}

		$this->layouts[$identifier] = array(
			'id' => $identifier,
			'name' => $name,
			'public' => $isPublic,
			'description' => $description,
			'catalyz' => $catalyzIdentifier
			);
	}
	public function unregister($identifier)
	{
		unset($this->layouts[$identifier]);
	}

	public function getAll($withPrivate = false)
	{
		$result = $this->layouts;
		if(!$withPrivate){
			foreach($result as $id => $properties) {
				if (!$properties['public']) {
					unset($result[$id]);
				}
			}
		}
		return $result;
	}

	public function getSupportedLayouts($page)	{
		if($page instanceof ICatalyzLayoutProvider){
			$result = $page->getAvailableLayouts();
		}else{
			$result = array_keys($this->getAll());
		}
		return $result;
	}

	public function getLayout($identifier){
		return isset($this->layouts[$identifier])?$this->layouts[$identifier]:null;
	}
	public function getCatalyzTemplate($template){
		return explode('/', $this->layouts[implode('/', $template)]['catalyz']);
	}
}

LayoutManager::instance()->register('catalyz/default', 'Default', 'Default layout', true);

?>