mirror of
https://github.com/Karaka-Management/phpOMS.git
synced 2026-02-13 07:18:39 +00:00
Implemented language support
This commit is contained in:
parent
35248732ce
commit
6ce5a7b29f
|
|
@ -69,6 +69,14 @@ class DataMapperAbstract implements DataMapperInterface
|
||||||
*/
|
*/
|
||||||
protected static $createdAt = '';
|
protected static $createdAt = '';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Language
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
|
protected static $language_field = '';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Columns.
|
* Columns.
|
||||||
*
|
*
|
||||||
|
|
@ -474,6 +482,7 @@ class DataMapperAbstract implements DataMapperInterface
|
||||||
throw new \Exception('No mapper set for relation object.');
|
throw new \Exception('No mapper set for relation object.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @var DataMapperAbstract $mapper */
|
||||||
$mapper = static::$hasMany[$propertyName]['mapper'];
|
$mapper = static::$hasMany[$propertyName]['mapper'];
|
||||||
$objsIds = [];
|
$objsIds = [];
|
||||||
$relReflectionClass = null;
|
$relReflectionClass = null;
|
||||||
|
|
@ -501,6 +510,7 @@ class DataMapperAbstract implements DataMapperInterface
|
||||||
|
|
||||||
// Setting relation value (id) for relation (since the relation is not stored in an extra relation table)
|
// Setting relation value (id) for relation (since the relation is not stored in an extra relation table)
|
||||||
/** @var string $table */
|
/** @var string $table */
|
||||||
|
/** @var array $columns */
|
||||||
if (static::$hasMany[$propertyName]['table'] === static::$hasMany[$propertyName]['mapper']::$table) {
|
if (static::$hasMany[$propertyName]['table'] === static::$hasMany[$propertyName]['mapper']::$table) {
|
||||||
$relProperty = $relReflectionClass->getProperty($mapper::$columns[static::$hasMany[$propertyName]['dst']]['internal']);
|
$relProperty = $relReflectionClass->getProperty($mapper::$columns[static::$hasMany[$propertyName]['dst']]['internal']);
|
||||||
|
|
||||||
|
|
@ -524,7 +534,7 @@ class DataMapperAbstract implements DataMapperInterface
|
||||||
|
|
||||||
private static function createHasOne(\ReflectionClass $reflectionClass, $obj)
|
private static function createHasOne(\ReflectionClass $reflectionClass, $obj)
|
||||||
{
|
{
|
||||||
throw new \Excpetion();
|
throw new \Exception();
|
||||||
|
|
||||||
foreach (static::$hasOne as $propertyName => $rel) {
|
foreach (static::$hasOne as $propertyName => $rel) {
|
||||||
|
|
||||||
|
|
@ -547,6 +557,7 @@ class DataMapperAbstract implements DataMapperInterface
|
||||||
private static function createOwnsOne(string $propertyName, $obj)
|
private static function createOwnsOne(string $propertyName, $obj)
|
||||||
{
|
{
|
||||||
if (is_object($obj)) {
|
if (is_object($obj)) {
|
||||||
|
/** @var DataMapperAbstract $mapper */
|
||||||
$mapper = static::$ownsOne[$propertyName]['mapper'];
|
$mapper = static::$ownsOne[$propertyName]['mapper'];
|
||||||
$primaryKey = $mapper::getObjectId($obj);
|
$primaryKey = $mapper::getObjectId($obj);
|
||||||
|
|
||||||
|
|
@ -576,6 +587,7 @@ class DataMapperAbstract implements DataMapperInterface
|
||||||
private static function createBelongsTo(string $propertyName, $obj)
|
private static function createBelongsTo(string $propertyName, $obj)
|
||||||
{
|
{
|
||||||
if (is_object($obj)) {
|
if (is_object($obj)) {
|
||||||
|
/** @var DataMapperAbstract $mapper */
|
||||||
$mapper = static::$belongsTo[$propertyName]['mapper'];
|
$mapper = static::$belongsTo[$propertyName]['mapper'];
|
||||||
$primaryKey = $mapper::getObjectId($obj);
|
$primaryKey = $mapper::getObjectId($obj);
|
||||||
|
|
||||||
|
|
@ -695,6 +707,7 @@ class DataMapperAbstract implements DataMapperInterface
|
||||||
throw new \Exception('No mapper set for relation object.');
|
throw new \Exception('No mapper set for relation object.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @var DataMapperAbstract $mapper */
|
||||||
$mapper = static::$hasMany[$propertyName]['mapper'];
|
$mapper = static::$hasMany[$propertyName]['mapper'];
|
||||||
$objsIds = [];
|
$objsIds = [];
|
||||||
$relReflectionClass = null;
|
$relReflectionClass = null;
|
||||||
|
|
@ -721,6 +734,8 @@ class DataMapperAbstract implements DataMapperInterface
|
||||||
}
|
}
|
||||||
|
|
||||||
// create if not existing
|
// create if not existing
|
||||||
|
/** @var string $table */
|
||||||
|
/** @var array $columns */
|
||||||
if (static::$hasMany[$propertyName]['table'] === static::$hasMany[$propertyName]['mapper']::$table) {
|
if (static::$hasMany[$propertyName]['table'] === static::$hasMany[$propertyName]['mapper']::$table) {
|
||||||
$relProperty = $relReflectionClass->getProperty($mapper::$columns[static::$hasMany[$propertyName]['dst']]['internal']);
|
$relProperty = $relReflectionClass->getProperty($mapper::$columns[static::$hasMany[$propertyName]['dst']]['internal']);
|
||||||
|
|
||||||
|
|
@ -753,11 +768,14 @@ class DataMapperAbstract implements DataMapperInterface
|
||||||
*
|
*
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*
|
*
|
||||||
|
* @throws \Exception
|
||||||
|
*
|
||||||
* @since 1.0.0
|
* @since 1.0.0
|
||||||
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||||
*/
|
*/
|
||||||
private static function updateRelationTable(string $propertyName, array $objsIds, $objId)
|
private static function updateRelationTable(string $propertyName, array $objsIds, $objId)
|
||||||
{
|
{
|
||||||
|
/** @var string $table */
|
||||||
if (
|
if (
|
||||||
!empty($objsIds)
|
!empty($objsIds)
|
||||||
&& static::$hasMany[$propertyName]['table'] !== static::$table
|
&& static::$hasMany[$propertyName]['table'] !== static::$table
|
||||||
|
|
@ -796,6 +814,7 @@ class DataMapperAbstract implements DataMapperInterface
|
||||||
*/
|
*/
|
||||||
private static function deleteRelationTable(string $propertyName, array $objsIds, $objId)
|
private static function deleteRelationTable(string $propertyName, array $objsIds, $objId)
|
||||||
{
|
{
|
||||||
|
/** @var string $table */
|
||||||
if (
|
if (
|
||||||
!empty($objsIds)
|
!empty($objsIds)
|
||||||
&& static::$hasMany[$propertyName]['table'] !== static::$table
|
&& static::$hasMany[$propertyName]['table'] !== static::$table
|
||||||
|
|
@ -831,6 +850,7 @@ class DataMapperAbstract implements DataMapperInterface
|
||||||
private static function updateOwnsOne(string $propertyName, $obj)
|
private static function updateOwnsOne(string $propertyName, $obj)
|
||||||
{
|
{
|
||||||
if (is_object($obj)) {
|
if (is_object($obj)) {
|
||||||
|
/** @var DataMapperAbstract $mapper */
|
||||||
$mapper = static::$ownsOne[$propertyName]['mapper'];
|
$mapper = static::$ownsOne[$propertyName]['mapper'];
|
||||||
|
|
||||||
// todo: delete owned one object is not recommended since it can be owned by by something else? or does owns one mean that nothing else can have a relation to this one?
|
// todo: delete owned one object is not recommended since it can be owned by by something else? or does owns one mean that nothing else can have a relation to this one?
|
||||||
|
|
@ -857,6 +877,7 @@ class DataMapperAbstract implements DataMapperInterface
|
||||||
private static function updateBelongsTo(string $propertyName, $obj)
|
private static function updateBelongsTo(string $propertyName, $obj)
|
||||||
{
|
{
|
||||||
if (is_object($obj)) {
|
if (is_object($obj)) {
|
||||||
|
/** @var DataMapperAbstract $mapper */
|
||||||
$mapper = static::$belongsTo[$propertyName]['mapper'];
|
$mapper = static::$belongsTo[$propertyName]['mapper'];
|
||||||
|
|
||||||
return $mapper::update($obj);
|
return $mapper::update($obj);
|
||||||
|
|
@ -998,6 +1019,7 @@ class DataMapperAbstract implements DataMapperInterface
|
||||||
throw new \Exception('No mapper set for relation object.');
|
throw new \Exception('No mapper set for relation object.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @var DataMapperAbstract $mapper */
|
||||||
$mapper = static::$hasMany[$propertyName]['mapper'];
|
$mapper = static::$hasMany[$propertyName]['mapper'];
|
||||||
$objsIds = [];
|
$objsIds = [];
|
||||||
$relReflectionClass = null;
|
$relReflectionClass = null;
|
||||||
|
|
@ -1051,6 +1073,7 @@ class DataMapperAbstract implements DataMapperInterface
|
||||||
private static function deleteOwnsOne(string $propertyName, $obj)
|
private static function deleteOwnsOne(string $propertyName, $obj)
|
||||||
{
|
{
|
||||||
if (is_object($obj)) {
|
if (is_object($obj)) {
|
||||||
|
/** @var DataMapperAbstract $mapper */
|
||||||
$mapper = static::$ownsOne[$propertyName]['mapper'];
|
$mapper = static::$ownsOne[$propertyName]['mapper'];
|
||||||
|
|
||||||
// todo: delete owned one object is not recommended since it can be owned by by something else? or does owns one mean that nothing else can have a relation to this one?
|
// todo: delete owned one object is not recommended since it can be owned by by something else? or does owns one mean that nothing else can have a relation to this one?
|
||||||
|
|
@ -1076,6 +1099,7 @@ class DataMapperAbstract implements DataMapperInterface
|
||||||
private static function deleteBelongsTo(string $propertyName, $obj)
|
private static function deleteBelongsTo(string $propertyName, $obj)
|
||||||
{
|
{
|
||||||
if (is_object($obj)) {
|
if (is_object($obj)) {
|
||||||
|
/** @var DataMapperAbstract $mapper */
|
||||||
$mapper = static::$belongsTo[$propertyName]['mapper'];
|
$mapper = static::$belongsTo[$propertyName]['mapper'];
|
||||||
|
|
||||||
return $mapper::delete($obj);
|
return $mapper::delete($obj);
|
||||||
|
|
@ -1429,13 +1453,14 @@ class DataMapperAbstract implements DataMapperInterface
|
||||||
* @param mixed $primaryKey Key
|
* @param mixed $primaryKey Key
|
||||||
* @param int $relations Load relations
|
* @param int $relations Load relations
|
||||||
* @param mixed $fill Object to fill
|
* @param mixed $fill Object to fill
|
||||||
|
* @param string $lang Object language
|
||||||
*
|
*
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*
|
*
|
||||||
* @since 1.0.0
|
* @since 1.0.0
|
||||||
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||||
*/
|
*/
|
||||||
public static function get($primaryKey, int $relations = RelationType::ALL, $fill = null)
|
public static function get($primaryKey, int $relations = RelationType::ALL, $fill = null, string $lang = '')
|
||||||
{
|
{
|
||||||
self::extend(__CLASS__);
|
self::extend(__CLASS__);
|
||||||
|
|
||||||
|
|
@ -1451,14 +1476,14 @@ class DataMapperAbstract implements DataMapperInterface
|
||||||
next($fill);
|
next($fill);
|
||||||
}
|
}
|
||||||
|
|
||||||
$obj[$value] = self::populate(self::getRaw($value), $toFill);
|
$obj[$value] = self::populate(self::getRaw($value, $lang), $toFill);
|
||||||
|
|
||||||
if (method_exists($obj[$value], 'initialize')) {
|
if (method_exists($obj[$value], 'initialize')) {
|
||||||
$obj[$value]->initialize();
|
$obj[$value]->initialize();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self::fillRelations($obj, $relations);
|
self::fillRelations($obj, $relations, $lang);
|
||||||
self::clear();
|
self::clear();
|
||||||
|
|
||||||
return count($obj) === 1 ? reset($obj) : $obj;
|
return count($obj) === 1 ? reset($obj) : $obj;
|
||||||
|
|
@ -1599,13 +1624,14 @@ class DataMapperAbstract implements DataMapperInterface
|
||||||
*
|
*
|
||||||
* @param mixed $obj Objects to fill
|
* @param mixed $obj Objects to fill
|
||||||
* @param int $relations Relations type
|
* @param int $relations Relations type
|
||||||
|
* @param string $lang Language
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*
|
*
|
||||||
* @since 1.0.0
|
* @since 1.0.0
|
||||||
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||||
*/
|
*/
|
||||||
public static function fillRelations(array &$obj, int $relations = RelationType::ALL)
|
public static function fillRelations(array &$obj, int $relations = RelationType::ALL, string $lang = '')
|
||||||
{
|
{
|
||||||
$hasMany = !empty(static::$hasMany);
|
$hasMany = !empty(static::$hasMany);
|
||||||
$hasOne = !empty(static::$hasOne);
|
$hasOne = !empty(static::$hasOne);
|
||||||
|
|
@ -1617,7 +1643,7 @@ class DataMapperAbstract implements DataMapperInterface
|
||||||
/* loading relations from relations table and populating them and then adding them to the object */
|
/* loading relations from relations table and populating them and then adding them to the object */
|
||||||
if ($relations !== RelationType::NONE) {
|
if ($relations !== RelationType::NONE) {
|
||||||
if ($hasMany) {
|
if ($hasMany) {
|
||||||
self::populateManyToMany(self::getHasManyRaw($key, $relations), $obj[$key]);
|
self::populateManyToMany(self::getHasManyRaw($key, $relations), $obj[$key], $lang);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($hasOne) {
|
if ($hasOne) {
|
||||||
|
|
@ -1640,17 +1666,22 @@ class DataMapperAbstract implements DataMapperInterface
|
||||||
* Get object.
|
* Get object.
|
||||||
*
|
*
|
||||||
* @param mixed $primaryKey Key
|
* @param mixed $primaryKey Key
|
||||||
|
* @param string $lang Language
|
||||||
*
|
*
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*
|
*
|
||||||
* @since 1.0.0
|
* @since 1.0.0
|
||||||
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||||
*/
|
*/
|
||||||
public static function getRaw($primaryKey) : array
|
public static function getRaw($primaryKey, string $lang = '') : array
|
||||||
{
|
{
|
||||||
$query = self::getQuery();
|
$query = self::getQuery();
|
||||||
$query->where(static::$table . '.' . static::$primaryField, '=', $primaryKey);
|
$query->where(static::$table . '.' . static::$primaryField, '=', $primaryKey);
|
||||||
|
|
||||||
|
if (!empty(self::$language_field)) {
|
||||||
|
$query->where(static::$table . '.' . static::$language_field, '=', $lang, 'AND');
|
||||||
|
}
|
||||||
|
|
||||||
$sth = self::$db->con->prepare($query->toSql());
|
$sth = self::$db->con->prepare($query->toSql());
|
||||||
$sth->execute();
|
$sth->execute();
|
||||||
|
|
||||||
|
|
@ -1683,13 +1714,14 @@ class DataMapperAbstract implements DataMapperInterface
|
||||||
*
|
*
|
||||||
* @param mixed $primaryKey Primary key
|
* @param mixed $primaryKey Primary key
|
||||||
* @param int $relations Load relations
|
* @param int $relations Load relations
|
||||||
|
* @param string $lang Language
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*
|
*
|
||||||
* @since 1.0.0
|
* @since 1.0.0
|
||||||
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||||
*/
|
*/
|
||||||
public static function getHasManyRaw($primaryKey, int $relations = RelationType::ALL) : array
|
public static function getHasManyRaw($primaryKey, int $relations = RelationType::ALL, string $lang = '') : array
|
||||||
{
|
{
|
||||||
$result = [];
|
$result = [];
|
||||||
|
|
||||||
|
|
@ -1698,6 +1730,7 @@ class DataMapperAbstract implements DataMapperInterface
|
||||||
$query->prefix(self::$db->getPrefix());
|
$query->prefix(self::$db->getPrefix());
|
||||||
|
|
||||||
if ($relations === RelationType::ALL) {
|
if ($relations === RelationType::ALL) {
|
||||||
|
/** @var string $primaryField */
|
||||||
$src = $value['src'] ?? $value['mapper']::$primaryField;
|
$src = $value['src'] ?? $value['mapper']::$primaryField;
|
||||||
|
|
||||||
$query->select($value['table'] . '.' . $src)
|
$query->select($value['table'] . '.' . $src)
|
||||||
|
|
@ -1724,6 +1757,12 @@ class DataMapperAbstract implements DataMapperInterface
|
||||||
*/
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @var string $language_field */
|
||||||
|
if (!empty($value['mapper']::$language_field)) {
|
||||||
|
/** @var string $table */
|
||||||
|
$query->where($value['mapper']::$table . '.' . $value['mapper']::$language_field, '=', $lang, 'AND');
|
||||||
|
}
|
||||||
|
|
||||||
$sth = self::$db->con->prepare($query->toSql());
|
$sth = self::$db->con->prepare($query->toSql());
|
||||||
$sth->execute();
|
$sth->execute();
|
||||||
$result[$member] = $sth->fetchAll(\PDO::FETCH_COLUMN);
|
$result[$member] = $sth->fetchAll(\PDO::FETCH_COLUMN);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user