diff --git a/src/46b98346-ec98-42b3-a393-96c7d1282b1c/README.md b/src/46b98346-ec98-42b3-a393-96c7d1282b1c/README.md index 620e57e..4520cea 100644 --- a/src/46b98346-ec98-42b3-a393-96c7d1282b1c/README.md +++ b/src/46b98346-ec98-42b3-a393-96c7d1282b1c/README.md @@ -20,13 +20,17 @@ class UsersSubform << (F,LightGreen) >> #RoyalBlue { + get(string $linkValue, string $linkKey, ...) : ?array + set(mixed $items, string $indexKey, ...) : bool + getTable() : string - # initializeUserProperties() : void + - initializeUserProperties() : void - purge(array $items, string $indexKey, ...) : void - getUsersDetails(array $items) : array - getUserDetails(array $item) : void - converter(array $items, array $keySet, ...) : array - process(mixed $items, string $indexKey, ...) : array - setUserDetails(array $item) : int + - loadUser(array $item) : ?User + - extractUserDetails(array $item, ?User $user) : array + - assignUserGroups($details, ?User $user, ...) : void + - saveUserDetails(array $details, int $userId) : int } note right of UsersSubform::__construct @@ -136,7 +140,42 @@ note right of UsersSubform::process end note note left of UsersSubform::setUserDetails - Set the user details. + Handles setting user details and saving them. +This function retrieves the user by ID, sets the user details, +and adds appropriate user groups before saving the user. + + since: 5.0.2 + return: int +end note + +note right of UsersSubform::loadUser + Load the user based on the user ID from the item array. + + since: 5.0.2 + return: ?User +end note + +note left of UsersSubform::extractUserDetails + Extract user details from the item array and prepare them for saving. + + since: 5.0.2 + return: array +end note + +note right of UsersSubform::assignUserGroups + Assigns user groups based on existing groups and entity type. + + since: 5.0.2 + return: void + + arguments: + $details + ?User $user + array $item +end note + +note left of UsersSubform::saveUserDetails + Save the user details using UserHelper and handle exceptions. since: 5.0.2 return: int diff --git a/src/46b98346-ec98-42b3-a393-96c7d1282b1c/code.php b/src/46b98346-ec98-42b3-a393-96c7d1282b1c/code.php index 8bdd4f1..8ce8ac5 100644 --- a/src/46b98346-ec98-42b3-a393-96c7d1282b1c/code.php +++ b/src/46b98346-ec98-42b3-a393-96c7d1282b1c/code.php @@ -12,11 +12,13 @@ namespace VDM\Joomla\Data; -use Joomla\CMS\Factory; +use Joomla\CMS\Factory; +use Joomla\CMS\User\User; use VDM\Joomla\Interfaces\Data\ItemsInterface as Items; use VDM\Joomla\Data\Guid; use VDM\Joomla\Componentbuilder\Utilities\UserHelper; use VDM\Joomla\Componentbuilder\Utilities\Exception\NoUserIdFoundException; +use VDM\Joomla\Utilities\Component\Helper as Component; use VDM\Joomla\Interfaces\Data\GuidInterface; use VDM\Joomla\Interfaces\Data\SubformInterface; @@ -85,7 +87,7 @@ final class UsersSubform implements GuidInterface, SubformInterface * @param string $table The table that should be active * * @return self - * @since 3.2.2 + * @since 3.2.2 */ public function table(string $table): self { @@ -103,7 +105,7 @@ final class UsersSubform implements GuidInterface, SubformInterface * @param array $get The array get:set of the keys of each row in the subform. * * @return array|null The subform - * @since 3.2.2 + * @since 3.2.2 */ public function get(string $linkValue, string $linkKey, string $field, array $get): ?array { @@ -128,7 +130,7 @@ final class UsersSubform implements GuidInterface, SubformInterface * @param string $linkValue The value of the link key in child table. * * @return bool - * @since 3.2.2 + * @since 3.2.2 */ public function set(mixed $items, string $indexKey, string $linkKey, string $linkValue): bool { @@ -150,7 +152,7 @@ final class UsersSubform implements GuidInterface, SubformInterface * Get the current active table * * @return string - * @since 3.2.2 + * @since 3.2.2 */ public function getTable(): string { @@ -163,7 +165,7 @@ final class UsersSubform implements GuidInterface, SubformInterface * @return void * @since 5.0.2 */ - protected function initializeUserProperties(): void + private function initializeUserProperties(): void { $user = UserHelper::getUserById(0); @@ -187,7 +189,7 @@ final class UsersSubform implements GuidInterface, SubformInterface * @param string $linkValue The value of the link key in child table. * * @return void - * @since 3.2.2 + * @since 3.2.2 */ private function purge(array $items, string $indexKey, string $linkKey, string $linkValue): void { @@ -229,7 +231,7 @@ final class UsersSubform implements GuidInterface, SubformInterface * @param array $items Array of objects or arrays to be filtered. * * @return array - * @since 5.0.2 + * @since 5.0.2 */ private function getUsersDetails(array $items): array { @@ -248,7 +250,7 @@ final class UsersSubform implements GuidInterface, SubformInterface * @param array $item The user map array * * @return void - * @since 5.0.2 + * @since 5.0.2 */ private function getUserDetails(array &$item): void { @@ -285,7 +287,7 @@ final class UsersSubform implements GuidInterface, SubformInterface * @param string $field The field prefix for the resulting associative array. * * @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 $field): array { @@ -329,7 +331,7 @@ final class UsersSubform implements GuidInterface, SubformInterface * @param string $linkValue The value of the link key in child table. * * @return array The processed array of arrays. - * @since 3.2.2 + * @since 3.2.2 */ private function process($items, string $indexKey, string $linkKey, string $linkValue): array { @@ -365,27 +367,66 @@ final class UsersSubform implements GuidInterface, SubformInterface } /** - * Set the user details. + * Handles setting user details and saving them. * - * @param array $item The user details + * This function retrieves the user by ID, sets the user details, + * and adds appropriate user groups before saving the user. * - * @return int - * @since 5.0.2 + * @param array $item The user details passed by reference. + * + * @return int The ID of the saved user, or 0 on failure. + * @since 5.0.2 */ private function setUserDetails(array &$item): int { - $details = []; - $user = null; + $user = $this->loadUser($item); + $details = $this->extractUserDetails($item, $user); + $this->assignUserGroups($details, $user, $item); + + return $this->saveUserDetails($details, $details['id'] ?? 0); + } - // now load the user ID - if (isset($item['user_id']) && is_numeric($item['user_id']) && $item['user_id'] > 0) + /** + * Load the user based on the user ID from the item array. + * + * @param array $item The array containing user details. + * + * @return User|null The user object if found, null otherwise. + * @since 5.0.2 + */ + private function loadUser(array $item): ?User + { + if (!isset($item['user_id']) || !is_numeric($item['user_id']) || $item['user_id'] <= 0) { - // Retrieve the user by ID - $user = UserHelper::getUserById((int)$item['user_id']); - if ($user->id == $item['user_id']) - { - $details['id'] = (int) $item['user_id']; - } + return null; + } + + $user = UserHelper::getUserById((int) $item['user_id']); + + if ($user && $user->id == $item['user_id']) + { + return $user; + } + + return null; + } + + /** + * Extract user details from the item array and prepare them for saving. + * + * @param array $item The array containing user details. + * @param User|null $user The user object if found, null otherwise. + * + * @return array The prepared user details array. + * @since 5.0.2 + */ + private function extractUserDetails(array &$item, ?User $user): array + { + $details = []; + + if ($user !== null) + { + $details['id'] = (int) $item['user_id']; } foreach ($this->user as $property) @@ -397,18 +438,54 @@ final class UsersSubform implements GuidInterface, SubformInterface } } - if ($user !== null) - { - $details['group'] = $user->getAuthorisedGroups(); - } + return $details; + } + /** + * Assigns user groups based on existing groups and entity type. + * + * @param array &$details The array to store user details including groups. + * @param User|null $user The user object if found, null otherwise. + * @param array $item The array containing additional user details. + * + * @return void + * @since 5.0.2 + */ + private function assignUserGroups(array &$details, ?User $user, array $item): void + { + $details['groups'] = $user !== null ? $user->groups : []; + + if (!empty($item['entity_type'])) + { + $groups = Component::getParams()->get($item['entity_type'] . '_groups', []); + foreach ($groups as $group) + { + if (!in_array($group, $details['groups'])) + { + $details['groups'][] = $group; + } + } + } + } + + /** + * Save the user details using UserHelper and handle exceptions. + * + * @param array $details The prepared user details array. + * @param int $userId The ID of the user being processed. + * + * @return int The ID of the saved user, or 0 on failure. + * @since 5.0.2 + */ + private function saveUserDetails(array $details, int $userId): int + { try { return UserHelper::save($details); - } catch(NoUserIdFoundException $e) { + } catch (NoUserIdFoundException $e) { Factory::getApplication()->enqueueMessage($e->getMessage(), 'error'); - } catch(\Exception $e) { + } catch (\Exception $e) { Factory::getApplication()->enqueueMessage($e->getMessage(), 'warning'); - return $item['user_id']; + return $userId; } return 0; diff --git a/src/46b98346-ec98-42b3-a393-96c7d1282b1c/code.power b/src/46b98346-ec98-42b3-a393-96c7d1282b1c/code.power index 4c3d6e8..78bb61f 100644 --- a/src/46b98346-ec98-42b3-a393-96c7d1282b1c/code.power +++ b/src/46b98346-ec98-42b3-a393-96c7d1282b1c/code.power @@ -55,7 +55,7 @@ * @param string $table The table that should be active * * @return self - * @since 3.2.2 + * @since 3.2.2 */ public function table(string $table): self { @@ -73,7 +73,7 @@ * @param array $get The array get:set of the keys of each row in the subform. * * @return array|null The subform - * @since 3.2.2 + * @since 3.2.2 */ public function get(string $linkValue, string $linkKey, string $field, array $get): ?array { @@ -98,7 +98,7 @@ * @param string $linkValue The value of the link key in child table. * * @return bool - * @since 3.2.2 + * @since 3.2.2 */ public function set(mixed $items, string $indexKey, string $linkKey, string $linkValue): bool { @@ -120,7 +120,7 @@ * Get the current active table * * @return string - * @since 3.2.2 + * @since 3.2.2 */ public function getTable(): string { @@ -133,7 +133,7 @@ * @return void * @since 5.0.2 */ - protected function initializeUserProperties(): void + private function initializeUserProperties(): void { $user = UserHelper::getUserById(0); @@ -157,7 +157,7 @@ * @param string $linkValue The value of the link key in child table. * * @return void - * @since 3.2.2 + * @since 3.2.2 */ private function purge(array $items, string $indexKey, string $linkKey, string $linkValue): void { @@ -199,7 +199,7 @@ * @param array $items Array of objects or arrays to be filtered. * * @return array - * @since 5.0.2 + * @since 5.0.2 */ private function getUsersDetails(array $items): array { @@ -218,7 +218,7 @@ * @param array $item The user map array * * @return void - * @since 5.0.2 + * @since 5.0.2 */ private function getUserDetails(array &$item): void { @@ -255,7 +255,7 @@ * @param string $field The field prefix for the resulting associative array. * * @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 $field): array { @@ -299,7 +299,7 @@ * @param string $linkValue The value of the link key in child table. * * @return array The processed array of arrays. - * @since 3.2.2 + * @since 3.2.2 */ private function process($items, string $indexKey, string $linkKey, string $linkValue): array { @@ -335,27 +335,66 @@ } /** - * Set the user details. + * Handles setting user details and saving them. * - * @param array $item The user details + * This function retrieves the user by ID, sets the user details, + * and adds appropriate user groups before saving the user. * - * @return int - * @since 5.0.2 + * @param array $item The user details passed by reference. + * + * @return int The ID of the saved user, or 0 on failure. + * @since 5.0.2 */ private function setUserDetails(array &$item): int { - $details = []; - $user = null; + $user = $this->loadUser($item); + $details = $this->extractUserDetails($item, $user); + $this->assignUserGroups($details, $user, $item); + + return $this->saveUserDetails($details, $details['id'] ?? 0); + } - // now load the user ID - if (isset($item['user_id']) && is_numeric($item['user_id']) && $item['user_id'] > 0) + /** + * Load the user based on the user ID from the item array. + * + * @param array $item The array containing user details. + * + * @return User|null The user object if found, null otherwise. + * @since 5.0.2 + */ + private function loadUser(array $item): ?User + { + if (!isset($item['user_id']) || !is_numeric($item['user_id']) || $item['user_id'] <= 0) { - // Retrieve the user by ID - $user = UserHelper::getUserById((int)$item['user_id']); - if ($user->id == $item['user_id']) - { - $details['id'] = (int) $item['user_id']; - } + return null; + } + + $user = UserHelper::getUserById((int) $item['user_id']); + + if ($user && $user->id == $item['user_id']) + { + return $user; + } + + return null; + } + + /** + * Extract user details from the item array and prepare them for saving. + * + * @param array $item The array containing user details. + * @param User|null $user The user object if found, null otherwise. + * + * @return array The prepared user details array. + * @since 5.0.2 + */ + private function extractUserDetails(array &$item, ?User $user): array + { + $details = []; + + if ($user !== null) + { + $details['id'] = (int) $item['user_id']; } foreach ($this->user as $property) @@ -367,18 +406,54 @@ } } - if ($user !== null) - { - $details['group'] = $user->getAuthorisedGroups(); - } + return $details; + } + /** + * Assigns user groups based on existing groups and entity type. + * + * @param array &$details The array to store user details including groups. + * @param User|null $user The user object if found, null otherwise. + * @param array $item The array containing additional user details. + * + * @return void + * @since 5.0.2 + */ + private function assignUserGroups(array &$details, ?User $user, array $item): void + { + $details['groups'] = $user !== null ? $user->groups : []; + + if (!empty($item['entity_type'])) + { + $groups = Component::getParams()->get($item['entity_type'] . '_groups', []); + foreach ($groups as $group) + { + if (!in_array($group, $details['groups'])) + { + $details['groups'][] = $group; + } + } + } + } + + /** + * Save the user details using UserHelper and handle exceptions. + * + * @param array $details The prepared user details array. + * @param int $userId The ID of the user being processed. + * + * @return int The ID of the saved user, or 0 on failure. + * @since 5.0.2 + */ + private function saveUserDetails(array $details, int $userId): int + { try { return UserHelper::save($details); - } catch(NoUserIdFoundException $e) { + } catch (NoUserIdFoundException $e) { Factory::getApplication()->enqueueMessage($e->getMessage(), 'error'); - } catch(\Exception $e) { + } catch (\Exception $e) { Factory::getApplication()->enqueueMessage($e->getMessage(), 'warning'); - return $item['user_id']; + return $userId; } return 0; diff --git a/src/46b98346-ec98-42b3-a393-96c7d1282b1c/settings.json b/src/46b98346-ec98-42b3-a393-96c7d1282b1c/settings.json index 5ab98c0..f17c492 100644 --- a/src/46b98346-ec98-42b3-a393-96c7d1282b1c/settings.json +++ b/src/46b98346-ec98-42b3-a393-96c7d1282b1c/settings.json @@ -28,12 +28,16 @@ "use_selection3": { "use": "1c10a5f1-204d-4f17-ad9f-0e0684f2030d", "as": "default" + }, + "use_selection4": { + "use": "640b5352-fb09-425f-a26e-cd44eda03f15", + "as": "Component" } }, "extendsinterfaces": null, "namespace": "[[[NamespacePrefix]]]\\Joomla\\Data.UsersSubform", "description": "CRUD the user data of any sub-form to another view (table)\r\n\r\n@since 5.0.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": "use Joomla\\CMS\\Factory;", + "head": "use Joomla\\CMS\\Factory;\r\nuse Joomla\\CMS\\User\\User;", "composer": "" } \ No newline at end of file