* @version 0.0.0
* @copyright Copyright (c) 2007, 2008 Roland Haeder, this is free software
* @license GNU GPL 3.0 or any newer version
* @link http://www.ship-simu.org
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
class WebFormHelper extends BaseWebHelper implements HelpableTemplate {
/**
* Wether the form tag is opened (keep at false or else your forms will
* never work!)
*/
private $formOpened = false;
/**
* Name of the form
*/
private $formName = "";
/**
* Wether form tag is enabled (default: true)
*/
private $formEnabled = true;
// Class Constants
const EXCEPTION_FORM_NAME_INVALID = 0x120;
const EXCEPTION_CLOSED_FORM = 0x121;
const EXCEPTION_OPENED_FORM = 0x122;
const EXCEPTION_UNEXPECTED_CLOSED_GROUP = 0x123;
/**
* Protected constructor
*
* @return void
*/
protected function __construct () {
// Call parent constructor
parent::__construct(__CLASS__);
}
/**
* Creates the helper class with the given template engine instance and form name
*
* @param $templateInstance An instance of a valid template engine
* @param $formName Name of the form
* @param $formId Value for "id" attribute (default: $formName)
* @param $withForm Wether include the form tag
* @return $helperInstance A preparedf instance of this helper
*/
public final static function createWebFormHelper (CompileableTemplate $templateInstance, $formName, $formId = false, $withForm = true) {
// Get new instance
$helperInstance = new WebFormHelper();
// Set template instance
$helperInstance->setTemplateInstance($templateInstance);
// Is the form id not set?
if ($formId === false) {
// Use form id from form name
$formId = $formName;
} // END - if
// Set form name
$helperInstance->setFormName($formName);
// A form-less field may say "false" here...
if ($withForm === true) {
// Create the form
$helperInstance->addFormTag($formName, $formId);
} else {
// Disable form
$helperInstance->enableForm(false);
}
// Return the prepared instance
return $helperInstance;
}
/**
* Add the form tag or close it an already opened form tag
*
* @param $formName Name of the form (default: false)
* @param $formId Id of the form (attribute "id"; default: false)
* @return void
* @throws InvalidFormNameException If the form name is invalid (=false)
* @todo Add some unique PIN here to bypass problems with some browser and/or extensions
*/
public function addFormTag ($formName = false, $formId = false) {
// When the form is not yet opened at least form name must be valid
if (($this->formOpened === false) && ($formName === false)) {
// Thrown an exception
throw new InvalidFormNameException ($this, self::EXCEPTION_FORM_NAME_INVALID);
} // END - if
// Close the form is default
$formContent = "";
// Check wether we shall open or close the form
if (($this->formOpened === false) && ($this->formEnabled === true)) {
// Add HTML code
$formContent = sprintf("