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

Source for file ScForms.php

Documentation is available at ScForms.php

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

Documentation generated on Mon, 20 Jul 2009 16:51:57 -0300 by phpDocumentor 1.4.1