| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- <?php
- Lib::loadClass('ACL');
- Lib::loadClass('Core_AclBase');
- /**
- * SE/schema/ant-object/default_db.{rootTableName}/{name}/build.xml
- */
- class AntAclBase extends Core_AclBase {
- public function __construct($zasobID = 0) {
- $this->_zasobID = $zasobID;
- $this->_name = '';
- $this->_namespace = '';
- $this->_rootTableName = '';
- $this->_rootNamespace = '';
- $this->_primaryKey = '';
- $this->_fields = [];
- }
- 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;
- }
- }
- $cols[$id] = 'ID';
- foreach ($fields as $kFieldID => $fieldName) {
- 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
- // 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];
- // }
- public function isGeomField($fieldName) {
- return ('the_geom' === $fieldName); // TODO: ...
- }
- 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() {
- 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}");
- // TODO: fetch conf by $idZasob (or find $namespace first)
- // Lib::loadClass('SchemaFactory');
- // $objectStorage = SchemaFactory::loadDefaultObject('SystemObject');
- // $item = $objectStorage->getItem($namespace, [
- // 'propertyName' => '*,field'
- // ]);
- }
- DBG::log($conf, 'array', 'AntAclBase::buildInstance $conf');
- $acl = new AntAclBase($idZasob);
- $acl->_name = $conf['name'];
- $acl->_rootTableName = $conf['_rootTableName'];
- $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",
- ])
- );
- }
- }
|