Advertisement
Guest User

unzend.com_294

a guest
Jul 11th, 2015
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 21.52 KB | None | 0 0
  1. <?php
  2. // Ioncube Decoder Unzend.Com Email unzend@gmail.com
  3. // http://www.unzend.com
  4. /**
  5.  * CForm class file.
  6.  *
  7.  * @author Qiang Xue <qiang.xue@gmail.com>
  8.  * @link http://www.yiiframework.com/
  9.  * @copyright 2008-2013 Yii Software LLC
  10.  * @license http://www.yiiframework.com/license/
  11.  */
  12.  
  13. /**
  14.  * CForm represents a form object that contains form input specifications.
  15.  *
  16.  * The main purpose of introducing the abstraction of form objects is to enhance the
  17.  * reusability of forms. In particular, we can divide a form in two parts: those
  18.  * that specify each individual form inputs, and those that decorate the form inputs.
  19.  * A CForm object represents the former part. It relies on the rendering process to
  20.  * accomplish form input decoration. Reusability is mainly achieved in the rendering process.
  21.  * That is, a rendering process can be reused to render different CForm objects.
  22.  *
  23.  * A form can be rendered in different ways. One can call the {@link render} method
  24.  * to get a quick form rendering without writing any HTML code; one can also override
  25.  * {@link render} to render the form in a different layout; and one can use an external
  26.  * view template to render each form element explicitly. In these ways, the {@link render}
  27.  * method can be applied to all kinds of forms and thus achieves maximum reusability;
  28.  * while the external view template keeps maximum flexibility in rendering complex forms.
  29.  *
  30.  * Form input specifications are organized in terms of a form element hierarchy.
  31.  * At the root of the hierarchy, it is the root CForm object. The root form object maintains
  32.  * its children in two collections: {@link elements} and {@link buttons}.
  33.  * The former contains non-button form elements ({@link CFormStringElement},
  34.  * {@link CFormInputElement} and CForm); while the latter mainly contains
  35.  * button elements ({@link CFormButtonElement}). When a CForm object is embedded in the
  36.  * {@link elements} collection, it is called a sub-form which can have its own {@link elements}
  37.  * and {@link buttons} collections and thus form the whole form hierarchy.
  38.  *
  39.  * Sub-forms are mainly used to handle multiple models. For example, in a user
  40.  * registration form, we can have the root form to collect input for the user
  41.  * table while a sub-form to collect input for the profile table. Sub-form is also
  42.  * a good way to partition a lengthy form into shorter ones, even though all inputs
  43.  * may belong to the same model.
  44.  *
  45.  * Form input specifications are given in terms of a configuration array which is
  46.  * used to initialize the property values of a CForm object. The {@link elements} and
  47.  * {@link buttons} properties need special attention as they are the main properties
  48.  * to be configured. To configure {@link elements}, we should give it an array like
  49.  * the following:
  50.  * <pre>
  51.  * 'elements'=>array(
  52.  *     'username'=>array('type'=>'text', 'maxlength'=>80),
  53.  *     'password'=>array('type'=>'password', 'maxlength'=>80),
  54.  * )
  55.  * </pre>
  56.  * The above code specifies two input elements: 'username' and 'password'. Note the model
  57.  * object must have exactly the same attributes 'username' and 'password'. Each element
  58.  * has a type which specifies what kind of input should be used. The rest of the array elements
  59.  * (e.g. 'maxlength') in an input specification are rendered as HTML element attributes
  60.  * when the input field is rendered. The {@link buttons} property is configured similarly.
  61.  *
  62.  * If you're going to use AJAX and/or client form validation with the enabled error summary
  63.  * you have to set {@link $showErrors} property to true. Please refer to it's documentation
  64.  * for more details.
  65.  *
  66.  * For more details about configuring form elements, please refer to {@link CFormInputElement}
  67.  * and {@link CFormButtonElement}.
  68.  *
  69.  * @property CForm $root The top-level form object.
  70.  * @property CActiveForm $activeFormWidget The active form widget associated with this form.
  71.  * This method will return the active form widget as specified by {@link activeForm}.
  72.  * @property CBaseController $owner The owner of this form. This refers to either a controller or a widget
  73.  * by which the form is created and rendered.
  74.  * @property CModel $model The model associated with this form. If this form does not have a model,
  75.  * it will look for a model in its ancestors.
  76.  * @property array $models The models that are associated with this form or its sub-forms.
  77.  * @property CFormElementCollection $elements The form elements.
  78.  * @property CFormElementCollection $buttons The form elements.
  79.  *
  80.  * @author Qiang Xue <qiang.xue@gmail.com>
  81.  * @package system.web.form
  82.  * @since 1.1
  83.  */
  84. class CForm extends CFormElement implements ArrayAccess
  85. {
  86.     /**
  87.      * @var string the title for this form. By default, if this is set, a fieldset may be rendered
  88.      * around the form body using the title as its legend. Defaults to null.
  89.      */
  90.     public $title;
  91.     /**
  92.      * @var string the description of this form.
  93.      */
  94.     public $description;
  95.     /**
  96.      * @var string the submission method of this form. Defaults to 'post'.
  97.      * This property is ignored when this form is a sub-form.
  98.      */
  99.     public $method='post';
  100.     /**
  101.      * @var mixed the form action URL (see {@link CHtml::normalizeUrl} for details about this parameter.)
  102.      * Defaults to an empty string, meaning the current request URL.
  103.      * This property is ignored when this form is a sub-form.
  104.      */
  105.     public $action='';
  106.     /**
  107.      * @var string the name of the class for representing a form input element. Defaults to 'CFormInputElement'.
  108.      */
  109.     public $inputElementClass='CFormInputElement';
  110.     /**
  111.      * @var string the name of the class for representing a form button element. Defaults to 'CFormButtonElement'.
  112.      */
  113.     public $buttonElementClass='CFormButtonElement';
  114.     /**
  115.      * @var array HTML attribute values for the form tag. When the form is embedded within another form,
  116.      * this property will be used to render the HTML attribute values for the fieldset enclosing the child form.
  117.      */
  118.     public $attributes=array();
  119.     /**
  120.      * @var boolean whether to show error summary. Defaults to false.
  121.      */
  122.     public $showErrorSummary=false;
  123.     /**
  124.      * @var boolean|null whether error elements of the form attributes should be rendered. There are three possible
  125.      * valid values: null, true and false.
  126.      *
  127.      * Defaults to null meaning that {@link $showErrorSummary} will be used as value. This is done mainly to keep
  128.      * backward compatibility with existing applications. If you want to use error summary with AJAX and/or client
  129.      * validation you have to set this property to true (recall that {@link CActiveForm::error()} should be called
  130.      * for each attribute that is going to be AJAX and/or client validated).
  131.      *
  132.      * False value means that the error elements of the form attributes shall not be displayed. True value means that
  133.      * the error elements of the form attributes will be rendered.
  134.      *
  135.      * @since 1.1.14
  136.      */
  137.     public $showErrors;
  138.     /**
  139.      * @var array the configuration used to create the active form widget.
  140.      * The widget will be used to render the form tag and the error messages.
  141.      * The 'class' option is required, which specifies the class of the widget.
  142.      * The rest of the options will be passed to {@link CBaseController::beginWidget()} call.
  143.      * Defaults to array('class'=>'CActiveForm').
  144.      * @since 1.1.1
  145.      */
  146.     public $activeForm=array('class'=>'CActiveForm');
  147.  
  148.     private $_model;
  149.     private $_elements;
  150.     private $_buttons;
  151.     private $_activeForm;
  152.  
  153.     /**
  154.      * Constructor.
  155.      * If you override this method, make sure you do not modify the method
  156.      * signature, and also make sure you call the parent implementation.
  157.      * @param mixed $config the configuration for this form. It can be a configuration array
  158.      * or the path alias of a PHP script file that returns a configuration array.
  159.      * The configuration array consists of name-value pairs that are used to initialize
  160.      * the properties of this form.
  161.      * @param CModel $model the model object associated with this form. If it is null,
  162.      * the parent's model will be used instead.
  163.      * @param mixed $parent the direct parent of this form. This could be either a {@link CBaseController}
  164.      * object (a controller or a widget), or a {@link CForm} object.
  165.      * If the former, it means the form is a top-level form; if the latter, it means this form is a sub-form.
  166.      */
  167.     public function __construct($config,$model=null,$parent=null)
  168.     {
  169.         $this->setModel($model);
  170.         if($parent===null)
  171.             $parent=Yii::app()->getController();
  172.         parent::__construct($config,$parent);
  173.         if($this->showErrors===null)
  174.             $this->showErrors=!$this->showErrorSummary;
  175.         $this->init();
  176.     }
  177.  
  178.     /**
  179.      * Initializes this form.
  180.      * This method is invoked at the end of the constructor.
  181.      * You may override this method to provide customized initialization (such as
  182.      * configuring the form object).
  183.      */
  184.     protected function init()
  185.     {
  186.     }
  187.  
  188.     /**
  189.      * Returns a value indicating whether this form is submitted.
  190.      * @param string $buttonName the name of the submit button
  191.      * @param boolean $loadData whether to call {@link loadData} if the form is submitted so that
  192.      * the submitted data can be populated to the associated models.
  193.      * @return boolean whether this form is submitted.
  194.      * @see loadData
  195.      */
  196.     public function submitted($buttonName='submit',$loadData=true)
  197.     {
  198.         $ret=$this->clicked($this->getUniqueId()) && $this->clicked($buttonName);
  199.         if($ret && $loadData)
  200.             $this->loadData();
  201.         return $ret;
  202.     }
  203.  
  204.     /**
  205.      * Returns a value indicating whether the specified button is clicked.
  206.      * @param string $name the button name
  207.      * @return boolean whether the button is clicked.
  208.      */
  209.     public function clicked($name)
  210.     {
  211.         if(strcasecmp($this->getRoot()->method,'get'))
  212.             return isset($_POST[$name]);
  213.         else
  214.             return isset($_GET[$name]);
  215.     }
  216.  
  217.     /**
  218.      * Validates the models associated with this form.
  219.      * All models, including those associated with sub-forms, will perform
  220.      * the validation. You may use {@link CModel::getErrors()} to retrieve the validation
  221.      * error messages.
  222.      * @return boolean whether all models are valid
  223.      */
  224.     public function validate()
  225.     {
  226.         $ret=true;
  227.         foreach($this->getModels() as $model)
  228.             $ret=$model->validate() && $ret;
  229.         return $ret;
  230.     }
  231.  
  232.     /**
  233.      * Loads the submitted data into the associated model(s) to the form.
  234.      * This method will go through all models associated with this form and its sub-forms
  235.      * and massively assign the submitted data to the models.
  236.      * @see submitted
  237.      */
  238.     public function loadData()
  239.     {
  240.         if($this->_model!==null)
  241.         {
  242.             $class=CHtml::modelName($this->_model);
  243.             if(strcasecmp($this->getRoot()->method,'get'))
  244.             {
  245.                 if(isset($_POST[$class]))
  246.                     $this->_model->setAttributes($_POST[$class]);
  247.             }
  248.             elseif(isset($_GET[$class]))
  249.                 $this->_model->setAttributes($_GET[$class]);
  250.         }
  251.         foreach($this->getElements() as $element)
  252.         {
  253.             if($element instanceof self)
  254.                 $element->loadData();
  255.         }
  256.     }
  257.  
  258.     /**
  259.      * @return CForm the top-level form object
  260.      */
  261.     public function getRoot()
  262.     {
  263.         $root=$this;
  264.         while($root->getParent() instanceof self)
  265.             $root=$root->getParent();
  266.         return $root;
  267.     }
  268.  
  269.     /**
  270.      * @return CActiveForm the active form widget associated with this form.
  271.      * This method will return the active form widget as specified by {@link activeForm}.
  272.      * @since 1.1.1
  273.      */
  274.     public function getActiveFormWidget()
  275.     {
  276.         if($this->_activeForm!==null)
  277.             return $this->_activeForm;
  278.         else
  279.             return $this->getRoot()->_activeForm;
  280.     }
  281.  
  282.     /**
  283.      * @return CBaseController the owner of this form. This refers to either a controller or a widget
  284.      * by which the form is created and rendered.
  285.      */
  286.     public function getOwner()
  287.     {
  288.         $owner=$this->getParent();
  289.         while($owner instanceof self)
  290.             $owner=$owner->getParent();
  291.         return $owner;
  292.     }
  293.  
  294.     /**
  295.      * Returns the model that this form is associated with.
  296.      * @param boolean $checkParent whether to return parent's model if this form doesn't have model by itself.
  297.      * @return CModel the model associated with this form. If this form does not have a model,
  298.      * it will look for a model in its ancestors.
  299.      */
  300.     public function getModel($checkParent=true)
  301.     {
  302.         if(!$checkParent)
  303.             return $this->_model;
  304.         $form=$this;
  305.         while($form->_model===null && $form->getParent() instanceof self)
  306.             $form=$form->getParent();
  307.         return $form->_model;
  308.     }
  309.  
  310.     /**
  311.      * @param CModel $model the model to be associated with this form
  312.      */
  313.     public function setModel($model)
  314.     {
  315.         $this->_model=$model;
  316.     }
  317.  
  318.     /**
  319.      * Returns all models that are associated with this form or its sub-forms.
  320.      * @return array the models that are associated with this form or its sub-forms.
  321.      */
  322.     public function getModels()
  323.     {
  324.         $models=array();
  325.         if($this->_model!==null)
  326.             $models[]=$this->_model;
  327.         foreach($this->getElements() as $element)
  328.         {
  329.             if($element instanceof self)
  330.                 $models=array_merge($models,$element->getModels());
  331.         }
  332.         return $models;
  333.     }
  334.  
  335.     /**
  336.      * Returns the input elements of this form.
  337.      * This includes text strings, input elements and sub-forms.
  338.      * Note that the returned result is a {@link CFormElementCollection} object, which
  339.      * means you can use it like an array. For more details, see {@link CMap}.
  340.      * @return CFormElementCollection the form elements.
  341.      */
  342.     public function getElements()
  343.     {
  344.         if($this->_elements===null)
  345.             $this->_elements=new CFormElementCollection($this,false);
  346.         return $this->_elements;
  347.     }
  348.  
  349.     /**
  350.      * Configures the input elements of this form.
  351.      * The configuration must be an array of input configuration array indexed by input name.
  352.      * Each input configuration array consists of name-value pairs that are used to initialize
  353.      * a {@link CFormStringElement} object (when 'type' is 'string'), a {@link CFormElement} object
  354.      * (when 'type' is a string ending with 'Form'), or a {@link CFormInputElement} object in
  355.      * all other cases.
  356.      * @param array $elements the elements configurations
  357.      */
  358.     public function setElements($elements)
  359.     {
  360.         $collection=$this->getElements();
  361.         foreach($elements as $name=>$config)
  362.             $collection->add($name,$config);
  363.     }
  364.  
  365.     /**
  366.      * Returns the button elements of this form.
  367.      * Note that the returned result is a {@link CFormElementCollection} object, which
  368.      * means you can use it like an array. For more details, see {@link CMap}.
  369.      * @return CFormElementCollection the form elements.
  370.      */
  371.     public function getButtons()
  372.     {
  373.         if($this->_buttons===null)
  374.             $this->_buttons=new CFormElementCollection($this,true);
  375.         return $this->_buttons;
  376.     }
  377.  
  378.     /**
  379.      * Configures the buttons of this form.
  380.      * The configuration must be an array of button configuration array indexed by button name.
  381.      * Each button configuration array consists of name-value pairs that are used to initialize
  382.      * a {@link CFormButtonElement} object.
  383.      * @param array $buttons the button configurations
  384.      */
  385.     public function setButtons($buttons)
  386.     {
  387.         $collection=$this->getButtons();
  388.         foreach($buttons as $name=>$config)
  389.             $collection->add($name,$config);
  390.     }
  391.  
  392.     /**
  393.      * Renders the form.
  394.      * The default implementation simply calls {@link renderBegin}, {@link renderBody} and {@link renderEnd}.
  395.      * @return string the rendering result
  396.      */
  397.     public function render()
  398.     {
  399.         return $this->renderBegin() . $this->renderBody() . $this->renderEnd();
  400.     }
  401.  
  402.     /**
  403.      * Renders the open tag of the form.
  404.      * The default implementation will render the open form tag.
  405.      * @return string the rendering result
  406.      */
  407.     public function renderBegin()
  408.     {
  409.         if($this->getParent() instanceof self)
  410.             return '';
  411.         else
  412.         {
  413.             $options=$this->activeForm;
  414.             if(isset($options['class']))
  415.             {
  416.                 $class=$options['class'];
  417.                 unset($options['class']);
  418.             }
  419.             else
  420.                 $class='CActiveForm';
  421.             $options['action']=$this->action;
  422.             $options['method']=$this->method;
  423.             if(isset($options['htmlOptions']))
  424.             {
  425.                 foreach($this->attributes as $name=>$value)
  426.                     $options['htmlOptions'][$name]=$value;
  427.             }
  428.             else
  429.                 $options['htmlOptions']=$this->attributes;
  430.             ob_start();
  431.             $this->_activeForm=$this->getOwner()->beginWidget($class, $options);
  432.             return ob_get_clean() . "<div style=\"visibility:hidden\">".CHtml::hiddenField($this->getUniqueID(),1)."</div>\n";
  433.         }
  434.     }
  435.  
  436.     /**
  437.      * Renders the close tag of the form.
  438.      * @return string the rendering result
  439.      */
  440.     public function renderEnd()
  441.     {
  442.         if($this->getParent() instanceof self)
  443.             return '';
  444.         else
  445.         {
  446.             ob_start();
  447.             $this->getOwner()->endWidget();
  448.             return ob_get_clean();
  449.         }
  450.     }
  451.  
  452.     /**
  453.      * Renders the body content of this form.
  454.      * This method mainly renders {@link elements} and {@link buttons}.
  455.      * If {@link title} or {@link description} is specified, they will be rendered as well.
  456.      * And if the associated model contains error, the error summary may also be displayed.
  457.      * The form tag will not be rendered. Please call {@link renderBegin} and {@link renderEnd}
  458.      * to render the open and close tags of the form.
  459.      * You may override this method to customize the rendering of the form.
  460.      * @return string the rendering result
  461.      */
  462.     public function renderBody()
  463.     {
  464.         $output='';
  465.         if($this->title!==null)
  466.         {
  467.             if($this->getParent() instanceof self)
  468.             {
  469.                 $attributes=$this->attributes;
  470.                 unset($attributes['name'],$attributes['type']);
  471.                 $output=CHtml::openTag('fieldset', $attributes)."<legend>".$this->title."</legend>\n";
  472.             }
  473.             else
  474.                 $output="<fieldset>\n<legend>".$this->title."</legend>\n";
  475.         }
  476.  
  477.         if($this->description!==null)
  478.             $output.="<div class=\"description\">\n".$this->description."</div>\n";
  479.  
  480.         if($this->showErrorSummary && ($model=$this->getModel(false))!==null)
  481.             $output.=$this->getActiveFormWidget()->errorSummary($model)."\n";
  482.  
  483.         $output.=$this->renderElements()."\n".$this->renderButtons()."\n";
  484.  
  485.         if($this->title!==null)
  486.             $output.="</fieldset>\n";
  487.  
  488.         return $output;
  489.     }
  490.  
  491.     /**
  492.      * Renders the {@link elements} in this form.
  493.      * @return string the rendering result
  494.      */
  495.     public function renderElements()
  496.     {
  497.         $output='';
  498.         foreach($this->getElements() as $element)
  499.             $output.=$this->renderElement($element);
  500.         return $output;
  501.     }
  502.  
  503.     /**
  504.      * Renders the {@link buttons} in this form.
  505.      * @return string the rendering result
  506.      */
  507.     public function renderButtons()
  508.     {
  509.         $output='';
  510.         foreach($this->getButtons() as $button)
  511.             $output.=$this->renderElement($button);
  512.         return $output!=='' ? "<div class=\"row buttons\">".$output."</div>\n" : '';
  513.     }
  514.  
  515.     /**
  516.      * Renders a single element which could be an input element, a sub-form, a string, or a button.
  517.      * @param mixed $element the form element to be rendered. This can be either a {@link CFormElement} instance
  518.      * or a string representing the name of the form element.
  519.      * @return string the rendering result
  520.      */
  521.     public function renderElement($element)
  522.     {
  523.         if(is_string($element))
  524.         {
  525.             if(($e=$this[$element])===null && ($e=$this->getButtons()->itemAt($element))===null)
  526.                 return $element;
  527.             else
  528.                 $element=$e;
  529.         }
  530.         if($element->getVisible())
  531.         {
  532.             if($element instanceof CFormInputElement)
  533.             {
  534.                 if($element->type==='hidden')
  535.                     return "<div style=\"visibility:hidden\">\n".$element->render()."</div>\n";
  536.                 else
  537.                     return "<div class=\"row field_{$element->name}\">\n".$element->render()."</div>\n";
  538.             }
  539.             elseif($element instanceof CFormButtonElement)
  540.                 return $element->render()."\n";
  541.             else
  542.                 return $element->render();
  543.         }
  544.         return '';
  545.     }
  546.  
  547.     /**
  548.      * This method is called after an element is added to the element collection.
  549.      * @param string $name the name of the element
  550.      * @param CFormElement $element the element that is added
  551.      * @param boolean $forButtons whether the element is added to the {@link buttons} collection.
  552.      * If false, it means the element is added to the {@link elements} collection.
  553.      */
  554.     public function addedElement($name,$element,$forButtons)
  555.     {
  556.     }
  557.  
  558.     /**
  559.      * This method is called after an element is removed from the element collection.
  560.      * @param string $name the name of the element
  561.      * @param CFormElement $element the element that is removed
  562.      * @param boolean $forButtons whether the element is removed from the {@link buttons} collection
  563.      * If false, it means the element is removed from the {@link elements} collection.
  564.      */
  565.     public function removedElement($name,$element,$forButtons)
  566.     {
  567.     }
  568.  
  569.     /**
  570.      * Evaluates the visibility of this form.
  571.      * This method will check the visibility of the {@link elements}.
  572.      * If any one of them is visible, the form is considered as visible. Otherwise, it is invisible.
  573.      * @return boolean whether this form is visible.
  574.      */
  575.     protected function evaluateVisible()
  576.     {
  577.         foreach($this->getElements() as $element)
  578.             if($element->getVisible())
  579.                 return true;
  580.         return false;
  581.     }
  582.  
  583.     /**
  584.      * Returns a unique ID that identifies this form in the current page.
  585.      * @return string the unique ID identifying this form
  586.      */
  587.     protected function getUniqueId()
  588.     {
  589.         if(isset($this->attributes['id']))
  590.             return 'yform_'.$this->attributes['id'];
  591.         else
  592.             return 'yform_'.sprintf('%x',crc32(serialize(array_keys($this->getElements()->toArray()))));
  593.     }
  594.  
  595.     /**
  596.      * Returns whether there is an element at the specified offset.
  597.      * This method is required by the interface ArrayAccess.
  598.      * @param mixed $offset the offset to check on
  599.      * @return boolean
  600.      */
  601.     public function offsetExists($offset)
  602.     {
  603.         return $this->getElements()->contains($offset);
  604.     }
  605.  
  606.     /**
  607.      * Returns the element at the specified offset.
  608.      * This method is required by the interface ArrayAccess.
  609.      * @param integer $offset the offset to retrieve element.
  610.      * @return mixed the element at the offset, null if no element is found at the offset
  611.      */
  612.     public function offsetGet($offset)
  613.     {
  614.         return $this->getElements()->itemAt($offset);
  615.     }
  616.  
  617.     /**
  618.      * Sets the element at the specified offset.
  619.      * This method is required by the interface ArrayAccess.
  620.      * @param integer $offset the offset to set element
  621.      * @param mixed $item the element value
  622.      */
  623.     public function offsetSet($offset,$item)
  624.     {
  625.         $this->getElements()->add($offset,$item);
  626.     }
  627.  
  628.     /**
  629.      * Unsets the element at the specified offset.
  630.      * This method is required by the interface ArrayAccess.
  631.      * @param mixed $offset the offset to unset element
  632.      */
  633.     public function offsetUnset($offset)
  634.     {
  635.         $this->getElements()->remove($offset);
  636.     }
  637. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement