meta-config-basic.php 4.7 KB

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