Переглянути джерело

fixed remove file - added log msg in M_DIST_FILES

Piotr Labudda 8 роки тому
батько
коміт
3d41e51206

+ 65 - 0
SE/se-lib/Route/ViewTableAjax.php

@@ -828,4 +828,69 @@ class Route_ViewTableAjax extends RouteBase {
 			'msg' => "Wgrano nowe pliki dla '{$primaryKey}'",
 		];
 	}
+
+	public function removeFileAjaxAction() {
+		Response::sendTryCatchJson([$this, 'removeFileAjax'], $args = $_REQUEST);
+	}
+	public function removeFileAjax($args) { // ajaxFileRemove
+		$namespace = V::get('namespace', '', $args, 'word');
+		if (!$namespace) throw new Exception("Missing namespace");
+		$id = V::get('ID', 0, $args, 'int');
+		if ($id <= 0) throw new Exception("Missing ID");
+		$filename = V::get('filename', '', $args);
+		if (empty($filename)) throw new Exception("Nie wybrano pliku do usunięcia");
+		$acl = Core_AclHelper::getAclByNamespace($namespace, $forceTblAclInit = ('1' == V::get('_force', '', $_GET)));
+
+		$dbID = $acl->getDB();
+		$db = DB::getPDO($dbID);
+		if (!$db) throw new HttpException("No DB ({$dbID})", 406);
+		$record = $acl->buildQuery([])->getItem($id);
+		if (!$record) throw new HttpException("No item ID({$id})", 404);
+		if (!$acl->canReadRecord($record)) throw new Exception("Brak uprawnień do odczytu");
+		if (!$acl->canWriteRecord($record)) throw new Exception("Brak uprawnień do zapisu");
+
+		Lib::loadClass('FileUploader');
+		Lib::loadClass('FoldersConfig');
+		$tblName = $acl->getName();
+		$confTblName = "{$tblName}_COLUMN";
+		$folderConfAll = FoldersConfig::getRawData();
+		if (!FoldersConfig::hasConfig($confTblName)) throw new HttpException("Brak danych konfiguracyjnych ({$tblName})", 404);
+		$folderConf = FoldersConfig::getAll($confTblName);
+
+		$uploader = new FileUploader($confTblName, (object)$record);
+		if (!$uploader->setConfig($folderConf)) throw new HttpException("Błąd danych konfiguracyjnych ({$tblName})", 404);
+		$uploader->findFolder();
+
+		$errorMsg = '';
+		$removed = $uploader->tryRemoveFromAjax($filename, $errorMsg);
+		if (!$removed) throw new Exception($errorMsg);
+
+		// $affected = DB::getPDO($acl->getDB())->update();
+		$rootTableName = $acl->getRootTableName();
+		$pkField = $acl->getSqlPrimaryKeyField();
+		$primaryKey = $id;
+		try {
+			$affected = DB::getPDO($acl->getDB())->update($rootTableName, $pkField, $primaryKey, [
+				'M_DIST_FILES' => "Usunięto plik '{$filename}'",
+				'A_RECORD_UPDATE_AUTHOR' => User::getLogin(),
+				'A_RECORD_UPDATE_DATE' => 'NOW()',
+			]);
+			if ($affected) {
+				DB::getPDO($acl->getDB())->insert("{$rootTableName}_HIST", [
+					'ID_USERS2' => $primaryKey,
+					'M_DIST_FILES' => "Usunięto plik '{$filename}'",
+					'A_RECORD_UPDATE_AUTHOR' => User::getLogin(),
+					'A_RECORD_UPDATE_DATE' => 'NOW()',
+				]);
+			}
+		} catch (Exception $e) {
+			DBG::log($e);
+		}
+
+		return [
+			'type' => 'success',
+			'msg' => 'Plik został usunięty',
+		];
+	}
+
 }

+ 2 - 39
SE/se-lib/TableAjax.php

@@ -4236,13 +4236,6 @@ function <?php echo $jsToogleFiltrProcesuFunctionName; ?>(n) {
 				$this->sendAjaxResponseJson('ajaxFileList', $_REQUEST);
 				break;
 			}
-			case 'FILE_REMOVE': {
-				$args = array();
-				$args['ID'] = V::get('ID', '', $_REQUEST);
-				$args['filename'] = V::get('filename', '', $_POST);
-				$this->sendAjaxResponseJson('ajaxFileRemove', $args);
-				break;
-			}
 			case 'filePermsRefresh': {
 				$this->sendAjaxResponseJson('ajaxFilePermsRefresh', $_REQUEST);
 				break;
@@ -5056,37 +5049,6 @@ jQuery(document).ready(function(){
 		return $retJson;
 	}
 
-	public function ajaxFileRemove($args) {
-		$id = V::get('ID', 0, $args, 'int');
-		if ($id <= 0) throw new HttpException("404", 404);
-		$filename = V::get('filename', '', $args);
-		if (empty($filename)) throw new Exception("Nie wybrano pliku do usunięcia");
-		$dbID = $this->_acl->getDB();
-		$db = DB::getDB($dbID);
-		if (!$db) throw new HttpException("No DB ({$dbID})", 406);
-		$record = $this->_acl->buildQuery([])->getItem($id);
-		if (!$record) throw new HttpException("No item ID({$id})", 404);
-		if (!$this->_acl->canReadRecord($record)) throw new Exception("Brak uprawnień do odczytu");
-		if (!$this->_acl->canWriteRecord($record)) throw new Exception("Brak uprawnień do zapisu");
-		$tblName = $this->_acl->getName();
-		$confTblName = "{$tblName}_COLUMN";
-		$folderConfAll = FoldersConfig::getRawData();
-		if (!FoldersConfig::hasConfig($confTblName)) throw new HttpException("Brak danych konfiguracyjnych ({$tblName})", 404);
-		$folderConf = FoldersConfig::getAll($confTblName);
-
-		$uploader = new FileUploader($confTblName, (object)$record);
-		if (!$uploader->setConfig($folderConf)) throw new HttpException("Błąd danych konfiguracyjnych ({$tblName})", 404);
-		$uploader->findFolder();
-
-		$errorMsg = '';
-		$removed = $uploader->tryRemoveFromAjax($filename, $errorMsg);
-		if (!$removed) throw new Exception($errorMsg);
-		$retJson = new stdClass();
-		$retJson->type = 'SUCCESS';
-		$retJson->msg = 'Plik został poprawnie usunięty';
-		return $retJson;
-	}
-
 	public function ajaxFileList($args) {
 		$id = V::get('ID', 0, $args, 'int');
 		if ($id <= 0) throw new HttpException("404", 404);
@@ -5623,6 +5585,7 @@ jQuery(document).ready(function(){
 				]),
 		]);
 		$className = __CLASS__;
+		$namespace = $acl->getNamespace();
 		UI::inlineJS(__FILE__ . '.files.js', [
 			'UNIQ_HASH' => $this->_htmlID,
 			'FUNCTION_FILE_LIST_UPDATE_AJAX' => "fileListUpdateAjax{$this->_htmlID}", // fileListUpdateAjax
@@ -5639,7 +5602,7 @@ jQuery(document).ready(function(){
 			'NODE_ID_FILES_LIST_ACTIONS' => "FILES_LIST_ACTIONS_{$this->_htmlID}",
 			'NODE_ID_FILES_MULTIPLE_UPLOAD' => "FILES_MULTIPLE_UPLOAD_{$this->_htmlID}",
 			'URL_FILE_LIST_UPDATE_AJAX' => "index-ajax.php?_zasobID={$this->_zasobID};&_cls={$className}&_hash={$this->_htmlID}&_task=FILES_LIST&ID={$record['ID']}",
-			'URL_FILE_REMOVE_AJAX' => "index-ajax.php?_zasobID={$this->_zasobID}&_cls={$className}&_hash={$this->_htmlID}&_task=FILE_REMOVE&ID={$record['ID']}",
+			'URL_FILE_REMOVE_AJAX' => "index.php?_route=ViewTableAjax&namespace={$namespace}&_task=removeFileAjax&ID={$record['ID']}",
 			'URL_CONNECTED_TABLE_LIST' => "index-ajax.php?_zasobID={$this->_zasobID}&_cls={$className}&_hash={$this->_htmlID}&_task=FILES_CONN_TBL_LIST&ID={$record['ID']}",
 			'URL_FILE_PERMS_REFRESH' => "index-ajax.php?_zasobID={$this->_zasobID}&_cls={$className}&_hash={$this->_htmlID}&_task=filePermsRefresh&ID={$record['ID']}",
 			'URL_FILE_UPLOAD' => "index-ajax.php?_zasobID={$this->_zasobID}&_cls={$className}&_hash={$this->_htmlID}&_task=FILES_UPLOAD&ID={$record['ID']}",

+ 16 - 22
SE/se-lib/TableAjax.php.files.js

@@ -68,28 +68,22 @@ function fileListUpdate(fileListJson) { // FUNCTION_FILE_LIST_UPDATE
 					return false;
 				}
 				if (confirm('Czy jesteś pewien, że chcesz usunąć plik o nazwie ' + fname + '?')) {
-					var postData = {filename: fname};
-					jQuery.ajax({
-						url: URL_FILE_REMOVE_AJAX,
-						type: 'POST',
-						data: postData,
-						success: function(data) {
-							if (data && data.type) {
-								if (data.type == 'SUCCESS') {
-									n.parents('tr').remove();
-									//console.log('TODO: SUCCESS msg: ', data.msg);
-								}
-								else if (data.type == 'error') {
-									console.log('TODO: ERROR msg: ', data.msg);
-								}
-							} else {
-								console.log('TODO: ??? data: ', data);
-							}
-						},
-						error: function(jhr, textStatus, errorThrown) {
-							console.log('rm error: ', errorThrown, ' textStatus: ', textStatus);
-						}
-					});
+					var postData = new FormData()
+					postData.append('filename', fname)
+					window.fetch(URL_FILE_REMOVE_AJAX, {
+						method: 'POST',
+						body: postData,
+						credentials: 'same-origin',
+					}).then(function (response) {
+						return response.json()
+					}).then(function (response) {
+						p5UI__notifyAjaxCallback(response)
+						fileListUpdateAjax()
+					}).catch(function (e) {
+						p5UI__notifyAjaxCallback({ type: 'error', msg: e })
+						console.warn(e)
+						fileListUpdateAjax()
+					})
 				}
 			});
 		}