new function and controller for emptying a folder

This commit is contained in:
Uwe Steinmann 2019-07-02 08:47:12 +02:00
parent d2614ba79d
commit c9d9bf6c7b
3 changed files with 139 additions and 0 deletions

View File

@ -1038,6 +1038,12 @@ class SeedDMS_Core_Folder extends SeedDMS_Core_Object {
* Remove recursively a folder * Remove recursively a folder
* *
* Removes a folder, all its subfolders and documents * Removes a folder, all its subfolders and documents
* This method triggers the callbacks onPreRemoveFolder and onPostRemoveFolder.
* If onPreRemoveFolder returns a boolean then this method will return
* imediately with the value returned by the callback. Otherwise the
* regular removal is executed, which in turn
* triggers further onPreRemoveFolder and onPostRemoveFolder callbacks
* and its counterparts for documents (onPreRemoveDocument, onPostRemoveDocument).
* *
* @return boolean true on success, false in case of an error * @return boolean true on success, false in case of an error
*/ */
@ -1093,6 +1099,62 @@ class SeedDMS_Core_Folder extends SeedDMS_Core_Object {
return $ret; return $ret;
} /* }}} */ } /* }}} */
/**
* Empty recursively a folder
*
* Removes all subfolders and documents of a folder but not the folder itself
* This method will call remove() on all its children.
* This method triggers the callbacks onPreEmptyFolder and onPostEmptyFolder.
* If onPreEmptyFolder returns a boolean then this method will return
* imediately.
* Be aware that the recursive calls of remove() will trigger the callbacks
* onPreRemoveFolder, onPostRemoveFolder, onPreRemoveDocument and onPostRemoveDocument.
*
* @return boolean true on success, false in case of an error
*/
function empty() { /* {{{ */
/** @noinspection PhpUnusedLocalVariableInspection */
$db = $this->_dms->getDB();
/* Check if 'onPreEmptyFolder' callback is set */
if(isset($this->_dms->callbacks['onPreEmptyFolder'])) {
foreach($this->_dms->callbacks['onPreEmptyFolder'] as $callback) {
$ret = call_user_func($callback[0], $callback[1], $this);
if(is_bool($ret))
return $ret;
}
}
//Entfernen der Unterordner und Dateien
$res = $this->getSubFolders();
if (is_bool($res) && !$res) return false;
$res = $this->getDocuments();
if (is_bool($res) && !$res) return false;
foreach ($this->_subFolders as $subFolder) {
$res = $subFolder->remove();
if (!$res) {
return false;
}
}
foreach ($this->_documents as $document) {
$res = $document->remove();
if (!$res) {
return false;
}
}
/* Check if 'onPostEmptyFolder' callback is set */
if(isset($this->_dms->callbacks['onPostEmptyFolder'])) {
foreach($this->_dms->callbacks['onPostEmptyFolder'] as $callback) {
call_user_func($callback[0], $callback[1], $this);
}
}
return true;
} /* }}} */
/** /**
* Returns a list of access privileges * Returns a list of access privileges
* *

View File

@ -29,6 +29,7 @@
- add SeedDMS_Core_Folder::hasSubFolderByName() - add SeedDMS_Core_Folder::hasSubFolderByName()
- fix SeedDMS_Core_Folder::hasDocumentByName() which returned an int > 0 if documents - fix SeedDMS_Core_Folder::hasDocumentByName() which returned an int > 0 if documents
has been loaded before and even if the document searching for was not among them. has been loaded before and even if the document searching for was not among them.
- add new method SeedDMS_Core_Folder::empty()
</notes> </notes>
<contents> <contents>
<dir baseinstalldir="SeedDMS" name="/"> <dir baseinstalldir="SeedDMS" name="/">

View File

@ -0,0 +1,76 @@
<?php
/**
* Implementation of EmptyFolder controller
*
* @category DMS
* @package SeedDMS
* @license GPL 2
* @version @version@
* @author Uwe Steinmann <uwe@steinmann.cx>
* @copyright Copyright (C) 2010-2013 Uwe Steinmann
* @version Release: @package_version@
*/
/**
* Class which does the busines logic for downloading a document
*
* @category DMS
* @package SeedDMS
* @author Uwe Steinmann <uwe@steinmann.cx>
* @copyright Copyright (C) 2010-2013 Uwe Steinmann
* @version Release: @package_version@
*/
class SeedDMS_Controller_EmptyFolder extends SeedDMS_Controller_Common {
public function run() {
$dms = $this->params['dms'];
$user = $this->params['user'];
$settings = $this->params['settings'];
$folder = $this->params['folder'];
$index = $this->params['index'];
$indexconf = $this->params['indexconf'];
/* Get the document id and name before removing the document */
$foldername = $folder->getName();
$folderid = $folder->getID();
if(false === $this->callHook('preEmptyFolder')) {
if(empty($this->errormsg))
$this->errormsg = 'hook_preEmptyFolder_failed';
return null;
}
$result = $this->callHook('emptyFolder', $folder);
if($result === null) {
/* Register a callback which removes each document from the fulltext index
* The callback must return null other the removal will be canceled.
*/
function removeFromIndex($arr, $document) {
$index = $arr[0];
$indexconf = $arr[1];
$lucenesearch = new $indexconf['Search']($index);
if($hit = $lucenesearch->getDocument($document->getID())) {
$index->delete($hit->id);
$index->commit();
}
return null;
}
if($index)
$dms->setCallback('onPreEmptyDocument', 'removeFromIndex', array($index, $indexconf));
if (!$folder->empty()) {
$this->errormsg = 'error_occured';
return false;
}
} elseif($result === false) {
if(empty($this->errormsg))
$this->errormsg = 'hook_emptyFolder_failed';
return false;
}
if(!$this->callHook('postEmptyFolder')) {
}
return true;
}
}