| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- <?php
- Lib::loadClass('RouteBase');
- Lib::loadClass('Router');
- Lib::loadClass('Response');
- Lib::loadClass('UI');
- Lib::loadClass('SchemaFactory');
- class Route_Storage_AclReinstall extends RouteBase {
- public function handleAuth() {
- if (!User::logged()) {
- User::authByRequest();
- }
- }
- public function defaultAction() {
- UI::gora();
- UI::startContainer();
- try {
- $namespace = V::get('namespace', '', $_GET);
- if (empty($namespace)) throw new Exception("Missing param namespace");
- echo UI::h('h3', [], $namespace);
- echo UI::h('p', [], [
- UI::h('a', [
- 'href' => Router::getRoute('Storage_AclStruct')->getLink('', [ 'namespace' => $namespace ]),
- 'class' => "btn btn-md btn-link",
- ], "<i class=\"glyphicon glyphicon-arrow-left\"></i> Wróć do struktury"),
- ]);
- if ('reinstall' == V::get('_postTask', '', $_POST)) {
- Lib::loadClass('Schema_SystemObjectFieldStorageAcl');
- $objFieldAcl = new Schema_SystemObjectFieldStorageAcl();
- $objFieldAcl->updateCache($namespace);
- DBG::nicePrint([
- 'idInstance' => ACL::getInstanceId($namespace),
- 'rootInstance' => ACL::getRootNamespace($namespace),
- 'conf' => ACL::fetchInstanceConfig($namespace),
- 'table' => ACL::getInstanceTable($namespace),
- ], "dbg");
- {
- $item = SchemaFactory::loadDefaultObject('SystemObject')->getItem($namespace, [ 'propertyName' => '*,field' ]);
- if ('AntAcl' === $item['_type']) {
- $dbName = DB::getPDO()->getDatabaseName();
- $sqlFunBody = ACL::generateIsInstanceFunctionBody($namespace, $item);
- DBG::nicePrint($sqlFunBody, "\$sqlFunBody");
- DB::getPDO()->execSql(" DROP FUNCTION IF EXISTS `{$dbName}`.`isInstance_{$namespace}` ");
- // CREATE
- // [DEFINER = { user | CURRENT_USER }]
- // FUNCTION sp_name ([func_parameter[,...]])
- // RETURNS type
- // [characteristic ...] routine_body
- DB::getPDO()->execSql("
- CREATE DEFINER=`root`@`localhost`
- FUNCTION `{$dbName}`.`isInstance_{$namespace}` ( pk INT(11) )
- RETURNS TINYINT(1)
- {$sqlFunBody}
- ");
- }
- }
- return;
- }
- echo UI::hButtonPost("Reinstall", [
- 'data' => [
- '_postTask' => 'reinstall'
- ],
- 'class' => 'btn btn-md btn-danger',
- 'title' => "Reinstall structure"
- ]);
- echo '<hr>';
- try {
- $this->printReinstallPreview($namespace);
- } catch (Exception $e) {
- DBG::log($e);
- UI::alert('danger', $e->getMessage());
- }
- } catch (Exception $e) {
- UI::alert('danger', "Error #" . $e->getCode() . "|" . $e->getLine() . ": " . $e->getMessage());
- DBG::log($e);
- }
- UI::endContainer();
- UI::dol();
- }
- public function printReinstallPreview($namespace) {
- $objectItem = SchemaFactory::loadDefaultObject('SystemObject')->getItem($namespace, [ 'propertyName' => "*,field" ]);
- DBG::log($objectItem, 'array', '$objectItem preview');
- switch ($objectItem['_type']) {
- case 'AntAcl': $this->printReinstallAntAclPreview($objectItem); break;
- case 'TableAcl': $this->printReinstallTableAclPreview($objectItem); break;
- default: throw new Exception("TODO: Not Implemented type '{$objectItem['_type']}'");
- }
- }
- public function printReinstallAntAclPreview($item) {
- $antAclPath = APP_PATH_SCHEMA . DS . 'ant-object' . DS . str_replace(['__x3A__', ':'], ['.', '/'], $item['typeName']);
- if (!file_exists("{$antAclPath}/build.xml")) throw new Exception("Ant build file not exists");
- Lib::loadClass('XML');
- $xsdType = XML::getXsdTypeFromXsdSchema("{$antAclPath}/{$item['name']}.xsd", $namespace = $item['namespace'], $name = $item['name']);
- DBG::nicePrint($xsdType, '$xsdType');
- if (empty($xsdType['struct'])) throw new Exception("Field list not found for '{$item['namespace']}'");
- foreach ($xsdType['struct'] as $fieldName => $x) {
- $listEnum = [];
- if (!empty($x['restrictions']['enumeration'])) {
- $listEnum = $x['restrictions']['enumeration'];
- unset($x['restrictions']['enumeration']);
- }
- if (!empty($listEnum)) {
- DBG::log($listEnum, 'array', "\$listEnum for field '{$fieldName}'");
- }
- }
- $old = [
- 'fields' => array_map(function ($field) { return $field['fieldNamespace']; }, $item['field']),
- ];
- $new = [
- 'fields' => array_keys($xsdType['struct']),
- ];
- sort($old['fields']);
- sort($new['fields']);
- $diffFieldsToCreate = array_diff($new['fields'], $old['fields']);
- $diffFieldsToRemove = array_diff($old['fields'], $new['fields']);
- $sameFields = array_intersect($new['fields'], $old['fields']);
- echo (!empty($diffFieldsToCreate))
- ? UI::h('details', [ 'open' => "open" ], [
- UI::h('summary', [], "Pola do dodania (".count($diffFieldsToCreate)."):"),
- UI::h('ul', [], array_map(function ($fieldName) {
- return UI::h('li', [], $fieldName);
- }, $diffFieldsToCreate)),
- ])
- : UI::h('p', [ 'style' => "font-style:italic" ], "Brak pól do dodania");
- echo (!empty($diffFieldsToRemove))
- ? UI::h('details', [ 'open' => "open", 'style' => "margin:4px 0; background-color:#ccc; border:1px solid #ddd" ], [
- UI::h('summary', [ 'style' => "padding:4px; outline:none; cursor:pointer" ], "Pola do usunięcia (".count($diffFieldsToRemove)."):"),
- UI::h('ul', [], array_map(function ($fieldName) {
- return UI::h('li', [], $fieldName);
- }, $diffFieldsToRemove)),
- ])
- : UI::h('p', [ 'style' => "font-style:italic" ], "Brak pól do usunięcia");
- foreach ($sameFields as $fieldName) {
- // UI::alert('warning', "TODO: is field changed? '{$fieldName}'");
- $oldField = array_filter($item['field'], function ($field) use ($fieldName) { return ( $fieldName === $field['fieldNamespace'] ); });
- $oldField = ($oldField) ? reset($oldField) : null;
- // DBG::nicePrint($oldField, "\$oldField '$fieldName'");
- $newField = $xsdType['struct'][$fieldName];
- // DBG::nicePrint($newField, "\$newField '$fieldName'");
- $fieldDiff = [];
- if ($newField['type'] !== $oldField['xsdType']) $fieldDiff[] = 'xsdType';
- if ($newField['minOccurs'] != $oldField['minOccurs']) $fieldDiff[] = 'minOccurs';
- if ($newField['maxOccurs'] != $oldField['maxOccurs']) $fieldDiff[] = 'maxOccurs';
- if (json_encode($newField['restrictions']) !== $oldField['xsdRestrictions']) $fieldDiff[] = 'xsdRestrictions';
- if (json_encode($newField['appInfo']) !== $oldField['appInfo']) $fieldDiff[] = 'appInfo';
- echo (!empty($fieldDiff))
- ? UI::h('p', [ 'style' => "" ], "Pole '{$fieldName}' - zmiany: " . implode(", ", $fieldDiff))
- : UI::h('p', [ 'style' => "font-style:italic" ], "Pole '{$fieldName}' - bez zmian");
- }
- throw new Exception("TODO: Podgląd zmian dla AntAcl {$item['namespace']} ...");
- }
- public function printReinstallTableAclPreview($item) {
- throw new Exception("TODO: Podgląd zmian dla tabeli {$item['namespace']} ...");
- }
- }
|