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

updated code style - ACL, AntAclBase

Piotr Labudda 8 лет назад
Родитель
Сommit
669a80f138
2 измененных файлов с 217 добавлено и 217 удалено
  1. 12 12
      SE/se-lib/ACL.php
  2. 205 205
      SE/se-lib/AntAclBase.php

+ 12 - 12
SE/se-lib/ACL.php

@@ -481,26 +481,26 @@ SQL;
 		return $refInfo;
 	}
 
-	public static function getInstanceId($namespace) { // CRM_INSTANCE_CONFIG
+	public static function getInstanceId($namespace) {
 		$conf = self::getInstanceConfig($namespace);
 		return $conf['id'];
 	}
-	public static function getInstanceConfig($namespace) { // CRM_INSTANCE_CONFIG
+	public static function getInstanceConfig($namespace) { // @returns { id, namespace, rootNamespace, idInstanceBase, _createdAt }
 		try {
 			$conf = self::fetchInstanceConfig($namespace);
 		} catch (Exception $e) {
 			DB::getPDO()->execSql("
-	      create table if not exists `CRM_INSTANCE_CONFIG` (
-	        `id` int(11) not null AUTO_INCREMENT,
-	        `namespace` varchar(255) NOT NULL DEFAULT '',
-	        `rootNamespace` varchar(255) NOT NULL DEFAULT '',
-	        `idInstanceBase` int(11) NOT NULL DEFAULT 0,
+				create table if not exists `CRM_INSTANCE_CONFIG` (
+					`id` int(11) not null AUTO_INCREMENT,
+					`namespace` varchar(255) NOT NULL DEFAULT '',
+					`rootNamespace` varchar(255) NOT NULL DEFAULT '',
+					`idInstanceBase` int(11) NOT NULL DEFAULT 0,
 					`_createdAt` datetime NOT NULL,
-	        UNIQUE KEY `namespace` (`namespace`),
-	        KEY `rootNamespace` (`rootNamespace`),
+					UNIQUE KEY `namespace` (`namespace`),
+					KEY `rootNamespace` (`rootNamespace`),
 					PRIMARY KEY (`id`)
-	      ) ENGINE=MyISAM  DEFAULT CHARSET=latin2
-	    ");
+				) ENGINE=MyISAM  DEFAULT CHARSET=latin2
+			");
 			// TODO:?: `_tableInstalled` tinyint(1) not null default 0,
 			$conf = self::fetchInstanceConfig($namespace);
 		}
@@ -560,7 +560,7 @@ SQL;
 			'pk' => $pk,
 		]));
 	}
-	public static function getInstanceTable($namespace) {
+	public static function getInstanceTable($namespace) { // @returns tableName with struct { pk, idInstance, _createdAt }
 		$conf = self::getInstanceConfig($namespace);
 		if (!empty($conf['idInstanceBase'])) return "CRM__#INSTANCE_TABLE__{$conf['idInstanceBase']}";
 

+ 205 - 205
SE/se-lib/AntAclBase.php

@@ -8,231 +8,231 @@ Lib::loadClass('Core_AclBase');
 */
 class AntAclBase extends Core_AclBase {
 
-  public function __construct($zasobID = 0) {
-    $this->_zasobID = (int)$zasobID;
-    $this->_name = '';
-    $this->_namespace = '';
-    $this->_rootTableName = '';
-    $this->_db = 0; // database id zasobu
-    $this->_rootNamespace = '';
-    $this->_primaryKey = '';
-    $this->_fields = [];
-  }
-  public function getDB() { return $this->_db; }
-  public function getName() { return $this->_name; }
-  public function getNamespace() { return $this->_namespace; }
-  public function getRootNamespace() { return $this->_rootNamespace; }
-  public function getSourceName() { return 'default_db'; } // TODO: ?
-  public function getRootTableName() { return $this->_rootTableName; }
-  public function getPrimaryKeyField() { return $this->_primaryKey; }
-  public function getFieldListByIdZasob() { return $this->getRealFieldListByIdZasob(); }
-  public function getVirtualFieldListByIdZasob() { return []; }
-  // public function getVisibleFieldListByIdZasob() { return $this->getRealFieldListByIdZasob(); } // TODO: get visible fields
-  public function getVisibleFieldListByIdZasob() {
-    $fields = $this->getRealFieldListByIdZasob();
-    $pkField = $this->getPrimaryKeyField();
+	public function __construct($zasobID = 0) {
+		$this->_zasobID = (int)$zasobID;
+		$this->_name = '';
+		$this->_namespace = '';
+		$this->_rootTableName = '';
+		$this->_db = 0; // database id zasobu
+		$this->_rootNamespace = '';
+		$this->_primaryKey = '';
+		$this->_fields = [];
+	}
+	public function getDB() { return $this->_db; }
+	public function getName() { return $this->_name; }
+	public function getNamespace() { return $this->_namespace; }
+	public function getRootNamespace() { return $this->_rootNamespace; }
+	public function getSourceName() { return 'default_db'; } // TODO: ?
+	public function getRootTableName() { return $this->_rootTableName; }
+	public function getPrimaryKeyField() { return $this->_primaryKey; }
+	public function getFieldListByIdZasob() { return $this->getRealFieldListByIdZasob(); }
+	public function getVirtualFieldListByIdZasob() { return []; }
+	// public function getVisibleFieldListByIdZasob() { return $this->getRealFieldListByIdZasob(); } // TODO: get visible fields
+	public function getVisibleFieldListByIdZasob() {
+		$fields = $this->getRealFieldListByIdZasob();
+		$pkField = $this->getPrimaryKeyField();
 		$cols = array();
 		foreach ($fields as $kFieldID => $fieldName) {
 			if ($pkField === $fieldName) {
 				$id = $kFieldID;
-        break;
+				break;
 			}
 		}
 		$cols[$id] = 'ID';
 		foreach ($fields as $kFieldID => $fieldName) {
-      if ($pkField === $fieldName) continue;
+			if ($pkField === $fieldName) continue;
 			$cols[$kFieldID] = $fieldName;
 		}
 		return $cols;
 	}
-  public function getRealFieldListByIdZasob() {
-    $cols = array();
-    $fakeZasobId = 1000000;
-    foreach ($this->getFields() as $field) {
-      $idZasobField = ($field['idZasob']) ? $field['idZasob'] : $fakeZasobId++;
-      $cols[$idZasobField] = $field['fieldNamespace'];
-    }
-    return $cols;
-  }
-  public function getFieldType($fieldName) { return null; }
-  //   try {
-  //     throw new Exception("TODO: AntAclBase::getFieldType({$fieldName})");
-  //   } catch (Exception $e) {
-  //     DBG::log($e);
-  //   }
-  //   $field = $this->_getField($fieldName);
-  //   return $field['xsdType'];
-  // }
-  public function getXsdFieldType($fieldName) {
-    $field = $this->_getField($fieldName);
-    return $field['xsdType'];
-  }
-  public function getXsdMaxOccurs($fieldName) {
-    $field = $this->_getField($fieldName);
-    return $field['maxOccurs'];
-  }
-  public function getXsdMinOccurs($fieldName) {
-    $field = $this->_getField($fieldName);
-    return $field['minOccurs'];
-  }
-  public function getAttributesFromZasoby() {
-    return [];// TODO: ...
-  }
-  public function getXsdFieldParam($fieldName, $paramKey) { // TODO: fetch from db
-    return null;
-  }
-  // public function getXsdFieldParam($fieldName, $paramKey) { // TableAcl
+	public function getRealFieldListByIdZasob() {
+		$cols = array();
+		$fakeZasobId = 1000000;
+		foreach ($this->getFields() as $field) {
+			$idZasobField = ($field['idZasob']) ? $field['idZasob'] : $fakeZasobId++;
+			$cols[$idZasobField] = $field['fieldNamespace'];
+		}
+		return $cols;
+	}
+	public function getFieldType($fieldName) { return null; }
+	//   try {
+	//     throw new Exception("TODO: AntAclBase::getFieldType({$fieldName})");
+	//   } catch (Exception $e) {
+	//     DBG::log($e);
+	//   }
+	//   $field = $this->_getField($fieldName);
+	//   return $field['xsdType'];
+	// }
+	public function getXsdFieldType($fieldName) {
+		$field = $this->_getField($fieldName);
+		return $field['xsdType'];
+	}
+	public function getXsdMaxOccurs($fieldName) {
+		$field = $this->_getField($fieldName);
+		return $field['maxOccurs'];
+	}
+	public function getXsdMinOccurs($fieldName) {
+		$field = $this->_getField($fieldName);
+		return $field['minOccurs'];
+	}
+	public function getAttributesFromZasoby() {
+		return [];// TODO: ...
+	}
+	public function getXsdFieldParam($fieldName, $paramKey) { // TODO: fetch from db
+		return null;
+	}
+	// public function getXsdFieldParam($fieldName, $paramKey) { // TableAcl
 	// 	return ($this->_schemaClass)
 	// 		? $this->_schemaClass->getFieldParam($fieldName, $paramKey)
 	// 		: null
 	// 	;
 	// }
 	// public function getXsdFieldParam($fieldName, $paramKey) { // SimpleSchema
-  //   if (empty($this->_simpleSchema['root'][$fieldName])) return null;
-  //   if (empty($this->_simpleSchema['root'][$fieldName]['@@params'])) return null;
-  //   if (empty($this->_simpleSchema['root'][$fieldName]['@@params'][$paramKey])) return null;
-  //   return $this->_simpleSchema['root'][$fieldName]['@@params'][$paramKey];
+	//   if (empty($this->_simpleSchema['root'][$fieldName])) return null;
+	//   if (empty($this->_simpleSchema['root'][$fieldName]['@@params'])) return null;
+	//   if (empty($this->_simpleSchema['root'][$fieldName]['@@params'][$paramKey])) return null;
+	//   return $this->_simpleSchema['root'][$fieldName]['@@params'][$paramKey];
 	// }
 
-  public function isGeomField($fieldName) {
-    return ('the_geom' === $fieldName); // TODO: check by xsdType
-  }
-  public function isEnumerationField($fieldName) {
-    return false; // TODO: ...
-  }
-
-  public function canWriteField($fieldName) {
-    return false; // TODO: return $this->getPerms($fieldName)->canWrite()
-  }
-  public function canCreateField($fieldName) {
-    return false; // TODO: return $this->getPerms($fieldName)->canCreate()
-  }
-  public function canReadField($fieldName) {
-    return true; // TODO: return $this->getPerms($fieldName)->canRead()
-  }
-  public function canReadObjectField($fieldName, $object) {
-    return true; // TODO: return $this->getPerms($fieldName, $object)->canRead()
-  }
-  public function canWriteObjectField($fieldName, $record) {
-    return false; // TODO: return $this->getPerms($fieldName, $object)->canWrite()
-  }
-
-  public function getFields() { // TODO: conflict return structure with TableAcl
-    if (empty($this->_fields)) {
-      // TODO: fetch fields from DB
-      // Lib::loadClass('SchemaFactory');
-      // $objectStorage = SchemaFactory::loadDefaultObject('SystemObject');
-      // $item = $objectStorage->getItem($namespace, [
-      // 	'propertyName' => '*,field'
-      // ]);
-    }
-    return $this->_fields;
-  }
-  public function _getField($fieldName) {
-    foreach ($this->getFields() as $field) {
-      if ($fieldName === $field['fieldNamespace']) return $field;
-    }
-    throw new Exception("Field not found '{$this->_namespace}/{$fieldName}'");
-  }
-
-  public function getSqlPrimaryKeyField() { return 'ID'; } // TODO: read from root object schema (_rootTableName)
-
-  public function getTotal($params = []) {
-    DBG::log($params, 'array', "AntAclBase::getTotal params");
-    $idInstance = ACL::getInstanceId($this->_namespace);
-    $instanceTable = ACL::getInstanceTable($this->_namespace);
-    $sqlPrimaryKey = $this->getSqlPrimaryKeyField();
-    return DB::getPDO()->fetchValue(" -- getTotal({$this->_namespace})
-      select count(1)
-      from `{$this->_rootTableName}` t
-        join `{$instanceTable}` i on(i.pk = t.{$sqlPrimaryKey} and i.idInstance = {$idInstance})
-    ");
-  }
-  public function getItems($params = []) {
-    DBG::log($params, 'array', "AntAclBase::getItems params");
-
-    // $sql->limit = V::get('limit', 10, $params, 'int');
-    // $sql->offset = V::get('limitstart', 0, $params, 'int');
-    $limit = V::get('limit', 0, $params, 'int');
-    $limit = ($limit < 0) ? 0 : $limit;
-    $offset = V::get('limitstart', 0, $params, 'int');
-    $offset = ($offset < 0) ? 0 : $offset;
-    $sqlLimit = ($limit > 0)
-      ? "limit {$limit} offset {$offset}"
-      : '';
-
-    $idInstance = ACL::getInstanceId($this->_namespace);
-    $instanceTable = ACL::getInstanceTable($this->_namespace);
-    $sqlPrimaryKey = $this->getSqlPrimaryKeyField();
-
-    {
-      $filtrIsInstance = [$this->_namespace];
-      $filtrIsNotInstance = [];
-      if (!empty($params['f_is_instance'])) $filtrIsInstance = $params['f_is_instance'];
-      if (!empty($params['f_is_not_instance'])) $filtrIsNotInstance = $params['f_is_not_instance'];
-    }
-
-    return ACL::query($this)
-      ->isInstance($filtrIsInstance)
-      ->isNotInstance($filtrIsNotInstance)
-      ->select('*') // TODO: fields
-      ->select(!empty($params['@instances']) ? '@instances' : '')
-      // ->join($instanceTable, 'i', [ 'rawJoin' => "i.pk = t.{$sqlPrimaryKey} and i.idInstance = {$idInstance}" ])
-      ->limit($limit)
-      ->offset($offset)
-      ->execute();
-  }
-
-  public static function buildInstance($idZasob, $conf = []) {
-    static $_cache;
-    if (!$_cache) $_cache = array();
-    if (array_key_exists($idZasob, $_cache)) {
-      return $_cache[$idZasob];
-    }
-
-    if (empty($conf)) throw new Exception("Brak danych konfiguracyjnych do obiektu ant nr {$idZasob}");
-    DBG::log($conf, 'array', 'AntAclBase::buildInstance $conf');
-    $acl = new AntAclBase($idZasob);
-    $acl->_name = $conf['name'];
-    $acl->_rootTableName = $conf['_rootTableName'];
-    $acl->_db = $conf['idDatabase'];
-    $acl->_namespace = $conf['namespace'];
-    $acl->_rootNamespace = str_replace('__x3A__', '/', $conf['nsPrefix']);
-    $acl->_fields = $conf['field']; // TODO: lazyLoading - use getFields() in all functions - TODO: use ACL::getObjectFields
-    $acl->_primaryKey = 'ID'; // $conf['primaryKey'];
-
-    $_cache[$idZasob] = $acl;
-    return $_cache[$idZasob];
-  }
-
-  public function buildQuery($params = array()) {
-    Lib::loadClass('AclQueryFeatures');
+	public function isGeomField($fieldName) {
+		return ('the_geom' === $fieldName); // TODO: check by xsdType
+	}
+	public function isEnumerationField($fieldName) {
+		return false; // TODO: ...
+	}
+
+	public function canWriteField($fieldName) {
+		return false; // TODO: return $this->getPerms($fieldName)->canWrite()
+	}
+	public function canCreateField($fieldName) {
+		return false; // TODO: return $this->getPerms($fieldName)->canCreate()
+	}
+	public function canReadField($fieldName) {
+		return true; // TODO: return $this->getPerms($fieldName)->canRead()
+	}
+	public function canReadObjectField($fieldName, $object) {
+		return true; // TODO: return $this->getPerms($fieldName, $object)->canRead()
+	}
+	public function canWriteObjectField($fieldName, $record) {
+		return false; // TODO: return $this->getPerms($fieldName, $object)->canWrite()
+	}
+
+	public function getFields() { // TODO: conflict return structure with TableAcl
+		if (empty($this->_fields)) {
+			// TODO: fetch fields from DB
+			// Lib::loadClass('SchemaFactory');
+			// $objectStorage = SchemaFactory::loadDefaultObject('SystemObject');
+			// $item = $objectStorage->getItem($namespace, [
+			// 	'propertyName' => '*,field'
+			// ]);
+		}
+		return $this->_fields;
+	}
+	public function _getField($fieldName) {
+		foreach ($this->getFields() as $field) {
+			if ($fieldName === $field['fieldNamespace']) return $field;
+		}
+		throw new Exception("Field not found '{$this->_namespace}/{$fieldName}'");
+	}
+
+	public function getSqlPrimaryKeyField() { return 'ID'; } // TODO: read from root object schema (_rootTableName)
+
+	public function getTotal($params = []) {
+		DBG::log($params, 'array', "AntAclBase::getTotal params");
+		$idInstance = ACL::getInstanceId($this->_namespace);
+		$instanceTable = ACL::getInstanceTable($this->_namespace);
+		$sqlPrimaryKey = $this->getSqlPrimaryKeyField();
+		return DB::getPDO()->fetchValue(" -- getTotal({$this->_namespace})
+		select count(1)
+		from `{$this->_rootTableName}` t
+		join `{$instanceTable}` i on(i.pk = t.{$sqlPrimaryKey} and i.idInstance = {$idInstance})
+		");
+	}
+	public function getItems($params = []) {
+		DBG::log($params, 'array', "AntAclBase::getItems params");
+
+		// $sql->limit = V::get('limit', 10, $params, 'int');
+		// $sql->offset = V::get('limitstart', 0, $params, 'int');
+		$limit = V::get('limit', 0, $params, 'int');
+		$limit = ($limit < 0) ? 0 : $limit;
+		$offset = V::get('limitstart', 0, $params, 'int');
+		$offset = ($offset < 0) ? 0 : $offset;
+		$sqlLimit = ($limit > 0)
+			? "limit {$limit} offset {$offset}"
+			: '';
+
+		$idInstance = ACL::getInstanceId($this->_namespace);
+		$instanceTable = ACL::getInstanceTable($this->_namespace);
+		$sqlPrimaryKey = $this->getSqlPrimaryKeyField();
+
+		{
+			$filtrIsInstance = [$this->_namespace];
+			$filtrIsNotInstance = [];
+			if (!empty($params['f_is_instance'])) $filtrIsInstance = $params['f_is_instance'];
+			if (!empty($params['f_is_not_instance'])) $filtrIsNotInstance = $params['f_is_not_instance'];
+		}
+
+		return ACL::query($this)
+			->isInstance($filtrIsInstance)
+			->isNotInstance($filtrIsNotInstance)
+			->select('*') // TODO: fields
+			->select(!empty($params['@instances']) ? '@instances' : '')
+			// ->join($instanceTable, 'i', [ 'rawJoin' => "i.pk = t.{$sqlPrimaryKey} and i.idInstance = {$idInstance}" ])
+			->limit($limit)
+			->offset($offset)
+			->execute();
+	}
+
+	public static function buildInstance($idZasob, $conf = []) {
+		static $_cache;
+		if (!$_cache) $_cache = array();
+		if (array_key_exists($idZasob, $_cache)) {
+			return $_cache[$idZasob];
+		}
+
+		if (empty($conf)) throw new Exception("Brak danych konfiguracyjnych do obiektu ant nr {$idZasob}");
+		DBG::log($conf, 'array', 'AntAclBase::buildInstance $conf');
+		$acl = new AntAclBase($idZasob);
+		$acl->_name = $conf['name'];
+		$acl->_rootTableName = $conf['_rootTableName'];
+		$acl->_db = $conf['idDatabase'];
+		$acl->_namespace = $conf['namespace'];
+		$acl->_rootNamespace = str_replace('__x3A__', '/', $conf['nsPrefix']);
+		$acl->_fields = $conf['field']; // TODO: lazyLoading - use getFields() in all functions - TODO: use ACL::getObjectFields
+		$acl->_primaryKey = 'ID'; // $conf['primaryKey'];
+
+		$_cache[$idZasob] = $acl;
+		return $_cache[$idZasob];
+	}
+
+	public function buildQuery($params = array()) {
+		Lib::loadClass('AclQueryFeatures');
 		return new AclQueryFeatures($this, $params, $legacyMode = false);
-  }
-
-  public function getInstanceList() {
-    $rootTableName = $this->_rootTableName;
-    return array_map(function ($row) use ($rootTableName) {
-      return $row['name'];
-    }, SchemaFactory::loadDefaultObject('SystemObject')->getItems([
-        'propertyName' => 'name', // TODO: SystemObject fix propertyName
-        'f__rootTableName' => "={$rootTableName}",
-        'f__type' => "=AntAcl",
-        'f_isObjectActive' => "=1",
-      ])
-    );
-  }
-
-  public function getLocalFieldList() {
-    return array_map(function ($field) {
-      return $field['fieldNamespace'];
-    }, array_filter($this->getFields(), function ($field) {
-      return ($field['isLocal']);
-    }));
-  }
-
-  public function isLocalField($fieldName) {
-    return $this->_getField($fieldName)['isLocal'];
-  }
+	}
+
+	public function getInstanceList() {
+		$rootTableName = $this->_rootTableName;
+		return array_map(function ($row) use ($rootTableName) {
+			return $row['name'];
+		}, SchemaFactory::loadDefaultObject('SystemObject')->getItems([
+			'propertyName' => 'name', // TODO: SystemObject fix propertyName
+			'f__rootTableName' => "={$rootTableName}",
+			'f__type' => "=AntAcl",
+			'f_isObjectActive' => "=1",
+		])
+	);
+	}
+
+	public function getLocalFieldList() {
+		return array_map(function ($field) {
+			return $field['fieldNamespace'];
+		}, array_filter($this->getFields(), function ($field) {
+			return ($field['isLocal']);
+		}));
+	}
+
+	public function isLocalField($fieldName) {
+		return $this->_getField($fieldName)['isLocal'];
+	}
 
 }