forked from joomla/super-powers
Update 2024-06-22 4:38:12
This commit is contained in:
parent
f2eb3a484a
commit
6ae30a020c
@ -23,7 +23,7 @@ class Items << (F,LightGreen) >> #RoyalBlue {
|
|||||||
+ get(array $values, string $key = 'guid') : ?array
|
+ get(array $values, string $key = 'guid') : ?array
|
||||||
+ values(array $values, string $key = 'guid', ...) : ?array
|
+ values(array $values, string $key = 'guid', ...) : ?array
|
||||||
+ set(array $items, string $key = 'guid') : bool
|
+ set(array $items, string $key = 'guid') : bool
|
||||||
+ delete(string $values, string $key = 'guid') : bool
|
+ delete(array $values, string $key = 'guid') : bool
|
||||||
+ getTable() : string
|
+ getTable() : string
|
||||||
- insert(array $items) : bool
|
- insert(array $items) : bool
|
||||||
- update(array $items, string $key) : bool
|
- update(array $items, string $key) : bool
|
||||||
|
@ -202,7 +202,7 @@ final class Items implements ItemsInterface
|
|||||||
* @return bool
|
* @return bool
|
||||||
* @since 3.2.2
|
* @since 3.2.2
|
||||||
*/
|
*/
|
||||||
public function delete(string $values, string $key = 'guid'): bool
|
public function delete(array $values, string $key = 'guid'): bool
|
||||||
{
|
{
|
||||||
return $this->delete->table($this->getTable())->items([$key => ['operator' => 'IN', 'value' => $values]]);
|
return $this->delete->table($this->getTable())->items([$key => ['operator' => 'IN', 'value' => $values]]);
|
||||||
}
|
}
|
||||||
|
@ -173,7 +173,7 @@
|
|||||||
* @return bool
|
* @return bool
|
||||||
* @since 3.2.2
|
* @since 3.2.2
|
||||||
*/
|
*/
|
||||||
public function delete(string $values, string $key = 'guid'): bool
|
public function delete(array $values, string $key = 'guid'): bool
|
||||||
{
|
{
|
||||||
return $this->delete->table($this->getTable())->items([$key => ['operator' => 'IN', 'value' => $values]]);
|
return $this->delete->table($this->getTable())->items([$key => ['operator' => 'IN', 'value' => $values]]);
|
||||||
}
|
}
|
||||||
|
@ -13,8 +13,8 @@
|
|||||||
@startuml
|
@startuml
|
||||||
interface SubformInterface #Lavender {
|
interface SubformInterface #Lavender {
|
||||||
+ table(string $table) : self
|
+ table(string $table) : self
|
||||||
+ get(string $value, string $key, ...) : ?array
|
+ get(string $linkValue, string $linkKey, ...) : ?array
|
||||||
+ set(array $items, string $key) : bool
|
+ set(array $items, string $indexKey, ...) : bool
|
||||||
+ getTable() : string
|
+ getTable() : string
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -32,8 +32,8 @@ note right of SubformInterface::get
|
|||||||
return: ?array
|
return: ?array
|
||||||
|
|
||||||
arguments:
|
arguments:
|
||||||
string $value
|
string $linkValue
|
||||||
string $key
|
string $linkKey
|
||||||
string $field
|
string $field
|
||||||
array $set
|
array $set
|
||||||
end note
|
end note
|
||||||
@ -43,6 +43,12 @@ note right of SubformInterface::set
|
|||||||
|
|
||||||
since: 3.2.2
|
since: 3.2.2
|
||||||
return: bool
|
return: bool
|
||||||
|
|
||||||
|
arguments:
|
||||||
|
array $items
|
||||||
|
string $indexKey
|
||||||
|
string $linkKey
|
||||||
|
string $linkValue
|
||||||
end note
|
end note
|
||||||
|
|
||||||
note right of SubformInterface::getTable
|
note right of SubformInterface::getTable
|
||||||
|
@ -32,26 +32,28 @@ interface SubformInterface
|
|||||||
/**
|
/**
|
||||||
* Get a subform items
|
* Get a subform items
|
||||||
*
|
*
|
||||||
* @param string $value The ids/values of the parent
|
* @param string $linkValue The value of the link key in child table.
|
||||||
* @param string $key The parent key on which the items are linked
|
* @param string $linkKey The link key on which the items where linked in the child table.
|
||||||
* @param string $field The parent field name of the subform
|
* @param string $field The parent field name of the subform in the parent view.
|
||||||
* @param array $set The array SET of the keys of each row in the subform
|
* @param array $set The array SET of the keys of each row in the subform.
|
||||||
*
|
*
|
||||||
* @return array|null The subform
|
* @return array|null The subform
|
||||||
* @since 3.2.2
|
* @since 3.2.2
|
||||||
*/
|
*/
|
||||||
public function get(string $value, string $key, string $field, array $set): ?array;
|
public function get(string $linkValue, string $linkKey, string $field, array $set): ?array;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set a subform items
|
* Set a subform items
|
||||||
*
|
*
|
||||||
* @param array $items The list of items to set
|
* @param array $items The list of items from the subform to set
|
||||||
* @param string $key The child key on which the items should be linked
|
* @param string $indexKey The index key on which the items should be observed as it relates to insert/update/delete.
|
||||||
|
* @param string $linkKey The link key on which the items where linked in the child table.
|
||||||
|
* @param string $linkValue The value of the link key in child table.
|
||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
* @since 3.2.2
|
* @since 3.2.2
|
||||||
*/
|
*/
|
||||||
public function set(array $items, string $key): bool;
|
public function set(array $items, string $indexKey, string $linkKey, string $linkValue): bool;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the current active table
|
* Get the current active table
|
||||||
|
@ -11,26 +11,28 @@
|
|||||||
/**
|
/**
|
||||||
* Get a subform items
|
* Get a subform items
|
||||||
*
|
*
|
||||||
* @param string $value The ids/values of the parent
|
* @param string $linkValue The value of the link key in child table.
|
||||||
* @param string $key The parent key on which the items are linked
|
* @param string $linkKey The link key on which the items where linked in the child table.
|
||||||
* @param string $field The parent field name of the subform
|
* @param string $field The parent field name of the subform in the parent view.
|
||||||
* @param array $set The array SET of the keys of each row in the subform
|
* @param array $set The array SET of the keys of each row in the subform.
|
||||||
*
|
*
|
||||||
* @return array|null The subform
|
* @return array|null The subform
|
||||||
* @since 3.2.2
|
* @since 3.2.2
|
||||||
*/
|
*/
|
||||||
public function get(string $value, string $key, string $field, array $set): ?array;
|
public function get(string $linkValue, string $linkKey, string $field, array $set): ?array;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set a subform items
|
* Set a subform items
|
||||||
*
|
*
|
||||||
* @param array $items The list of items to set
|
* @param array $items The list of items from the subform to set
|
||||||
* @param string $key The child key on which the items should be linked
|
* @param string $indexKey The index key on which the items should be observed as it relates to insert/update/delete.
|
||||||
|
* @param string $linkKey The link key on which the items where linked in the child table.
|
||||||
|
* @param string $linkValue The value of the link key in child table.
|
||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
* @since 3.2.2
|
* @since 3.2.2
|
||||||
*/
|
*/
|
||||||
public function set(array $items, string $key): bool;
|
public function set(array $items, string $indexKey, string $linkKey, string $linkValue): bool;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the current active table
|
* Get the current active table
|
||||||
|
@ -16,7 +16,7 @@ interface ItemsInterface #Lavender {
|
|||||||
+ get(array $values, string $key = 'guid') : ?array
|
+ get(array $values, string $key = 'guid') : ?array
|
||||||
+ values(array $values, string $key = 'guid', ...) : ?array
|
+ values(array $values, string $key = 'guid', ...) : ?array
|
||||||
+ set(array $items, string $key = 'guid') : bool
|
+ set(array $items, string $key = 'guid') : bool
|
||||||
+ delete(string $values, string $key = 'guid') : bool
|
+ delete(array $values, string $key = 'guid') : bool
|
||||||
+ getTable() : string
|
+ getTable() : string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -72,7 +72,7 @@ interface ItemsInterface
|
|||||||
* @return bool
|
* @return bool
|
||||||
* @since 3.2.2
|
* @since 3.2.2
|
||||||
*/
|
*/
|
||||||
public function delete(string $values, string $key = 'guid'): bool;
|
public function delete(array $values, string $key = 'guid'): bool;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the current active table
|
* Get the current active table
|
||||||
|
@ -51,7 +51,7 @@
|
|||||||
* @return bool
|
* @return bool
|
||||||
* @since 3.2.2
|
* @since 3.2.2
|
||||||
*/
|
*/
|
||||||
public function delete(string $values, string $key = 'guid'): bool;
|
public function delete(array $values, string $key = 'guid'): bool;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the current active table
|
* Get the current active table
|
||||||
|
@ -16,11 +16,12 @@ class Subform #Gold {
|
|||||||
# string $table
|
# string $table
|
||||||
+ __construct(Items $items, ?string $table = null)
|
+ __construct(Items $items, ?string $table = null)
|
||||||
+ table(string $table) : self
|
+ table(string $table) : self
|
||||||
+ get(string $value, string $key, ...) : ?array
|
+ get(string $linkValue, string $linkKey, ...) : ?array
|
||||||
+ set(array $items, string $key) : bool
|
+ set(array $items, string $indexKey, ...) : bool
|
||||||
+ getTable() : string
|
+ getTable() : string
|
||||||
|
- purge(array $items, string $indexKey, ...) : void
|
||||||
- converter(array $items, array $keySet, ...) : array
|
- converter(array $items, array $keySet, ...) : array
|
||||||
- process(array $items, string $key) : array
|
- process(array $items, string $indexKey, ...) : array
|
||||||
- setGuid(string $key, bool $trim = true) : string
|
- setGuid(string $key, bool $trim = true) : string
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -44,8 +45,8 @@ note right of Subform::get
|
|||||||
return: ?array
|
return: ?array
|
||||||
|
|
||||||
arguments:
|
arguments:
|
||||||
string $value
|
string $linkValue
|
||||||
string $key
|
string $linkKey
|
||||||
string $field
|
string $field
|
||||||
array $set
|
array $set
|
||||||
end note
|
end note
|
||||||
@ -55,6 +56,12 @@ note left of Subform::set
|
|||||||
|
|
||||||
since: 3.2.2
|
since: 3.2.2
|
||||||
return: bool
|
return: bool
|
||||||
|
|
||||||
|
arguments:
|
||||||
|
array $items
|
||||||
|
string $indexKey
|
||||||
|
string $linkKey
|
||||||
|
string $linkValue
|
||||||
end note
|
end note
|
||||||
|
|
||||||
note right of Subform::getTable
|
note right of Subform::getTable
|
||||||
@ -64,7 +71,20 @@ note right of Subform::getTable
|
|||||||
return: string
|
return: string
|
||||||
end note
|
end note
|
||||||
|
|
||||||
note left of Subform::converter
|
note left of Subform::purge
|
||||||
|
Purge all items no longer in subform
|
||||||
|
|
||||||
|
since: 3.2.2
|
||||||
|
return: void
|
||||||
|
|
||||||
|
arguments:
|
||||||
|
array $items
|
||||||
|
string $indexKey
|
||||||
|
string $linkKey
|
||||||
|
string $linkValue
|
||||||
|
end note
|
||||||
|
|
||||||
|
note right of Subform::converter
|
||||||
Filters the specified keys from an array of objects or arrays, converts them to arrays,
|
Filters the specified keys from an array of objects or arrays, converts them to arrays,
|
||||||
and sets them by association with a specified key and an incrementing integer.
|
and sets them by association with a specified key and an incrementing integer.
|
||||||
|
|
||||||
@ -74,17 +94,23 @@ and sets them by association with a specified key and an incrementing integer.
|
|||||||
arguments:
|
arguments:
|
||||||
array $items
|
array $items
|
||||||
array $keySet
|
array $keySet
|
||||||
string $key
|
string $field
|
||||||
end note
|
end note
|
||||||
|
|
||||||
note right of Subform::process
|
note left of Subform::process
|
||||||
Processes an array of arrays based on the specified key.
|
Processes an array of arrays based on the specified key.
|
||||||
|
|
||||||
since: 3.2.2
|
since: 3.2.2
|
||||||
return: array
|
return: array
|
||||||
|
|
||||||
|
arguments:
|
||||||
|
array $items
|
||||||
|
string $indexKey
|
||||||
|
string $linkKey
|
||||||
|
string $linkValue
|
||||||
end note
|
end note
|
||||||
|
|
||||||
note left of Subform::setGuid
|
note right of Subform::setGuid
|
||||||
Returns a GUIDv4 string
|
Returns a GUIDv4 string
|
||||||
Thanks to Dave Pearson (and other)
|
Thanks to Dave Pearson (and other)
|
||||||
https://www.php.net/manual/en/function.com-create-guid.php#119168
|
https://www.php.net/manual/en/function.com-create-guid.php#119168
|
||||||
|
@ -17,7 +17,7 @@ use VDM\Joomla\Interfaces\Data\SubformInterface;
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Store the data of a sub-form
|
* CRUD the data of any sub-form to another view (table)
|
||||||
*
|
*
|
||||||
* @since 3.2.2
|
* @since 3.2.2
|
||||||
*/
|
*/
|
||||||
@ -74,17 +74,17 @@ class Subform implements SubformInterface
|
|||||||
/**
|
/**
|
||||||
* Get a subform items
|
* Get a subform items
|
||||||
*
|
*
|
||||||
* @param string $value The ids/values of the parent
|
* @param string $linkValue The value of the link key in child table.
|
||||||
* @param string $key The parent key on which the items are linked
|
* @param string $linkKey The link key on which the items where linked in the child table.
|
||||||
* @param string $field The parent field name of the subform
|
* @param string $field The parent field name of the subform in the parent view.
|
||||||
* @param array $set The array SET of the keys of each row in the subform
|
* @param array $set The array SET of the keys of each row in the subform.
|
||||||
*
|
*
|
||||||
* @return array|null The subform
|
* @return array|null The subform
|
||||||
* @since 3.2.2
|
* @since 3.2.2
|
||||||
*/
|
*/
|
||||||
public function get(string $value, string $key, string $field, array $set): ?array
|
public function get(string $linkValue, string $linkKey, string $field, array $set): ?array
|
||||||
{
|
{
|
||||||
if (($items = $this->items->table($this->getTable())->get([$value], $key)) !== null)
|
if (($items = $this->items->table($this->getTable())->get([$linkValue], $linkKey)) !== null)
|
||||||
{
|
{
|
||||||
return $this->converter($items, $set, $field);
|
return $this->converter($items, $set, $field);
|
||||||
}
|
}
|
||||||
@ -94,16 +94,22 @@ class Subform implements SubformInterface
|
|||||||
/**
|
/**
|
||||||
* Set a subform items
|
* Set a subform items
|
||||||
*
|
*
|
||||||
* @param array $items The list of items to set
|
* @param array $items The list of items from the subform to set
|
||||||
* @param string $key The child key on which the items should be linked
|
* @param string $indexKey The index key on which the items should be observed as it relates to insert/update/delete.
|
||||||
|
* @param string $linkKey The link key on which the items where linked in the child table.
|
||||||
|
* @param string $linkValue The value of the link key in child table.
|
||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
* @since 3.2.2
|
* @since 3.2.2
|
||||||
*/
|
*/
|
||||||
public function set(array $items, string $key): bool
|
public function set(array $items, string $indexKey, string $linkKey, string $linkValue): bool
|
||||||
{
|
{
|
||||||
|
$items = $this->process($items, $indexKey, $linkKey, $linkValue);
|
||||||
|
|
||||||
|
$this->purge($items, $indexKey, $linkKey, $linkValue);
|
||||||
|
|
||||||
return $this->items->table($this->getTable())->set(
|
return $this->items->table($this->getTable())->set(
|
||||||
$this->process($items, $key), $key
|
$items, $indexKey
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -118,18 +124,52 @@ class Subform implements SubformInterface
|
|||||||
return $this->table;
|
return $this->table;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Purge all items no longer in subform
|
||||||
|
*
|
||||||
|
* @param array $items The list of items to set.
|
||||||
|
* @param string $indexKey The index key on which the items should be observed as it relates to insert/update/delete
|
||||||
|
* @param string $linkKey The link key on which the items where linked in the child table.
|
||||||
|
* @param string $linkValue The value of the link key in child table.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
* @since 3.2.2
|
||||||
|
*/
|
||||||
|
private function purge(array $items, string $indexKey, string $linkKey, string $linkValue): void
|
||||||
|
{
|
||||||
|
// Get the current index values from the database
|
||||||
|
$currentIndexValues = $this->items->table($this->getTable())->values([$linkValue], $linkKey, $indexKey);
|
||||||
|
|
||||||
|
if ($currentIndexValues !== null)
|
||||||
|
{
|
||||||
|
// Extract the index values from the items array
|
||||||
|
$activeIndexValues = array_values(array_map(function($item) use ($indexKey) {
|
||||||
|
return $item[$indexKey] ?? null;
|
||||||
|
}, $items));
|
||||||
|
|
||||||
|
// Find the index values that are no longer in the items array
|
||||||
|
$inactiveIndexValues = array_diff($currentIndexValues, $activeIndexValues);
|
||||||
|
|
||||||
|
// Delete the inactive index values
|
||||||
|
if (!empty($inactiveIndexValues))
|
||||||
|
{
|
||||||
|
$this->items->table($this->getTable())->delete($inactiveIndexValues, $indexKey);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Filters the specified keys from an array of objects or arrays, converts them to arrays,
|
* Filters the specified keys from an array of objects or arrays, converts them to arrays,
|
||||||
* and sets them by association with a specified key and an incrementing integer.
|
* and sets them by association with a specified key and an incrementing integer.
|
||||||
*
|
*
|
||||||
* @param array $items Array of objects or arrays to be filtered.
|
* @param array $items Array of objects or arrays to be filtered.
|
||||||
* @param array $keySet Array of keys to retain in each item.
|
* @param array $keySet Array of keys to retain in each item.
|
||||||
* @param string $key The key prefix for the resulting associative array.
|
* @param string $field The field prefix for the resulting associative array.
|
||||||
*
|
*
|
||||||
* @return array Array of filtered arrays set by association.
|
* @return array Array of filtered arrays set by association.
|
||||||
* @since 3.2.2
|
* @since 3.2.2
|
||||||
*/
|
*/
|
||||||
private function converter(array $items, array $keySet, string $key): array
|
private function converter(array $items, array $keySet, string $field): array
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Filters keys for a single item and converts it to an array.
|
* Filters keys for a single item and converts it to an array.
|
||||||
@ -144,7 +184,7 @@ class Subform implements SubformInterface
|
|||||||
$filteredArray = [];
|
$filteredArray = [];
|
||||||
foreach ($keySet as $key) {
|
foreach ($keySet as $key) {
|
||||||
if (is_object($item) && property_exists($item, $key)) {
|
if (is_object($item) && property_exists($item, $key)) {
|
||||||
$filteredArray[$key] = $item->$key;
|
$filteredArray[$key] = $item->{$key};
|
||||||
} elseif (is_array($item) && array_key_exists($key, $item)) {
|
} elseif (is_array($item) && array_key_exists($key, $item)) {
|
||||||
$filteredArray[$key] = $item[$key];
|
$filteredArray[$key] = $item[$key];
|
||||||
}
|
}
|
||||||
@ -156,7 +196,7 @@ class Subform implements SubformInterface
|
|||||||
foreach ($items as $index => $item)
|
foreach ($items as $index => $item)
|
||||||
{
|
{
|
||||||
$filteredArray = $filterKeys($item, $keySet);
|
$filteredArray = $filterKeys($item, $keySet);
|
||||||
$result[$key . $index] = $filteredArray;
|
$result[$field . $index] = $filteredArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
@ -166,33 +206,38 @@ class Subform implements SubformInterface
|
|||||||
* Processes an array of arrays based on the specified key.
|
* Processes an array of arrays based on the specified key.
|
||||||
*
|
*
|
||||||
* @param array $items Array of arrays to be processed.
|
* @param array $items Array of arrays to be processed.
|
||||||
* @param string $key The key to check and modify values.
|
* @param string $indexKey The index key on which the items should be observed as it relates to insert/update/delete
|
||||||
|
* @param string $linkKey The link key on which the items where linked in the child table.
|
||||||
|
* @param string $linkValue The value of the link key in child table.
|
||||||
*
|
*
|
||||||
* @return array The processed array of arrays.
|
* @return array The processed array of arrays.
|
||||||
* @since 3.2.2
|
* @since 3.2.2
|
||||||
*/
|
*/
|
||||||
private function process(array $items, string $key): array
|
private function process(array $items, string $indexKey, string $linkKey, string $linkValue): array
|
||||||
{
|
{
|
||||||
foreach ($items as &$item)
|
foreach ($items as &$item)
|
||||||
{
|
{
|
||||||
$value = $item[$key] ?? '';
|
$value = $item[$indexKey] ?? '';
|
||||||
switch ($key) {
|
switch ($indexKey) {
|
||||||
case 'guid':
|
case 'guid':
|
||||||
if (empty($value))
|
if (empty($value))
|
||||||
{
|
{
|
||||||
$item[$key] = $this->setGuid($key);
|
// set INDEX
|
||||||
|
$item[$indexKey] = $this->setGuid($indexKey);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'id':
|
case 'id':
|
||||||
if ($value === '')
|
if (empty($value))
|
||||||
{
|
{
|
||||||
$item[$key] = 0;
|
$item[$indexKey] = 0;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
// No action for other keys if empty
|
// No action for other keys if empty
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
// set LINK
|
||||||
|
$item[$linkKey] = $linkValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
return array_values($items);
|
return array_values($items);
|
||||||
|
@ -49,17 +49,17 @@
|
|||||||
/**
|
/**
|
||||||
* Get a subform items
|
* Get a subform items
|
||||||
*
|
*
|
||||||
* @param string $value The ids/values of the parent
|
* @param string $linkValue The value of the link key in child table.
|
||||||
* @param string $key The parent key on which the items are linked
|
* @param string $linkKey The link key on which the items where linked in the child table.
|
||||||
* @param string $field The parent field name of the subform
|
* @param string $field The parent field name of the subform in the parent view.
|
||||||
* @param array $set The array SET of the keys of each row in the subform
|
* @param array $set The array SET of the keys of each row in the subform.
|
||||||
*
|
*
|
||||||
* @return array|null The subform
|
* @return array|null The subform
|
||||||
* @since 3.2.2
|
* @since 3.2.2
|
||||||
*/
|
*/
|
||||||
public function get(string $value, string $key, string $field, array $set): ?array
|
public function get(string $linkValue, string $linkKey, string $field, array $set): ?array
|
||||||
{
|
{
|
||||||
if (($items = $this->items->table($this->getTable())->get([$value], $key)) !== null)
|
if (($items = $this->items->table($this->getTable())->get([$linkValue], $linkKey)) !== null)
|
||||||
{
|
{
|
||||||
return $this->converter($items, $set, $field);
|
return $this->converter($items, $set, $field);
|
||||||
}
|
}
|
||||||
@ -69,16 +69,22 @@
|
|||||||
/**
|
/**
|
||||||
* Set a subform items
|
* Set a subform items
|
||||||
*
|
*
|
||||||
* @param array $items The list of items to set
|
* @param array $items The list of items from the subform to set
|
||||||
* @param string $key The child key on which the items should be linked
|
* @param string $indexKey The index key on which the items should be observed as it relates to insert/update/delete.
|
||||||
|
* @param string $linkKey The link key on which the items where linked in the child table.
|
||||||
|
* @param string $linkValue The value of the link key in child table.
|
||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
* @since 3.2.2
|
* @since 3.2.2
|
||||||
*/
|
*/
|
||||||
public function set(array $items, string $key): bool
|
public function set(array $items, string $indexKey, string $linkKey, string $linkValue): bool
|
||||||
{
|
{
|
||||||
|
$items = $this->process($items, $indexKey, $linkKey, $linkValue);
|
||||||
|
|
||||||
|
$this->purge($items, $indexKey, $linkKey, $linkValue);
|
||||||
|
|
||||||
return $this->items->table($this->getTable())->set(
|
return $this->items->table($this->getTable())->set(
|
||||||
$this->process($items, $key), $key
|
$items, $indexKey
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -93,18 +99,52 @@
|
|||||||
return $this->table;
|
return $this->table;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Purge all items no longer in subform
|
||||||
|
*
|
||||||
|
* @param array $items The list of items to set.
|
||||||
|
* @param string $indexKey The index key on which the items should be observed as it relates to insert/update/delete
|
||||||
|
* @param string $linkKey The link key on which the items where linked in the child table.
|
||||||
|
* @param string $linkValue The value of the link key in child table.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
* @since 3.2.2
|
||||||
|
*/
|
||||||
|
private function purge(array $items, string $indexKey, string $linkKey, string $linkValue): void
|
||||||
|
{
|
||||||
|
// Get the current index values from the database
|
||||||
|
$currentIndexValues = $this->items->table($this->getTable())->values([$linkValue], $linkKey, $indexKey);
|
||||||
|
|
||||||
|
if ($currentIndexValues !== null)
|
||||||
|
{
|
||||||
|
// Extract the index values from the items array
|
||||||
|
$activeIndexValues = array_values(array_map(function($item) use ($indexKey) {
|
||||||
|
return $item[$indexKey] ?? null;
|
||||||
|
}, $items));
|
||||||
|
|
||||||
|
// Find the index values that are no longer in the items array
|
||||||
|
$inactiveIndexValues = array_diff($currentIndexValues, $activeIndexValues);
|
||||||
|
|
||||||
|
// Delete the inactive index values
|
||||||
|
if (!empty($inactiveIndexValues))
|
||||||
|
{
|
||||||
|
$this->items->table($this->getTable())->delete($inactiveIndexValues, $indexKey);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Filters the specified keys from an array of objects or arrays, converts them to arrays,
|
* Filters the specified keys from an array of objects or arrays, converts them to arrays,
|
||||||
* and sets them by association with a specified key and an incrementing integer.
|
* and sets them by association with a specified key and an incrementing integer.
|
||||||
*
|
*
|
||||||
* @param array $items Array of objects or arrays to be filtered.
|
* @param array $items Array of objects or arrays to be filtered.
|
||||||
* @param array $keySet Array of keys to retain in each item.
|
* @param array $keySet Array of keys to retain in each item.
|
||||||
* @param string $key The key prefix for the resulting associative array.
|
* @param string $field The field prefix for the resulting associative array.
|
||||||
*
|
*
|
||||||
* @return array Array of filtered arrays set by association.
|
* @return array Array of filtered arrays set by association.
|
||||||
* @since 3.2.2
|
* @since 3.2.2
|
||||||
*/
|
*/
|
||||||
private function converter(array $items, array $keySet, string $key): array
|
private function converter(array $items, array $keySet, string $field): array
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Filters keys for a single item and converts it to an array.
|
* Filters keys for a single item and converts it to an array.
|
||||||
@ -119,7 +159,7 @@
|
|||||||
$filteredArray = [];
|
$filteredArray = [];
|
||||||
foreach ($keySet as $key) {
|
foreach ($keySet as $key) {
|
||||||
if (is_object($item) && property_exists($item, $key)) {
|
if (is_object($item) && property_exists($item, $key)) {
|
||||||
$filteredArray[$key] = $item->$key;
|
$filteredArray[$key] = $item->{$key};
|
||||||
} elseif (is_array($item) && array_key_exists($key, $item)) {
|
} elseif (is_array($item) && array_key_exists($key, $item)) {
|
||||||
$filteredArray[$key] = $item[$key];
|
$filteredArray[$key] = $item[$key];
|
||||||
}
|
}
|
||||||
@ -131,7 +171,7 @@
|
|||||||
foreach ($items as $index => $item)
|
foreach ($items as $index => $item)
|
||||||
{
|
{
|
||||||
$filteredArray = $filterKeys($item, $keySet);
|
$filteredArray = $filterKeys($item, $keySet);
|
||||||
$result[$key . $index] = $filteredArray;
|
$result[$field . $index] = $filteredArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
@ -141,33 +181,38 @@
|
|||||||
* Processes an array of arrays based on the specified key.
|
* Processes an array of arrays based on the specified key.
|
||||||
*
|
*
|
||||||
* @param array $items Array of arrays to be processed.
|
* @param array $items Array of arrays to be processed.
|
||||||
* @param string $key The key to check and modify values.
|
* @param string $indexKey The index key on which the items should be observed as it relates to insert/update/delete
|
||||||
|
* @param string $linkKey The link key on which the items where linked in the child table.
|
||||||
|
* @param string $linkValue The value of the link key in child table.
|
||||||
*
|
*
|
||||||
* @return array The processed array of arrays.
|
* @return array The processed array of arrays.
|
||||||
* @since 3.2.2
|
* @since 3.2.2
|
||||||
*/
|
*/
|
||||||
private function process(array $items, string $key): array
|
private function process(array $items, string $indexKey, string $linkKey, string $linkValue): array
|
||||||
{
|
{
|
||||||
foreach ($items as &$item)
|
foreach ($items as &$item)
|
||||||
{
|
{
|
||||||
$value = $item[$key] ?? '';
|
$value = $item[$indexKey] ?? '';
|
||||||
switch ($key) {
|
switch ($indexKey) {
|
||||||
case 'guid':
|
case 'guid':
|
||||||
if (empty($value))
|
if (empty($value))
|
||||||
{
|
{
|
||||||
$item[$key] = $this->setGuid($key);
|
// set INDEX
|
||||||
|
$item[$indexKey] = $this->setGuid($indexKey);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'id':
|
case 'id':
|
||||||
if ($value === '')
|
if (empty($value))
|
||||||
{
|
{
|
||||||
$item[$key] = 0;
|
$item[$indexKey] = 0;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
// No action for other keys if empty
|
// No action for other keys if empty
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
// set LINK
|
||||||
|
$item[$linkKey] = $linkValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
return array_values($items);
|
return array_values($items);
|
||||||
|
@ -19,7 +19,7 @@
|
|||||||
},
|
},
|
||||||
"extendsinterfaces": null,
|
"extendsinterfaces": null,
|
||||||
"namespace": "[[[NamespacePrefix]]]\\Joomla\\Data.Subform",
|
"namespace": "[[[NamespacePrefix]]]\\Joomla\\Data.Subform",
|
||||||
"description": "Store the data of a sub-form\r\n\r\n@since 3.2.2",
|
"description": "CRUD the data of any sub-form to another view (table)\r\n\r\n@since 3.2.2",
|
||||||
"licensing_template": "\/**\r\n * @package Joomla.Component.Builder\r\n *\r\n * @created 3rd September, 2020\r\n * @author Llewellyn van der Merwe <https:\/\/dev.vdm.io>\r\n * @git Joomla Component Builder <https:\/\/git.vdm.dev\/joomla\/Component-Builder>\r\n * @copyright Copyright (C) 2015 Vast Development Method. All rights reserved.\r\n * @license GNU General Public License version 2 or later; see LICENSE.txt\r\n *\/\r\n",
|
"licensing_template": "\/**\r\n * @package Joomla.Component.Builder\r\n *\r\n * @created 3rd September, 2020\r\n * @author Llewellyn van der Merwe <https:\/\/dev.vdm.io>\r\n * @git Joomla Component Builder <https:\/\/git.vdm.dev\/joomla\/Component-Builder>\r\n * @copyright Copyright (C) 2015 Vast Development Method. All rights reserved.\r\n * @license GNU General Public License version 2 or later; see LICENSE.txt\r\n *\/\r\n",
|
||||||
"head": "",
|
"head": "",
|
||||||
"composer": ""
|
"composer": ""
|
||||||
|
Loading…
Reference in New Issue
Block a user