Jelajahi Sumber

added default_objects/SystemSource

Piotr Labudda 9 tahun lalu
induk
melakukan
6ca68172d1
1 mengubah file dengan 138 tambahan dan 0 penghapusan
  1. 138 0
      SE/se-lib/Schema/SystemSourceStorageAcl.php

+ 138 - 0
SE/se-lib/Schema/SystemSourceStorageAcl.php

@@ -0,0 +1,138 @@
+<?php
+
+Lib::loadClass('Core_AclSimpleSchemaBase');
+Lib::loadClass('ParseOgcFilter');
+
+class Schema_SystemSourceStorageAcl extends Core_AclSimpleSchemaBase {
+
+  public $_simpleSchema = [
+    'root' => [
+      '@namespace' => 'default_objects/SystemSource',
+      '@primaryKey' => 'idZasob',
+      'idZasob' => [ '@type' => 'xsd:integer' ],
+      'name' => [ '@type' => 'xsd:string' ],
+      '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' ],
+      'SystemObject' => [ '@ref' => 'default_objects/SystemObject', '@maxOccurs' => 'unbounded' ]
+    ]
+  ];
+  public $_rootTableName = 'CRM_#CACHE_ACL_SOURCE';
+  public $_version = '1';
+
+  public function updateCache() {
+    DBG::simpleLog('schema', "SystemSource::updateCache...");
+    DB::getPDO()->execSql("
+      create table if not exists `{$this->_rootTableName}` (
+				`idZasob` int(11) NOT NULL,
+				`name` varchar(255) DEFAULT '',
+				`description` varchar(255) DEFAULT '',
+        `hasConfig` tinyint(1) DEFAULT 0,
+        `A_RECORD_CREATE_AUTHOR` varchar(20) NOT NULL,
+        `A_RECORD_CREATE_DATE` datetime DEFAULT NULL,
+        `A_RECORD_UPDATE_AUTHOR` varchar(20) NOT NULL DEFAULT '',
+        `A_RECORD_UPDATE_DATE` datetime DEFAULT NULL,
+				UNIQUE KEY `idZasob` (idZasob)
+			) ENGINE=MyISAM  DEFAULT CHARSET=latin2
+    ");
+    $host = V::get('SERVER_NAME', '', $_SERVER);
+    if (!$host) die('Missing SERVER_NAME!');
+    function hideConfPass($line) {
+      if (false !== ($pos = strpos($line, "pass' =>"))) {
+        return substr($line, 0, $pos + 9) . "*****";
+      }
+      return $line;
+    }
+    foreach (glob(APP_PATH_CONFIG . "/.cnf--zasob_*-{$host}.ini.php", GLOB_NOSORT) as $file) {
+      $fname = basename($file);
+      // DBG::nicePrint($fname, '$fname');
+      $idZasob = (int)substr($fname, strlen('.cnf--zasob_'), strpos($fname, '-', strlen('.cnf--zasob_') + 1) - strlen('.cnf--zasob_'));// '.cnf--zasob_931-...'
+      // DBG::nicePrint($idZasob, '$idZasob');
+      if (!$idZasob) continue;
+      $conf = parse_ini_file($file, true);
+      if (!$conf) continue;
+      if (in_array(V::get('type', '', $conf), ['mysql', 'pgsql', 'mssql'])) {
+        DB::getPDO()->insertOrUpdate($this->_rootTableName, 'idZasob', $idZasob, [
+          'idZasob' => $idZasob,
+          'name' => V::get('database', $idZasob, $conf),
+          'hasConfig' => 1,
+          '@insert' => [
+            'A_RECORD_CREATE_AUTHOR' => User::getLogin(),
+            'A_RECORD_CREATE_DATE' => 'NOW()',
+          ],
+          '@update' => [
+            'A_RECORD_UPDATE_AUTHOR' => User::getLogin(),
+            'A_RECORD_UPDATE_DATE' => 'NOW()',
+          ]
+        ]);
+      }
+      // DBG::nicePrint(implode("\n", array_map( hideConfPass, explode("\n", var_export($conf, true)) ) ), '$conf');
+    }
+    foreach (DB::getPDO()->fetchAll("
+      select z.`ID`, z.`DESC`, z.OPIS
+      from CRM_LISTA_ZASOBOW z
+      where z.`TYPE` in ('BAZA_DANYCH', 'DATABASE_MYSQL', 'DATABASE_POSTGRESQL')
+        and z.A_STATUS not in ('DELETED')
+    ") as $item) {
+      $desc = V::get('DESC', $item['ID'], $item);
+      DB::getPDO()->insertOrUpdate($this->_rootTableName, 'idZasob', $item['ID'], [
+        'idZasob' => $item['ID'],
+        'name' => $desc,
+        'description' => V::get('OPIS', $desc, $item),
+        '@insert' => [
+          'A_RECORD_CREATE_AUTHOR' => User::getLogin(),
+          'A_RECORD_CREATE_DATE' => 'NOW()',
+          'hasConfig' => 0,
+        ],
+        '@update' => [
+          'A_RECORD_UPDATE_AUTHOR' => User::getLogin(),
+          'A_RECORD_UPDATE_DATE' => 'NOW()',
+        ]
+      ]);
+    }
+  }
+
+  public function getTotal($params = []) {
+    return DB::getPDO()->fetchValue(" select count(1) as cnt from `{$this->_rootTableName}` ");
+  }
+
+  public function getItems($params = []) {
+    $items = DB::getPDO()->fetchAll("
+      select t.*
+      from `{$this->_rootTableName}` t
+    ");
+    $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);
+  }
+
+}