Pārlūkot izejas kodu

update add object acl to Zasoby in Storage

Piotr Labudda 9 gadi atpakaļ
vecāks
revīzija
b682972a22
2 mainītis faili ar 134 papildinājumiem un 96 dzēšanām
  1. 133 96
      SE/se-lib/Route/Storage.php
  2. 1 0
      SE/se-lib/Schema/SystemObjectStorageAcl.php

+ 133 - 96
SE/se-lib/Route/Storage.php

@@ -568,15 +568,16 @@ jQuery(document).on('p5UIBtnAjax:Storage:checkObjectInstallAjax:ajaxLoaded', fun
 						]
 					]),
 				'rows' => array_map(function ($item) use ($idStorage) {
-					$addToZasobyLink = $this->getLink('addTableToZasoby', ['idStorage' => $idStorage, 'tblName' => $item['name']]);
+					$addToZasobyLink = $this->getLink('addAclObjectToZasoby', ['idStorage' => $idStorage, 'namespace' => $item['namespace']]);
 					return [
-						'nazwa' => $item['name'],
+						'namespace' => '<span style="color:#888">' . substr($item['namespace'], 0, strlen($item['namespace']) - strlen($item['name'])) . '</span>' .
+							'<span>' . $item['name'] . '</span>',
 						'_type' => $item['_type'], // TODO: editable?
 						'Nr zasobu' => ($item['idZasob'] > 0)
 							? $item['idZasob']
 							: UI::h('button', [
 								'class' => "btn btn-xs btn-primary",
-								'onClick' => "return p5UI__ButtonAjax(this, 'p5UIBtnAjax:Storage:addTableToZasoby', { href: '{$addToZasobyLink}' })"
+								'onClick' => "return p5UI__ButtonAjax(this, 'p5UIBtnAjax:Storage:addAclObjectToZasoby', { href: '{$addToZasobyLink}' })"
 							], "Dodaj do Zasobów"),
 						// 'opis' => $item['description'],
 						'active?' => ($item['isActive']) ? '<span class="label label-success">TAK</span>' : '<span class="text text-muted">nie</span>',
@@ -592,11 +593,11 @@ jQuery(document).on('p5UIBtnAjax:Storage:checkObjectInstallAjax:ajaxLoaded', fun
 				]))
 			]);
 			echo UI::h('script', [], "
-				jQuery(document).on('p5UIBtnAjax:Storage:addTableToZasoby:click', function(e, n, payload) {
-					console.log('event p5UIBtnAjax:Storage:addTableToZasoby:click', n, payload);
+				jQuery(document).on('p5UIBtnAjax:Storage:addAclObjectToZasoby:click', function(e, n, payload) {
+					console.log('event p5UIBtnAjax:Storage:addAclObjectToZasoby:click', n, payload);
 				});
-				jQuery(document).on('p5UIBtnAjax:Storage:addTableToZasoby:ajaxLoaded', function(e, n, payload) {
-					console.log('event p5UIBtnAjax:Storage:addTableToZasoby:ajaxLoaded', n, payload);
+				jQuery(document).on('p5UIBtnAjax:Storage:addAclObjectToZasoby:ajaxLoaded', function(e, n, payload) {
+					console.log('event p5UIBtnAjax:Storage:addAclObjectToZasoby:ajaxLoaded', n, payload);
 					if ('success' == payload.type && payload.body && payload.body.id > 0) {
 						jQuery(n).parents('td').text(payload.body.id);
 					}
@@ -631,6 +632,18 @@ jQuery(document).on('p5UIBtnAjax:Storage:checkObjectInstallAjax:ajaxLoaded', fun
 		UI::dol();
 	}
 
+	public function objectStructAction() {
+		UI::gora();
+		UI::menu();
+		$this->navView();
+		try {
+
+		} catch (Exception $e) {
+			UI::alert('danger', "Error #" . $e->getCode() .  "|" . $e->getLine() .  ": " . $e->getMessage());
+			DBG::log($e);
+		}
+		UI::dol();
+	}
 	public function tableStructAction() {
 		UI::gora();
 		UI::menu();
@@ -1309,24 +1322,30 @@ jQuery(document).on('p5UIBtnAjax:Storage:checkObjectInstallAjax:ajaxLoaded', fun
 			$idStorage = V::get('idStorage', 0, $_REQUEST, 'int');
 			$tblName = V::get('table', '', $_REQUEST, 'word');
 			$objName = V::get('object', '', $_REQUEST, 'word');
+			$namespace = V::get('namespace', '', $_REQUEST, 'word');
 			switch ($task) {
+				case 'obejctList':
 				case 'tableList':
 				case 'viewList':
 				case 'rawInfo':
 					$backLabel = 'Storage';
-					$backLink = 'index.php?_route=Storage';
-					$currentLink = "index.php?_route=Storage&_task={$task}&idStorage={$idStorage}";
+					$backLink = $this->getLink();
+					$currentLink = $this->getLink($task, ['idStorage' => $idStorage]);
 					break;
 				case 'tableStruct':
 					$backLabel = "Tabele [{$idStorage}]";
-					$backLink = "index.php?_route=Storage&_task=tableList&idStorage={$idStorage}";
-					$currentLink = "index.php?_route=Storage&_task={$task}&idStorage={$idStorage}&table={$tblName}";
+					$backLink = $this->getLink('tableList', ['idStorage' => $idStorage]);
+					$currentLink = $this->getLink($task, ['idStorage' => $idStorage, 'table' => $tblName]);
+					break;
+				case 'objectStruct':
+					$backLabel = "Obiekty [{$idStorage}]";
+					$backLink = $this->getLink('tableList', ['idStorage' => $idStorage]);// TODO: mv tableList to objectList
+					$currentLink = $this->getLink($task, ['idStorage' => $idStorage, 'namespace' => $namespace]);
 					break;
-				case 'objectStruct': break;
 				case 'coreObjectStruct':
 					$backLabel = "Obiekty podstawowe";
-					$backLink = "index.php?_route=Storage&_task=coreObjectList";
-					$currentLink = "index.php?_route=Storage&_task={$task}&object={$objName}";
+					$backLink = $this->getLink('coreObjectList', ['idStorage' => $idStorage]);
+					$currentLink = $this->getLink($task, ['idStorage' => $idStorage, 'object' => $objName]);
 					break;
 			}
 			switch ($task) {
@@ -1339,38 +1358,38 @@ jQuery(document).on('p5UIBtnAjax:Storage:checkObjectInstallAjax:ajaxLoaded', fun
 				case 'objectList': $currentLabel = "Obiekty z aktualnej domeny"; break;// TODO: domain from $_GET
 			}
 		}
-		UI::startTag('nav', ['class'=>"navbar navbar-default navbar-static-top", 'style'=>"z-index:999"]);
-		UI::startTag('div', ['class'=>"container-fluid"]);
-		UI::startTag('div', ['class'=>"navbar-left"]);
-			UI::startTag('ul', ['class'=>"nav navbar-nav navbar-center"]);
-				UI::startTag('li');
-					UI::tag('a', ['href'=>$backLink, 'class'=>"btn" . ($backDisabled ? ' disabled' : '')], '<i class="glyphicon glyphicon-chevron-left"></i> ' . $backLabel);
-				UI::endTag('li');
-			UI::endTag('ul');
-		UI::endTag('div');
-		UI::startTag('div', ['class'=>"navbar-left"]);
-			UI::startTag('ul', ['class'=>"nav navbar-nav navbar-center"]);
-				UI::startTag('li');
-					UI::tag('a', ['href'=>$currentLink, 'class'=>"btn"], $currentLabel);
-				UI::endTag('li');
-			UI::endTag('ul');
-		UI::endTag('div');
-		UI::startTag('div', ['class'=>"navbar-right"]);
-			// <ul class="nav navbar-nav navbar-right">
-			// 	<li><a href="#">Link</a></li>
-			// 	<li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
-			// 		<ul class="dropdown-menu">
-			// 			<li><a href="#">Action</a></li>
-			// 			<li><a href="#">Another action</a></li>
-			// 			<li><a href="#">Something else here</a></li>
-			// 			<li role="separator" class="divider"></li>
-			// 			<li><a href="#">Separated link</a></li>
-			// 		</ul>
-			// 	</li>
-			// </ul>
-		UI::endTag('div');
-		UI::endTag('div');// .container-fluid
-		UI::endTag('nav');
+		echo UI::h('nav', ['class'=>"navbar navbar-default navbar-static-top", 'style'=>"z-index:999"], [
+			UI::h('div', ['class'=>"container-fluid"], [
+				UI::h('div', ['class'=>"navbar-left"], [
+					UI::h('ul', ['class'=>"nav navbar-nav navbar-center"], [
+						UI::h('li', [], [
+							UI::h('a', ['href'=>$backLink, 'class'=>"btn" . ($backDisabled ? ' disabled' : '')], '<i class="glyphicon glyphicon-chevron-left"></i> ' . $backLabel)
+						])
+					])
+				]),
+				UI::h('div', ['class'=>"navbar-left"], [
+					UI::h('ul', ['class'=>"nav navbar-nav navbar-center"], [
+						UI::h('li', [], [
+							UI::h('a', ['href'=>$currentLink, 'class'=>"btn"], $currentLabel)
+						])
+					])
+				]),
+				UI::h('div', ['class'=>"navbar-right"], [
+					// <ul class="nav navbar-nav navbar-right">
+					// 	<li><a href="#">Link</a></li>
+					// 	<li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
+					// 		<ul class="dropdown-menu">
+					// 			<li><a href="#">Action</a></li>
+					// 			<li><a href="#">Another action</a></li>
+					// 			<li><a href="#">Something else here</a></li>
+					// 			<li role="separator" class="divider"></li>
+					// 			<li><a href="#">Separated link</a></li>
+					// 		</ul>
+					// 	</li>
+					// </ul>
+				])
+			])
+		]);
 	}
 
 	public function getStorageList() {
@@ -1388,69 +1407,86 @@ jQuery(document).on('p5UIBtnAjax:Storage:checkObjectInstallAjax:ajaxLoaded', fun
 		return $storageList;
 	}
 
-	public function addTableToZasobyAction() {// sends JSON
+	public function addAclObjectToZasobyAction() {// sends JSON
 		$response = new stdClass();
 		try {
-			$idStorage = V::get('storageId', '', $_GET);
-			$tblName = V::get('tblName', '', $_GET, 'word');
-			if (empty($tblName)) throw new HttpException("Wrong table name");
-
-			$storage = DB::getStorage($idStorage);
-
-			$sqlTableName = DB::getPDO($idStorage)->quote($tblName, PDO::PARAM_STR);
-			$dbName = DB::getPDO($idStorage)->getDatabaseName();
-			$rootTableName = DB::getPDO($idStorage)->fetchValue("
-				select t.TABLE_NAME
-				from `information_schema`.`TABLES` t
-				where t.TABLE_SCHEMA = '{$dbName}'
-					and t.TABLE_NAME LIKE {$sqlTableName}
-			");
-			if (!$rootTableName) throw new Exception("Table '{$tblName}' not exists!");
-
-			$tableStruct = $storage->getTableStruct($tblName);
-
-			$zasobStorageId = $storage->getZasobId();
-			if (!is_numeric($zasobStorageId)) throw new HttpException("Storage id is not set in config file");
+			$idStorage = V::get('idStorage', '', $_GET);
+			$namespace = V::get('namespace', '', $_GET);
+			if (empty($idStorage)) throw new HttpException("Missing idStorage param");
+			if (empty($namespace)) throw new HttpException("Missing namespace param");
 
-			$foundZasobItem = null;
-			{
-				$rows = DB::getPDO()->fetchAll("
-					select z.`ID`, z.`DESC`
-					from `CRM_LISTA_ZASOBOW` z
-					where z.`PARENT_ID`='{$zasobStorageId}'
-						and z.`DESC`='{$tblName}'
-						and z.`A_STATUS` in('NORMAL','WAITING')
-				");
-				if (!empty($rows)) {
-					$foundZasobItem = $rows[0]['ID'];
-				}
+			$objectStorage = SchemaFactory::loadDefaultObject('SystemObject');
+			$items = $objectStorage->getItems([
+				'f_namespace' => "={$namespace}",
+			]);
+			if (empty($items)) throw new Exception("SystemObject '{$namespace}' not found");
+			$objectItem = reset($items);
+			if (empty($objectItem)) throw new Exception("SystemObject '{$namespace}' not found");
+			DBG::log($objectItem, 'array', 'object acl $objectItem');
+
+			if ($objectItem['idZasob'] > 0) {
+				// TODO: check if realy exists? @see SchemaFactory::loadDefaultObject('SystemObject')::updateCache()
+				$response->_replaceButtonNode = "[{$objectItem['idZasob']}]";
+				throw new AlertInfoException("Zasob '{$objectItem['namespace']}' już istnieje - nr '{$objectItem['idZasob']}'");
 			}
 
-			if ($foundZasobItem > 0) {
-				$response->_replaceButtonNode = "[{$foundZasobItem}]";
-				throw new AlertInfoException("Zasob tabela '{$tblName}' już istnieje - nr '{$foundZasobItem}'");
+			$idZasobFound = 0;
+			switch ($objectItem['_type']) {
+				case 'TableAcl': $idZasobFound = DB::getPDO()->fetchValue(" select ID from CRM_LISTA_ZASOBOW where PARENT_ID = {$objectItem['idDatabase']} and `DESC` = '{$objectItem['name']}' "); break;
+				case 'AntAcl': $idZasobFound = DB::getPDO()->fetchValue(" select ID from CRM_LISTA_ZASOBOW where PARENT_ID = {$objectItem['idDatabase']} and `DESC` = '{$objectItem['namespace']}' "); break;
+				default: throw new Exception("Not implemented acl type '{$objectItem['_type']}'");
+			}
+			if ($idZasobFound > 0) {
+				DB::getPDO()->update($objectStorage->_rootTableName, 'namespace', $objectItem['namespace'], [
+					'idZasob' => $idZasobFound
+				]);
+				$response->id = $idZasobFound;
+				throw new AlertSuccessException("Zasob '{$objectItem['namespace']}' już istnieje - nr '{$idZasobFound}' - cache zaktualizowany");
 			}
 
 			try {
 				$acl = User::getAcl()->getObjectAcl('default_db', 'crm_lista_zasobow');
 			} catch (Exception $e) {
+				DBG::log($e);
 				throw new Exception("Brak dostępu do tabeli Zasoby");
 			}
 
-			$item = array();
-			$item['PARENT_ID'] = $zasobStorageId;
-			$item['TYPE'] = 'TABELA';
-			$item['DESC'] = $rootTableName;
-			$item['DESC_PL'] = $tblName;
-			if (DBG::isActive()) $response->_itemToCreate = $item;
-
-			$createdId = $acl->addItem($item);
-			if (!$createdId) throw new Exception("Nie udało się utworzyć nowego rekordu!");
-
-			$response->id = $createdId;
-			$response->record = $acl->getItem($createdId);
-			$response->_replaceButtonNode = "[{$createdId}]";
-			throw new AlertSuccessException("Utworzono pomyślnie rekord nr {$createdId}");
+			if (empty($objectItem['idDatabase'])) throw new Exception("Missing database id");
+			if ($idStorage != $objectItem['idDatabase']) throw new Exception("Database id must be the same");
+			if (empty($objectItem['_rootTableName'])) throw new Exception("Missing root table name");
+
+			switch ($objectItem['_type']) {
+				case 'TableAcl':
+					$newZasobItem = [
+						'PARENT_ID' => $objectItem['idDatabase'],
+						'TYPE' => 'TABELA',
+						'DESC' => $objectItem['_rootTableName'],
+						'DESC_PL' => $objectItem['name'],
+					];
+					break;
+				case 'AntAcl':
+					$newZasobItem = [
+						'PARENT_ID' => $objectItem['idDatabase'],
+						'TYPE' => 'TABELA',
+						'DESC' => $objectItem['namespace'],
+						'DESC_PL' => $objectItem['name'],
+					];
+					break;
+				default: throw new Exception("Not implemented acl type '{$objectItem['_type']}'");
+			}
+			$idCreated = $acl->addItem($newZasobItem);
+			if (!$idCreated) throw new Exception("Nie udało się utworzyć nowego rekordu!");
+			try {
+				DB::getPDO()->update($objectStorage->_rootTableName, 'namespace', $objectItem['namespace'], [
+					'idZasob' => $idCreated
+				]);
+			} catch (Exception $e) {
+				DBG::log($e);
+			}
+			$response->id = $idCreated;
+			$response->record = $acl->getItem($idCreated);
+			$response->_replaceButtonNode = "[{$idCreated}]";
+			throw new AlertSuccessException("Utworzono pomyślnie rekord nr {$idCreated}");
 		} catch (AlertSuccessException $e) {
 			$response->type = 'success';
 			$response->msg = $e->getMessage();
@@ -1460,6 +1496,7 @@ jQuery(document).on('p5UIBtnAjax:Storage:checkObjectInstallAjax:ajaxLoaded', fun
 		} catch (Exception $e) {
 			$response->type = 'error';
 			$response->msg = $e->getMessage();
+			DBG::log($e);
 		}
 
 		Response::sendJsonExit($response);

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

@@ -221,6 +221,7 @@ class Schema_SystemObjectStorageAcl extends Core_AclSimpleSchemaBase {
           'namespace' => $namespace,
           'idDatabase' => $idDatabase,
           '_type' => "AntAcl",
+          '_rootTableName' => $rootTableName,
           'isActive' => 1
         ]);
       } catch (Exception $e) {