From 8b153555af76698096f081f0efa328b683d1d7f3 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Thu, 26 Jan 2023 21:54:13 +0100 Subject: [PATCH] org -> unit change, some new functionality --- Admin/Install/db.json | 54 +------------------------- Controller/ApiController.php | 21 +++++++++- Models/UnitMapper.php | 22 +++++------ tests/Controller/ApiControllerTest.php | 2 +- 4 files changed, 33 insertions(+), 66 deletions(-) diff --git a/Admin/Install/db.json b/Admin/Install/db.json index e9e4e32..6934b0c 100755 --- a/Admin/Install/db.json +++ b/Admin/Install/db.json @@ -1,54 +1,4 @@ { - "organization_unit": { - "name": "organization_unit", - "fields": { - "organization_unit_id": { - "name": "organization_unit_id", - "type": "INT", - "null": false, - "primary": true, - "autoincrement": true - }, - "organization_unit_name": { - "name": "organization_unit_name", - "type": "VARCHAR(50)", - "default": null, - "null": true - }, - "organization_unit_image": { - "name": "organization_unit_image", - "type": "INT", - "default": null, - "null": true - }, - "organization_unit_description": { - "name": "organization_unit_description", - "type": "TEXT", - "default": null, - "null": true - }, - "organization_unit_descriptionraw": { - "name": "organization_unit_descriptionraw", - "type": "TEXT", - "default": null, - "null": true - }, - "organization_unit_parent": { - "name": "organization_unit_parent", - "type": "INT", - "default": null, - "null": true, - "foreignTable": "organization_unit", - "foreignKey": "organization_unit_id" - }, - "organization_unit_status": { - "name": "organization_unit_status", - "type": "TINYINT", - "default": null, - "null": true - } - } - }, "organization_department": { "name": "organization_department", "fields": { @@ -95,8 +45,8 @@ "name": "organization_department_unit", "type": "INT", "null": false, - "foreignTable": "organization_unit", - "foreignKey": "organization_unit_id" + "foreignTable": "unit", + "foreignKey": "unit_id" } } }, diff --git a/Controller/ApiController.php b/Controller/ApiController.php index d897c56..6856820 100755 --- a/Controller/ApiController.php +++ b/Controller/ApiController.php @@ -14,6 +14,9 @@ declare(strict_types=1); namespace Modules\Organization\Controller; +use Model\Setting; +use Model\SettingMapper; +use Modules\Admin\Models\SettingsEnum as ModelsSettingsEnum; use Modules\Media\Models\PathSettings; use Modules\Organization\Models\Department; use Modules\Organization\Models\DepartmentMapper; @@ -28,6 +31,7 @@ use Modules\Organization\Models\Unit; use Modules\Organization\Models\UnitMapper; use phpOMS\Account\GroupStatus; use phpOMS\Message\Http\HttpRequest; +use phpOMS\Message\Http\HttpResponse; use phpOMS\Message\Http\RequestStatusCode; use phpOMS\Message\NotificationLevel; use phpOMS\Message\RequestAbstract; @@ -187,11 +191,24 @@ final class ApiController extends Controller /** @var \Model\Setting $setting */ $setting = $this->app->appSettings->get(null, SettingsEnum::GROUP_GENERATE_AUTOMATICALLY_UNIT); if ($setting->content === '1') { + $internalResponse = new HttpResponse(); $newRequest = new HttpRequest(); $newRequest->header->account = $request->header->account; - $newRequest->setData('name', 'org:unit:' . \strtolower($unit->name)); + $newRequest->setData('name', 'unit:' . \strtolower($unit->name)); $newRequest->setData('status', GroupStatus::ACTIVE); - $this->app->moduleManager->get('Admin')->apiGroupCreate($newRequest, $response, $data); + + $this->app->moduleManager->get('Admin')->apiGroupCreate($newRequest, $internalResponse, $data); + $group = $internalResponse->get($newRequest->uri->__toString())['response']; + + $setting = new Setting( + 0, + ModelsSettingsEnum::UNIT_DEFAULT_GROUPS, + \json_encode([$group->getId()]), + unit: $unit->getId(), + module: 'Admin' + ); + $this->createModel($request->header->account, $setting, SettingMapper::class, 'setting', $request->getOrigin()); + } $this->fillJsonResponse($request, $response, NotificationLevel::OK, 'Unit', 'Unit successfully created.', $unit); diff --git a/Models/UnitMapper.php b/Models/UnitMapper.php index e915b26..bd07df0 100755 --- a/Models/UnitMapper.php +++ b/Models/UnitMapper.php @@ -34,13 +34,13 @@ final class UnitMapper extends DataMapperFactory * @since 1.0.0 */ public const COLUMNS = [ - 'organization_unit_id' => ['name' => 'organization_unit_id', 'type' => 'int', 'internal' => 'id'], - 'organization_unit_name' => ['name' => 'organization_unit_name', 'type' => 'string', 'internal' => 'name', 'autocomplete' => true], - 'organization_unit_image' => ['name' => 'organization_unit_image', 'type' => 'int', 'internal' => 'image'], - 'organization_unit_description' => ['name' => 'organization_unit_description', 'type' => 'string', 'internal' => 'description'], - 'organization_unit_descriptionraw' => ['name' => 'organization_unit_descriptionraw', 'type' => 'string', 'internal' => 'descriptionRaw'], - 'organization_unit_parent' => ['name' => 'organization_unit_parent', 'type' => 'int', 'internal' => 'parent'], - 'organization_unit_status' => ['name' => 'organization_unit_status', 'type' => 'int', 'internal' => 'status'], + 'unit_id' => ['name' => 'unit_id', 'type' => 'int', 'internal' => 'id'], + 'unit_name' => ['name' => 'unit_name', 'type' => 'string', 'internal' => 'name', 'autocomplete' => true], + 'unit_image' => ['name' => 'unit_image', 'type' => 'int', 'internal' => 'image'], + 'unit_description' => ['name' => 'unit_description', 'type' => 'string', 'internal' => 'description'], + 'unit_descriptionraw' => ['name' => 'unit_descriptionraw', 'type' => 'string', 'internal' => 'descriptionRaw'], + 'unit_parent' => ['name' => 'unit_parent', 'type' => 'int', 'internal' => 'parent'], + 'unit_status' => ['name' => 'unit_status', 'type' => 'int', 'internal' => 'status'], ]; /** @@ -52,7 +52,7 @@ final class UnitMapper extends DataMapperFactory public const OWNS_ONE = [ 'image' => [ 'mapper' => MediaMapper::class, - 'external' => 'organization_unit_image', + 'external' => 'unit_image', ], ]; @@ -65,7 +65,7 @@ final class UnitMapper extends DataMapperFactory public const BELONGS_TO = [ 'parent' => [ 'mapper' => self::class, - 'external' => 'organization_unit_parent', + 'external' => 'unit_parent', ], ]; @@ -83,7 +83,7 @@ final class UnitMapper extends DataMapperFactory * @var string * @since 1.0.0 */ - public const TABLE = 'organization_unit'; + public const TABLE = 'unit'; /** * Primary field name. @@ -91,5 +91,5 @@ final class UnitMapper extends DataMapperFactory * @var string * @since 1.0.0 */ - public const PRIMARYFIELD ='organization_unit_id'; + public const PRIMARYFIELD ='unit_id'; } diff --git a/tests/Controller/ApiControllerTest.php b/tests/Controller/ApiControllerTest.php index 8e7113b..dfa963f 100755 --- a/tests/Controller/ApiControllerTest.php +++ b/tests/Controller/ApiControllerTest.php @@ -53,7 +53,7 @@ final class ApiControllerTest extends \PHPUnit\Framework\TestCase }; $this->app->dbPool = $GLOBALS['dbpool']; - $this->app->orgId = 1; + $this->app->unitId = 1; $this->app->accountManager = new AccountManager($GLOBALS['session']); $this->app->appSettings = new CoreSettings(); $this->app->moduleManager = new ModuleManager($this->app, __DIR__ . '/../../../Modules/');