Initial commit

This commit is contained in:
Nathan Adams
2013-07-20 17:41:56 -05:00
commit 3b1e713fc4
606 changed files with 136001 additions and 0 deletions

View File

@@ -0,0 +1,156 @@
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# ***** END LICENSE BLOCK ***** */
/**
* A class to store field, widget and data.
*
* Used when rendering a form.
*/
class Pluf_Form_BoundField
{
public $form = null;
public $field = null;
public $name = null;
public $html_name = null;
public $label = null;
public $help_text = null;
public $errors = array();
public function __construct($form, $field, $name)
{
$this->form = $form;
$this->field = $field;
$this->name = $name;
$this->html_name = $this->form->addPrefix($name);
if ($this->field->label == '') {
$this->label = mb_ereg_replace('/\_/', '/ /', mb_ucfirst($name));
} else {
$this->label = $this->field->label;
}
$this->help_text = ($this->field->help_text) ? $this->field->help_text : '';
if (isset($this->form->errors[$name])) {
$this->errors = $this->form->errors[$name];
}
}
public function render_w($widget=null, $attrs=array())
{
if ($widget === null) {
$widget = $this->field->widget;
}
$id = $this->autoId();
if ($id and !array_key_exists('id', $attrs)
and !array_key_exists('id', $widget->attrs)) {
$attrs['id'] = $id;
}
if (!$this->form->is_bound) {
$data = $this->form->initial($this->name);
} else {
$data = $this->field->widget->valueFromFormData($this->html_name, $this->form->data);
}
return $widget->render($this->html_name, $data, $attrs);
}
/**
* Returns the HTML of the label tag. Wraps the given contents in
* a <label>, if the field has an ID attribute. Does not
* HTML-escape the contents. If contents aren't given, uses the
* field's HTML-escaped label. If attrs are given, they're used as
* HTML attributes on the <label> tag.
*
* @param string Content of the label, will not be escaped (null).
* @param array Extra attributes.
* @return string HTML of the label.
*/
public function labelTag($contents=null, $attrs=array())
{
$contents = ($contents) ? $contents : htmlspecialchars($this->label);
$widget = $this->field->widget;
$id = (isset($widget->attrs['id'])) ? $widget->attrs['id'] : $this->autoId();
$_tmp = array();
foreach ($attrs as $attr=>$val) {
$_tmp[] = $attr.'="'.$val.'"';
}
if (count($_tmp)) {
$attrs = ' '.implode(' ', $_tmp);
} else {
$attrs = '';
}
return new Pluf_Template_SafeString(sprintf('<label for="%s"%s>%s</label>',
$widget->idForLabel($id), $attrs, $contents), true);
}
/**
* Calculates and returns the ID attribute for this BoundField, if
* the associated Form has specified auto_id. Returns an empty
* string otherwise.
*
* @return string Id or empty string if no auto id defined.
*/
public function autoId()
{
$id_fields = $this->form->id_fields;
if (false !== strpos($id_fields, '%s')) {
return sprintf($id_fields, $this->html_name);
} elseif ($id_fields) {
return $this->html_name;
}
return '';
}
/**
* Return HTML to display the errors.
*/
public function fieldErrors()
{
Pluf::loadFunction('Pluf_Form_renderErrorsAsHTML');
return new Pluf_Template_SafeString(Pluf_Form_renderErrorsAsHTML($this->errors), true);
}
/**
* Overloading of the property access.
*/
public function __get($prop)
{
if (!in_array($prop, array('labelTag', 'fieldErrors', 'render_w'))) {
return $this->$prop;
}
return $this->$prop();
}
/**
* Render as string.
*/
public function __toString()
{
return (string)$this->render_w();
}
}
if (!function_exists('mb_ucfirst')) {
function mb_ucfirst($str) {
return mb_strtoupper(mb_substr($str, 0, 1)).mb_substr($str, 1);
}
}

View File

@@ -0,0 +1,187 @@
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# ***** END LICENSE BLOCK ***** */
/**
* Default form field.
*
* A form field is providing a defined set of methods and properties
* to be used in the rendering of the fields in forms, in the
* conversion of the data from the user input to a form usable by the
* models.
*/
class Pluf_Form_Field
{
/**
* Store the name of the class.
*/
public $class = 'Pluf_Form_Field';
/**
* Widget. The way to "present" the field to the user.
*/
public $widget = 'Pluf_Form_Widget_TextInput';
public $label = ''; /**< The label of the field. */
public $required = false; /**< Allowed to be blank. */
public $help_text = ''; /**< Help text for the field. */
public $initial = ''; /**< Default value when empty. */
public $choices = null; /**< Predefined choices for the field. */
/*
* Following member variables are more for internal cooking.
*/
public $hidden_widget = 'Pluf_Form_Widget_HiddenInput';
public $value = ''; /**< Current value of the field. */
/**
* Returning multiple values (select multiple etc.)
*/
public $multiple = false;
protected $empty_values = array('', null, array());
/**
* Constructor.
*
* Example:
* $field = new Your_Field(array('required'=>true,
* 'widget'=>'Pluf_Form_Widget_TextInput',
* 'initial'=>'your name here',
* 'label'=>__('Your name'),
* 'help_text'=>__('You are?'));
*
* @param array Params of the field.
*/
function __construct($params=array())
{
// We basically take the parameters, for each one we grab the
// corresponding member variable and populate the $default
// array with. Then we merge with the values given in the
// parameters and update the member variables.
// This allows to pass extra parameters likes 'min_size'
// etc. and update the member variables accordingly. This is
// practical when you extend this class with your own class.
$default = array();
foreach ($params as $key=>$in) {
if ($key !== 'widget_attrs')
$default[$key] = $this->$key; // Here on purpose it
// will fail if a
// parameter not needed
// for this field is
// passed.
}
$m = array_merge($default, $params);
foreach ($params as $key=>$in) {
if ($key !== 'widget_attrs')
$this->$key = $m[$key];
}
// Set the widget to be an instance and not the string name.
$widget_name = $this->widget;
if (isset($params['widget_attrs'])) {
$attrs = $params['widget_attrs'];
} else {
$attrs = array();
}
$widget = new $widget_name($attrs);
$attrs = $this->widgetAttrs($widget);
if (count($attrs)) {
$widget->attrs = array_merge($widget->attrs, $attrs);
}
$this->widget = $widget;
}
/**
* Validate some possible input for the field.
*
* @param mixed Value to clean.
* @return mixed Cleaned data or throw a Pluf_Form_Invalid exception.
*/
function clean($value)
{
if (!$this->multiple and $this->required
and in_array($value, $this->empty_values)) {
throw new Pluf_Form_Invalid(__('This field is required.'));
}
if ($this->multiple and $this->required and empty($value)) {
throw new Pluf_Form_Invalid(__('This field is required.'));
}
return $value;
}
/**
* Set the default empty value for a field.
*
* @param mixed Value
* @return mixed Value
*/
function setDefaultEmpty($value)
{
if (in_array($value, $this->empty_values) and !$this->multiple) {
$value = '';
}
if (in_array($value, $this->empty_values) and $this->multiple) {
$value = array();
}
return $value;
}
/**
* Multi-clean a value.
*
* If you are getting multiple values, you need to go through all
* of them and validate them against the requirements. This will
* do that for you. Basically, it is cloning the field, marking it
* as not multiple and validate each value. It will throw an
* exception in case of failure.
*
* If you are implementing your own field which could be filled by
* a "multiple" widget, you need to perform a check on
* $this->multiple.
*
* @see Pluf_Form_Field_Integer::clean
*
* @param array Values
* @return array Values
*/
public function multiClean($value)
{
$field = clone($this);
$field->multiple = false;
reset($value);
while (list($i, $val) = each($value)) {
$value[$i] = $field->clean($val);
}
reset($value);
return $value;
}
/**
* Returns the HTML attributes to add to the field.
*
* @param object Widget
* @return array HTML attributes.
*/
public function widgetAttrs($widget)
{
return array();
}
}

View File

@@ -0,0 +1,36 @@
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# ***** END LICENSE BLOCK ***** */
class Pluf_Form_Field_Boolean extends Pluf_Form_Field
{
public $widget = 'Pluf_Form_Widget_CheckboxInput';
public function clean($value)
{
//parent::clean($value);
if (in_array($value, array('on', 'y', '1', 1, true))) {
return true;
}
return false;
}
}

View File

@@ -0,0 +1,55 @@
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# ***** END LICENSE BLOCK ***** */
class Pluf_Form_Field_Date extends Pluf_Form_Field
{
public $widget = 'Pluf_Form_Widget_TextInput';
public $input_formats = array(
'%Y-%m-%d', '%m/%d/%Y', '%m/%d/%y', // 2006-10-25, 10/25/2006, 10/25/06
'%b %d %Y', '%b %d, %Y', // 'Oct 25 2006', 'Oct 25, 2006'
'%d %b %Y', '%d %b, %Y', // '25 Oct 2006', '25 Oct, 2006'
'%B %d %Y', '%B %d, %Y', // 'October 25 2006', 'October 25, 2006'
'%d %B %Y', '%d %B, %Y', // '25 October 2006', '25 October, 2006'
);
public function clean($value)
{
parent::clean($value);
if (in_array($value, $this->empty_values)) {
return '';
}
foreach ($this->input_formats as $format) {
if (false !== ($date = strptime($value, $format))) {
$day = $date['tm_mday'];
$month = $date['tm_mon'] + 1;
$year = $date['tm_year'] + 1900;
if (checkdate($month, $day, $year)) {
return str_pad($year, 4, '0', STR_PAD_LEFT).'-'.
str_pad($month, 2, '0', STR_PAD_LEFT).'-'.
str_pad($day, 2, '0', STR_PAD_LEFT);
}
}
}
throw new Pluf_Form_Invalid(__('Enter a valid date.'));
}
}

View File

@@ -0,0 +1,68 @@
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# ***** END LICENSE BLOCK ***** */
class Pluf_Form_Field_Datetime extends Pluf_Form_Field
{
public $widget = 'Pluf_Form_Widget_DatetimeInput';
public $input_formats = array(
'%Y-%m-%d %H:%M:%S', // '2006-10-25 14:30:59'
'%Y-%m-%d %H:%M', // '2006-10-25 14:30'
'%Y-%m-%d', // '2006-10-25'
'%m/%d/%Y %H:%M:%S', // '10/25/2006 14:30:59'
'%m/%d/%Y %H:%M', // '10/25/2006 14:30'
'%m/%d/%Y', // '10/25/2006'
'%m/%d/%y %H:%M:%S', // '10/25/06 14:30:59'
'%m/%d/%y %H:%M', // '10/25/06 14:30'
'%m/%d/%y', // '10/25/06'
);
public function clean($value)
{
parent::clean($value);
if (in_array($value, $this->empty_values)) {
return '';
}
foreach ($this->input_formats as $format) {
if (false !== ($date = strptime($value, $format))) {
$day = $date['tm_mday'];
$month = $date['tm_mon'] + 1;
$year = $date['tm_year'] + 1900;
// PHP's strptime has various quirks, e.g. it doesn't check
// gregorian dates for validity and it also allows '60' in
// the seconds part
if (checkdate($month, $day, $year) && $date['tm_sec'] < 60) {
$date = str_pad($year, 4, '0', STR_PAD_LEFT).'-'.
str_pad($month, 2, '0', STR_PAD_LEFT).'-'.
str_pad($day, 2, '0', STR_PAD_LEFT).' '.
str_pad($date['tm_hour'], 2, '0', STR_PAD_LEFT).':'.
str_pad($date['tm_min'], 2, '0', STR_PAD_LEFT).':';
str_pad($date['tm_sec'], 2, '0', STD_PAD_LEFT);
// we internally use GMT, so we convert it to a GMT date.
return gmdate('Y-m-d H:i:s', strtotime($date));
}
}
}
throw new Pluf_Form_Invalid(__('Enter a valid date/time.'));
}
}

View File

@@ -0,0 +1,42 @@
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# ***** END LICENSE BLOCK ***** */
class Pluf_Form_Field_Email extends Pluf_Form_Field
{
public $widget = 'Pluf_Form_Widget_TextInput';
public function clean($value)
{
parent::clean($value);
if (in_array($value, $this->empty_values)) {
$value = '';
}
if ($value == '') {
return $value;
}
if (!Pluf_Utils::isValidEmail($value)) {
throw new Pluf_Form_Invalid(__('Enter a valid email address.'));
}
return $value;
}
}

View File

@@ -0,0 +1,151 @@
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# ***** END LICENSE BLOCK ***** */
class Pluf_Form_Field_File extends Pluf_Form_Field
{
public $widget = 'Pluf_Form_Widget_FileInput';
public $move_function = 'Pluf_Form_Field_File_moveToUploadFolder';
public $max_size = 2097152; // 2MB
public $move_function_params = array();
/**
* Validate some possible input for the field.
*
* @param mixed Input
* @return string Path to the file relative to 'upload_path'
*/
function clean($value)
{
parent::clean($value);
if (is_null($value) and !$this->required) {
return ''; // no file
} elseif (is_null($value) and $this->required) {
throw new Pluf_Form_Invalid(__('No files were uploaded. Please try to send the file again.'));
}
$errors = array();
$no_files = false;
switch ($value['error']) {
case UPLOAD_ERR_OK:
break;
case UPLOAD_ERR_INI_SIZE:
throw new Pluf_Form_Invalid(sprintf(__('The uploaded file is too large. Reduce the size of the file to %s and send it again.'),
Pluf_Utils::prettySize(ini_get('upload_max_filesize'))));
break;
case UPLOAD_ERR_FORM_SIZE:
throw new Pluf_Form_Invalid(sprintf(__('The uploaded file is too large. Reduce the size of the file to %s and send it again.'),
Pluf_Utils::prettySize($_REQUEST['MAX_FILE_SIZE'])));
break;
case UPLOAD_ERR_PARTIAL:
throw new Pluf_Form_Invalid(__('The upload did not complete. Please try to send the file again.'));
break;
case UPLOAD_ERR_NO_FILE:
if ($this->required) {
throw new Pluf_Form_Invalid(__('No files were uploaded. Please try to send the file again.'));
} else {
return ''; // no file
}
break;
case UPLOAD_ERR_NO_TMP_DIR:
case UPLOAD_ERR_CANT_WRITE:
throw new Pluf_Form_Invalid(__('The server has no temporary folder correctly configured to store the uploaded file.'));
break;
case UPLOAD_ERR_EXTENSION:
throw new Pluf_Form_Invalid(__('The uploaded file has been stopped by an extension.'));
break;
default:
throw new Pluf_Form_Invalid(__('An error occured when upload the file. Please try to send the file again.'));
}
if ($value['size'] > $this->max_size) {
throw new Pluf_Form_Invalid(sprintf(__('The uploaded file is to big (%1$s). Reduce the size to less than %2$s and try again.'),
Pluf_Utils::prettySize($value['size']),
Pluf_Utils::prettySize($this->max_size)));
}
// copy the file to the final destination and updated $value
// with the final path name. 'final_name' is relative to
// Pluf::f('upload_path')
Pluf::loadFunction($this->move_function);
// Should throw a Pluf_Form_Invalid exception if error or the
// value to be stored in the database.
return call_user_func($this->move_function, $value,
$this->move_function_params);
}
}
/**
* Default move function. The file name is sanitized.
*
* In the extra parameters, options can be used so that this function is
* matching most of the needs:
*
* * 'upload_path': The path in which the uploaded file will be
* stored.
* * 'upload_path_create': If set to true, try to create the
* upload path if not existing.
*
* * 'upload_overwrite': Set it to true if you want to allow overwritting.
*
* * 'file_name': Force the file name to this name and do not use the
* original file name. If this name contains '%s' for
* example 'myid-%s', '%s' will be replaced by the
* original filename. This can be used when for
* example, you want to prefix with the id of an
* article all the files attached to this article.
*
* If you combine those options, you can dynamically generate the path
* name in your form (for example date base) and let this upload
* function create it on demand.
*
* @param array Upload value of the form.
* @param array Extra parameters. If upload_path key is set, use it. (array())
* @return string Name relative to the upload path.
*/
function Pluf_Form_Field_File_moveToUploadFolder($value, $params=array())
{
$name = Pluf_Utils::cleanFileName($value['name']);
$upload_path = Pluf::f('upload_path', '/tmp');
if (isset($params['file_name'])) {
if (false !== strpos($params['file_name'], '%s')) {
$name = sprintf($params['file_name'], $name);
} else {
$name = $params['file_name'];
}
}
if (isset($params['upload_path'])) {
$upload_path = $params['upload_path'];
}
$dest = $upload_path.'/'.$name;
if (isset($params['upload_path_create'])
and !is_dir(dirname($dest))) {
if (false == @mkdir(dirname($dest), 0777, true)) {
throw new Pluf_Form_Invalid(__('An error occured when creating the upload path. Please try to send the file again.'));
}
}
if ((!isset($params['upload_overwrite']) or $params['upload_overwrite'] == false) and file_exists($dest)) {
throw new Pluf_Form_Invalid(sprintf(__('A file with the name "%s" has already been uploaded.'), $name));
}
if (@!move_uploaded_file($value['tmp_name'], $dest)) {
throw new Pluf_Form_Invalid(__('An error occured when uploading the file. Please try to send the file again.'));
}
@chmod($dest, 0666);
return $name;
}

View File

@@ -0,0 +1,48 @@
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# ***** END LICENSE BLOCK ***** */
class Pluf_Form_Field_Float extends Pluf_Form_Field
{
public $widget = 'Pluf_Form_Widget_TextInput';
public $max_value = null;
public $min_value = null;
public function clean($value)
{
parent::clean($value);
if (in_array($value, $this->empty_values)) {
$value = '';
}
if (!is_numeric($value)) {
throw new Pluf_Form_Invalid(__('Enter a number.'));
}
$value = (float) $value;
if ($this->max_value !== null and $this->max_value < $value) {
throw new Pluf_Form_Invalid(sprintf(__('Ensure this value is less than or equal to %s.'), $this->max_value));
}
if ($this->min_value !== null and $this->min_value > $value) {
throw new Pluf_Form_Invalid(sprintf(__('Ensure this value is greater than or equal to %s.'), $this->min_value));
}
return $value;
}
}

View File

@@ -0,0 +1,56 @@
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# ***** END LICENSE BLOCK ***** */
class Pluf_Form_Field_Integer extends Pluf_Form_Field
{
public $widget = 'Pluf_Form_Widget_TextInput';
public $max = null;
public $min = null;
public function clean($value)
{
parent::clean($value);
$value = $this->setDefaultEmpty($value);
if ($this->multiple) {
return $this->multiClean($value);
} else {
if ($value == '') return $value;
if (!preg_match('/^[\+\-]?[0-9]+$/', $value)) {
throw new Pluf_Form_Invalid(__('The value must be an integer.'));
}
$this->checkMinMax($value);
}
return (int) $value;
}
protected function checkMinMax($value)
{
if ($this->max !== null and $value > $this->max) {
throw new Pluf_Form_Invalid(sprintf(__('Ensure that this value is not greater than %1$d.'), $this->max));
}
if ($this->min !== null and $value < $this->min) {
throw new Pluf_Form_Invalid(sprintf(__('Ensure that this value is not lower than %1$d.'), $this->min));
}
}
}

View File

@@ -0,0 +1,170 @@
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# ***** END LICENSE BLOCK ***** */
/**
* Add ReCaptcha control to your forms.
*
* You need first to get a ReCaptcha account, create a domain and get
* the API keys for your domain. Check http://recaptcha.net/ for more
* information.
*
* The recaptcha field needs to know the IP address of the user
* submitting the form and if the request is made over SSL or
* not. This means that you need to provide the $request object in the
* extra parameters of your form.
*
* To add the ReCaptcha field to your form, simply add the following
* to your form object (note the use of $extra['request']):
*
* <pre>
* $ssl = (!empty($extra['request']->SERVER['HTTPS'])
* and $extra['request']->SERVER['HTTPS'] != 'off');
*
* $this->fields['recaptcha'] = new Pluf_Form_Field_ReCaptcha(
* array('required' => true,
* 'label' => __('Please solve this challenge'),
* 'privkey' => 'PRIVATE_RECAPTCHA_KEY_HERE',
* 'remoteip' => $extra['request']->remote_addr,
* 'widget_attrs' => array(
* 'pubkey' => 'PUBLIC_RECAPTCHA_KEY_HERE',
* ),
* ));
* </pre>
*
* Then in your template, you simply need to add the ReCaptcha field:
*
* <pre>
* {if $form.f.recaptcha.errors}{$form.f.recaptcha.fieldErrors}{/if}
* {$form.f.recaptcha|safe}
* </pre>
*
* Based on http://recaptcha.googlecode.com/files/recaptcha-php-1.10.zip
*
* Copyright (c) 2007 reCAPTCHA -- http://recaptcha.net
* AUTHORS:
* Mike Crawford
* Ben Maurer
*/
class Pluf_Form_Field_ReCaptcha extends Pluf_Form_Field
{
public $widget = 'Pluf_Form_Widget_ReCaptcha';
public $privkey = '';
public $remoteip = '';
public $extra_params = array();
public function clean($value)
{
// will throw the Pluf_Form_Invalid exception in case of
// error.
self::checkAnswer($this->privkey, $this->remoteip,
$value[0], $value[1], $this->extra_params);
return $value;
}
/**
* Submits an HTTP POST to a reCAPTCHA server
*
* @param string Host
* @param string Path
* @param array Data
* @param int port (80
* @return array response
*/
public static function httpPost($host, $path, $data, $port=80)
{
$req = self::qsencode($data);
$http_request = "POST $path HTTP/1.0\r\n";
$http_request .= "Host: $host\r\n";
$http_request .= "Content-Type: application/x-www-form-urlencoded;\r\n";
$http_request .= "Content-Length: " . strlen($req) . "\r\n";
$http_request .= "User-Agent: reCAPTCHA/PHP\r\n";
$http_request .= "\r\n";
$http_request .= $req;
if (false === ($fs=@fsockopen($host, $port, $errno, $errstr, 10))) {
throw new Pluf_Form_Invalid(__('Cannot connect to the reCaptcha server for validation.'));
}
fwrite($fs, $http_request);
$response = '';
while (!feof($fs)) {
$response .= fgets($fs, 1160); // One TCP-IP packet
}
fclose($fs);
return explode("\r\n\r\n", $response, 2);
}
/**
* Encodes the given data into a query string format
*
* @param array Array of string elements to be encoded
* @return string Encoded request
*/
public static function qsencode($data)
{
$d = array();
foreach ($data as $key => $value) {
$d[] = $key.'='.urlencode(stripslashes($value));
}
return implode('&', $d);
}
/**
* Calls an HTTP POST function to verify if the user's guess was correct
* @param string $privkey
* @param string $remoteip
* @param string $challenge
* @param string $response
* @param array $extra_params an array of extra variables to post to the server
* @return ReCaptchaResponse
*/
public static function checkAnswer($privkey, $remoteip, $challenge, $response, $extra_params=array())
{
if ($privkey == '') {
throw new Pluf_Form_Invalid(__('To use reCAPTCHA you must set your API key.'));
}
if ($remoteip == '') {
throw new Pluf_Form_Invalid(__('For security reasons, you must pass the remote ip to reCAPTCHA.'));
}
//discard spam submissions
if (strlen($challenge) == 0 || strlen($response) == 0) {
return false;
}
$response = self::httpPost('api-verify.recaptcha.net', '/verify',
array(
'privatekey' => $privkey,
'remoteip' => $remoteip,
'challenge' => $challenge,
'response' => $response
) + $extra_params
);
$answers = explode("\n", $response[1]);
if (trim($answers[0]) == 'true') {
return true;
} else {
throw new Pluf_Form_Invalid($answers[1]);
}
}
}

View File

@@ -0,0 +1,111 @@
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2010 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# ***** END LICENSE BLOCK ***** */
class Pluf_Form_Field_Slug extends Pluf_Form_Field
{
/**
* Name of the widget to use for build the forms.
*
* @var string
*/
public $widget = 'Pluf_Form_Widget_TextInput';
/**
* Minimum size of field.
*
* Default to 1.
*
* @var int
**/
public $min_size = 1;
/**
* Maximum size of field.
*
* Default to 50.
*
* @var int
**/
public $max_size = 50;
protected $_error_messages = array();
public function __construct($params=array())
{
if (in_array($this->help_text, $this->empty_values)) {
$this->help_text = __('The &#8220;slug&#8221; is the URL-friendly'.
' version of the name, consisting of '.
'letters, numbers, underscores or hyphens.');
}
$this->_error_messages = array(
'min_size' => __('Ensure this value has at most %1$d characters (it has %2$d).'),
'max_size' => __('Ensure this value has at least %1$d characters (it has %2$d).')
);
parent::__construct($params);
}
/**
* Removes any character not allowed and valid the size of the field.
*
* @see Pluf_Form_Field::clean()
* @throws Pluf_Form_Invalid If the lenght of the field has not a valid size.
*/
public function clean($value)
{
parent::clean($value);
if ($value) {
$value = Pluf_DB_Field_Slug::slugify($value);
$len = mb_strlen($value, Pluf::f('encoding', 'UTF-8'));
if ($this->max_size < $len) {
throw new Pluf_Form_Invalid(sprintf($this->_error_messages['max_size'],
$this->max_size,
$len));
}
if ($this->min_size > $len) {
throw new Pluf_Form_Invalid(sprintf($this->_error_messages['min_size'],
$this->min_size,
$len));
}
}
else
$value = '';
return $value;
}
/**
* @see Pluf_Form_Field::widgetAttrs()
*/
public function widgetAttrs($widget)
{
$attrs = array();
if (!isset($widget->attrs['maxlength'])) {
$attrs['maxlength'] = $this->max_size;
} else {
$this->max_size = $widget->attrs['maxlength'];
}
return $attrs;
}
}

View File

@@ -0,0 +1,39 @@
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# ***** END LICENSE BLOCK ***** */
class Pluf_Form_Field_Url extends Pluf_Form_Field
{
public $widget = 'Pluf_Form_Widget_TextInput';
public function clean($value)
{
parent::clean($value);
if (in_array($value, $this->empty_values)) {
return '';
}
if (!Pluf_Utils::isValidUrl($value)) {
throw new Pluf_Form_Invalid(__('Enter a valid address.'));
}
return $value;
}
}

View File

@@ -0,0 +1,57 @@
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# ***** END LICENSE BLOCK ***** */
class Pluf_Form_Field_Varchar extends Pluf_Form_Field
{
public $widget = 'Pluf_Form_Widget_TextInput';
public $max_length = null;
public $min_length = null;
public function clean($value)
{
parent::clean($value);
if (in_array($value, $this->empty_values)) {
$value = '';
}
$value_length = mb_strlen($value);
if ($this->max_length !== null and $value_length > $this->max_length) {
throw new Pluf_Form_Invalid(sprintf(__('Ensure this value has at most %1$d characters (it has %2$d).'), $this->max_length, $value_length));
}
if ($this->min_length !== null and $value_length < $this->min_length) {
throw new Pluf_Form_Invalid(sprintf(__('Ensure this value has at least %1$d characters (it has %2$d).'), $this->min_length, $value_length));
}
return $value;
}
public function widgetAttrs($widget)
{
if ($this->max_length !== null and
in_array(get_class($widget),
array('Pluf_Form_Widget_TextInput',
'Pluf_Form_Widget_PasswordInput'))) {
return array('maxlength'=>$this->max_length);
}
return array();
}
}

View File

@@ -0,0 +1,48 @@
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# ***** END LICENSE BLOCK ***** */
/**
* Field proxy to access a form field through {$form.f.fieldname} in a
* template.
*/
class Pluf_Form_FieldProxy
{
protected $form = null;
public function __construct(&$form)
{
$this->form = $form;
}
/**
* No control are performed. If you access a non existing field it
* will simply throw an error.
*/
public function __get($field)
{
return new Pluf_Form_BoundField($this->form,
$this->form->fields[$field],
$field);
}
}

View File

@@ -0,0 +1,26 @@
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# ***** END LICENSE BLOCK ***** */
class Pluf_Form_Invalid extends Exception
{
}

View File

@@ -0,0 +1,87 @@
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# ***** END LICENSE BLOCK ***** */
/**
* Dynamic form validation class.
*
* This class is used to generate a form for a given model.
*/
class Pluf_Form_Model extends Pluf_Form
{
/**
* The model for which the form applies.
*/
public $model = null;
function initFields($extra=array())
{
$this->model = $extra['model'];
if (isset($extra['fields'])) {
// Only display a subset of the fields
$cols = array();
foreach ($extra['fields'] as $field) {
$cols[$field] = $this->model->_a['cols'][$field];
}
} else {
$cols = $this->model->_a['cols'];
}
foreach ($cols as $name=>$def) {
$db_field = new $def['type']('', $name);
$def = array_merge(array('blank' => true,
'verbose' => $name,
'help_text' => '',
'editable' => true),
$def);
if ($def['editable']) {
// The 'model_instance' and 'name' are used by the
// ManyToMany field.
$def['model_instance'] = $this->model;
$def['name'] = $name;
if (null !== ($form_field=$db_field->formField($def))) {
$this->fields[$name] = $form_field;
}
}
}
}
/**
* Save the model in the database.
*
* @param bool Commit in the database or not. If not, the object
* is returned but not saved in the database.
* @return Object Model with data set from the form.
*/
function save($commit=true)
{
if ($this->isValid()) {
$this->model->setFromFormData($this->cleaned_data);
if ($commit && $this->model->id) {
$this->model->update();
} elseif ($commit) {
$this->model->create();
}
return $this->model;
}
throw new Exception(__('Cannot save the model from an invalid form.'));
}
}

View File

@@ -0,0 +1,116 @@
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# ***** END LICENSE BLOCK ***** */
/**
* Base class to display a form field.
*
*/
class Pluf_Form_Widget
{
public $is_hidden = false; /**< Is an hidden field? */
public $needs_multipart_form = false; /**< Do we need multipart? */
public $input_type = ''; /**< Input type of the field. */
public $attrs = array(); /**< HTML attributes for the widget. */
public function __construct($attrs=array())
{
$this->attrs = $attrs;
}
/**
* Renders the HTML of the input.
*
* @param string Name of the field.
* @param mixed Value for the field, can be a non valid value.
* @param array Extra attributes to add to the input form (array())
* @return string The HTML string of the input.
*/
public function render($name, $value, $extra_attrs=array())
{
throw new Exception('Not Implemented.');
}
/**
* Build the list of attributes for the form.
* It should be called this way:
* $this->buildAttrs(array('name'=>$name, 'type'=>$this->input_type),
* $extra_attrs);
*
* @param array Contains the name and type attributes.
* @param array Extra attributes, like 'class' for example.
* @return array The attributes for the field.
*/
protected function buildAttrs($attrs, $extra_attrs=array())
{
return array_merge($this->attrs, $attrs, $extra_attrs);
}
/**
* A widget can split itself in multiple input form. For example
* you can have a datetime value in your model and you use 2
* inputs one for the date and one for the time to input the
* value. So the widget must know how to get back the values from
* the submitted form.
*
* @param string Name of the form.
* @param array Submitted form data.
* @return mixed Value or null if not defined.
*/
public function valueFromFormData($name, $data)
{
if (isset($data[$name])) {
return $data[$name];
}
return null;
}
/**
* Returns the HTML ID attribute of this Widget for use by a
* <label>, given the ID of the field. Returns None if no ID is
* available.
*
* This hook is necessary because some widgets have multiple HTML
* elements and, thus, multiple IDs. In that case, this method
* should return an ID value that corresponds to the first ID in
* the widget's tags.
*/
public function idForLabel($id)
{
return $id;
}
}
/**
* Convert an array in a string ready to use for HTML attributes.
*
* As all the widget will extend the Pluf_Form_Widget class, it means
* that this function is available directly in the extended class.
*/
function Pluf_Form_Widget_Attrs($attrs)
{
$_tmp = array();
foreach ($attrs as $attr=>$val) {
$_tmp[] = $attr.'="'.$val.'"';
}
return ' '.implode(' ', $_tmp);
}

View File

@@ -0,0 +1,66 @@
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# ***** END LICENSE BLOCK ***** */
/**
* Simple checkbox.
*/
class Pluf_Form_Widget_CheckboxInput extends Pluf_Form_Widget_Input
{
public $input_type = 'checkbox';
/**
* Renders the HTML of the input.
*
* @param string Name of the field.
* @param mixed Value for the field, can be a non valid value.
* @param array Extra attributes to add to the input form (array())
* @return string The HTML string of the input.
*/
public function render($name, $value, $extra_attrs=array())
{
if ((bool)$value) {
// We consider that if a value can be boolean casted to
// true, then we check the box.
$extra_attrs['checked'] = 'checked';
}
// Value of a checkbox is always "1" but when not checked, the
// corresponding key in the form associative array is not set.
return parent::render($name, '1', $extra_attrs);
}
/**
* A non checked checkbox is simply not returned in the form array.
*
* @param string Name of the form.
* @param array Submitted form data.
* @return mixed Value or null if not defined.
*/
public function valueFromFormData($name, $data)
{
if (!isset($data[$name]) or false === $data[$name]
or (string)$data[$name] === '0' or $data[$name] == '') {
return false;
}
return true;
}
}

View File

@@ -0,0 +1,50 @@
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# ***** END LICENSE BLOCK ***** */
/**
* Simple input of type datetime.
*/
class Pluf_Form_Widget_DatetimeInput extends Pluf_Form_Widget_Input
{
public $input_type = 'text';
public $format = 'Y-m-d H:i'; // '2006-10-25 14:30' by default do
// not show the seconds.
/**
* Renders the HTML of the input.
*
* @param string Name of the field.
* @param mixed Value for the field, can be a non valid value.
* @param array Extra attributes to add to the input form (array())
* @return string The HTML string of the input.
*/
public function render($name, $value, $extra_attrs=array())
{
// Internally we use GMT, so we convert back to the current
// timezone.
if (strlen($value) > 0) {
$value = date($this->format, strtotime($value.' GMT'));
}
return parent::render($name, $value, $extra_attrs);
}
}

View File

@@ -0,0 +1,38 @@
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# ***** END LICENSE BLOCK ***** */
/**
* Simple input of type file.
*/
class Pluf_Form_Widget_FileInput extends Pluf_Form_Widget_Input
{
public $input_type = 'file';
public $needs_multipart_form = true;
public function render($name, $value, $extra_attrs=array())
{
$value = '';
return parent::render($name, $value, $extra_attrs);
}
}

View File

@@ -0,0 +1,31 @@
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# ***** END LICENSE BLOCK ***** */
/**
* Simple input of type text.
*/
class Pluf_Form_Widget_HiddenInput extends Pluf_Form_Widget_Input
{
public $input_type = 'hidden';
public $is_hidden = true;
}

View File

@@ -0,0 +1,49 @@
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# ***** END LICENSE BLOCK ***** */
/**
* Base class for all the input widgets. (Except radio and checkbox).
*/
class Pluf_Form_Widget_Input extends Pluf_Form_Widget
{
/**
* Renders the HTML of the input.
*
* @param string Name of the field.
* @param mixed Value for the field, can be a non valid value.
* @param array Extra attributes to add to the input form (array())
* @return string The HTML string of the input.
*/
public function render($name, $value, $extra_attrs=array())
{
if ($value === null) $value = '';
$final_attrs = $this->buildAttrs(array('name' => $name,
'type' => $this->input_type),
$extra_attrs);
if ($value !== '') {
$value = htmlspecialchars($value, ENT_COMPAT, 'UTF-8');
$final_attrs['value'] = $value;
}
return new Pluf_Template_SafeString('<input'.Pluf_Form_Widget_Attrs($final_attrs).' />', true);
}
}

View File

@@ -0,0 +1,46 @@
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# ***** END LICENSE BLOCK ***** */
/**
* Simple input of type text.
*/
class Pluf_Form_Widget_PasswordInput extends Pluf_Form_Widget_Input
{
public $input_type = 'password';
public $render_value = true;
public function __construct($attrs=array())
{
$this->render_value = (isset($attrs['render_value'])) ? $attrs['render_value'] : $this->render_value;
unset($attrs['render_value']);
parent::__construct($attrs);
}
public function render($name, $value, $extra_attrs=array())
{
if ($this->render_value === false) {
$value = '';
}
return parent::render($name, $value, $extra_attrs);
}
}

View File

@@ -0,0 +1,106 @@
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# ***** END LICENSE BLOCK ***** */
/**
* reCAPTCHA input for your forms.
*
* Based on http://recaptcha.googlecode.com/files/recaptcha-php-1.10.zip
*
* Copyright (c) 2007 reCAPTCHA -- http://recaptcha.net
* AUTHORS:
* Mike Crawford
* Ben Maurer
*
* @see Pluf_Form_Field_ReCaptcha
*
*/
class Pluf_Form_Widget_ReCaptcha extends Pluf_Form_Widget_Input
{
public $input_type = 'text';
public $ssl = false;
public $pubkey = '';
/**
* Renders the HTML of the input.
*
* @param string Name of the field.
* @param mixed Value for the field, can be a non valid value.
* @param array Extra attributes to add to the input form (array())
* @return string The HTML string of the input.
*/
public function render($name, $value, $extra_attrs=array())
{
return Pluf_Template::markSafe(self::getHtml($this->attrs['pubkey']));
}
/**
* Gets the challenge HTML (javascript and non-javascript
* version). This is called from the browser, and the resulting
* reCAPTCHA HTML widget is embedded within the HTML form it was
* called from.
*
* @param string A public key for reCAPTCHA
* @param string The error given by reCAPTCHA (null)
* @param boolean Should the request be made over ssl? (false)
* @return string The HTML to be embedded in the user's form.
*/
public static function getHtml($pubkey, $error=null, $use_ssl=false)
{
$server = ($use_ssl) ? 'https://api-secure.recaptcha.net'
: 'http://api.recaptcha.net';
$errorpart = ($error) ? '&amp;error='.$error : '';
return '<script type="text/javascript" src="'.$server.'/challenge?k='
.$pubkey.$errorpart.'"></script>
<noscript>
<iframe src="'.$server.'/noscript?k='.$pubkey.$errorpart
.'" height="300" width="500" frameborder="0"></iframe><br/>
<textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea>
<input type="hidden" name="recaptcha_response_field" value="manual_challenge"/>
</noscript>';
}
/**
* Get the form data from the reCaptcha fields.
*
* We need to get back two fields from the POST request
* 'recaptcha_challenge_field' and 'recaptcha_response_field'.
*
* They are hardcoded, so we do not even bother checking something
* else.
*
* @param string Name of the form
* @param array Submitted form data
* @return array Challenge and answer
*/
public function valueFromFormData($name, $data)
{
$res = array('', '');
$res[0] = isset($data['recaptcha_challenge_field'])
? $data['recaptcha_challenge_field'] : '';
$res[1] = isset($data['recaptcha_response_field'])
? $data['recaptcha_response_field'] : '';
return $res;
}
}

View File

@@ -0,0 +1,78 @@
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# ***** END LICENSE BLOCK ***** */
/**
* Simple checkbox with grouping.
*/
class Pluf_Form_Widget_SelectInput extends Pluf_Form_Widget
{
public $choices = array();
public function __construct($attrs=array())
{
$this->choices = $attrs['choices'];
unset($attrs['choices']);
parent::__construct($attrs);
}
/**
* Renders the HTML of the input.
*
* @param string Name of the field.
* @param mixed Value for the field, can be a non valid value.
* @param array Extra attributes to add to the input form (array())
* @param array Extra choices (array())
* @return string The HTML string of the input.
*/
public function render($name, $value, $extra_attrs=array(),
$choices=array())
{
$output = array();
if ($value === null) {
$value = '';
}
$final_attrs = $this->buildAttrs(array('name' => $name), $extra_attrs);
$output[] = '<select'.Pluf_Form_Widget_Attrs($final_attrs).'>';
$groups = $this->choices + $choices;
foreach($groups as $option_group => $c) {
if (!is_array($c)) {
$subchoices = array($option_group => $c);
} else {
$output[] = '<optgroup label="'.htmlspecialchars($option_group, ENT_COMPAT, 'UTF-8').'">';
$subchoices = $c;
}
foreach ($subchoices as $option_label=>$option_value) {
$selected = ($option_value == $value) ? ' selected="selected"':'';
$output[] = sprintf('<option value="%s"%s>%s</option>',
htmlspecialchars($option_value, ENT_COMPAT, 'UTF-8'),
$selected,
htmlspecialchars($option_label, ENT_COMPAT, 'UTF-8'));
}
if (is_array($c)) {
$output[] = '</optgroup>';
}
}
$output[] = '</select>';
return new Pluf_Template_SafeString(implode("\n", $output), true);
}
}

View File

@@ -0,0 +1,79 @@
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# ***** END LICENSE BLOCK ***** */
/**
* Simple checkbox.
*/
class Pluf_Form_Widget_SelectMultipleInput extends Pluf_Form_Widget
{
public $choices = array();
public function __construct($attrs=array())
{
$this->choices = $attrs['choices'];
unset($attrs['choices']);
parent::__construct($attrs);
}
/**
* Renders the HTML of the input.
*
* @param string Name of the field.
* @param array Value for the field, can be a non valid value.
* @param array Extra attributes to add to the input form (array())
* @param array Extra choices (array())
* @return string The HTML string of the input.
*/
public function render($name, $value, $extra_attrs=array(),
$choices=array())
{
$output = array();
if ($value === null) {
$value = array();
}
$final_attrs = $this->buildAttrs(array('name' => $name.'[]'),
$extra_attrs);
$output[] = '<select multiple="multiple"'
.Pluf_Form_Widget_Attrs($final_attrs).'>';
$choices = array_merge($this->choices, $choices);
foreach ($choices as $option_label=>$option_value) {
$selected = (in_array($option_value, $value)) ? ' selected="selected"':'';
$output[] = sprintf('<option value="%s"%s>%s</option>',
htmlspecialchars($option_value, ENT_COMPAT, 'UTF-8'),
$selected,
htmlspecialchars($option_label, ENT_COMPAT, 'UTF-8'));
}
$output[] = '</select>';
return new Pluf_Template_SafeString(implode("\n", $output), true);
}
public function valueFromFormData($name, $data)
{
if (isset($data[$name]) and is_array($data[$name])) {
return $data[$name];
}
return null;
}
}

View File

@@ -0,0 +1,74 @@
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# ***** END LICENSE BLOCK ***** */
/**
* Simple checkbox.
*/
class Pluf_Form_Widget_SelectMultipleInput_Checkbox extends Pluf_Form_Widget_SelectMultipleInput
{
/**
* Renders the HTML of the input.
*
* @param string Name of the field.
* @param array Value for the field, can be a non valid value.
* @param array Extra attributes to add to the input form (array())
* @param array Extra choices (array())
* @return string The HTML string of the input.
*/
public function render($name, $value, $extra_attrs=array(),
$choices=array())
{
$output = array();
if ($value === null or $value == '') {
$value = array();
}
$final_attrs = $this->buildAttrs($extra_attrs);
$output[] = '<ul>';
$choices = array_merge($this->choices, $choices);
$i=0;
$base_id = $final_attrs['id'];
foreach ($choices as $option_label=>$option_value) {
$final_attrs['id'] = $base_id.'_'.$i;
$final_attrs['value'] = htmlspecialchars($option_value, ENT_COMPAT, 'UTF-8');
$checkbox = new Pluf_Form_Widget_CheckboxInput($final_attrs);
$rendered = $checkbox->render($name.'[]', in_array($option_value, $value));
$output[] = sprintf('<li><label>%s %s</label></li>', $rendered,
htmlspecialchars($option_label, ENT_COMPAT, 'UTF-8'));
$i++;
}
$output[] = '</ul>';
return new Pluf_Template_SafeString(implode("\n", $output), true);
}
public function idForLabel($id)
{
if ($id) {
$id += '_0';
}
return $id;
}
}

View File

@@ -0,0 +1,30 @@
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# ***** END LICENSE BLOCK ***** */
/**
* Simple input of type text.
*/
class Pluf_Form_Widget_TextInput extends Pluf_Form_Widget_Input
{
public $input_type = 'text';
}

View File

@@ -0,0 +1,55 @@
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# ***** END LICENSE BLOCK ***** */
/**
* Textarea.
*/
class Pluf_Form_Widget_TextareaInput extends Pluf_Form_Widget
{
public function __construct($attrs=array())
{
$this->attrs = array_merge(array('cols' => '40', 'rows' => '10'),
$attrs);
}
/**
* Renders the HTML of the input.
*
* @param string Name of the field.
* @param mixed Value for the field, can be a non valid value.
* @param array Extra attributes to add to the input form (array())
* @return string The HTML string of the input.
*/
public function render($name, $value, $extra_attrs=array())
{
if ($value === null) $value = '';
$final_attrs = $this->buildAttrs(array('name' => $name),
$extra_attrs);
return new Pluf_Template_SafeString(
sprintf('<textarea%s>%s</textarea>',
Pluf_Form_Widget_Attrs($final_attrs),
htmlspecialchars($value, ENT_COMPAT, 'UTF-8')),
true);
}
}

View File

@@ -0,0 +1,98 @@
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# ***** END LICENSE BLOCK ***** */
/**
* Textarea with TinyMCE addition.
*/
class Pluf_Form_Widget_TinyMCEInput extends Pluf_Form_Widget
{
public $tiny_mceurl = '/media/js/editor/tiny_mce.js';
public $mode = 'textareas';
public $theme = 'simple';
public $include_tinymce = true;
public function __construct($attrs=array())
{
$defaults = array('cols' => '70',
'rows' => '20');
$config = array('tinymce_url', 'mode', 'theme', 'include_tinymce');
foreach ($config as $cfg) {
if (isset($attrs[$cfg])) {
$this->$cfg = $attrs[$cfg];
unset($attrs[$cfg]);
}
}
$this->attrs = array_merge($defaults, $attrs);
}
/**
* Renders the HTML of the input.
*
* @param string Name of the field.
* @param mixed Value for the field, can be a non valid value.
* @param array Extra attributes to add to the input form (array())
* @return string The HTML string of the input.
*/
public function render($name, $value, $extra_attrs=array())
{
if ($value === null) $value = '';
$extra_config = '';
if (isset($this->attrs['editor_config'])) {
$_ec = $this->attrs['editor_config'];
unset($this->attrs['editor_config']);
$_st = array();
foreach ($_ec as $key=>$val) {
if (is_bool($val)) {
if ($val) {
$_st[] = $key.' : true';
} else {
$_st[] = $key.' : false';
}
} else {
$_st[] = $key.' : "'.$val.'"';
}
}
if ($_st) {
$extra_config = ",\n".implode(",\n", $_st);
}
}
$final_attrs = $this->buildAttrs(array('name' => $name),
$extra_attrs);
// The special include for tinyMCE
$out = '';
if ($this->include_tinymce) {
$out .= '<script language="javascript" type="text/javascript" src="'.$this->tinymce_url.'"></script>'."\n";
}
$out .='<script language="javascript" type="text/javascript">
tinyMCE.init({
mode : "'.$this->mode.'",
theme : "'.$this->theme.'"'.$extra_config.'
});
</script>';
return new Pluf_Template_SafeString(
$out.sprintf('<textarea%s>%s</textarea>',
Pluf_Form_Widget_Attrs($final_attrs),
htmlspecialchars($value, ENT_COMPAT, 'UTF-8')),
true);
}
}