1
0
mirror of https://github.com/typemill/typemill.git synced 2025-07-26 00:31:38 +02:00

Delete content folder and check for published pages

This commit is contained in:
trendschau
2023-08-10 20:37:45 +02:00
parent 2274ab42f7
commit 6c01a3a96e
7 changed files with 145 additions and 81 deletions

View File

@@ -1,2 +0,0 @@
meta:
navtitle: folder

File diff suppressed because one or more lines are too long

View File

@@ -70,9 +70,3 @@
noindex: false
path: /01-cyanine-theme/03-content-elements.md
keyPath: '1.3'
/folder:
navtitle: folder
hide: false
noindex: false
path: /02-folder
keyPath: 2

File diff suppressed because one or more lines are too long

View File

@@ -855,22 +855,26 @@ class ControllerApiAuthorArticle extends Controller
return $response->withHeader('Content-Type', 'application/json')->withStatus(404);
}
$content = new Content($urlinfo['baseurl']);
# check if it is a folder and if the folder has published pages.
if($item->elementType == 'folder')
{
if($this->folderHasPublishedPages($item))
{
$response->getBody()->write(json_encode([
'message' => 'The folder contains published pages. Please unpublish or delete the pages first.',
]));
return $response->withHeader('Content-Type', 'application/json')->withStatus(422);
}
$result = $content->deleteFolder($item);
}
else
{
$result = $content->deletePage($item);
}
# publish content
$content = new Content($urlinfo['baseurl']);
$content->deletePage($item);
if($result !== true)
{
$response->getBody()->write(json_encode([
'message' => $result,
]));
return $response->withHeader('Content-Type', 'application/json')->withStatus(422);
}
# refresh navigation
$navigation->clearNavigation();
@@ -897,21 +901,4 @@ class ControllerApiAuthorArticle extends Controller
return $response->withHeader('Content-Type', 'application/json');
}
private function folderHasPublishedPages($folder)
{
if(isset($folder->folderContent) && is_array($folder->folderContent) && count($folder->folderContent) > 0 )
{
foreach($folder->folderContent as $page)
{
if($page->status == 'published')
{
return true;
}
}
}
return false;
}
}

View File

@@ -113,20 +113,10 @@ class Content
return $this->storage->getError();
}
public function deletePage($item, $result = NULL)
public function deletePage($item)
{
$extensions = ['.md', '.txt', '.yaml'];
if($item->elementType == 'folder' && isset($item->folderContent) && is_array($item->folderContent))
{
foreach($item->folderContent as $content)
{
$result = $this->deletePage($content);
if($result !== true){ break; }
}
}
foreach($extensions as $extension)
{
$result = $this->storage->deleteFile('contentFolder', '', $item->pathWithoutType . $extension);
@@ -140,6 +130,73 @@ class Content
return true;
}
public function deleteFolder($item, $result = true)
{
if($item->elementType == 'folder' && isset($item->folderContent) && is_array($item->folderContent) && !empty($item->folderContent))
{
if($this->hasPublishedItems($item))
{
return 'The folder contains published pages. Please unpublish or delete them first.';
}
foreach($item->folderContent as $subitem)
{
if($subitem->elementType == 'folder')
{
$result = $this->deleteFolder($subitem);
}
else
{
$result = $this->deletePage($subitem);
}
if($result !== true)
{
return $result;
}
}
$result = $this->deletePage($item);
if($result !== true)
{
return $result;
}
$result = $this->storage->deleteContentFolder($item->path);
if($result !== true)
{
return $this->storage->getError();
}
}
return true;
}
private function hasPublishedItems($folder, $published = false)
{
$published = false;
if(isset($folder->folderContent) && is_array($folder->folderContent) && !empty($folder->folderContent))
{
foreach($folder->folderContent as $item)
{
if($item->status == 'published')
{
return true;
}
if($item->elementType == 'folder')
{
$published = $this->hasPublishedItems($item);
}
}
}
return $published;
}
public function addDraftHtml($markdownArray)
{
$content = [];

View File

@@ -156,6 +156,55 @@ class Storage
return true;
}
public function deleteFolder($location, $folder, $filename)
{
if(!isset($this->isWritable[$location]))
{
$this->error = "It is not allowed to write into $location";
return false;
}
$filepath = $this->getFolderPath($location, $folder) . $filename;
if(is_dir($filepath))
{
if(rmdir($dir))
{
return true;
}
$this->error = "We found the folder but could not delete $filepath";
return false;
}
$this->error = "The path $filepath is not a folder.";
return false;
}
public function deleteContentFolder($filepath)
{
$filepath = $this->getFolderPath('contentFolder') . $filepath;
if(is_dir($filepath))
{
if(rmdir($filepath))
{
return true;
}
$this->error = "We found the folder but could not delete $filepath";
return false;
}
$this->error = "The path $filepath is not a folder.";
return false;
}
public function checkFile($location, $folder, $filename)
{
$filepath = $this->getFolderPath($location, $folder) . $filename;
@@ -285,34 +334,6 @@ class Storage
return true;
}
public function deleteFolder($location, $folder, $filename)
{
if(!isset($this->isWritable[$location]))
{
$this->error = "It is not allowed to write into $location";
return false;
}
$filepath = $this->getFolderPath($location, $folder) . $filename;
if(is_dir($filepath))
{
if(rmdir($dir))
{
return true;
}
$this->error = "We found the folder but could not delete $filepath";
return false;
}
$this->error = "The path $filepath is not a folder.";
return false;
}
public function deleteFile($location, $folder, $filename)
{
if(!isset($this->isWritable[$location]))
@@ -332,9 +353,14 @@ class Storage
}
$this->error = "We found the file but could not delete $filepath";
return false;
}
return false;
$this->error = "We did not find a file with that name";
# we do not want to stop delete operation just because a file was not there, so return a message and true.
return true;
}
# used to sort the navigation / files
@@ -351,11 +377,14 @@ class Storage
{
$oldPath = $this->contentFolder . $item->path;
if(@rename($oldPath, $newPath))
if(is_dir($oldPath))
{
return true;
if(@rename($oldPath, $newPath))
{
return true;
}
return false;
}
return false;
}
# create old path but without filetype