From 754f854dd03c381a40d79210a8a3e9ac34caddd0 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Wed, 14 May 2014 12:42:22 +0200 Subject: [PATCH] Multistep pages should know about their multistep form refs #6136 --- library/Icinga/Web/Wizard/Page.php | 19 ++++++++++++++++++ library/Icinga/Web/Wizard/Wizard.php | 30 ++++++++++++++++++++++------ 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/library/Icinga/Web/Wizard/Page.php b/library/Icinga/Web/Wizard/Page.php index 53383fd1a..fdc0fd171 100644 --- a/library/Icinga/Web/Wizard/Page.php +++ b/library/Icinga/Web/Wizard/Page.php @@ -15,6 +15,13 @@ class Page extends Form */ protected $tokenDisabled = true; + /** + * The wizard this page is part of + * + * @var Wizard + */ + protected $wizard; + /** * The title of this wizard page * @@ -22,6 +29,18 @@ class Page extends Form */ protected $title = ''; + /** + * Create a new wizard page + * + * @param Wizard $wizard The wizard this page is part of + * @param mixed $options Zend_Form options + */ + public function __construct(Wizard $wizard = null, $options = null) + { + parent::__construct($options); + $this->wizard = $wizard; + } + /** * Overwrite this to initialize this wizard page */ diff --git a/library/Icinga/Web/Wizard/Wizard.php b/library/Icinga/Web/Wizard/Wizard.php index 6be831ae8..64e9e5f24 100644 --- a/library/Icinga/Web/Wizard/Wizard.php +++ b/library/Icinga/Web/Wizard/Wizard.php @@ -34,6 +34,27 @@ class Wizard extends Page return $this->pages; } + /** + * Return a page by its name or null if it's not found + * + * @param string $pageName The name of the page + * + * @return Page|null + */ + public function getPage($pageName) + { + $candidates = array_filter( + $this->pages, // Cannot use getPages() here because I might get called as part of Page::isRequired() + function ($page) use ($pageName) { return $page->getName() === $pageName; } + ); + + if (!empty($candidates)) { + return array_shift($candidates); + } elseif ($this->wizard !== null) { + return $this->wizard->getPage($pageName); + } + } + /** * Add a new page to this wizard * @@ -68,17 +89,14 @@ class Wizard extends Page { foreach ($pages as $title => $pageClassOrArray) { if (is_array($pageClassOrArray)) { - $wizard = new static(); + $wizard = new static($this); $wizard->setTitle($title); $this->addPage($wizard); $wizard->addPages($pageClassOrArray); - } elseif (is_string($pageClassOrArray)) { - $page = new $pageClassOrArray(); + } else { + $page = new $pageClassOrArray($this); $page->setTitle($title); $this->addPage($page); - } else { - $pageClassOrArray->setTitle($title); - $this->addPage($pageClassOrArray); } } }