ContentMailBuilder.php 4.75 KB
Newer Older
Marcello Pivanti's avatar
Marcello Pivanti committed
1
2
3
4
5
6
7
<?php

/**
 * Art-ER Attrattività, ricerca e territorio dell’Emilia-Romagna
 * OPEN 2.0
 *
 *
waltermazza's avatar
waltermazza committed
8
 * @package    arter\amos\notificationmanager\base\builder
Marcello Pivanti's avatar
Marcello Pivanti committed
9
10
11
12
 * @category   CategoryName
 * @author     Elite Division S.r.l.
 */

waltermazza's avatar
waltermazza committed
13
namespace arter\amos\notificationmanager\base\builder;
Marcello Pivanti's avatar
Marcello Pivanti committed
14

waltermazza's avatar
waltermazza committed
15
16
17
18
19
20
21
use arter\amos\core\interfaces\ModelLabelsInterface;
use arter\amos\core\models\ModelsClassname;
use arter\amos\core\record\Record;
use arter\amos\core\user\User;
use arter\amos\notificationmanager\AmosNotify;
use arter\amos\notificationmanager\models\Notification;
use arter\amos\notificationmanager\record\NotifyRecord;
Marcello Pivanti's avatar
Marcello Pivanti committed
22
23
24
25
26
use Yii;
use yii\helpers\Console;

/**
 * Class ContentMailBuilder
waltermazza's avatar
waltermazza committed
27
 * @package arter\amos\notificationmanager\base\builder
Marcello Pivanti's avatar
Marcello Pivanti committed
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
 */
class ContentMailBuilder extends AMailBuilder
{
    /**
     * @return string
     */
    public function getSubject(array $resultset)
    {
        return Yii::t('amosnotify', "#Content_Change_Subject_Notify");
    }

    /**
     * @inheritdoc
     */
    public function renderEmail(array $resultset, User $user)
    {
        $mail = '';
        $class_content = '';
        try {
            $mail .= $this->renderContentHeader($resultset);
            foreach ($resultset as $notify) {
                /** @var Notification $notify */
                $cls_name = $notify->class_name;
                $modelClassname = ModelsClassname::find()->andWhere(['classname' => $cls_name])->one();

                /** @var NotifyRecord|ModelLabelsInterface $model */
                $model = $cls_name::find()->andWhere(['id' => $notify->content_id])->one();
                if (!is_null($model) && $model->sendCommunication()) {
                    if (strcmp($class_content, $notify->class_name)) {
                        $mail .= $this->renderContentTitle($model);
                        $class_content = $notify->class_name;

                        if($modelClassname) {
                            $module = \Yii::$app->getModule($modelClassname->module);
                            if (!empty($module->viewPathEmailContentSubtitle[$cls_name])) {
                                $mail .= $this->renderPersonalizedContentSubtitle($model, $user, $module->viewPathEmailContentSubtitle[$cls_name]);
                            }
                        }
                    }
                    // render list of content of default
                    $mail .= $this->renderContent($model, $user);
                }
            }
            $mail .= $this->renderContentFooter($resultset, $user);
        } catch (\Exception $ex) {
            Yii::getLogger()->log($ex->getTraceAsString(), \yii\log\Logger::LEVEL_ERROR);
        }

        return $mail;
    }

    /**
     * @param Record $model
     * @param User $user
     * @return string
     */
    protected function renderContent(Record $model, $user)
    {
        $controller = \Yii::$app->controller;
waltermazza's avatar
waltermazza committed
87
        $ris = $controller->renderPartial("@vendor/arter/amos-" . AmosNotify::getModuleName() . "/src/views/email/content", [
Marcello Pivanti's avatar
Marcello Pivanti committed
88
89
90
91
92
93
94
95
96
97
98
99
100
            'model' => $model,
            'profile' => $user->userProfile
        ]);
        return $ris;
    }

    /**
     * @param ModelLabelsInterface $model
     * @return string
     */
    protected function renderContentTitle(ModelLabelsInterface $model)
    {
        $controller = \Yii::$app->controller;
waltermazza's avatar
waltermazza committed
101
        $ris = $controller->renderPartial("@vendor/arter/amos-" . AmosNotify::getModuleName() . "/src/views/email/content_title", [
Marcello Pivanti's avatar
Marcello Pivanti committed
102
103
104
105
106
107
108
109
110
111
112
113
114
            'title' => $model->getGrammar()->getModelLabel(),
        ]);
        return $ris;
    }

    /**
     * @param array $resultset
     * @return string
     */
    protected function renderContentHeader(array $resultset)
    {
        $controller = \Yii::$app->controller;
        $contents_number = count($resultset);
waltermazza's avatar
waltermazza committed
115
        $ris = $controller->renderPartial("@vendor/arter/amos-" . AmosNotify::getModuleName() . "/src/views/email/content_header", [
Marcello Pivanti's avatar
Marcello Pivanti committed
116
117
118
119
120
121
122
123
124
125
126
127
            'contents_number' => $contents_number
        ]);
        return $ris;
    }

    /**
     * @param array $resultset
     * @return string
     */
    protected function renderContentFooter(array $resultset, User $user)
    {
        $controller = \Yii::$app->controller;
waltermazza's avatar
waltermazza committed
128
        $ris = $controller->renderPartial("@vendor/arter/amos-" . AmosNotify::getModuleName() . "/src/views/email/content_footer", ['user' => $user]);
Marcello Pivanti's avatar
Marcello Pivanti committed
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
        return $ris;
    }

    /**
     * @param Record[] $model
     * @param User $user
     * @return string
     */
    protected function renderPersonalizedContentSubtitle($model, $user, $viewPath)
    {
        Console::stdout($user->userProfile->id. PHP_EOL);

        $controller = \Yii::$app->controller;
        $ris = $controller->renderPartial($viewPath, [
            'model' => $model,
            'profile' => $user->userProfile,
        ]);
        return $ris;
    }
}