begin process at 2012 02 09 12:49:22
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

Class et Objet ( POO )

 > CLASS DE FORMULAIRE HTML

CLASS DE FORMULAIRE HTML


 Information sur la source

Note :
Aucune note
Catégorie :Class et Objet ( POO ) Niveau :Initié Date de création :23/12/2004 Date de mise à jour :28/07/2005 20:09:58 Vu / téléchargé :5 908 / 657

Auteur : JeanPoldeux

Ecrire un message privé
Commentaire sur cette source (5)
Ajouter un commentaire et/ou une note

 Description

Cette classe permet de créer un formulaire HTML, d'y ajouter des composants et de l'afficher. Possibilité de coupler ce script à une feuille CSS et à des fonctions javascript

Source

  • <?php
  • /**
  • * Project:
  • * File:
  • * Classes: Component, Button, Text, List, Check, Option, Form
  • *
  • * This file contains a set of class used to create and handle a xhtml form.
  • *
  • * @author Jean Poldeux <jean_poldeux@hotmail.com>
  • * @date July 27th, 2005
  • * @version 2.1
  • */
  • /**
  • * This class is used to create any form component.
  • */
  • class Component
  • {
  • /**
  • * Constructor that creates a form object with a name and a default value
  • * @param name Component name (STRING)
  • * @param default Default value - default="" (STRING)
  • */
  • function Component($name, $default = "")
  • {
  • $this->att["name"] = $name;
  • $this->att["default"] = $default;
  • }
  • /**
  • * Set the default value (STRING)
  • * @param default Default value - default="" (STRING)
  • */
  • function setDefault($default = "")
  • {
  • $this->att["default"] = $default;
  • }
  • /**
  • * Set the class attribute of the component xhtml tag
  • * @param class The value of class attribute - default="" (STRING)
  • */
  • function setClass($class = "")
  • {
  • $this->att["class"] = $class;
  • }
  • /**
  • * Set the id attribute of the component xhtml tag
  • * @param id The value of id attribute - default="" (STRING)
  • */
  • function setId($id = "")
  • {
  • $this->att["id"] = $id;
  • }
  • /**
  • * Add a new attribute of the component xhtml tag
  • * @param name The name of the new attribute (STRING)
  • * @param value The value of this attribute - default = "" (STRING)
  • * @return (BOOLEAN) TRUE if the new attribute has correctly been added - FALSE if it already exits or an error occured
  • */
  • function addAtt($name, $value = "")
  • {
  • if(!array_key_exists($name, $this->att) && strlen($value) != 0 )
  • {
  • $this->att[$name] = $value;
  • return true;
  • }
  • else
  • return false;
  • }
  • /**
  • * Set a new value to xhtml attribute given as parameter
  • * @param attribute The name of the attribute (STRING)
  • * @param value The value of this attribute - default = "" (STRING)
  • * @return (BOOLEAN) TRUE if the attribute has correctly been changed - FALSE if it doesn't exits or an error occured
  • */
  • function setAtt($name, $value = "")
  • {
  • if(array_key_exists($name, $this->att) && strlen($value) != 0 )
  • {
  • $this->att[$name] = $value;
  • return true;
  • }
  • else
  • return false;
  • }
  • /**
  • * Remove a xhtml attribute of this component
  • * @param attribute The name of the attribute (STRING)
  • * @return (BOOLEAN) TRUE if the attribute has correctly been removed - FALSE if it doesn't exits or an error occured
  • */
  • function removeAtt($name)
  • {
  • if(array_key_exists($name, $this->att))
  • {
  • //Find the place of this element
  • $index = array_search($name, array_keys($this->att));
  • //remove the attribute
  • array_splice($this->att, $index, 1);
  • //return true
  • return true;
  • }
  • else
  • return false;
  • }
  • /**
  • * This function makes you able to display the component within a form object
  • */
  • function display()
  • {
  • //Initialise the display string var
  • $var = "";
  • //Point to the first element of the attribute array
  • reset($this->att);
  • //Loop through the array and extract the key and value
  • while(list($attribute, $value) = each($this->att))
  • {
  • //check if the attribute is not empty
  • if(isset($value) && strlen($value) != 0)
  • {
  • $var = $attribute."=\"".$value."\" ";
  • }
  • }
  • //display the obtained string
  • echo $var;
  • }
  • /**
  • * @var att An indexed array that contains the xhtml attribute as key and
  • * the xhtml value as value
  • */
  • var $att = array();
  • }
  • /**
  • * This class is used to create a button within a form object
  • */
  • class Button extends Component
  • {
  • /**
  • * Constructor that initializes the button with compulsory values
  • * @param name Name of this button. Be careful that this name have to be unique within a single form (STRING)
  • * @param type (INTEGER)
  • * <ul>
  • * <li>1 => a text submit button (the default value)</li>
  • * <li>2 => a text reset button</li>
  • * <li>3 => a text button (It calls the javascript contained in js_func var)</li>
  • * <li>4 => a image button(It calls the javascript contained in js_func var)</li>
  • * </ul>
  • * @param size The button size in pixels (INTEGER)
  • * @param caption The button caption (STRING)
  • * @param image The absolute filename of the image displayed on the button (STRING)
  • * @param js_func The javascript function that should be called when the button is clicked. (STRING)
  • * Set its name if it's defined in xhtml header or the all function definition.
  • */
  • function Button($name = "button", $type = 1, $size = 75, $caption = "", $image = "", $js_func = "")
  • {
  • //call the super constructor
  • $this->Component($name,"");
  • //Set the button type and add the new attributes in relation with this type
  • switch($type)
  • {
  • case 2:
  • $this->att["type"] = "reset";
  • if(strlen($caption) != 0)
  • {
  • $this->att["value"] = htmlentities($caption, ENT_QUOTES);
  • }
  • else
  • {
  • $this->att["value"] = "Cancel";
  • }
  • break;
  • case 3:
  • $this->att["type"] = "button";
  • if(strlen($caption) != 0)
  • {
  • $this->att["value"] = htmlentities($caption, ENT_QUOTES);
  • }
  • else
  • {
  • $this->att["value"] = htmlentities("Click here", ENT_QUOTES);
  • }
  • $this->att["OnClick()"] = $js_func;
  • break;
  • case 4:
  • $this->att["type"] = "button";
  • if(strlen($image) != 0)
  • {
  • $this->att["src"] = $image;
  • }
  • else
  • {
  • $this->att["src"] = "";
  • }
  • $this->att["OnClick()"] = $js_func;
  • break;
  • default:
  • $this->att["type"] = "submit";
  • if(strlen($caption) != 0)
  • {
  • $this->att["value"] = htmlentities($caption, ENT_QUOTES);
  • }
  • else
  • {
  • $this->att["value"] = "Submit";
  • }
  • break;
  • }
  • //Add the specific attributes to the new button
  • $this->att["size"] = $taille." px";
  • }
  • /**
  • * Set the button caption
  • * @param caption The caption (STRING)
  • * @return (BOOLEAN) True if the caption has been changed - FALSE if this is
  • * image button and has no caption
  • */
  • function setCaption($caption = "")
  • {
  • if (strcmp($this->att["type"], "submit") == 0)
  • {
  • if(strlen($caption) != 0)
  • {
  • $this->att["value"] = $caption;
  • }
  • else
  • {
  • $this->att["value"] = "Submit";
  • }
  • return true;
  • }
  • elseif (strcmp($this->att["type"], "reset") == 0)
  • {
  • if(strlen($caption) != 0)
  • {
  • $this->att["value"] = $caption;
  • }
  • else
  • {
  • $this->att["value"] = "Cancel";
  • }
  • return true;
  • }
  • elseif ( strcmp($this->att["type"], "button") == 0 && (!isset($this->["src"]) || strlen($this->["src"]) == 0) )
  • {
  • if(strlen($caption) != 0)
  • {
  • $this->att["value"] = $caption;
  • }
  • else
  • {
  • $this->att["value"] = "Submit";
  • }
  • return true;
  • }
  • else
  • {
  • return false;
  • }
  • }
  • /**
  • * Set the button image
  • * @param image The absolute filename of the image (STRING)
  • * @return (BOOLEAN) True if the image has been changed - FALSE if this is
  • * text, cancel or submit button and has a caption
  • */
  • function setImage($image = "")
  • {
  • if ( strcmp($this->att["type"], "button") == 0 && (!isset($this->["value"]) || strlen($this->["value"]) == 0) )
  • {
  • if(strlen($image) != 0)
  • {
  • $this->att["src"] = $caption;
  • }
  • else
  • {
  • $this->att["src"] = "";
  • }
  • return true;
  • }
  • else
  • {
  • return false;
  • }
  • }
  • /**
  • * Set the button size
  • * @param size The button size in pixels (INTEGER)
  • */
  • function setSize($size)
  • {
  • $this->att["size"] = $size." px";
  • }
  • /**
  • * Set the javascript function name or the all function definition called when
  • * the button is clicked.
  • * @param js_func The javascript function call or definition (STRING)
  • * @return (BOOLEAN) True if the function has been changed - FALSE if this is
  • * cancel or submit button and has a caption
  • */
  • function setJsFunc($js_func)
  • {
  • if(strcmp($this->att["type"], "button") == 0)
  • {
  • $this->att["OnClick"] = $js_func;
  • return true;
  • }
  • else
  • {
  • return false;
  • }
  • }
  • /**
  • * Display the button within the form.
  • */
  • function display()
  • {
  • echo "<input ";
  • Composant::affiche();
  • echo "/>\n";
  • }
  • }
  • /**
  • * This class is used to create a textbox, a password box or a text area
  • */
  • class Text extends Component
  • {
  • /**
  • * Constructor that initializes the text component with compulsory values
  • * @param name Name of this component. Be careful that this name have to be unique within a single form (STRING)
  • * @param type (INTEGER)
  • * <ul>
  • * <li>1 => a textbox </li>
  • * <li>2 => a textarea </li>
  • * <li>3 => a textbox used for password ('*' are insered during completion)</li>
  • * <li>4 => an hidden textbox</li>
  • * </ul>
  • * @param default The default value (STRING)
  • */
  • function Text($name = "text", $type = 1, $default)
  • {
  • //Call the super constructor
  • $this->Component($name, $default);
  • //Choose the type and the specific attribute
  • switch($type)
  • {
  • case 2:
  • $this->box_or_area = "textarea";
  • break;
  • case 3:
  • $this->box_or_area = "input";
  • $this->att["type"] = "password";
  • case 4:
  • $this->box_or_area = "input";
  • $this->att["type"] = "hidden";
  • default:
  • $this->box_or_area = "input";
  • $this->att["type"] = "text";
  • }
  • }
  • /**
  • * Set the textbox size. This function can only be used with textbox or password
  • * @param size The textbox size in pixels (INTEGER)
  • * @return (BOOLEAN) True if the size has been changed - False if it's not the
  • * right type.
  • */
  • function setSize($size = 75)
  • {
  • if (strcmp($this->box_or_area, "textarea") != 0)
  • {
  • if(strcmp($this->att["type"], "hidden") != 0)
  • {
  • this->att["size"] = $size." px";
  • return true;
  • }
  • else
  • return false;
  • }
  • else
  • return false;
  • }
  • /**
  • * Set the maximum number of characters that a text box can contain.
  • * This function can only be used with textbox or password
  • * @param nb_char The maximum number of characters (INTEGER)
  • * @return (BOOLEAN) True if the number has been changed - False if it's not the
  • * right type.
  • */
  • function setMax($nbChar)
  • {
  • if (strcmp($this->box_or_area, "textarea") != 0)
  • {
  • if(strcmp($this->att["type"], "hidden") != 0)
  • {
  • this->att["maxlength"] = $nbChar;
  • return true;
  • }
  • else
  • return false;
  • }
  • else
  • return false;
  • }
  • /**
  • * Set the text area size in number of rows X number of columns.
  • * @param rows The number of rows (INTEGER)
  • * @param cols The number of columns (INTEGER)
  • * @return (BOOLEAN) True if the size has been changed - False if it's not the
  • * right type.
  • */
  • function setDimension($rows, $cols)
  • {
  • if (strcmp($this->box_or_area, "textarea") == 0)
  • {
  • this->att["rows"] = $rows;
  • this->att["cols"] = $cols;
  • return true;
  • }
  • else
  • return false;
  • }
  • /**
  • * Set the way new line character is handled. It only works for text area
  • * @param new_line This parameter can take these values :
  • * <ul>
  • * <li>1 => The text area doesn't accept new line </li>
  • * <li>2 => The text area accepts new line but they are not sent </li>
  • * <li>3 => The text area accepts new line and these are sent</li>
  • * </ul>
  • * @return (BOOLEAN) True if the method has been changed - False if it's not the
  • * right type.
  • */
  • function newLineHandling($new_line)
  • {
  • if (strcmp($this->box_or_area, "textarea") == 0)
  • {
  • switch($new_line)
  • {
  • case 1:
  • $this->att["wrap"] = "off";
  • case 3:
  • $this->att["wrap"] = "physical";
  • default:
  • $this->att["wrap"] = "virtual";
  • }
  • return true;
  • }
  • else
  • return false;
  • }
  • /**
  • * Display the textbox within the form.
  • */
  • function display()
  • {
  • echo "<".$this->box_or_area." ";
  • Composant::affiche();
  • echo "/>\n";
  • }
  • /**
  • * @var box_or_area Used to store if this object is a <input/> xhtml tag or
  • * an <textarea> tag.
  • */
  • var $box_or_area;
  • }
  • /**
  • * This class is used to create a radio button. In opposition with other
  • * components, you can have more than one option button with the same name.
  • * In that case, the value sent to the server will be the one contained in
  • * $data_sent of the selected radio button.
  • */
  • class Option extends Component
  • {
  • /**
  • * Constructor that initializes the button with compulsory values
  • * @param name Name of this button. (STRING)
  • * @param caption The caption displayed near the radio button (STRING)
  • * @param data_sent The string that's sent when the form is submited if the option is selected.
  • */
  • function Option($name = "option", $caption="Option", $data_sent="Option selected")
  • {
  • $this->Component($name);
  • $this->att["type"] = "radio";
  • $this->att["value"] = $data_sent;
  • $this->caption = $caption;
  • }
  • /**
  • * Set the button which will be selected by default among all the buttons
  • * that have the same name.
  • * @param $selected True if the button has to be selected - False if not (BOOLEAN)
  • */
  • function selected($selected = true)
  • {
  • if($selected)
  • $this->selected = "checked";
  • else
  • $this->selected = "";
  • }
  • /**
  • * Set the data which will be sent if the button is selected
  • * @param data_sent The data that 'll be sent to the server (STRING)
  • */
  • function setDataSent($data_sent)
  • {
  • $this->att["value"] = $data_sent;
  • }
  • /**
  • * Display the radio button within the form.
  • */
  • function display()
  • {
  • echo "<input ";
  • Composant::affiche();
  • echo $this->selected." /> ".$this->caption."\n";
  • }
  • /**
  • * @var caption The text value that follows the radio button. (STRING)
  • */
  • var $caption;
  • /**
  • * @var caption Flag if the radio button must be selected by default. (STRING)
  • */
  • var $selected;
  • }
  • /**
  • * This class is used to create a check button.
  • */
  • class Check extends Component
  • {
  • /**
  • * Constructor that initializes the button with compulsory values
  • * @param name Name of this button. Be careful that this name have to be unique within a single form (STRING)
  • * @param caption The caption displayed near the check button (STRING)
  • * @param data_sent The string that's sent when the form is submited if the option is checked.
  • */
  • function Check($name = "check", $caption="Check", $data_sent="Option Checked")
  • {
  • $this->Component($name);
  • $this->att["type"] = "checkbox";
  • $this->att["value"] = $data_sent;
  • $this->caption = $caption;
  • }
  • /**
  • * Check or Uncheck the button
  • * @param $selected True if the button has to be checked - False if not (BOOLEAN)
  • */
  • function checked($checked = true)
  • {
  • if($checked)
  • $this->checked = "checked";
  • else
  • $this->checked = "";
  • }
  • /**
  • * Set the data which will be sent if the button is checked
  • * @param data_sent The data that 'll be sent to the server (STRING)
  • */
  • function setDataSent($data_sent)
  • {
  • $this->att["value"] = $data_sent;
  • }
  • /**
  • * Display the check button within the form.
  • */
  • function display()
  • {
  • echo "<input ";
  • Composant::affiche();
  • echo $this->checked." /> ".$this->caption."\n";
  • }
  • /**
  • * @var caption The text value that follows the radio button. (STRING)
  • */
  • var $caption;
  • /**
  • * @var caption Flag if the radio button must be selected by default. (STRING)
  • */
  • var $selected;
  • }
  • /**
  • * This class is used to create a dropdown list
  • */
  • class Listbox extends Component
  • {
  • /**
  • * Constructor that initializes all the compulsory attributes.
  • * @param name The dropdown list name. Be careful that this name have to be unique within a single form (STRING)
  • * @param width The dropdown list width. It's given in pixels (INTEGER)
  • * @param options The list of options contained in the list (KEYED ARRAY of STRING)
  • * where the key is the text contained in the list and the value is the one that
  • * will be sent to the server.
  • * @param displayed_options The number of list items that will be shown to the user. (INTEGER)
  • * @param multiple True if the selection can be multiple - False if not (BOOLEAN)
  • */
  • function Listbox($name = "list", $width= 75, $options = array(), $displayed_options = 5, $multiple = "false")
  • {
  • $this->Composant($name);
  • $this->options = $options;
  • $this->att["size"] = $displayed_options;
  • $this->multiple = $multiple;
  • $this->width = $width." px";
  • }
  • /**
  • * Set the different items contained in the dropdown list.
  • * @param options The list of options contained in the list (KEYED ARRAY of STRING)
  • * where the key is the text contained in the list and the value is the one that
  • * will be sent to the server.
  • */
  • function setOptions($options = array())
  • {
  • $this->options = $options;
  • }
  • /**
  • * Set the list item that will be selected when the form is displayed.
  • * @param index The index number of the option that should be selected. (INTEGER)
  • * @return (BOOLEAN) True if the item index has been found - False if the index is out of bounds.
  • */
  • function setSelectedItem($index = 0)
  • {
  • if(is_array($this->options))
  • {
  • if ($index>=1 && $index<=count($this->options))
  • {
  • $this->default = $index;
  • return true;
  • }
  • else
  • return false;
  • }
  • else
  • return false;
  • }
  • /**
  • * Set if the user can select more than one item in the list or not.
  • * @param multiple True if the selection can be multiple - False if not (BOOLEAN)
  • */
  • function setMultiple($multiple = "false")
  • {
  • $this->multiple = $multiple;
  • }
  • /**
  • * Set the number of item that are shown to the user when the list is displayed.
  • * @param displayed_options The number of list items (INTEGER)
  • */
  • function setDisplayedOptions($displayed_options = 5)
  • {
  • $this->att["size"] = $displayed_options;
  • }
  • /**
  • * Set the list width.
  • * @param width The dropdown list width. It's given in pixels (INTEGER)
  • */
  • function setWidth($width = 75)
  • {
  • $this->width = $width." px";
  • }
  • /**
  • * Display the dropdown list.
  • */
  • function display()
  • {
  • //Display the <select> xhtml tag
  • echo "<select ";
  • //Display the component attributes
  • Component::display();
  • if($this->multiple)
  • {
  • echo "multiple ";
  • }
  • echo ">\n";
  • //Display all the options
  • if(is_array($this->options))
  • {
  • //Point to the first element
  • reset($this->options);
  • //Set the pointer position to start
  • $pos = 0;
  • //Loop through the array
  • while(list($text, $value) = each($options))
  • {
  • //Display the xhtml <option> tag
  • echo "<option value=\"".$value."\" width=\"".$this->width."\" ";
  • //If the current position in the array is the default option, write it down.
  • if($this->default == $pos)
  • {
  • echo "selected ";
  • }
  • echo ">\n";
  • //Increment the position counter
  • $pos++;
  • //Display the option caption
  • echo $text."\n";
  • echo "</option>\n";
  • }
  • }
  • //Display the closing xhtml tag.
  • echo "</select>\n";
  • }
  • /**
  • * @var multiple True if the selection can be mutliple - False if not (BOOLEAN)
  • */
  • var $multiple;
  • /**
  • * @var options The list of options contained in the list (KEYED ARRAY of STRING)
  • */
  • var $options = array();
  • /**
  • * @var default The option that will be selected by default when the list is displayed (INTEGER)
  • */
  • var $default;
  • /**
  • * @var width The dropdown list width. It's given in pixels (INTEGER)
  • */
  • var $width;
  • }
  • /**
  • * This class is used to create a form. Every component can be added to this form.
  • */
  • class Form extends Component
  • {
  • /**
  • * Constructor that initializes the form with compulsory values
  • * @param name Name of this button. Be careful that this name have to be unique within a single page (STRING)
  • * @param type (INTEGER)
  • * <ul>
  • * <li>
  • * 1 => the method used to send the form data is POST. (Default)
  • * These values can be used with $_POST["name"] = value
  • * </li>
  • * <li>
  • * 2 => the method used to send the form data is GET. The data are
  • * send through the URL.
  • * These values can be used with $_GET["name"] = value
  • * </li>
  • * </ul>
  • * @param script The absolute or relative path to the script that will handle the form data (STRING)
  • */
  • function Form($name, $type, $script)
  • {
  • $this->Component($name);
  • $this->components = array();
  • switch($type)
  • {
  • case 2:
  • $this->att["method"] = "get"
  • default:
  • $this->att["method"] = "post"
  • }
  • $this->att["action"] = $script;
  • }
  • /**
  • * Add a component to the form.
  • * @param component A Component object (Button, Listbox, etc.) (COMPONENT CLASS)
  • * @param br True if a newline character should be displayed just after this component - False if not (BOOLEAN)
  • * @return (BOOLEAN) True if the component has been added - False if an error has occured
  • */
  • function addComponent($component, $br)
  • {
  • if(isset($component))
  • {
  • if($br)
  • $comp = array($component, "<br/>");
  • else
  • $comp = array($component, "");
  • $this->components = array_push($this->components, $comp);
  • return true;
  • }
  • else
  • return false;
  • }
  • /**
  • * Remove a component from the form.
  • * @param index The index number of the componant that should be removed. (INTEGER)
  • * @return (BOOLEAN) True if the component has been removed - False if an error has occured
  • */
  • function removeComponent($index)
  • {
  • if(isset($index) && $index >= 1 && $index<=count($this->components))
  • {
  • array_splice($this->components, $index-1, 1);
  • return true;
  • }
  • else
  • return false;
  • }
  • /**
  • * Remove all components from the form.
  • */
  • function removeComponents()
  • {
  • this->components = array();
  • }
  • /**
  • * Display the form
  • */
  • function display()
  • {
  • echo "<form ";
  • Component::display();
  • echo ">\n";
  • for($i=0; $i<count($this->components); $i++)
  • {
  • $this->components[$i][0]->display();
  • if($this->composants[$i][1])
  • {
  • echo "<br/>\n";
  • }
  • }
  • echo "</form>\n";
  • }
  • }
  • ?>
<?php

/**
 * Project:     
 * File:        
 * Classes:     Component, Button, Text, List, Check, Option, Form
 *
 * This file contains a set of class used to create and handle a xhtml form.
 *
 * @author Jean Poldeux <jean_poldeux@hotmail.com>
 * @date July 27th, 2005
 * @version 2.1
 */

/**
 * This class is used to create any form component.
 */
class Component

  {

  /**
   * Constructor that creates a form object with a name and a default value
   * @param name Component name  (STRING)
   * @param default Default value - default=""  (STRING)
   */
  function Component($name, $default = "")

    {

    $this->att["name"] = $name;

    $this->att["default"] = $default;

    }
    
  /**
   * Set the default value (STRING)
   * @param default Default value - default="" (STRING)
   */  

  function setDefault($default = "")

    {

    $this->att["default"] = $default;

    }
    
  /**
   * Set the class attribute of the component xhtml tag
   * @param class The value of class attribute - default="" (STRING)
   */

  function setClass($class = "")

    {

    $this->att["class"] = $class;

    }
    
  /**
   * Set the id attribute of the component xhtml tag
   * @param id The value of id attribute - default="" (STRING)
   */ 

  function setId($id = "")

    {

    $this->att["id"] = $id;

    }

  /**
   * Add a new attribute of the component xhtml tag
   * @param name The name of the new attribute (STRING)
   * @param value The value of this attribute - default = "" (STRING)
   * @return (BOOLEAN) TRUE if the new attribute has correctly been added - FALSE if it already exits or an error occured
   */ 

  function addAtt($name, $value = "")

    {
    if(!array_key_exists($name, $this->att) && strlen($value) != 0 )
      {
      $this->att[$name] = $value;
      return true;
      }
    else 
      return false;

    }
    
  /**
   * Set a new value to xhtml attribute given as parameter
   * @param attribute The name of the attribute (STRING)
   * @param value The value of this attribute - default = "" (STRING)
   * @return (BOOLEAN) TRUE if the attribute has correctly been changed - FALSE if it doesn't exits or an error occured
   */ 

  function setAtt($name, $value = "")

    {
    if(array_key_exists($name, $this->att) && strlen($value) != 0 )
      {
      $this->att[$name] = $value;
      return true;
      }
    else 
      return false;

    }
    
  /**
   * Remove a xhtml attribute of this component
   * @param attribute The name of the attribute (STRING)
   * @return (BOOLEAN) TRUE if the attribute has correctly been removed - FALSE if it doesn't exits or an error occured
   */ 

  function removeAtt($name)

    {
    if(array_key_exists($name, $this->att))
      {
      //Find the place of this element
      $index = array_search($name, array_keys($this->att));
      //remove the attribute
      array_splice($this->att, $index, 1);
      //return true
      return true;
      }
    else
      return false;

    }
    
  /**
   * This function makes you able to display the component within a form object
   */     

  function display()

    {
    //Initialise the display string var
    $var = "";
    
    //Point to the first element of the attribute array
    reset($this->att);
    
    //Loop through the array and extract the key and value
    while(list($attribute, $value) = each($this->att))
      {
      //check if the attribute is not empty
      if(isset($value) && strlen($value) != 0)
        {
        $var = $attribute."=\"".$value."\" ";
        }
      }
    //display the obtained string
    echo $var;

    }
    
 /**
  * @var att An indexed array that contains the xhtml attribute as key and 
  *          the xhtml value as value
  */
  var $att = array();  

  }


/**
 * This class is used to create a button within a form object
 */

class Button extends Component

  {
  /**
   * Constructor that initializes the button with compulsory values
   * @param name Name of this button. Be careful that this name have to be unique within a single form (STRING)
   * @param type (INTEGER)
   *           <ul>
   *            <li>1 => a text submit button (the default value)</li>
   *            <li>2 => a text reset button</li>
   *            <li>3 => a text button (It calls the javascript contained in js_func var)</li>
   *            <li>4 => a image button(It calls the javascript contained in js_func var)</li>
   *           </ul>
   * @param size The button size in pixels (INTEGER)
   * @param caption The button caption (STRING)
   * @param image The absolute filename of the image displayed on the button (STRING)
   * @param js_func The javascript function that should be called when the button is clicked. (STRING)
   *        Set its name if it's defined in xhtml header or the all function definition.
   */

  function Button($name = "button", $type = 1, $size = 75, $caption = "", $image = "", $js_func = "")

    {
    //call the super constructor

    $this->Component($name,"");
    
    //Set the button type and add the new attributes in relation with this type
    switch($type)
      {
      case 2:
        $this->att["type"] = "reset";
        if(strlen($caption) != 0)
          {
          $this->att["value"] = htmlentities($caption, ENT_QUOTES);
          }
        else
          {
          $this->att["value"] = "Cancel";
          }
        break;
      case 3:
        $this->att["type"] = "button";
        if(strlen($caption) != 0)
          {
          $this->att["value"] = htmlentities($caption, ENT_QUOTES);
          }
        else
          {
          $this->att["value"] = htmlentities("Click here", ENT_QUOTES);
          }
        $this->att["OnClick()"] = $js_func;
        break;
      case 4:
        $this->att["type"] = "button";
        if(strlen($image) != 0)
          {
          $this->att["src"] = $image;
          }
        else
          {
          $this->att["src"] = "";
          }
        $this->att["OnClick()"] = $js_func;
        break;
      default:
        $this->att["type"] = "submit";
        if(strlen($caption) != 0)
          {
          $this->att["value"] = htmlentities($caption, ENT_QUOTES);
          }
        else
          {
          $this->att["value"] = "Submit";
          }
         break;
      }
    
    //Add the specific attributes to the new button
    $this->att["size"] = $taille." px";

    }

    

  /**
   * Set the button caption
   * @param caption The caption (STRING)
   * @return (BOOLEAN) True if the caption has been changed - FALSE if this is 
   *  image button and has no caption
   */

  function setCaption($caption = "")

    {
    if (strcmp($this->att["type"], "submit") == 0)
      {
      if(strlen($caption) != 0)
        {
        $this->att["value"] = $caption;
        }
      else
        {
        $this->att["value"] = "Submit";
        }
      return true;
      }
    elseif (strcmp($this->att["type"], "reset") == 0)
      {
      if(strlen($caption) != 0)
        {
        $this->att["value"] = $caption;
        }
      else
        {
        $this->att["value"] = "Cancel";
        }
      return true;
      }
    elseif ( strcmp($this->att["type"], "button") == 0 && (!isset($this->["src"]) || strlen($this->["src"]) == 0) )
      {
      if(strlen($caption) != 0)
        {
        $this->att["value"] = $caption;
        }
      else
        {
        $this->att["value"] = "Submit";
        }
      return true;
      }

    else
      {
      return false;
      }

    }
    

  /**
   * Set the button image
   * @param image The absolute filename of the image (STRING)
   * @return (BOOLEAN) True if the image has been changed - FALSE if this is 
   *  text, cancel or submit button and has a caption
   */

  function setImage($image = "")

    {

    if ( strcmp($this->att["type"], "button") == 0 && (!isset($this->["value"]) || strlen($this->["value"]) == 0) )
      {
      if(strlen($image) != 0)
        {
        $this->att["src"] = $caption;
        }
      else
        {
        $this->att["src"] = "";
        }
      return true;
      }

    else
      {
      return false;
      }

    }
 
  /**
   * Set the button size
   * @param size The button size in pixels (INTEGER)
   */

  function setSize($size)

    {

    $this->att["size"] = $size." px";

    }



  /**
   * Set the javascript function name or the all function definition called when
   * the button is clicked.
   * @param js_func The javascript function call or definition (STRING)
   * @return (BOOLEAN) True if the function has been changed - FALSE if this is 
   *  cancel or submit button and has a caption
   */

  function setJsFunc($js_func)

    {
    if(strcmp($this->att["type"], "button") == 0)
      {

      $this->att["OnClick"] = $js_func;
      return true;
      }
    else

      {

      return false;
      }
    }
    

  /**
   * Display the button within the form.
   */

  function display()

    {

    echo "<input ";

    Composant::affiche();

    echo "/>\n";

    }

  }



/**
 * This class is used to create a textbox, a password box or a text area
 */

class Text extends Component

  {

  /**
   * Constructor that initializes the text component with compulsory values
   * @param name Name of this component. Be careful that this name have to be unique within a single form (STRING)
   * @param type (INTEGER)
   *           <ul>
   *            <li>1 => a textbox </li>
   *            <li>2 => a textarea </li>
   *            <li>3 => a textbox used for password ('*' are insered during completion)</li>
   *            <li>4 => an hidden textbox</li>
   *           </ul>
   * @param default The default value (STRING)
   */
  function Text($name = "text", $type = 1, $default)

    {
    //Call the super constructor

    $this->Component($name, $default);
    
    //Choose the type and the specific attribute
    switch($type)
      {
      case 2:
        $this->box_or_area = "textarea";
        break;
      case 3:
        $this->box_or_area = "input";
        $this->att["type"] = "password";
      case 4:
        $this->box_or_area = "input";
        $this->att["type"] = "hidden";
      default:
        $this->box_or_area = "input";
        $this->att["type"] = "text";      
      }

    }
    
 /**
  * Set the textbox size. This function can only be used with textbox or password
  * @param size The textbox size in pixels (INTEGER)
  * @return (BOOLEAN) True if the size has been changed - False if it's not the
  *                   right type.
  */

  function setSize($size = 75)

    {

    if (strcmp($this->box_or_area, "textarea") != 0)

      {

      if(strcmp($this->att["type"], "hidden") != 0)
        {
        this->att["size"] = $size." px";
        return true;
        }
      else
        return false;

      }
    else
      return false;

    }
    
 /**
  * Set the maximum number of characters that a text box can contain.
  * This function can only be used with textbox or password
  * @param nb_char The maximum number of characters (INTEGER)
  * @return (BOOLEAN) True if the number has been changed - False if it's not the
  *                   right type.
  */  
  function setMax($nbChar)

    {

    if (strcmp($this->box_or_area, "textarea") != 0)

      {

      if(strcmp($this->att["type"], "hidden") != 0)
        {
        this->att["maxlength"] = $nbChar;
        return true;
        }
      else
        return false;

      }
    else
      return false;

    }


 /**
  * Set the text area size in number of rows X number of columns.
  * @param rows The number of rows (INTEGER)
  * @param cols The number of columns (INTEGER)
  * @return (BOOLEAN) True if the size has been changed - False if it's not the
  *                   right type.
  */

  function setDimension($rows, $cols)

    {

    if (strcmp($this->box_or_area, "textarea") == 0)

      {
      this->att["rows"] = $rows;

      this->att["cols"] = $cols;
      return true;
      }
    else
      return false;

    }
    
 /**
  * Set the way new line character is handled. It only works for text area
  * @param new_line This parameter can take these values :
  *           <ul>
  *            <li>1 => The text area doesn't accept new line </li>
  *            <li>2 => The text area accepts new line but they are not sent </li>
  *            <li>3 => The text area accepts new line and these are sent</li>
  *           </ul>
  * @return (BOOLEAN) True if the method has been changed - False if it's not the
  *                   right type.
  */

  function newLineHandling($new_line)
    {
    if (strcmp($this->box_or_area, "textarea") == 0)

      {
      switch($new_line)
        {
        case 1:
          $this->att["wrap"] = "off";
        case 3:
          $this->att["wrap"] = "physical";
        default:
          $this->att["wrap"] = "virtual";
        }
      return true;
      }
    else
      return false;    
    }
  
  /**
   * Display the textbox within the form.
   */

  function display()

    {

    echo "<".$this->box_or_area." ";

    Composant::affiche();

    echo "/>\n";

    }
    
 /**
  * @var box_or_area Used to store if this object is a <input/> xhtml tag or
  *      an <textarea> tag.
  */

  var $box_or_area;

  }





/**
 * This class is used to create a radio button. In opposition with other
 * components, you can have more than one option button with the same name.
 * In that case, the value sent to the server will be the one contained in 
 * $data_sent of the selected radio button.
 */
class Option extends Component

  {

 /**
  * Constructor that initializes the button with compulsory values
  * @param name Name of this button. (STRING)
  * @param caption The caption displayed near the radio button (STRING)
  * @param data_sent The string that's sent when the form is submited if the option is selected.
  */

  function Option($name = "option", $caption="Option", $data_sent="Option selected")

    {

    $this->Component($name);
    $this->att["type"] = "radio";
    $this->att["value"] = $data_sent;
    $this->caption = $caption;

    }
    

 /**
  * Set the button which will be selected by default among all the buttons 
  * that have the same name.
  * @param $selected True if the button has to be selected - False if not (BOOLEAN)
  */
  function selected($selected = true)

    {
    if($selected)

      $this->selected = "checked";
    else
      $this->selected = "";

    }
    

 /**
  * Set the data which will be sent if the button is selected
  * @param data_sent The data that 'll be sent to the server (STRING)
  */
  function setDataSent($data_sent)

    {

    $this->att["value"] = $data_sent;

    }
    
 /**
  * Display the radio button within the form.
  */

  function display()

    {

    echo "<input ";

    Composant::affiche();

    echo $this->selected." /> ".$this->caption."\n";

    }
   

 /**
  * @var caption The text value that follows the radio button. (STRING)
  */

  var $caption;
  

 /**
  * @var caption Flag if the radio button must be selected by default. (STRING)
  */

  var $selected;

  }



/**
 * This class is used to create a check button.
 */
class Check extends Component

  {

 /**
  * Constructor that initializes the button with compulsory values
  * @param name Name of this button. Be careful that this name have to be unique within a single form (STRING)
  * @param caption The caption displayed near the check button (STRING)
  * @param data_sent The string that's sent when the form is submited if the option is checked.
  */

  function Check($name = "check", $caption="Check", $data_sent="Option Checked")

    {

    $this->Component($name);
    $this->att["type"] = "checkbox";
    $this->att["value"] = $data_sent;
    $this->caption = $caption;

    }
    

 /**
  * Check or Uncheck the button
  * @param $selected True if the button has to be checked - False if not (BOOLEAN)
  */
  function checked($checked = true)

    {
    if($checked)

      $this->checked = "checked";
    else
      $this->checked = "";

    }
    

 /**
  * Set the data which will be sent if the button is checked
  * @param data_sent The data that 'll be sent to the server (STRING)
  */
  function setDataSent($data_sent)

    {

    $this->att["value"] = $data_sent;

    }
    
 /**
  * Display the check button within the form.
  */

  function display()

    {

    echo "<input ";

    Composant::affiche();

    echo $this->checked." /> ".$this->caption."\n";

    }
   

 /**
  * @var caption The text value that follows the radio button. (STRING)
  */

  var $caption;
  

 /**
  * @var caption Flag if the radio button must be selected by default. (STRING)
  */

  var $selected;

  }




/**
 * This class is used to create a dropdown list
 */
class Listbox extends Component

  {

 /**
  * Constructor that initializes all the compulsory attributes.
  * @param name The dropdown list name. Be careful that this name have to be unique within a single form (STRING)
  * @param width The dropdown list width. It's given in pixels (INTEGER)
  * @param options The list of options contained in the list (KEYED ARRAY of STRING)
  *                where the key is the text contained in the list and the value is the one that
  *                will be sent to the server.
  * @param displayed_options The number of list items that will be shown to the user. (INTEGER)
  * @param multiple True if the selection can be multiple - False if not (BOOLEAN)
  */
  function Listbox($name = "list", $width= 75, $options = array(), $displayed_options = 5, $multiple = "false")

    {

    $this->Composant($name);

    $this->options = $options;
    $this->att["size"] = $displayed_options;
    $this->multiple = $multiple;
    $this->width = $width." px";

    }
    
 /**
  * Set the different items contained in the dropdown list.
  * @param options The list of options contained in the list (KEYED ARRAY of STRING)
  *                where the key is the text contained in the list and the value is the one that
  *                will be sent to the server.
  */

  function setOptions($options = array())
    {
    $this->options = $options;
    }
    
 /**
  * Set the list item that will be selected when the form is displayed.
  * @param index The index number of the option that should be selected. (INTEGER)
  * @return (BOOLEAN) True if the item index has been found - False if the index is out of bounds.
  */

  function setSelectedItem($index = 0)

    {

    if(is_array($this->options))

      {

      if ($index>=1 && $index<=count($this->options))

        {

        $this->default = $index;
        return true;

        }
      else
        return false;

      }
    else
      return false;

    }
    
 /**
  * Set if the user can select more than one item in the list or not.
  * @param multiple True if the selection can be multiple - False if not (BOOLEAN)
  */

  function setMultiple($multiple = "false")

    {

    $this->multiple = $multiple;

    }
    
 /**
  * Set the number of item that are shown to the user when the list is displayed.
  * @param displayed_options The number of list items (INTEGER)
  */

  function setDisplayedOptions($displayed_options = 5)

    {

    $this->att["size"] = $displayed_options;

    }
    
 /**
  * Set the list width.
  * @param width The dropdown list width. It's given in pixels (INTEGER)
  */

  function setWidth($width = 75)

    {

    $this->width = $width." px";

    }

  

 /**
  * Display the dropdown list.
  */

  function display()

    {

    //Display the <select> xhtml tag

    echo "<select ";
    //Display the component attributes
    Component::display();

    if($this->multiple)

      {

      echo "multiple ";

      }

    echo ">\n";



    //Display all the options

    if(is_array($this->options))

      {
      //Point to the first element
      reset($this->options);
      //Set the pointer position to start
      $pos = 0;
      //Loop through the array
      while(list($text, $value) = each($options))
        {
        //Display the xhtml <option> tag
        echo "<option value=\"".$value."\" width=\"".$this->width."\" ";
        //If the current position in the array is the default option, write it down.
        if($this->default == $pos)
          {
          echo "selected ";
          }
        echo ">\n";
        //Increment the position counter
        $pos++;
        //Display the option caption
        echo $text."\n";
        echo "</option>\n";
        }
      }
    //Display the closing xhtml tag.

    echo "</select>\n";

    }
    

 /**
  * @var multiple True if the selection can be mutliple - False if not (BOOLEAN)
  */

  var $multiple;
  

 /**
  * @var options The list of options contained in the list (KEYED ARRAY of STRING)
  */

  var $options = array();
  

 /**
  * @var default The option that will be selected by default when the list is displayed (INTEGER)
  */

  var $default;
  

 /**
  * @var width The dropdown list width. It's given in pixels (INTEGER)
  */

  var $width;

  }


/**
 * This class is used to create a form. Every component can be added to this form.
 */

class Form extends Component

  {

  /**
   * Constructor that initializes the form with compulsory values
   * @param name Name of this button. Be careful that this name have to be unique within a single page (STRING)
   * @param type (INTEGER)
   *           <ul>
   *            <li>
   *              1 => the method used to send the form data is POST. (Default)
   *                   These values can be used with $_POST["name"] = value
   *            </li>
   *            <li>
   *              2 => the method used to send the form data is GET. The data are
   *                   send through the URL.
   *                   These values can be used with $_GET["name"] = value
   *            </li>
   *           </ul>
   * @param script The absolute or relative path to the script that will handle the form data (STRING)
   */

  function Form($name, $type, $script)

    {
    $this->Component($name);

    $this->components = array();
    switch($type)
      {
      case 2:
        $this->att["method"] = "get"
      default:
        $this->att["method"] = "post"
      }

    $this->att["action"] = $script;

    }
    
 /**
  * Add a component to the form.
  * @param component A Component object (Button, Listbox, etc.) (COMPONENT CLASS)
  * @param br True if a newline character should be displayed just after this component - False if not (BOOLEAN)
  * @return (BOOLEAN) True if the component has been added - False if an error has occured
  */

  function addComponent($component, $br)
    {
    if(isset($component))
      {
      if($br)
        $comp = array($component, "<br/>");
      else
        $comp = array($component, "");
      $this->components = array_push($this->components, $comp);
      return true;
      }
    else
      return false;
    }
    

 /**
  * Remove a component from the form.
  * @param index The index number of the componant that should be removed. (INTEGER)
  * @return (BOOLEAN) True if the component has been removed - False if an error has occured
  */

  function removeComponent($index)
    {
    if(isset($index) && $index >= 1 && $index<=count($this->components))
      {
      array_splice($this->components, $index-1, 1);
      return true;
      }
    else
      return false;
    }
    
 /**
  * Remove all components from the form.
  */

  function removeComponents()
    {
    this->components = array();
    }
    
 /**
  * Display the form
  */
  function display()

    {

    echo "<form ";
    Component::display();

    echo ">\n";

    for($i=0; $i<count($this->components); $i++)

      {

      $this->components[$i][0]->display();

      if($this->composants[$i][1])

        {

        echo "<br/>\n";

        }

      }

    echo "</form>\n";

    }

  }



?>

 Conclusion

Cette version n'a pas encore été testée en production. Peut contenir certains légers bugs. Merci de m'en faire part si vous en rencontrez.

La possibilité d'insérer des procédures de validations soit via javascript, soit via php sont pour l'instant à l'étude.

 Fichier Zip

Les Membres Club peuvent télécharger directement un fichier contenu dans le zip sans télécharger le zip en entier !

Télécharger le zip


 Historique

28 juillet 2005 20:09:58 :
Refonte totale de cette source. Parmi les améliorations, on peut noter : * Meilleure gestion des attributs des tags HTML * Possibilités d'ajout et de suppression d'attributs non gérés en standart * Simplification profonde de la classe Form

 Sources du même auteur

CLASS D'ENSEMBLE DE FICHIERS À TÉLÉCHARGER
Source avec Zip CLASS DE CONNEXION MYSQL
CLASS D'OBJET TEXTE
Source avec Zip CLASS DE TABLEAU HTML

 Sources de la même categorie

CLASSE DE GESTION DE "VARIABLES GLOBALES D'ENVIRONNEMENT" par pifou25
Source avec Zip COLLECTION.CLASS.MIN.PHP par thunderhunter
Source avec Zip SIMPLETEMPLATE par thunderhunter
Source avec Zip Source avec une capture VOIR QUI VISITE VOTRE SITE par Dariumis
Source avec Zip CLASS SIMPLE CBASEDONNEE par smag42

Commentaires et avis

Commentaire de lumesh le 23/12/2004 11:44:57

Ah zut je n'avais pas vu ta source, j'ai posté à l'instant une petite source avec plein de fonctions pour faire des formulaires .... oups bah dsl.

Commentaire de GRenard le 30/12/2004 17:27:23

Salut, j'ai un petit reproche à effectuer :) bien que ta source est cool car j'en fait autant pour mon site.
Tu as mis beaucoup trop de paramêtres pour faire par exemple un listbox... (j'ai vu, largeur, class, ... )
C'est TROP :) Pourquoi?
Si jamais moi je désire rajouter un attribut que tu n'as pas mis, je suis obliger d'aller modifier ton code. Le best dans tout ca alors c'est de dire toi même quel attribut avec quelle valeur tu veux lui associer...
Tu peux aller voir sur le script suivant : http://www.phpcs.com/code.aspx?ID=25632
Regarde le fonctionnement de addCellAttribute(), c'est exactement ce que je te parle :) On ajoute l'attribut, et sa valeur après. Ainsi on peut en ajouter autant qu'on veut !

C'est bien pensé quand même :)
See ya !

Commentaire de Nerdz le 03/01/2005 17:50:46

Quel est l'avantage d'utiliser une class pour les formulaires ?? C'est pas plus compliquer que de mettre un simple input ?

Commentaire de GRenard le 05/01/2005 08:19:09

Tu n'as plus besoin de connaitre le HTML... tu ne fais que du PHP et de la programmation object... Pourquoi classe plutot que fonction ? Tu peux très bien tout faire en fonctions, par contre l'avenir pousse à utiliser les classes car elle peut contenir des méthodes qui regroupent EXACTEMENT les "fonctions" que tu veux.
Par contre lit mon commentaire plus haut, il y a quelques petits ajustement à faire...

Commentaire de gomoz le 16/04/2006 04:19:40

tiens j'avais jamais pensé à faire une class pour un formulaire... mais c'est pas bête en fait. Je m'en servirai à l'avenir :)

 Ajouter un commentaire




Nos sponsors


Sondage...

CalendriCode

Février 2012
LMMJVSD
  12345
6789101112
13141516171819
20212223242526
272829    

Consulter la suite du CalendriCode

Photothèque

 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 2,980 sec (3)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales