Skip to content

Commit 7f7e8dc

Browse files
committed
Use usersModel with filters
1 parent 2758f0e commit 7f7e8dc

File tree

1 file changed

+39
-39
lines changed

1 file changed

+39
-39
lines changed

administrator/components/com_joomlaupdate/src/Model/NotificationModel.php

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,14 @@
1010

1111
namespace Joomla\Component\Joomlaupdate\Administrator\Model;
1212

13-
use Joomla\CMS\Access\Access;
1413
use Joomla\CMS\Component\ComponentHelper;
1514
use Joomla\CMS\Factory;
1615
use Joomla\CMS\Mail\MailHelper;
1716
use Joomla\CMS\Mail\MailTemplate;
1817
use Joomla\CMS\MVC\Model\BaseDatabaseModel;
19-
use Joomla\CMS\Table\Asset;
18+
2019
use Joomla\CMS\Uri\Uri;
2120
use Joomla\CMS\Version;
22-
use Joomla\Database\ParameterType;
2321
use Joomla\Registry\Registry;
2422
use Joomla\Utilities\ArrayHelper;
2523

@@ -78,13 +76,13 @@ public function sendNotification($type, $oldVersion): void
7876
];
7977

8078
// Send the emails to the Super Users
81-
foreach ($superUsers as $superUser) {
82-
$params = new Registry($superUser->params);
79+
foreach ($emailReceivers as $receiver) {
80+
$params = new Registry($receiver->params);
8381
$jLanguage->load('com_joomlaupdate', JPATH_ADMINISTRATOR, 'en-GB', true, true);
8482
$jLanguage->load('com_joomlaupdate', JPATH_ADMINISTRATOR, $params->get('admin_language', null), true, true);
8583

8684
$mailer = new MailTemplate('com_joomlaupdate.update.' . $type, $jLanguage->getTag());
87-
$mailer->addRecipient($superUser->email);
85+
$mailer->addRecipient($receiver->email);
8886
$mailer->addTemplateData($substitutions);
8987
$mailer->send();
9088
}
@@ -107,38 +105,40 @@ private function getEmailReceivers($emailGroups): array
107105

108106
$emailReceivers = [];
109107

110-
$groupModel = Factory::getApplication()->bootComponent('com_users')
111-
->getMVCFactory()->createModel('Group', 'Administrator');
112-
113-
// Get the emails of all groups in the emailGroups
114-
foreach ($emailGroups as $group) {
115-
$usersInGroup = $groupModel->getUsersInGroup($group);
116-
117-
if (empty($usersInGroup)) {
118-
return [];
119-
}
120-
121-
// Only users with valid email address who are not blocked can receive the email
122-
foreach ($usersInGroup as $user) {
123-
if (MailHelper::isEmailAddress($user->email) && !$user->block) {
124-
$user->email = strtolower(trim($user->email));
125-
126-
// Check if the email already exists in the emailReceivers array
127-
$exist = false;
128-
for ($i = 0; $i < count($emailReceivers); $i++) {
129-
if ($emailReceivers[$i]->email === $user->email) {
130-
$exist = true;
131-
break;
132-
}
133-
}
134-
// Add to the list if it is not already in the list
135-
if (!$exist) {
136-
$emailReceivers[] = $user;
137-
}
138-
}
139-
}
140-
}
141-
142-
return $emailReceivers;
108+
// Get the users of all groups in the emailGroups
109+
$usersModel = Factory::getApplication()->bootComponent('com_users')
110+
->getMVCFactory()->createModel('Users', 'Administrator');
111+
112+
$usersModel->setState('filter.groups', $emailGroups);
113+
$usersModel->setState('filter.block', (int) 0);
114+
115+
$usersInGroup = $usersModel->getItems();
116+
117+
if (empty($usersInGroup)) {
118+
return [];
119+
}
120+
121+
// Only users with valid email address who are not blocked can receive the email
122+
foreach ($usersInGroup as $user) {
123+
if (MailHelper::isEmailAddress($user->email) && !$user->block) {
124+
$user->email = strtolower(trim($user->email));
125+
126+
// Check if the email already exists in the emailReceivers array
127+
$exist = false;
128+
foreach ($emailReceivers as $rec) {
129+
if ($rec->email === $user->email) {
130+
$exist = true;
131+
break;
132+
}
133+
}
134+
135+
// Add to the list if it is not already in the list
136+
if (!$exist) {
137+
$emailReceivers[] = $user;
138+
}
139+
}
140+
}
141+
142+
return $emailReceivers;
143143
}
144144
}

0 commit comments

Comments
 (0)