-
Notifications
You must be signed in to change notification settings - Fork 17
/
FormBase.php
156 lines (137 loc) · 4.06 KB
/
FormBase.php
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php namespace pceuropa\forms;
use Yii;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
/**
* Base method for Yii2-forms extension
* @author Rafal Marguzewicz <info@pceuropa.net>
* @version 1.5.1
* @license MIT
* https://github.com/pceuropa/yii2-forum
* Please report all issues at GitHub
* https://github.com/pceuropa/yii2-forum/issues
*/
class FormBase {
/**
* Filter fields array and retun only fields with attribute name (data fields)
* @param array $array Array represent elements of form
* @return array Only fields with attribute name
*/
public function gridViewItemsForm($array = []){
yield ['class' => 'yii\grid\SerialColumn'];
foreach ($array as $key_row => $row) {
foreach ($row as $key => $v) {
$field = $array[$key_row][$key];
if (ArrayHelper::keyExists('name', $field) ){
yield $field['name'];
}
}
}
yield [
'class' => 'yii\grid\ActionColumn',
'template' => ' {delete}',
'buttons' => [
'delete' => function ($url, $model) {
return Html::a(
'<span class="glyphicon glyphicon-trash"></span>',
['deleteitem', 'id'=> $model['id'], 'form' => $_GET['id']],
[
'data-method' => 'post',
'data-confirm' => 'Are you sure ?'
]);
}
]
];
}
public function onlyCorrectDataFields($array = []){
$data_fields = [];
foreach ($array as $key_row => $row) {
foreach ($row as $key => $v) {
$field = $array[$key_row][$key];
if (ArrayHelper::keyExists('name', $field) ){
array_push($data_fields, $field);
}
}
}
return $data_fields;
}
/**
* Generate table shema need to create table
* Get array with data fields and
* @param array $array Data fields
* @return array
*/
public static function tableSchema($array = []){
$schema = [];
foreach ($array as $row) {
foreach ($row as $v) {
if ( isset($v['name']) ){
$schema[$v['name']] = ($v['field'] === 'textarea') ? 'text' : 'string';
}
}
}
return $schema;
}
/**
* Return column type.
* Need to add column in table SQL
* @param array $field Data one field.
* @return null|string
*/
public function getColumnType($field){
if (!ArrayHelper::keyExists('field', $field)){
return null;
}
$type = [
'input' => [
'text' => 'string',
'email' => 'string',
'password' => 'string',
'date' => 'string',
'number' => 'integer',
'url' => 'string',
'tel' => 'string',
'url' => 'string',
'color' => 'string',
'range' => 'string',
'url' => 'string',
],
'textarea' => 'text',
'checkbox' => 'string',
'radio' => 'string',
'select' => 'string',
];
if ($field['field'] === 'input'){
return $type[ $field['field'] ][ $field['type'] ];
}
return $type[ $field['field'] ];
}
/**
* Return validation rule type.
* Need to dynamic model validation
* @param array $f Data field.
* @return null|array
*/
public function ruleType($f){
$types = ['input' => [
'text' => 'string',
'email' => 'email',
'password' => 'string',
'date' => 'date',
'number' => 'integer',
'url' => 'url',
'tel' => 'string',
'color' => 'string',
'range' => 'string',
],
'textarea' => 'string',
'checkbox' => 'each' ,
'radio' => 'string',
'select' => 'string',
];
if ($f['field'] === 'input'){
return $types[$f['field']][$f['type']];
}
return $types[$f['field']];
}
}