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

version 1.3.3: Pages and Posts

This commit is contained in:
trendschau
2020-02-26 21:34:56 +01:00
parent 3524bd36d8
commit fe3fb05e41
19 changed files with 864 additions and 72 deletions

View File

@@ -13,6 +13,8 @@ use Typemill\Events\OnPagePublished;
use Typemill\Events\OnPageUnpublished;
use Typemill\Events\OnPageDeleted;
use Typemill\Events\OnPageSorted;
use \URLify;
class ContentApiController extends ContentController
{
@@ -412,13 +414,14 @@ class ContentApiController extends ContentController
return $response->withJson(array('data' => $internalStructure, 'errors' => false, 'url' => $url));
}
public function createArticle(Request $request, Response $response, $args)
public function createPost(Request $request, Response $response, $args)
{
# get params from call
$this->params = $request->getParams();
$this->uri = $request->getUri();
# url is only needed, if an active page is moved
$url = false;
@@ -426,7 +429,76 @@ class ContentApiController extends ContentController
if(!$this->setStructure($draft = true)){ return $response->withJson(array('data' => false, 'errors' => $this->errors, 'url' => $url), 404); }
# validate input
if(!$this->validateNaviItem()){ return $response->withJson(array('data' => $this->structure, 'errors' => 'Special Characters not allowed. Length between 1 and 20 chars.', 'url' => $url), 422); }
if(!$this->validateNaviItem()){ return $response->withJson(array('data' => $this->structure, 'errors' => 'Special Characters not allowed. Length between 1 and 60 chars.', 'url' => $url), 422); }
# get the ids (key path) for item, old folder and new folder
$folderKeyPath = explode('.', $this->params['folder_id']);
# get the item from structure
$folder = Folder::getItemWithKeyPath($this->structure, $folderKeyPath);
if(!$folder){ return $response->withJson(array('data' => $this->structure, 'errors' => 'We could not find this page. Please refresh and try again.', 'url' => $url), 404); }
$name = $this->params['item_name'];
$slug = URLify::filter(iconv(mb_detect_encoding($this->params['item_name'], mb_detect_order(), true), "UTF-8", $this->params['item_name']));
$namePath = date("YmdHi") . '-' . $slug;
$folderPath = 'content' . $folder->path;
$content = json_encode(['# ' . $name, 'Content']);
# initialise write object
$write = new WriteYaml();
# check, if name exists
if($write->checkFile($folderPath, $namePath . '.txt') OR $write->checkFile($folderPath, $namePath . '.md'))
{
return $response->withJson(array('data' => $this->structure, 'errors' => 'There is already a page with this name. Please choose another name.', 'url' => $url), 404);
}
if(!$write->writeFile($folderPath, $namePath . '.txt', $content))
{
return $response->withJson(array('data' => $this->structure, 'errors' => 'We could not create the file. Please refresh the page and check, if all folders and files are writable.', 'url' => $url), 404);
}
# get extended structure
$extended = $write->getYaml('cache', 'structure-extended.yaml');
# create the url for the item
$urlWoF = $folder->urlRelWoF . '/' . $slug;
# add the navigation name to the item htmlspecialchars needed for frensh language
$extended[$urlWoF] = ['hide' => false, 'navtitle' => $name];
# store the extended structure
$write->updateYaml('cache', 'structure-extended.yaml', $extended);
# update the structure for editor
$this->setStructure($draft = true, $cache = false);
$folder = Folder::getItemWithKeyPath($this->structure, $folderKeyPath);
# activate this if you want to redirect after creating the page...
# $url = $this->uri->getBaseUrl() . '/tm/content/' . $this->settings['editor'] . $folder->urlRelWoF . '/' . $slug;
return $response->withJson(array('posts' => $folder, $this->structure, 'errors' => false, 'url' => $url));
}
public function createArticle(Request $request, Response $response, $args)
{
# get params from call
$this->params = $request->getParams();
$this->uri = $request->getUri();
# url is only needed, if an active page is moved
$url = false;
# set structure
if(!$this->setStructure($draft = true)){ return $response->withJson(array('data' => false, 'errors' => $this->errors, 'url' => $url), 404); }
# validate input
if(!$this->validateNaviItem()){ return $response->withJson(array('data' => $this->structure, 'errors' => 'Special Characters not allowed. Length between 1 and 60 chars.', 'url' => $url), 422); }
# get the ids (key path) for item, old folder and new folder
$folderKeyPath = explode('.', $this->params['folder_id']);
@@ -440,16 +512,19 @@ class ContentApiController extends ContentController
# get the content of the target folder
$folderContent = $folder->folderContent;
$name = $this->params['item_name'];
$slug = URLify::filter(iconv(mb_detect_encoding($this->params['item_name'], mb_detect_order(), true), "UTF-8", $this->params['item_name']));
# create the name for the new item
$nameParts = Folder::getStringParts($this->params['item_name']);
$name = implode("-", $nameParts);
$slug = $name;
# $nameParts = Folder::getStringParts($this->params['item_name']);
# $name = implode("-", $nameParts);
# $slug = $name;
# initialize index
$index = 0;
# initialise write object
$write = new Write();
$write = new WriteYaml();
# iterate through the whole content of the new folder
$writeError = false;
@@ -472,13 +547,15 @@ class ContentApiController extends ContentController
if($writeError){ return $response->withJson(array('data' => $this->structure, 'errors' => 'Something went wrong. Please refresh the page and check, if all folders and files are writable.', 'url' => $url), 404); }
# add prefix number to the name
$namePath = $index > 9 ? $index . '-' . $name : '0' . $index . '-' . $name;
# $namePath = $index > 9 ? $index . '-' . $name : '0' . $index . '-' . $name;
$namePath = $index > 9 ? $index . '-' . $slug : '0' . $index . '-' . $slug;
$folderPath = 'content' . $folder->path;
$title = implode(" ", $nameParts);
# $title = implode(" ", $nameParts);
# create default content
$content = json_encode(['# ' . $title, 'Content']);
# $content = json_encode(['# ' . $title, 'Content']);
$content = json_encode(['# ' . $name, 'Content']);
if($this->params['type'] == 'file')
{
@@ -494,8 +571,27 @@ class ContentApiController extends ContentController
return $response->withJson(array('data' => $this->structure, 'errors' => 'We could not create the folder. Please refresh the page and check, if all folders and files are writable.', 'url' => $url), 404);
}
$write->writeFile($folderPath . DIRECTORY_SEPARATOR . $namePath, 'index.txt', $content);
# always redirect to a folder
$url = $this->uri->getBaseUrl() . '/tm/content/' . $this->settings['editor'] . $folder->urlRelWoF . '/' . $slug;
}
# get extended structure
$extended = $write->getYaml('cache', 'structure-extended.yaml');
# create the url for the item
$urlWoF = $folder->urlRelWoF . '/' . $slug;
# add the navigation name to the item htmlspecialchars needed for frensh language
$extended[$urlWoF] = ['hide' => false, 'navtitle' => $name];
# store the extended structure
$write->updateYaml('cache', 'structure-extended.yaml', $extended);
# update the structure for editor
$this->setStructure($draft = true, $cache = false);
@@ -506,7 +602,7 @@ class ContentApiController extends ContentController
}
# activate this if you want to redirect after creating the page...
# $url = $this->uri->getBaseUrl() . '/tm/content' . $folder->urlRelWoF . '/' . $name;
# $url = $this->uri->getBaseUrl() . '/tm/content/' . $this->settings['editor'] . $folder->urlRelWoF . '/' . $slug;
return $response->withJson(array('data' => $this->structure, 'errors' => false, 'url' => $url));
}
@@ -527,15 +623,18 @@ class ContentApiController extends ContentController
if(!$this->validateBaseNaviItem()){ return $response->withJson(array('data' => $this->structure, 'errors' => 'Special Characters not allowed. Length between 1 and 20 chars.', 'url' => $url), 422); }
# create the name for the new item
$nameParts = Folder::getStringParts($this->params['item_name']);
$name = implode("-", $nameParts);
$slug = $name;
# $nameParts = Folder::getStringParts($this->params['item_name']);
# $name = implode("-", $nameParts);
# $slug = $name;
$name = $this->params['item_name'];
$slug = URLify::filter(iconv(mb_detect_encoding($this->params['item_name'], mb_detect_order(), true), "UTF-8", $this->params['item_name']));
# initialize index
$index = 0;
# initialise write object
$write = new Write();
$write = new WriteYaml();
# iterate through the whole content of the new folder
$writeError = false;
@@ -558,11 +657,12 @@ class ContentApiController extends ContentController
if($writeError){ return $response->withJson(array('data' => $this->structure, 'errors' => 'Something went wrong. Please refresh the page and check, if all folders and files are writable.', 'url' => $url), 404); }
# add prefix number to the name
$namePath = $index > 9 ? $index . '-' . $name : '0' . $index . '-' . $name;
$namePath = $index > 9 ? $index . '-' . $slug : '0' . $index . '-' . $slug;
$folderPath = 'content';
# create default content
$content = json_encode(['# Add Title', 'Add Content']);
# $content = json_encode(['# Add Title', 'Add Content']);
$content = json_encode(['# ' . $name, 'Content']);
if($this->params['type'] == 'file')
{
@@ -578,8 +678,25 @@ class ContentApiController extends ContentController
return $response->withJson(array('data' => $this->structure, 'errors' => 'We could not create the folder. Please refresh the page and check, if all folders and files are writable.', 'url' => $url), 404);
}
$write->writeFile($folderPath . DIRECTORY_SEPARATOR . $namePath, 'index.txt', $content);
# activate this if you want to redirect after creating the page...
$url = $this->uri->getBaseUrl() . '/tm/content/' . $this->settings['editor'] . '/' . $slug;
}
# get extended structure
$extended = $write->getYaml('cache', 'structure-extended.yaml');
# create the url for the item
$urlWoF = '/' . $slug;
# add the navigation name to the item htmlspecialchars needed for frensh language
$extended[$urlWoF] = ['hide' => false, 'navtitle' => $name];
# store the extended structure
$write->updateYaml('cache', 'structure-extended.yaml', $extended);
# update the structure for editor
$this->setStructure($draft = true, $cache = false);
@@ -1006,8 +1123,6 @@ class ContentApiController extends ContentController
return $response->withJson(['errors' => ['message' => 'Could not write to file. Please check if the file is writable']], 404);
}
/* set safe mode to escape javascript and html in markdown */
$parsedown->setSafeMode(true);
/* parse markdown-file to content-array, if title parse title. */
if($this->params['block_id'] == 0)
@@ -1016,6 +1131,9 @@ class ContentApiController extends ContentController
}
else
{
/* set safe mode to escape javascript and html in markdown */
$parsedown->setSafeMode(true);
$blockArray = $parsedown->text($blockMarkdown);
}

View File

@@ -377,13 +377,18 @@ abstract class ContentController
{
$files = array_diff(scandir($path), array('.', '..'));
# check if there are folders first, then stop the operation
# check if there are published pages or folders inside, then stop the operation
foreach ($files as $file)
{
if(is_dir(realpath($path) . DIRECTORY_SEPARATOR . $file))
{
$this->errors = ['message' => 'Please delete the sub-folder first.'];
}
if(substr($file, -3) == '.md' )
{
$this->errors = ['message' => 'Please unpublish all pages in the folder first.'];
}
}
if(!$this->errors)

View File

@@ -18,12 +18,22 @@ class MetaApiController extends ContentController
}
# get the standard meta-definitions and the meta-definitions from plugins (same for all sites)
public function aggregateMetaDefinitions()
public function aggregateMetaDefinitions($folder = null)
{
$writeYaml = new writeYaml();
$metatabs = $writeYaml->getYaml('system' . DIRECTORY_SEPARATOR . 'author', 'metatabs.yaml');
if($folder)
{
$metatabs['meta']['fields']['contains'] = [
'type' => 'radio',
'label' => 'This folder contains:',
'options' => ['pages' => 'PAGES (sort in navigation with drag & drop)', 'posts' => 'POSTS (sorted by publish date, for news or blogs)'],
'class' => 'medium'
];
}
# loop through all plugins
foreach($this->settings['plugins'] as $name => $plugin)
{
@@ -78,8 +88,20 @@ class MetaApiController extends ContentController
$pagemeta = $writeYaml->getPageMetaDefaults($this->content, $this->settings, $this->item);
}
# get global metadefinitions
$metadefinitions = $this->aggregateMetaDefinitions();
# if item is a folder
if($this->item->elementType == "folder" && isset($this->item->contains))
{
$pagemeta['meta']['contains'] = isset($pagemeta['meta']['contains']) ? $pagemeta['meta']['contains'] : $this->item->contains;
# get global metadefinitions
$metadefinitions = $this->aggregateMetaDefinitions($folder = true);
}
else
{
# get global metadefinitions
$metadefinitions = $this->aggregateMetaDefinitions();
}
$metadata = [];
$metascheme = [];
@@ -98,7 +120,7 @@ class MetaApiController extends ContentController
# store the metascheme in cache for frontend
$writeYaml->updateYaml('cache', 'metatabs.yaml', $metascheme);
return $response->withJson(array('metadata' => $metadata, 'metadefinitions' => $metadefinitions, 'errors' => false));
return $response->withJson(array('metadata' => $metadata, 'metadefinitions' => $metadefinitions, 'item' => $this->item, 'errors' => false));
}
public function updateArticleMeta(Request $request, Response $response, $args)
@@ -117,8 +139,25 @@ class MetaApiController extends ContentController
return $response->withJson($this->errors, 404);
}
# load metadefinitions
$metaDefinitions = $this->aggregateMetaDefinitions();
# set structure
if(!$this->setStructure($draft = true)){ return $response->withJson($this->errors, 404); }
# set item
if(!$this->setItem()){ return $response->withJson($this->errors, 404); }
# if item is a folder
if($this->item->elementType == "folder")
{
$pagemeta['meta']['contains'] = isset($pagemeta['meta']['contains']) ? $pagemeta['meta']['contains'] : $this->item->contains;
# get global metadefinitions
$metaDefinitions = $this->aggregateMetaDefinitions($folder = true);
}
else
{
# get global metadefinitions
$metaDefinitions = $this->aggregateMetaDefinitions();
}
# create validation object
$validate = $this->getValidator();
@@ -147,12 +186,6 @@ class MetaApiController extends ContentController
# return validation errors
if($errors){ return $response->withJson(array('errors' => $errors),422); }
# set structure
if(!$this->setStructure($draft = true)){ return $response->withJson($this->errors, 404); }
# set item
if(!$this->setItem()){ return $response->withJson($this->errors, 404); }
$writeYaml = new writeYaml();
@@ -167,6 +200,50 @@ class MetaApiController extends ContentController
if($tab == 'meta')
{
# if manual date has been modified
if(isset($metaInput['manualdate']) && !isset($metaPage['meta']['manualdate']) OR ($metaInput['manualdate'] != $metaPage['meta']['manualdate']))
{
# update the time
$metaInput['time'] = date('H-i-s', time());
# if it is a post, then rename the post
if($this->item->elementType == "file" && strlen($this->item->order) == 12)
{
# create file-prefix with date
$datetime = $metaInput['manualdate'] . '-' . $metaInput['time'];
$datetime = implode(explode('-', $datetime));
$datetime = substr($datetime,0,12);
# create the new filename
$pathWithoutFile = str_replace($this->item->originalName, "", $this->item->path);
$newPathWithoutType = $pathWithoutFile . $datetime . '-' . $this->item->slug;
$writeYaml->renamePost($this->item->pathWithoutType, $newPathWithoutType);
# recreate the draft structure
$this->setStructure($draft = true, $cache = false);
# update item
$this->setItem();
}
}
# if folder has changed and contains pages instead of posts or posts instead of pages
if($this->item->elementType == "folder" && ($metaPage['meta']['contains'] !== $metaInput['contains']))
{
$structure = true;
if($metaInput['contains'] == "posts")
{
$writeYaml->transformPagesToPosts($this->item);
}
if($metaInput['contains'] == "pages")
{
$writeYaml->transformPostsToPages($this->item);
}
}
# normalize the meta-input
$metaInput['navtitle'] = (isset($metaInput['navtitle']) && $metaInput['navtitle'] !== null )? $metaInput['navtitle'] : '';
$metaInput['hide'] = (isset($metaInput['hide']) && $metaInput['hide'] !== null) ? $metaInput['hide'] : false;
@@ -179,9 +256,8 @@ class MetaApiController extends ContentController
$structure = true;
}
# check if navtitle or hide-value has been changed
elseif(
# check if navtitle or hide-value has been changed
($metaPage['meta']['navtitle'] != $metaInput['navtitle'])
OR
($metaPage['meta']['hide'] != $metaInput['hide'])
@@ -192,21 +268,6 @@ class MetaApiController extends ContentController
$structure = true;
}
if($structure)
{
# store the file
$writeYaml->updateYaml('cache', 'structure-extended.yaml', $extended);
# recreate the draft structure
$this->setStructure($draft = true, $cache = false);
# set item in navigation active again
$activeItem = Folder::getItemForUrl($this->structure, $this->item->urlRel, $this->uri->getBaseUrl());
# send new structure to frontend
$structure = $this->structure;
}
}
# add the new/edited metadata
@@ -215,8 +276,26 @@ class MetaApiController extends ContentController
# store the metadata
$writeYaml->updateYaml($this->settings['contentFolder'], $this->item->pathWithoutType . '.yaml', $meta);
if($structure)
{
# store the extended file
$writeYaml->updateYaml('cache', 'structure-extended.yaml', $extended);
# recreate the draft structure
$this->setStructure($draft = true, $cache = false);
# update item
$this->setItem();
# set item in navigation active again
$activeItem = Folder::getItemForUrl($this->structure, $this->item->urlRel, $this->uri->getBaseUrl());
# send new structure to frontend
$structure = $this->structure;
}
# return with the new metadata
return $response->withJson(array('metadata' => $metaInput, 'structure' => $structure, 'errors' => false));
return $response->withJson(array('metadata' => $metaInput, 'structure' => $structure, 'item' => $this->item, 'errors' => false));
}
}

View File

@@ -63,7 +63,8 @@ class PageController extends Controller
$sitemap->updateSitemap('cache', 'sitemap.xml', 'lastSitemap.txt', $structure, $uri->getBaseUrl());
/* check and update the typemill-version in the user settings */
$this->updateVersion($uri->getBaseUrl());
# this version check is not needed
# $this->updateVersion($uri->getBaseUrl());
}
}
@@ -249,7 +250,7 @@ class PageController extends Controller
/* cache structure */
$cache->updateCache('cache', 'structure.txt', 'lastCache.txt', $structure);
if($this->containsHiddenPages($extended))
if($extended && $this->containsHiddenPages($extended))
{
# generate the navigation (delete empty pages)
$navigation = $this->createNavigationFromStructure($structure);
@@ -295,6 +296,7 @@ class PageController extends Controller
return $navigation;
}
# not in use, stored the latest version in user settings, but that does not make sense because checkd on the fly with api in admin
protected function updateVersion($baseUrl)
{
/* check the latest public typemill version */

View File

@@ -136,15 +136,18 @@ class SettingsController extends Controller
/* add the preview image */
$img = getcwd() . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR . $themeName . DIRECTORY_SEPARATOR . $themeName;
$jpg = $img . '.jpg';
$png = $img . '.png';
$img = file_exists($jpg) ? $jpg : false;
if(!$img)
$image = false;
if(file_exists($img . '.jpg'))
{
$img = file_exists($png) ? $png : false;
$image = $themeName . '.jpg';
}
if(file_exists($img . '.png'))
{
$image = $themeName . '.png';
}
$themedata[$themeName]['img'] = $img;
$themedata[$themeName]['img'] = $image;
}
/* add the users for navigation */