Browse Source

use charset when create instance view

Piotr Labudda 8 năm trước cách đây
mục cha
commit
1a53d56cd8
1 tập tin đã thay đổi với 12 bổ sung5 xóa
  1. 12 5
      SE/se-lib/InstanceConfig.php

+ 12 - 5
SE/se-lib/InstanceConfig.php

@@ -154,19 +154,24 @@ class InstanceConfig {
 		]));
 		]));
 	}
 	}
 
 
-	static function generateSqlWhereFromFieldsWithRestrictions($fieldsRestrictions, $sqlTablePrefix = 't') { // @param $fieldsRestrictions Array [ fieldName => xsdRestrictions | Null ]
+	static function generateSqlWhereFromFieldsWithRestrictions($fieldsRestrictions, $sqlTablePrefix = 't', $charset = 'utf8') { // @param $fieldsRestrictions Array [ fieldName => xsdRestrictions | Null ]
 		// TODO: add @param $acl to check if field is local, and if not then generate remote query (join)
 		// TODO: add @param $acl to check if field is local, and if not then generate remote query (join)
 		if (empty($fieldsRestrictions)) return '';
 		if (empty($fieldsRestrictions)) return '';
 		$fieldsWithRestrictions = array_filter($fieldsRestrictions, ['V', 'filterNotEmpty']);
 		$fieldsWithRestrictions = array_filter($fieldsRestrictions, ['V', 'filterNotEmpty']);
 		if (empty($fieldsWithRestrictions)) return '';
 		if (empty($fieldsWithRestrictions)) return '';
 
 
 		return array_reduce(
 		return array_reduce(
-			array_map(function ($fieldName, $xsdRestriction) use ($sqlTablePrefix) {
+			array_map(function ($fieldName, $xsdRestriction) use ($sqlTablePrefix, $charset) {
 				$sqlRestrictions = [];
 				$sqlRestrictions = [];
 				if (false !== strpos($fieldName, ':')) return $sqlRestrictions; // SKIP ref fields - TODO: generate remote query, require $acl
 				if (false !== strpos($fieldName, ':')) return $sqlRestrictions; // SKIP ref fields - TODO: generate remote query, require $acl
 				if (!empty($xsdRestriction)) {
 				if (!empty($xsdRestriction)) {
 					if (!empty($xsdRestriction['enumeration'])) {
 					if (!empty($xsdRestriction['enumeration'])) {
-						$sqlRestrictions[] = "{$sqlTablePrefix}.`{$fieldName}` in (" . implode(",", array_map([DB::getPDO(), 'quote'], array_keys($xsdRestriction['enumeration']))) . ")";
+						// $sqlRestrictions[] = "{$sqlTablePrefix}.`{$fieldName}` in (" . implode(",", array_map([DB::getPDO(), 'quote'], array_keys($xsdRestriction['enumeration']))) . ")";
+						$sqlRestrictions[] = "{$sqlTablePrefix}.`{$fieldName}` in (" . implode(",", array_map(function ($option) use ($charset) {
+							return ($charset && $charset !== 'utf8')
+							?	"CONVERT(" . DB::getPDO()->quote($option) . " using {$charset})"
+							:	DB::getPDO()->quote($option);
+						}, array_keys($xsdRestriction['enumeration']))) . ")";
 					}
 					}
 				}
 				}
 				return $sqlRestrictions;
 				return $sqlRestrictions;
@@ -247,10 +252,12 @@ class InstanceConfig {
 			return $ret;
 			return $ret;
 		}, []);
 		}, []);
 		$sqlTablePrefix = "instance_{$idInstance}";
 		$sqlTablePrefix = "instance_{$idInstance}";
-		$sqlListWhere = self::generateSqlWhereFromFieldsWithRestrictions($fieldsWithRestrictions, $sqlTablePrefix);
+		$appInfo = (!empty($item['appInfo'])) ? @json_decode($item['appInfo'], $assoc = true) : null;
+		$charset = (!empty($appInfo) && !empty($appInfo['table_structure']['@charset'])) ? $appInfo['table_structure']['@charset'] : null;
+		$sqlListWhere = self::generateSqlWhereFromFieldsWithRestrictions($fieldsWithRestrictions, $sqlTablePrefix, $charset);
 		$sqlWhere = (!empty($sqlListWhere)) ? implode("\n\t  and ", $sqlListWhere) : "1=1";
 		$sqlWhere = (!empty($sqlListWhere)) ? implode("\n\t  and ", $sqlListWhere) : "1=1";
 		$rootTableName = $item['_rootTableName'];
 		$rootTableName = $item['_rootTableName'];
-		$primaryKey = 'ID'; // TODO: primaryKeyField into SystemObject structure // TODO: is in struct, read from `primaryKey`? field
+		$primaryKey = V::get('primaryKey', 'ID', $item);
 		return implode("\n\t", [
 		return implode("\n\t", [
 			"select {$primaryKey}",
 			"select {$primaryKey}",
 			"from `{$rootTableName}` `{$sqlTablePrefix}`",
 			"from `{$rootTableName}` `{$sqlTablePrefix}`",