mirror of
https://github.com/typemill/typemill.git
synced 2025-08-11 16:44:21 +02:00
Version 1.2.0 Introducing a Basic Content Editor
This commit is contained in:
@@ -16,7 +16,7 @@ class AuthController extends Controller
|
||||
{
|
||||
if(isset($_SESSION['login']))
|
||||
{
|
||||
return $response->withRedirect($this->c->router->pathFor('settings.show'));
|
||||
return $response->withRedirect($this->c->router->pathFor('content.show'));
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -125,7 +125,7 @@ class AuthController extends Controller
|
||||
$yaml->updateYaml('settings/users', '.logins', $logins);
|
||||
}
|
||||
|
||||
return $response->withRedirect($this->c->router->pathFor('settings.show'));
|
||||
return $response->withRedirect($this->c->router->pathFor('content.show'));
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -5,10 +5,16 @@ namespace Typemill\Controllers;
|
||||
use Slim\Views\Twig;
|
||||
use Slim\Http\Request;
|
||||
use Slim\Http\Response;
|
||||
use Typemill\Models\Validation;
|
||||
use Typemill\Models\Folder;
|
||||
use Typemill\Models\Write;
|
||||
use Typemill\Models\WriteYaml;
|
||||
use Typemill\Models\WriteCache;
|
||||
use \Symfony\Component\Yaml\Yaml;
|
||||
use Typemill\Models\Helpers;
|
||||
use Typemill\Extensions\ParsedownExtension;
|
||||
use \Parsedown;
|
||||
|
||||
|
||||
class ContentController extends Controller
|
||||
{
|
||||
@@ -51,6 +57,13 @@ class ContentController extends Controller
|
||||
{
|
||||
/* 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;
|
||||
|
||||
/* if there is content (index.md), then add a marker for frontend, so ajax calls for homepage-index-urls work */
|
||||
if($contentMD)
|
||||
{
|
||||
$item = new \stdClass;
|
||||
$item->urlRel = 'is_homepage_index';
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -95,7 +108,104 @@ class ContentController extends Controller
|
||||
$title = trim($contentParts[0], "# \t\n\r\0\x0B");
|
||||
$content = trim($contentParts[1]);
|
||||
}
|
||||
|
||||
|
||||
return $this->render($response, 'content/content.twig', array('navigation' => $structure, 'title' => $title, 'content' => $content, 'item' => $item, 'settings' => $settings ));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function updateArticle(Request $request, Response $response, $args)
|
||||
{
|
||||
/* Extract the parameters from get-call */
|
||||
$params = $request->getParams();
|
||||
|
||||
/* validate input */
|
||||
$validate = new Validation();
|
||||
$vResult = $validate->editorInput($params);
|
||||
|
||||
if(is_array($vResult))
|
||||
{
|
||||
return $response->withJson(['errors' => $vResult], 422);
|
||||
}
|
||||
|
||||
/* initiate variables and objects that we need */
|
||||
$settings = $this->c->get('settings');
|
||||
$pathToContent = $settings['rootPath'] . $settings['contentFolder'];
|
||||
$uri = $request->getUri();
|
||||
$base_url = $uri->getBaseUrl();
|
||||
$write = new writeCache();
|
||||
|
||||
/* we will use the cached structure to find the url for the page-update. It acts as whitelist and is more secure than a file-path, for example. */
|
||||
$structure = $write->getCache('cache', 'structure.txt');
|
||||
|
||||
/* if there is no structure, create a fresh structure */
|
||||
if(!$structure)
|
||||
{
|
||||
$structure = $this->getFreshStructure($pathToContent, $write, $uri);
|
||||
if(!$structure)
|
||||
{
|
||||
return $response->withJson(['errors' => ['content folder is empty']], 404);
|
||||
}
|
||||
}
|
||||
|
||||
/* if it is the homepage */
|
||||
if($params['url'] == 'is_homepage_index')
|
||||
{
|
||||
$item = new \stdClass;
|
||||
$item->elementType = 'folder';
|
||||
$item->path = '';
|
||||
}
|
||||
else
|
||||
{
|
||||
/* search for the url in the structure */
|
||||
$item = Folder::getItemForUrl($structure, $params['url']);
|
||||
}
|
||||
|
||||
if(!$item)
|
||||
{
|
||||
return $response->withJson(['errors' => ['requested page-url not found']], 404);
|
||||
}
|
||||
|
||||
if($item->elementType == 'folder')
|
||||
{
|
||||
$path = $item->path . DIRECTORY_SEPARATOR . 'index.md';
|
||||
}
|
||||
elseif($item->elementType == 'file')
|
||||
{
|
||||
$path = $item->path;
|
||||
}
|
||||
|
||||
/* get the markdown file */
|
||||
$mdFile = $write->getFile($settings['contentFolder'], $path);
|
||||
if($mdFile)
|
||||
{
|
||||
/* merge title with content for complete markdown document */
|
||||
$updatedContent = '# ' . $params['title'] . "\r\n\r\n" . $params['content'];
|
||||
|
||||
/* update the file */
|
||||
$write->writeFile($settings['contentFolder'], $path, $updatedContent);
|
||||
return $response->withJson(['success'], 200);
|
||||
}
|
||||
return $response->withJson(['errors' => ['requested markdown-file not found']], 404);
|
||||
}
|
||||
|
||||
protected function getFreshStructure($pathToContent, $cache, $uri)
|
||||
{
|
||||
/* scan the content of the folder */
|
||||
$structure = Folder::scanFolder($pathToContent);
|
||||
|
||||
/* if there is no content, render an empty page */
|
||||
if(count($structure) == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/* create an array of object with the whole content of the folder */
|
||||
$structure = Folder::getFolderContentDetails($structure, $uri->getBaseUrl(), $uri->getBasePath());
|
||||
|
||||
/* cache navigation */
|
||||
$cache->updateCache('cache', 'structure.txt', 'lastCache.txt', $structure);
|
||||
|
||||
return $structure;
|
||||
}
|
||||
}
|
@@ -122,6 +122,7 @@ class PageController extends Controller
|
||||
|
||||
/* initialize parsedown */
|
||||
$parsedown = new ParsedownExtension();
|
||||
$parsedown->setSafeMode(true);
|
||||
|
||||
/* parse markdown-file to content-array */
|
||||
$contentArray = $parsedown->text($contentMD);
|
||||
|
Reference in New Issue
Block a user