From fa7e92a8b5bba48ce541566b275cec714a216672 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Sat, 11 Dec 2021 11:54:16 +0100 Subject: [PATCH] new datamapper mostly implemented --- Controller/BackendController.php | 29 ++++++++++++++-------- Models/Client.php | 21 ++++++++++++++++ Models/ClientAttributeMapper.php | 12 ++++----- Models/ClientAttributeTypeL11nMapper.php | 10 ++++---- Models/ClientAttributeTypeMapper.php | 16 ++++++------ Models/ClientAttributeValueMapper.php | 10 ++++---- Models/ClientMapper.php | 16 ++++++------ Theme/Backend/client-list.tpl.php | 2 +- Theme/Backend/client-profile-bills.tpl.php | 8 +++--- Theme/Backend/client-profile.tpl.php | 6 +++-- tests/Bootstrap.php | 4 +-- tests/Models/ClientMapperTest.php | 4 +-- 12 files changed, 84 insertions(+), 54 deletions(-) diff --git a/Controller/BackendController.php b/Controller/BackendController.php index 646e066..1ad37cc 100755 --- a/Controller/BackendController.php +++ b/Controller/BackendController.php @@ -20,6 +20,7 @@ use Modules\ClientManagement\Models\ClientMapper; use Modules\Media\Models\Media; use phpOMS\Asset\AssetType; use phpOMS\Contract\RenderableInterface; +use phpOMS\DataStorage\Database\Query\OrderType; use phpOMS\Localization\ISO3166CharEnum; use phpOMS\Localization\ISO3166NameEnum; use phpOMS\Localization\Money; @@ -57,10 +58,13 @@ final class BackendController extends Controller $view->setTemplate('/Modules/ClientManagement/Theme/Backend/client-list'); $view->addData('nav', $this->app->moduleManager->get('Navigation')->createNavigationMid(1003102001, $request, $response)); - $client = ClientMapper::with('notes', models: null) - ::with('contactElements', models: null) - //::with('type', 'backend_image', models: [Media::class]) // @todo: it would be nicer if I coult say files:type or files/type and remove the models parameter? @todo: uncommented for now because the type is also part of client and therefore bug. that's the problem with a mix of black/whitelisting in the datamapper with the "with" feature. make it whitelist only for belongsTo, ownsMany, hasOne, .... - ::getAfterPivot(0, null, 25); + $client = ClientMapper::getAll() + ->with('profile') + ->with('profile/account') + ->with('profile/image') + ->with('mainAddress') + ->limit(25) + ->execute(); $view->addData('client', $client); @@ -111,10 +115,15 @@ final class BackendController extends Controller $view->setTemplate('/Modules/ClientManagement/Theme/Backend/client-profile'); $view->addData('nav', $this->app->moduleManager->get('Navigation')->createNavigationMid(1003102001, $request, $response)); - $client = ClientMapper - ::with('files', limit: 5)::orderBy('createdAt', 'ASC') - ::with('notes', limit: 5)::orderBy('id', 'ASC') - ::get((int) $request->getData('id')); + $client = ClientMapper::get() + ->with('profile') + ->with('profile/account') + ->with('contactElements') + ->with('mainAddress') + ->with('files')->limit(5, 'files')->sort('files', OrderType::DESC) + ->with('notes')->limit(5, 'files')->sort('notes', OrderType::DESC) + ->where('id', (int) $request->getData('id')) + ->execute(); $view->setData('client', $client); @@ -123,9 +132,7 @@ final class BackendController extends Controller $ytd = SalesBillMapper::getSalesByClientId($client->getId(), new SmartDateTime('Y-01-01'), new SmartDateTime('now')); $mtd = SalesBillMapper::getSalesByClientId($client->getId(), new SmartDateTime('Y-m-01'), new SmartDateTime('now')); $lastOrder = SalesBillMapper::getLastOrderDateByClientId($client->getId()); - $newestInvoices = SalesBillMapper - ::with('language', $response->getLanguage(), [BillTypeL11n::class]) - ::getNewestClientInvoices($client->getId(), 5); + $newestInvoices = SalesBillMapper::getAll()->with('client')->where('client', $client->getId())->sort('id', OrderType::DESC)->limit(5)->execute(); $monthlySalesCosts = SalesBillMapper::getClientMonthlySalesCosts($client->getId(), (new SmartDateTime('now'))->createModify(-1), new SmartDateTime('now')); $items = SalesBillMapper::getClientItem($client->getId(), (new SmartDateTime('now'))->createModify(-1), new SmartDateTime('now')); } else { diff --git a/Models/Client.php b/Models/Client.php index 4890e48..ca99697 100755 --- a/Models/Client.php +++ b/Models/Client.php @@ -18,6 +18,7 @@ use Modules\Admin\Models\Address; use Modules\Admin\Models\NullAddress; use Modules\Editor\Models\EditorDoc; use Modules\Media\Models\Media; +use Modules\Media\Models\NullMedia; use Modules\Profile\Models\ContactElement; use Modules\Profile\Models\NullContactElement; use Modules\Profile\Models\Profile; @@ -308,6 +309,26 @@ class Client return $files; } + /** + * Get all media files by type + * + * @param int $type Media type + * + * @return Media + * + * @since 1.0.0 + */ + public function getFileByType(int $type) : Media + { + foreach ($this->files as $file) { + if ($file->type === $type) { + return $file; + } + } + + return new NullMedia(); + } + /** * {@inheritdoc} */ diff --git a/Models/ClientAttributeMapper.php b/Models/ClientAttributeMapper.php index ea637d4..9710749 100755 --- a/Models/ClientAttributeMapper.php +++ b/Models/ClientAttributeMapper.php @@ -14,7 +14,7 @@ declare(strict_types=1); namespace Modules\ClientManagement\Models; -use phpOMS\DataStorage\Database\DataMapperAbstract; +use phpOMS\DataStorage\Database\Mapper\DataMapperFactory; /** * Client mapper class. @@ -24,7 +24,7 @@ use phpOMS\DataStorage\Database\DataMapperAbstract; * @link https://orange-management.org * @since 1.0.0 */ -final class ClientAttributeMapper extends DataMapperAbstract +final class ClientAttributeMapper extends DataMapperFactory { /** * Columns. @@ -32,7 +32,7 @@ final class ClientAttributeMapper extends DataMapperAbstract * @var array * @since 1.0.0 */ - protected static array $columns = [ + public const COLUMNS = [ 'clientmgmt_client_attr_id' => ['name' => 'clientmgmt_client_attr_id', 'type' => 'int', 'internal' => 'id'], 'clientmgmt_client_attr_client' => ['name' => 'clientmgmt_client_attr_client', 'type' => 'int', 'internal' => 'client'], 'clientmgmt_client_attr_type' => ['name' => 'clientmgmt_client_attr_type', 'type' => 'int', 'internal' => 'type'], @@ -45,7 +45,7 @@ final class ClientAttributeMapper extends DataMapperAbstract * @var array * @since 1.0.0 */ - protected static array $ownsOne = [ + public const OWNS_ONE = [ 'type' => [ 'mapper' => ClientAttributeTypeMapper::class, 'external' => 'clientmgmt_client_attr_type', @@ -62,7 +62,7 @@ final class ClientAttributeMapper extends DataMapperAbstract * @var string * @since 1.0.0 */ - protected static string $table = 'clientmgmt_client_attr'; + public const TABLE = 'clientmgmt_client_attr'; /** * Primary field name. @@ -70,5 +70,5 @@ final class ClientAttributeMapper extends DataMapperAbstract * @var string * @since 1.0.0 */ - protected static string $primaryField = 'clientmgmt_client_attr_id'; + public const PRIMARYFIELD ='clientmgmt_client_attr_id'; } diff --git a/Models/ClientAttributeTypeL11nMapper.php b/Models/ClientAttributeTypeL11nMapper.php index 6015713..40668b5 100755 --- a/Models/ClientAttributeTypeL11nMapper.php +++ b/Models/ClientAttributeTypeL11nMapper.php @@ -14,7 +14,7 @@ declare(strict_types=1); namespace Modules\ClientManagement\Models; -use phpOMS\DataStorage\Database\DataMapperAbstract; +use phpOMS\DataStorage\Database\Mapper\DataMapperFactory; /** * Client mapper class. @@ -24,7 +24,7 @@ use phpOMS\DataStorage\Database\DataMapperAbstract; * @link https://orange-management.org * @since 1.0.0 */ -final class ClientAttributeTypeL11nMapper extends DataMapperAbstract +final class ClientAttributeTypeL11nMapper extends DataMapperFactory { /** * Columns. @@ -32,7 +32,7 @@ final class ClientAttributeTypeL11nMapper extends DataMapperAbstract * @var array * @since 1.0.0 */ - protected static array $columns = [ + public const COLUMNS = [ 'clientmgmt_attr_type_l11n_id' => ['name' => 'clientmgmt_attr_type_l11n_id', 'type' => 'int', 'internal' => 'id'], 'clientmgmt_attr_type_l11n_title' => ['name' => 'clientmgmt_attr_type_l11n_title', 'type' => 'string', 'internal' => 'title', 'autocomplete' => true], 'clientmgmt_attr_type_l11n_type' => ['name' => 'clientmgmt_attr_type_l11n_type', 'type' => 'int', 'internal' => 'type'], @@ -45,7 +45,7 @@ final class ClientAttributeTypeL11nMapper extends DataMapperAbstract * @var string * @since 1.0.0 */ - protected static string $table = 'clientmgmt_attr_type_l11n'; + public const TABLE = 'clientmgmt_attr_type_l11n'; /** * Primary field name. @@ -53,5 +53,5 @@ final class ClientAttributeTypeL11nMapper extends DataMapperAbstract * @var string * @since 1.0.0 */ - protected static string $primaryField = 'clientmgmt_attr_type_l11n_id'; + public const PRIMARYFIELD ='clientmgmt_attr_type_l11n_id'; } diff --git a/Models/ClientAttributeTypeMapper.php b/Models/ClientAttributeTypeMapper.php index 821839d..384cca8 100755 --- a/Models/ClientAttributeTypeMapper.php +++ b/Models/ClientAttributeTypeMapper.php @@ -14,7 +14,7 @@ declare(strict_types=1); namespace Modules\ClientManagement\Models; -use phpOMS\DataStorage\Database\DataMapperAbstract; +use phpOMS\DataStorage\Database\Mapper\DataMapperFactory; /** * Client mapper class. @@ -24,7 +24,7 @@ use phpOMS\DataStorage\Database\DataMapperAbstract; * @link https://orange-management.org * @since 1.0.0 */ -final class ClientAttributeTypeMapper extends DataMapperAbstract +final class ClientAttributeTypeMapper extends DataMapperFactory { /** * Columns. @@ -32,7 +32,7 @@ final class ClientAttributeTypeMapper extends DataMapperAbstract * @var array * @since 1.0.0 */ - protected static array $columns = [ + public const COLUMNS = [ 'clientmgmt_attr_type_id' => ['name' => 'clientmgmt_attr_type_id', 'type' => 'int', 'internal' => 'id'], 'clientmgmt_attr_type_name' => ['name' => 'clientmgmt_attr_type_name', 'type' => 'string', 'internal' => 'name', 'autocomplete' => true], 'clientmgmt_attr_type_fields' => ['name' => 'clientmgmt_attr_type_fields', 'type' => 'int', 'internal' => 'fields'], @@ -47,21 +47,19 @@ final class ClientAttributeTypeMapper extends DataMapperAbstract * @var array * @since 1.0.0 */ - protected static array $hasMany = [ + public const HAS_MANY = [ 'l11n' => [ 'mapper' => ClientAttributeTypeL11nMapper::class, 'table' => 'clientmgmt_attr_type_l11n', 'self' => 'clientmgmt_attr_type_l11n_type', 'column' => 'title', - 'conditional' => true, 'external' => null, ], 'defaults' => [ 'mapper' => ClientAttributeValueMapper::class, 'table' => 'clientmgmt_client_attr_default', 'self' => 'clientmgmt_client_attr_default_type', - 'external' => 'clientmgmt_client_attr_default_value', - 'conditional' => false, + 'external' => 'clientmgmt_client_attr_default_value' ], ]; @@ -71,7 +69,7 @@ final class ClientAttributeTypeMapper extends DataMapperAbstract * @var string * @since 1.0.0 */ - protected static string $table = 'clientmgmt_attr_type'; + public const TABLE = 'clientmgmt_attr_type'; /** * Primary field name. @@ -79,5 +77,5 @@ final class ClientAttributeTypeMapper extends DataMapperAbstract * @var string * @since 1.0.0 */ - protected static string $primaryField = 'clientmgmt_attr_type_id'; + public const PRIMARYFIELD ='clientmgmt_attr_type_id'; } diff --git a/Models/ClientAttributeValueMapper.php b/Models/ClientAttributeValueMapper.php index a319522..0fab3d8 100755 --- a/Models/ClientAttributeValueMapper.php +++ b/Models/ClientAttributeValueMapper.php @@ -14,7 +14,7 @@ declare(strict_types=1); namespace Modules\ClientManagement\Models; -use phpOMS\DataStorage\Database\DataMapperAbstract; +use phpOMS\DataStorage\Database\Mapper\DataMapperFactory; /** * Client mapper class. @@ -24,7 +24,7 @@ use phpOMS\DataStorage\Database\DataMapperAbstract; * @link https: //orange-management.org * @since 1.0.0 */ -final class ClientAttributeValueMapper extends DataMapperAbstract +final class ClientAttributeValueMapper extends DataMapperFactory { /** * Columns. @@ -32,7 +32,7 @@ final class ClientAttributeValueMapper extends DataMapperAbstract * @var array * @since 1.0.0 */ - protected static array $columns = [ + public const COLUMNS = [ 'clientmgmt_attr_value_id' => ['name' => 'clientmgmt_attr_value_id', 'type' => 'int', 'internal' => 'id'], 'clientmgmt_attr_value_default' => ['name' => 'clientmgmt_attr_value_default', 'type' => 'bool', 'internal' => 'isDefault'], 'clientmgmt_attr_value_type' => ['name' => 'clientmgmt_attr_value_type', 'type' => 'int', 'internal' => 'type'], @@ -50,7 +50,7 @@ final class ClientAttributeValueMapper extends DataMapperAbstract * @var string * @since 1.0.0 */ - protected static string $table = 'clientmgmt_attr_value'; + public const TABLE = 'clientmgmt_attr_value'; /** * Primary field name. @@ -58,5 +58,5 @@ final class ClientAttributeValueMapper extends DataMapperAbstract * @var string * @since 1.0.0 */ - protected static string $primaryField = 'clientmgmt_attr_value_id'; + public const PRIMARYFIELD ='clientmgmt_attr_value_id'; } diff --git a/Models/ClientMapper.php b/Models/ClientMapper.php index 2b048b7..2aae8ee 100755 --- a/Models/ClientMapper.php +++ b/Models/ClientMapper.php @@ -19,7 +19,7 @@ use Modules\Editor\Models\EditorDocMapper; use Modules\Media\Models\MediaMapper; use Modules\Profile\Models\ContactElementMapper; use Modules\Profile\Models\ProfileMapper; -use phpOMS\DataStorage\Database\DataMapperAbstract; +use phpOMS\DataStorage\Database\Mapper\DataMapperFactory; /** * Client mapper class. @@ -29,7 +29,7 @@ use phpOMS\DataStorage\Database\DataMapperAbstract; * @link https://orange-management.org * @since 1.0.0 */ -final class ClientMapper extends DataMapperAbstract +final class ClientMapper extends DataMapperFactory { /** * Columns. @@ -37,7 +37,7 @@ final class ClientMapper extends DataMapperAbstract * @var array * @since 1.0.0 */ - protected static array $columns = [ + public const COLUMNS = [ 'clientmgmt_client_id' => ['name' => 'clientmgmt_client_id', 'type' => 'int', 'internal' => 'id'], 'clientmgmt_client_no' => ['name' => 'clientmgmt_client_no', 'type' => 'string', 'internal' => 'number'], 'clientmgmt_client_no_reverse' => ['name' => 'clientmgmt_client_no_reverse', 'type' => 'string', 'internal' => 'numberReverse'], @@ -55,7 +55,7 @@ final class ClientMapper extends DataMapperAbstract * @var string * @since 1.0.0 */ - protected static string $table = 'clientmgmt_client'; + public const TABLE = 'clientmgmt_client'; /** * Primary field name. @@ -63,7 +63,7 @@ final class ClientMapper extends DataMapperAbstract * @var string * @since 1.0.0 */ - protected static string $primaryField = 'clientmgmt_client_id'; + public const PRIMARYFIELD ='clientmgmt_client_id'; /** * Created at column @@ -71,7 +71,7 @@ final class ClientMapper extends DataMapperAbstract * @var string * @since 1.0.0 */ - protected static string $createdAt = 'clientmgmt_client_created_at'; + public const CREATED_AT = 'clientmgmt_client_created_at'; /** * Has one relation. @@ -79,7 +79,7 @@ final class ClientMapper extends DataMapperAbstract * @var array * @since 1.0.0 */ - protected static array $ownsOne = [ + public const OWNS_ONE = [ 'profile' => [ 'mapper' => ProfileMapper::class, 'external' => 'clientmgmt_client_profile', @@ -96,7 +96,7 @@ final class ClientMapper extends DataMapperAbstract * @var array * @since 1.0.0 */ - protected static array $hasMany = [ + public const HAS_MANY = [ 'files' => [ 'mapper' => MediaMapper::class, /* mapper of the related object */ 'table' => 'clientmgmt_client_media', /* table of the related object, null if no relation table is used (many->1) */ diff --git a/Theme/Backend/client-list.tpl.php b/Theme/Backend/client-list.tpl.php index 6947624..d53806e 100755 --- a/Theme/Backend/client-list.tpl.php +++ b/Theme/Backend/client-list.tpl.php @@ -104,7 +104,7 @@ echo $this->getData('nav')->render(); ?> $value) : ++$count; $url = UriFactory::build('{/prefix}sales/client/profile?{?}&id=' . $value->getId()); - $image = $value->getFileByType('backend_image'); + $image = $value->getFileByType(0); ?> getData('newestInvoices') ?? []; - $value) : + $value) : ++$count; $url = UriFactory::build('{/prefix}sales/bill?{?}&id=' . $value->getId()); ?> @@ -179,8 +181,8 @@ $bills = $this->getData('newestInvoices') ?? []; billZip; ?> billCity; ?> billCountry; ?> - net->getCurrency(); ?> - profit->getCurrency(); ?> + netSales->getCurrency(); ?> + netProfit->getCurrency(); ?> createdAt->format('Y-m-d'); ?> diff --git a/Theme/Backend/client-profile.tpl.php b/Theme/Backend/client-profile.tpl.php index f4dd3fe..3ea9abf 100755 --- a/Theme/Backend/client-profile.tpl.php +++ b/Theme/Backend/client-profile.tpl.php @@ -262,14 +262,16 @@ echo $this->getData('nav')->render(); getHtml('Net'); ?> getHtml('Date'); ?> - getId()); ?> getNumber(); ?> type->getL11n(); ?> billTo; ?> - net->getCurrency(); ?> + netSales->getCurrency(); ?> createdAt->format('Y-m-d'); ?> diff --git a/tests/Bootstrap.php b/tests/Bootstrap.php index f5fc99c..252a62f 100755 --- a/tests/Bootstrap.php +++ b/tests/Bootstrap.php @@ -12,7 +12,7 @@ require_once __DIR__ . '/../vendor/autoload.php'; require_once __DIR__ . '/Autoloader.php'; use phpOMS\DataStorage\Database\DatabasePool; -use phpOMS\DataStorage\Database\DataMapperAbstract; +use phpOMS\DataStorage\Database\Mapper\DataMapperFactory; use phpOMS\DataStorage\Session\HttpSession; $CONFIG = [ @@ -329,7 +329,7 @@ $GLOBALS['dbpool']->create('delete', $CONFIG['db']['core']['masters']['delete']) $GLOBALS['dbpool']->create('insert', $CONFIG['db']['core']['masters']['insert']); $GLOBALS['dbpool']->create('schema', $CONFIG['db']['core']['masters']['schema']); -DataMapperAbstract::setConnection($GLOBALS['dbpool']->get()); +DataMapperFactory::db($GLOBALS['dbpool']->get()); $GLOBALS['frameworkpath'] = '/phpOMS/'; diff --git a/tests/Models/ClientMapperTest.php b/tests/Models/ClientMapperTest.php index ededef1..ad56488 100755 --- a/tests/Models/ClientMapperTest.php +++ b/tests/Models/ClientMapperTest.php @@ -37,7 +37,7 @@ final class ClientMapperTest extends \PHPUnit\Framework\TestCase // This is required because by default a NullAccount without an ID is created in the Profile model // but NullModels without ids are handled like "null" values which are not allowed for Accounts. - $profile = ProfileMapper::getFor(1, 'account'); + $profile = ProfileMapper::get()->where('account', 1)->execute(); $profile = $profile instanceof NullProfile ? new Profile() : $profile; if ($profile->account->getId() === 0) { $profile->account = new NullAccount(1); @@ -45,7 +45,7 @@ final class ClientMapperTest extends \PHPUnit\Framework\TestCase $client->profile = $profile; - $id = ClientMapper::create($client); + $id = ClientMapper::create()->execute($client); self::assertGreaterThan(0, $client->getId()); self::assertEquals($id, $client->getId()); }