opensupports/server/controllers/article/add.php

73 lines
2.1 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 a article.
*
* @apiName Add
*
* @apiGroup article
*
* @apiDescription This path adds a new article.
*
* @apiPermission Staff level 2
*
* @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.
*
* @apiUse NO_PERMISSION
* @apiUse INVALID_NAME
* @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' => [
'title' => [
2017-04-21 06:43:06 +02:00
'validation' => DataValidator::length(3, 100),
2016-11-23 02:27:05 +01:00
'error' => ERRORS::INVALID_NAME
],
'content' => [
'validation' => DataValidator::length(10),
'error' => ERRORS::INVALID_CONTENT
],
'topicId' => [
'validation' => DataValidator::dataStoreId('topic'),
'error' => ERRORS::INVALID_TOPIC
]
]
];
}
public function handler() {
$article = new Article();
$article->setProperties([
'title' => Controller::request('title'),
'content' => Controller::request('content', true),
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()
]);
}
}