Guillermo Giuliana ba457c8d51 add last files
2021-12-09 03:18:20 -03:00

97 lines
3.1 KiB
PHP
Executable File

<?php
use Respect\Validation\Validator as DataValidator;
DataValidator::with('CustomValidations', true);
/**
* @api {post} /article/add Add article
* @apiVersion 4.10.0
*
* @apiName Add article
*
* @apiGroup Article
*
* @apiDescription This path adds a new article.
*
* @apiPermission staff2
*
* @apiParam {String} title Title of the new article.
* @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.
* @apiParam {Number} images The number of images in the content
* @apiParam image_i The image file of index `i` (mutiple params accepted)
*
* @apiUse NO_PERMISSION
* @apiUse INVALID_TITLE
* @apiUse INVALID_CONTENT
* @apiUse INVALID_TOPIC
* @apiUse TITLE_ALREADY_USED
* @apiUse CONTENT_ALREADY_USED
*
* @apiSuccess {Object} data Article info
* @apiSuccess {Number} data.articleId Article id
*/
class AddArticleController extends Controller {
const PATH = '/add';
const METHOD = 'POST';
public function validations() {
return [
'permission' => 'staff_2',
'requestData' => [
'title' => [
'validation' => DataValidator::notBlank()->length(LengthConfig::MIN_LENGTH_NAME, LengthConfig::MAX_LENGTH_NAME),
'error' => ERRORS::INVALID_TITLE
],
'content' => [
'validation' => DataValidator::content(),
'error' => ERRORS::INVALID_CONTENT
],
'topicId' => [
'validation' => DataValidator::dataStoreId('topic'),
'error' => ERRORS::INVALID_TOPIC
]
]
];
}
public function handler() {
$content = Controller::request('content', true);
$title = Controller::request('title', true);
$createdArticleTookByTitle = Article::getDataStore($title,'title');
$createdArticleTookByContent = Article::getDataStore($content,'content');
if(!$createdArticleTookByTitle->isNull()){
throw new RequestException(ERRORS::TITLE_ALREADY_USED);
}
if(!$createdArticleTookByContent->isNull()){
throw new RequestException(ERRORS::CONTENT_ALREADY_USED);
}
$fileUploader = FileUploader::getInstance();
$fileUploader->setPermission(FileManager::PERMISSION_ARTICLE);
$imagePaths = $this->uploadImages(true);
$article = new Article();
$article->setProperties([
'title' => $title,
'content' => $this->replaceWithImagePaths($imagePaths, $content),
'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);
Response::respondSuccess([
'articleId' => $article->store()
]);
}
}