Commit b708f46b authored by Patrick Müller's avatar Patrick Müller

Merge branch 'dev'

parents 287209de be8194c5
<?xml version="1.0" encoding="UTF-8"?>
<console>
<tool exec="\QUI\FrontendUsers\Console\SendUserMails" />
<tool exec="\QUI\FrontendUsers\Console\SetUserGroups" />
</console>
\ No newline at end of file
......@@ -115,6 +115,20 @@ class SendUserMails extends QUI\System\Console\Tool
$this->writeLn("E-Mail subject?: ");
$subject = $this->readInput();
$this->writeLn("E-Mail sender mail? [system default]: ");
$senderMail = $this->readInput();
if (empty($senderMail)) {
$senderMail = QUI::conf('mail', 'MAILFrom');
}
$this->writeLn("E-Mail sender name? [system default]: ");
$senderName = $this->readInput();
if (empty($senderName)) {
$senderName = QUI::conf('mail', 'MAILFromText');
}
// SUMMARY
$this->writeLn("\nSUMMARY\n===============================================\n");
......@@ -123,6 +137,8 @@ class SendUserMails extends QUI\System\Console\Tool
$this->writeLn("User groups: ".(empty($groupIds) ? "ALL" : implode(', ', $groupIds)));
$this->writeLn("ORDER BY: ".(empty($orderBy) ? "DEFAULT" : $orderBy));
$this->writeLn("\nE-Mail subject: ".$subject);
$this->writeLn("\nE-Mail sender mail: ".$senderMail);
$this->writeLn("\nE-Mail sender name: ".$senderName);
$this->writeLn(
"\nE-Mail will be sent to ".count($recipients)." out of ".count($result)." selected users."
." ".(count($result) - count($recipients))." users have no e-mail address and are ignored."
......@@ -137,12 +153,7 @@ class SendUserMails extends QUI\System\Console\Tool
}
// Queue mails
$Mailer = QUI::getMailManager()->getMailer();
foreach ($recipients as $recipient) {
$Mailer->setSubject($subject);
$Mailer->setHTML(true);
if (!empty($recipient['firstname']) && !empty($recipient['lastname'])) {
$name = $recipient['firstname'].' '.$recipient['lastname'];
} else {
......@@ -157,6 +168,12 @@ class SendUserMails extends QUI\System\Console\Tool
$body
);
$Mailer = QUI::getMailManager()->getMailer();
$Mailer->setFrom($senderMail);
$Mailer->setFromName($senderName);
$Mailer->setSubject($subject);
$Mailer->setHTML(true);
$Mailer->setBody($body);
$Mailer->addRecipient($email);
......
<?php
namespace QUI\FrontendUsers\Console;
use QUI;
/**
* Console tool to add users to groups
*
* @author www.pcsg.de (Patrick Müller)
*/
class SetUserGroups extends QUI\System\Console\Tool
{
/**
* Constructor
*/
public function __construct()
{
$this->setName('frontend-users:setUserGroups')
->setDescription(
"Add users to groups"
);
}
/**
* Execute the console tool
*/
public function execute()
{
QUI\Permissions\Permission::isAdmin();
// Determine groups
$this->writeLn(
"Add users to the following groups (comma separated list of group ids): "
);
$groupIds = $this->readInput();
if (empty($groupIds)) {
$this->exitSuccess();
}
$groupIds = explode(',', $groupIds);
// Determine users
// INCLUDE INACTIVE USERS?
$this->writeLn("Include INACTIVE users? (y/N): ");
$inactiveUsers = mb_strtolower($this->readInput()) === 'y';
// USER LANGUAGE
$this->writeLn("Languages of the users? (comma separated language abbreviations) [en]: ");
$languages = $this->readInput();
if (!empty($languages)) {
$languages = explode(',', $languages);
} else {
$languages = ['en'];
}
// RESTRICT TO GROUPS
$this->writeLn(
"Select users in the following GROUPS only (comma separated list of group ids;"
." leave empty to ignore groups): "
);
$userGroupIds = $this->readInput();
if (empty($userGroupIds)) {
$userGroupIds = [];
} else {
$userGroupIds = explode(',', $userGroupIds);
}
// Get all users
$sql = "SELECT `id` FROM ".QUI::getUsers()::table();
$where[] = "`lang` IN ('".implode("','", $languages)."')";
if (!$inactiveUsers) {
$where[] = "`active` = 1";
}
if (!empty($userGroupIds)) {
$whereOR = [];
foreach ($userGroupIds as $groupId) {
$whereOR[] = "`usergroup` LIKE '%,$groupId,%'";
}
$where[] = "(".implode(" OR ", $whereOR).")";
}
$sql .= " WHERE ".implode(" AND ", $where);
$result = QUI::getDataBase()->fetchSQL($sql);
// SUMMARY
$this->writeLn("\nSUMMARY\n===============================================\n");
$this->writeLn("Add users to groups: ".implode(', ', $groupIds));
$this->writeLn("Include INACTIVE users: ".($inactiveUsers ? "YES" : "NO"));
$this->writeLn("User languages: ".implode(', ', $languages));
$this->writeLn(
"Select users in this groups only: ".(empty($userGroupIds) ? "ALL" : implode(', ', $userGroupIds))
);
$this->writeLn("\n\nIs everything correct? (Y/n): ");
$confirm = mb_strtolower($this->readInput()) !== 'n';
if (!$confirm) {
$this->execute();
return;
}
// Set groups
$this->writeLn("\n\nSTART SETTING GROUPS\n");
$Users = QUI::getUsers();
$SystemUser = QUI::getUsers()->getSystemUser();
foreach ($result as $row) {
$User = $Users->get($row['id']);
$this->writeLn("Add groups for User #".$User->getId()." (".$User->getUsername().")...");
foreach ($groupIds as $groupId) {
try {
$this->writeLn("\tGroup #$groupId...");
$User->addToGroup($groupId);
$User->save($SystemUser);
$this->write(" OK!");
} catch (\Exception $Exception) {
QUI\System\Log::writeException($Exception);
$this->write(" ERROR: ".$Exception->getMessage());
}
}
}
$this->exitSuccess();
}
/**
* Exits the console tool with a success msg and status 0
*
* @return void
*/
protected function exitSuccess()
{
$this->writeLn("User groups have been successfully set.");
$this->writeLn("");
exit(0);
}
/**
* Exits the console tool with an error msg and status 1
*
* @param $msg
* @return void
*/
protected function exitFail($msg)
{
$this->writeLn("Script aborted due to an error:");
$this->writeLn("");
$this->writeLn($msg);
$this->writeLn("");
$this->writeLn("");
exit(1);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment