2016-11-23 02:27:05 +01:00
|
|
|
<?php
|
|
|
|
use Respect\Validation\Validator as DataValidator;
|
|
|
|
DataValidator::with('CustomValidations', true);
|
|
|
|
|
2017-04-18 02:40:44 +02:00
|
|
|
/**
|
2017-05-12 06:58:40 +02:00
|
|
|
* @api {post} /article/edit Edit article
|
2021-10-19 03:06:20 +02:00
|
|
|
* @apiVersion 4.10.0
|
2017-04-18 02:40:44 +02:00
|
|
|
*
|
|
|
|
* @apiName Edit a article
|
|
|
|
*
|
2017-05-12 06:58:40 +02:00
|
|
|
* @apiGroup Article
|
2017-04-18 02:40:44 +02:00
|
|
|
*
|
2017-05-12 06:58:40 +02:00
|
|
|
* @apiDescription This path edits an article.
|
2017-04-18 02:40:44 +02:00
|
|
|
*
|
2017-05-12 06:58:40 +02:00
|
|
|
* @apiPermission staff2
|
2017-04-18 02:40:44 +02:00
|
|
|
*
|
2017-04-21 08:09:24 +02:00
|
|
|
* @apiParam {Number} articleId Id of the article.
|
2017-05-12 06:58:40 +02:00
|
|
|
* @apiParam {Number} topicId Id of the topic of the article. Optional.
|
|
|
|
* @apiParam {String} content The new content of the article. Optional.
|
|
|
|
* @apiParam {String} title The new title of the article. Optional.
|
|
|
|
* @apiParam {Number} position The new position of the article. Optional.
|
2018-09-20 22:19:47 +02:00
|
|
|
* @apiParam {Number} images The number of images in the content
|
|
|
|
* @apiParam image_i The image file of index `i` (mutiple params accepted)
|
2018-09-14 06:14:15 +02:00
|
|
|
*
|
2017-04-21 08:09:24 +02:00
|
|
|
* @apiUse NO_PERMISSION
|
|
|
|
* @apiUse INVALID_TOPIC
|
2018-09-20 22:19:47 +02:00
|
|
|
* @apiUse INVALID_FILE
|
2017-04-18 02:40:44 +02:00
|
|
|
*
|
2017-04-21 08:09:24 +02:00
|
|
|
* @apiSuccess {Object} data Empty object
|
2017-04-18 02:40:44 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-11-23 02:27:05 +01:00
|
|
|
class EditArticleController extends Controller {
|
|
|
|
const PATH = '/edit';
|
2017-02-08 19:09:15 +01:00
|
|
|
const METHOD = 'POST';
|
2016-11-23 02:27:05 +01:00
|
|
|
|
|
|
|
public function validations() {
|
|
|
|
return [
|
|
|
|
'permission' => 'staff_2',
|
|
|
|
'requestData' => [
|
|
|
|
'articleId' => [
|
|
|
|
'validation' => DataValidator::dataStoreId('article'),
|
|
|
|
'error' => ERRORS::INVALID_TOPIC
|
2020-01-31 18:19:48 +01:00
|
|
|
],
|
|
|
|
'title' => [
|
2020-02-05 20:43:14 +01:00
|
|
|
'validation' => DataValidator::oneOf(DataValidator::notBlank()->length(1, 200),DataValidator::nullType()),
|
2020-01-31 18:19:48 +01:00
|
|
|
'error' => ERRORS::INVALID_TITLE
|
|
|
|
],
|
|
|
|
'content' => [
|
2020-02-04 20:22:08 +01:00
|
|
|
'validation' => DataValidator::oneOf(DataValidator::content(),DataValidator::nullType()),
|
2020-01-31 18:19:48 +01:00
|
|
|
'error' => ERRORS::INVALID_CONTENT
|
|
|
|
],
|
2016-11-23 02:27:05 +01:00
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function handler() {
|
|
|
|
$article = Article::getDataStore(Controller::request('articleId'));
|
|
|
|
|
|
|
|
if (Controller::request('topicId')) {
|
|
|
|
$newArticleTopic = Topic::getDataStore(Controller::request('topicId'));
|
|
|
|
|
2016-12-05 03:51:45 +01:00
|
|
|
if (!$newArticleTopic->isNull()) {
|
2016-11-23 02:27:05 +01:00
|
|
|
$article->topic = $newArticleTopic;
|
|
|
|
} else {
|
2018-11-20 23:41:00 +01:00
|
|
|
throw new RequestException(ERRORS::INVALID_TOPIC);
|
2016-11-23 02:27:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(Controller::request('content')) {
|
2018-09-14 06:14:15 +02:00
|
|
|
$fileUploader = FileUploader::getInstance();
|
|
|
|
$fileUploader->setPermission(FileManager::PERMISSION_ARTICLE);
|
|
|
|
|
|
|
|
$content = Controller::request('content', true);
|
2018-09-20 22:19:47 +02:00
|
|
|
$imagePaths = $this->uploadImages(true);
|
2018-09-14 06:14:15 +02:00
|
|
|
|
|
|
|
$article->content = $this->replaceWithImagePaths($imagePaths, $content);
|
2016-11-23 02:27:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if(Controller::request('title')) {
|
|
|
|
$article->title = Controller::request('title');
|
|
|
|
}
|
|
|
|
|
|
|
|
if(Controller::request('position')) {
|
|
|
|
$article->position = Controller::request('position');
|
|
|
|
}
|
|
|
|
|
|
|
|
$article->lastEdited = Date::getCurrentDate();
|
|
|
|
|
|
|
|
$article->store();
|
2016-12-29 01:55:00 +01:00
|
|
|
|
|
|
|
Log::createLog('EDIT_ARTICLE', $article->title);
|
|
|
|
|
2016-11-23 02:27:05 +01:00
|
|
|
Response::respondSuccess();
|
|
|
|
}
|
2018-09-14 06:14:15 +02:00
|
|
|
}
|