baseline
This commit is contained in:
41
pages/page/copy-language().php
Normal file
41
pages/page/copy-language().php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
use MintyPHP\Router;
|
||||
use MintyPHP\Session;
|
||||
use MintyPHP\Support\Flash;
|
||||
use MintyPHP\Service\PageService;
|
||||
use MintyPHP\Http\Request;
|
||||
use MintyPHP\I18n;
|
||||
|
||||
$user = $_SESSION['user'] ?? [];
|
||||
$currentUserId = (int) ($user['id'] ?? 0);
|
||||
if ($currentUserId <= 0) {
|
||||
Flash::error(t('Login required'), Request::safeReturnTarget((string) ($_POST['return'] ?? '')), 'login_required');
|
||||
Router::redirect('login');
|
||||
}
|
||||
|
||||
if (!Session::checkCsrfToken()) {
|
||||
$return = Request::safeReturnTarget((string) ($_POST['return'] ?? ''));
|
||||
Flash::error(t('Form expired, please try again'), $return, 'page_csrf');
|
||||
Router::redirect($return !== '' ? $return : '');
|
||||
}
|
||||
|
||||
$slug = trim((string) ($_POST['slug'] ?? ''));
|
||||
$fromLocale = trim((string) ($_POST['from_locale'] ?? (I18n::$locale ?? '')));
|
||||
$toLocale = trim((string) ($_POST['to_locale'] ?? ''));
|
||||
|
||||
$return = Request::safeReturnTarget((string) ($_POST['return'] ?? ''));
|
||||
if ($return === '' && $slug !== '') {
|
||||
$return = 'page/' . $slug;
|
||||
}
|
||||
|
||||
$result = PageService::copyContentToLocale($slug, $fromLocale, $toLocale, $currentUserId);
|
||||
if (!($result['ok'] ?? false)) {
|
||||
$errors = $result['errors'] ?? [t('Page can not be updated')];
|
||||
Flash::error((string) $errors[0], $return, 'page_copy_failed');
|
||||
Router::redirect($return);
|
||||
}
|
||||
|
||||
$_SESSION['page_edit_mode'][$slug] = 1;
|
||||
Flash::success(t('Content copied'), $return, 'page_copy_ok');
|
||||
Router::redirect($return);
|
||||
96
pages/page/index($slug).php
Normal file
96
pages/page/index($slug).php
Normal file
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
use MintyPHP\Buffer;
|
||||
use MintyPHP\Router;
|
||||
use MintyPHP\Session;
|
||||
use MintyPHP\Support\Flash;
|
||||
use MintyPHP\Service\PageService;
|
||||
use MintyPHP\Http\Request;
|
||||
use MintyPHP\I18n;
|
||||
|
||||
$slug = trim((string) ($slug ?? ''));
|
||||
if ($slug === '') {
|
||||
Router::redirect('');
|
||||
}
|
||||
|
||||
$pageBundle = PageService::findBySlugWithLocale($slug, I18n::$locale ?? null);
|
||||
$page = $pageBundle['page'] ?? null;
|
||||
$pageContent = $pageBundle['content'] ?? null;
|
||||
$notFound = $page === null;
|
||||
if ($notFound) {
|
||||
http_response_code(404);
|
||||
Buffer::set('title', t('Page not found'));
|
||||
}
|
||||
|
||||
$user = $_SESSION['user'] ?? [];
|
||||
$currentUserId = (int) ($user['id'] ?? 0);
|
||||
$canEdit = $currentUserId > 0;
|
||||
$returnPath = Request::path();
|
||||
if ($returnPath === '') {
|
||||
$returnPath = 'page/' . $slug;
|
||||
}
|
||||
$requestedReturn = Request::safeReturnTarget((string) ($_POST['return'] ?? ''));
|
||||
if ($requestedReturn !== '') {
|
||||
$returnPath = $requestedReturn;
|
||||
}
|
||||
$formAction = '/' . ltrim($returnPath, '/');
|
||||
$wantsJson = Request::wantsJson();
|
||||
|
||||
$sessionEdit = false;
|
||||
if (!empty($_SESSION['page_edit_mode']) && is_array($_SESSION['page_edit_mode'])) {
|
||||
$sessionEdit = !empty($_SESSION['page_edit_mode'][$slug]);
|
||||
unset($_SESSION['page_edit_mode'][$slug]);
|
||||
}
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
if (!$canEdit || $notFound) {
|
||||
http_response_code(403);
|
||||
Flash::error(t('You are not allowed to edit this page'), $returnPath, 'page_forbidden');
|
||||
if ($wantsJson) {
|
||||
Router::json(['ok' => false, 'error' => 'forbidden', 'redirect' => $returnPath]);
|
||||
}
|
||||
Router::redirect($returnPath);
|
||||
}
|
||||
|
||||
if (!Session::checkCsrfToken()) {
|
||||
Flash::error(t('Form expired, please try again'), $returnPath, 'page_csrf');
|
||||
if ($wantsJson) {
|
||||
Router::json(['ok' => false, 'error' => 'csrf', 'redirect' => $returnPath]);
|
||||
}
|
||||
Router::redirect($returnPath);
|
||||
}
|
||||
|
||||
$content = (string) ($_POST['content'] ?? '');
|
||||
$result = PageService::updateContentBySlug($slug, $content, $currentUserId, I18n::$locale ?? null);
|
||||
|
||||
if (!($result['ok'] ?? false)) {
|
||||
$errors = $result['errors'] ?? [t('Page can not be updated')];
|
||||
Flash::error((string) $errors[0], $returnPath, 'page_update_failed');
|
||||
if ($wantsJson) {
|
||||
$_SESSION['page_edit_mode'][$slug] = 1;
|
||||
Router::json(['ok' => false, 'error' => 'invalid', 'redirect' => $returnPath]);
|
||||
}
|
||||
$_SESSION['page_edit_mode'][$slug] = 1;
|
||||
Router::redirect($returnPath);
|
||||
}
|
||||
|
||||
Flash::success(t('Page updated'), $returnPath, 'page_updated');
|
||||
if ($wantsJson) {
|
||||
$_SESSION['page_edit_mode'][$slug] = 1;
|
||||
Router::json(['ok' => true, 'redirect' => $returnPath]);
|
||||
}
|
||||
$_SESSION['page_edit_mode'][$slug] = 1;
|
||||
Router::redirect($returnPath);
|
||||
}
|
||||
|
||||
$contentJson = '';
|
||||
if (is_array($pageContent) && isset($pageContent['content'])) {
|
||||
$contentJson = (string) $pageContent['content'];
|
||||
}
|
||||
if ($contentJson === '') {
|
||||
$contentJson = json_encode(['blocks' => []], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||
}
|
||||
|
||||
$editMode = $canEdit && (($sessionEdit === true) || (($_GET['edit'] ?? '') === '1'));
|
||||
$locales = defined('APP_LOCALES') ? APP_LOCALES : [I18n::$defaultLocale];
|
||||
$currentLocale = I18n::$locale ?? (I18n::$defaultLocale ?? 'de');
|
||||
113
pages/page/index(default).phtml
Normal file
113
pages/page/index(default).phtml
Normal file
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @var array|null $page
|
||||
* @var bool $canEdit
|
||||
* @var bool $editMode
|
||||
* @var string $contentJson
|
||||
* @var string $slug
|
||||
*/
|
||||
|
||||
?>
|
||||
<div class="app-details-container app-page">
|
||||
<section>
|
||||
<?php if (!$page): ?>
|
||||
<p><?php e(t('Page not found')); ?></p>
|
||||
<?php else: ?>
|
||||
<form method="post" action="<?php e($formAction ?? ''); ?>" class="page-editor" id="page-editor" data-page-editor
|
||||
data-can-edit="<?php e($canEdit ? '1' : '0'); ?>" data-edit-mode="<?php e($editMode ? 'edit' : 'view'); ?>"
|
||||
data-placeholder="<?php e(t('Start writing...')); ?>" data-header-placeholder="<?php e(t('Heading')); ?>">
|
||||
<?php MintyPHP\Session::getCsrfInput(); ?>
|
||||
<input type="hidden" name="return" value="<?php e($returnPath ?? ''); ?>">
|
||||
<textarea name="content" id="page-content" hidden><?php e($contentJson); ?></textarea>
|
||||
<div data-editor-holder></div>
|
||||
</form>
|
||||
<?php if (file_exists('web/vendor/editorjs/tools/header.umd.js')): ?>
|
||||
<script src="<?php e(asset('vendor/editorjs/tools/header.umd.js')); ?>"></script>
|
||||
<?php endif; ?>
|
||||
<?php if (file_exists('web/vendor/editorjs/tools/list.umd.js')): ?>
|
||||
<script src="<?php e(asset('vendor/editorjs/tools/list.umd.js')); ?>"></script>
|
||||
<?php endif; ?>
|
||||
<?php if (file_exists('web/vendor/editorjs/tools/checklist.umd.js')): ?>
|
||||
<script src="<?php e(asset('vendor/editorjs/tools/checklist.umd.js')); ?>"></script>
|
||||
<?php endif; ?>
|
||||
<?php if (file_exists('web/vendor/editorjs/tools/table.umd.js')): ?>
|
||||
<script src="<?php e(asset('vendor/editorjs/tools/table.umd.js')); ?>"></script>
|
||||
<?php endif; ?>
|
||||
<?php if (file_exists('web/vendor/editorjs/tools/columns.js')): ?>
|
||||
<script src="<?php e(asset('vendor/editorjs/tools/columns.js')); ?>"></script>
|
||||
<?php endif; ?>
|
||||
<?php if (file_exists('web/vendor/editorjs/tools/marker.umd.js')): ?>
|
||||
<script src="<?php e(asset('vendor/editorjs/tools/marker.umd.js')); ?>"></script>
|
||||
<?php endif; ?>
|
||||
<script type="module" src="<?php e(asset('js/components/app-page-editor.js')); ?>"></script>
|
||||
<?php endif; ?>
|
||||
</section>
|
||||
<?php if ($canEdit): ?>
|
||||
<aside id="app-details-aside-section">
|
||||
<div class="app-details-aside-section">
|
||||
|
||||
<div class="grid">
|
||||
<button type="button" class="secondary outline" data-tooltip-pos="top"
|
||||
data-tooltip="<?php e($editMode ? t('View') : t('Edit')); ?>" data-editor-toggle
|
||||
data-edit-label="<?php e(t('Edit')); ?>" data-view-label="<?php e(t('View')); ?>">
|
||||
<?php if ($editMode): ?>
|
||||
<i class="bi bi-eye-fill" data-editor-icon aria-hidden="true"></i>
|
||||
<?php else: ?>
|
||||
<i class="bi bi-pencil-square" data-editor-icon aria-hidden="true"></i>
|
||||
<?php endif; ?>
|
||||
</button>
|
||||
<button type="submit" form="page-editor" class="primary" data-editor-save>
|
||||
<?php e(t('Save')); ?>
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<?php if (isset($locales, $currentLocale) && is_array($locales) && count($locales) > 1): ?>
|
||||
<hr>
|
||||
<details>
|
||||
<summary><?php e(t('Copy')); ?></summary>
|
||||
<hr>
|
||||
<form method="post" action="page/copy-language" class="app-page-copy">
|
||||
<?php MintyPHP\Session::getCsrfInput(); ?>
|
||||
<input type="hidden" name="slug" value="<?php e($slug ?? ''); ?>">
|
||||
<input type="hidden" name="from_locale" value="<?php e($currentLocale); ?>">
|
||||
<input type="hidden" name="return" value="<?php e($returnPath ?? ''); ?>">
|
||||
<label class="app-field">
|
||||
<span>
|
||||
<?php e(t('Copy content to')); ?>
|
||||
</span>
|
||||
<div role="group">
|
||||
<select name="to_locale" required>
|
||||
<?php foreach ($locales as $locale): ?>
|
||||
<?php if ($locale === $currentLocale) {
|
||||
continue;
|
||||
} ?>
|
||||
<option value="<?php e($locale); ?>">
|
||||
<?php
|
||||
$label = strtoupper($locale);
|
||||
if ($locale === 'de') {
|
||||
$label = t('German');
|
||||
} elseif ($locale === 'en') {
|
||||
$label = t('English');
|
||||
}
|
||||
e($label);
|
||||
?>
|
||||
</option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
<button type="submit" class="secondary outline">
|
||||
<?php e(t('Copy')); ?>
|
||||
</button>
|
||||
</div>
|
||||
</label>
|
||||
</form>
|
||||
<small>
|
||||
<?php e(t('Existing content in the target language will be overwritten')); ?>
|
||||
</small>
|
||||
</details>
|
||||
<hr>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</aside>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
Reference in New Issue
Block a user