api de gestion de ticket, basé sur php-crud-api. Le but est de décorrélé les outils de gestion des données, afin
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

php_crud_api_transform.js 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. function php_crud_api_transform(tables) {
  2. var array_flip = function (trans) {
  3. var key, tmp_ar = {};
  4. for (key in trans) {
  5. tmp_ar[trans[key]] = key;
  6. }
  7. return tmp_ar;
  8. };
  9. var get_objects = function (tables,table_name,where_index,match_value) {
  10. var objects = [];
  11. for (var record in tables[table_name]['records']) {
  12. record = tables[table_name]['records'][record];
  13. if (!where_index || record[where_index]==match_value) {
  14. var object = {};
  15. for (var index in tables[table_name]['columns']) {
  16. var column = tables[table_name]['columns'][index];
  17. object[column] = record[index];
  18. for (var relation in tables) {
  19. var reltable = tables[relation];
  20. for (var key in reltable['relations']) {
  21. var target = reltable['relations'][key];
  22. if (target == table_name+'.'+column) {
  23. column_indices = array_flip(reltable['columns']);
  24. object[relation] = get_objects(tables,relation,column_indices[key],record[index]);
  25. }
  26. }
  27. }
  28. }
  29. objects.push(object);
  30. }
  31. }
  32. return objects;
  33. };
  34. tree = {};
  35. for (var name in tables) {
  36. var table = tables[name];
  37. if (!table['relations']) {
  38. tree[name] = get_objects(tables,name);
  39. if (table['results']) {
  40. tree['_results'] = table['results'];
  41. }
  42. }
  43. }
  44. return tree;
  45. }