Selaa lähdekoodia

added OBJ class, object view in Storage

Piotr Labudda 9 vuotta sitten
vanhempi
commit
d7003a55a6
2 muutettua tiedostoa jossa 92 lisäystä ja 28 poistoa
  1. 56 0
      SE/se-lib/OBJ.php
  2. 36 28
      SE/se-lib/Route/Storage.php

+ 56 - 0
SE/se-lib/OBJ.php

@@ -0,0 +1,56 @@
+<?php
+
+/**
+ * json files in schema/gui/core/
+ *   default_db-{tbl_name}.json - config for raw table - no 'parent_object' defined
+ *   {object_name}.json - config for object - require 'parent_object' (another object or default_db-* raw table object)
+ */
+class OBJ {
+
+  public static function getLabel($json) {
+    if (empty($json['label'])) return null;
+    return (is_array($json['label']))? end($json['label']) : $json['label'];
+  }
+
+  // @returns array of parent object names
+  public static function getParentList($json) {
+    if (empty($json['parent_object'])) return array();
+    return (!is_array($json['parent_object']))? array($json['parent_object']) : array_reverse($json['parent_object']);
+  }
+
+  public static function getFields($json) {
+    if (empty($json['fields'])) return array();
+    return $json['fields'];
+  }
+
+  public static function getCoreObjectFromFile($objectName) {
+		$objectFileName = str_replace("/", '-', $objectName);
+		$filePath = APP_PATH_SCHEMA . "/gui/core/{$objectFileName}.json";
+		if (!file_exists($filePath)) throw new Exception("File not exists {$filePath}", 404);
+
+		$json = file_get_contents($filePath);
+		DBG::_('DBG', '>1', "{$objectName} filePath", $filePath, __CLASS__, __FUNCTION__, __LINE__);
+		DBG::_('DBG', '>1', "{$objectName} file content", $json, __CLASS__, __FUNCTION__, __LINE__);
+		$json = @json_decode($json, $assoc = true);
+		DBG::_('DBG', '>1', "{$objectName} json_last_error()", json_last_error(), __CLASS__, __FUNCTION__, __LINE__);
+		if (null == $json && 0 !== json_last_error()) throw new Exception("Parse json error for object '{$objectName}': " . json_last_error());
+		if (empty($json['parent_object'])) return $json;
+		$jsonParent = array();
+		$parent = (is_array($json['parent_object']))? end($json['parent_object']) : $json['parent_object'];
+		$jsonParent = self::getCoreObjectFromFile($parent);
+		return array_merge_recursive($jsonParent, $json);
+	}
+
+  public static function getCoreObjectList() {
+    $objectList = array();
+    $files = glob(APP_PATH_SCHEMA . "/gui/core/*.json", GLOB_NOSORT);
+    //DBG::_(true, true, "files", $files, __CLASS__, __FUNCTION__, __LINE__);
+    foreach ($files as $filePath) {
+			$fileName = basename($filePath);
+			$objName = substr($fileName, 0, -5);// remove ext '.json'
+      $objectList[] = $objName;
+    }
+    return $objectList;
+  }
+
+}

+ 36 - 28
SE/se-lib/Route/Storage.php

@@ -5,6 +5,7 @@ Lib::loadClass('RouteBase');
 Lib::loadClass('Schema_TableFactory');
 Lib::loadClass('Response');
 Lib::loadClass('UI');
+Lib::loadClass('OBJ');
 
 /*
 # Storage:
@@ -73,26 +74,6 @@ class Route_Storage extends RouteBase {
 		UI::dol();
 	}
 
-	public function getCoreObjectFromFile($objectName) {
-		if ('default_db/' === substr($objectName, 0, 11)) {
-			return array('name' => substr($objectName, 11));
-		}
-
-		$filePath = APP_PATH_SCHEMA . "/gui/core/{$objectName}.json";
-		if (!file_exists($filePath)) throw new Exception("File not exists", 404);
-
-		$json = file_get_contents($filePath);
-		DBG::_('DBG', '>1', "{$objectName} filePath", $filePath, __CLASS__, __FUNCTION__, __LINE__);
-		DBG::_('DBG', '>1', "{$objectName} file content", $json, __CLASS__, __FUNCTION__, __LINE__);
-		$json = @json_decode($json, $assoc = true);
-		DBG::_('DBG', '>1', "{$objectName} json_last_error()", json_last_error(), __CLASS__, __FUNCTION__, __LINE__);
-		if (null == $json && 0 !== json_last_error()) throw new Exception("Parse json error for object '{$objectName}': " . json_last_error());
-		$jsonParent = array();
-		$parent = (is_array($json['parent_object']))? end($json['parent_object']) : $json['parent_object'];
-		$jsonParent = $this->getCoreObjectFromFile($parent);
-		return array_merge_recursive($jsonParent, $json);
-	}
-
 	public function coreObjectStructAction() {
 		UI::gora();
 		UI::menu();
@@ -101,10 +82,41 @@ class Route_Storage extends RouteBase {
 
 			$object = V::get('object', '', $_REQUEST, 'word');
 			if (empty($object)) throw new Exception("Missing Object name");
-			$json = $this->getCoreObjectFromFile($object);
+			$json = OBJ::getCoreObjectFromFile($object);
+
+			$label = OBJ::getLabel($json);
+			$parentList = OBJ::getParentList($json);
+?>
+<div class="container">
+	<h1>Obiekt <code><?php echo $label; ?></code></h1>
+	<?php if (!empty($parentList)) : ?>
+		<p>Dziedziczy z: <?php echo implode(", ", $parentList); ?></p>
+	<?php endif; ?>
+	Struktura:
+	<table class="table table-bordered table-hovered">
+		<thead>
+			<tr>
+				<th>nazwa</th>
+				<th>typ</th>
+				<th>label</th>
+				<th>json</th>
+			</tr>
+		</thead>
+		<tbody>
+			<?php foreach (OBJ::getFields($json) as $fieldName => $field) : ?>
+				<tr>
+					<td><?php echo $fieldName; ?></td>
+					<td><?php echo $field['type']; ?></td>
+					<td><?php echo $field['label']; ?></td>
+					<td><?php echo json_encode($field); ?></td>
+				</tr>
+			<?php endforeach; ?>
+		</tbody>
+	</table>
+</div>
+<?php
 			DBG::_(true, true, "json", $json, __CLASS__, __FUNCTION__, __LINE__);
 
-
 		} catch (Exception $e) {
 			UI::alert('danger', "Error #" . $e->getCode() .  "|" . $e->getLine() .  ": " . $e->getMessage());
 		}
@@ -116,15 +128,11 @@ class Route_Storage extends RouteBase {
 		UI::menu();
 		$this->navView();
 		try {
-			$files = glob(APP_PATH_SCHEMA . "/gui/core/*.json", GLOB_NOSORT);
-			//DBG::_(true, true, "files", $files);
+			$coreObjlist = OBJ::getCoreObjectList();
 			$objectList = array();
-			foreach ($files as $filePath) {
+			foreach ($coreObjlist as $objName) {
 				$objItem = array();
-				$fileName = basename($filePath);
-				$objName = substr($fileName, 0, -5);// remove ext '.json'
 				$objItem['name'] = $objName;
-				$objItem['file_name'] = $fileName;
 				$objItem['label'] = "";// TODO: read from json
 				$objItem['struktura'] = '<a href="index.php?_route=Storage&_task=coreObjectStruct&object=' . $objName . '">' . "struct" . '</a>';
 				$objectList[] = $objItem;