opensupports/server/controllers/article/add.php

83 lines
2.6 KiB
PHP
Raw Normal View History

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
/**
* @api {post} /article/add Add article
2022-01-04 17:24:06 +01:00
* @apiVersion 4.11.0
2017-04-18 02:40:44 +02:00
*
* @apiName Add article
2017-04-18 02:40:44 +02:00
*
* @apiGroup Article
2017-04-18 02:40:44 +02:00
*
* @apiDescription This path adds a new article.
*
* @apiPermission staff2
2017-04-18 02:40:44 +02:00
*
2021-12-02 00:00:36 +01:00
* @apiParam {String} title Title of the new article.
2017-04-21 08:09:24 +02:00
* @apiParam {String} content Content of the new article.
* @apiParam {Number} position Position of the new article.
* @apiParam {Number} topicId Id of the articles's topic.
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)
2017-04-21 08:09:24 +02:00
*
* @apiUse NO_PERMISSION
2021-12-02 00:00:36 +01:00
* @apiUse INVALID_TITLE
2017-04-21 08:09:24 +02:00
* @apiUse INVALID_CONTENT
* @apiUse INVALID_TOPIC
2017-04-18 02:40:44 +02:00
*
2017-04-22 03:33:17 +02:00
* @apiSuccess {Object} data Article info
* @apiSuccess {Number} data.articleId Article id
2017-04-18 02:40:44 +02:00
*/
2016-11-23 02:27:05 +01:00
class AddArticleController extends Controller {
const PATH = '/add';
const METHOD = 'POST';
2016-11-23 02:27:05 +01:00
public function validations() {
return [
'permission' => 'staff_2',
'requestData' => [
2021-12-02 00:00:36 +01:00
'title' => [
'validation' => DataValidator::notBlank()->length(LengthConfig::MIN_LENGTH_TITLE, LengthConfig::MAX_LENGTH_TITLE),
2021-12-02 00:00:36 +01:00
'error' => ERRORS::INVALID_TITLE
2016-11-23 02:27:05 +01:00
],
'content' => [
'validation' => DataValidator::content(),
2016-11-23 02:27:05 +01:00
'error' => ERRORS::INVALID_CONTENT
],
'topicId' => [
'validation' => DataValidator::dataStoreId('topic'),
'error' => ERRORS::INVALID_TOPIC
]
]
];
}
public function handler() {
$content = Controller::request('content', true);
$fileUploader = FileUploader::getInstance();
$fileUploader->setPermission(FileManager::PERMISSION_ARTICLE);
2018-09-20 22:19:47 +02:00
$imagePaths = $this->uploadImages(true);
2016-11-23 02:27:05 +01:00
$article = new Article();
$article->setProperties([
'title' => Controller::request('title', true),
'content' => $this->replaceWithImagePaths($imagePaths, $content),
2016-11-23 02:27:05 +01:00
'lastEdited' => Date::getCurrentDate(),
'position' => Controller::request('position') || 1
]);
$topic = Topic::getDataStore(Controller::request('topicId'));
$topic->ownArticleList->add($article);
$topic->store();
Log::createLog('ADD_ARTICLE', $article->title);
2016-11-23 02:27:05 +01:00
Response::respondSuccess([
'articleId' => $article->store()
]);
}
}