Skip to content
Snippets Groups Projects
Commit 2ad0bca6 authored by Odran PHILIBERT's avatar Odran PHILIBERT
Browse files

Reports abstract class and controller with routes and guzzle client

parent 104b118b
No related branches found
No related tags found
No related merge requests found
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
}, },
"require": { "require": {
"slim/slim": "^3", "slim/slim": "^3",
"respect/validation": "^1.1" "respect/validation": "^1.1",
"guzzlehttp/guzzle": "~6.0"
} }
} }
<?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 Status Controller
* @author dev@maarch.org
* @ingroup core
*/
namespace Core\Controllers;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Respect\Validation\Validator;
use Core\Models\AdminReportsModel;
class AdminReportsController
{
public function getList(RequestInterface $request, ResponseInterface $response)
{
$obj = AdminReportsModel::getList();
$datas = [
$obj,
];
return $response->withJson($datas);
}
public function getAllGroups(RequestInterface $request, ResponseInterface $response)
{
$obj = AdminReportsModel::getAllGroups();
$datas = [
$obj,
];
return $response->withJson($obj);
}
public function getUsers (RequestInterface $request, ResponseInterface $response, $aArgs)
{
if (isset($aArgs['id']))
{
$id = $aArgs['id'];
$obj = AdminReportsModel::getUsers([
'id' => $id
]);
} else {
return $response
->withStatus(500)
->withJson(['errors' => _ID . ' ' . _IS_EMPTY]);
} $obj = AdminReportsModel::getUsers();
$datas = [
$obj,
];
return $response->withJson($datas);
}
public function getUserByGroupId(RequestInterface $request, ResponseInterface $response, $aArgs)
{
if (isset($aArgs['id'])) {
$id = $aArgs['id'];
$obj = AdminReportsModel::getUserByGroupId([
'id' => $id
]);
} else {
return $response
->withStatus(500)
->withJson(['errors' => _ID . ' ' . _IS_EMPTY]);
}
$datas = [
$obj,
];
return $response->withJson($datas);
}
public function getReportsTypesByXML(RequestInterface $request, ResponseInterface $response, $aArgs)
{
if (isset($aArgs['id'])) {
$id = $aArgs['id'];
$obj = AdminReportsModel::getReportsTypesByXML([
'id' => $id
]);
} else {
return $response
->withStatus(500)
->withJson(['errors' => _ID . ' ' . _IS_EMPTY]);
}
/* $datas = [
$obj,
];*/
return $response->withJson($obj);
}
public function update(RequestInterface $request, ResponseInterface $response, $aArgs)
{
$data = $request->getParams();
//$data = $aArgs['data'];
/*
*
*{"id":{"id" : "entity_late_mail","checked" : false },{"id" : "process_delay","checked" : false },{"id" : "folder_view_stat","checked" : false }}
A mettre dans le body du reste client, c'est ce qui sera dans getParams
*
*
*/
$id = $aArgs['id'];
$obj = AdminReportsModel::update([
'id' => $id,
'data' => $data
]);
return $response->withJson($obj);
}
/* public function getById(RequestInterface $request, ResponseInterface $response, $aArgs)
{
if (isset($aArgs['id'])) {
$id = $aArgs['id'];
$obj = StatusModel::getById([
'id' => $id
]);
} else {
return $response
->withStatus(500)
->withJson(['errors' => _ID . ' ' . _IS_EMPTY]);
}
$datas = [
$obj,
];
return $response->withJson($datas);
}
public function create(RequestInterface $request, ResponseInterface $response, $aArgs)
{
$errors = [];
$errors = $this->control($request, 'create');
if (!empty($errors)) {
return $response
->withStatus(500)
->withJson(['errors' => $errors]);
}
$aArgs = $request->getQueryParams();
$return = StatusModel::create($aArgs);
if ($return) {
$id = $aArgs['id'];
$obj = StatusModel::getById([
'id' => $id
]);
} else {
return $response
->withStatus(500)
->withJson(['errors' => _NOT_CREATE]);
}
$datas = [
$obj,
];
return $response->withJson($datas);
}
public function update(RequestInterface $request, ResponseInterface $response, $aArgs)
{
$errors = [];
$errors = $this->control($request, 'update');
if (!empty($errors)) {
return $response
->withStatus(500)
->withJson(['errors' => $errors]);
}
$aArgs = $request->getQueryParams();
$return = StatusModel::update($aArgs);
if ($return) {
$id = $aArgs['id'];
$obj = StatusModel::getById([
'id' => $id
]);
} else {
return $response
->withStatus(500)
->withJson(['errors' => _NOT_UPDATE]);
}
$datas = [
$obj,
];
return $response->withJson($datas);
}
public function delete(RequestInterface $request, ResponseInterface $response, $aArgs)
{
if (isset($aArgs['id'])) {
$id = $aArgs['id'];
$obj = StatusModel::delete([
'id' => $id
]);
} else {
return $response
->withStatus(500)
->withJson(['errors' => _NOT_DELETE]);
}
$datas = [
$obj,
];
return $response->withJson($datas);
}
protected function control($request, $mode)
{
$errors = [];
if ($mode == 'update') {
$obj = StatusModel::getById([
'id' => $request->getParam('id')
]);
if (empty($obj)) {
array_push(
$errors,
_ID . ' ' . $request->getParam('id') . ' ' . _NOT_EXISTS
);
}
}
if (!Validator::notEmpty()->validate($request->getParam('id'))) {
array_push($errors, _ID . ' ' . _IS_EMPTY);
} elseif ($mode == 'create') {
$obj = StatusModel::getById([
'id' => $request->getParam('id')
]);
if (!empty($obj)) {
array_push(
$errors,
_ID . ' ' . $obj[0]['id'] . ' ' . _ALREADY_EXISTS
);
}
}
if (!Validator::regex('/^[\w.-]*$/')->validate($request->getParam('id'))) {
array_push($errors, _ID . ' ' . _NOT . ' ' . _VALID);
}
if (!Validator::notEmpty()->validate($request->getParam('label_status'))) {
array_push($errors, _LABEL_STATUS . ' ' . _IS_EMPTY);
}
if (Validator::notEmpty()
->validate($request->getParam('is_system')) &&
!Validator::contains('Y')
->validate($request->getParam('is_system')) &&
!Validator::contains('N')
->validate($request->getParam('is_system'))
) {
array_push($errors, _IS_SYSTEM . ' ' . _NOT . ' ' . _VALID);
}
if (Validator::notEmpty()
->validate($request->getParam('is_folder_status')) &&
!Validator::contains('Y')
->validate($request->getParam('is_folder_status')) &&
!Validator::contains('N')
->validate($request->getParam('is_folder_status'))
) {
array_push($errors, _IS_FOLDER_STATUS . ' ' . _NOT . ' ' . _VALID);
}
if (Validator::notEmpty()
->validate($request->getParam('img_filename')) &&
(!Validator::regex('/^[\w-.]+$/')
->validate($request->getParam('img_filename')) ||
!Validator::length(null, 255)
->validate($request->getParam('img_filename')))
) {
array_push($errors, _IMG_FILENAME . ' ' . _NOT . ' ' . _VALID);
}
if (Validator::notEmpty()
->validate($request->getParam('maarch_module')) &&
!Validator::length(null, 255)
->validate($request->getParam('maarch_module'))
) {
array_push($errors, _MAARCH_MODULE . ' ' . _NOT . ' ' . _VALID);
}
if (Validator::notEmpty()
->validate($request->getParam('can_be_searched')) &&
!Validator::contains('Y')
->validate($request->getParam('can_be_searched')) &&
!Validator::contains('N')
->validate($request->getParam('can_be_searched'))
) {
array_push($errors, _CAN_BE_SEARCHED . ' ' . _NOT . ' ' . _VALID);
}
if (Validator::notEmpty()
->validate($request->getParam('can_be_modified')) &&
!Validator::contains('Y')
->validate($request->getParam('can_be_modified')) &&
!Validator::contains('N')
->validate($request->getParam('can_be_modified'))
) {
array_push($errors, _CAN_BE_MODIFIED . ' ' . _NOT . ' ' . _VALID);
}
return $errors;
}*/
}
<?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 Model
* @author dev@maarch.org
* @ingroup core
*/
namespace Core\Models;
class AdminReportsModel extends AdminReportsModelAbstract
{
// Do your stuff in this class
}
<?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 Status Model
* @author dev@maarch.org
* @ingroup core
*/
namespace Core\Models;
require_once 'apps/maarch_entreprise/services/Table.php';
class AdminReportsModelAbstract extends \Apps_Table_Service
{
public static function getList()
{
$aReturn = static::select([
'select' => ['*'],
'table' => ['usergroups'],
]);
return $aReturn;
}
public static function getUsers(array $aArgs = [])
{
$val = $this->group;
static::checkRequired($aArgs, ['id']);
static::checkString($aArgs, ['id']);
$aReturn = static::select([
'select' => ['*'],
'table' => ['usergroup-content'],
'where' => ['group_id = ?'],
'data' => [$val]
]);
return $aReturn;
}
public static function getAllGroups()
{
$aReturn = static::select([
'select' => ['*'],
'table' => ['usergroups'],
]);
return $aReturn;
}
public static function getReportsTypesByXML(array $aArgs = [])
{
static::checkRequired($aArgs, ['id']);
static::checkString($aArgs, ['id']);
if (file_exists('custom/' .$_SESSION['custom_override_id']. '/apps/maarch_entreprise/xml/entreprise.xml')) {
$path = 'custom/' .$_SESSION['custom_override_id']. '/apps/maarch_entreprise/xml/entreprise.xml';
} else {
$path = 'modules/reports/xml/reports.xml';
}
$xmlfile = simplexml_load_file($path);
$reportsTypes = [];
$reportsTypes = $xmlfile->REPORT;
$tab = [];
$tab_id = [];
if (count($reportsTypes) > 0) {
foreach ($reportsTypes as $value) {
if ($value->ENABLED == "true") {
$tab[] = [
'id' => (string)$value->ID,
'label' => (string)$value->LABEL,
'desc' => (string)$value->DESCRIPTION,
'url' => (string)$value->URL,
'in_menu_reports' =>(string)$value->IN_MENU_REPORTS,
'origin' => (string)$value->ORIGIN,
'module' => (string)$value->MODULE,
'module_label' => (string)$value->MODULE_LABEL,
'checked' => false
];
$tab_id[] = $value->ID; // Array containing all possible values for report_id
}
}
$aReturn = static::select([
'select' => ['*'],
'table' => ['usergroups_reports'],
'where' => ['report_id in (?)', 'group_id = ?'],
'data' => [$tab_id, $aArgs['id']]
]);
$tab_id_query = [];
foreach($aReturn as $rep ) { // First loop which stores the reports_id from the SQL query
$tab_id_query[] = $rep['report_id'];
}
/*
Loop testing each values of reports_id from the SQL query, if there is a match up then [checked] = true otherwise false
*/
foreach($tab as $rep => $value) { // Checking match up and affecting boolean
$tab[$rep]['checked'] = in_array($tab[$rep]['id'],$tab_id_query);
}
return $tab;
} else return ['error' => 'xml issue'];
}
public static function getUserByGroupId(array $aArgs = [])
{
static::checkRequired($aArgs, ['id']);
static::checkString($aArgs, ['id']);
$aReturn = static::select([
'select' => ['*'],
'table' => ['usergroup_content'],
'where' => ['group_id = ?'],
'data' => [$aArgs['id']]
]);
return $aReturn;
}
public static function create(array $aArgs = [])
{
static::checkRequired($aArgs, ['id']);
static::checkString($aArgs, ['id']);
$aReturn = static::insertInto($aArgs, 'status');
return $aReturn;
}
public static function update(array $aArgs = [])
{
static::checkRequired($aArgs, ['id']);
static::checkString($aArgs, ['id']);
$reps_by_id = [];
$reps_by_id = $aArgs['data'];
var_dump($reps_by_id);
$tab_delete = [];
$tab_update = [];
$tab_id_args = [];
foreach($reps_by_id as $rep ) { //First loop which stores the report_id from the SQL query
$tab_id_args[] = $rep['id'];
}
$aReturn = static::select([
'select' =>['*'],
'table' => ['usergroups_reports'],
'where' => ['group_id = ?','report_id in (?)'],
'data' => [$aArgs['id'], $tab_id_args]
]);
$tabIdQuery = [];
foreach($aReturn as $rep ) { // First loop which stores the report_id from the SQL query
$tab_id_query[] = $rep['report_id'];
}
/*Loop checking if there is a match up with the values from the SQL query
*/
foreach($reps_by_id as $value) { //Checking if there is a match up with the values from $reps_by_id array and $tab_id_query,adding/deleting a line in the usergroups_reports table in the database
if(in_array($value['id'],$tab_id_query)) {
if (!$value['checked']) { // If the value is not checked ( = false) and in the array( i.e in the table usergroups_report) then we delete the line in the database
$tab_delete = [
'group_id' => $aArgs['id'],
'report_id' => $value['id']
];
static::delete($tab_delete);
}
}else{
if ($value['checked']) { // If the value is checked ( = true) and not in the array( i.e in the table usergroups_report) then we add the line in the database
$tab_update = [
'group_id' => $aArgs['id'],
'report_id' => $value['id']
];
static::insertInto($tab_update, 'usergroups_reports');
}
}
}
$test_unitaire_true_only = static::select([
'select' =>['*'],
'table' => ['usergroups_reports'],
'where' => ['group_id = ?','report_id in (?)'],
'data' => [$aArgs['id'], $tab_id_args]
]);
return $test_unitaire_true_only;
}
public static function delete(array $aArgs = [])
{
/*static::checkRequired($aArgs, ['report_id']);
static::checkString($aArgs, ['group_id']);*/
$aReturn = static::deleteFrom([
'table' => 'usergroups_reports',
'where' => ['report_id = ?','group_id = ?'],
'data' => [$aArgs['report_id'],$aArgs['group_id']]
]);
return $aReturn;
}
}
<?php
namespace MaarchTest;
/**
* 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.
*
*/
require_once __DIR__.'/define.php';
class AdminReportsTest extends \PHPUnit_Framework_TestCase
{
public function testGetList()
{
$client = new \GuzzleHttp\Client([
'base uri' => '127.0.0.1/MaarchCourrier/rest/report/groups',
'timeout' => 42.0,]
);
$response = $client->request('GET','127.0.0.1/MaarchCourrier/rest/report/groups',[
'auth' => ['superadmin','superadmin']
]);
$decoded_response = json_decode($response->getBody(), true);
$this->assertNotNull($decoded_response);
}
public function testGetReportsTypesByXML(){
$client = new \GuzzleHttp\Client([
'base uri' => '127.0.0.1/MaarchCourrier/rest/report/groups',
'timeout' => 42.0,]
);
$aArgs = [
'id'=> 'ELU'
];
$response = $client->request('GET','127.0.0.1/MaarchCourrier/rest/report/groups/'.$aArgs['id'],[
'auth' => ['superadmin','superadmin']
]);
$decoded_response = json_decode($response->getBody(), true);
$this->assertNotNull($decoded_response);
}
/* public function testGetUserByGroupId()
{
$action = new \Core\Controllers\AdminReportsController();
$environment = \Slim\Http\Environment::mock(
[
'REQUEST_METHOD' => 'GET',
]
);
$aArgs = [
'id'=> 'NEW'
];
$request = \Slim\Http\Request::createFromEnvironment($environment);
$response = new \Slim\Http\Response();
$response = $action->getUserByGroupId($request, $response, $aArgs);
$compare = '[[{"user_id":"NEW","group_id":"Nouveau",'
. '"primary_group":"Y","role":"N"}]]';
$this->assertSame((string)$response->getBody(), $compare);
}
*/
public function testUpdate()
{
$client = new \GuzzleHttp\Client([
'base uri' => '127.0.0.1/MaarchCourrier/rest/report/groups',
'timeout' => 42.0,]
);
$aArgs = [
'id'=> 'ELU'
];
$response_guzzle_XML = $client->request('GET','127.0.0.1/MaarchCourrier/rest/report/groups/'.$aArgs['id'],[
'auth' => ['superadmin','superadmin']
]);
$d = [];
$d = json_decode($response_guzzle_XML->getBody(), true);
$checked_val_reverse = !$d[0]['checked'];
$tab_test_unitaire [] = [
'id' => "folder_view_stat",
'checked' => $checked_val_reverse
];
$aArgs['data'] = $tab_test_unitaire;
$response_guzzle = $client->request('PUT','127.0.0.1/MaarchCourrier/rest/report/groups/'.$aArgs['id'],[
'auth' => ['superadmin','superadmin'],'form_params' => $aArgs['data']
]);
$response_guzzle_XML_after = $client->request('GET','127.0.0.1/MaarchCourrier/rest/report/groups/'.$aArgs['id'],[
'auth' => ['superadmin','superadmin']
]);
$guzzle_xml_after = json_decode($response_guzzle_XML_after->getBody(), true);
$checked_val_after_update = $guzzle_xml_after[0]['checked'];
$this->assertSame($checked_val_reverse, $checked_val_after_update);
}
}
...@@ -108,8 +108,25 @@ $app->get('/docserver', \Core\Controllers\DocserverController::class . ':getList ...@@ -108,8 +108,25 @@ $app->get('/docserver', \Core\Controllers\DocserverController::class . ':getList
$app->get('/docserver/{id}', \Core\Controllers\DocserverController::class . ':getById'); $app->get('/docserver/{id}', \Core\Controllers\DocserverController::class . ':getById');
//docserverType //docserverType
$app->get('/docserverType', \Core\Controllers\DocserverTypeController::class . ':getList'); $app->get('/docserverType', \core\Controllers\DocserverTypeController::class . ':getList');
$app->get('/docserverType/{id}', \Core\Controllers\DocserverTypeController::class . ':getById'); $app->get('/docserverType/{id}', \core\Controllers\DocserverTypeController::class . ':getById');
/**
*
*
*
*
*
*
*
**/
//admin_reports
$app->get('/report/groups', \Core\Controllers\AdminReportsController::class . ':getList');
$app->get('/report/groups/{id}', \Core\Controllers\AdminReportsController::class . ':getReportsTypesByXML');
$app->put('/report/groups/{id}', \Core\Controllers\AdminReportsController::class . ':update');
//attachments //attachments
$app->get('/attachments', \Attachments\Controllers\AttachmentsController::class . ':getList'); $app->get('/attachments', \Attachments\Controllers\AttachmentsController::class . ':getList');
......
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