Blame view

protected/modules/admin/components/widgets/FormWidget.php 3.58 KB
a1684257   Administrator   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
  <?php
  /**
   * Widget to render form by config in BHorizontalForm.
   */
  class FormWidget extends CWidget
  {
      /**
       * @var BHorizontalForm
       */
      public $form;
      /**
       * @var CModel
       */
      public $model;
      /**
       * Suffix to separate different input  with same model, like: Model[suf1][field1] and Model[suf2][field1]
       * @var string
       */
      public $suffix;
      /**
       * @var array
       */
      public $config = array(
          array(
              'type' => 'widget',
              'class' => 'ClassName',
              'config' => array(),
          ),
          array(
              'type' => 'inputWigdet',
              'class' => 'ClassName',
              'attribute' => 'attribute',
              'config' => array(),
          ),
          array(
              'type' => 'simpleInput',
              'name' => 'textField',
              'attribute' => 'attribute',
              'options' => array(),
          ),
          array(
              'type' => 'listInput',
              'name' => 'textField',
              'attribute' => 'attribute',
              'data' => array(),
              'options' => array(),
          ),
      );
  
      public function init()
      {
          parent::init();
      }
  
      public function run()
      {
          parent::run();
          $this->renderForm($this->config);
      }
  
      private function renderForm($config)
      {
          foreach ($config as $item) {
              switch ($item['type']) {
                  case 'fieldset':
                      echo  $this->form->beginFieldset($item['legend']);
                      $this->renderForm($item['config']);
                      echo $this->form->endFieldset();
                      break;
                  case 'widget':
                      Yii::app()->getController()->widget($item['class'], $item['config']);
                      break;
  
                  case 'inputWidget':
                      $config = isset($item['config']) ? $item['config'] : array();
                      if (isset($this->suffix)) {
                          $attr = '[' . $this->suffix . ']' . $item['attribute'];
                      } else {
                          $attr = $item['attribute'];
                      }
                      $config['attribute'] = $attr;
                      $config['model'] = $this->model;
                      echo $this->form->beginControlGroup($this->model, $attr);
                      Yii::app()->getController()->widget($item['class'], $config);
                      echo $this->form->endControlGroup();
                      break;
                  case 'simpleInput':
                      if (isset($this->suffix)) {
                          $attr = '[' . $this->suffix . ']' . $item['attribute'];
                      } else {
                          $attr = $item['attribute'];
                      }
                      $method = $item['name'] . 'ControlGroup';
                      $options = isset($item['options'])?$item['options']:array();
                      echo $this->form->$method($this->model, $attr, $options);
                      break;
                  case 'listInput':
                      if (isset($this->suffix)) {
                          $attr = '[' . $this->suffix . ']' . $item['attribute'];
                      } else {
                          $attr = $item['attribute'];
                      }
                      $method = $item['name'] . 'ControlGroup';
                      $options = isset($item['options'])?$item['options']:array();
                      echo $this->form->$method($this->model, $attr, $item['data'], $options);
                      break;
                  default:
                      throw new CException('Not implemented!!!');
              }
          }
      }
  
  }