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

FEAT #16063 TIME 2:50 WIP Tiles creation

parent b8d9010f
No related branches found
No related tags found
No related merge requests found
......@@ -43,3 +43,16 @@ DO $$ BEGIN
ALTER TABLE entities RENAME COLUMN country TO address_country;
END IF;
END$$;
DROP TABLE IF EXISTS tiles;
CREATE TABLE tiles
(
id SERIAL NOT NULL,
user_id INTEGER NOT NULL,
type text NOT NULL,
view text NOT NULL,
position INTEGER NOT NULL,
parameters jsonb DEFAULT '{}' NOT NULL,
CONSTRAINT tiles_pkey PRIMARY KEY (id)
)
WITH (OIDS=FALSE);
......@@ -525,6 +525,10 @@ $app->post('/templates/{id}/duplicate', \Template\controllers\TemplateController
$app->get('/administration/templates/new', \Template\controllers\TemplateController::class . ':initTemplates');
$app->post('/templates/{id}/mergeEmail', \Template\controllers\TemplateController::class . ':mergeEmailTemplate');
//Tiles
$app->get('/tiles', \Home\controllers\TileController::class . ':get');
$app->post('/tiles', \Home\controllers\TileController::class . ':create');
//Users
$app->put('/users/export', \User\controllers\UserController::class . ':getExport');
$app->put('/users/import', \User\controllers\UserController::class . ':setImport');
......
......@@ -1512,3 +1512,15 @@ CREATE TABLE attachment_types
CONSTRAINT attachment_types_unique_key UNIQUE (type_id)
)
WITH (OIDS=FALSE);
CREATE TABLE tiles
(
id SERIAL NOT NULL,
user_id INTEGER NOT NULL,
type text NOT NULL,
view text NOT NULL,
position INTEGER NOT NULL,
parameters jsonb DEFAULT '{}' NOT NULL,
CONSTRAINT tiles_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 Tile Controller
* @author dev@maarch.org
*/
namespace Home\controllers;
use History\controllers\HistoryController;
use Home\models\TileModel;
use Respect\Validation\Validator;
use Slim\Http\Request;
use Slim\Http\Response;
class TileController
{
const TYPES = ['lastViewMails', 'basket', 'savedQuery', 'followedMails', 'folder', 'externalSignatureBook', 'shortcut'];
const VIEWS = ['list', 'resume', 'chart'];
public function get(Request $request, Response $response)
{
$tiles = TileModel::get([
'select' => ['*'],
'where' => ['user_id = ?'],
'data' => [$GLOBALS['id']]
]);
return $response->withJson(['tiles' => $tiles]);
}
public function create(Request $request, Response $response)
{
$body = $request->getParsedBody();
if (empty($body)) {
return $response->withStatus(400)->withJson(['errors' => 'Body is empty']);
} elseif (!Validator::stringType()->notEmpty()->validate($body['type'] ?? null) || !in_array($body['type'], TileController::TYPES)) {
return $response->withStatus(400)->withJson(['errors' => 'Body type is empty, not a string or not valid']);
} elseif (!Validator::stringType()->notEmpty()->validate($body['view'] ?? null) || !in_array($body['view'], TileController::VIEWS)) {
return $response->withStatus(400)->withJson(['errors' => 'Body view is empty, not a string or not valid']);
} elseif (!Validator::intVal()->notEmpty()->validate($body['position'] ?? null)) {
return $response->withStatus(400)->withJson(['errors' => 'Body position is empty or not an integer']);
}
$tiles = TileModel::get([
'select' => [1],
'where' => ['user_id = ?'],
'data' => [$GLOBALS['id']]
]);
if (count($tiles) >= 6) {
return $response->withStatus(400)->withJson(['errors' => 'Too many tiles (limited to 6)']);
}
$id = TileModel::create([
'user_id' => $GLOBALS['id'],
'type' => $body['type'],
'view' => $body['view'],
'position' => $body['position'],
'parameters' => empty($body['parameters']) ? '{}' : json_encode($body['parameters'])
]);
HistoryController::add([
'tableName' => 'tiles',
'recordId' => $GLOBALS['id'],
'eventType' => 'ADD',
'eventId' => 'tileCreation',
'info' => 'tile creation'
]);
return $response->withJson(['id' => $id]);
}
}
<?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 Tile Model
* @author dev@maarch.org
*/
namespace Home\models;
use SrcCore\models\ValidatorModel;
use SrcCore\models\DatabaseModel;
class TileModel
{
public static function get(array $args = [])
{
ValidatorModel::arrayType($args, ['select', 'where', 'data', 'orderBy']);
ValidatorModel::intType($args, ['limit']);
$tiles = DatabaseModel::select([
'select' => empty($args['select']) ? ['*'] : $args['select'],
'table' => ['tiles'],
'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 $tiles;
}
public static function getById(array $args)
{
ValidatorModel::notEmpty($args, ['id']);
ValidatorModel::intVal($args, ['id']);
ValidatorModel::arrayType($args, ['select']);
$tile = DatabaseModel::select([
'select' => empty($args['select']) ? ['*'] : $args['select'],
'table' => ['tiles'],
'where' => ['id = ?'],
'data' => [$args['id']],
]);
if (empty($tile[0])) {
return [];
}
return $tile[0];
}
public static function create(array $args)
{
ValidatorModel::notEmpty($args, ['user_id', 'type', 'view', 'position']);
ValidatorModel::stringType($args, ['type', 'view', 'parameters']);
ValidatorModel::intVal($args, ['user_id', 'position']);
$nextSequenceId = DatabaseModel::getNextSequenceValue(['sequenceId' => 'tiles_id_seq']);
DatabaseModel::insert([
'table' => 'tiles',
'columnsValues' => [
'id' => $nextSequenceId,
'user_id' => $args['user_id'],
'type' => $args['type'],
'view' => $args['view'],
'position' => $args['position'],
'parameters' => $args['parameters'],
]
]);
return $nextSequenceId;
}
public static function update(array $args)
{
ValidatorModel::notEmpty($args, ['where']);
ValidatorModel::arrayType($args, ['set', 'where', 'data']);
DatabaseModel::update([
'table' => 'tiles',
'set' => empty($args['set']) ? [] : $args['set'],
'where' => $args['where'],
'data' => empty($args['data']) ? [] : $args['data']
]);
return true;
}
public static function delete(array $args)
{
ValidatorModel::notEmpty($args, ['where', 'data']);
ValidatorModel::arrayType($args, ['where', 'data']);
DatabaseModel::delete([
'table' => 'tiles',
'where' => $args['where'],
'data' => $args['data']
]);
return true;
}
}
......@@ -402,7 +402,7 @@ class EntityControllerTest extends TestCase
$this->assertSame('rue du parc des princes', $responseBody['entity']['addressStreet']);
$this->assertSame('75016', $responseBody['entity']['addressPostcode']);
$this->assertSame('PARIS', $responseBody['entity']['addressTown']);
$this->assertSame(null, $responseBody['entity']['parent_entity_id']);
$this->assertSame('COU', $responseBody['entity']['parent_entity_id']);
$this->assertIsArray($responseBody['entity']['listTemplate']);
$this->assertNotEmpty($responseBody['entity']['listTemplate']);
......
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