Piotr Labudda 8 years ago
parent
commit
873b0b36e3
1 changed files with 184 additions and 0 deletions
  1. 184 0
      SE/se-lib/Route/Test/TypespecialBug.php

+ 184 - 0
SE/se-lib/Route/Test/TypespecialBug.php

@@ -0,0 +1,184 @@
+<?php
+
+Lib::loadClass('RouteBase');
+Lib::loadClass('ACL');
+
+class Route_Test_TypespecialBug extends RouteBase {
+
+	function defaultAction() { UI::layout([ $this, 'defaultView' ]); }
+	function defaultView() {
+		echo "<p>Jeśli PARAM_IN wskazuje na komórkę wirtualną (nieistniejącą w strukturze tabeli) to PARAM_OUT nie może zwracać wartości (posiadać aliasa)<p>";
+		echo "<p>TODO: funkcja sprawdzająca system: wyszukać takie typespecial-e i/lub od razu naprawić usuwając ALIAS z PARMA_OUT</p>";
+
+		if ('fixByParamOutAliasId' === V::get('_postTask', '', $_POST)) {
+			echo UI::h('a', [ 'class' => "btn btn-primary", 'href' => $this->getLink() ], "wróć");
+			echo '<hr>';
+			$id = V::get('param_out_alias_id', 0, $_POST, 'int');
+
+			$listTypespecialsInfo = DB::getPDO()->fetchAll("
+				select z.`ID`
+					, z.`ALIAS_ID` as fieldID
+					, zpp.`ID` as TYPE_ID
+					, zpp.`DESC` as LABEL
+					, zp.`ID` as PARAM_OUT_ID
+					, zp.`DESC` as PARAM_OUT_DESC
+					, zp.`ALIAS_ID` as RET_ID
+					, IF (zp.ALIAS_ID is not null and zp.ALIAS_ID > 0,
+						( select c.xsdType
+							from `CRM_#CACHE_ACL_OBJECT_FIELD` c
+							where c.idZasob = zp.ALIAS_ID
+						)
+						, NULL) as out_alias_xsdType
+				from `CRM_LISTA_ZASOBOW` as z
+					join `CRM_LISTA_ZASOBOW` as zp on(zp.`ID`=z.`PARENT_ID` and zp.`TYPE` = 'PARAM_OUT')
+					join `CRM_LISTA_ZASOBOW` as zpp on(zpp.`ID`=zp.`PARENT_ID`)
+				where zpp.`TYPE`='TYPESPECIAL'
+					and zpp.`A_STATUS` in('WAITING','NORMAL')
+					and zp.`TYPE`='PARAM_OUT'
+					and z.`TYPE`='PARAM_IN'
+					and zp.ALIAS_ID = :id
+			", [ ':id' => $id ]);
+			UI::table([ 'rows' => $listTypespecialsInfo ]);
+			array_walk($listTypespecialsInfo, function ($row) use ($id) {
+				UI::alert('warning', "Usuwam ALIAS_ID z PARAM_OUT (ID={$id}) - Alias do zasobu id = {$row['PARAM_OUT_ID']}");
+				$affected = DB::getPDO()->update('CRM_LISTA_ZASOBOW', 'ID', $row['PARAM_OUT_ID'], [
+					'ALIAS_ID' => 0
+				]);
+				if ($affected > 0) DB::getPDO()->insert('CRM_LISTA_ZASOBOW_HIST', [
+					'ID_USERS2' => $row['PARAM_OUT_ID'],
+					'ALIAS_ID' => 0,
+					'A_RECORD_UPDATE_DATE' => 'NOW()',
+					'A_RECORD_UPDATE_AUTHOR' => 'fix-typespecial-bug',
+				]);
+			});
+
+			throw new Exception("TODO: fix ts by PARAM_OUT.ALIAS_ID = {$id}");
+
+			// UI::hSimplePostTaskButton("napraw", 'fixByParamOutAliasId', [ 'class' => "btn btn-danger", 'param_out_alias_id' => $id ]),
+		}
+
+		$listTypespecialsInfo = DB::getPDO()->fetchAll("
+			select z.`ID`
+				, z.`ALIAS_ID` as fieldID
+				, zpp.`ID` as TYPE_ID
+				, zpp.`DESC` as LABEL
+				, zp.`ID` as PARAM_OUT_ID
+				, zp.`DESC` as PARAM_OUT_DESC
+				, zp.`ALIAS_ID` as RET_ID
+				, IF (zp.ALIAS_ID is not null and zp.ALIAS_ID > 0,
+					( select c.xsdType
+						from `CRM_#CACHE_ACL_OBJECT_FIELD` c
+						where c.idZasob = zp.ALIAS_ID
+					)
+					, NULL) as out_alias_xsdType
+			from `CRM_LISTA_ZASOBOW` as z
+				join `CRM_LISTA_ZASOBOW` as zp on(zp.`ID`=z.`PARENT_ID` and zp.`TYPE` = 'PARAM_OUT')
+				join `CRM_LISTA_ZASOBOW` as zpp on(zpp.`ID`=zp.`PARENT_ID`)
+			where zpp.`TYPE`='TYPESPECIAL'
+				and zpp.`A_STATUS` in('WAITING','NORMAL')
+				and zp.`TYPE`='PARAM_OUT'
+				and z.`TYPE`='PARAM_IN'
+		");
+		$grouped = array_reduce($listTypespecialsInfo, function ($ret, $ts) {
+			$id = $ts['TYPE_ID'];
+			if (!array_key_exists($id, $ret)) $ret[$id] = [ 'label' => $ts['LABEL'], 'fields' => [] ];
+			$ret[$id]['fields'][] = $ts;
+			return $ret;
+		}, []);
+		UI::table([ 'rows' => array_map(function ($gr) {
+			return [
+				'label' => $gr['label'],
+				// 'row' => UI::h('pre', [ 'style' => "font-size:x-small" ], var_export($gr['fields'], true)),
+				'row' => UI::hTable([ 'rows' => $gr['fields'] ]),
+			];
+		}, $grouped) ]);
+
+		$retIds = array_reduce($listTypespecialsInfo, function ($ret, $ts) {
+			if ($ts['RET_ID'] > 0 && !in_array($ts['RET_ID'], $ret)) $ret[] = $ts['RET_ID'];
+			return $ret;
+		}, []);
+		DBG::nicePrint($retIds, '$retIds');
+
+		if (empty($retIds)) return;
+
+		$sqlRetIds = implode(",", $retIds);
+		$retCellsInfo = DB::getPDO()->fetchAllByKey("
+			select i.*
+			from `CRM_PROCES_idx_TABLES_INFO_VIEW` i
+			where i.ID_CELL in( {$sqlRetIds} )
+		", 'ID_CELL');
+		UI::table([ 'caption' => "Ret id info", 'rows' => array_map(function ($id) use ($retCellsInfo) {
+			if (!array_key_exists($id, $retCellsInfo)) {
+				return [
+					'ID' => $id,
+					'msg' => "<nobr>ERROR: Zasób nie jest komórką</nobr>" . UI::hSimplePostTaskButton("napraw", 'fixByParamOutAliasId', [ 'class' => "btn btn-danger", 'data' => [ 'param_out_alias_id' => $id ] ]),
+					'info' => UI::hTable([ 'rows' => [ DB::getPDO()->fetchFirst(" select * from CRM_LISTA_ZASOBOW where ID = :id ", [ ':id' => $id ]) ] ]),
+				];
+			} else {
+				$cellInfo = $retCellsInfo[$id];
+				$ns = "default_db/{$cellInfo['TABLE_NAME']}";
+				$fieldName = $cellInfo['CELL_NAME'];
+				try {
+					$isLocalField = ACL::getAclByNamespace($ns)->isLocalField($fieldName);
+				} catch (Exception $e) {
+					// Access Denied for 'default_db/...' -> Try to find in table struct
+					try {
+						$idDB = $cellInfo['ID_DATABASE'];
+						if (!DB::getPDO($idDB)) throw new Exception("DB {$idDB} not exists");
+						$dbName = DB::getPDO($idDB)->getDatabaseName();
+						$tableName = $cellInfo['TABLE_NAME'];
+						$zasobInfo = DB::getPDO()->fetchFirst(" select * from CRM_LISTA_ZASOBOW where ID = :id ", [ ':id' => $id ]);
+						$structInfo = DB::getPDO()->fetchFirst(" select * from information_schema.COLUMNS where TABLE_SCHEMA = :dbName and TABLE_NAME = :tableName ", [ ':tableName' => $tableName, ':dbName' => $dbName ]);
+						if (!$structInfo) {
+							return [
+								'ID' => $id,
+								'msg' => "<nobr>ERROR: check table struct</nobr>" . UI::hSimplePostTaskButton("napraw", 'fixByParamOutAliasId', [ 'class' => "btn btn-danger", 'data' => [ 'param_out_alias_id' => $id ] ]),
+								'info' => UI::hTable([ 'rows' => [
+									[ 'type' => 'cell', 'row' => UI::hTable([ 'rows' => [ $cellInfo ] ]) ],
+									[ 'type' => 'zasob', 'row' => UI::hTable([ 'rows' => [ $zasobInfo ] ]) ],
+									[ 'type' => 'struct', 'row' => UI::hTable([ 'rows' => [ $structInfo ] ]) ],
+								] ]),
+							];
+						} else {
+							return [
+								'ID' => $id,
+								'msg' => "<nobr>OK?: struct ok</nobr>",
+								'info' => UI::hTable([ 'rows' => [
+									[ 'type' => 'cell', 'row' => UI::hTable([ 'rows' => [ $cellInfo ] ]) ],
+									[ 'type' => 'zasob', 'row' => UI::hTable([ 'rows' => [ $zasobInfo ] ]) ],
+									[ 'type' => 'struct', 'row' => UI::hTable([ 'rows' => [ $structInfo ] ]) ],
+								] ]),
+							];
+						}
+					} catch (Exception $e) {
+						return [
+							'ID' => $id,
+							'msg' => "<nobr>ERR: " . $e->getMessage() . "</nobr>" . UI::hSimplePostTaskButton("napraw", 'fixByParamOutAliasId', [ 'class' => "btn btn-danger", 'data' => [ 'param_out_alias_id' => $id ] ]),
+							'info' => UI::hTable([ 'rows' => [
+								[ 'type' => 'cell', 'row' => UI::hTable([ 'rows' => [ $cellInfo ] ]) ],
+								[ 'type' => 'zasob', 'row' => UI::hTable([ 'rows' => [ $zasobInfo ] ]) ],
+								[ 'type' => 'struct', 'row' => UI::hTable([ 'rows' => [ $structInfo ] ]) ],
+							] ]),
+						];
+					}
+				}
+				if ($isLocalField) {
+					return [
+						'ID' => $id,
+						'msg' => "<nobr>OK: Zasób jest komórką</nobr>",
+						'info' => UI::hTable([ 'rows' => [ $retCellsInfo[$id] ] ]),
+					];
+				} else {
+					return [
+						'ID' => $id,
+						'msg' => "<nobr>ERR: Zasób nie istnieje w tabeli</nobr>" . UI::hSimplePostTaskButton("napraw", 'fixByParamOutAliasId', [ 'class' => "btn btn-danger", 'data' => [ 'param_out_alias_id' => $id ] ]),
+						'info' => UI::hTable([ 'rows' => [ $retCellsInfo[$id] ] ]),
+					];
+				}
+			}
+		}, $retIds) ]);
+
+		throw new Exception("TODO: ...");
+	}
+
+}