Guest User

Untitled

a guest
May 24th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.00 KB | None | 0 0
  1. <?php
  2.  
  3. // виджет
  4. class CActiveFormWrapper extends CWidget {
  5.  
  6.     public $defaultFieldClass = 'Text';
  7.     public $fields = null;
  8.     public $form = null;
  9.     public $formOptions = array();
  10.     public $htmlOptions = array('class'=>'form');
  11.     public $model = null;
  12.  
  13.     public function run() {
  14.         $this->begin();
  15.         $this->fields();
  16.         $this->end():
  17.     }
  18.  
  19.     public function begin() {
  20.         echo CHtml::openTag('div', $this->htmlOptions);
  21.         $this->form = $this->beginWidget('CActiveForm', $this->formOptions);
  22.     }
  23.  
  24.     public function end() {
  25.         $this->endWidget();
  26.         echo CHtml::closeTag('div');
  27.     }
  28.  
  29.     public function fields() {
  30.         // если поля не указаны явно, будем показывать все подряд поля модели
  31.         if (is_null($this->fields)) {
  32.             $this->fields = array_keys($this->model->getAttributes());
  33.         }
  34.         // циклично выводим поля
  35.         foreach ($this->fields as $k=>$v) {
  36.             $options = (is_array($v))
  37.                 ? $v
  38.                 : array('attribute'=>$v);
  39.             $this->field($options);
  40.         }
  41.     }
  42.  
  43.     public function field($options) {
  44.         $class = (!empty($options['class']))
  45.             ? $options['class']
  46.             : $this->defaultFieldClass;
  47.         $class = ucfirst($class).'Field';
  48.         $field = new $class($this);
  49.         foreach ($options as $k=>$v) {
  50.             $field->$k => $v;
  51.         }
  52.         $field->init();
  53.         $field->run();
  54.     }
  55. }
  56.  
  57. // абстрактное поле
  58. // в интерфейсе IField такие методы: init(), run(), element()
  59. abstract class Field extends CComponent implements IField {
  60.  
  61.     public $attribute = '';
  62.     public $class = '';
  63.     public $htmlOptions = array();
  64.     public $rowHtmlOptions = array('class'=>'row');
  65.     public $owner = null;
  66.  
  67.     public function __construct($owner) {
  68.         $this->owner = $owner;
  69.     }
  70.  
  71.     public function init() {
  72.     }
  73.  
  74.     public function run() {
  75.         $this->begin();
  76.         $this->label();
  77.         $this->element();
  78.         $this->error();
  79.         $this->end();
  80.     }
  81.  
  82.     public function begin() {
  83.         echo CHtml::openTag('div', $this->rowHtmlOptions);
  84.     }
  85.  
  86.     public function end() {
  87.         echo CHtml::closeTag('div');
  88.     }
  89.  
  90.     public function label() {
  91.         echo $this->owner->form->labelEx($this->model, $this->attribute);
  92.     }
  93.  
  94.     public function error() {
  95.         echo $this->owner->form->error($this->model, $this->attribute);
  96.     }
  97.  
  98.     public function getModel() {
  99.         return $this->owner->model;
  100.     }
  101. }
  102.  
  103. // текстовое, например, поле
  104. class TextField extends Field {
  105.  
  106.     public function element() {
  107.         echo $this->owner->form->textField($this->model, $this->attribute,
  108.                 $this->htmlOptions);
  109.     }
  110. }
  111.  
  112. // textarea, допустим
  113. class TextAreaField extends Field {
  114.  
  115.     public function element() {
  116.         echo $this->owner->form->textArea($this->model, $this->attribute,
  117.                 $this->htmlOptions);
  118.     }
  119. }
  120.  
  121. // Ну и примеры вызова всей этой хуйни из вьюшки:
  122.  
  123. $model = new Post(); // id, title, content, ctime, utime
  124.  
  125. // Тупо модель, без параметров - выведутся все поля, включая ID, ctime, utime, нехорошо
  126. $this->widget('ext.CActiveFormWrapper', array(
  127.     'model'=>$model,
  128. ));
  129.  
  130. // То же самое, но с AJAX валидацией
  131. $this->widget('ext.CActiveFormWrapper', array(
  132.     'model'=>$model,
  133.     'formOptions'=>array(
  134.         'enableAjaxValidation'=>true,
  135.     ),
  136. ));
  137.  
  138. // Показать только title и content, причем content - textarea/
  139. $this->widget('ext.CActiveFormWrapper', array(
  140.     'model'=>$model,
  141.     'fields'=>array(
  142.         'title',
  143.         array(
  144.             'attribute'=>'content',
  145.             'class'=>'TextArea',
  146.         ),
  147.     ),
  148. ));
Add Comment
Please, Sign In to add comment