<?php
/*
* Copyright 2008 Blandware (http://www.blandware.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Contains utils to work with forms.
*
* @package AtleapLite
* @author Roman Puchkovskiy
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
*/
/**
*/
require 'include/constants.php';
require_once 'HTML/QuickForm.php';
require_once 'HTML/QuickForm/livesearch_select.php';
require_once 'include/util/messages.php';
/**
* Base class for all forms.
*
* @package AtleapLite
*/
class FormBase extends HTML_QuickForm {
/**
* Does some init work.
*/
function init() {
$this->setJsWarnings(getMessage('form.error.prefix'), getMessage('form.error.postfix'));
$this->registerElementType('richedit', 'include/form/formelements.php', 'RichEdit');
$this->registerElementType('calendardate', 'include/form/formelements.php', 'CalendarDate');
$this->registerRule('image', 'callback', 'validate_image');
}
/**
* Constructor.
*
* @param string $name form name
* @param string $method HTTP method
* @param string $url form action URL
*/
function FormBase($name, $method, $url) {
$this->HTML_QuickForm($name, $method, $url);
$this->init();
}
/**
* Returns true if element with a given name exists.
*
* @param string $name element name
* @return true if exists
*/
function elementExists($name) {
$elem =& $this->getElement($name);
return !PEAR::isError($elem);
}
/**
* Adds an abstract text element to form.
*
* @param string $type element type
* @param string $name element name
* @param string $title element title
* @param array $params optional parameters
* @return object added element
* @access private
*/
function addAbstractTextElement($type, $name, $title, $params = null) {
$params = $this->_computeParams($params, array('maxlength' => 255, 'class' => 'form_text'));
return $this->addElement($type, $name, $title, $params);
}
/**
* Adds a text element to form.
*
* @param string $name element name
* @param string $title element title
* @param array $params optional parameters
* @return object added element
*/
function addTextElement($name, $title, $params = null) {
return $this->addAbstractTextElement('text', $name, $title, $params);
}
/**
* Adds a password element to form.
*
* @param string $name element name
* @param string $title element title
* @param array $params optional parameters
* @return object added element
*/
function addPasswordElement($name, $title, $params = null) {
$params = $this->_computeParams($params, array('maxlength' => 40));
return $this->addAbstractTextElement('password', $name, $title, $params);
}
/**
* Adds a textarea element to form.
*
* @param string $name element name
* @param string $title element title
* @param array $params optional parameters
* @return object added element
*/
function addTextareaElement($name, $title, $params = null) {
return $this->addElement('textarea', $name, $title, $params);
}
/**
* Adds a select element to form.
*
* @param string $name element name
* @param string $title element title
* @param array $options options (from values to titles)
* @param array $params optional parameters
* @return object added element
*/
function addSelectElement($name, $title, $options, $params = null) {
$params = $this->_computeParams($params, array('class' => 'form_select'));
return $this->addElement('select', $name, $title, $options, $params);
}
/**
* Adds a checkbox element to form.
*
* @param string $name element name
* @param string $title element title
* @param array $params optional parameters
* @return object added element
*/
function addCheckboxElement($name, $title, $params = null) {
return $this->addElement('checkbox', $name, $title, $params);
}
/**
* Adds a hidden element to form.
*
* @param string $name element name
* @param string $value element value
* @param array $params optional parameters
* @return object added element
*/
function addHiddenElement($name, $value, $params = null) {
return $this->addElement('hidden', $name, $value, $params);
}
/**
* Adds a file element to form.
*
* @param string $name element name
* @param string $title element title
* @param array $params optional parameters
* @return object added element
*/
function addFileElement($name, $title, $params = null) {
$params = $this->_computeParams($params, array('class' => 'form_file'));
return $this->addElement('file', $name, $title, $params);
}
/**
* Adds a date element to form.
*
* @param string $name element name
* @param string $title element title
* @param array $params optional parameters
* @return object added element
*/
function addDateElement($name, $title, $params = null) {
return $this->addElement('date', $name, $title, $params);
}
/**
* Adds a calendar date element to form.
*
* @param string $name element name
* @param string $title element title
* @param array $attributes optional attributes
* @param array options optional options
* @return object added element
*/
function addCalendarDateElement($name, $title, $attributes = null,
$options = null) {
$attributes = $this->_computeParams($attributes, array('class' => 'form_text'));
return $this->addElement('calendardate', $name, $title, $attributes, $options);
}
/**
* Adds a rich edit element to form.
*
* @param string $name element name
* @param string $title element title
* @param array $params optional parameters
* @return object added element
*/
function addRicheditElement($name, $title, $params = null) {
global $fckBase;
$params = $this->_computeParams($params,
array('fckBase' => $fckBase, 'width' => FCKE_WIDTH, 'height' => FCKE_HEIGHT, 'toolbarSet' => 'Custom'));
return $this->addElement('richedit', $name, $title, $params);
}
/**
* Adds a livesearch element to form.
*
* @param string $name element name
* @param string $title element title
* @param array $attributes optional attributes
* @param array $params optional parameters
* @return object added element
*/
function addLivesearchElement($name, $title, $attributes = null,
$params = null) {
$attributes = $this->_computeParams($attributes,
array(
'elementId' => 'search',
'style' => '',
'divstyle' => '',
'ulstyle' => '',
'listyle' => '',
'searchZeroLength' => 0,
'buffer' => 350,
'printStyle' => 0,
'autoComplete' => 1,
'autoserverPath' => ''
));
$params = $this->_computeParams($params,
array(
'onchange' => 'livesearchChanged(this);',
'class' => 'form_text'
));
$this->addElement('livesearch_select', $name, $title, $attributes, $params);
}
/**
* Adds a submit element to form.
*
* @param string $name element name
* @param string $title element title
* @param array $params optional parameters
* @return object added element
*/
function addSubmitElement($name, $title, $params = null) {
$params = $this->_computeParams($params, array('class' => 'form_button'));
return $this->addElement('submit', $name, $title, $params);
}
/**
* Adds a proceed element to form.
*
* @param string $title element title
* @param array $params optional parameters
* @return object added element
*/
function addProceedElement($title, $params = null) {
return $this->addSubmitElement('submit', $title, $params);
}
/**
* Adds a cancel element to form.
*
* @param string $title element title
* @param array $params optional parameters
* @return object added element
*/
function addCancelElement($title, $params = null) {
$params = $this->_computeParams($params, array('onclick' => 'bCancel = true;'));
return $this->addSubmitElement('cancel', $title, $params);
}
/**
* Adds a back button element to form.
*
* @param string $title element title
* @param array $params optional parameters
* @return object added element
*/
function addBackElement($title, $params = null) {
$params = $this->_computeParams($params, array('onclick' => 'bCancel = true;'));
return $this->addSubmitElement('back', $title, $params);
}
/**
* Adds a 'required' validation rule to form.
*
* @param string $name field name
* @param string $message error message
*/
function addRequiredRule($name, $message) {
return $this->addRule($name, $message, 'required', null, 'client');
}
/**
* Adds an 'identifier' validation rule to form.
*
* @param string $name field name
* @param string $message error message
*/
function addIdentifierRule($name, $message) {
return $this->addRule($name, $message, 'lettersonly', null, 'client');
}
/**
* Adds an 'extended identifier' validation rule to form.
*
* @param string $name field name
* @param string $message error message
*/
function addExtendedIdentifierRule($name, $message) {
return $this->addRule($name, $message, 'regex', '/^[a-zA-Z]([a-zA-Z\d\_\-])*$/', 'client');
}
/**
* Adds a 'digits only' validation rule to form.
*
* @param string $name field name
* @param string $message error message
*/
function addDigitsRule($name, $message) {
return $this->addRule($name, $message, 'regex', '/^\d+$/', 'client');
}
/**
* Adds a 'file name' validation rule to form.
*
* @param string $name field name
* @param string $message error message
*/
function addFileNameRule($name, $message) {
return $this->addRule($name, $message, 'regex', '/^[a-zA-Z0-9-_.]+\.[a-zA-Z0-9-_]+$/', 'client');
}
/**
* Adds a 'file uploaded' validation rule to form.
*
* @param string $name field name
* @param string $message error message
*/
function addUploadedFileRule($name, $message) {
return $this->addRule($name, $message, 'uploadedfile');
}
/**
* Adds an 'image' validation rule to form.
*
* @param string $name field name
* @param string $message error message
*/
function addImageRule($name, $message) {
return $this->addRule($name, $message, 'image');
}
/**
* Merges given params with default params.
*
* @param array $params given params
* @param array $defaultParams default params
* @return array merged params
* @access private
*/
function _computeParams($params, $defaultParams) {
if ($params !== null) {
foreach ($params as $k => $v) {
$defaultParams[$k] = $v;
}
}
return $defaultParams;
}
}
/**
* Validates that uploaded file contains image of one of supported formats.
*
* @param array $value value
* @param mixed $dummy ignored
* @return bool true if this is valid image
*/
function validate_image($value, $dummy)
{
$sizes = @getimagesize($value['tmp_name']);
if ($sizes == null) {
return false;
}
if ($sizes[2] != IMAGETYPE_JPEG && $sizes[2] != IMAGETYPE_PNG) {
return false;
}
return true;
}
/**
* Shows a form to user.
*
* @param object $smarty smarty instance
* @param object $form form object which to display
* @param string $template template to use when displaying
* @param string $title page title to show
*/
function showForm(&$smarty, &$form, $template, $title)
{
// Create, set up and assign a renderer
$renderer =& new HTML_QuickForm_Renderer_ArraySmarty($smarty);
$form->accept($renderer);
$formContent = $renderer->toArray();
// Replace validate_*(this) with validate_*(forms['*'])
$formContent['attributes'] = fixFormOnSubmit($formContent['attributes']);
$smarty->assign('form', $formContent);
// capture the array stucture
ob_start();
print_r($renderer->toArray());
$smarty->assign('static_array', ob_get_contents());
ob_end_clean();
// render and display the template
$smarty->assign('template', $template);
$smarty->assign('title', $title);
$smarty->assign('js', $formContent['javascript']);
}
/**
* Replaces validate_*(this) with validate_*(forms['*']).
*
* @param string $attrs string that represents form attributes
* @return string fixed string
*/
function fixFormOnSubmit($attrs)
{
$idBegin = "id=\"";
$onsubmitBegin = "onsubmit=\"";
$onsubmitPos = stristr($attrs, $onsubmitBegin);
$idPos = stristr($attrs, $idBegin);
if ($onsubmitPos !== false && $idPos !== false) {
$id = substr($idPos, strlen($idBegin));
list($id, $rest) = explode("\"", $id, 2);
list($before, $after) = explode($onsubmitBegin, $attrs, 2);
/* $afterOnSubmit = strstr($after, '"');
$onSubmitLen = strlen($after) - strlen($afterOnSubmit);
$onSubmitCode = substr($after, 0, $onSubmitLen);var_dump($onSubmitCode);
if ($onSubmitCode[$onSubmitLen-1] == ';') {
$onSubmitCode = substr($onSubmitCode, 0, $onSubmitCode-1);
}
$returnCode = 'return ';
if (strstr($onSubmitCode, $returnCode) == 0) {
$onSubmitCode = substr($onSubmitCode, strlen($returnCode));
}
$after = $returnCode . "bCancel ? true : ($onSubmitCode);" . $afterOnSubmit;*/
$afterOnSubmit = strstr($after, '"');
$onSubmitLen = strlen($after) - strlen($afterOnSubmit);
$onSubmitCode = substr($after, 0, $onSubmitLen);
$mid = "if (bCancel) {return true;} else { $onSubmitCode }";
if (ENFORCE_1251) {
$mid = "fixBadChars(this); " . $mid;
}
$after = $returnCode . $mid . $afterOnSubmit;
$attrs = $before . $onsubmitBegin . str_replace("this", "document.forms['$id']", $after);
}
return $attrs;
}
?>