NotifyUtility.php 7.44 KB
Newer Older
Marcello Pivanti's avatar
Marcello Pivanti committed
1
2
3
4
5
6
<?php

/**
 * Art-ER Attrattività, ricerca e territorio dell’Emilia-Romagna
 * OPEN 2.0
 *
7
8
9
 * @see https://www.open2.0.regione.lombardia.it Developers'community
 * @license GPLv3
 * @license https://opensource.org/licenses/gpl-3.0.html GNU General Public License version 3
Marcello Pivanti's avatar
Marcello Pivanti committed
10
 *
waltermazza's avatar
waltermazza committed
11
 * @package    arter\amos\notificationmanager\utility
Marcello Pivanti's avatar
Marcello Pivanti committed
12
13
14
15
 * @category   CategoryName
 * @author     Elite Division S.r.l.
 */

waltermazza's avatar
waltermazza committed
16
namespace arter\amos\notificationmanager\utility;
Marcello Pivanti's avatar
Marcello Pivanti committed
17

waltermazza's avatar
waltermazza committed
18
19
20
21
22
23
use arter\amos\core\models\ModelsClassname;
use arter\amos\core\user\User;
use arter\amos\notificationmanager\AmosNotify;
use arter\amos\notificationmanager\models\NotificationConf;
use arter\amos\notificationmanager\models\NotificationsConfOpt;
use arter\amos\notificationmanager\models\NotificationconfNetwork;
Marcello Pivanti's avatar
Marcello Pivanti committed
24
25
26
27
28
29
use yii\base\BaseObject;
use yii\db\ActiveQuery;
use yii\db\Query;

/**
 * Class NotifyUtility
waltermazza's avatar
waltermazza committed
30
 * @package arter\amos\notificationmanager\utility
Marcello Pivanti's avatar
Marcello Pivanti committed
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
 */
class NotifyUtility extends BaseObject
{
    /**
     * @var AmosNotify $notifyModule
     */
    protected $notifyModule = null;

    /**
     * @inheritdoc
     */
    public function init()
    {
        parent::init();
        $this->notifyModule = AmosNotify::instance();
    }

    /**
     * The method save the notification configuration.
     * @param int $userId
     * @param int $emailFrequency
     * @param int $smsFrequency
     * @param array $params
     * @return bool
     */
    public function saveNotificationConf($userId, $emailFrequency = 0, $smsFrequency = 0, $params = [])
    {
        // Check the params type
        if (!is_numeric($userId) || !is_numeric($emailFrequency) || !is_numeric($smsFrequency)) {
            return false;
        }
        // Check the params presence
        if (!$emailFrequency && !$smsFrequency) {
            return false;
        }
        /** @var NotificationConf $notificationConfModel */
        $notificationConfModel = $this->notifyModule->createModel('NotificationConf');
        // Find the notification conf for the user
        $notificationConf = $notificationConfModel::findOne(['user_id' => $userId]);
        if (is_null($notificationConf)) {
            /** @var NotificationConf $notificationConf */
            $notificationConf = $this->notifyModule->createModel('NotificationConf');
            $notificationConf->user_id = $userId;
        }
        if ($emailFrequency) {
            /** @var NotificationsConfOpt $notificationConfOpt */
            $notificationConfOpt = $this->notifyModule->createModel('NotificationsConfOpt');
            // Check the params correct value for email frequency
            $emailFrequencyValues = $notificationConfOpt::emailFrequencyValues();
            if (!in_array($emailFrequency, $emailFrequencyValues)) {
                return false;
            }
            $notificationConf->email = $emailFrequency;
        }
        if ($smsFrequency) {
            /** @var NotificationsConfOpt $notificationConfOpt */
            $notificationConfOpt = $this->notifyModule->createModel('NotificationsConfOpt');
            // Check the params correct value for sms frequency
            $smsFrequencyValues = $notificationConfOpt::smsFrequencyValues();
            if (!in_array($smsFrequency, $smsFrequencyValues)) {
                return false;
            }
            $notificationConf->sms = $smsFrequency;
        }

        $modelAttributesList = $notificationConf->attributes();
        foreach ($modelAttributesList as $fieldName) {
            if (isset($params[$fieldName])) {
                $notificationConf->{$fieldName} = $params[$fieldName];
            }
        }
        $ok = $notificationConf->save();
        $this->saveNetworkNotification($userId, $params);
        return $ok;
    }

    /**
     * @param $userId
     * @param $params
     */
    public function saveNetworkNotification($userId, $params)
    {
        if (!empty($params['notifyCommunity'])) {
            foreach ($params['notifyCommunity'] as $communityId => $value) {
                $modelClassname = ModelsClassname::find()->andWhere(['module' => 'community'])->one();
                if ($modelClassname) {
                    /** @var NotificationconfNetwork $notificationConfNetworkModel */
                    $notificationConfNetworkModel = $this->notifyModule->createModel('NotificationconfNetwork');
                    $confNetwork = $notificationConfNetworkModel::find()
                        ->andWhere(['models_classname_id' => $modelClassname->id, 'record_id' => $communityId])
                        ->andWhere(['user_id' => $userId])->one();
                    if (empty($confNetwork)) {
                        /** @var NotificationconfNetwork $confNetwork */
                        $confNetwork = $this->notifyModule->createModel('NotificationconfNetwork');
                    }
                    $confNetwork->user_id = $userId;
                    $confNetwork->models_classname_id = $modelClassname->id;
                    $confNetwork->record_id = $communityId;
                    $confNetwork->email = $value;
                    $confNetwork->save(false);
                }
            }
        }
    }

    /**
     * @param $userId
     * @param $notificationType
     * @return array|\yii\db\ActiveRecord[]
     */
    public static function getNetworkNotificationConf($userId, $notificationType)
    {
        /** @var NotificationconfNetwork $notificationConfNetworkModel */
        $notificationConfNetworkModel = AmosNotify::instance()->createModel('NotificationconfNetwork');
        /** @var  $query ActiveQuery */
        $query = $notificationConfNetworkModel::find()
            ->andWhere(['user_id' => $userId])
            ->andWhere(['IS NOT', 'record_id', null])
            ->andWhere(['IS NOT', 'models_classname_id', null]);

        $query->andWhere(['!=', 'email', $notificationType]);
        $query->andWhere(['IS NOT', 'email', null]);

        return $query->all();
    }

    /**
     * This method set the user default notifications configurations.
     * @param int $userId
     * @return bool
     */
    public function setDefaultNotificationsConfs($userId)
    {
        $emailFrequency = NotificationsConfOpt::EMAIL_DAY;
        $smsFrequency = 0;
        $params = [
            'notifications_enabled' => 1,
            'notify_content_pubblication' => 1,
            'notify_comments' => 1,
        ];
        return $this->saveNotificationConf($userId, $emailFrequency, $smsFrequency, $params);
    }

    /**
     * This method add the notifications configurations for all users that these configurations are missing.
     * @return bool
     * @throws \yii\base\InvalidConfigException
     */
    public function addMissingUserNotificationsConfs()
    {
        /** @var ActiveQuery $query */
        $query = User::find();
        $query->select(['id']);
        $allUserIds = $query->column();

        /** @var NotificationConf $notificationConfModel */
        $notificationConfModel = $this->notifyModule->createModel('NotificationConf');

        /** @var ActiveQuery $queryNotify */
        $queryNotify = $notificationConfModel::find();
        $queryNotify->select(['user_id']);
        $allNotificationConfsUserIds = $queryNotify->column();

        $missingNotificationConfsUserIds = array_diff($allUserIds, $allNotificationConfsUserIds);

        $allOk = true;
        foreach ($missingNotificationConfsUserIds as $userId) {
            $ok = $this->setDefaultNotificationsConfs($userId);
            if (!$ok) {
                $allOk = false;
            }
        }

        return $allOk;
    }
}