File: InactiveUsersNotificationEmail.php

package info (click to toggle)
matomo 5.5.1%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 73,596 kB
  • sloc: php: 231,041; javascript: 102,286; python: 202; xml: 189; sh: 172; makefile: 20; sql: 10
file content (102 lines) | stat: -rw-r--r-- 2,831 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php

/**
 * Matomo - free/libre analytics platform
 *
 * @link    https://matomo.org
 * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

namespace Piwik\Plugins\UsersManager\Emails;

use Piwik\Mail;
use Piwik\Piwik;
use Piwik\Plugins\LanguagesManager\LanguagesHelper;
use Piwik\Plugins\UsersManager\Model;
use Piwik\Plugins\UsersManager\UserNotifications\UserNotification;
use Piwik\SettingsPiwik;
use Piwik\Url;
use Piwik\View;

class InactiveUsersNotificationEmail extends Mail
{
    /**
     * @var UserNotification
     */
    private $notification;

    /** @var string */
    private $recipient;

    /** @var array */
    private $emailData;

    /** @var Model */
    private $userModel;

    public function __construct(UserNotification $notification, string $recipient, array $emailData)
    {
        parent::__construct();

        $this->notification = $notification;
        $this->recipient = $recipient;
        $this->emailData = $emailData;
        $this->userModel = new Model();

        $this->setUpEmail();
    }

    private function setUpEmail(): void
    {
        LanguagesHelper::doWithUserLanguage($this->recipient, function () {
            $this->setDefaultFromPiwik();
            $this->addTo($this->recipient);
            $this->setSubject($this->getDefaultSubject());
            $this->addReplyTo($this->getFrom(), $this->getFromName());
            $this->setBodyText($this->getDefaultBodyText());
            $this->setWrappedHtmlBody($this->getDefaultBodyView());
        });
    }

    protected function getManageUsersLink(): string
    {
        return SettingsPiwik::getPiwikUrl()
            . 'index.php?'
            . Url::getQueryStringFromParameters(['module' => 'UsersManager', 'action' => 'index']);
    }

    protected function getDefaultSubject(): string
    {
        return Piwik::translate('UsersManager_InactiveUsersNotificationEmailSubject');
    }

    protected function getDefaultBodyText(): string
    {
        $view = new View('@UsersManager/_inactiveUsersNotificationTextEmail.twig');
        $view->setContentType('text/plain');

        $this->assignCommonParameters($view);

        return $view->render();
    }

    protected function getDefaultBodyView(): View
    {
        $view = new View('@UsersManager/_inactiveUsersNotificationHtmlEmail.twig');

        $this->assignCommonParameters($view);

        return $view;
    }

    protected function assignCommonParameters(View $view): void
    {
        $view->inactiveUsers = $this->notification->getUsers();
        $view->manageUsersLink = $this->getManageUsersLink();
        $view->superuserLogin = $this->userModel->getUserByEmail($this->recipient)['login'];

        foreach ($this->emailData as $item => $value) {
            $view->assign($item, $value);
        }
    }
}