ch1p_io_web/handler/admin/AutoEdit.php
2023-03-04 01:46:45 +03:00

130 lines
3.7 KiB
PHP

<?php
namespace handler\admin;
use csrf;
use pages;
use posts;
use Response;
class AutoEdit extends AutoAddOrEdit {
public function get(): Response {
list($short_name, $saved) = $this->input('short_name, b:saved');
$post = posts::getPostByName($short_name);
if ($post) {
$tags = $post->getTags();
return $this->_get_postEdit($post,
title: $post->title,
text: $post->md,
tags: $post->getTags(),
visible: $post->visible,
toc: $post->toc,
short_name: $post->shortName,
saved: $saved,
);
}
$page = pages::getPageByName($short_name);
if ($page) {
return $this->_get_pageEdit($page,
title: $page->title,
text: $page->md,
saved: $saved,
visible: $page->visible,
);
}
throw new \NotFoundException();
}
public function post(): Response {
list($short_name) = $this->input('short_name');
$post = posts::getPostByName($short_name);
if ($post) {
csrf::check('editpost'.$post->id);
list($text, $title, $tags, $visible, $toc, $short_name)
= $this->input('text, title, tags, b:visible, b:toc, new_short_name');
$tags = posts::splitStringToTags($tags);
$error_code = null;
if (!$title) {
$error_code = 'no_title';
} else if (!$text) {
$error_code = 'no_text';
} else if (empty($tags)) {
$error_code = 'no_tags';
} else if (empty($short_name)) {
$error_code = 'no_short_name';
}
if ($error_code)
$this->_get_postEdit($post,
title: $title,
text: $text,
tags: $tags,
visible: $visible,
toc: $toc,
short_name: $short_name,
error_code: $error_code
);
$post->edit([
'title' => $title,
'md' => $text,
'visible' => (int)$visible,
'toc' => (int)$toc,
'short_name' => $short_name
]);
$tag_ids = posts::getTagIds($tags);
$post->setTagIds($tag_ids);
return new \RedirectResponse($post->getUrl().'edit/?saved=1');
}
$page = pages::getPageByName($short_name);
if ($page) {
csrf::check('editpage'.$page->shortName);
list($text, $title, $visible, $short_name)
= $this->input('text, title, b:visible, new_short_name');
$text = trim($text);
$title = trim($title);
$error_code = null;
if (!$title) {
$error_code = 'no_title';
} else if (!$text) {
$error_code = 'no_text';
} else if (!$short_name) {
$error_code = 'no_short_name';
}
if ($error_code) {
return $this->_get_pageEdit($page,
title: $title,
text: $text,
visible: $visible,
error_code: $error_code
);
}
$page->edit([
'title' => $title,
'md' => $text,
'visible' => (int)$visible,
'short_name' => $short_name,
]);
return new \RedirectResponse($page->getUrl().'edit/?saved=1');
}
throw new \NotFoundException();
}
}