From 6ae30a020c5758f28689ee51507d72968d419a2a Mon Sep 17 00:00:00 2001 From: aB0t Date: Sat, 22 Jun 2024 03:41:16 +0200 Subject: [PATCH] Update 2024-06-22 4:38:12 --- .../README.md | 2 +- .../code.php | 2 +- .../code.power | 2 +- .../README.md | 14 ++- .../code.php | 18 ++-- .../code.power | 18 ++-- .../README.md | 2 +- .../code.php | 2 +- .../code.power | 2 +- .../README.md | 44 +++++++-- .../code.php | 91 ++++++++++++++----- .../code.power | 89 +++++++++++++----- .../settings.json | 2 +- 13 files changed, 207 insertions(+), 81 deletions(-) diff --git a/src/21bca8a4-5b28-41c4-843e-8097f0ba7cca/README.md b/src/21bca8a4-5b28-41c4-843e-8097f0ba7cca/README.md index 8d20de9..1ff3cc3 100644 --- a/src/21bca8a4-5b28-41c4-843e-8097f0ba7cca/README.md +++ b/src/21bca8a4-5b28-41c4-843e-8097f0ba7cca/README.md @@ -23,7 +23,7 @@ class Items << (F,LightGreen) >> #RoyalBlue { + get(array $values, string $key = 'guid') : ?array + values(array $values, string $key = 'guid', ...) : ?array + set(array $items, string $key = 'guid') : bool - + delete(string $values, string $key = 'guid') : bool + + delete(array $values, string $key = 'guid') : bool + getTable() : string - insert(array $items) : bool - update(array $items, string $key) : bool diff --git a/src/21bca8a4-5b28-41c4-843e-8097f0ba7cca/code.php b/src/21bca8a4-5b28-41c4-843e-8097f0ba7cca/code.php index 2e960b2..b6fc1fb 100644 --- a/src/21bca8a4-5b28-41c4-843e-8097f0ba7cca/code.php +++ b/src/21bca8a4-5b28-41c4-843e-8097f0ba7cca/code.php @@ -202,7 +202,7 @@ final class Items implements ItemsInterface * @return bool * @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]]); } diff --git a/src/21bca8a4-5b28-41c4-843e-8097f0ba7cca/code.power b/src/21bca8a4-5b28-41c4-843e-8097f0ba7cca/code.power index 4d17647..34e12c4 100644 --- a/src/21bca8a4-5b28-41c4-843e-8097f0ba7cca/code.power +++ b/src/21bca8a4-5b28-41c4-843e-8097f0ba7cca/code.power @@ -173,7 +173,7 @@ * @return bool * @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]]); } diff --git a/src/34959721-415b-4b5e-8002-3d1fc84b3b2b/README.md b/src/34959721-415b-4b5e-8002-3d1fc84b3b2b/README.md index 48f9ca9..f388fcf 100644 --- a/src/34959721-415b-4b5e-8002-3d1fc84b3b2b/README.md +++ b/src/34959721-415b-4b5e-8002-3d1fc84b3b2b/README.md @@ -13,8 +13,8 @@ @startuml interface SubformInterface #Lavender { + table(string $table) : self - + get(string $value, string $key, ...) : ?array - + set(array $items, string $key) : bool + + get(string $linkValue, string $linkKey, ...) : ?array + + set(array $items, string $indexKey, ...) : bool + getTable() : string } @@ -32,8 +32,8 @@ note right of SubformInterface::get return: ?array arguments: - string $value - string $key + string $linkValue + string $linkKey string $field array $set end note @@ -43,6 +43,12 @@ note right of SubformInterface::set since: 3.2.2 return: bool + + arguments: + array $items + string $indexKey + string $linkKey + string $linkValue end note note right of SubformInterface::getTable diff --git a/src/34959721-415b-4b5e-8002-3d1fc84b3b2b/code.php b/src/34959721-415b-4b5e-8002-3d1fc84b3b2b/code.php index 6b95ebc..ce42642 100644 --- a/src/34959721-415b-4b5e-8002-3d1fc84b3b2b/code.php +++ b/src/34959721-415b-4b5e-8002-3d1fc84b3b2b/code.php @@ -32,26 +32,28 @@ interface SubformInterface /** * Get a subform items * - * @param string $value The ids/values of the parent - * @param string $key The parent key on which the items are linked - * @param string $field The parent field name of the subform - * @param array $set The array SET of the keys of each row in the subform + * @param string $linkValue The value of the link key in child table. + * @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 in the parent view. + * @param array $set The array SET of the keys of each row in the subform. * * @return array|null The subform * @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 * - * @param array $items The list of items to set - * @param string $key The child key on which the items should be linked + * @param array $items The list of items from the subform 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 bool * @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 diff --git a/src/34959721-415b-4b5e-8002-3d1fc84b3b2b/code.power b/src/34959721-415b-4b5e-8002-3d1fc84b3b2b/code.power index 8a3f646..85b5618 100644 --- a/src/34959721-415b-4b5e-8002-3d1fc84b3b2b/code.power +++ b/src/34959721-415b-4b5e-8002-3d1fc84b3b2b/code.power @@ -11,26 +11,28 @@ /** * Get a subform items * - * @param string $value The ids/values of the parent - * @param string $key The parent key on which the items are linked - * @param string $field The parent field name of the subform - * @param array $set The array SET of the keys of each row in the subform + * @param string $linkValue The value of the link key in child table. + * @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 in the parent view. + * @param array $set The array SET of the keys of each row in the subform. * * @return array|null The subform * @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 * - * @param array $items The list of items to set - * @param string $key The child key on which the items should be linked + * @param array $items The list of items from the subform 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 bool * @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 diff --git a/src/7212e4db-371f-4cfd-8122-32e9bb100d83/README.md b/src/7212e4db-371f-4cfd-8122-32e9bb100d83/README.md index cf60bab..be1a092 100644 --- a/src/7212e4db-371f-4cfd-8122-32e9bb100d83/README.md +++ b/src/7212e4db-371f-4cfd-8122-32e9bb100d83/README.md @@ -16,7 +16,7 @@ interface ItemsInterface #Lavender { + get(array $values, string $key = 'guid') : ?array + values(array $values, string $key = 'guid', ...) : ?array + set(array $items, string $key = 'guid') : bool - + delete(string $values, string $key = 'guid') : bool + + delete(array $values, string $key = 'guid') : bool + getTable() : string } diff --git a/src/7212e4db-371f-4cfd-8122-32e9bb100d83/code.php b/src/7212e4db-371f-4cfd-8122-32e9bb100d83/code.php index 62e2f08..408d132 100644 --- a/src/7212e4db-371f-4cfd-8122-32e9bb100d83/code.php +++ b/src/7212e4db-371f-4cfd-8122-32e9bb100d83/code.php @@ -72,7 +72,7 @@ interface ItemsInterface * @return bool * @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 diff --git a/src/7212e4db-371f-4cfd-8122-32e9bb100d83/code.power b/src/7212e4db-371f-4cfd-8122-32e9bb100d83/code.power index 4723bb1..c442180 100644 --- a/src/7212e4db-371f-4cfd-8122-32e9bb100d83/code.power +++ b/src/7212e4db-371f-4cfd-8122-32e9bb100d83/code.power @@ -51,7 +51,7 @@ * @return bool * @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 diff --git a/src/85785701-07b2-4f81-bc1e-0f423700c254/README.md b/src/85785701-07b2-4f81-bc1e-0f423700c254/README.md index 4c4eb31..a73ac45 100644 --- a/src/85785701-07b2-4f81-bc1e-0f423700c254/README.md +++ b/src/85785701-07b2-4f81-bc1e-0f423700c254/README.md @@ -16,11 +16,12 @@ class Subform #Gold { # string $table + __construct(Items $items, ?string $table = null) + table(string $table) : self - + get(string $value, string $key, ...) : ?array - + set(array $items, string $key) : bool + + get(string $linkValue, string $linkKey, ...) : ?array + + set(array $items, string $indexKey, ...) : bool + getTable() : string + - purge(array $items, string $indexKey, ...) : void - 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 } @@ -44,8 +45,8 @@ note right of Subform::get return: ?array arguments: - string $value - string $key + string $linkValue + string $linkKey string $field array $set end note @@ -55,6 +56,12 @@ note left of Subform::set since: 3.2.2 return: bool + + arguments: + array $items + string $indexKey + string $linkKey + string $linkValue end note note right of Subform::getTable @@ -64,7 +71,20 @@ note right of Subform::getTable return: string 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, 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: array $items array $keySet - string $key + string $field end note -note right of Subform::process +note left of Subform::process Processes an array of arrays based on the specified key. since: 3.2.2 return: array + + arguments: + array $items + string $indexKey + string $linkKey + string $linkValue end note -note left of Subform::setGuid +note right of Subform::setGuid Returns a GUIDv4 string Thanks to Dave Pearson (and other) https://www.php.net/manual/en/function.com-create-guid.php#119168 diff --git a/src/85785701-07b2-4f81-bc1e-0f423700c254/code.php b/src/85785701-07b2-4f81-bc1e-0f423700c254/code.php index 168acfe..0568dd5 100644 --- a/src/85785701-07b2-4f81-bc1e-0f423700c254/code.php +++ b/src/85785701-07b2-4f81-bc1e-0f423700c254/code.php @@ -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 */ @@ -74,17 +74,17 @@ class Subform implements SubformInterface /** * Get a subform items * - * @param string $value The ids/values of the parent - * @param string $key The parent key on which the items are linked - * @param string $field The parent field name of the subform - * @param array $set The array SET of the keys of each row in the subform + * @param string $linkValue The value of the link key in child table. + * @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 in the parent view. + * @param array $set The array SET of the keys of each row in the subform. * * @return array|null The subform * @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); } @@ -94,16 +94,22 @@ class Subform implements SubformInterface /** * Set a subform items * - * @param array $items The list of items to set - * @param string $key The child key on which the items should be linked + * @param array $items The list of items from the subform 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 bool * @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( - $this->process($items, $key), $key + $items, $indexKey ); } @@ -118,18 +124,52 @@ class Subform implements SubformInterface 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, * 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 $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. * @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. @@ -144,7 +184,7 @@ class Subform implements SubformInterface $filteredArray = []; foreach ($keySet as $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)) { $filteredArray[$key] = $item[$key]; } @@ -156,7 +196,7 @@ class Subform implements SubformInterface foreach ($items as $index => $item) { $filteredArray = $filterKeys($item, $keySet); - $result[$key . $index] = $filteredArray; + $result[$field . $index] = $filteredArray; } return $result; @@ -165,34 +205,39 @@ class Subform implements SubformInterface /** * Processes an array of arrays based on the specified key. * - * @param array $items Array of arrays to be processed. - * @param string $key The key to check and modify values. + * @param array $items Array of arrays to be processed. + * @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. * @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) { - $value = $item[$key] ?? ''; - switch ($key) { + $value = $item[$indexKey] ?? ''; + switch ($indexKey) { case 'guid': if (empty($value)) { - $item[$key] = $this->setGuid($key); + // set INDEX + $item[$indexKey] = $this->setGuid($indexKey); } break; case 'id': - if ($value === '') + if (empty($value)) { - $item[$key] = 0; + $item[$indexKey] = 0; } break; default: // No action for other keys if empty break; } + // set LINK + $item[$linkKey] = $linkValue; } return array_values($items); diff --git a/src/85785701-07b2-4f81-bc1e-0f423700c254/code.power b/src/85785701-07b2-4f81-bc1e-0f423700c254/code.power index a428546..2ac08d8 100644 --- a/src/85785701-07b2-4f81-bc1e-0f423700c254/code.power +++ b/src/85785701-07b2-4f81-bc1e-0f423700c254/code.power @@ -49,17 +49,17 @@ /** * Get a subform items * - * @param string $value The ids/values of the parent - * @param string $key The parent key on which the items are linked - * @param string $field The parent field name of the subform - * @param array $set The array SET of the keys of each row in the subform + * @param string $linkValue The value of the link key in child table. + * @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 in the parent view. + * @param array $set The array SET of the keys of each row in the subform. * * @return array|null The subform * @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); } @@ -69,16 +69,22 @@ /** * Set a subform items * - * @param array $items The list of items to set - * @param string $key The child key on which the items should be linked + * @param array $items The list of items from the subform 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 bool * @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( - $this->process($items, $key), $key + $items, $indexKey ); } @@ -93,18 +99,52 @@ 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, * 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 $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. * @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. @@ -119,7 +159,7 @@ $filteredArray = []; foreach ($keySet as $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)) { $filteredArray[$key] = $item[$key]; } @@ -131,7 +171,7 @@ foreach ($items as $index => $item) { $filteredArray = $filterKeys($item, $keySet); - $result[$key . $index] = $filteredArray; + $result[$field . $index] = $filteredArray; } return $result; @@ -140,34 +180,39 @@ /** * Processes an array of arrays based on the specified key. * - * @param array $items Array of arrays to be processed. - * @param string $key The key to check and modify values. + * @param array $items Array of arrays to be processed. + * @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. * @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) { - $value = $item[$key] ?? ''; - switch ($key) { + $value = $item[$indexKey] ?? ''; + switch ($indexKey) { case 'guid': if (empty($value)) { - $item[$key] = $this->setGuid($key); + // set INDEX + $item[$indexKey] = $this->setGuid($indexKey); } break; case 'id': - if ($value === '') + if (empty($value)) { - $item[$key] = 0; + $item[$indexKey] = 0; } break; default: // No action for other keys if empty break; } + // set LINK + $item[$linkKey] = $linkValue; } return array_values($items); diff --git a/src/85785701-07b2-4f81-bc1e-0f423700c254/settings.json b/src/85785701-07b2-4f81-bc1e-0f423700c254/settings.json index 0ebe116..713366e 100644 --- a/src/85785701-07b2-4f81-bc1e-0f423700c254/settings.json +++ b/src/85785701-07b2-4f81-bc1e-0f423700c254/settings.json @@ -19,7 +19,7 @@ }, "extendsinterfaces": null, "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 \r\n * @git 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": "", "composer": ""