new datamapper mostly implemented

This commit is contained in:
Dennis Eichhorn 2021-12-11 11:54:17 +01:00
parent e284ef4f24
commit f13220e817
11 changed files with 76 additions and 49 deletions

View File

@ -45,6 +45,6 @@ class Media
$defaultProfileImage = \reset($media['upload'][0]);
$setting = new Setting();
SettingMapper::create($setting->with(0, 'default_profile_image', (string) $defaultProfileImage->getId(), '\\d+', null, 'Profile'));
SettingMapper::create()->execute($setting->with(0, 'default_profile_image', (string) $defaultProfileImage->getId(), '\\d+', null, 'Profile'));
}
}

View File

@ -59,7 +59,7 @@ final class Installer extends InstallerAbstract
*/
private static function createProfiles() : void
{
$profile = new Profile(AccountMapper::get(1));
ProfileMapper::create($profile);
$profile = new Profile(AccountMapper::get()->where('id', 1)->execute());
ProfileMapper::create()->execute($profile);
}
}

View File

@ -104,7 +104,7 @@ final class ApiController extends Controller
*/
public function apiProfileTempLoginCreate(RequestAbstract $request, ResponseAbstract $response) : void
{
$account = AccountMapper::get($request->header->account);
$account = AccountMapper::get()->where('id', $request->header->account)->execute();
$account->tempPassword = \password_hash(\random_bytes(64), \PASSWORD_BCRYPT);
$this->updateModel($request->header->account, $account, $account, AccountMapper::class, 'profile', $request->getOrigin());
@ -136,14 +136,14 @@ final class ApiController extends Controller
$account = (int) \trim($account);
/** @var Profile $isInDb */
$isInDb = ProfileMapper::getFor($account, 'account');
$isInDb = ProfileMapper::get()->where('account', $account)->execute();
if ($isInDb->getId() !== 0) {
$profiles[] = $isInDb;
continue;
}
$profiles[] = new Profile(AccountMapper::get($account));
$profiles[] = new Profile(AccountMapper::get()->where('id', $account)->execute());
}
return $profiles;
@ -174,7 +174,7 @@ final class ApiController extends Controller
}
/** @var Profile $profile */
$profile = ProfileMapper::getFor($request->header->account, 'account');
$profile = ProfileMapper::get()->where('account', $request->header->account)->execute();
$old = clone $profile;
$uploaded = $this->app->moduleManager->get('Media')->uploadFiles(
@ -219,7 +219,7 @@ final class ApiController extends Controller
}
/** @var Profile $profile */
$profile = (int) ($request->getData('profile') ?? ProfileMapper::getFor($request->getData('account'), 'account')->getId());
$profile = (int) ($request->getData('profile') ?? ProfileMapper::get()->where('account', $request->getData('account'))->execute()->getId());
$contactElement = $this->createContactElementFromRequest($request);
@ -293,7 +293,7 @@ final class ApiController extends Controller
}
/** @var Profile $profile */
$profile = (int) ($request->getData('profile') ?? ProfileMapper::getFor($request->getData('account'), 'account')->getId());
$profile = (int) ($request->getData('profile') ?? ProfileMapper::get()->where('account', $request->getData('account'))->execute()->getId());
$address = $this->createAddressFromRequest($request);

View File

@ -14,11 +14,13 @@ declare(strict_types=1);
namespace Modules\Profile\Controller;
use Modules\Admin\Models\LocalizationMapper;
use Modules\Media\Models\MediaMapper;
use Modules\Media\Models\NullMedia;
use Modules\Profile\Models\ProfileMapper;
use phpOMS\Asset\AssetType;
use phpOMS\Contract\RenderableInterface;
use phpOMS\Localization\NullLocalization;
use phpOMS\Message\RequestAbstract;
use phpOMS\Message\ResponseAbstract;
use phpOMS\Views\View;
@ -70,15 +72,33 @@ final class BackendController extends Controller
$view->setTemplate('/Modules/Profile/Theme/Backend/profile-list');
if ($request->getData('ptype') === 'p') {
$view->setData('accounts', ProfileMapper::getBeforePivot((int) ($request->getData('id') ?? 0), null, 25));
$view->setData('accounts',
ProfileMapper::getAll()
->with('account')
->with('image')
->where('id', (int) ($request->getData('id') ?? 0), '<')
->limit(25)->execute()
);
} elseif ($request->getData('ptype') === 'n') {
$view->setData('accounts', ProfileMapper::getAfterPivot((int) ($request->getData('id') ?? 0), null, 25));
$view->setData('accounts',
ProfileMapper::getAll()
->with('account')
->with('image')
->where('id', (int) ($request->getData('id') ?? 0), '>')
->limit(25)->execute()
);
} else {
$view->setData('accounts', ProfileMapper::getAfterPivot(0, null, 25));
$view->setData('accounts',
ProfileMapper::getAll()
->with('account')
->with('image')
->where('id', 0, '>')
->limit(25)->execute()
);
}
$profileImage = $this->app->appSettings->get(null, 'default_profile_image', null, 'Profile');
$image = MediaMapper::get((int) $profileImage->content);
$image = MediaMapper::get()->where('id', (int) $profileImage->content)->execute();
$view->setData('defaultImage', $image);
@ -115,20 +135,29 @@ final class BackendController extends Controller
$calendarView->setTemplate('/Modules/Calendar/Theme/Backend/Components/Calendar/mini');
$view->addData('calendar', $calendarView);
$mapperQuery = ProfileMapper::get()->with('account')->with('image')->with('location')->with('contactElements');
$profile = $request->getData('for') !== null
? ProfileMapper::getFor((int) $request->getData('for'), 'account')
: ProfileMapper::get((int) $request->getData('id'));
? $mapperQuery->where('account', (int) $request->getData('for'))->execute()
: $mapperQuery->where('id', (int) $request->getData('id'))->execute();
$view->setData('account', $profile);
$l11n = null;
if ($profile->account->getId() === $request->header->account) {
$l11n = LocalizationMapper::get()->where('id', $profile->account->l11n->getId())->execute();
}
$view->setData('l11n', $l11n ?? new NullLocalization());
$accGrpSelector = new \Modules\Profile\Theme\Backend\Components\AccountGroupSelector\BaseView($this->app->l11nManager, $request, $response);
$view->addData('accGrpSelector', $accGrpSelector);
$media = MediaMapper::getFor((int) $profile->account->getId(), 'createdBy');
$media = MediaMapper::get()->where('createdBy', (int) $profile->account->getId())->limit(25)->execute();
$view->setData('media', $media instanceof NullMedia ? [] : (!\is_array($media) ? [$media] : $media));
$profileImage = $this->app->appSettings->get(null, 'default_profile_image', null, 'Profile');
$image = MediaMapper::get((int) $profileImage->content);
$image = MediaMapper::get()->where('id', (int) $profileImage->content)->execute();
$view->setData('defaultImage', $image);

View File

@ -14,7 +14,7 @@ declare(strict_types=1);
namespace Modules\Profile\Models;
use phpOMS\DataStorage\Database\DataMapperAbstract;
use phpOMS\DataStorage\Database\Mapper\DataMapperFactory;
/**
* Contact mapper class.
@ -24,7 +24,7 @@ use phpOMS\DataStorage\Database\DataMapperAbstract;
* @link https://orange-management.org
* @since 1.0.0
*/
final class ContactElementMapper extends DataMapperAbstract
final class ContactElementMapper extends DataMapperFactory
{
/**
* Columns.
@ -32,7 +32,7 @@ final class ContactElementMapper extends DataMapperAbstract
* @var array<string, array{name:string, type:string, internal:string, autocomplete?:bool, readonly?:bool, writeonly?:bool, annotations?:array}>
* @since 1.0.0
*/
protected static array $columns = [
public const COLUMNS = [
'profile_contact_element_id' => ['name' => 'profile_contact_element_id', 'type' => 'int', 'internal' => 'id'],
'profile_contact_element_type' => ['name' => 'profile_contact_element_type', 'type' => 'int', 'internal' => 'type'],
'profile_contact_element_subtype' => ['name' => 'profile_contact_element_subtype', 'type' => 'int', 'internal' => 'subtype'],
@ -46,7 +46,7 @@ final class ContactElementMapper extends DataMapperAbstract
* @var string
* @since 1.0.0
*/
protected static string $table = 'profile_contact_element';
public const TABLE = 'profile_contact_element';
/**
* Primary field name.
@ -54,5 +54,5 @@ final class ContactElementMapper extends DataMapperAbstract
* @var string
* @since 1.0.0
*/
protected static string $primaryField = 'profile_contact_element_id';
public const PRIMARYFIELD ='profile_contact_element_id';
}

View File

@ -15,7 +15,7 @@ declare(strict_types=1);
namespace Modules\Profile\Models;
use Modules\Media\Models\MediaMapper;
use phpOMS\DataStorage\Database\DataMapperAbstract;
use phpOMS\DataStorage\Database\Mapper\DataMapperFactory;
/**
* Contact mapper class.
@ -25,7 +25,7 @@ use phpOMS\DataStorage\Database\DataMapperAbstract;
* @link https://orange-management.org
* @since 1.0.0
*/
final class ContactMapper extends DataMapperAbstract
final class ContactMapper extends DataMapperFactory
{
/**
* Columns.
@ -33,7 +33,7 @@ final class ContactMapper extends DataMapperAbstract
* @var array<string, array{name:string, type:string, internal:string, autocomplete?:bool, readonly?:bool, writeonly?:bool, annotations?:array}>
* @since 1.0.0
*/
protected static array $columns = [
public const COLUMNS = [
'profile_contact_id' => ['name' => 'profile_contact_id', 'type' => 'int', 'internal' => 'id'],
'profile_contact_name1' => ['name' => 'profile_contact_name1', 'type' => 'string', 'internal' => 'name1'],
'profile_contact_name2' => ['name' => 'profile_contact_name2', 'type' => 'string', 'internal' => 'name2'],
@ -51,7 +51,7 @@ final class ContactMapper extends DataMapperAbstract
* @var array<string, array{mapper:string, external:string, by?:string, column?:string, conditional?:bool}>
* @since 1.0.0
*/
protected static array $ownsOne = [
public const OWNS_ONE = [
'image' => [
'mapper' => MediaMapper::class,
'external' => 'profile_contact_image',
@ -64,7 +64,7 @@ final class ContactMapper extends DataMapperAbstract
* @var string
* @since 1.0.0
*/
protected static string $table = 'profile_contact';
public const TABLE = 'profile_contact';
/**
* Primary field name.
@ -72,5 +72,5 @@ final class ContactMapper extends DataMapperAbstract
* @var string
* @since 1.0.0
*/
protected static string $primaryField = 'profile_contact_id';
public const PRIMARYFIELD ='profile_contact_id';
}

View File

@ -17,7 +17,7 @@ namespace Modules\Profile\Models;
use Modules\Admin\Models\AccountMapper;
use Modules\Admin\Models\AddressMapper;
use Modules\Media\Models\MediaMapper;
use phpOMS\DataStorage\Database\DataMapperAbstract;
use phpOMS\DataStorage\Database\Mapper\DataMapperFactory;
/**
* Profile mapper.
@ -27,7 +27,7 @@ use phpOMS\DataStorage\Database\DataMapperAbstract;
* @link https://orange-management.org
* @since 1.0.0
*/
final class ProfileMapper extends DataMapperAbstract
final class ProfileMapper extends DataMapperFactory
{
/**
* Columns.
@ -35,7 +35,7 @@ final class ProfileMapper extends DataMapperAbstract
* @var array<string, array<string, bool|string|array>>
* @since 1.0.0
*/
protected static array $columns = [
public const COLUMNS = [
'profile_account_id' => ['name' => 'profile_account_id', 'type' => 'int', 'internal' => 'id'],
'profile_account_image' => ['name' => 'profile_account_image', 'type' => 'int', 'internal' => 'image', 'annotations' => ['gdpr' => true]],
'profile_account_birthday' => ['name' => 'profile_account_birthday', 'type' => 'DateTime', 'internal' => 'birthday', 'annotations' => ['gdpr' => true]],
@ -50,7 +50,7 @@ final class ProfileMapper extends DataMapperAbstract
* @var array<string, array{mapper:string, external:string, by?:string, column?:string, conditional?:bool}>
* @since 1.0.0
*/
protected static array $ownsOne = [
public const OWNS_ONE = [
'image' => [
'mapper' => MediaMapper::class,
'external' => 'profile_account_image',
@ -63,7 +63,7 @@ final class ProfileMapper extends DataMapperAbstract
* @var array<string, array{mapper:string, external:string}>
* @since 1.0.0
*/
protected static array $belongsTo = [
public const BELONGS_TO = [
'account' => [
'mapper' => AccountMapper::class,
'external' => 'profile_account_account',
@ -76,7 +76,7 @@ final class ProfileMapper extends DataMapperAbstract
* @var array<string, array{mapper:string, table:string, self?:?string, external?:?string, column?:string}>
* @since 1.0.0
*/
protected static array $hasMany = [
public const HAS_MANY = [
'location' => [
'mapper' => AddressMapper::class,
'table' => 'profile_addressrel',
@ -97,7 +97,7 @@ final class ProfileMapper extends DataMapperAbstract
* @var string
* @since 1.0.0
*/
protected static string $model = Profile::class;
public const MODEL = Profile::class;
/**
* Primary table.
@ -105,7 +105,7 @@ final class ProfileMapper extends DataMapperAbstract
* @var string
* @since 1.0.0
*/
protected static string $table = 'profile_account';
public const TABLE = 'profile_account';
/**
* Primary field name.
@ -113,5 +113,5 @@ final class ProfileMapper extends DataMapperAbstract
* @var string
* @since 1.0.0
*/
protected static string $primaryField = 'profile_account_id';
public const PRIMARYFIELD ='profile_account_id';
}

View File

@ -34,10 +34,10 @@ use phpOMS\Utils\Converter\WeightType;
$profile = $this->getData('account');
/** @var \Modules\Media\Models\Media[] $media */
$media = $this->getDatA('media') ?? [];
$media = $this->getData('media') ?? [];
$account = $profile->account;
$l11n = $account->l11n;
$l11n = $this->getData('l11n');
echo $this->getData('nav')->render();
?>

View File

@ -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/';

View File

@ -106,7 +106,6 @@ final class ApiControllerTest extends \PHPUnit\Framework\TestCase
$this->module->apiProfileCreate($request, $response);
self::assertEquals('admin', $response->get('')['response'][0]->account->login);
self::assertGreaterThan(0, $response->get('')['response'][0]->getId());
}
@ -168,7 +167,7 @@ final class ApiControllerTest extends \PHPUnit\Framework\TestCase
]);
$this->module->apiSettingsAccountImageSet($request, $response);
$image = ProfileMapper::get($response->get('')['response']->getId())->image;
$image = ProfileMapper::get()->with('image')->where('id', $response->get('')['response']->getId())->execute()->image;
self::assertEquals('Profile Logo', $image->name);
}

View File

@ -39,26 +39,25 @@ final class ProfileMapperTest extends \PHPUnit\Framework\TestCase
$media->extension = 'png';
$media->name = 'Image';
if (($profile = ProfileMapper::getFor(1, 'account'))->getId() === 0) {
if (($profile = ProfileMapper::get()->where('account', 1)->execute())->getId() === 0) {
$profile = new Profile();
$profile->account = AccountMapper::get(1);
$profile->account = AccountMapper::get()->where('id', 1)->execute();
$profile->image = $media;
$profile->birthday = new \DateTime('now');
$id = ProfileMapper::create($profile);
$id = ProfileMapper::create()->execute($profile);
self::assertGreaterThan(0, $profile->getId());
self::assertEquals($id, $profile->getId());
} else {
$profile->image = $media;
$profile->birthday = new \DateTime('now');
ProfileMapper::update($profile);
ProfileMapper::update()->with('image')->execute($profile);
}
$profileR = ProfileMapper::get($profile->getId());
$profileR = ProfileMapper::get()->with('image')->with('account')->where('id', $profile->getId())->execute();
self::assertEquals($profile->birthday->format('Y-m-d'), $profileR->birthday->format('Y-m-d'));
self::assertEquals($profile->image->name, $profileR->image->name);
self::assertEquals($profile->account->name1, $profileR->account->name1);
}
}