]>
Commit | Line | Data |
---|---|---|
6527f429 DM |
1 | <?php\r |
2 | $date_format = 'Y-j-m';\r | |
3 | \r | |
4 | /**\r | |
5 | * Returns an array containing a random data type and the editor\r | |
6 | * associated with that type, used by the generateData function.\r | |
7 | */\r | |
8 | function generateType($index) {\r | |
9 | global $date_format;\r | |
10 | $rand = $index === 0 ? 1 : rand(0, 4);\r | |
11 | \r | |
12 | switch ($rand) {\r | |
13 | case 0:\r | |
14 | return array('data' => 'string', 'editor' => array(\r | |
15 | 'xtype' => 'textfield',\r | |
16 | 'allowBlank' => false\r | |
17 | ));\r | |
18 | case 1:\r | |
19 | return array('data' => 'int', 'editor' => array(\r | |
20 | 'xtype' => 'numberfield',\r | |
21 | 'minValue' => 1,\r | |
22 | 'maxValue' => 200\r | |
23 | ));\r | |
24 | case 2:\r | |
25 | return array('data' => 'date', 'editor' => array(\r | |
26 | 'xtype' => 'datefield',\r | |
27 | 'format' => $date_format\r | |
28 | ));\r | |
29 | case 3:\r | |
30 | return array('data' => 'float', 'editor' => array(\r | |
31 | 'xtype' => 'numberfield',\r | |
32 | 'minValue' => 400,\r | |
33 | 'maxValue' => 800\r | |
34 | ));\r | |
35 | case 4:\r | |
36 | return array('data' => 'bool', 'editor' => array(\r | |
37 | 'xtype' => 'checkbox'\r | |
38 | ));\r | |
39 | }\r | |
40 | }\r | |
41 | \r | |
42 | /**\r | |
43 | * Returns a hard-coded data value matching the type passed in.\r | |
44 | */\r | |
45 | function getDataValue($type) {\r | |
46 | global $date_format;\r | |
47 | switch ($type['data']) {\r | |
48 | case 'string':\r | |
49 | return 'data';\r | |
50 | case 'int':\r | |
51 | return 123;\r | |
52 | case 'date':\r | |
53 | return date($date_format);\r | |
54 | case 'float':\r | |
55 | return 456.78;\r | |
56 | case 'bool':\r | |
57 | return true;\r | |
58 | }\r | |
59 | return $type;\r | |
60 | }\r | |
61 | \r | |
62 | /**\r | |
63 | * Generates all of the test data and field/column definitions that will\r | |
64 | * make up the data and metadata for this request.\r | |
65 | */\r | |
66 | function generateData() {\r | |
67 | global $date_format;\r | |
68 | $row_count = rand(10, 30);\r | |
69 | $col_count = rand(3, 7);\r | |
70 | $types = array();\r | |
71 | $data['data'] = array();\r | |
72 | $fields = array();\r | |
73 | $columns = array();\r | |
74 | $defineFields = true;\r | |
75 | \r | |
76 | for ($i=0; $i<$row_count; $i++) {\r | |
77 | \r | |
78 | for ($j=0; $j<$col_count; $j++) {\r | |
79 | // first pass through columns only, define fields and columns\r | |
80 | if ($defineFields) {\r | |
81 | // generate a random data type for the field/column\r | |
82 | $type = generateType($j);\r | |
83 | array_push($types, $type);\r | |
84 | \r | |
85 | // =====================================================================\r | |
86 | // define the default placeholder field definition. this fields\r | |
87 | // config is supported by the metachange handling in Ext by default\r | |
88 | // to reconfigure the data store's field definitions.\r | |
89 | $field = array(\r | |
90 | 'name' => 'field-'.($j+1),\r | |
91 | 'type' => $type['data']\r | |
92 | );\r | |
93 | // add any type-specific field attributes\r | |
94 | if ($type['data'] === 'date') {\r | |
95 | $field['dateFormat'] = $date_format;\r | |
96 | }\r | |
97 | // add the field to the fields list\r | |
98 | array_push($fields, $field);\r | |
99 | \r | |
100 | // =====================================================================\r | |
101 | // define the default placeholder column definition to match the field.\r | |
102 | // note that this columns block only applies to grids. in the past the \r | |
103 | // fields config was reused both by the store and also by grids, but since\r | |
104 | // it is usually preferable to add column-specific metadata that the store\r | |
105 | // doesn't care about, it's usually better to split the two definitions.\r | |
106 | $col = array(\r | |
107 | 'dataIndex' => 'field-'.($j+1)\r | |
108 | );\r | |
109 | // add in column-specific attributes\r | |
110 | if ($j === 0) {\r | |
111 | // special config for the id column, fixed width and non-editable\r | |
112 | $col['text'] = 'ID';\r | |
113 | $col['width'] = 40;\r | |
114 | }\r | |
115 | else {\r | |
116 | $col['text'] = 'Field '.($j+1).' ('.$type['data'].')';\r | |
117 | $col['editor'] = $type['editor'];\r | |
118 | $col['flex'] = 1;\r | |
119 | }\r | |
120 | // add in type-specific column attributes\r | |
121 | switch ($type['data']) {\r | |
122 | case 'date':\r | |
123 | $col['xtype'] = 'datecolumn';\r | |
124 | $col['format'] = $date_format;\r | |
125 | break;\r | |
126 | case 'float':\r | |
127 | $col['xtype'] = 'numbercolumn';\r | |
128 | $col['format'] = '$0.00';\r | |
129 | break;\r | |
130 | case 'bool':\r | |
131 | //$col['xtype'] = 'checkcolumn';\r | |
132 | break;\r | |
133 | }\r | |
134 | // finally, add the column to the columns list\r | |
135 | array_push($columns, $col);\r | |
136 | }\r | |
137 | \r | |
138 | // every row/col pass, load up some data\r | |
139 | $row['field-'.($j+1)] = $j == 0 ? ($i+1) : getDataValue($types[$j]);\r | |
140 | }\r | |
141 | \r | |
142 | // flip this flag after the first column pass since the fields are defined\r | |
143 | $defineFields = false;\r | |
144 | \r | |
145 | // add the row of generated data to the top-level data object\r | |
146 | // that will be returned in the response\r | |
147 | array_push($data['data'], $row);\r | |
148 | }\r | |
149 | \r | |
150 | // assemble the metadata\r | |
151 | $meta = array();\r | |
152 | $meta['fields'] = $fields;\r | |
153 | $meta['columns'] = $columns;\r | |
154 | $meta['root'] = 'data';\r | |
155 | $meta['idProperty'] = 'field-1';\r | |
156 | $meta['messageProperty'] = 'msg';\r | |
157 | \r | |
158 | // assemble the top-level data object being returned.\r | |
159 | // the data is already in $data['data'] at this point.\r | |
160 | $data['metaData'] = $meta;\r | |
161 | $data['total'] = $row_count;\r | |
162 | $data['msg'] = 'Success!';\r | |
163 | \r | |
164 | return $data;\r | |
165 | }\r | |
166 | \r | |
167 | echo json_encode(generateData());\r | |
168 | ?> |