Skip to content
Snippets Groups Projects
Verified Commit a760e3c8 authored by Damien's avatar Damien
Browse files

FEAT #10118 TIME 3:20 WIP Attachment types

parent 1161b726
No related branches found
No related tags found
No related merge requests found
......@@ -93,7 +93,16 @@ $app->put('/attachments/{id}/sign', \SignatureBook\controllers\SignatureBookCont
$app->put('/attachments/{id}/unsign', \SignatureBook\controllers\SignatureBookController::class . ':unsignAttachment');
$app->post('/attachments/{id}/mailing', \Attachment\controllers\AttachmentController::class . ':getMailingById');
$app->get('/attachmentsInformations', \Attachment\controllers\AttachmentController::class . ':getByChrono');
//AttachmentsTypes
$app->get('/attachmentsTypes', \Attachment\controllers\AttachmentController::class . ':getAttachmentsTypes');
//$app->get('/attachmentsTypes', \Attachment\controllers\AttachmentTypeController::class . ':get');
$app->post('/attachmentsTypes', \Attachment\controllers\AttachmentTypeController::class . ':create');
$app->put('/attachmentsTypes/{id}', \Attachment\controllers\AttachmentTypeController::class . ':update');
$app->delete('/attachmentsTypes/{id}', \Attachment\controllers\AttachmentTypeController::class . ':delete');
//AutoComplete
$app->get('/autocomplete/users', \SrcCore\controllers\AutoCompleteController::class . ':getUsers');
......
......@@ -1492,3 +1492,18 @@ CREATE TABLE IF NOT EXISTS registered_mail_resources (
CONSTRAINT registered_mail_resources_pkey PRIMARY KEY (id),
CONSTRAINT registered_mail_resources_unique_key UNIQUE (res_id)
);
CREATE TABLE attachment_types
(
id SERIAL NOT NULL,
type_id text NOT NULL,
label text NOT NULL,
visible BOOLEAN NOT NULL,
email_link BOOLEAN NOT NULL,
signable BOOLEAN NOT NULL,
icon text,
version_enabled BOOLEAN NOT NULL,
new_version_default BOOLEAN NOT NULL,
CONSTRAINT attachment_types_pkey PRIMARY KEY (id)
)
WITH (OIDS=FALSE);
<?php
/**
* Copyright Maarch since 2008 under licence GPLv3.
* See LICENCE.txt file at the root folder for more details.
* This file is part of Maarch software.
*
*/
/**
* @brief Attachment Type Controller
* @author dev@maarch.org
*/
namespace Attachment\controllers;
use Attachment\models\AttachmentTypeModel;
use Group\controllers\PrivilegeController;
use Respect\Validation\Validator;
use Slim\Http\Request;
use Slim\Http\Response;
class AttachmentTypeController
{
public function get(Request $request, Response $response)
{
$attachmentsTypes = AttachmentTypeModel::get(['select' => ['*']]);
return $response->withJson(['attachmentsTypes' => $attachmentsTypes]);
}
public function create(Request $request, Response $response)
{
if (!PrivilegeController::hasPrivilege(['privilegeId' => 'admin_attachments', 'userId' => $GLOBALS['id']])) {
return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']);
}
$body = $request->getParsedBody();
if (empty($body)) {
return $response->withStatus(400)->withJson(['errors' => 'Body is not set or empty']);
} elseif (!Validator::stringType()->notEmpty()->validate($body['typeId'])) {
return $response->withStatus(400)->withJson(['errors' => 'Body typeId is empty or not a string']);
} elseif (!Validator::stringType()->notEmpty()->validate($body['label'])) {
return $response->withStatus(400)->withJson(['errors' => 'Body label is empty or not a string']);
}
$id = AttachmentTypeModel::create([
'type_id' => $body['type_id'],
'label' => $body['label'],
'visible' => empty($body['visible']) ? 'false' : 'true',
'email_link' => empty($body['emailLink']) ? 'false' : 'true',
'signable' => empty($body['signable']) ? 'false' : 'true',
'icon' => $body['icon'] ?? null,
'version_enabled' => empty($body['versionEnabled']) ? 'false' : 'true',
'new_version_default' => empty($body['newVersionDefault']) ? 'false' : 'true'
]);
return $response->withJson(['id' => $id]);
}
public function update(Request $request, Response $response, array $args)
{
if (!PrivilegeController::hasPrivilege(['privilegeId' => 'admin_attachments', 'userId' => $GLOBALS['id']])) {
return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']);
}
$body = $request->getParsedBody();
if (empty($body)) {
return $response->withStatus(400)->withJson(['errors' => 'Body is not set or empty']);
} elseif (!Validator::stringType()->notEmpty()->validate($body['label'])) {
return $response->withStatus(400)->withJson(['errors' => 'Body label is empty or not a string']);
}
$attachmentType = AttachmentTypeModel::getById(['select' => 1, 'id' => $args['id']]);
if (empty($attachmentType)) {
return $response->withStatus(400)->withJson(['errors' => 'Attachment type does not exist']);
}
$set = ['label' => $body['label']];
if (isset($body['visible'])) {
$set['visible'] = empty($body['visible']) ? 'false' : 'true';
}
if (isset($body['emailLink'])) {
$set['email_link'] = empty($body['emailLink']) ? 'false' : 'true';
}
if (isset($body['signable'])) {
$set['signable'] = empty($body['signable']) ? 'false' : 'true';
}
if (isset($body['versionEnabled'])) {
$set['version_enabled'] = empty($body['versionEnabled']) ? 'false' : 'true';
}
if (isset($body['newVersionDefault'])) {
$set['new_version_default'] = empty($body['newVersionDefault']) ? 'false' : 'true';
}
if (isset($body['icon'])) {
$set['icon'] = $body['icon'];
}
AttachmentTypeModel::update([
'set' => $set,
'where' => ['id = ?'],
'data' => [$args['id']],
]);
return $response->withStatus(204);
}
public function delete(Request $request, Response $response, array $args)
{
if (!PrivilegeController::hasPrivilege(['privilegeId' => 'admin_attachments', 'userId' => $GLOBALS['id']])) {
return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']);
}
//TODO que faire quand on supprime
$attachmentType = AttachmentTypeModel::getById(['select' => 1, 'id' => $args['id']]);
if (empty($attachmentType)) {
return $response->withStatus(400)->withJson(['errors' => 'Attachment type does not exist']);
}
AttachmentTypeModel::delete([
'where' => ['id = ?'],
'data' => [$args['id']],
]);
return $response->withStatus(204);
}
}
<?php
/**
* Copyright Maarch since 2008 under licence GPLv3.
* See LICENCE.txt file at the root folder for more details.
* This file is part of Maarch software.
*
*/
/**
* @brief Attachment Type Model
* @author dev@maarch.org
*/
namespace Attachment\models;
use SrcCore\models\DatabaseModel;
use SrcCore\models\ValidatorModel;
class AttachmentTypeModel
{
public static function get(array $args)
{
ValidatorModel::notEmpty($args, ['select']);
ValidatorModel::arrayType($args, ['select', 'where', 'data', 'orderBy', 'groupBy']);
ValidatorModel::intType($args, ['limit']);
$types = DatabaseModel::select([
'select' => $args['select'],
'table' => ['attachment_types'],
'where' => empty($args['where']) ? [] : $args['where'],
'data' => empty($args['data']) ? [] : $args['data'],
'order_by' => empty($args['orderBy']) ? [] : $args['orderBy'],
'groupBy' => empty($args['groupBy']) ? [] : $args['groupBy'],
'limit' => empty($args['limit']) ? 0 : $args['limit']
]);
return $types;
}
public static function getById(array $args)
{
ValidatorModel::notEmpty($args, ['id']);
ValidatorModel::intVal($args, ['id']);
ValidatorModel::arrayType($args, ['select']);
$type = DatabaseModel::select([
'select' => empty($args['select']) ? ['*'] : $args['select'],
'table' => ['attachment_types'],
'where' => ['id = ?'],
'data' => [$args['id']],
]);
if (empty($type[0])) {
return [];
}
return $type[0];
}
public static function create(array $args)
{
ValidatorModel::notEmpty($args, ['type_id', 'label', 'visible', 'email_link', 'signable', 'version_enabled', 'new_version_default']);
ValidatorModel::stringType($args, ['type_id', 'label', 'visible', 'email_link', 'signable', 'version_enabled', 'new_version_default', 'icon']);
$nextSequenceId = DatabaseModel::getNextSequenceValue(['sequenceId' => 'attachment_types_id_seq']);
DatabaseModel::insert([
'table' => 'attachment_types',
'columnsValues' => [
'id' => $nextSequenceId,
'type_id' => $args['type_id'],
'label' => $args['label'],
'visible' => $args['visible'],
'email_link' => $args['email_link'],
'signable' => $args['signable'],
'icon' => $args['icon'],
'version_enabled' => $args['version_enabled'],
'new_version_default' => $args['new_version_default'],
]
]);
return $nextSequenceId;
}
public static function update(array $args)
{
ValidatorModel::notEmpty($args, ['set', 'where', 'data']);
ValidatorModel::arrayType($args, ['set', 'where', 'data', 'postSet']);
DatabaseModel::update([
'table' => 'attachment_types',
'set' => $args['set'],
'where' => $args['where'],
'data' => $args['data']
]);
return true;
}
public static function delete(array $args)
{
ValidatorModel::notEmpty($args, ['where', 'data']);
ValidatorModel::arrayType($args, ['where', 'data']);
DatabaseModel::delete([
'table' => 'attachment_types',
'where' => $args['where'],
'data' => $args['data']
]);
return true;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment