1 <?php
2
3 4 5
6 class CodeProvider
7 {
8
9 public function resolveController($relation)
10 {
11 $model = new $relation[1];
12 $reflection = new ReflectionClass($model);
13 $module = preg_match("/\/modules\/([a-zA-Z0-9]+)\//", $reflection->getFileName(), $matches);
14
15
16 $modulePrefix = "";
17
18 $controller = $modulePrefix . strtolower(substr($relation[1], 0, 1)) . substr($relation[1], 1);
19
20 return $controller;
21 }
22
23 public function ($model, $relationName, $relationInfo)
24 {
25 $controller = $this->resolveController($relationInfo);
26
27 $code = "";
28
29
30 $code .= "\$this->widget('bootstrap.widgets.TbButtonGroup', array(
31 'type'=>'', // '', 'primary', 'info', 'success', 'warning', 'danger' or 'inverse'
32 'buttons'=>array(
33 array('label'=>'{$relationName}', 'icon'=>'icon-list-alt', 'url'=> array('{$controller}/admin')),
34 array('icon'=>'icon-plus', 'url'=>array('" . $controller . "/create', '$relationInfo[1]' => array('$relationInfo[2]'=>\$model->{\$model->tableSchema->primaryKey}))),
35 ),
36 ));";
37
38 return $code;
39 }
40
41 public function generateRelation($model, $relationName, $relationInfo)
42 {
43 if ($columns = CActiveRecord::model($relationInfo[1])->tableSchema->columns) {
44
45 $suggestedfield = FullCrudCode::suggestName($columns);
46 $field = current($columns);
47 $style = $relationInfo[0] == 'CManyManyRelation' ? 'multiselect' : 'dropdownlist';
48
49 if (is_object($field)) {
50 if ($relationInfo[0] == 'CManyManyRelation') {
51 $allowEmpty = 'false';
52 }
53 elseif ($relationInfo[0] == 'CHasOneRelation') {
54 $allowEmpty = (CActiveRecord::model($relationInfo[1])->tableSchema->columns[$relationInfo[2]]->allowNull ?
55 'true' : 'false');
56
57 return "if (\$model->{$relationName} !== null) echo \$model->{$relationName}->{$suggestedfield};";
58 }
59 else {
60 $allowEmpty = (CActiveRecord::model($model)->tableSchema->columns[$relationInfo[2]]->allowNull ?
61 'true' : 'false');
62 }
63
64 return ("\$this->widget(
65 'Relation',
66 array(
67 'model' => \$model,
68 'relation' => '{$relationName}',
69 'fields' => '{$suggestedfield}',
70 'allowEmpty' => {$allowEmpty},
71 'style' => '{$style}',
72 'htmlOptions' => array(
73 'checkAll' => 'all'),
74 )
75 )");
76 }
77 }
78 }
79
80 81 82 83
84 public function generateValueField($modelClass, $column, $view = false)
85 {
86 if ($column->isForeignKey) {
87
88 $model = CActiveRecord::model($modelClass);
89 $table = $model->getTableSchema();
90 $fk = $table->foreignKeys[$column->name];
91
92
93
94 foreach ($model->relations() as $key => $value) {
95 if (strcasecmp($value[2], $column->name) == 0) {
96 $relation = $value;
97 }
98 }
99
100 if (!isset($relation)) {
101 return "'" . $column->name . "'";
102 }
103
104 $fmodel = CActiveRecord::model($relation[1]);
105 $fmodelName = $relation[1];
106
107 $modelTable = ucfirst($fmodel->tableName());
108 $fcolumns = $fmodel->attributeNames();
109
110 111 112
113
114
115 $relname = strtolower($fk[0]);
116 foreach ($model->relations() as $key => $value) {
117 if (strcasecmp($value[2], $column->name) == 0) {
118 $relname = $key;
119 }
120 }
121
122
123
124 if ($view === true) {
125 return "array(
126 'name'=>'{$column->name}',
127 'value'=>CHtml::value(\$model,'{$relname}.{$fcolumns[1]}'),
128 )";
129 }
130 elseif ($view == 'search') {
131 return "echo \$form->dropDownList(\$model,'{$column->name}',CHtml::listData({$fmodelName}::model()->findAll(), '{$fmodel->getTableSchema()->primaryKey}', '{$fcolumns[1]}'),array('prompt'=>'all'))";
132 }
133 else {
134 return "array(
135 'name'=>'{$column->name}',
136 'value'=>'CHtml::value(\$data,\\'{$relname}.{$fcolumns[1]}\\')',
137 'filter'=>CHtml::listData({$fmodelName}::model()->findAll(), '{$fcolumns[0]}', '{$fcolumns[1]}'),
138 )";
139 }
140
141 }
142 else {
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
162 if ($column->name == 'createtime'
163 or $column->name == 'updatetime'
164 or $column->name == 'timestamp'
165 ) {
166 return "array(
167 'name'=>'{$column->name}',
168 'value' =>'date(\"Y. m. d G:i:s\", \$data->{$column->name})')";
169 }
170 else {
171 return ("'" . $column->name . "'");
172 }
173
174 }
175 }
176
177 }
178
179 ?>
180