OBJ.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. /**
  3. * json files in schema/gui/core/
  4. * default_db-{tbl_name}.json - config for raw table - no 'parent_object' defined
  5. * {object_name}.json - config for object - require 'parent_object' (another object or default_db-* raw table object)
  6. */
  7. class OBJ {
  8. public static function getLabel($json) {
  9. if (empty($json['label'])) return null;
  10. return (is_array($json['label']))? end($json['label']) : $json['label'];
  11. }
  12. // @returns array of parent object names
  13. public static function getParentList($json) {
  14. if (empty($json['parent_object'])) return array();
  15. return (!is_array($json['parent_object']))? array($json['parent_object']) : array_reverse($json['parent_object']);
  16. }
  17. public static function getFields($json) {
  18. if (empty($json['fields'])) return array();
  19. return $json['fields'];
  20. }
  21. public static function getCoreObjectFromFile($objectName) {
  22. $objectFileName = str_replace("/", '-', $objectName);
  23. $filePath = APP_PATH_SCHEMA . "/gui/core/{$objectFileName}.json";
  24. if (!file_exists($filePath)) throw new Exception("File not exists {$filePath}", 404);
  25. $json = file_get_contents($filePath);
  26. DBG::_('DBG', '>1', "{$objectName} filePath", $filePath, __CLASS__, __FUNCTION__, __LINE__);
  27. DBG::_('DBG', '>1', "{$objectName} file content", $json, __CLASS__, __FUNCTION__, __LINE__);
  28. $json = @json_decode($json, $assoc = true);
  29. DBG::_('DBG', '>1', "{$objectName} json_last_error()", json_last_error(), __CLASS__, __FUNCTION__, __LINE__);
  30. if (null == $json && 0 !== json_last_error()) throw new Exception("Parse json error for object '{$objectName}': " . json_last_error());
  31. if (empty($json['parent_object'])) return $json;
  32. $jsonParent = array();
  33. $parent = (is_array($json['parent_object']))? end($json['parent_object']) : $json['parent_object'];
  34. $jsonParent = self::getCoreObjectFromFile($parent);
  35. return array_merge_recursive($jsonParent, $json);
  36. }
  37. public static function getCoreObjectList() {
  38. $objectList = array();
  39. $files = glob(APP_PATH_SCHEMA . "/gui/core/*.json", GLOB_NOSORT);
  40. //DBG::_(true, true, "files", $files, __CLASS__, __FUNCTION__, __LINE__);
  41. foreach ($files as $filePath) {
  42. $fileName = basename($filePath);
  43. $objName = substr($fileName, 0, -5);// remove ext '.json'
  44. $objectList[] = $objName;
  45. }
  46. return $objectList;
  47. }
  48. }