SCPHP
[ class tree: SCPHP ] [ index: SCPHP ] [ all elements ]

Source for file ScForms.php

Documentation is available at ScForms.php

  1. <?php
  2. /**
  3.  * SCPHP
  4.  *
  5.  * An open source SmartClient library for PHP
  6.  *
  7.  * @package        SCPHP
  8.  * @author        Fernando Marcelo Morgenstern <fernando@consultorpc.com>
  9.  * @copyright    Copyright (c) 2009, ConsultorPC
  10.  * @license        http://www.gnu.org/licenses/lgpl-3.0-standalone.html
  11.  * @link        http://smartclientphp.com/
  12.  * @since        Version 0.1b
  13.  * @filesource
  14.  */
  15.  
  16.  
  17. /**
  18.  * Smart Client Forms Helper
  19.  *
  20.  */
  21. class ScForms extends ScGeneral  
  22. {
  23.     
  24.     /**
  25.      * @var array 
  26.      */
  27.     protected  $_fields;
  28.     
  29.     /**
  30.      * @var string Default title orientation for the form fields
  31.      */
  32.     protected  $_defaultTitleOrientation = 'left';
  33.  
  34.     /**
  35.      * 
  36.      * Add Button
  37.      *
  38.      * @param mixed $data If string, it must be the title of the field. If array, them we will loop on it
  39.      * @param array $options Additional options
  40.      * 
  41.      */
  42.     public function addButton$data $click '' $options '' )
  43.     {
  44.         $newField array();
  45.         
  46.         // Check if it is an array
  47.         if is_array$data ) )
  48.         {
  49.             $newField $data;
  50.         }
  51.         else
  52.         {
  53.             // Add vars to the array
  54.             $newField['title'$data;
  55.             $newField['click'$click;
  56.             $newField['type''button';
  57.             
  58.             // Set default value for titleOrientation if it is not set
  59.             if empty$options['titleOrientation') )
  60.             {
  61.                 $options['titleOrientation'$this->_defaultTitleOrientation;
  62.             }
  63.             
  64.             // Check if options is array and do the loop
  65.             if is_array$options ) )
  66.             {
  67.                 foreach $options as $name => $value )
  68.                 {
  69.                     $newField[$name$value;
  70.                 }
  71.             }
  72.         }
  73.         
  74.         // Add field to the array
  75.         $this->_fields[$newField;
  76.     }
  77.     
  78.     /**
  79.      * 
  80.      * Add Checkbox Field
  81.      *
  82.      * @param mixed $data If string, it must be the name of the field. If array, them we will loop on it
  83.      * @param string $title Title of the field
  84.      * @param boolean $checked Default value
  85.      * @param array $options Additional options
  86.      * 
  87.      */
  88.     public function addCheckboxField$data $title '' $checked false $options '' )
  89.     {
  90.         $newField array();
  91.         
  92.         // Check if it is an array
  93.         if is_array$data ) )
  94.         {
  95.             $newField $data;
  96.         }
  97.         else
  98.         {
  99.             // Add vars to the array
  100.             $newField['name'$data;
  101.             $newField['title'$title;
  102.             $newField['defaultValue'$checked;
  103.             $newField['type''checkbox';
  104.             
  105.             // Set default value for titleOrientation if it is not set
  106.             if empty$options['titleOrientation') )
  107.             {
  108.                 $options['titleOrientation'$this->_defaultTitleOrientation;
  109.             }
  110.             
  111.             // Check if options is array and do the loop
  112.             if is_array$options ) )
  113.             {
  114.                 foreach $options as $name => $value )
  115.                 {
  116.                     $newField[$name$value;
  117.                 }
  118.             }
  119.         }
  120.         
  121.         // Add field to the array
  122.         $this->_fields[$newField;
  123.     }
  124.     
  125.     /**
  126.      * 
  127.      * Add Date Field
  128.      *
  129.      * @param mixed $data If string, it must be the name of the field. If array, them we will loop on it
  130.      * @param string $title Title of the field
  131.      * @param string $defaultValue Default value
  132.      * @param boolean $useTextField Use a text field
  133.      * @param string $dateDisplayFormat Display format
  134.      * @param string $startDate Start date
  135.      * @param string $endDate End date
  136.      * @param array $options Additional options
  137.      * 
  138.      */
  139.     public function addDateField$data $title '' $defaultValue '' $useTextField false $dateDisplayFormat '' $startDate '' $endDate '' $options '' )
  140.     {
  141.         $newField array();
  142.         
  143.         // Check if it is an array
  144.         if is_array$data ) )
  145.         {
  146.             $newField $data;
  147.         }
  148.         else
  149.         {
  150.             // Add vars to the array
  151.             $newField['name'$data;
  152.             $newField['title'$title;
  153.             $newField['defaultValue'$defaultValue;
  154.             $newField['useTextField'$useTextField;
  155.             $newField['dateDisplayFormat'$dateDisplayFormat;
  156.             $newField['startDate'$startDate;
  157.             $newField['endDate'$endDate;
  158.             $newField['type''date';
  159.             
  160.             // Set default value for titleOrientation if it is not set
  161.             if empty$options['titleOrientation') )
  162.             {
  163.                 $options['titleOrientation'$this->_defaultTitleOrientation;
  164.             }
  165.             
  166.             // Check if options is array and do the loop
  167.             if is_array$options ) )
  168.             {
  169.                 foreach $options as $name => $value )
  170.                 {
  171.                     $newField[$name$value;
  172.                 }
  173.             }
  174.         }
  175.         
  176.         // Add field to the array
  177.         $this->_fields[$newField;
  178.     }
  179.     
  180.     /**
  181.      * 
  182.      * Add Header
  183.      *
  184.      * @param mixed $data If string, it must be the value of the field. If array, them we will loop on it
  185.      * @param array $options Additional options
  186.      * 
  187.      */
  188.     public function addHeader$data $options '' )
  189.     {
  190.         $newField array();
  191.         
  192.         // Check if it is an array
  193.         if is_array$data ) )
  194.         {
  195.             $newField $data;
  196.         }
  197.         else
  198.         {
  199.             // Add vars to the array
  200.             $newField['defaultValue'$data;
  201.             $newField['type''header';
  202.             
  203.             // Set default value for titleOrientation if it is not set
  204.             if empty$options['titleOrientation') )
  205.             {
  206.                 $options['titleOrientation'$this->_defaultTitleOrientation;
  207.             }
  208.             
  209.             // Check if options is array and do the loop
  210.             if is_array$options ) )
  211.             {
  212.                 foreach $options as $name => $value )
  213.                 {
  214.                     $newField[$name$value;
  215.                 }
  216.             }
  217.         }
  218.         
  219.         // Add field to the array
  220.         $this->_fields[$newField;
  221.     }
  222.     
  223.     /**
  224.      * 
  225.      * Add Hidden Field
  226.      *
  227.      * @param mixed $data If string, it must be the name of the field. If array, them we will loop on it
  228.      * @param string $defaultValue Default value for the hidden field
  229.      * @param array $options Additional options
  230.      * 
  231.      */
  232.     public function addHiddenField$data $defaultValue '' $options '' )
  233.     {
  234.         $newField array();
  235.         
  236.         // Check if it is an array
  237.         if is_array$data ) )
  238.         {
  239.             $newField $data;
  240.         }
  241.         else
  242.         {
  243.             // Add vars to the array
  244.             $newField['name'$data;
  245.             $newField['defaultValue'$defaultValue;
  246.             $newField['type''hidden';
  247.             
  248.             // Set default value for titleOrientation if it is not set
  249.             if empty$options['titleOrientation') )
  250.             {
  251.                 $options['titleOrientation'$this->_defaultTitleOrientation;
  252.             }
  253.             
  254.             // Check if options is array and do the loop
  255.             if is_array$options ) )
  256.             {
  257.                 foreach $options as $name => $value )
  258.                 {
  259.                     $newField[$name$value;
  260.                 }
  261.             }
  262.         }
  263.         
  264.         // Add field to the array
  265.         $this->_fields[$newField;
  266.     }
  267.     
  268.     /**
  269.      * 
  270.      * Add Spinner Field
  271.      *
  272.      * @param mixed $data If string, it must be the name of the field. If array, them we will loop on it
  273.      * @param string $title Title of the field
  274.      * @param float $min Min value
  275.      * @param float $max Max value
  276.      * @param float $step Step value
  277.      * @param float $defaultValue Default value
  278.      * @param array $options Additional options
  279.      * 
  280.      */
  281.     public function addSpinnerField$data $title '' $min '' $max '' $step '' $defaultValue '' $options '' )
  282.     {
  283.         $newField array();
  284.         
  285.         // Check if it is an array
  286.         if is_array$data ) )
  287.         {
  288.             $newField $data;
  289.         }
  290.         else
  291.         {
  292.             // Add vars to the array
  293.             $newField['name'$data;
  294.             $newField['title'$title;
  295.             $newField['min'$min;
  296.             $newField['max'$max;
  297.             $newField['step'$step;
  298.             $newField['defaultValue'$defaultValue;
  299.             $newField['type''spinner';
  300.             
  301.             // Set default value for titleOrientation if it is not set
  302.             if empty$options['titleOrientation') )
  303.             {
  304.                 $options['titleOrientation'$this->_defaultTitleOrientation;
  305.             }
  306.             
  307.             // Check if options is array and do the loop
  308.             if is_array$options ) )
  309.             {
  310.                 foreach $options as $name => $value )
  311.                 {
  312.                     $newField[$name$value;
  313.                 }
  314.             }
  315.         }
  316.         
  317.         // Add field to the array
  318.         $this->_fields[$newField;
  319.     }
  320.     
  321.     /**
  322.      * 
  323.      * Add Password Field
  324.      *
  325.      * @param mixed $data If string, it must be the name of the field. If array, them we will loop on it
  326.      * @param string $title Title of the field
  327.      * @param string $defaultValue Default value for the text area
  328.      * @param array $options Additional options
  329.      * 
  330.      */
  331.     public function addPasswordField$data $title '' $defaultValue '' $options '' )
  332.     {
  333.         $newField array();
  334.         
  335.         // Check if it is an array
  336.         if is_array$data ) )
  337.         {
  338.             $newField $data;
  339.         }
  340.         else
  341.         {
  342.             // Add vars to the array
  343.             $newField['name'$data;
  344.             $newField['title'$title;
  345.             $newField['defaultValue'$defaultValue;
  346.             $newField['type''password';
  347.             
  348.             // Set default value for titleOrientation if it is not set
  349.             if empty$options['titleOrientation') )
  350.             {
  351.                 $options['titleOrientation'$this->_defaultTitleOrientation;
  352.             }
  353.             
  354.             // Check if options is array and do the loop
  355.             if is_array$options ) )
  356.             {
  357.                 foreach $options as $name => $value )
  358.                 {
  359.                     $newField[$name$value;
  360.                 }
  361.             }
  362.         }
  363.         
  364.         // Add field to the array
  365.         $this->_fields[$newField;
  366.     }
  367.     
  368.     /**
  369.      * 
  370.      * Add Slider Field
  371.      *
  372.      * @param mixed $data If string, it must be the name of the field. If array, them we will loop on it
  373.      * @param string $title Title of the field
  374.      * @param float $min Min value
  375.      * @param float $max Max value
  376.      * @param float $step Step value
  377.      * @param float $defaultValue Default value
  378.      * @param array $options Additional options
  379.      * 
  380.      */
  381.     public function addSliderField$data $title '' $minValue '' $maxValue '' $numValues '' $defaultValue '' $options '' )
  382.     {
  383.         $newField array();
  384.         
  385.         // Check if it is an array
  386.         if is_array$data ) )
  387.         {
  388.             $newField $data;
  389.         }
  390.         else
  391.         {
  392.             // Add vars to the array
  393.             $newField['name'$data;
  394.             $newField['title'$title;
  395.             $newField['minValue'$minValue;
  396.             $newField['maxValue'$maxValue;
  397.             $newField['numValues'$numValues;
  398.             $newField['defaultValue'$defaultValue;
  399.             $newField['type''slider';
  400.             
  401.             // Set default value for titleOrientation if it is not set
  402.             if empty$options['titleOrientation') )
  403.             {
  404.                 $options['titleOrientation'$this->_defaultTitleOrientation;
  405.             }
  406.             
  407.             // Check if options is array and do the loop
  408.             if is_array$options ) )
  409.             {
  410.                 foreach $options as $name => $value )
  411.                 {
  412.                     $newField[$name$value;
  413.                 }
  414.             }
  415.         }
  416.         
  417.         // Add field to the array
  418.         $this->_fields[$newField;
  419.     }
  420.     
  421.     /**
  422.      * 
  423.      * Add Section
  424.      *
  425.      * @param mixed $data If string, it must be a unique value for this section. If array, them we will loop on it
  426.      * @param string $defaultValue Title of the section
  427.      * @param array $itemIds Fields on this section
  428.      * @param boolean $sectionExpanded True if section is expanded
  429.      * @param array $options Additional options
  430.      * 
  431.      */
  432.     public function addSection$data $defaultValue '' $itemIds '' $sectionExpanded true $options '' )
  433.     {
  434.         $newField array();
  435.         
  436.         // Check if it is an array
  437.         if is_array$data ) )
  438.         {
  439.             $newField $data;
  440.         }
  441.         else
  442.         {
  443.             // Add vars to the array
  444.             $newField['ID'$data;
  445.             $newField['defaultValue'$defaultValue;
  446.             $newField['itemIds'$itemIds;
  447.             $newField['sectionExpanded'$sectionExpanded;
  448.             $newField['type''section';
  449.             
  450.             // Set default value for titleOrientation if it is not set
  451.             if empty$options['titleOrientation') )
  452.             {
  453.                 $options['titleOrientation'$this->_defaultTitleOrientation;
  454.             }
  455.             
  456.             // Check if options is array and do the loop
  457.             if is_array$options ) )
  458.             {
  459.                 foreach $options as $name => $value )
  460.                 {
  461.                     $newField[$name$value;
  462.                 }
  463.             }
  464.         }
  465.         
  466.         // Set $_currentSection
  467.         $this->_currentSection $newField['ID'];
  468.         
  469.         // Add field to the array
  470.         $this->_fields[$newField;
  471.     }
  472.  
  473.     /**
  474.      * 
  475.      * Add Select Field
  476.      *
  477.      * @param mixed $data If string, it must be the name of the field. If array, them we will loop on it
  478.      * @param string $title Title of the field
  479.      * @param array $valueMap Array containing the values for this select field
  480.      * @param mixed $defaultValue Default value
  481.      * @param array $options Additional options
  482.      * 
  483.      */
  484.     public function addSelectField$data $title '' $valueMap '' $defaultValue '' $options '' )
  485.     {
  486.         $newField array();
  487.         
  488.         // Check if it is an array
  489.         if is_array$data ) )
  490.         {
  491.             $newField $data;
  492.         }
  493.         else
  494.         {
  495.             // Add vars to the array
  496.             $newField['name'$data;
  497.             $newField['title'$title;
  498.             $newField['valueMap'$valueMap;
  499.             $newField['defaultValue'$defaultValue;
  500.             $newField['type''select';
  501.             
  502.             // Set default value for titleOrientation if it is not set
  503.             if empty$options['titleOrientation') )
  504.             {
  505.                 $options['titleOrientation'$this->_defaultTitleOrientation;
  506.             }
  507.             
  508.             // Check if options is array and do the loop
  509.             if is_array$options ) )
  510.             {
  511.                 foreach $options as $name => $value )
  512.                 {
  513.                     $newField[$name$value;
  514.                 }
  515.             }
  516.         }
  517.         
  518.         // Add field to the array
  519.         $this->_fields[$newField;
  520.     }
  521.     
  522.     /**
  523.      * 
  524.      * Add Submit Button
  525.      *
  526.      * @param mixed $data If string, it must be the value of the field. If array, them we will loop on it
  527.      * @param array $options Additional options
  528.      * 
  529.      */
  530.     public function addSubmitButton$data $options '' )
  531.     {
  532.         $newField array();
  533.         
  534.         // Check if it is an array
  535.         if is_array$data ) )
  536.         {
  537.             $newField $data;
  538.         }
  539.         else
  540.         {
  541.             // Add vars to the array
  542.             $newField['title'$data;
  543.             $newField['type''submit';
  544.             
  545.             // Set default value for titleOrientation if it is not set
  546.             if empty$options['titleOrientation') )
  547.             {
  548.                 $options['titleOrientation'$this->_defaultTitleOrientation;
  549.             }
  550.             
  551.             // Check if options is array and do the loop
  552.             if is_array$options ) )
  553.             {
  554.                 foreach $options as $name => $value )
  555.                 {
  556.                     $newField[$name$value;
  557.                 }
  558.             }
  559.         }
  560.         
  561.         // Add field to the array
  562.         $this->_fields[$newField;
  563.     }
  564.     
  565.     /**
  566.      * 
  567.      * Add Text Area Field
  568.      *
  569.      * @param mixed $data If string, it must be the name of the field. If array, them we will loop on it
  570.      * @param string $title Title of the field
  571.      * @param string $defaultValue Default value for the text area
  572.      * @param array $options Additional options
  573.      * 
  574.      */
  575.     public function addTextArea$data $title '' $defaultValue '' $options '' )
  576.     {
  577.         $newField array();
  578.         
  579.         // Check if it is an array
  580.         if is_array$data ) )
  581.         {
  582.             $newField $data;
  583.         }
  584.         else
  585.         {
  586.             // Add vars to the array
  587.             $newField['name'$data;
  588.             $newField['title'$title;
  589.             $newField['defaultValue'$defaultValue;
  590.             $newField['type''textArea';
  591.             
  592.             // Set default value for titleOrientation if it is not set
  593.             if empty$options['titleOrientation') )
  594.             {
  595.                 $options['titleOrientation'$this->_defaultTitleOrientation;
  596.             }
  597.             
  598.             // Check if options is array and do the loop
  599.             if is_array$options ) )
  600.             {
  601.                 foreach $options as $name => $value )
  602.                 {
  603.                     $newField[$name$value;
  604.                 }
  605.             }
  606.         }
  607.         
  608.         // Add field to the array
  609.         $this->_fields[$newField;
  610.     }
  611.     
  612.     /**
  613.      * 
  614.      * Add Text Field
  615.      *
  616.      * @param mixed $data If string, it must be the name of the field. If array, them we will loop on it
  617.      * @param string $title Title of the field
  618.      * @param string $defaultValue Default value for the text area
  619.      * @param array $options Additional options
  620.      * 
  621.      */
  622.     public function addTextField$data $title '' $defaultValue '' $options '' )
  623.     {
  624.         $newField array();
  625.         
  626.         // Check if it is an array
  627.         if is_array$data ) )
  628.         {
  629.             $newField $data;
  630.         }
  631.         else
  632.         {
  633.             // Add vars to the array
  634.             $newField['name'$data;
  635.             $newField['title'$title;
  636.             $newField['defaultValue'$defaultValue;
  637.             $newField['type''text';
  638.             
  639.             // Set default value for titleOrientation if it is not set
  640.             if empty$options['titleOrientation') )
  641.             {
  642.                 $options['titleOrientation'$this->_defaultTitleOrientation;
  643.             }
  644.             
  645.             // Check if options is array and do the loop
  646.             if is_array$options ) )
  647.             {
  648.                 foreach $options as $name => $value )
  649.                 {
  650.                     $newField[$name$value;
  651.                 }
  652.             }
  653.         }
  654.         
  655.         // Add field to the array
  656.         $this->_fields[$newField;
  657.     }
  658.     
  659.     /**
  660.      * 
  661.      * Add Time Field
  662.      *
  663.      * @param mixed $data If string, it must be the name of the field. If array, them we will loop on it
  664.      * @param string $title Title of the field
  665.      * @param string $defaultValue Default value
  666.      * @param array $options Additional options
  667.      * 
  668.      */
  669.     public function addTimeField$data $title '' $defaultValue '' $options '' )
  670.     {
  671.         $newField array();
  672.         
  673.         // Check if it is an array
  674.         if is_array$data ) )
  675.         {
  676.             $newField $data;
  677.         }
  678.         else
  679.         {
  680.             // Add vars to the array
  681.             $newField['name'$data;
  682.             $newField['title'$title;
  683.             $newField['defaultValue'$defaultValue;
  684.             $newField['type''time';
  685.             
  686.             // Set default value for titleOrientation if it is not set
  687.             if empty$options['titleOrientation') )
  688.             {
  689.                 $options['titleOrientation'$this->_defaultTitleOrientation;
  690.             }
  691.             
  692.             // Check if options is array and do the loop
  693.             if is_array$options ) )
  694.             {
  695.                 foreach $options as $name => $value )
  696.                 {
  697.                     $newField[$name$value;
  698.                 }
  699.             }
  700.         }
  701.         
  702.         // Add field to the array
  703.         $this->_fields[$newField;
  704.     }
  705.     
  706.     /**
  707.      * Get Fields
  708.      *
  709.      * Return the current fields array
  710.      * 
  711.      * @return array 
  712.      *  
  713.      */
  714.     public function getFields()
  715.     {
  716.         return $this->_fields;
  717.     }
  718.     
  719.     /**
  720.      * 
  721.      * Clear fields
  722.      *
  723.      * Clear Fields array
  724.      *  
  725.      */
  726.     public function clearFields()
  727.     {
  728.         unset$this->_fields );
  729.     }
  730.  
  731.     /**
  732.      * 
  733.      * Create
  734.      * 
  735.      * Create the dynamic form
  736.      *
  737.      * @param mixed $data If string, it must be the id of the form. If array, them we will loop through it
  738.      * @param string $action Action of the form
  739.      * @param array $options Additional options
  740.      * @return string 
  741.      * 
  742.      */
  743.     public function create$data $action '' $options '' )
  744.     {
  745.         $newForm array();
  746.         
  747.         // Check if it is an array
  748.         if is_array$data ) )
  749.         {
  750.             $newForm $data;
  751.         }
  752.         else
  753.         {
  754.             // Add vars to the new form
  755.             $newForm['ID'$data;
  756.             $newForm['action'$action;
  757.             $newForm['fields'$this->_fields;
  758.             
  759.             // Set the default value for canSubmit
  760.             if !isset$options['canSubmit') )
  761.             {
  762.                 $options['canSubmit'true;
  763.             }
  764.             
  765.             // Set the default value for position
  766.             if !isset$options['position') )
  767.             {
  768.                 $options['position''relative';
  769.             }
  770.             
  771.             // Set the default value for saveOnEnter
  772.             if !isset$options['saveOnEnter') )
  773.             {
  774.                 $options['saveOnEnter'true;
  775.             }
  776.             
  777.             // Check if options is array and do the loop
  778.             if is_array$options ) )
  779.             {
  780.                 foreach $options as $name => $value )
  781.                 {
  782.                     $newForm[$name$value;
  783.                 }
  784.             }
  785.         }
  786.         
  787.         // Encode all data
  788.         $dataEncoded json_encode$newForm );
  789.         
  790.         // Remove vars that shouldn't be quoted
  791.         $dataEncoded $this->removeQuotes$dataEncoded );
  792.         
  793.         // Generate js code
  794.         $js '
  795.         <script type="text/javascript">
  796.             isc.DynamicForm.create(
  797.                 '$dataEncoded .'
  798.             );
  799.         </script>
  800.         ';
  801.         
  802.         return $js;
  803.     }
  804. }
  805. ?>

Documentation generated on Wed, 29 Jul 2009 22:37:14 -0300 by phpDocumentor 1.4.1