Kaynağa Gözat

updated Scheme objects getID

Piotr Labudda 9 yıl önce
ebeveyn
işleme
925e4cc820

+ 0 - 1
SE/se-lib/Schema/AccessGroupStorageAcl.php

@@ -144,7 +144,6 @@ class Schema_AccessGroupStorageAcl extends Core_AclBase {// Read only class
   public function updateItem($itemPatch) { throw new Exception("Update not allowed"); }
   public function getGeomFieldType($fieldName) { return null; }
   public function getPrimaryKeyField() { return 'id'; }
-  public function getID() { return 0; }
   public function getAttributesFromZasoby() { return array(); }
   public function isEnumerationField($fieldName) { return false; }
   public function getEnumerations($fieldName) { return null; }

+ 0 - 1
SE/se-lib/Schema/AccessOwnerStorageAcl.php

@@ -143,7 +143,6 @@ class Schema_AccessOwnerStorageAcl extends Core_AclBase {
   public function updateItem($itemPatch) { throw new Exception("Update not allowed"); }
   public function getGeomFieldType($fieldName) { return null; }
   public function getPrimaryKeyField() { return 'id'; }
-  public function getID() { return 0; }
   public function getAttributesFromZasoby() { return array(); }
   public function isEnumerationField($fieldName) { return false; }
   public function getEnumerations($fieldName) { return null; }

+ 0 - 1
SE/se-lib/Schema/FileStorageAcl.php

@@ -236,7 +236,6 @@ class Schema_FileStorageAcl extends Core_AclBase {
 
   public function getGeomFieldType($fieldName) { return null; }
   public function getPrimaryKeyField() { return 'id'; }
-  public function getID() { return 0; }
   public function getAttributesFromZasoby() {
 		$attributes = array();// fldName => [ 'id_zasob' => int, 'label' => str, 'description' => str ]
     // if ($acl->hasFieldPerm($idZasob, 'W')) $elNode->setAttributeNS($rootWfsNsUri, "{$rootWfsNs}:allow_write", "true");

+ 0 - 1
SE/se-lib/Schema/KorespondencjaStorageAcl.php

@@ -103,7 +103,6 @@ class Schema_KorespondencjaStorageAcl extends Core_AclBase {
 
   public function getGeomFieldType($fieldName) { return null; }
   public function getPrimaryKeyField() { return 'ID'; }
-  public function getID() { return 0; }
   public function getAttributesFromZasoby() {
 		$attributes = array();// fldName => [ 'id_zasob' => int, 'label' => str, 'description' => str ]
     // if ($acl->hasFieldPerm($idZasob, 'W')) $elNode->setAttributeNS($rootWfsNsUri, "{$rootWfsNs}:allow_write", "true");

+ 156 - 33
SE/se-lib/Schema/SystemObjectStorageAcl.php

@@ -8,6 +8,7 @@ class Schema_SystemObjectStorageAcl extends Core_AclSimpleSchemaBase {
   public $_simpleSchema = [
     'root' => [
       '@namespace' => 'default_objects/SystemObject',
+      '@primaryKey' => 'ID',
       'ID' => [ '@type' => 'xsd:integer' ],
       'namespace' => [ '@type' => 'xsd:string' ],
       'typeName' => [ '@type' => 'xsd:string' ],
@@ -21,7 +22,104 @@ class Schema_SystemObjectStorageAcl extends Core_AclSimpleSchemaBase {
       'zaktualizowano' => [ '@type' => 'xsd:date', '@alias' => 'A_RECORD_UPDATE_DATE' ]
     ]
   ];
-  public $_rootTableName = 'CRM_LISTA_ZASOBOW';
+  // 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() {
+    DBG::simpleLog('schema', "SystemObject::updateCache...");
+    DB::getPDO()->execSql("
+      create table if not exists `{$this->_rootTableName}` (
+				`idZasob` int(11) NOT NULL,
+				`idDatabase` int(11) NOT NULL,
+				`name` varchar(255) DEFAULT '',
+				`namespace` varchar(255) DEFAULT '',
+				`typeName` varchar(255) DEFAULT '',
+				`_rootTableName` varchar(255) DEFAULT '',
+				UNIQUE KEY `idZasob` (idZasob)
+			) ENGINE=MyISAM  DEFAULT CHARSET=latin2
+    ");
+    $idDefDB = DB::getPDO()->getZasobId();
+    DB::getPDO()->execSql("
+      insert ignore into `{$this->_rootTableName}` (idZasob, idDatabase, name, namespace, typeName)
+      select t.ID as idZasob
+          ,  d.ID as idDatabase
+          ,  d.`DESC` as name
+          ,  concat(IF({$idDefDB} = d.ID, 'default_db', 'zasob_'), '/', t.`DESC`) as namespace
+          ,  concat(IF({$idDefDB} = d.ID, 'default_db', concat('zasob_', d.ID)), ':', t.`DESC`) as typeName
+      from CRM_LISTA_ZASOBOW t
+        join CRM_LISTA_ZASOBOW d on(d.ID = t.PARENT_ID and d.A_STATUS in('NORMAL', 'WAITING'))
+      where t.`TYPE` = 'TABELA'
+        and t.A_STATUS in('NORMAL', 'WAITING')
+        and t.`DESC` not like '%/%'
+    ");
+    // _rootTableName
+    // select idDatabase, name from `{$this->_rootTableName}` where _rootTableName = '' and idDatabase != $idDefDB
+    // group by idDtabase
+    // 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 getTotal($params = []) {
     return count($this->_getAllItems());
@@ -69,42 +167,67 @@ class Schema_SystemObjectStorageAcl extends Core_AclSimpleSchemaBase {
     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(
-        array_map(
-          function ($row) {
-            $row['ID'] = $row['id_zasob'];
-            if (false !== strpos($row['tabela'], '/')) {
-              $row['namespace'] = "{$row['tabela']}";
-              $row['typeName'] = "default_db:{$row['tabela']}";// TODO: parse namespace - get from acl
-            } 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')
-          ")
-        )
+        $listFromZasoby
         , array_map(
           function ($typeName) {
-            $namespace = str_replace(':', '/', $typeName);
-            $namespace = str_replace('__x3A__', '/', $namespace);
+            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' => $this->_generateUniqueKeyFromNamespace($namespace),
+              'ID' => $id,
+              'id_zasob' => $id,
               'namespace' => $namespace,
               'typeName' => $typeName,
               'tabela' => '', // TODO: $acl->getRootTableName(),
@@ -116,7 +239,7 @@ class Schema_SystemObjectStorageAcl extends Core_AclSimpleSchemaBase {
               'zaktualizowano' => ''
             ];
           }
-          , Core_AclHelper::getAclList()
+          , array_filter(Core_AclHelper::getAclList(), function ($typeName) use ($notRawTables) { return !in_array($typeName, $notRawTables); })
         )
       )
       , function ($item) use ($params) {

+ 0 - 1
SE/se-lib/Schema/TestPermsStorageAcl.php

@@ -113,7 +113,6 @@ class Schema_TestPermsStorageAcl extends Core_AclBase {
 
   public function getGeomFieldType($fieldName) { return ('the_geom' == $fieldName) ? 'polygon' : null; }
   public function getPrimaryKeyField() { return 'ID'; }
-  public function getID() { return 0; }
   public function getAttributesFromZasoby() { return array(); }
   public function isEnumerationField($fieldName) {
     if ('A_STATUS' == $fieldName) return true;