Update 2024-09-03 02:37:43
This commit is contained in:
parent
26e410cc2d
commit
b894dfc8af
@ -20,13 +20,17 @@ class UsersSubform << (F,LightGreen) >> #RoyalBlue {
|
|||||||
+ get(string $linkValue, string $linkKey, ...) : ?array
|
+ get(string $linkValue, string $linkKey, ...) : ?array
|
||||||
+ set(mixed $items, string $indexKey, ...) : bool
|
+ set(mixed $items, string $indexKey, ...) : bool
|
||||||
+ getTable() : string
|
+ getTable() : string
|
||||||
# initializeUserProperties() : void
|
- initializeUserProperties() : void
|
||||||
- purge(array $items, string $indexKey, ...) : void
|
- purge(array $items, string $indexKey, ...) : void
|
||||||
- getUsersDetails(array $items) : array
|
- getUsersDetails(array $items) : array
|
||||||
- getUserDetails(array $item) : void
|
- getUserDetails(array $item) : void
|
||||||
- converter(array $items, array $keySet, ...) : array
|
- converter(array $items, array $keySet, ...) : array
|
||||||
- process(mixed $items, string $indexKey, ...) : array
|
- process(mixed $items, string $indexKey, ...) : array
|
||||||
- setUserDetails(array $item) : int
|
- 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
|
note right of UsersSubform::__construct
|
||||||
@ -136,7 +140,42 @@ note right of UsersSubform::process
|
|||||||
end note
|
end note
|
||||||
|
|
||||||
note left of UsersSubform::setUserDetails
|
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
|
since: 5.0.2
|
||||||
return: int
|
return: int
|
||||||
|
@ -13,10 +13,12 @@ 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\Interfaces\Data\ItemsInterface as Items;
|
||||||
use VDM\Joomla\Data\Guid;
|
use VDM\Joomla\Data\Guid;
|
||||||
use VDM\Joomla\Componentbuilder\Utilities\UserHelper;
|
use VDM\Joomla\Componentbuilder\Utilities\UserHelper;
|
||||||
use VDM\Joomla\Componentbuilder\Utilities\Exception\NoUserIdFoundException;
|
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\GuidInterface;
|
||||||
use VDM\Joomla\Interfaces\Data\SubformInterface;
|
use VDM\Joomla\Interfaces\Data\SubformInterface;
|
||||||
|
|
||||||
@ -163,7 +165,7 @@ final class UsersSubform implements GuidInterface, SubformInterface
|
|||||||
* @return void
|
* @return void
|
||||||
* @since 5.0.2
|
* @since 5.0.2
|
||||||
*/
|
*/
|
||||||
protected function initializeUserProperties(): void
|
private function initializeUserProperties(): void
|
||||||
{
|
{
|
||||||
$user = UserHelper::getUserById(0);
|
$user = UserHelper::getUserById(0);
|
||||||
|
|
||||||
@ -365,28 +367,67 @@ 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
|
* @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
|
* @since 5.0.2
|
||||||
*/
|
*/
|
||||||
private function setUserDetails(array &$item): int
|
private function setUserDetails(array &$item): int
|
||||||
{
|
{
|
||||||
$details = [];
|
$user = $this->loadUser($item);
|
||||||
$user = null;
|
$details = $this->extractUserDetails($item, $user);
|
||||||
|
$this->assignUserGroups($details, $user, $item);
|
||||||
|
|
||||||
// now load the user ID
|
return $this->saveUserDetails($details, $details['id'] ?? 0);
|
||||||
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
|
||||||
{
|
{
|
||||||
// Retrieve the user by ID
|
if (!isset($item['user_id']) || !is_numeric($item['user_id']) || $item['user_id'] <= 0)
|
||||||
$user = UserHelper::getUserById((int)$item['user_id']);
|
{
|
||||||
if ($user->id == $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'];
|
$details['id'] = (int) $item['user_id'];
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($this->user as $property)
|
foreach ($this->user as $property)
|
||||||
{
|
{
|
||||||
@ -397,18 +438,54 @@ final class UsersSubform implements GuidInterface, SubformInterface
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($user !== null)
|
return $details;
|
||||||
{
|
|
||||||
$details['group'] = $user->getAuthorisedGroups();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 {
|
try {
|
||||||
return UserHelper::save($details);
|
return UserHelper::save($details);
|
||||||
} catch(NoUserIdFoundException $e) {
|
} catch (NoUserIdFoundException $e) {
|
||||||
Factory::getApplication()->enqueueMessage($e->getMessage(), 'error');
|
Factory::getApplication()->enqueueMessage($e->getMessage(), 'error');
|
||||||
} catch(\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
Factory::getApplication()->enqueueMessage($e->getMessage(), 'warning');
|
Factory::getApplication()->enqueueMessage($e->getMessage(), 'warning');
|
||||||
return $item['user_id'];
|
return $userId;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -133,7 +133,7 @@
|
|||||||
* @return void
|
* @return void
|
||||||
* @since 5.0.2
|
* @since 5.0.2
|
||||||
*/
|
*/
|
||||||
protected function initializeUserProperties(): void
|
private function initializeUserProperties(): void
|
||||||
{
|
{
|
||||||
$user = UserHelper::getUserById(0);
|
$user = UserHelper::getUserById(0);
|
||||||
|
|
||||||
@ -335,28 +335,67 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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
|
* @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
|
* @since 5.0.2
|
||||||
*/
|
*/
|
||||||
private function setUserDetails(array &$item): int
|
private function setUserDetails(array &$item): int
|
||||||
{
|
{
|
||||||
$details = [];
|
$user = $this->loadUser($item);
|
||||||
$user = null;
|
$details = $this->extractUserDetails($item, $user);
|
||||||
|
$this->assignUserGroups($details, $user, $item);
|
||||||
|
|
||||||
// now load the user ID
|
return $this->saveUserDetails($details, $details['id'] ?? 0);
|
||||||
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
|
||||||
{
|
{
|
||||||
// Retrieve the user by ID
|
if (!isset($item['user_id']) || !is_numeric($item['user_id']) || $item['user_id'] <= 0)
|
||||||
$user = UserHelper::getUserById((int)$item['user_id']);
|
{
|
||||||
if ($user->id == $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'];
|
$details['id'] = (int) $item['user_id'];
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($this->user as $property)
|
foreach ($this->user as $property)
|
||||||
{
|
{
|
||||||
@ -367,18 +406,54 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($user !== null)
|
return $details;
|
||||||
{
|
|
||||||
$details['group'] = $user->getAuthorisedGroups();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 {
|
try {
|
||||||
return UserHelper::save($details);
|
return UserHelper::save($details);
|
||||||
} catch(NoUserIdFoundException $e) {
|
} catch (NoUserIdFoundException $e) {
|
||||||
Factory::getApplication()->enqueueMessage($e->getMessage(), 'error');
|
Factory::getApplication()->enqueueMessage($e->getMessage(), 'error');
|
||||||
} catch(\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
Factory::getApplication()->enqueueMessage($e->getMessage(), 'warning');
|
Factory::getApplication()->enqueueMessage($e->getMessage(), 'warning');
|
||||||
return $item['user_id'];
|
return $userId;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -28,12 +28,16 @@
|
|||||||
"use_selection3": {
|
"use_selection3": {
|
||||||
"use": "1c10a5f1-204d-4f17-ad9f-0e0684f2030d",
|
"use": "1c10a5f1-204d-4f17-ad9f-0e0684f2030d",
|
||||||
"as": "default"
|
"as": "default"
|
||||||
|
},
|
||||||
|
"use_selection4": {
|
||||||
|
"use": "640b5352-fb09-425f-a26e-cd44eda03f15",
|
||||||
|
"as": "Component"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"extendsinterfaces": null,
|
"extendsinterfaces": null,
|
||||||
"namespace": "[[[NamespacePrefix]]]\\Joomla\\Data.UsersSubform",
|
"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",
|
"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 <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": "use Joomla\\CMS\\Factory;",
|
"head": "use Joomla\\CMS\\Factory;\r\nuse Joomla\\CMS\\User\\User;",
|
||||||
"composer": ""
|
"composer": ""
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user