QuickBaseForm: take over parts of QuickForm

This commit is contained in:
Thomas Gelf 2016-03-24 01:59:50 +01:00
parent 561b786aff
commit 5fc16f393d
2 changed files with 121 additions and 106 deletions

View File

@ -0,0 +1,120 @@
<?php
namespace Icinga\Module\Director\Web\Form;
use Zend_Form;
abstract class QuickBaseForm extends Zend_Form
{
/**
* The Icinga module this form belongs to. Usually only set if the
* form is initialized through the FormLoader
*/
protected $icingaModule;
protected $icingaModuleName;
public function __construct($options = null)
{
parent::__construct($this->handleOptions($options));
if ($this->icingaModule) {
$basedir = sprintf(
'%s/%s/Web/Form',
$this->icingaModule->getLibDir(),
ucfirst($this->icingaModuleName)
);
$this->addPrefixPaths(array(
array(
'prefix' => __NAMESPACE__ . '\\Element\\',
'path' => $basedir . '/Element',
'type' => static::ELEMENT
)
));
}
}
public function addHidden($name, $value = null)
{
$this->addElement('hidden', $name);
$el = $this->getElement($name);
$el->setDecorators(array('ViewHelper'));
if ($value !== null) {
$this->setDefault($name, $value);
$el->setValue($value);
}
return $this;
}
// TODO: Should be an element
public function addHtmlHint($html, $options = array())
{
return $this->addHtml('<div class="hint">' . $html . '</div>', $options);
}
public function addHtml($html, $options = array())
{
if (array_key_exists('name', $options)) {
$name = $options['name'];
unset($options['name']);
} else {
$name = '_HINT' . ++$this->hintCount;
}
$this->addElement('simpleNote', $name, $options);
$this->getElement($name)
->setValue($html)
->setIgnore(true)
->setDecorators(array('ViewHelper'));
return $this;
}
public function optionalEnum($enum)
{
return array(
null => $this->translate('- please choose -')
) + $enum;
}
protected function handleOptions($options = null)
{
if ($options === null) {
return $options;
}
if (array_key_exists('icingaModule', $options)) {
$this->icingaModule = $options['icingaModule'];
$this->icingaModuleName = $this->icingaModule->getName();
unset($options['icingaModule']);
}
return $options;
}
public function setIcingaModule(Module $module)
{
$this->icingaModule = $module;
return $this;
}
protected function loadForm($name, Module $module = null)
{
if ($module === null) {
$module = $this->icingaModule;
}
return FormLoader::load($name, $module);
}
public function translate($string)
{
if ($this->icingaModuleName === null) {
return t($string);
} else {
return mt($this->icingaModuleName, $string);
}
}
}

View File

@ -9,12 +9,11 @@ use Icinga\Web\Notification;
use Icinga\Web\Request;
use Icinga\Web\Url;
use Exception;
use Zend_Form;
/**
* QuickForm wants to be a base class for simple forms
*/
abstract class QuickForm extends Zend_Form
abstract class QuickForm extends QuickBaseForm
{
const ID = '__FORM_NAME';
@ -60,14 +59,6 @@ abstract class QuickForm extends Zend_Form
*/
protected $didSetup = false;
/**
* The Icinga module this form belongs to. Usually only set if the
* form is initialized through the FormLoader
*/
protected $icingaModule;
protected $icingaModuleName;
protected $hintCount = 0;
protected $isApiRequest = false;
@ -76,20 +67,6 @@ abstract class QuickForm extends Zend_Form
{
parent::__construct($this->handleOptions($options));
$basedir = Icinga::app()
->getModuleManager()
->getModule('director')
->getLibDir()
. '/Director/Web/Form';
$this->addPrefixPaths(array(
array(
'prefix' => __NAMESPACE__ . '\\Element\\',
'path' => $basedir . '/Element',
'type' => static::ELEMENT
)
));
$this->setMethod('post');
$this->setAction(Url::fromRequest());
$this->createIdElement();
@ -104,21 +81,6 @@ abstract class QuickForm extends Zend_Form
);
}
protected function handleOptions($options = null)
{
if ($options === null) {
return $options;
}
if (array_key_exists('icingaModule', $options)) {
$this->icingaModule = $options['icingaModule'];
$this->icingaModuleName = $this->icingaModule->getName();
unset($options['icingaModule']);
}
return $options;
}
protected function addSubmitButtonIfSet()
{
if (false !== ($label = $this->getSubmitLabel())) {
@ -212,15 +174,6 @@ abstract class QuickForm extends Zend_Form
return $this->isApiRequest;
}
protected function loadForm($name, Module $module = null)
{
if ($module === null) {
$module = $this->icingaModule;
}
return FormLoader::load($name, $module);
}
public function regenerateCsrfToken()
{
if (! $element = $this->getElement(self::CSRF)) {
@ -238,49 +191,6 @@ abstract class QuickForm extends Zend_Form
return $this;
}
public function addHidden($name, $value = null)
{
$this->addElement('hidden', $name);
$el = $this->getElement($name);
$el->setDecorators(array('ViewHelper'));
if ($value !== null) {
$this->setDefault($name, $value);
$el->setValue($value);
}
return $this;
}
public function addHtmlHint($html, $options = array())
{
return $this->addHtml('<div class="hint">' . $html . '</div>', $options);
}
public function addHtml($html, $options = array())
{
if (array_key_exists('name', $options)) {
$name = $options['name'];
unset($options['name']);
} else {
$name = '_HINT' . ++$this->hintCount;
}
$this->addElement('simpleNote', $name, $options);
$this->getElement($name)
->setValue($html)
->setIgnore(true)
->setDecorators(array('ViewHelper'));
return $this;
}
public function optionalEnum($enum)
{
return array(
null => $this->translate('- please choose -')
) + $enum;
}
public function setSuccessUrl($url, $params = null)
{
if (! $url instanceof Url) {
@ -324,12 +234,6 @@ abstract class QuickForm extends Zend_Form
return parent::setAction($action);
}
public function setIcingaModule(Module $module)
{
$this->icingaModule = $module;
return $this;
}
public function hasBeenSubmitted()
{
if ($this->hasBeenSubmitted === null) {
@ -420,15 +324,6 @@ abstract class QuickForm extends Zend_Form
return $this;
}
public function translate($string)
{
if ($this->icingaModuleName === null) {
return t($string);
} else {
return mt($this->icingaModuleName, $string);
}
}
public function onSuccess()
{
$this->redirectOnSuccess();