diff --git a/application/controllers/ConfigurationController.php b/application/controllers/ConfigurationController.php index ae895695c..e48932558 100644 --- a/application/controllers/ConfigurationController.php +++ b/application/controllers/ConfigurationController.php @@ -28,7 +28,7 @@ use Icinga\Application\Benchmark; use Icinga\Authentication\Manager; use Icinga\Web\ActionController; -use Icinga\Web\Hook\Configuration\ConfigurationTab; +use Icinga\Web\Widget\Tabs; use Icinga\Web\Hook\Configuration\ConfigurationTabBuilder; /** @@ -48,7 +48,7 @@ class ConfigurationController extends ActionController public function indexAction() { $tabBuilder = new ConfigurationTabBuilder( - $this->widget('tabs') + new Tabs() ); $tabBuilder->build(); diff --git a/application/controllers/ModulesController.php b/application/controllers/ModulesController.php index 9fb215978..fc5fdfdaf 100644 --- a/application/controllers/ModulesController.php +++ b/application/controllers/ModulesController.php @@ -33,8 +33,7 @@ use Icinga\Web\ActionController; use Icinga\Application\Icinga; use Icinga\Web\Hook\Configuration\ConfigurationTabBuilder; -use Icinga\Application\Modules\Manager as ModuleManager; -use Zend_Controller_Action as ZfActionController; +use Icinga\Web\Widget\Tabs; /** * Handle module depending frontend actions @@ -60,9 +59,7 @@ class ModulesController extends ActionController */ public function indexAction() { - $tabBuilder = new ConfigurationTabBuilder( - $this->widget('tabs') - ); + $tabBuilder = new ConfigurationTabBuilder(new Tabs()); $tabBuilder->build(); diff --git a/application/views/scripts/configuration/index.phtml b/application/views/scripts/configuration/index.phtml index 75703c7c9..e6445410a 100644 --- a/application/views/scripts/configuration/index.phtml +++ b/application/views/scripts/configuration/index.phtml @@ -1,4 +1,5 @@ -tabs; ?> +tabs->render($this); ?> +

Configuration

diff --git a/application/views/scripts/modules/overview.phtml b/application/views/scripts/modules/overview.phtml index 29e80a94f..5978530e5 100644 --- a/application/views/scripts/modules/overview.phtml +++ b/application/views/scripts/modules/overview.phtml @@ -3,7 +3,7 @@ $this->modules->limit(10); $modules = $this->modules->paginate(); ?> -tabs; ?> +tabs->render($this); ?>

Installed Modules

paginationControl($modules, null, null, array( 'preserve' => $this->preserve diff --git a/library/Icinga/Web/ActionController.php b/library/Icinga/Web/ActionController.php index 7d989373e..e7c39bf42 100755 --- a/library/Icinga/Web/ActionController.php +++ b/library/Icinga/Web/ActionController.php @@ -153,19 +153,6 @@ class ActionController extends ZfController return t($string); } - /** - * Helper function creating a new widget - * - * @param string $name The widget name - * @param string $properties Optional widget properties - * - * @return Widget\AbstractWidget - */ - public function widget($name, $properties = array()) - { - return Widget::create($name, $properties); - } - /** * Whether the current user has the given permission * diff --git a/library/Icinga/Web/Widget.php b/library/Icinga/Web/Widget.php deleted file mode 100644 index 0947b1643..000000000 --- a/library/Icinga/Web/Widget.php +++ /dev/null @@ -1,73 +0,0 @@ - - * @author Icinga Development Team - */ -// {{{ICINGA_LICENSE_HEADER}}} - -namespace Icinga\Web; - -use Icinga\Exception\ProgrammingError; - -/** - * Web widgets make things easier for you! - * - * This class provides nothing but a static factory method for widget creation. - * Usually it will not be used directly as there are widget()-helpers available - * in your action controllers and view scripts. - * - * Usage example: - * - * $tabs = Widget::create('tabs'); - * - * - * @copyright Copyright (c) 2013 Icinga-Web Team - * @author Icinga-Web Team - * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License - */ -class Widget -{ - - /** - * Create a new widget - * - * @param string $name Widget name - * @param array $options Widget constructor options - * - * @return Icinga\Web\Widget\AbstractWidget - */ - public static function create($name, $options = array(), $module_name = null) - { - $class = 'Icinga\\Web\\Widget\\' . ucfirst($name); - - if (! class_exists($class)) { - throw new ProgrammingError( - sprintf( - 'There is no such widget: %s', - $name - ) - ); - } - - $widget = new $class($options, $module_name); - return $widget; - } -} diff --git a/library/Icinga/Web/Widget/AbstractWidget.php b/library/Icinga/Web/Widget/AbstractWidget.php deleted file mode 100644 index 214032413..000000000 --- a/library/Icinga/Web/Widget/AbstractWidget.php +++ /dev/null @@ -1,167 +0,0 @@ - - * @author Icinga-Web Team - * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License - */ -abstract class AbstractWidget -{ - /** - * If you are going to access the current view with the view() function, - * it's instance is stored here for performance reasons. - * - * @var Zend_View_Abstract - */ - protected static $view; - - protected $module_name; - - /** - * Fill $properties with default values for all your valid widget properties - * - * @var array - */ - protected $properties = array(); - - /** - * You MUST extend this function. This is where all your HTML voodoo happens - * - * @return string - */ - abstract public function renderAsHtml(); - - /** - * You are not allowed to override the constructor, but you can put - * initialization stuff in your init() function - * - * @return void - */ - protected function init() - { - } - - /** - * We are not allowing you to override the constructor unless someone - * presents a very good reason for doing so - * - * @param array $properties An optional properties array - */ - final public function __construct($properties = array(), $module_name = null) - { - if ($module_name !== null) { - $this->module_name = $module_name; - } - foreach ($properties as $key => $val) { - $this->$key = $val; - } - $this->init(); - } - - /** - * Getter for widget properties - * - * @param string $key The option you're interested in - * - * @throws ProgrammingError for unknown property name - * - * @return mixed - */ - public function __get($key) - { - if (array_key_exists($key, $this->properties)) { - return $this->properties[$key]; - } - - throw new ProgrammingError( - sprintf( - 'Trying to get invalid "%s" property for %s', - $key, - get_class($this) - ) - ); - } - - /** - * Setter for widget properties - * - * @param string $key The option you want to set - * @param string $val The new value going to be assigned to this option - * - * @throws ProgrammingError for unknown property name - * - * @return mixed - */ - public function __set($key, $val) - { - if (array_key_exists($key, $this->properties)) { - $this->properties[$key] = $val; - return; - } - - throw new ProgrammingError( - sprintf( - 'Trying to set invalid "%s" property in %s. Allowed are: %s', - $key, - get_class($this), - empty($this->properties) - ? 'none' - : implode(', ', array_keys($this->properties)) - ) - ); - } - - /** - * Access the current view - * - * Will instantiate a new one if none exists - * // TODO: App->getView - * - * @return Zend_View_Abstract - */ - protected function view() - { - if (self::$view === null) { - - $renderer = ZfActionHelper::getStaticHelper( - 'viewRenderer' - ); - - if (null === $renderer->view) { - $renderer->initView(); - } - - self::$view = $renderer->view; - } - - return self::$view; - } - - /** - * Cast this widget to a string. Will call your renderAsHtml() function - * - * @return string - */ - public function __toString() - { - return $this->renderAsHtml(); - } -} diff --git a/library/Icinga/Web/Widget/Dashboard.php b/library/Icinga/Web/Widget/Dashboard.php index 91da715e5..15d2fba49 100644 --- a/library/Icinga/Web/Widget/Dashboard.php +++ b/library/Icinga/Web/Widget/Dashboard.php @@ -4,13 +4,16 @@ namespace Icinga\Web\Widget; use Icinga\Application\Icinga; use Icinga\Config\Config; -use Icinga\Web\Widget; +use Icinga\Web\Widget\Widget; use Icinga\Web\Widget\Dashboard\Pane; use Icinga\Web\Url; use Zend_Config as ZfConfig; -class Dashboard extends AbstractWidget +class Dashboard implements Widget { + /** + * @var Config + */ protected $config; protected $configfile; protected $panes = array(); @@ -24,7 +27,7 @@ class Dashboard extends AbstractWidget protected function init() { if ($this->url === null) { - $this->url = Url::fromRequest()->without($this->tabParam); + $this->url = Url::fromRequest()->getUrlWithout($this->tabParam); } } @@ -36,7 +39,7 @@ class Dashboard extends AbstractWidget public function tabs() { if ($this->tabs === null) { - $this->tabs = Widget::create('tabs'); + $this->tabs = new Tabs(); foreach ($this->panes as $key => $pane) { $this->tabs->add($key, array( 'title' => $pane->getTitle(), @@ -65,8 +68,7 @@ class Dashboard extends AbstractWidget public function readConfig(ZfConfig $config) { - $this->configfile = Config::getInstance()->getConfigDir() - . '/dashboard.ini'; + $this->configfile = Icinga::app('dashboard')->getApplicationDir("dashboard"); $this->config = $config; $this->panes = array(); $this->loadConfigPanes(); @@ -128,7 +130,7 @@ class Dashboard extends AbstractWidget return $this->panes[$name]; } - public function renderAsHtml() + public function render(\Zend_View_Abstract $view) { if (empty($this->panes)) { return ''; @@ -164,7 +166,7 @@ class Dashboard extends AbstractWidget protected function loadConfigPanes() { - $items = $this->config->dashboard->toArray(); + $items = $this->config->keys(); $app = Icinga::app(); foreach ($items as $key => $item) { if (false === strstr($key, '.')) { diff --git a/library/Icinga/Web/Widget/Form.php b/library/Icinga/Web/Widget/Form.php index 50525b21c..9ee4bb827 100644 --- a/library/Icinga/Web/Widget/Form.php +++ b/library/Icinga/Web/Widget/Form.php @@ -1,21 +1,18 @@ * @author Icinga-Web Team * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License */ -class Form extends AbstractWidget +class Form { protected $form; protected $properties = array( @@ -74,7 +71,7 @@ class Form extends AbstractWidget $this->form = new $class($this->options); } - public function renderAsHtml() + public function render() { return (string) $this->form; } diff --git a/library/Icinga/Web/Widget/Tab.php b/library/Icinga/Web/Widget/Tab.php index a8ea47f68..501e358de 100644 --- a/library/Icinga/Web/Widget/Tab.php +++ b/library/Icinga/Web/Widget/Tab.php @@ -1,8 +1,5 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License */ -class Tab extends AbstractWidget +class Tab implements Widget { /** * Whether this tab is currently active * * @var bool */ - protected $active = false; + private $active = false; /** * Default values for widget properties * * @var array */ - protected $properties = array( - 'name' => null, - 'title' => '', - 'url' => null, - 'urlParams' => array(), - 'icon' => null, - ); + private $name = null; + + private $title = ''; + private $url = null; + private $urlParams = array(); + private $icon = null; + + + /** + * @param mixed $icon + */ + public function setIcon($icon) + { + $this->icon = $icon; + } + + /** + * @return mixed + */ + public function getIcon() + { + return $this->icon; + } + + /** + * @param mixed $name + */ + public function setName($name) + { + $this->name = $name; + } + + /** + * @return mixed + */ + public function getName() + { + return $this->name; + } + + /** + * @param mixed $title + */ + public function setTitle($title) + { + $this->title = $title; + } + + /** + * @return mixed + */ + public function getTitle() + { + return $this->title; + } + + /** + * @param mixed $url + */ + public function setUrl($url) + { + $this->url = $url; + } + + /** + * @return mixed + */ + public function getUrl() + { + return $this->url; + } + + public function __construct(array $properties = array()) + { + foreach ($properties as $name=>$value) { + $setter = 'set'.ucfirst($name); + if (method_exists($this, $setter)) { + $this->$setter($value); + } + } + + } /** * Health check at initialization time @@ -56,9 +128,7 @@ class Tab extends AbstractWidget protected function init() { if ($this->name === null) { - throw new ProgrammingError( - 'Cannot create a nameless tab' - ); + throw new ProgrammingError('Cannot create a nameless tab'); } } @@ -93,16 +163,15 @@ class Tab extends AbstractWidget * * @return string */ - public function renderAsHtml() + public function render(\Zend_View_Abstract $view) { - $view = $this->view(); $class = $this->isActive() ? ' class="active"' : ''; $caption = $this->title; if ($this->icon !== null) { $caption = $view->img($this->icon, array( - 'width' => 16, - 'height' => 16 - )) . ' ' . $caption; + 'width' => 16, + 'height' => 16 + )) . ' ' . $caption; } if ($this->url !== null) { $tab = $view->qlink( @@ -114,6 +183,8 @@ class Tab extends AbstractWidget } else { $tab = $caption; } + return "
  • $tab
  • \n"; } + } diff --git a/library/Icinga/Web/Widget/Tabs.php b/library/Icinga/Web/Widget/Tabs.php index 8e9c3483d..af3fa1f21 100644 --- a/library/Icinga/Web/Widget/Tabs.php +++ b/library/Icinga/Web/Widget/Tabs.php @@ -1,12 +1,10 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License */ -class Tabs extends AbstractWidget implements Countable +class Tabs implements Countable, Widget { /** * This is where single tabs added to this container will be stored * * @var array */ - protected $tabs = array(); + private $tabs = array(); /** * The name of the currently activated tab * * @var string */ - protected $active; + private $active; /** * Class name(s) going to be assigned to the <ul> element * * @var string */ - protected $tab_class = 'nav-tabs'; + private $tab_class = 'nav-tabs'; - protected $specialActions = false; + private $specialActions = false; /** * Activate the tab with the given name @@ -188,48 +186,46 @@ class Tabs extends AbstractWidget implements Countable * * @return string */ - public function renderAsHtml() + public function render(\Zend_View_Abstract $view) { - $view = $this->view(); - if (empty($this->tabs)) { return ''; } $html = '