_zasobID = (int)$zasobID; $this->_name = ''; $this->_namespace = ''; $this->_rootTableName = ''; $this->_db = 0; // database id zasobu $this->_rootNamespace = ''; $this->_primaryKey = ''; $this->_fields = []; $this->_zasobyInfoFetched = false; } 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 _fetchInfoFromZasobyIfNeeded() { if (!$this->_zasobyInfoFetched) { $zasobyIds = array_filter( array_map(function ($field) { return (int)$field['idZasob']; }, $this->_fields), function ($id) { return $id > 0; } ); if (!empty($zasobyIds)) { DBG::log("DBG sort fields - TODO: ids [".implode(",", $zasobyIds)."]"); $zasobyInfo = DB::getPDO()->fetchAllByKey(" select z.ID, z.DESC_PL, z.OPIS, z.SORT_PRIO from CRM_LISTA_ZASOBOW z where z.ID in(".implode(",", $zasobyIds).") ", 'ID'); DBG::log($zasobyInfo, 'array', "DBG sort fields - zasobyInfo"); $maxSortPrio = 0; array_map(function ($zInfo) use (&$maxSortPrio) { if ($zInfo['SORT_PRIO'] > 0 && $zInfo['SORT_PRIO'] > $maxSortPrio) { $maxSortPrio = $zInfo['SORT_PRIO']; } }, $zasobyInfo); foreach ($this->_fields as $idx => $field) { // $this->_fields[$idx]['name'] = $field['fieldNamespace']; // TODO: BUG query for non existing fields - check if isLocal is used if ($field['idZasob'] > 0 && array_key_exists($field['idZasob'], $zasobyInfo)) { $this->_fields[$idx]['sort_prio'] = $zasobyInfo[ $field['idZasob'] ]['SORT_PRIO']; if (!empty($zasobyInfo[ $field['idZasob'] ]['DESC_PL'])) $this->_fields[$idx]['label'] = $zasobyInfo[ $field['idZasob'] ]['DESC_PL']; if (!empty($zasobyInfo[ $field['idZasob'] ]['OPIS'])) $this->_fields[$idx]['opis'] = $zasobyInfo[ $field['idZasob'] ]['OPIS']; } else { // !$field['idZasob'] => generate sortPrio $this->_fields[$idx]['sort_prio'] = ++$maxSortPrio; } } } $this->_zasobyInfoFetched = true; } usort($this->_fields, array($this, '_sortFieldsCallback')); DBG::log($this->_fields, 'array', "DBG sort fields - sorted \$this->_fields"); } public function _sortFieldsCallback($a, $b) { if ($a['name'] == 'ID') { return -1; } else if ($b['name'] == 'ID') { return 1; } else if ($a['sort_prio'] < $b['sort_prio']) { return -1; } else if ($a['sort_prio'] > $b['sort_prio']) { return 1; } else { return 0; } } public function getVisibleFieldListByIdZasob() { $this->_fetchInfoFromZasobyIfNeeded(); $fields = $this->getRealFieldListByIdZasob(); $pkField = $this->getPrimaryKeyField(); $cols = array(); foreach ($fields as $kFieldID => $fieldName) { if ($pkField === $fieldName) { $id = $kFieldID; break; } } $cols[$id] = 'ID'; // TODO: why rename primary key field to 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: check by xsdType } public function isEnumerationField($fieldName) { return false; // TODO: ... } public function canWriteField($fieldName) { return false; // TODO: $this->getAclInfo($fieldName); @see canReadField } public function canCreateField($fieldName) { try { $fieldAclInfo = $this->getAclInfo($fieldName); DBG::log($fieldAclInfo, 'array', "AntAclBase: canReadField({$fieldName})..."); return ($fieldAclInfo['PERM_C'] > 0); } catch (Exception $e) { DBG::log($e); return false; } return false; } public function canReadField($fieldName) { try { $fieldAclInfo = $this->getAclInfo($fieldName); DBG::log($fieldAclInfo, 'array', "AntAclBase: canReadField({$fieldName})..."); return ($fieldAclInfo['PERM_R'] > 0 || $fieldAclInfo['PERM_V'] > 0 || $fieldAclInfo['PERM_O'] > 0); } catch (Exception $e) { DBG::log($e); return false; } return false; } public function canReadObjectField($fieldName, $object) { return true; // TODO: $this->getAclInfo($fieldName); @see canReadField } public function canWriteObjectField($fieldName, $record) { return false; // TODO: $this->getAclInfo($fieldName); @see canReadField } public function getAclInfo($fieldName = null) { static $_aclInfo = null; // [ fieldName => [ id => {idZasob}, perms => 'RWX...' ] // TODO: , sort_prio => {SORT_PRIO}, label => {CELL_LABEL} ] if (null === $_aclInfo) { $_aclInfo = []; $fieldsConfig = User::getAcl()->getPermsForTable($this->getID()); // @returns [ permInfo group by ID_CELL ]; permInfo = [ ID_CELL, CELL_NAME, CELL_LABEL, SORT_PRIO, PERM_R, PERM_W, PERM_... ] DBG::log($fieldsConfig, 'array', "AntAclBase: User::getAcl()->getPermsForTable(".$this->getID().");"); // $this->initFieldsFromConfig($fieldsConfig); $permCols = [ 'PERM_R', 'PERM_W', 'PERM_X', 'PERM_C', 'PERM_S', 'PERM_O', 'PERM_V', 'PERM_E' ]; foreach ($fieldsConfig as $row) { $nameField = $row['CELL_NAME']; $_aclInfo[ $nameField ] = [ 'idZasob' => $row['ID_CELL'], 'label' => $row['CELL_LABEL'], 'sort_prio' => $row['SORT_PRIO'], ]; foreach ($permCols as $colPerm) $_aclInfo[ $nameField ][ $colPerm ] = (int)$row[ $colPerm ]; } } if ($fieldName && !array_key_exists($fieldName, $_aclInfo)) { throw new Exception("Field not exists or missing access '{$fieldName}'"); } return ($fieldName) ? $_aclInfo[$fieldName] : $_aclInfo; } 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 init($force = false) { } public function isInitialized($force = false) { return true; } }