meta-config-basic.php
4.71 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
<?php
$date_format = 'Y-j-m';
/**
* Returns an array containing a random data type and the editor
* associated with that type, used by the generateData function.
*/
function generateType($index) {
global $date_format;
$rand = $index === 0 ? 1 : rand(0, 4);
switch ($rand) {
case 0:
return array('data' => 'string', 'editor' => array(
'xtype' => 'textfield',
'allowBlank' => false
));
case 1:
return array('data' => 'int', 'editor' => array(
'xtype' => 'numberfield',
'minValue' => 1,
'maxValue' => 200
));
case 2:
return array('data' => 'date', 'editor' => array(
'xtype' => 'datefield',
'format' => $date_format
));
case 3:
return array('data' => 'float', 'editor' => array(
'xtype' => 'numberfield',
'minValue' => 400,
'maxValue' => 800
));
case 4:
return array('data' => 'bool', 'editor' => array(
'xtype' => 'checkbox'
));
}
}
/**
* Returns a hard-coded data value matching the type passed in.
*/
function getDataValue($type) {
global $date_format;
switch ($type['data']) {
case 'string':
return 'data';
case 'int':
return 123;
case 'date':
return date($date_format);
case 'float':
return 456.78;
case 'bool':
return true;
}
return $type;
}
/**
* Generates all of the test data and field/column definitions that will
* make up the data and metadata for this request.
*/
function generateData() {
global $date_format;
$row_count = rand(10, 30);
$col_count = rand(5, 10);
$types = array();
$data['data'] = array();
$fields = array();
$columns = array();
$defineFields = true;
for ($i=0; $i<$row_count; $i++) {
for ($j=0; $j<$col_count; $j++) {
// first pass through columns only, define fields and columns
if ($defineFields) {
// generate a random data type for the field/column
$type = generateType($j);
array_push($types, $type);
// =====================================================================
// define the default placeholder field definition. this fields
// config is supported by the metachange handling in Ext by default
// to reconfigure the data store's field definitions.
$field = array(
'name' => 'field-'.($j+1),
'type' => $type['data']
);
// add any type-specific field attributes
if ($type['data'] === 'date') {
$field['dateFormat'] = $date_format;
}
// add the field to the fields list
array_push($fields, $field);
// =====================================================================
// define the default placeholder column definition to match the field.
// note that this columns block only applies to grids. in the past the
// fields config was reused both by the store and also by grids, but since
// it is usually preferable to add column-specific metadata that the store
// doesn't care about, it's usually better to split the two definitions.
$col = array(
'dataIndex' => 'field-'.($j+1)
);
// add in column-specific attributes
if ($j === 0) {
// special config for the id column, fixed width and non-editable
$col['text'] = 'ID';
$col['width'] = 40;
}
else {
$col['text'] = 'Field '.($j+1).' ('.$type['data'].')';
$col['editor'] = $type['editor'];
$col['flex'] = 1;
}
// add in type-specific column attributes
switch ($type['data']) {
case 'date':
$col['xtype'] = 'datecolumn';
$col['format'] = $date_format;
break;
case 'float':
$col['xtype'] = 'numbercolumn';
$col['format'] = '$0.00';
break;
case 'bool':
//$col['xtype'] = 'checkcolumn';
break;
}
// finally, add the column to the columns list
array_push($columns, $col);
}
// every row/col pass, load up some data
$row['field-'.($j+1)] = $j == 0 ? ($i+1) : getDataValue($types[$j]);
}
// flip this flag after the first column pass since the fields are defined
$defineFields = false;
// add the row of generated data to the top-level data object
// that will be returned in the response
array_push($data['data'], $row);
}
// assemble the metadata
$meta = array();
$meta['fields'] = $fields;
$meta['columns'] = $columns;
$meta['root'] = 'data';
$meta['idProperty'] = 'field-1';
$meta['messageProperty'] = 'msg';
// assemble the top-level data object being returned.
// the data is already in $data['data'] at this point.
$data['metaData'] = $meta;
$data['total'] = $row_count;
$data['msg'] = 'Success!';
return $data;
}
echo json_encode(generateData());
?>