Login   Register  
PHP Classes
elePHPant
Icontem

File: yumaforms.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Marco van Hylckama Vlieg  >  YumaForms  >  yumaforms.php  >  Download  
File: yumaforms.php
Role: ???
Content type: text/plain
Description: The class + an example of usage
Class: YumaForms
Author: By
Last change:
Date: 2001-01-27 14:32
Size: 18,445 bytes
 

Contents

Class file image Download
<?
/*************************************************************/
/* YumaForm Form generation Class                            */
/* Copyright 2001 (c) YumaNet S.A.                           */
/* email:yumanet@yumanet.com                                 */
/*                                                           */
/* This code may be used and modified freely as long as this */
/* notice stays intact. If you modify/enhance/improve this   */
/* code, I would appreciate a copy of what you did.          */
/*                                                           */
/* Author: Marco van Hylckama Vlieg                          */
/* marco@windowmaker.org                                     */
/*                                                           */
/* LINKWARE                                                  */
/* --------                                                  */
/* If you like this code and use it, we would appreciate it  */
/* greatly if you would visit our portal about the           */
/* Dominican Republic, or even better, place a link to it on */
/* your own website.                                         */
/*                                                           */
/* http://www.zonacolonial.com/                              */
/*                                                           */
/*-----------------------------------------------------------*/
/*                                                           */
/* Credits                                                   */
/*                                                           */
/* Javascript popup calendar programmed by:                  */
/* Sev Kotchnev                                              */
/* email: webmaster@personal-connections.com                 */
/*                                                           */        
/*************************************************************/

Class YumaForm  {

  // form attributes

  var $name;           // element name
  var $formname;       // form name, double, needed for datebox();
  var $method;         // form method
  var $action;         // form action
  var $size;           // size of a widget
  var $password;       // when "YES" textfield becomes passwordfield
  var $maxlen;         // max lenght for textfield
  var $cols;           // amount of columns for a textarea
  var $rows;           // amount of rows for a textares
  var $disabled;       // when "YES" element is disabled
  var $readonly;       // when "YES" element is readonly
  var $jsevents;       // javascript event attached to element
  var $selected;       // single or array variable that holds selected item(s)
  var $checked;        // whether checkbox or radio button is checked
  var $items;          // associative array for hidden fields (key => value)
  var $list;           // associative array for listbox items (key => value)
  var $wrap;           // when "YES", a textarea will wrap text
  var $simage;         // when this is set, the submit button is graphical
  var $submitlabel;    // text for on submitbutton
  var $resetlabel;     // text for on resetlabel

  // style attributes

  var $font_type;
  var $font_size;
  var $font_color;
  var $font_weight;
  var $background_color;
  var $border_style;
  var $border_width;

  function FinishForm()  {

    if ($this->finish == "YES")  {
      echo "\n</form>\n";
      }
    }

  function YumaForm($parameters)  {

    $this->default_form_name                                  = "yumaform";
    $this->default_method                                     = "POST";
    $this->default_action                                     = "noaction.php";
    $this->default_textfield_size                             = 20;

    $this->default_textarea_cols                              = 30;
    $this->default_textarea_rows                              = 5;
    $this->default_textarea_wrap                              = "YES";

    $this->default_multilist_size                             = 3;

    $this->default_submit_label                               = "Send YF";

    for ($i=0;$i<sizeof($parameters);$i++)  {
      $key  = key($parameters);
      $item = current($parameters);
      $this->$key = $item;
      next($parameters);
    }
  }

  function SetStyle()   {

    if (($this->font_type        != "") ||
        ($this->font_size        != "") ||
        ($this->font_color       != "") ||
        ($this->font_weight      != "") ||
        ($this->background_color != "") ||
        ($this->border_style     != "") ||
        ($this->border_width     != "")) {
      $style = "YES";
    }

    if ($style == "YES") {
      echo " style=\"";
    }

    if ($this->font_type != "") {
      echo "font-family: $this->font_type;";
    }
    if ($this->font_size != "") {
      echo "font-size: $this->font_size;";
    }
    if ($this->font_color != "") {
      echo "color: $this->font_color;";
    }
    if ($this->font_weight != "") {
      echo "font-weight: $this->font_weight;";
    }
    if ($this->background_color != "") {
      echo "background-color: $this->background_color;";
    }
    if ($this->border_style != "") {
      echo "border-style: $this->border_style;";
    }
    if ($this->border_width != "") {
      echo "border-width: $this->border_width;";
    }
    if ($style == "YES") {
      echo "\"";
    }
  }

  function FormHead()   {

    if ($this->method == "") {
      $this->method = $this->default_method;
    }
    if ($this->action == "") {
      $this->action = $this->default_action;
    }
    if ($this->name == "") {
      $this->name = $this->default_form_name;
    }
    echo "<form method=\"".$this->method."\" name=\"".$this->name."\" action=\"".$this->action."\"";
    if ($this->jsevents != "") :
      echo " ".$this->jsevents.">\n";
    else :
      echo ">\n";
    endif;
  }

  function TextInput()  {

    if ($this->size == "") {
      $this->size = $this->default_textfield_size;
    }
    if ($this->password != "") :
      echo "<input type=\"password\"";
    else :
      echo "<input type=\"text\"";
    endif;
    echo " name=\"".$this->name."\"";
    echo " size=\"".$this->size."\"";
    if ($this->disabled != "") :
      echo " disabled";
    endif;
    if ($this->readonly != "") :
      echo " readonly";
    endif;
    if ($this->maxlen != "") :
      echo " maxlength=\"".$this->maxlen."\"";
    endif;
    echo " value=\"".$this->value."\"";
    if ($this->jsevents != "") :
      echo " ".$this->jsevents;
    endif;
    $this->SetStyle();
    echo ">\n";
    $this->FinishForm();
    }

  function TextArea()  {

    if ($this->cols == "") {
      $this->cols = $this->default_textarea_cols;
      }
    if ($this->rows == "") {
      $this->rows = $this->default_textarea_rows;
      }
    if ($this->wrap == "") {
      $this->wrap = $this->default_textarea_wrap;
      }
    echo "<textarea name=\"".$this->name."\" rows=\"".$this->rows."\" cols=\"".$this->cols."\"";
    if ($this->disabled == "YES") {
      echo " disabled";
    }
    if ($this->readonly == "YES") {
      echo " readonly";
    }
    if ($this->wrap == "YES") {
      echo " WRAP";
    }
    if ($this->jsevents != "") {
      echo " ".$this->jsevents;
    }
    $this->SetStyle();
    echo ">".$this->value."</textarea>\n";
    $this->FinishForm();
  }

  function CheckBox()  {

    echo "<input type=\"checkbox\" name=\"".$this->name. "\" value=\"".$this->value. "\" ";
    if ($this->checked == "YES") {
      echo " checked";
    }
    if ($this->disabled == "YES") {
      echo " disabled";
    }
    if ($this->jsevents != "") {
      echo " ".$this->jsevents;
    }
    $this->SetStyle();
    echo ">";
    $this->FinishForm();
  }

  function CheckBoxGroup()  {

    if ($this->checked == "") {
      $this->checked = array();
    }
    if ($this->disabled == "") {
      $this->disabled = array();
    } 
      
    for ($i=0;$i<sizeof($this->list);$i++)  {
      $thisname = current($this->names);
      echo "<input type=\"checkbox\" name=\"".$thisname."\"
value=\"".key($this->list)."\"";
        if (in_array(key($this->list), $this->checked)) {
          echo " checked";
        }
        if (in_array(key($this->list), $this->disabled)) {
          echo " disabled";
        }
        $this->SetStyle();
        echo ">".current($this->list)."<br>\n";
        next($this->names);
        next($this->list);
    }
  }


  function RadioGroup()  {

    for ($i=0;$i<sizeof($this->list);$i++)  {
      echo "<input type=\"radio\" name=\"".$this->name."\" value=\"".key($this->list)."\"";
      if ($this->jsevents != "") {
        echo " ".$this->jsevents;
      }
      if ($this->disabled != "") {
        echo " disabled";
      }
      $this->SetStyle();
      if ($this->checked == key($this->list))  :
        echo " checked>".current($this->list)."<br>\n";
      else :
        echo ">".current($this->list)."<br>\n";
      endif;
      next($this->list);
    }
    $this->FinishForm();
  }

  function MultiList()  {

  if ($this->selected == "") { 
    $this->selected = array();
  }

  if ($this->size == "") {
    $this->size = $this->default_multilist_size;
  }

    echo "<select name=\"".$this->name."\" size=\"".$this->size."\" multiple";
    if ($this->disabled != "") {
      echo " disabled";
    }
    if ($this->jsevents != "") {
      echo " ".$this->jsevents;
    }
    $this->SetStyle();
    echo ">\n";
    for ($i=0;$i<sizeof($this->list);$i++)  {
      echo "<option value=\"".key($this->list)."\"";
      if (in_array(key($this->list),$this->selected)) {
        echo " selected";
      }
    echo ">".current($this->list)."\n";
    next($this->list);
    }
    echo "</select>\n";
    $this->FinishForm();
  }    

  function SingleList()  {

    echo "<select name=\"".$this->name."\"";
    if ($this->disabled != "") {
      echo " disabled";
    }
    if ($this->jsevents != "") {
      echo " ".$this->jsevents;
    }
    $this->SetStyle();
    echo ">\n";
    for ($i=0;$i<sizeof($this->list);$i++)  {
      echo "<option value=\"".key($this->list)."\"";
      if ($this->selected == key($this->list)) {
        echo " selected";
      }
    echo ">".current($this->list)."\n";
    next($this->list);
    }
    echo "</select>\n";
    $this->FinishForm();
  }

  function FileUpload()  {
  
  echo "<input type=\"file\" name=\"".$this->name."\">";
  
  }

  function DateBox()  {

    echo "<script language=\"javascript\" src=\"calendar1.inc\"></script>\n";
    echo "<input type=\"text\" name=\"".$this->name."\" size=\"10\" onFocus=\"getCalendarFor(this); this.blur()\"";
    $this->SetStyle();
    echo">";
    echo "<input type=\"button\" value=\"..\"";
    echo " onClick=\"getCalendarFor(document.".$this->formname.".".$this->name.")\">";
    echo "\n";
 }

  function AddJS()  {
    include "calendar2.inc";
  }

  function Buttons()  {


    if ($this->submitlabel == "") {
      $this->submitlabel = $this->default_submit_label;
    }

    if ($this->simage != "") :
      echo "<input type=\"image\" src=\"".$this->image."\"";
    else :
      echo "<input type=\"submit\" value=\"".$this->submitlabel."\"";
    endif;
    if ($this->jsevents != "") {
      echo " ".$this->jsevents;
    }
    echo ">";

    if ($this->resetlabel != "")  {
      echo "<input type=\"reset\" value=\"".$this->resetlabel."\"";
      echo ">";
    }
    $this->FinishForm();
  }

  function HiddenFields()  {

    for ($i=0;$i<sizeof($this->items);$i++)  {
      echo "<input type=\"hidden\" name=\"".key($this->items)."\" value=\"".current($this->items)."\">\n";
      next($this->items);
      }
    $this->FinishForm();
  }
}


























/* Usage Example */

/* set parameters */

echo "Below you see an example of what you can do with the YumaForms class.  Check the source to see how it works!";

$formparameters = array ("name"   => "exampleform",
                         "method" => "GET");


$textfieldparameters = array ("name"             => "form_name",
                              "font_type"        => "verdana",
                              "font_color"       => "#FF0000",
                              "font_weight"      => "bold",
                              "background_color" => "#CCCCCC",
                              "border_style"     => "ridge",
                              "value"            => "type here");


$textareaparameters = array ("name"             => "form_textarea",
                             "rows"             => 7,
                             "background_color" => "#FFCCCC",
                             "border_style"     => "groove",
                             "font_type"        => "Verdana",
                             "value"            => "This is a textfield, type as you like.",
                             "cols"             => 30);

$checkboxparameters = array ("name"        => "form_checkbox",
                             "checked"     => "YES",
                             "value"       => 5);

$radioparameters = array ("label"       => "This is a Radio set",
                          "name"        => "form_radio",
                          "list"        => array (1 => "Radio 1",
                                                  2 => "Radio 2",
                                                  3 => "Radio 3"),
                          "checked"     => 3);
                            
$multilistparameters = array ("label"            => "This is a multiselect box",
                              "name"             => "form_multi",
                              "font_type"        => "Arial",
                              "font_weight"      => "bold",
                              "background_color" => "#DDDDDD",
                              "list"             => array (1 => "Option one",
                                                           2 => "Option two",
                                                           3 => "Option three",
                                                           4 => "Option four",
                                                           5 => "Option five",
                                                           6 => "Option six",
                                                           7 => "Option seven"),
                              "selected"         => array (2,5));

$singlelistparameters = array ("label"      => "This is a select box",
                               "name"       => "form_select",
                               "list"       => array (
                                                               1 => "Option one",
                                                               2 => "Option two",
                                                               3 => "Option three",
                                                               4 => "Option four",
                                                               5 => "Option five",
                                                               6 => "Option six",
                                                               7 => "Option seven"),
                               "selected"   => 4,
                               "font_color" => "#005550",
                               "jsevents"   => "onChange=\"DoSomething();\"");

$fileparameters = array ("name"          => "form_file");

$dateparameters = array ("name"          => "form_date",
                         "formname"      => "exampleform");
$date2parameters = array ("name"          => "form_date2",
                          "formname"      => "exampleform");

$buttonparameters = array ("resetlabel"  => "Reset Form",
                           "finish"      => "YES");

$hiddenparameters = array ("items"   => array ("marco"   => "maaike",
                                               "jihad"   => "joelle",
                                               "olivier" => "katia"));
$checkgroupparameters = array ("label"          => "Check these options",
                               "names"          => array ("check1","check2","check3"),
                               "checked"        => array (1),
                               "disabled"       => array (1),
                               "list"           => array (1 => "I have children",
                                                          2 => "I have a dog",
                                                          3 => "I want a sea view"));
/* create objects */

$myFormHead = new YumaForm($formparameters);
$myTextField = new YumaForm($textfieldparameters);
$myTextArea = new YumaForm($textareaparameters);
$myCheckBox = new YumaForm($checkboxparameters);
$myRadioGroup = new YumaForm($radioparameters);
$myMultiList = new YumaForm($multilistparameters);
$mySingleList = new YumaForm($singlelistparameters);
$myFileUpload = new YumaForm($fileparameters);
$myDate = new YumaForm($dateparameters);
$myDate2 = new YumaForm($date2parameters);
$myCheckGroup = new YumaForm($checkgroupparameters);
$myHiddenFields = new YumaForm($hiddenparameters);
$myButtons = new YumaForm($buttonparameters);

/* generate output */
?>
<html>
<head>
<title>YumaForms</title>
</head>
<body>
<?
$myFormHead->FormHead();
echo "<p>\n";
echo "Here is a text box&nbsp;";
$myTextField->TextInput();
echo "<p>\n";
$myTextArea->TextArea();
echo "<p>\n";
$myCheckBox->CheckBox();
echo "you could check this box...";
echo "<p>\n";
$myRadioGroup = $myRadioGroup->RadioGroup();
echo "<p>\n";
$myMultiList->MultiList();
echo "<p>\n";
$mySingleList->SingleList();
echo "<p>\n";
$myFileUpload->FileUpload();
echo "<p>\n";
$myDate->DateBox();
$myDate2->DateBox();
echo "<p>\n";
$myCheckGroup->CheckBoxGroup();
$myHiddenFields->HiddenFields();
echo "<p>\n";
$myButtons->Buttons();
$myDate->AddJS(); // You have to add this right before the </body> tag for the date popup to function.
?>

</body>
</html>