Просмотр исходного кода

added generate isInstance sql function at object reinstall

Piotr Labudda 8 лет назад
Родитель
Сommit
b9fc873498
2 измененных файлов с 70 добавлено и 8 удалено
  1. 46 0
      SE/se-lib/ACL.php
  2. 24 8
      SE/se-lib/Route/Storage.php

+ 46 - 0
SE/se-lib/ACL.php

@@ -462,6 +462,52 @@ class ACL {
 		return $refInfo;
 	}
 
+	public static function generateIsInstanceFunctionBody($namespace, $item = null) {
+		if (!$item) $item = SchemaFactory::loadDefaultObject('SystemObject')->getItem($namespace, [ 'propertyName' => '*,field' ]);
+		if (!in_array( $item['_type'], [ 'AntAcl' ] )) return null;
+		$dbName = DB::getPDO()->getDatabaseName();
+		$sqlFunBody = " RETURN 1; ";
+		$localFieldsWithRestrictions = array_filter($item['field'], function ($field) {
+			if (!$field['isLocal']) return false;
+			if (empty($field['xsdRestrictions'])) return false;
+			if ('[]' == $field['xsdRestrictions']) return false;
+			return true;
+		});
+		// TODO: get fields with minOccurs > 1 (may require select by ref)
+		$sqlTablePrefix = 'root';
+		$sqlWhereFromRestrictions = (!empty($localFieldsWithRestrictions))
+		?	array_reduce(
+				array_map(function ($field) use ($sqlTablePrefix) {
+					$sqlRestrictions = [];
+					// 'xsdRestrictions' => '{"enumeration":{"PROCES":"PROCES"}}',
+					$restrictions = @json_decode($field['xsdRestrictions'], $assoc = true);
+					if (!empty($restrictions)) {
+						if (!empty($restrictions['enumeration'])) {
+							$sqlRestrictions[] = "{$sqlTablePrefix}.`{$field['fieldNamespace']}` in (" . implode(",", array_map([DB::getPDO(), 'quote'], array_keys($restrictions['enumeration']))) . ")";
+						}
+					}
+					return $sqlRestrictions;
+				}, $localFieldsWithRestrictions),
+				function ($ret, $cur) {
+					return array_merge($ret, array_filter($cur, ['V', 'filterNotEmpty']));
+				},
+				[]
+			)
+		:	'';
+		DBG::nicePrint($localFieldsWithRestrictions, "\$localFieldsWithRestrictions");
+		DBG::nicePrint($sqlWhereFromRestrictions, "\$sqlWhereFromRestrictions");
+		$sqlWhereFromRestrictions = (!empty($sqlWhereFromRestrictions)) ? implode(" and ", $sqlWhereFromRestrictions) : "1=1";
+		$pkField = 'ID'; // TODO: primaryKeyField into SystemObject structure
+		$rootTableName = $item['_rootTableName'];
+		$sqlFunBody = (!empty($sqlWhereFromRestrictions))
+		?	" RETURN IF(
+				(select count(1) as cnt from `{$rootTableName}` root where root.`{$pkField}` = pk and {$sqlWhereFromRestrictions}) > 0
+				, 1, 0)
+			"
+		:	" RETURN 1; ";
+		return $sqlFunBody;
+	}
+
 	public static function getInstanceId($namespace) {
 		$conf = self::getInstanceConfig($namespace);
 		return $conf['id'];

+ 24 - 8
SE/se-lib/Route/Storage.php

@@ -1596,16 +1596,32 @@ jQuery(document).on('p5UIBtnAjax:Storage:checkObjectInstallAjax:ajaxLoaded', fun
 			$objFieldAcl = new Schema_SystemObjectFieldStorageAcl();
 			$objFieldAcl->updateCache($namespace);
 
+			DBG::nicePrint([
+				'idInstance' => ACL::getInstanceId($namespace),
+				'rootInstance' => ACL::getRootNamespace($namespace),
+				'conf' => ACL::fetchInstanceConfig($namespace),
+				'table' => ACL::getInstanceTable($namespace),
+			], "dbg");
+
 			{
-				DBG::nicePrint([
-					'idInstance' => ACL::getInstanceId($namespace),
-					'rootInstance' => ACL::getRootNamespace($namespace),
-					'conf' => ACL::fetchInstanceConfig($namespace),
-					'table' => ACL::getInstanceTable($namespace),
-				], "dbg");
-				throw new Exception("TODO...");
+				$item = SchemaFactory::loadDefaultObject('SystemObject')->getItem($namespace, [ 'propertyName' => '*,field' ]);
+				if ('AntAcl' === $item['_type']) {
+					$sqlFunBody = ACL::generateIsInstanceFunctionBody($namespace, $item);
+					DBG::nicePrint($sqlFunBody, "\$sqlFunBody");
+					DB::getPDO()->execSql(" DROP FUNCTION IF EXISTS `{$dbName}`.`isInstance_{$namespace}` ");
+					// CREATE
+					//     [DEFINER = { user | CURRENT_USER }]
+					//     FUNCTION sp_name ([func_parameter[,...]])
+					//     RETURNS type
+					//     [characteristic ...] routine_body
+					DB::getPDO()->execSql("
+						CREATE DEFINER=`root`@`localhost`
+						FUNCTION `{$dbName}`.`isInstance_{$namespace}` ( pk INT(11) )
+						RETURNS TINYINT(1)
+						{$sqlFunBody}
+					");
+				}
 			}
-
 		} catch (Exception $e) {
 			UI::alert('danger', "Error #" . $e->getCode() .  "|" . $e->getLine() .  ": " . $e->getMessage());
 			DBG::log($e);