SystemObjectStorageAcl.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. Lib::loadClass('Core_AclSimpleSchemaBase');
  3. Lib::loadClass('ParseOgcFilter');
  4. class Schema_SystemObjectStorageAcl extends Core_AclSimpleSchemaBase {
  5. public $_simpleSchema = [
  6. 'root' => [
  7. '@namespace' => 'default_objects/SystemObject',
  8. '@primaryKey' => 'ID',
  9. 'ID' => [ '@type' => 'xsd:integer' ],
  10. 'namespace' => [ '@type' => 'xsd:string' ],
  11. 'typeName' => [ '@type' => 'xsd:string' ],
  12. 'tabela' => [ '@type' => 'xsd:string', '@alias' => 'DESC' ],
  13. 'nazwa' => [ '@type' => 'xsd:string', '@alias' => 'DESC' ],
  14. 'opis' => [ '@type' => 'xsd:string', '@alias' => 'OPIS' ],
  15. 'id_zasob' => [ '@type' => 'xsd:integer' ],
  16. 'autor' => [ '@type' => 'xsd:string' , '@alias' => 'A_RECORD_CREATE_AUTHOR' ],
  17. 'utworzono' => [ '@type' => 'xsd:date' , '@alias' => 'A_RECORD_CREATE_DATE' ],
  18. 'zaktualizował' => [ '@type' => 'xsd:string' , '@alias' => 'A_RECORD_UPDATE_AUTHOR' ],
  19. 'zaktualizowano' => [ '@type' => 'xsd:date', '@alias' => 'A_RECORD_UPDATE_DATE' ]
  20. ]
  21. ];
  22. // public $_rootTableName = 'CRM_LISTA_ZASOBOW';
  23. public $_rootTableName = 'CRM_#CACHE_ACL_OBJECT';
  24. public $_version = '1';
  25. // public function __construct($simpleSchema = null) {
  26. // parent::__construct($simpleSchema);
  27. // self::updateCacheIfNeeded(array($this, 'updateCache'));
  28. // }
  29. public static function updateCacheIfNeeded($updateCallback) {
  30. static $_cacheUpdated = null;
  31. if (null !== $_cacheUpdated) return;
  32. $conf = DB::getPDO()->fetchFirst("
  33. select (select CONF_VAL from CRM_CONFIG where CONF_KEY = 'SystemObject__last_cache_update_date') as lastCacheUpdate
  34. , (select CONF_VAL from CRM_CONFIG where CONF_KEY = 'SystemObject__force_cache_update') as forceCacheUpdate
  35. , (select CONF_VAL from CRM_CONFIG where CONF_KEY = 'SystemObject__schema_version') as schemaVersion
  36. , (select CONF_VAL from CRM_CONFIG where CONF_KEY = 'tbl_indexer_CRM_PROCES_last_exec_end') as lastProcesIndexer
  37. , (
  38. 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
  39. from CRM_LISTA_ZASOBOW_HIST
  40. order by ID DESC
  41. limit 1
  42. ) as lastZasobyUpdate
  43. ");
  44. if ($conf['lastZasobyUpdate']) {// fix date format - convert to Mysql date time format '2002-05-30 09:00:00'
  45. if (preg_match('/^\d\d\d\d\-\d\d\-\d\d\-\d\d:\d\d$/', $conf['lastZasobyUpdate'])) { // 'Y-m-d-H:i'
  46. $conf['lastZasobyUpdate'] = substr($conf['lastZasobyUpdate'], 0, 10) . " " . substr($conf['lastZasobyUpdate'], 11) . ":00";
  47. } 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'
  48. $conf['lastZasobyUpdate'] = substr($conf['lastZasobyUpdate'], 0, 10) . " " . substr($conf['lastZasobyUpdate'], 11);
  49. } else if (preg_match('/^\d\d\d\d\-\d\d\-\d\d\ \d\d:\d\d:\d\d$/', $conf['lastZasobyUpdate'])) {
  50. $conf['lastZasobyUpdate'] = substr($conf['lastZasobyUpdate'], 0, 10) . " " . substr($conf['lastZasobyUpdate'], 11);
  51. }
  52. }
  53. // $lastProcesIndexer = DB::getPDO()->fetchValue(" select CONF_VAL from CRM_CONFIG where CONF_KEY = 'tbl_indexer_CRM_PROCES_last_exec_end' ");
  54. $updateReason = null;
  55. if ('1' == $conf['forceCacheUpdate']) $updateReason = 'forceCacheUpdate = 1';
  56. if (!$conf['lastCacheUpdate']) $updateReason = '!lastCacheUpdate';
  57. if (!$conf['schemaVersion']) $updateReason = '!schemaVersion';
  58. else if ($conf['schemaVersion'] < $this->_version) $updateReason = 'schemaVersion < ' . $this->_version;
  59. if ($conf['lastCacheUpdate'] && $conf['lastZasobyUpdate'] && $conf['lastCacheUpdate'] < $conf['lastZasobyUpdate']) $updateReason = 'lastZasobyUpdate';
  60. if ($updateReason) {
  61. DBG::simpleLog('schema', "SystemObject: update cache reason '{$updateReason}' '" . json_encode($conf) . "'");
  62. call_user_func($updateCallback);
  63. }
  64. DB::getPDO()->execSql("
  65. insert into CRM_CONFIG (CONF_KEY, CONF_VAL)
  66. values('SystemObject__last_cache_update_date', NOW())
  67. on duplicate key update CONF_VAL = NOW()
  68. ");
  69. DB::getPDO()->execSql("
  70. insert into CRM_CONFIG (CONF_KEY, CONF_VAL)
  71. values('SystemObject__schema_version', {$this->_version})
  72. on duplicate key update CONF_VAL = {$this->_version}
  73. ");
  74. if ('1' == $conf['forceCacheUpdate']) DB::getPDO()->execSql(" update CRM_CONFIG set CONF_VAL = '0' where CONF_KEY = 'SystemObject__force_cache_update' ");
  75. $_cacheUpdated = true;
  76. }
  77. public function updateCache() {
  78. DBG::simpleLog('schema', "SystemObject::updateCache...");
  79. DB::getPDO()->execSql("
  80. create table if not exists `{$this->_rootTableName}` (
  81. `idZasob` int(11) NOT NULL,
  82. `idDatabase` int(11) NOT NULL,
  83. `name` varchar(255) DEFAULT '',
  84. `namespace` varchar(255) DEFAULT '',
  85. `typeName` varchar(255) DEFAULT '',
  86. `_rootTableName` varchar(255) DEFAULT '',
  87. UNIQUE KEY `idZasob` (idZasob)
  88. ) ENGINE=MyISAM DEFAULT CHARSET=latin2
  89. ");
  90. $idDefDB = DB::getPDO()->getZasobId();
  91. DB::getPDO()->execSql("
  92. insert ignore into `{$this->_rootTableName}` (idZasob, idDatabase, name, namespace, typeName)
  93. select t.ID as idZasob
  94. , d.ID as idDatabase
  95. , d.`DESC` as name
  96. , concat(IF({$idDefDB} = d.ID, 'default_db', 'zasob_'), '/', t.`DESC`) as namespace
  97. , concat(IF({$idDefDB} = d.ID, 'default_db', concat('zasob_', d.ID)), ':', t.`DESC`) as typeName
  98. from CRM_LISTA_ZASOBOW t
  99. join CRM_LISTA_ZASOBOW d on(d.ID = t.PARENT_ID and d.A_STATUS in('NORMAL', 'WAITING'))
  100. where t.`TYPE` = 'TABELA'
  101. and t.A_STATUS in('NORMAL', 'WAITING')
  102. and t.`DESC` not like '%/%'
  103. ");
  104. // _rootTableName
  105. // select idDatabase, name from `{$this->_rootTableName}` where _rootTableName = '' and idDatabase != $idDefDB
  106. // group by idDtabase
  107. // foreach ... DB::getPDO($idDatabase)->fetchAll(select real _rootTableName)
  108. foreach (Core_AclHelper::getAclList() as $typeName) {
  109. $ns = Core_AclHelper::parseTypeName($typeName);
  110. $namespace = str_replace('__x3A__', '/', $ns['prefix']) . "/{$ns['name']}";
  111. $sqlNs = DB::getPDO()->quote($namespace, PDO::PARAM_STR);
  112. $idZasob = DB::getPDO()->fetchValue(" select ID from CRM_LISTA_ZASOBOW where `DESC` = {$sqlNs} and `TYPE` = 'TABELA' and A_STATUS in('WAITING', 'NORMAL') ");
  113. if (!$idZasob) {
  114. DBG::nicePrint($ns, "TODO: insert zasob PARENT_ID = ?");
  115. }
  116. }
  117. }
  118. public function getTotal($params = []) {
  119. return count($this->_getAllItems());
  120. }
  121. public function getItems($params = []) {
  122. $items = $this->_getAllItems();
  123. $currSortCol = V::get('order_by', 'ID', $params);
  124. $currSortFlip = strtolower(V::get('order_dir', 'desc', $params));
  125. // TODO: validate $currSortCol is in field list
  126. // TODO: validate $currSortFlip ('asc' or 'desc')
  127. $aliasMap = array();
  128. foreach ($this->_simpleSchema['root'] as $key => $field) {
  129. if ('@' === substr($key, 0, 1)) continue;
  130. $aliasMap[ $key ] = $key;// (!empty($field['@alias'])) ? $field['@alias'] : $key;
  131. }
  132. // TODO: if (!array_key_exists($currSortCol, $aliasMap)) throw new Exception("field name not allowed to sort");
  133. $currSortCol = (array_key_exists($currSortCol, $aliasMap)) ? $aliasMap[$currSortCol] : null;
  134. if (!empty($currSortCol) && ('asc' == $currSortFlip || 'desc' == $currSortFlip)) {
  135. usort($items, function ($itemA, $itemB) use ($currSortCol, $currSortFlip) {
  136. $a = strtolower(V::get($currSortCol, '', $itemA));
  137. $b = strtolower(V::get($currSortCol, '', $itemB));
  138. if ($a == $b) return 0;
  139. else if ('asc' == $currSortFlip) return ($a < $b) ? -1 : 1;
  140. else if ('desc' == $currSortFlip) return ($a > $b) ? -1 : 1;
  141. throw new Exception("BUG - Wrong sort param - order dir");
  142. });
  143. }
  144. $limit = V::get('limit', 0, $params);
  145. $limit = ($limit < 0) ? 0 : $limit;
  146. $offset = V::get('limitstart', 0, $params);
  147. $offset = ($offset < 0) ? 0 : $offset;
  148. return array_slice($items, $offset, ($limit > 0) ? $limit : null, $preserve_keys = true);
  149. }
  150. public function _generateUniqueKeyFromNamespace($namespace) {
  151. return str_replace('/', '__x3A__', $namespace);
  152. }
  153. public function _getAllItems($params = []) {
  154. static $_cacheAllItems = null;
  155. if (null !== $_cacheAllItems) return $_cacheAllItems;
  156. $idMainDatabase = DB::getPDO()->getZasobId();
  157. $listFromZasoby = array_map(
  158. function ($row) {
  159. $row['ID'] = $row['id_zasob'];
  160. if (false !== strpos($row['tabela'], '/')) {
  161. $ns = Core_AclHelper::parseNamespaceUrl($row['tabela']);
  162. $row['namespace'] = "{$row['tabela']}";
  163. $row['typeName'] = "{$ns['prefix']}:{$ns['name']}";
  164. $row['nazwa'] = $ns['name'];
  165. } else {
  166. $row['namespace'] = "default_db/{$row['tabela']}";
  167. $row['typeName'] = "default_db:{$row['tabela']}";
  168. // $row['ID'] = $this->_generateUniqueKeyFromNamespace($row['namespace']);
  169. }
  170. return $row;
  171. }
  172. , DB::getPDO()->fetchAll("
  173. select z.ID as id_zasob
  174. , z.`DESC` as tabela
  175. , IF(z.`DESC_PL` != '', z.`DESC_PL`, z.`DESC`) as nazwa
  176. , z.`OPIS` as opis
  177. , z.A_RECORD_CREATE_AUTHOR as `autor`
  178. , z.A_RECORD_CREATE_DATE as `utworzono`
  179. , z.A_RECORD_UPDATE_AUTHOR as `zaktualizował`
  180. , z.A_RECORD_UPDATE_DATE as `zaktualizowano`
  181. from `CRM_LISTA_ZASOBOW` z
  182. where z.PARENT_ID = {$idMainDatabase}
  183. and z.`TYPE` = 'TABELA'
  184. and z.`A_STATUS` not in('DELETED', 'OFF_HARD', 'OFF_SOFT')
  185. ")
  186. );
  187. $notRawTables = array_filter(
  188. array_map(
  189. function ($item) {
  190. return $item['typeName'];
  191. }
  192. , $listFromZasoby
  193. )
  194. , function ($typeName) {
  195. return 'default_db:' !== substr($typeName, 0, strlen('default_db:'));
  196. }
  197. );
  198. $_cacheAllItems = array_filter(
  199. array_merge(
  200. $listFromZasoby
  201. , array_map(
  202. function ($typeName) {
  203. list($prefix, $objectName) = explode(':', $typeName);
  204. $namespace = str_replace([':', '__x3A__'], '/', $typeName);
  205. $id = 0;
  206. if ('objects' == $prefix) {
  207. $id = SchemaFactory::loadDefaultObject($objectName)->getID();
  208. } else if ('default_objects' == $prefix) {
  209. $id = SchemaFactory::loadDefaultObject($objectName)->getID();
  210. } else if ('default_db__x3A__' == substr($prefix, 0, 17)) {
  211. $rootTableName = strtolower(substr($prefix, 17));
  212. $id = SchemaFactory::loadTableObject($rootTableName, $objectName)->getID();
  213. }
  214. if (!$id) $id = $this->_generateUniqueKeyFromNamespace($namespace);
  215. return [
  216. 'ID' => $id,
  217. 'id_zasob' => $id,
  218. 'namespace' => $namespace,
  219. 'typeName' => $typeName,
  220. 'tabela' => '', // TODO: $acl->getRootTableName(),
  221. 'nazwa' => substr($typeName, strrpos($typeName, ':') + 1),
  222. 'opis' => '...',
  223. 'autor' => '',
  224. 'utworzono' => '',
  225. 'zaktualizował' => '',
  226. 'zaktualizowano' => ''
  227. ];
  228. }
  229. , array_filter(Core_AclHelper::getAclList(), function ($typeName) use ($notRawTables) { return !in_array($typeName, $notRawTables); })
  230. )
  231. )
  232. , function ($item) use ($params) {
  233. return true;
  234. }
  235. );
  236. return $_cacheAllItems;
  237. }
  238. }