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.php 1.1KB

12345678910111213141516171819202122232425262728293031323334
  1. <?php
  2. function php_crud_api_transform(&$tables) {
  3. $get_objects = function (&$tables,$table_name,$where_index=false,$match_value=false) use (&$get_objects) {
  4. $objects = array();
  5. foreach ($tables[$table_name]['records'] as $record) {
  6. if ($where_index===false || $record[$where_index]==$match_value) {
  7. $object = array();
  8. foreach ($tables[$table_name]['columns'] as $index=>$column) {
  9. $object[$column] = $record[$index];
  10. foreach ($tables as $relation=>$reltable) {
  11. foreach ($reltable['relations'] as $key=>$target) {
  12. if ($target == "$table_name.$column") {
  13. $column_indices = array_flip($reltable['columns']);
  14. $object[$relation] = $get_objects($tables,$relation,$column_indices[$key],$record[$index]);
  15. }
  16. }
  17. }
  18. }
  19. $objects[] = $object;
  20. }
  21. }
  22. return $objects;
  23. };
  24. $tree = array();
  25. foreach ($tables as $name=>$table) {
  26. if (!isset($table['relations'])) {
  27. $tree[$name] = $get_objects($tables,$name);
  28. if (isset($table['results'])) {
  29. $tree['_results'] = $table['results'];
  30. }
  31. }
  32. }
  33. return $tree;
  34. }