| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339 |
- <?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 = (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 = []; // [ fieldName => [ id => {idZasob}, perms => 'RWX...' ] // TODO: , sort_prio => {SORT_PRIO}, label => {CELL_LABEL} ]
- if (!array_key_exists($this->getID(), $_aclInfo)) {
- $_aclInfo[ $this->getID() ] = [];
- $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[ $this->getID() ][ $nameField ] = [
- 'idZasob' => $row['ID_CELL'],
- 'label' => $row['CELL_LABEL'],
- 'sort_prio' => $row['SORT_PRIO'],
- ];
- foreach ($permCols as $colPerm) $_aclInfo[ $this->getID() ][ $nameField ][ $colPerm ] = (int)$row[ $colPerm ];
- }
- }
- if ($fieldName && !array_key_exists($fieldName, $_aclInfo[ $this->getID() ])) {
- throw new Exception("Field not exists or missing access '{$fieldName}'");
- }
- return ($fieldName) ? $_aclInfo[ $this->getID() ][ $fieldName ] : $_aclInfo[ $this->getID() ];
- }
- 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; }
- }
|