Skip to content

Instantly share code, notes, and snippets.

@Aurielle
Created January 12, 2011 15:57
Show Gist options
  • Save Aurielle/776341 to your computer and use it in GitHub Desktop.
Save Aurielle/776341 to your computer and use it in GitHub Desktop.
WidgetContainer - spravuje widgety
<?php
/**
* PHP-in' CMS Framework
*
* Copyright (c) 2010 Vaclav Vrbka (http://www.php-info.cz)
*
* This source file is subject to the "PHP-in' CMS Framework licence".
* For more information please see http://www.phpin.eu
*/
namespace PHPin\Application;
/**
* Widget container; parent of all site widgets
* Provides same functionality as Control and some extra :)
*
* @author Vaclav Vrbka
*/
class WidgetContainer extends Control
{
/** @var array */
protected $widgets = array();
/**
* Constructor
*/
public function __construct(\Nette\IComponentContainer $parent = NULL, $name = NULL)
{
parent::__construct($parent, $name);
\PHPin\Tools::getHooker()->onWidgetContainerCreated($this);
}
/**
* Adds the specified component to the IComponentContainer.
* @param IComponent
* @param string
* @param string
* @return void
* @throws \InvalidStateException
*/
public function addComponent(\Nette\IComponent $component, $name, $insertBefore = NULL, $priority = 0)
{
if(!is_string($name) || strpos($name, ':') === FALSE)
throw new \InvalidArgumentException("Components without given category are not accepted as widgets ('$name' given).");
$name = str_replace(':', '_', strtolower($name));
$cleanName = ltrim(strrchr($name, '_'), '_');
$category = substr($name, 0, -strlen(strrchr($name, '_')));
self::$widgets[$category][$priority][] = $cleanName;
return parent::addComponent($component, $name, $insertBefore);
}
/**
* Renders all widgets by parameter
* @param string $name
*/
public function render($name = NULL)
{
if(empty($name))
{
trigger_error("Missing category in widget rendering!", E_USER_WARNING);
return;
}
$name = str_replace(':', '_', strtolower($name));
if(!isset(self::$widgets[$name]) || empty(self::$widgets[$name]))
return; // No widgets are registed in this category
krsort(self::$widgets[$name], SORT_NUMERIC);
foreach(self::$widgets[$name] as $priority => $widgets)
{
foreach($widgets as $widget)
{
// Render each widget in a group, following priority rules
$component = $this->getComponent("{$name}_{$widget}");
$renderMode = ($component instanceof Widget) ? ucfirst($component->renderMode) : NULL;
$renderParams = ($component instanceof Widget) ? (array) $component->renderParams : array();
call_user_func_array(array($component, 'render' . $renderMode), $renderParams);
}
}
}
/**
* Calling behavior
* @param string $name
* @param array $args
* @return mixed
* @throws MemberAccessException
*/
public function __call($name, $args)
{
$matches = \Nette\String::match($name, '~^render(.+)~i');
if($matches)
{
return call_user_func_array(array($this, 'render'), array($matches[1]));
}
else
parent::__call($name, $args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment