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

Version 1.3.0: Meta-Information

This commit is contained in:
trendschau
2019-12-31 15:57:45 +01:00
parent ead51d6540
commit 7d41c4894c
85 changed files with 964 additions and 1364 deletions

View File

@@ -6,6 +6,7 @@ use Slim\Http\Request;
use Slim\Http\Response;
use Typemill\Models\Folder;
use Typemill\Models\Write;
use Typemill\Models\WriteYaml;
use Typemill\Models\ProcessImage;
use Typemill\Extensions\ParsedownExtension;
use Typemill\Events\OnPagePublished;
@@ -220,7 +221,7 @@ class ContentApiController extends ContentController
if($this->item->elementType == 'file')
{
$delete = $this->deleteContentFiles(['md','txt']);
$delete = $this->deleteContentFiles(['md','txt', 'yaml']);
}
elseif($this->item->elementType == 'folder')
{
@@ -323,13 +324,19 @@ class ContentApiController extends ContentController
$parentKeyFrom = explode('.', $this->params['parent_id_from']);
$parentKeyTo = explode('.', $this->params['parent_id_to']);
/*
echo '<pre>';
print_r(array($itemKeyPath 0,$parentKeyFrom navi,$parentKeyTo 2));
die();
*/
# get the item from structure
$item = Folder::getItemWithKeyPath($this->structure, $itemKeyPath);
if(!$item){ return $response->withJson(array('data' => $this->structure, 'errors' => 'We could not find this page. Please refresh and try again.', 'url' => $url), 404); }
# if a folder is moved on the first level
if($this->params['parent_id_from'] == 'navi')
# if an item is moved to the first level
if($this->params['parent_id_to'] == 'navi')
{
# create empty and default values so that the logic below still works
$newFolder = new \stdClass();
@@ -388,7 +395,7 @@ class ContentApiController extends ContentController
# get item for url and set it active again
if(isset($this->params['url']))
{
$activeItem = Folder::getItemForUrl($this->structure, $this->params['url']);
$activeItem = Folder::getItemForUrl($this->structure, $this->params['url'], $this->uri->getBaseUrl());
}
# keep the internal structure for response
@@ -434,7 +441,7 @@ class ContentApiController extends ContentController
$nameParts = Folder::getStringParts($this->params['item_name']);
$name = implode("-", $nameParts);
$slug = $name;
# initialize index
$index = 0;
@@ -465,8 +472,10 @@ class ContentApiController extends ContentController
$namePath = $index > 9 ? $index . '-' . $name : '0' . $index . '-' . $name;
$folderPath = 'content' . $folder->path;
$title = implode(" ", $nameParts);
# create default content
$content = json_encode(['# Add Title', 'Add Content']);
$content = json_encode(['# ' . $title, 'Content']);
if($this->params['type'] == 'file')
{
@@ -490,7 +499,7 @@ class ContentApiController extends ContentController
# get item for url and set it active again
if(isset($this->params['url']))
{
$activeItem = Folder::getItemForUrl($this->structure, $this->params['url']);
$activeItem = Folder::getItemForUrl($this->structure, $this->params['url'], $this->uri->getBaseUrl());
}
# activate this if you want to redirect after creating the page...
@@ -499,7 +508,7 @@ class ContentApiController extends ContentController
return $response->withJson(array('data' => $this->structure, 'errors' => false, 'url' => $url));
}
public function createBaseFolder(Request $request, Response $response, $args)
public function createBaseItem(Request $request, Response $response, $args)
{
# get params from call
$this->params = $request->getParams();
@@ -512,7 +521,7 @@ 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->validateBaseFolder()){ return $response->withJson(array('data' => $this->structure, 'errors' => 'Special Characters not allowed. Length between 1 and 20 chars.', 'url' => $url), 422); }
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']);
@@ -527,16 +536,16 @@ class ContentApiController extends ContentController
# iterate through the whole content of the new folder
$writeError = false;
foreach($this->structure as $folder)
foreach($this->structure as $item)
{
# check, if the same name as new item, then return an error
if($folder->slug == $slug)
if($item->slug == $slug)
{
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->moveElement($folder, '', $index))
if(!$write->moveElement($item, '', $index))
{
$writeError = true;
}
@@ -549,23 +558,32 @@ class ContentApiController extends ContentController
$namePath = $index > 9 ? $index . '-' . $name : '0' . $index . '-' . $name;
$folderPath = 'content';
if(!$write->checkPath($folderPath . DIRECTORY_SEPARATOR . $namePath))
{
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);
}
# create default content
$content = json_encode(['# Add Title', 'Add Content']);
$write->writeFile($folderPath . DIRECTORY_SEPARATOR . $namePath, 'index.txt', $content);
if($this->params['type'] == 'file')
{
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);
}
}
elseif($this->params['type'] == 'folder')
{
if(!$write->checkPath($folderPath . DIRECTORY_SEPARATOR . $namePath))
{
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);
}
# update the structure for editor
$this->setStructure($draft = true, $cache = false);
# get item for url and set it active again
if(isset($this->params['url']))
{
$activeItem = Folder::getItemForUrl($this->structure, $this->params['url']);
$activeItem = Folder::getItemForUrl($this->structure, $this->params['url'], $this->uri->getBaseUrl());
}
return $response->withJson(array('data' => $this->structure, 'errors' => false, 'url' => $url));
@@ -586,7 +604,7 @@ class ContentApiController extends ContentController
# get item for url and set it active again
if(isset($this->params['url']))
{
$activeItem = Folder::getItemForUrl($this->structure, $this->params['url']);
$activeItem = Folder::getItemForUrl($this->structure, $this->params['url'], $this->uri->getBaseUrl());
}
return $response->withJson(array('data' => $this->structure, 'homepage' => $this->homepage, 'errors' => false));

View File

@@ -92,6 +92,11 @@ abstract class ContentController
return $this->c->view->render($response->withStatus(404), '/intern404.twig', $data);
}
protected function getValidator()
{
return new Validation();
}
protected function validateEditorInput()
{
$validate = new Validation();
@@ -151,6 +156,21 @@ abstract class ContentController
}
return true;
}
protected function validateBaseNaviItem()
{
$validate = new Validation();
$vResult = $validate->navigationBaseItem($this->params);
if(is_array($vResult))
{
$message = reset($vResult);
$this->errors = ['errors' => $vResult];
if(isset($message[0])){ $this->errors['errors']['message'] = $message[0]; }
return false;
}
return true;
}
protected function setStructure($draft = false, $cache = true)
{
@@ -221,35 +241,11 @@ abstract class ContentController
protected function setItem()
{
# if it is the homepage
if($this->params['url'] == $this->uri->getBasePath() OR $this->params['url'] == '/')
{
$item = new \stdClass;
$item->elementType = 'folder';
$item->path = '';
$item->urlRel = '/';
}
else
{
# search for the url in the structure
$item = Folder::getItemForUrl($this->structure, $this->params['url']);
}
# search for the url in the structure
$item = Folder::getItemForUrl($this->structure, $this->params['url'], $this->uri->getBasePath());
if($item)
{
if($item->elementType == 'file')
{
$pathParts = explode('.', $item->path);
$fileType = array_pop($pathParts);
$pathWithoutType = implode('.', $pathParts);
$item->pathWithoutType = $pathWithoutType;
}
elseif($item->elementType == 'folder')
{
$item->pathWithoutItem = $item->path;
$item->path = $item->path . DIRECTORY_SEPARATOR . 'index';
$item->pathWithoutType = $item->path;
}
$this->item = $item;
return true;
}
@@ -315,7 +311,7 @@ abstract class ContentController
protected function deleteContentFolder()
{
$basePath = $this->settings['rootPath'] . $this->settings['contentFolder'];
$path = $basePath . $this->item->pathWithoutItem;
$path = $basePath . $this->item->path;
if(file_exists($path))
{

View File

@@ -0,0 +1,158 @@
<?php
namespace Typemill\Controllers;
use Slim\Http\Request;
use Slim\Http\Response;
use Typemill\Models\WriteYaml;
class MetaApiController extends ContentController
{
# get the standard meta-definitions and the meta-definitions from plugins (same for all sites)
public function getMetaDefinitions(Request $request, Response $response, $args)
{
$metatabs = $this->aggregateMetaDefinitions();
return $response->withJson(array('definitions' => $metatabs, 'errors' => false));
}
# get the standard meta-definitions and the meta-definitions from plugins (same for all sites)
public function aggregateMetaDefinitions()
{
$writeYaml = new writeYaml();
$metatabs = $writeYaml->getYaml('system' . DIRECTORY_SEPARATOR . 'author', 'metatabs.yaml');
# load cached metadefinitions
# check if valid
# if not, refresh cache
# loop through all plugins
foreach($this->settings['plugins'] as $name => $plugin)
{
$pluginSettings = \Typemill\Settings::getObjectSettings('plugins', $name);
if($pluginSettings && isset($pluginSettings['metatabs']))
{
$metatabs = array_merge_recursive($metatabs, $pluginSettings['metatabs']);
}
}
return $metatabs;
}
public function getArticleMetaObject(Request $request, Response $response, $args)
{
/* get params from call */
$this->params = $request->getParams();
$this->uri = $request->getUri();
# 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();
$pagemeta = $writeYaml->getPageMeta($this->settings, $this->item);
if(!$pagemeta)
{
# set the status for published and drafted
$this->setPublishStatus();
# set path
$this->setItemPath($this->item->fileType);
# read content from file
if(!$this->setContent()){ return $response->withJson(array('data' => false, 'errors' => $this->errors), 404); }
$pagemeta = $writeYaml->getPageMetaDefaults($this->content, $this->settings, $this->item);
}
# get global metadefinitions
$metadefinitions = $this->aggregateMetaDefinitions();
$metadata = [];
foreach($metadefinitions as $tabname => $tab )
{
$metadata[$tabname] = [];
foreach($tab['fields'] as $fieldname => $fielddefinitions)
{
$metadata[$tabname][$fieldname] = isset($pagemeta[$tabname][$fieldname]) ? $pagemeta[$tabname][$fieldname] : null;
}
}
return $response->withJson(array('metadata' => $metadata, 'metadefinitions' => $metadefinitions, 'errors' => false));
}
public function updateArticleMeta(Request $request, Response $response, $args)
{
/* get params from call */
$this->params = $request->getParams();
$this->uri = $request->getUri();
$tab = isset($this->params['tab']) ? $this->params['tab'] : false;
$metaData = isset($this->params['data']) ? $this->params['data'] : false ;
$objectName = 'meta';
$errors = false;
if(!$tab or !$metaData)
{
return $response->withJson($this->errors, 404);
}
# load metadefinitions
$metaDefinitions = $this->aggregateMetaDefinitions();
# create validation object
$validate = $this->getValidator();
# take the user input data and iterate over all fields and values
foreach($metaData as $fieldName => $fieldValue)
{
# get the corresponding field definition from original plugin settings */
$fieldDefinition = isset($metaDefinitions[$tab]['fields'][$fieldName]) ? $metaDefinitions[$tab]['fields'][$fieldName] : false;
if(!$fieldDefinition)
{
$errors[$tab][$fieldName] = 'This field is not defined';
}
else
{
# validate user input for this field
$result = $validate->objectField($fieldName, $fieldValue, $objectName, $fieldDefinition);
if($result !== true)
{
$errors[$tab][$fieldName] = $result[$fieldName][0];
}
}
}
# 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();
# get existing metadata for page
$meta = $writeYaml->getYaml($this->settings['contentFolder'], $this->item->pathWithoutType . '.yaml');
# add the new/edited metadata
$meta[$tab] = $metaData;
# store the metadata
$writeYaml->updateYaml($this->settings['contentFolder'], $this->item->pathWithoutType . '.yaml', $meta);
# return with the new metadata
return $response->withJson(array('metadata' => $metaData, 'errors' => false));
}
}

View File

@@ -14,6 +14,7 @@ use Typemill\Events\OnPagetreeLoaded;
use Typemill\Events\OnBreadcrumbLoaded;
use Typemill\Events\OnItemLoaded;
use Typemill\Events\OnOriginalLoaded;
use Typemill\Events\OnMetaLoaded;
use Typemill\Events\OnMarkdownLoaded;
use Typemill\Events\OnContentArrayLoaded;
use Typemill\Events\OnHtmlLoaded;
@@ -27,6 +28,7 @@ class PageController extends Controller
$structure = false;
$contentHTML = false;
$item = false;
$home = false;
$breadcrumb = false;
$description = '';
$settings = $this->c->get('settings');
@@ -34,7 +36,7 @@ class PageController extends Controller
$cache = new WriteCache();
$uri = $request->getUri();
$base_url = $uri->getBaseUrl();
try
{
/* if the cached structure is still valid, use it */
@@ -74,56 +76,68 @@ class PageController extends Controller
exit(1);
}
/* if the user is on startpage */
# if the user is on startpage
if(empty($args))
{
/* check, if there is an index-file in the root of the content folder */
$contentMD = file_exists($pathToContent . DIRECTORY_SEPARATOR . 'index.md') ? file_get_contents($pathToContent . DIRECTORY_SEPARATOR . 'index.md') : NULL;
{
$home = true;
$item = Folder::getItemForUrl($structure, $uri->getBasePath(), $uri->getBasePath());
}
else
{
/* get the request url */
$urlRel = $uri->getBasePath() . '/' . $args['params'];
$urlRel = $uri->getBasePath() . '/' . $args['params'];
/* find the url in the content-item-tree and return the item-object for the file */
$item = Folder::getItemForUrl($structure, $urlRel);
$item = Folder::getItemForUrl($structure, $urlRel, $uri->getBasePath());
/* if there is still no item, return a 404-page */
if(!$item)
{
return $this->render404($response, array( 'navigation' => $structure, 'settings' => $settings, 'base_url' => $base_url ));
}
/* get breadcrumb for page */
$breadcrumb = Folder::getBreadcrumb($structure, $item->keyPathArray);
$breadcrumb = $this->c->dispatcher->dispatch('onBreadcrumbLoaded', new OnBreadcrumbLoaded($breadcrumb))->getData();
/* add the paging to the item */
$item = Folder::getPagingForItem($structure, $item);
$item = $this->c->dispatcher->dispatch('onItemLoaded', new OnItemLoaded($item))->getData();
/* check if url is a folder. If so, check if there is an index-file in that folder */
if($item->elementType == 'folder')
{
$filePath = $pathToContent . $item->path . DIRECTORY_SEPARATOR . 'index.md';
}
elseif($item->elementType == 'file')
{
$filePath = $pathToContent . $item->path;
}
/* add the modified date for the file */
$item->modified = isset($filePath) ? filemtime($filePath) : false;
/* read the content of the file */
$contentMD = isset($filePath) ? file_get_contents($filePath) : false;
}
# dispatch the item
$item = $this->c->dispatcher->dispatch('onItemLoaded', new OnItemLoaded($item))->getData();
# set the filepath
$filePath = $pathToContent . $item->path;
# check if url is a folder and add index.md
if($item->elementType == 'folder')
{
$filePath = $filePath . DIRECTORY_SEPARATOR . 'index.md';
}
# read the content of the file
$contentMD = file_exists($filePath) ? file_get_contents($filePath) : false;
# dispatch the original content without plugin-manipulations for case anyone wants to use it
$this->c->dispatcher->dispatch('onOriginalLoaded', new OnOriginalLoaded($contentMD));
$contentMD = $this->c->dispatcher->dispatch('onMarkdownLoaded', new OnMarkdownLoaded($contentMD))->getData();
# get meta-Information
$writeYaml = new WriteYaml();
$metatabs = $writeYaml->getPageMeta($settings, $item);
if(!$metatabs)
{
$metatabs = $writeYaml->getPageMetaDefaults($contentMD, $settings, $item);
}
# dispatch meta
$metatabs = $this->c->dispatcher->dispatch('onMetaLoaded', new OnMetaLoaded($metatabs))->getData();
# dispatch content
$contentMD = $this->c->dispatcher->dispatch('onMarkdownLoaded', new OnMarkdownLoaded($contentMD))->getData();
/* initialize parsedown */
$parsedown = new ParsedownExtension();
@@ -148,18 +162,23 @@ class PageController extends Controller
$title = isset($contentParts[0]) ? strip_tags($contentParts[0]) : $settings['title'];
$contentHTML = isset($contentParts[1]) ? $contentParts[1] : $contentHTML;
/* create excerpt from content */
$excerpt = substr($contentHTML,0,500);
/* create description from excerpt */
$description = isset($excerpt) ? strip_tags($excerpt) : false;
if($description)
# if there is not meta description
if(!isset($metatabs['meta']['description']) or !$metatabs['meta']['description'])
{
$description = trim(preg_replace('/\s+/', ' ', $description));
$description = substr($description, 0, 300);
$lastSpace = strrpos($description, ' ');
$description = substr($description, 0, $lastSpace);
# create excerpt from html
$excerpt = substr($contentHTML,0,500);
# create description from excerpt
$description = isset($excerpt) ? strip_tags($excerpt) : false;
if($description)
{
$description = trim(preg_replace('/\s+/', ' ', $description));
$description = substr($description, 0, 300);
$lastSpace = strrpos($description, ' ');
$metatabs['meta']['description'] = substr($description, 0, $lastSpace);
}
}
/* get url and alt-tag for first image, if exists */
@@ -174,20 +193,18 @@ class PageController extends Controller
}
}
$home = empty($args) ? true : false;
$theme = $settings['theme'];
$route = empty($args) && isset($settings['themes'][$theme]['cover']) ? '/cover.twig' : '/index.twig';
return $this->render($response, $route, [
'home' => $home,
'navigation' => $structure,
'title' => $title,
'content' => $contentHTML,
'item' => $item,
'breadcrumb' => $breadcrumb,
'settings' => $settings,
'title' => $title,
'description' => $description,
'settings' => $settings,
'metatabs' => $metatabs,
'base_url' => $base_url,
'image' => $firstImage ]);
}