Commits

Christoffer Niska  committed 7ee543e

add initial form builder support

  • Participants
  • Parent commits 2883271

Comments (0)

Files changed (3)

File form/TbForm.php

+<?php
+/**
+ * TbForm class file.
+ * @author Christoffer Niska <ChristofferNiska@gmail.com>
+ * @copyright Copyright &copy; Christoffer Niska 2012-
+ * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
+ * @package bootstrap.form
+ * @since 2.0.0
+ */
+
+Yii::import('bootstrap.form.*');
+
+/**
+ * Bootstrap form builder.
+ */
+class TbForm extends CForm {
+	/**
+	 * @var string the name of the class for representing a form input element.
+	 */
+    public $buttonElementClass = 'TbFormButtonElement';
+	/**
+	 * @var string the name of the class for representing a form button element.
+	 */
+    public $inputElementClass = 'TbFormInputElement';
+	/**
+	 * @var array the configuration used to create the active form widget.
+	 */
+    public $activeForm = array('class'=>'bootstrap.widgets.TbActiveForm');
+
+	/**
+	 * Renders a single element which could be an input element, a sub-form, a string, or a button.
+	 * @param mixed $element the form element to be rendered
+	 * @return string the rendering result
+	 */
+    public function renderElement($element)
+    {
+        if(is_string($element))
+        {
+            if(($e=$this[$element])===null && ($e=$this->getButtons()->itemAt($element))===null)
+                return $element;
+            else
+                $element=$e;
+        }
+        if($element->getVisible())
+        {
+            if($element instanceof CFormInputElement)
+            {
+                if($element->type==='hidden')
+                    return '<div class="hidden">'.$element->render().'</div>';
+            }
+
+            return $element->render();
+        }
+        return '';
+    }
+
+	/**
+	 * Renders the {@link buttons} in this form.
+	 * @return string the rendering result
+	 */
+    public function renderButtons()
+    {
+        $output='';
+        foreach($this->getButtons() as $button)
+            $output.=$this->renderElement($button);
+        return $output!=='' ? '<div class="form-actions">'.$output.'</div>' : '';
+    }
+}

File form/TbFormButtonElement.php

+<?php
+/**
+ * TbFormButtonElement class file.
+ * @author Christoffer Niska <ChristofferNiska@gmail.com>
+ * @copyright Copyright &copy; Christoffer Niska 2012-
+ * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
+ * @package bootstrap.form
+ * @since 2.0.0
+ */
+
+/**
+ * Bootstrap form builder button element.
+ */
+class TbFormButtonElement extends CFormButtonElement {
+	/**
+	 * @var string the button callback types.
+	 * @see TbButton::$buttonType
+	 */
+	public $buttonType;
+	/**
+	 * @var string the button type.
+	 * @see TbButton::$type
+	 */
+	public $type;
+	/**
+	 * @var string the button size.
+	 * @see TbButton::$size
+	 */
+	public $size;
+	/**
+	 * @var string the button icon.
+	 * @see TbButton::$icon
+	 */
+	public $icon;
+	/**
+	 * @var string the button URL.
+	 * @see TbButton::$url
+	 */
+	public $url;
+	/**
+	 * @var boolean indicates whether the button should span the full width of the a parent.
+	 */
+	public $block = false;
+	/**
+	 * @var boolean indicates whether the button is active.
+	 */
+	public $active = false;
+	/**
+	 * @var boolean indicates whether the button is disabled.
+	 */
+	public $disabled = false;
+	/**
+	 * @var boolean indicates whether to encode the label.
+	 */
+	public $encodeLabel = true;
+	/**
+	 * @var boolean indicates whether to enable toggle.
+	 */
+	public $toggle;
+	/**
+	 * @var string the text to display while loading.
+	 */
+	public $loadingText;
+	/**
+	 * @var string the text to display when loading is complete.
+	 */
+	public $completeText;
+	/**
+	 * @var array the dropdown button items.
+	 */
+	public $items = array();
+	/**
+	 * @var array the HTML attributes for the widget container.
+	 */
+	public $htmlOptions = array();
+	/**
+	 * @var array array the button ajax options.
+	 * @see TbButton::$ajaxOptions
+	 */
+	public $ajaxOptions = array();
+	/**
+	 * @var array the HTML attributes for the dropdown menu.
+	 */
+	public $dropdownOptions = array();
+
+	/**
+	 * Returns this button.
+	 * @return string the rendering result
+	 */
+    public function render()
+    {
+        ob_start();
+        $this->getParent()->getOwner()->widget('bootstrap.widgets.TbButton', array(
+            'buttonType'=>isset($this->buttonType) ? $this->buttonType : null,
+            'type'=>isset($this->type) ? $this->type : null,
+            'size'=>isset($this->size) ? $this->size : null,
+            'icon'=>$this->icon,
+            'label'=>$this->label,
+            'url'=>$this->url,
+            'block'=>$this->block,
+            'active'=>$this->active,
+            'disabled'=>$this->disabled,
+            'encodeLabel'=>$this->encodeLabel,
+            'toggle'=>$this->toggle,
+            'loadingText'=>$this->loadingText,
+            'completeText'=>$this->completeText,
+            'items'=>$this->items,
+            'htmlOptions'=>$this->htmlOptions,
+            'ajaxOptions'=>$this->ajaxOptions,
+            'dropdownOptions'=>$this->dropdownOptions,
+        ));
+        return ob_get_clean();
+    }
+}

File form/TbFormInputElement.php

+<?php
+/**
+ * TbFormButtonElement class file.
+ * @author Christoffer Niska <ChristofferNiska@gmail.com>
+ * @copyright Copyright &copy; Christoffer Niska 2012-
+ * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
+ * @package bootstrap.form
+ * @since 2.0.0
+ */
+
+/**
+ * Bootstrap form builder input element.
+ */
+class TbFormInputElement extends CFormInputElement {
+	/**
+	 * @var array the data for list inputs.
+	 */
+	public $data;
+	/**
+	 * @var array additional HTML options to be rendered in the input tag.
+	 */
+	public $htmlOptions = array();
+
+	/**
+	 * Renders everything for this input.
+	 * @return string the complete rendering result for this input, including label, input field, hint, and error.
+	 */
+    public function render()
+    {
+        $form = $this->getParent();
+        return $form->getActiveFormWidget()->inputRow($this->type, $form->getModel(), $this->name, $this->data, $this->htmlOptions);
+    }
+}