LautaroCesso d7ccff1a5a
[DEV-26] Update length validations (#1075)
* Update length validations

* Fix language validations

* Remove unnecessary import

* Delete some semicolons
2021-11-11 17:17:39 -03:00

101 lines
3.1 KiB
PHP
Executable File

<?php
use Respect\Validation\Validator as DataValidator;
DataValidator::with('CustomValidations', true);
/**
* @api {post} /article/edit Edit article
* @apiVersion 4.10.0
*
* @apiName Edit a article
*
* @apiGroup Article
*
* @apiDescription This path edits an article.
*
* @apiPermission staff2
*
* @apiParam {Number} articleId Id of the article.
* @apiParam {Number} topicId Id of the topic of the article. Optional.
* @apiParam {String} content The new content of the article. Optional.
* @apiParam {String} name The new name of the article. Optional.
* @apiParam {Number} position The new position of the article. Optional.
* @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_TOPIC
* @apiUse INVALID_FILE
*
* @apiSuccess {Object} data Empty object
*
*/
class EditArticleController extends Controller {
const PATH = '/edit';
const METHOD = 'POST';
public function validations() {
return [
'permission' => 'staff_2',
'requestData' => [
'articleId' => [
'validation' => DataValidator::dataStoreId('article'),
'error' => ERRORS::INVALID_TOPIC
],
'name' => [
'validation' => DataValidator::oneOf(
DataValidator::notBlank()->length(LengthConfig::MIN_LENGTH_NAME, LengthConfig::MAX_LENGTH_NAME),
DataValidator::nullType()
),
'error' => ERRORS::INVALID_NAME
],
'content' => [
'validation' => DataValidator::oneOf(DataValidator::content(),DataValidator::nullType()),
'error' => ERRORS::INVALID_CONTENT
],
]
];
}
public function handler() {
$article = Article::getDataStore(Controller::request('articleId'));
if (Controller::request('topicId')) {
$newArticleTopic = Topic::getDataStore(Controller::request('topicId'));
if (!$newArticleTopic->isNull()) {
$article->topic = $newArticleTopic;
} else {
throw new RequestException(ERRORS::INVALID_TOPIC);
return;
}
}
if(Controller::request('content')) {
$fileUploader = FileUploader::getInstance();
$fileUploader->setPermission(FileManager::PERMISSION_ARTICLE);
$content = Controller::request('content', true);
$imagePaths = $this->uploadImages(true);
$article->content = $this->replaceWithImagePaths($imagePaths, $content);
}
if(Controller::request('name')) {
$article->title = Controller::request('name');
}
if(Controller::request('position')) {
$article->position = Controller::request('position');
}
$article->lastEdited = Date::getCurrentDate();
$article->store();
Log::createLog('EDIT_ARTICLE', $article->title);
Response::respondSuccess();
}
}