فهرست منبع

add basic FixZasobBaseAlias

Piotr Labudda 10 سال پیش
والد
کامیت
26dcda8c33
1فایلهای تغییر یافته به همراه188 افزوده شده و 0 حذف شده
  1. 188 0
      SE/se-lib/Route/FixZasobBaseAlias.php

+ 188 - 0
SE/se-lib/Route/FixZasobBaseAlias.php

@@ -0,0 +1,188 @@
+<?php
+
+Lib::loadClass('RouteBase');
+
+class Route_FixZasobBaseAlias extends RouteBase {
+
+	public function handleAuth() {
+		if (!User::logged()) {
+			throw new HttpException('Unauthorized', 401);
+		}
+	}
+
+	public function defaultAction() {
+		SE_Layout::gora();
+		?>
+<div class="jumbotron">
+  <div class="container">
+		<form class="form-inline" method="POST">
+			<input type="hidden" name="_route" value="FixZasobPath" />
+			<input type="hidden" name="_task" value="run" />
+			<button type="submit" id="fldExecuteBtn" class="btn btn-primary" autocomplete="off">
+				Zaktualizuj <code>ID_ZASOB_BASE</code>
+			</button>
+		</form>
+	</div>
+</div>
+<script type="text/javascript">
+jQuery(document).ready(function () {
+  jQuery('#fldExecuteBtn').on('click', function () {
+    jQuery(this).text(jQuery(this).text() + '...').attr('disabled', 'disabled');
+		jQuery(this).parent().submit();
+  })
+});
+</script>
+		<?php
+		SE_Layout::dol();
+	}
+
+	public function checkPathsAction() {
+		$sql = "
+
+		";
+		$rows = array();
+		$db = DB::getDB();
+		if ($db->has_errors()) {
+			throw new Exception("DB Errors: " . implode("\n<br>", $db->get_errors()));
+		}
+		$res = $db->query($sql);
+		while ($r = $db->fetch($res)) {
+			$rows[] = $r;
+		}
+		echo $sql;
+		echo'<pre>';print_r($rows);echo'</pre>';
+		die('OK');
+	}
+
+	public function runAction() {
+		SE_Layout::gora();
+		SE_Layout::menu();
+
+		$this->_callProcedure();
+
+		?>
+		<div class="container">
+			<div class="alert alert-success">
+				Zaktualizowano ścieżki zasobów
+			</div>
+		</div>
+		<?php
+		SE_Layout::dol();
+	}
+
+	public function runApiAction() {
+		$this->_callProcedure();
+		die('Zaktualizowano ścieżki zasobów');
+	}
+
+	private function _callProcedure() {
+		$sql = <<<SQL
+update `CRM_LISTA_ZASOBOW` SET
+	`ID_ZASOB_BASE`=IF(`ALIAS_ID`>0, FIND_ID_ZASOB_BASE(`ALIAS_ID`), `ID`);
+SQL;
+		/* update fields:
+		 *	`CRM_LISTA_ZASOBOW`.`path`
+		 *	`CRM_WSKAZNIK`.`path_CRM_LISTA_ZASOBOW`
+		 */
+		$db = DB::getDB();
+		if ($db->has_errors()) {
+			throw new Exception("DB Errors: " . implode("\n<br>", $db->get_errors()));
+		}
+		$res = $db->query($sql);
+		if ($db->has_errors()) {
+			throw new Exception("DB Errors: " . implode("\n<br>", $db->get_errors()));
+		}
+	}
+
+	public function reinstallAction() {
+		$this->reinstall();
+		die('OK');
+	}
+
+	public function reinstall() {
+		$sqlList = array();
+		$sqlList['RemoveFunction'] = "DROP FUNCTION IF EXISTS `FIND_ID_ZASOB_BASE`";
+		$sqlList['InstallFunction'] = <<<SQL_FUNCTION
+CREATE DEFINER=`root`@`localhost`
+	FUNCTION `FIND_ID_ZASOB_BASE`(`ID_ZASOB` INT) RETURNS text CHARSET latin2
+		READS SQL DATA
+BEGIN
+	SET @ID_ZASOB_BASE = `ID_ZASOB`;
+	SET @LOOP_LIMIT = 10;
+	REPEAT
+		SELECT `ALIAS_ID`
+			INTO @ALIAS_ID
+			FROM `CRM_LISTA_ZASOBOW`
+			WHERE `ID`=@ID_ZASOB_BASE;
+
+		IF @ALIAS_ID IS NOT NULL AND @ALIAS_ID > 0 THEN
+			SET @ID_ZASOB_BASE = @ALIAS_ID;
+		END IF;
+
+		SET @LOOP_LIMIT = @LOOP_LIMIT - 1;
+	UNTIL @LOOP_LIMIT <= 0 END REPEAT;
+	RETURN @ID_ZASOB_BASE;
+END
+SQL_FUNCTION;
+		$db = DB::getDB();
+		if ($db->has_errors()) {
+			throw new Exception("DB Errors: " . implode("\n<br>", $db->get_errors()));
+		}
+		foreach ($sqlList as $sqlName => $sql) {
+			$res = $db->query($sql);
+			if ($db->has_errors()) {
+				throw new Exception("DB Errors at sql '{$sqlName}': " . implode("\n<br>", $db->get_errors()));
+			}
+		}
+	}
+
+	/*
+		Table 'DB.P5-MSG:Route_FixZasobPath:WARNING: Update all paths' doesn't exist
+		Table 'DB.P5-MSG:Route_FixZasobPath:ERROR: Loop detected ID=P_ID' doesn't exist
+		Table 'DB.P5-MSG:Route_FixZasobPath:ERROR: Parent item not exists' doesn't exist
+		Table 'DB.P5-MSG:Route_FixZasobPath:ERROR: Loop detected in path' doesn't exist
+	 */
+	public function parseMessageFromStorage($msg) {
+		switch ($msg) {
+			case 'WARNING: Update all paths': {
+				$msg = "Zaktualizuj ścieżki zasobów!";
+				break;
+			}
+			case 'ERROR: Loop detected ID=P_ID': {
+				$msg = "Nr rekordu nadrzędnego musi różnić się od nr rekordu";
+				break;
+			}
+			case 'ERROR: Parent item not exists': {
+				$msg = "Nie istnieje rekord o numerze podanym jako nr nadrzędny";
+				break;
+			}
+			case 'ERROR: Loop detected in path': {
+				$msg = "Nieprawidłowy nr nadrzędny";
+				break;
+			}
+		}
+		return $msg;
+	}
+
+	public function parseMessageFromMsgsSystem($msg) {
+		switch ($msg) {
+			case 'Update all paths': {
+				$msg = "Zaktualizuj ścieżki zasobów";
+				break;
+			}
+		}
+		return $msg;
+	}
+
+	public function runByMessageFromMsgsSystem($msg, &$execNotes) {
+		switch ($msg) {
+			case 'Update all paths': {
+				$execNotes .= 'call procedure... ';
+				$this->_callProcedure();
+				$execNotes .= ' done';
+				break;
+			}
+		}
+	}
+
+}