1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-23 21:33:01 +02:00

feat(core): revert multi data for Entries #212 #186

This commit is contained in:
Awilum
2019-09-07 19:44:14 +03:00
parent e94f9b193b
commit 5482f51d1a
3 changed files with 35 additions and 64 deletions

View File

@@ -13,6 +13,7 @@ use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Criteria;
use Doctrine\Common\Collections\Expr\Comparison;
use Flextype\Component\Filesystem\Filesystem;
use function array_replace_recursive;
use function ltrim;
use function md5;
use function rename;
@@ -99,14 +100,17 @@ class Entries
*/
public function fetch(string $id)
{
// Get entry file location
$entry_file = $this->_file_location($id);
// If requested entry file founded then process it
if ($_entry = $this->read($id)) {
if (Filesystem::has($entry_file)) {
// Create unique entry cache_id
// Entry Cache ID = entry + entry file + entry file time stamp
if ($timestamp = Filesystem::getTimestamp($_entry['file_path'])) {
$entry_cache_id = md5('entry' . $_entry['file_path'] . $timestamp);
if ($timestamp = Filesystem::getTimestamp($entry_file)) {
$entry_cache_id = md5('entry' . $entry_file . $timestamp);
} else {
$entry_cache_id = md5('entry' . $_entry['file_path']);
$entry_cache_id = md5('entry' . $entry_file);
}
// Try to get the requested entry from cache
@@ -125,15 +129,15 @@ class Entries
// else Try to get requested entry from the filesystem
}
$entry_decoded = $_entry['file_data'];
$entry_decoded = Parser::decode(Filesystem::read($entry_file), 'frontmatter');
// Add predefined entry items
// Entry Date
$entry_decoded['published_at'] = $entry_decoded['published_at'] ? $entry_decoded['published_at'] : Filesystem::getTimestamp($_entry['file_path']);
$entry_decoded['created_at'] = $entry_decoded['created_at'] ? $entry_decoded['created_at'] : Filesystem::getTimestamp($_entry['file_path']);
$entry_decoded['published_at'] = $entry_decoded['published_at'] ? $entry_decoded['published_at'] : Filesystem::getTimestamp($entry_file);
$entry_decoded['created_at'] = $entry_decoded['created_at'] ? $entry_decoded['created_at'] : Filesystem::getTimestamp($entry_file);
// Entry Timestamp
$entry_decoded['modified_at'] = Filesystem::getTimestamp($_entry['file_path']);
$entry_decoded['modified_at'] = Filesystem::getTimestamp($entry_file);
// Entry Slug
$entry_decoded['slug'] = $entry_decoded['slug'] ?? ltrim(rtrim($id, '/'), '/');
@@ -260,17 +264,12 @@ class Entries
// Create entries array from entries list and ignore current requested entry
//echo count($entries_list);
foreach ($entries_list as $current_entry) {
if (strpos($current_entry['path'], $bind_id . '/entry' . '.' . $current_entry['ext']) !== false) {
if (strpos($current_entry['path'], $bind_id . '/entry' . '.' . 'md') !== false) {
// ignore ...
} else {
// We are checking...
// Whether the requested entry is a director and whether the file entry is in this directory.
if ($current_entry['type'] === 'dir' && (
// @todo refactoring this
Filesystem::has($current_entry['path'] . '/entry' . '.' . Parser::$parsers['frontmatter']['ext']) ||
Filesystem::has($current_entry['path'] . '/entry' . '.' . Parser::$parsers['json']['ext']) ||
Filesystem::has($current_entry['path'] . '/entry' . '.' . Parser::$parsers['yaml']['ext'])
)) {
if ($current_entry['type'] === 'dir' && Filesystem::has($current_entry['path'] . '/entry' . '.' . 'md')) {
// Get entry uid
// 1. Remove entries path
// 2. Remove left and right slashes
@@ -371,8 +370,12 @@ class Entries
*/
public function update(string $id, array $data) : bool
{
if ($_entry = $this->read($id)) {
return Filesystem::write($_entry['file_path'], Parser::encode(array_replace_recursive($_entry['file_data'], $data), $_entry['file_parser']));
$entry_file = $this->_file_location($id);
if (Filesystem::has($entry_file)) {
$entry = Parser::decode($entry_file, 'frontmatter');
return Filesystem::write($entry_file, Parser::encode(array_replace_recursive($entry, $data), 'frontmatter'));
}
return false;
@@ -381,15 +384,14 @@ class Entries
/**
* Create entry
*
* @param string $id Entry ID
* @param array $data Data
* @param string $parser Parser - on of Parser::$parsers
* @param string $id Entry ID
* @param array $data Data
*
* @return bool True on success, false on failure.
*
* @access public
*/
public function create(string $id, array $data, string $parser = 'frontmatter') : bool
public function create(string $id, array $data) : bool
{
$entry_dir = $this->_dir_location($id);
@@ -398,11 +400,11 @@ class Entries
// Try to create directory for new entry
if (Filesystem::createDir($entry_dir)) {
// Entry file path
$entry_file = $entry_dir . '/entry' . '.' . Parser::$parsers[$parser]['ext'];
$entry_file = $entry_dir . '/entry' . '.' . 'md';
// Check if new entry file exists
if (! Filesystem::has($entry_file)) {
return Filesystem::write($entry_file, Parser::encode($data, $parser));
return Filesystem::write($entry_file, Parser::encode($data, 'frontmatter'));
}
return false;
@@ -433,7 +435,7 @@ class Entries
* @param string $new_id New entry id
* @param bool $recursive Recursive copy entries.
*
* @return bool True on success, false on failure.
* @return bool|null True on success, false on failure.
*
* @access public
*/
@@ -453,47 +455,21 @@ class Entries
*/
public function has(string $id) : bool
{
foreach (Parser::$parsers as $parser) {
if (Filesystem::has(PATH['entries'] . '/' . $id . '/' . 'entry' . '.' . $parser['ext'])) {
return true;
}
}
return false;
return Filesystem::has($this->_file_location($id));
}
/**
* Read entry
* Helper method _file_location
*
* @param string $id Entry ID
* @param bool $raw Return raw or decoded content
* @param string $id Entry ID
*
* @return bool|array Array of file path, decoded file data and file parser,
* false on failure.
* @return string entry directory location
*
* @access public
* @access private
*/
public function read(string $id, bool $raw = false)
private function _file_location(string $id) : string
{
foreach (Parser::$parsers as $parser) {
if (Filesystem::has(PATH['entries'] . '/' . $id . '/' . 'entry' . '.' . $parser['ext'])) {
$file_path = PATH['entries'] . '/' . $id . '/' . 'entry' . '.' . $parser['ext'];
if ($raw) {
$file_data = Filesystem::read($file_path);
} else {
$file_data = Parser::decode(Filesystem::read($file_path), $parser['name']);
}
return [
'file_path' => $file_path,
'file_data' => $file_data,
'file_parser' => $parser['name'],
];
}
}
return false;
return PATH['entries'] . '/' . $id . '/entry.md';
}
/**

View File

@@ -606,9 +606,6 @@ class EntriesController extends Controller
Arr::delete($entry, 'slug');
Arr::delete($entry, 'modified_at');
// Read Entry
$_entry = $this->entries->read($this->getEntryID($query), true);
// Fieldsets for current entry template
$fieldsets_path = PATH['site'] . '/fieldsets/' . (isset($entry['fieldset']) ? $entry['fieldset'] : 'default') . '.yaml';
$fieldsets = Parser::decode(Filesystem::read($fieldsets_path), 'yaml');
@@ -623,8 +620,7 @@ class EntriesController extends Controller
'i' => count($parts),
'last' => Arr::last($parts),
'id' => $this->getEntryID($query),
'data' => $_entry['file_data'],
'parser' => $_entry['file_parser'],
'data' => Parser::encode($entry, 'frontmatter'),
'type' => $type,
'menu_item' => 'entries',
'links' => [
@@ -761,7 +757,7 @@ class EntriesController extends Controller
$data['published_by'] = Session::get('uuid');
// Update entry
if ($this->entries->update($id, Parser::decode($data['data'], $data['parser']))) {
if ($this->entries->update($id, Parser::decode($data['data'], 'frontmatter'))) {
$this->flash->addMessage('success', __('admin_message_entry_changes_saved'));
} else {
$this->flash->addMessage('error', __('admin_message_entry_changes_not_saved'));

View File

@@ -5,7 +5,6 @@
{{ csrf() }}
<input type="hidden" name="action" value="save-form">
<input type="hidden" name="id" value="{{ id }}">
<input type="hidden" name="parser" value="{{ parser }}">
<div class="row">
<div class="col-12">
<div class="form-group">