Browse Source

Merge branch 'master' of bn.git:plabudda/se

Piotr Labudda 7 years ago
parent
commit
ab57e55794

+ 19 - 0
SE/se-lib/Core/AclHelper.php

@@ -125,6 +125,25 @@ class Core_AclHelper {// Helper class for Acl
 					$acl = StorageAclBase::buildInstance($objItem['idZasob'], $objItem);
 					return $acl;
 				} break;
+				case 'TableAcl': {
+					if (!$objItem['isObjectActive']) throw new Exception("TODO:RENAME Not Implemented acl type '{$objItem['_type']}' for namespace '{$namespace}' -- Object not active");
+					Lib::loadClass('TableAcl');
+					$tableAcl = new TableAcl($objItem['idZasob']);
+					$tableAcl->fromArray([
+						'db' => $objItem['idDatabase'],
+						'name' => $objItem['name'],
+						'_rootTableName' => $objItem['_rootTableName'],
+						'label' => '', // TODO: read from zasoby table?
+						'opis' => '', // TODO: read from zasoby table?
+						'fields' => [], // if not set then will be fetched by `TableAcl::fieldsInit`
+						// 'virtualFieldsIdList' => [],
+						// 'types' => [],
+					]);
+					$tableAcl->_primaryKeyField = $objItem['primaryKey'];
+					$tableAcl->_sourceNamespace = $objItem['nsPrefix'];
+					DBG::log($tableAcl, 'array', "DBG: \$tableAcl '{$namespace}'");
+					return $tableAcl;
+				}
 				default: throw new Exception("Not Implemented acl type '{$objItem['_type']}' for namespace '{$namespace}'");
 			}
 		} catch (Exception $e) {

+ 59 - 8
SE/se-lib/Route/Storage.php

@@ -989,11 +989,8 @@ class Route_Storage extends RouteBase {
 		UI::dol();
 	}
 
-	public function activateObjectAjaxAction() {
-		DBG::log($_REQUEST, 'array', '$_REQUEST');
-		Response::sendTryCatchJson(array($this, 'activateObjectAjax'), $_REQUEST);
-	}
-	public function activateObjectAjax($args) {
+	function activateObjectAjaxAction() { Response::sendTryCatchJson(array($this, 'activateObjectAjax'), $_REQUEST); }
+	function activateObjectAjax($args) {
 		$namespace = V::get('namespace', '', $args);
 		if (empty($namespace)) throw new Exception("Missing param namespace");
 		$item = SchemaFactory::loadDefaultObject('SystemObject')->getItem($namespace, [ 'propertyName' => '*,field' ]);
@@ -1010,9 +1007,40 @@ class Route_Storage extends RouteBase {
 		if (empty($activeFields)) throw new Exception("Missing active fields for namespace '{$namespace}'");
 
 		$affected = SchemaFactory::loadDefaultObject('SystemObject')->updateItem([
-      'namespace' => $item['namespace'],
-      'isObjectActive' => 1
-    ]);
+			'namespace' => $item['namespace'],
+			'isObjectActive' => 1,
+		]);
+		if ($affected < 0) throw new Exception("Nie udało się aktywować obiektu '{$namespace}'");
+		return [
+			'type' => "success",
+			'msg' => "Aktywowano obiekt '{$namespace}'",
+			'body' => [
+				'isObjectActive' => 1
+			]
+		];
+	}
+
+	function activateTableAclAjaxAction() { Response::sendTryCatchJson(array($this, 'activateTableAclAjax'), $_REQUEST); }
+	function activateTableAclAjax($args) {
+		$namespace = V::get('namespace', '', $args);
+		if (empty($namespace)) throw new Exception("Missing param namespace");
+		$item = SchemaFactory::loadDefaultObject('SystemObject')->getItem($namespace, [ 'propertyName' => '*,field' ]);
+		if (empty($item)) throw new HttpException("Namespace not found", 404);
+		DBG::log($item, 'array', "activateTableAclAjax \$item");
+		if ($item['isObjectActive']) throw new AlertSuccessException("Namespace '{$namespace}' already active");
+		// if (!$item['hasStruct']) throw new Exception("Missing struct for namespace '{$namespace}'");
+		// if (!$item['isStructInstalled']) throw new Exception("Namespace struct not installed '{$namespace}'");
+		// $activeFields = array_filter($item['field'], function ($field) {
+		// 	if (!$field['isActive']) return false;
+		// 	if (!$field['idZasob']) return false;
+		// 	return true;
+		// });
+		// if (empty($activeFields)) throw new Exception("Missing active fields for namespace '{$namespace}'");
+
+		$affected = SchemaFactory::loadDefaultObject('SystemObject')->updateItem([
+			'namespace' => $item['namespace'],
+			'isObjectActive' => 1,
+		]);
 		if ($affected < 0) throw new Exception("Nie udało się aktywować obiektu '{$namespace}'");
 		return [
 			'type' => "success",
@@ -1023,4 +1051,27 @@ class Route_Storage extends RouteBase {
 		];
 	}
 
+	function deactivateObjectAjaxAction() { Response::sendTryCatchJson(array($this, 'deactivateObjectAjax'), $_REQUEST); }
+	function deactivateObjectAjax($args) {
+		$namespace = V::get('namespace', '', $args);
+		if (empty($namespace)) throw new Exception("Missing param namespace");
+		$item = SchemaFactory::loadDefaultObject('SystemObject')->getItem($namespace, [ 'propertyName' => '*,field' ]);
+		if (empty($item)) throw new HttpException("Namespace not found", 404);
+		DBG::log($item, 'array', "activateObjectAjax \$item");
+		if (!$item['isObjectActive']) throw new AlertSuccessException("Namespace '{$namespace}' already not active");
+
+		$affected = SchemaFactory::loadDefaultObject('SystemObject')->updateItem([
+			'namespace' => $item['namespace'],
+			'isObjectActive' => 0,
+		]);
+		if ($affected < 0) throw new Exception("Nie udało się wyłączyć obiektu '{$namespace}'");
+		return [
+			'type' => "success",
+			'msg' => "Wyłączono obiekt '{$namespace}'",
+			'body' => [
+				'isObjectActive' => 0
+			]
+		];
+	}
+
 }

+ 99 - 28
SE/se-lib/Route/Storage/AclStruct.php

@@ -8,13 +8,13 @@ Lib::loadClass('SchemaFactory');
 
 class Route_Storage_AclStruct extends RouteBase {
 
-	public function handleAuth() {
+	function handleAuth() {
 		if (!User::logged()) {
 			User::authByRequest();
 		}
 	}
 
-	public function defaultAction() {
+	function defaultAction() {
 		UI::gora();
 		UI::menu();
 		// Router::getRoute('Storage')->navView(); // TODO: header like in Storage_AclUsage
@@ -35,6 +35,24 @@ class Route_Storage_AclStruct extends RouteBase {
 			// [nsPrefix] => default_db__x3A__CRM_PROCES
 			// [typeName] => default_db__x3A__CRM_PROCES:CRM_PROCES
 			// [reinstallLink] => https://biuro.biall-net.pl/dev-pl/se-master/index.php?_route=Storage_AclReinstall&namespace=default_db/CRM_PROCES/CRM_PROCES
+
+			if ('_activateTableAclPostTask' === V::get('_postTask', '', $_POST)) {
+				try {
+					$ret = Router::getRoute('Storage')->activateTableAclAjax([ 'namespace' => $namespace ]);
+					if (!empty($ret) && 'success' == $ret['type']) UI::alert('success', $ret['msg']);
+				} catch (Exception $e) {
+					UI::alert('danger', $e->getMessage());
+				}
+			}
+			if ('_deactivateObjectPostTask' === V::get('_postTask', '', $_POST)) {
+				try {
+					$ret = Router::getRoute('Storage')->deactivateObjectAjax([ 'namespace' => $namespace ]);
+					if (!empty($ret) && 'success' == $ret['type']) UI::alert('success', $ret['msg']);
+				} catch (Exception $e) {
+					UI::alert('danger', $e->getMessage());
+				}
+			}
+
 			$item = SchemaFactory::loadDefaultObject('SystemObject')->getItem($namespace, [ 'propertyName' => '*,field' ]);
 			switch ($item['_type']) {
 				case 'TableAcl': $this->tableStructView($item, $item['name'], $item['idDatabase']); break;
@@ -50,7 +68,7 @@ class Route_Storage_AclStruct extends RouteBase {
 		UI::dol();
 	}
 
-	public function tableStructAction() {
+	function tableStructAction() {
 		UI::gora();
 		UI::menu();
 		// Router::getRoute('Storage')->navView(); // TODO: header like in Storage_AclUsage
@@ -213,9 +231,9 @@ class Route_Storage_AclStruct extends RouteBase {
 			]),
 			UI::h('div', ['style'=>"padding:4px 24px; border-top:1px solid #fff"], [
 				UI::h('span', ['style'=>"margin-right:12px"], [
-					( ($item['idZasob'] > 0)
-					? UI::h('span', [ 'title' => "Nr zasobu '{$item['idZasob']}'" ], "Nr zasobu [{$item['idZasob']}]")
-					: UI::h('span', [ 'title' => "Brak nr zasobu - dodaj do zasobów" ], [
+					(	($item['idZasob'] > 0)
+					?	UI::h('span', [ 'title' => "Nr zasobu '{$item['idZasob']}'" ], "Nr zasobu [{$item['idZasob']}]")
+					:	UI::h('span', [ 'title' => "Brak nr zasobu - dodaj do zasobów" ], [
 							UI::hButtonAjax("+ do zasobów", 'addAclObjectToZasobyAjax', [
 								'class' => "btn btn-xs btn-primary",
 								'href' => Router::getRoute('Storage')->getLink('addAclObjectToZasobyAjax'),
@@ -227,41 +245,53 @@ class Route_Storage_AclStruct extends RouteBase {
 						])
 					),
 				]),
-				// UI::h('span', ['style'=>"margin:0 12px"], [
-				// 	( ($item['isObjectActive'] > 0)
-				// 		? UI::h('span', [ 'class' => "label label-success", 'title' => "Namespace active" ], "Active")
-				// 		: UI::h('span', [ 'title' => "Namespace not active" ], [
-				// 			($item['idZasob'] > 0)
-				// 			? UI::hButtonAjax("Aktywuj", 'activateObjectAjax', [
-				// 					'class' => "btn btn-xs btn-primary",
-				// 					'href' => Router::getRoute('Storage')->getLink('activateObjectAjax'),
-				// 					'data' => [
-				// 						'namespace' => $item['namespace'],
-				// 					]
-				// 				])
-				// 			: '',
-				// 		])
-				// 	),
-				// ]),
+				UI::h('span', ['style'=>"margin:0 12px"], [
+					(	($item['isObjectActive'] > 0)
+					?	UI::h('span', [ 'class' => "label label-success", 'title' => "Namespace active" ], "Active")
+					:	UI::h('span', [ 'title' => "Namespace not active" ], [
+							($item['idZasob'] > 0)
+							?	UI::hButtonPost("Aktywuj", [
+									'class' => "btn btn-xs btn-primary",
+									'data' => [
+										'_postTask' => '_activateTableAclPostTask',
+										'namespace' => $item['namespace'],
+									]
+								])
+							:	'',
+						])
+					),
+					(	($item['isObjectActive'] > 0)
+						?	UI::h('span', [ 'title' => "Namespace is active" ], [
+								UI::hButtonPost("Wyłącz", [
+									'class' => "btn btn-xs btn-default",
+									'data' => [
+										'_postTask' => '_deactivateObjectPostTask',
+										'namespace' => $item['namespace'],
+									]
+								])
+							])
+						:	''
+					),
+				]),
 				UI::h('a', [
 					'href' => "index.php?_route=ViewTableAjax&namespace={$item['namespace']}",
 					'class' => "btn btn-sm btn-link"
 				], "Przeglądaj tabelę"),
-				( ($item['idZasob'] > 0)
-					? UI::h('a', [
+				(	($item['idZasob'] > 0)
+					?	UI::h('a', [
 							'href' => "procesy5.php?task=CRM_LISTA_ZASOBOW&filtr_id={$item['idZasob']}&filtr_ids=%2B&filtr_ob=%2B",
 							'class' => "btn btn-sm btn-link",
 							'title' => "Struktura tabeli w drzewie zasobów"
 						], "Zasoby")
-					: ''
+					:	''
 				),
-				( ($item['idZasob'] > 0)
-					? UI::h('a', [
+				(	($item['idZasob'] > 0)
+					?	UI::h('a', [
 							'href' => "index.php?FUNCTION_INIT=PROCES_MENU&HEADER_NOT_INIT=YES&_task=PROCES_FOR_TABLE&tblId={$item['idZasob']}",
 							'class' => "btn btn-sm btn-link",
 							'title' => "Procesy dla aktualnie przeglądanej tabeli"
 						], "Procesy")
-					: ''
+					:	''
 				),
 				// UI::h('a', [
 				// 	'href' => Router::getRoute('Storage_AclReinstall')->getLink('', [ 'namespace' => $item['namespace'] ]),
@@ -284,6 +314,47 @@ class Route_Storage_AclStruct extends RouteBase {
 				], "xsd"),
 			])
 		]);
+
+		$listFieldNames = array_map(V::makePick('name'), $tableList);
+		if ('_setTableAclPrimaryKey' === V::get('_postTask', '', $_POST)) {
+			$primaryKey = V::get('primryKey', '', $_POST);
+			DBG::nicePrint($primaryKey, "\$primaryKey='{$primaryKey}'");
+			if (!in_array($primaryKey, $listFieldNames)) {
+				UI::alert('danger', "Wybrano niewłaściwe pole");
+			} else {
+				UI::alert('warning', "TODO: ustaw '{$primaryKey}'");
+				SchemaFactory::loadDefaultObject('SystemObject')->updateItem([
+					'namespace' => $item['namespace'],
+					'primaryKey' => $primaryKey,
+				]);
+				$item = SchemaFactory::loadDefaultObject('SystemObject')->getItem($item['namespace'], [ 'propertyName' => '*,field' ]);
+			}
+		}
+		$primaryKey = $item['primaryKey'];
+		echo UI::h('form', [
+			'class' => "form-inline",
+			'style' => "margin:12px 0",
+			'method' => "POST",
+		], [
+			UI::h('label', [], "Primary Key:"),
+			UI::h('select', [ 'type' => "select", 'class' => "form-control", 'name' => "primryKey" ],
+				array_merge(
+					[ UI::h('option', [ 'value' => "-1" ], " [  wybierz ] ") ],
+					array_map(function ($itemTableList) use ($primaryKey) {
+						return UI::h('option', array_merge(
+							[ 'value' => $itemTableList['name'] ],
+							($primaryKey === $itemTableList['name'])
+							?	[ 'selected' => "selected" ]
+							:	[]
+						), $itemTableList['name']);
+					}, $tableList)
+				)
+			),
+			UI::h('input', [ 'type' => "hidden", 'name' => "_postTask", 'value' => "_setTableAclPrimaryKey" ]),
+			UI::h('input', [ 'type' => "submit", 'class' => "btn btn-default", 'value' => "Zapisz" ]),
+		]);
+		if (!$item['primaryKey']) UI::alert('danger', "Nie ustawiono primaryKey");
+
 		UI::table([
 			'__html_id' => 'struct_table',
 			'caption' => "Struktura tabeli {$tblName} [{$idTable}]",

+ 15 - 7
SE/se-lib/TableAcl.php

@@ -42,11 +42,13 @@ class TableAcl extends Core_AclBase {
 	public $_types = array();
 	public $_virtualFieldsIdList = array();
 	public $_schemaLoaded = false;
+	public $_primaryKeyField = null;
+	public $_sourceNamespace = null;
 
 	public function __construct($zasobID) { $this->_zasobID = $zasobID; }
-	public function getNamespace() { return 'default_db/' . $this->getName(); }
-	public function getRootNamespace() { return 'default_db/' . $this->getName(); }
-	public function getSourceName() { return 'default_db'; }
+	public function getNamespace() { return $this->_sourceNamespace ? $this->_sourceNamespace . "/" . $this->getName() : 'default_db/' . $this->getName(); }
+	public function getRootNamespace() { return $this->getNamespace(); }
+	public function getSourceName() { return $this->_sourceNamespace ? $this->_sourceNamespace : 'default_db'; }
 	public function getName() { return $this->_name; }
 	public function getRootTableName() {
 		if (empty($this->_name)) return null;// throw new Exception("Table name not defined");
@@ -952,7 +954,7 @@ class TableAcl extends Core_AclBase {
 				if (empty($objectList)) throw new Exception("Acl [{$idTable}] not exists in SystemObject"); // TODO: update Storage object list?
 
 				$objItem = reset($objectList);
-				DBG::log($objItem, 'array', "DBG objItem({$idTable})");
+				// DBG::log($objItem, 'array', "DBG objItem({$idTable})");
 				switch ($objItem['_type']) {
 					// case 'TableAcl': // TODO: TEST - to replace TableAcl by AntAcl or use object with namespace + '/tableName'?
 					case 'AntAcl': {
@@ -1456,7 +1458,7 @@ class TableAcl extends Core_AclBase {
 	}
 
 	public function fromArray($arr) {
-		// DBG::log("fromArray(name='{$arr['name']}')");
+		// DBG::log($arr, 'array', "fromArray(name='{$arr['name']}')");
 		$this->_db = $arr['db'];
 		$this->_name = $arr['name'];
 		$this->_rootTableName = V::get('_rootTableName', null, $arr);
@@ -1805,16 +1807,22 @@ class TableAcl extends Core_AclBase {
 		$dsConfig['field_types'] = $this->getTypes();
 		$dsConfig['fields_virtual'] = $this->getVirtualFieldListByIdZasob();
 		$dsConfig['acl_fltr_allowed'] = !$this->hasSuperAccessPerms(); // filtr Access is visible only if user dont have supe access perms. If has then see all rows
+		// DBG::log($dsConfig, 'array', "DBG:DataSourceFactory::buildFromZasobInfo(\$dsConfig)");
 		return DataSourceFactory::buildFromZasobInfo($dsConfig);
 	}
 
 	public function getPrimaryKeyField() {
+		if ($this->_primaryKeyField) return $this->_primaryKeyField;
 		if ($this->loadSchema()) {
 			$pkField = $this->_schemaClass->getPrimaryKeyField();
-			if ($pkField) return $pkField;
+			if ($pkField) {
+				$this->_primaryKeyField = $pkField;
+				return $this->_primaryKeyField;
+			}
 		}
 		$ds = $this->getDataSource();
-		return $ds->getPrimaryKeyField();
+		$this->_primaryKeyField = $ds->getPrimaryKeyField();
+		return $this->_primaryKeyField;
 	}
 
 	public function isIntegerField($fldName) {