[ '@namespace' => 'default_objects/SystemObject', '@primaryKey' => 'idZasob', 'idZasob' => [ '@type' => 'xsd:integer' ], 'idDatabase' => [ '@type' => 'xsd:integer' ], 'nsPrefix' => [ '@type' => 'xsd:string' ], 'name' => [ '@type' => 'xsd:string' ], '_rootTableName' => [ '@type' => 'xsd:string' ], '_type' => [ '@type' => 'xsd:string' ], 'isActive' => [ '@type' => 'xsd:integer' ], // installed 'description' => [ '@type' => 'xsd:string' ], // 'A_RECORD_CREATE_AUTHOR' => [ '@type' => 'xsd:string' , '@label' => 'autor' ], // 'A_RECORD_CREATE_DATE' => [ '@type' => 'xsd:date' , '@label' => 'utworzono' ], // 'A_RECORD_UPDATE_AUTHOR' => [ '@type' => 'xsd:string' , '@label' => 'zaktualizował' ], // 'A_RECORD_UPDATE_DATE' => [ '@type' => 'xsd:date', '@label' => 'zaktualizowano' ], ] // 'root' => [ // '@namespace' => 'default_objects/SystemObject', // '@primaryKey' => 'ID', // 'ID' => [ '@type' => 'xsd:integer' ], // 'namespace' => [ '@type' => 'xsd:string' ], // 'typeName' => [ '@type' => 'xsd:string' ], // 'tabela' => [ '@type' => 'xsd:string', '@alias' => 'DESC' ], // 'nazwa' => [ '@type' => 'xsd:string', '@alias' => 'DESC' ], // 'opis' => [ '@type' => 'xsd:string', '@alias' => 'OPIS' ], // 'id_zasob' => [ '@type' => 'xsd:integer' ], // 'autor' => [ '@type' => 'xsd:string' , '@alias' => 'A_RECORD_CREATE_AUTHOR' ], // 'utworzono' => [ '@type' => 'xsd:date' , '@alias' => 'A_RECORD_CREATE_DATE' ], // 'zaktualizował' => [ '@type' => 'xsd:string' , '@alias' => 'A_RECORD_UPDATE_AUTHOR' ], // 'zaktualizowano' => [ '@type' => 'xsd:date', '@alias' => 'A_RECORD_UPDATE_DATE' ] // ] ]; // public $_rootTableName = 'CRM_LISTA_ZASOBOW'; public $_rootTableName = 'CRM_#CACHE_ACL_OBJECT'; public $_version = '1'; // public function __construct($simpleSchema = null) { // parent::__construct($simpleSchema); // self::updateCacheIfNeeded(array($this, 'updateCache')); // } // public static function updateCacheIfNeeded($updateCallback) { // static $_cacheUpdated = null; // if (null !== $_cacheUpdated) return; // $conf = DB::getPDO()->fetchFirst(" // select (select CONF_VAL from CRM_CONFIG where CONF_KEY = 'SystemObject__last_cache_update_date') as lastCacheUpdate // , (select CONF_VAL from CRM_CONFIG where CONF_KEY = 'SystemObject__force_cache_update') as forceCacheUpdate // , (select CONF_VAL from CRM_CONFIG where CONF_KEY = 'SystemObject__schema_version') as schemaVersion // , (select CONF_VAL from CRM_CONFIG where CONF_KEY = 'tbl_indexer_CRM_PROCES_last_exec_end') as lastProcesIndexer // , ( // select IF('N/S;' = A_RECORD_UPDATE_DATE or A_RECORD_UPDATE_DATE is null or '' = A_RECORD_UPDATE_DATE, A_RECORD_CREATE_DATE, A_RECORD_UPDATE_DATE) as lastUpdateDate // from CRM_LISTA_ZASOBOW_HIST // order by ID DESC // limit 1 // ) as lastZasobyUpdate // "); // if ($conf['lastZasobyUpdate']) {// fix date format - convert to Mysql date time format '2002-05-30 09:00:00' // if (preg_match('/^\d\d\d\d\-\d\d\-\d\d\-\d\d:\d\d$/', $conf['lastZasobyUpdate'])) { // 'Y-m-d-H:i' // $conf['lastZasobyUpdate'] = substr($conf['lastZasobyUpdate'], 0, 10) . " " . substr($conf['lastZasobyUpdate'], 11) . ":00"; // } else if (preg_match('/^\d\d\d\d\-\d\d\-\d\d\-\d\d:\d\d:\d\d$/', $conf['lastZasobyUpdate'])) { // 'Y-m-d-H:i:s' // $conf['lastZasobyUpdate'] = substr($conf['lastZasobyUpdate'], 0, 10) . " " . substr($conf['lastZasobyUpdate'], 11); // } else if (preg_match('/^\d\d\d\d\-\d\d\-\d\d\ \d\d:\d\d:\d\d$/', $conf['lastZasobyUpdate'])) { // $conf['lastZasobyUpdate'] = substr($conf['lastZasobyUpdate'], 0, 10) . " " . substr($conf['lastZasobyUpdate'], 11); // } // } // // $lastProcesIndexer = DB::getPDO()->fetchValue(" select CONF_VAL from CRM_CONFIG where CONF_KEY = 'tbl_indexer_CRM_PROCES_last_exec_end' "); // $updateReason = null; // if ('1' == $conf['forceCacheUpdate']) $updateReason = 'forceCacheUpdate = 1'; // if (!$conf['lastCacheUpdate']) $updateReason = '!lastCacheUpdate'; // if (!$conf['schemaVersion']) $updateReason = '!schemaVersion'; // else if ($conf['schemaVersion'] < $this->_version) $updateReason = 'schemaVersion < ' . $this->_version; // if ($conf['lastCacheUpdate'] && $conf['lastZasobyUpdate'] && $conf['lastCacheUpdate'] < $conf['lastZasobyUpdate']) $updateReason = 'lastZasobyUpdate'; // if ($updateReason) { // DBG::simpleLog('schema', "SystemObject: update cache reason '{$updateReason}' '" . json_encode($conf) . "'"); // call_user_func($updateCallback); // } // DB::getPDO()->execSql(" // insert into CRM_CONFIG (CONF_KEY, CONF_VAL) // values('SystemObject__last_cache_update_date', NOW()) // on duplicate key update CONF_VAL = NOW() // "); // DB::getPDO()->execSql(" // insert into CRM_CONFIG (CONF_KEY, CONF_VAL) // values('SystemObject__schema_version', {$this->_version}) // on duplicate key update CONF_VAL = {$this->_version} // "); // if ('1' == $conf['forceCacheUpdate']) DB::getPDO()->execSql(" update CRM_CONFIG set CONF_VAL = '0' where CONF_KEY = 'SystemObject__force_cache_update' "); // $_cacheUpdated = true; // } public function updateCache($idDatabase = null) { DBG::simpleLog('schema', "SystemObject::updateCache..."); DB::getPDO()->execSql(" drop table if exists `{$this->_rootTableName}` "); // TODO: DBG DB::getPDO()->execSql(" create table if not exists `{$this->_rootTableName}` ( `idZasob` int(11) DEFAULT NULL, `idDatabase` int(11) NOT NULL, `nsPrefix` varchar(255) DEFAULT '', `name` varchar(255) DEFAULT '', `_rootTableName` varchar(255) DEFAULT '', `_type` varchar(255) DEFAULT '', `isActive` tinyint(1) NOT NULL DEFAULT 0 COMMENT 'installed', `description` varchar(255) DEFAULT '', UNIQUE KEY `idZasob` (idZasob), UNIQUE KEY `typeName` (nsPrefix, name), KEY `isActive` (isActive) ) ENGINE=MyISAM DEFAULT CHARSET=latin2 "); $idDefDB = DB::getPDO()->getZasobId(); $sourceStorage = SchemaFactory::loadDefaultObject('SystemSource'); foreach ($sourceStorage->getItems([ 'f_hasConfig' => 1 ]) as $source) { if ('default_objects' == $source['nsPrefix']) { foreach (glob(APP_PATH_LIB . "/Schema/*StorageAcl.php", GLOB_NOSORT) as $file) { $clsName = substr(basename($file), 0, -1 * strlen('StorageAcl.php')); try { $acl = SchemaFactory::loadDefaultObject($clsName); $namespace = $acl->getNamespace(); $name = $acl->getName(); DB::getPDO()->execSql(" insert ignore into `{$this->_rootTableName}` (nsPrefix, idDatabase, _type, name, description, isActive) values ( '{$source['nsPrefix']}' , '{$source['idZasob']}' , 'StorageAcl' , '{$name}' , '' , '1' ) "); } catch (Exception $e) { UI::alert('danger', $e->getMessage()); } } DB::getPDO()->execSql(" insert into `{$this->_rootTableName}` (idZasob, nsPrefix, idDatabase, name, description, isActive) select t.ID as idZasob , '{$source['nsPrefix']}' as nsPrefix , '{$source['idZasob']}' as idDatabase , t.`DESC` as name , t.`OPIS` as description , 1 as isActive from CRM_LISTA_ZASOBOW t where t.`TYPE` = 'TABELA' and t.A_STATUS in('NORMAL', 'WAITING') and t.PARENT_ID = {$source['idZasob']} and t.`DESC` not like '%/%' on duplicate key update idZasob = t.ID , isActive = 1 "); } else { try { $dbName = DB::getPDO($source['idZasob'])->getDatabaseName(); $dbType = DB::getPDO($source['idZasob'])->getType(); if ('mysql' == $dbType) { UI::alert('warning', "TODO: { id: {$source['idZasob']}, nsPrefix: '{$source['nsPrefix']}', dbName: '{$dbName}', dbType: '{$dbType}'} ..."); // TODO: if another DB split select and insert DB::getPDO()->execSql(" insert into `{$this->_rootTableName}` (idDatabase, nsPrefix, name, _rootTableName, _type, description) select '{$source['idZasob']}' as idDatabase , '{$source['nsPrefix']}' as nsPrefix , t.TABLE_NAME as name , t.TABLE_NAME as _rootTableName , 'TableAcl' as _type , t.TABLE_COMMENT as description from INFORMATION_SCHEMA.TABLES t where t.TABLE_SCHEMA = '{$dbName}' on duplicate key update _rootTableName = t.TABLE_NAME "); DB::getPDO()->execSql(" insert into `{$this->_rootTableName}` (idZasob, nsPrefix, idDatabase, name, description, isActive) select t.ID as idZasob , '{$source['nsPrefix']}' as nsPrefix , '{$source['idZasob']}' as idDatabase , t.`DESC` as name , t.`OPIS` as description , 1 as isActive from CRM_LISTA_ZASOBOW t where t.`TYPE` = 'TABELA' and t.A_STATUS in('NORMAL', 'WAITING') and t.PARENT_ID = {$source['idZasob']} on duplicate key update idZasob = t.ID , isActive = 1 "); // } else if ('pgsql' == $dbType) {// TODO: use pgsql @see Storage Pgsql getTables from information_schema } else { UI::alert('warning', "TODO: { id: {$source['idZasob']}, nsPrefix: '{$source['nsPrefix']}', dbName: '{$dbName}', dbType: '{$dbType}'} ..."); } } catch (Exception $e) { UI::alert('danger', "Error source '{$source['idZasob']}' " . $e->getMessage()); continue; } } } // // foreach ... DB::getPDO($idDatabase)->fetchAll(select real _rootTableName) // foreach (Core_AclHelper::getAclList() as $typeName) { // $ns = Core_AclHelper::parseTypeName($typeName); // $namespace = str_replace('__x3A__', '/', $ns['prefix']) . "/{$ns['name']}"; // $sqlNs = DB::getPDO()->quote($namespace, PDO::PARAM_STR); // $idZasob = DB::getPDO()->fetchValue(" select ID from CRM_LISTA_ZASOBOW where `DESC` = {$sqlNs} and `TYPE` = 'TABELA' and A_STATUS in('WAITING', 'NORMAL') "); // if (!$idZasob) { // DBG::nicePrint($ns, "TODO: insert zasob PARENT_ID = ?"); // } // } } public function _parseWhere($params = []) { $sqlWhere = []; if (!empty($params['#refFrom'])) { // '#refFrom' => [ // 'namespace' => 'default_objects/SystemSource', // 'primaryKey' => $sourceItem['idZasob'] // ] if (empty($params['#refFrom']['namespace'])) throw new Exception("Missing refFrom/namespace"); if (empty($params['#refFrom']['primaryKey'])) throw new Exception("Missing refFrom/primaryKey"); if ('default_objects/SystemSource' != $params['#refFrom']['namespace']) throw new Exception("Unsupported refFrom/namespace '{$params['#refFrom']['namespace']}'"); $sqlWhere[] = "idDatabase = " . DB::getPDO()->quote($params['#refFrom']['primaryKey'], PDO::PARAM_INT); } return (!empty($sqlWhere)) ? "where " . implode(" and ", $sqlWhere) : ''; } public function getTotal($params = []) { $sqlWhere = $this->_parseWhere($params); return DB::getPDO()->fetchValue(" select count(1) as cnt from `{$this->_rootTableName}` t {$sqlWhere} "); } public function getItems($params = []) { $sqlWhere = $this->_parseWhere($params); return DB::getPDO()->fetchAll(" select t.* from `{$this->_rootTableName}` t {$sqlWhere} "); $items = $this->_getAllItems(); $currSortCol = V::get('order_by', 'ID', $params); $currSortFlip = strtolower(V::get('order_dir', 'desc', $params)); // TODO: validate $currSortCol is in field list // TODO: validate $currSortFlip ('asc' or 'desc') $aliasMap = array(); foreach ($this->_simpleSchema['root'] as $key => $field) { if ('@' === substr($key, 0, 1)) continue; $aliasMap[ $key ] = $key;// (!empty($field['@alias'])) ? $field['@alias'] : $key; } // TODO: if (!array_key_exists($currSortCol, $aliasMap)) throw new Exception("field name not allowed to sort"); $currSortCol = (array_key_exists($currSortCol, $aliasMap)) ? $aliasMap[$currSortCol] : null; if (!empty($currSortCol) && ('asc' == $currSortFlip || 'desc' == $currSortFlip)) { usort($items, function ($itemA, $itemB) use ($currSortCol, $currSortFlip) { $a = strtolower(V::get($currSortCol, '', $itemA)); $b = strtolower(V::get($currSortCol, '', $itemB)); if ($a == $b) return 0; else if ('asc' == $currSortFlip) return ($a < $b) ? -1 : 1; else if ('desc' == $currSortFlip) return ($a > $b) ? -1 : 1; throw new Exception("BUG - Wrong sort param - order dir"); }); } $limit = V::get('limit', 0, $params); $limit = ($limit < 0) ? 0 : $limit; $offset = V::get('limitstart', 0, $params); $offset = ($offset < 0) ? 0 : $offset; return array_slice($items, $offset, ($limit > 0) ? $limit : null, $preserve_keys = true); } public function _generateUniqueKeyFromNamespace($namespace) { return str_replace('/', '__x3A__', $namespace); } public function _getAllItems($params = []) { static $_cacheAllItems = null; if (null !== $_cacheAllItems) return $_cacheAllItems; $idMainDatabase = DB::getPDO()->getZasobId(); $listFromZasoby = array_map( function ($row) { $row['ID'] = $row['id_zasob']; if (false !== strpos($row['tabela'], '/')) { $ns = Core_AclHelper::parseNamespaceUrl($row['tabela']); $row['namespace'] = "{$row['tabela']}"; $row['typeName'] = "{$ns['prefix']}:{$ns['name']}"; $row['nazwa'] = $ns['name']; } else { $row['namespace'] = "default_db/{$row['tabela']}"; $row['typeName'] = "default_db:{$row['tabela']}"; // $row['ID'] = $this->_generateUniqueKeyFromNamespace($row['namespace']); } return $row; } , DB::getPDO()->fetchAll(" select z.ID as id_zasob , z.`DESC` as tabela , IF(z.`DESC_PL` != '', z.`DESC_PL`, z.`DESC`) as nazwa , z.`OPIS` as opis , z.A_RECORD_CREATE_AUTHOR as `autor` , z.A_RECORD_CREATE_DATE as `utworzono` , z.A_RECORD_UPDATE_AUTHOR as `zaktualizował` , z.A_RECORD_UPDATE_DATE as `zaktualizowano` from `CRM_LISTA_ZASOBOW` z where z.PARENT_ID = {$idMainDatabase} and z.`TYPE` = 'TABELA' and z.`A_STATUS` not in('DELETED', 'OFF_HARD', 'OFF_SOFT') ") ); $notRawTables = array_filter( array_map( function ($item) { return $item['typeName']; } , $listFromZasoby ) , function ($typeName) { return 'default_db:' !== substr($typeName, 0, strlen('default_db:')); } ); $_cacheAllItems = array_filter( array_merge( $listFromZasoby , array_map( function ($typeName) { list($prefix, $objectName) = explode(':', $typeName); $namespace = str_replace([':', '__x3A__'], '/', $typeName); $id = 0; if ('objects' == $prefix) { $id = SchemaFactory::loadDefaultObject($objectName)->getID(); } else if ('default_objects' == $prefix) { $id = SchemaFactory::loadDefaultObject($objectName)->getID(); } else if ('default_db__x3A__' == substr($prefix, 0, 17)) { $rootTableName = strtolower(substr($prefix, 17)); $id = SchemaFactory::loadTableObject($rootTableName, $objectName)->getID(); } if (!$id) $id = $this->_generateUniqueKeyFromNamespace($namespace); return [ 'ID' => $id, 'id_zasob' => $id, 'namespace' => $namespace, 'typeName' => $typeName, 'tabela' => '', // TODO: $acl->getRootTableName(), 'nazwa' => substr($typeName, strrpos($typeName, ':') + 1), 'opis' => '...', 'autor' => '', 'utworzono' => '', 'zaktualizował' => '', 'zaktualizowano' => '' ]; } , array_filter(Core_AclHelper::getAclList(), function ($typeName) use ($notRawTables) { return !in_array($typeName, $notRawTables); }) ) ) , function ($item) use ($params) { return true; } ); return $_cacheAllItems; } }