NotifyFrequencyAdvancedWidget.php 10.7 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\widgets
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\widgets;
Marcello Pivanti's avatar
Marcello Pivanti committed
17

waltermazza's avatar
waltermazza committed
18
19
20
21
22
23
24
use arter\amos\admin\models\UserProfile;
use arter\amos\core\helpers\Html;
use arter\amos\core\models\ModelsClassname;
use arter\amos\notificationmanager\AmosNotify;
use arter\amos\notificationmanager\models\NotificationConf;
use arter\amos\notificationmanager\models\NotificationconfNetwork;
use arter\amos\notificationmanager\models\NotificationsConfOpt;
Marcello Pivanti's avatar
Marcello Pivanti committed
25
26
27
28
29
30
use kartik\select2\Select2;
use yii\base\Widget;
use yii\data\ActiveDataProvider;

/**
 * Class NotifyFrequencyWidget
waltermazza's avatar
waltermazza committed
31
 * @package arter\amos\notificationmanager\widgets
Marcello Pivanti's avatar
Marcello Pivanti committed
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
 */
class NotifyFrequencyAdvancedWidget extends Widget
{
    /**
     * @var string $layout It's permitted to set {emailNotifyFrequency} and {smsNotifyFrequency}. Default to {emailNotifyFrequency}.
     */
    public $layout = '{generalNotifyFrequency}';
    
    /**
     * @var UserProfile $model
     */
    private $model;
    
    /**
     * @var array $containerOptions Options array
     */
    private $containerOptions = [];
    
    /**
     * @var array $emailContainerOptions Options array
     */
    private $emailContainerOptions = [];
    
    /**
     * @var array $smsContainerOptions Options array
     */
    private $smsContainerOptions = [];
    
    /**
     * @var NotificationConf|null $notificationConf
     */
    private $notificationConf = null;

    /**
     * @var null
     */
    private $defaultValueEmail = 2;

    /**
     * @var AmosNotify $notifyModule
     */
    protected $notifyModule = null;

    /**
     * @inheritdoc
     */
    public function init()
    {
        parent::init();

        $this->notifyModule = AmosNotify::instance();

        if (is_null($this->model)) {
            throw new \Exception(AmosNotify::t('amosnotify', 'NotifyFrequencyWidget: missing model'));
        }

        /** @var NotificationConf $notificationConfModel */
        $notificationConfModel = $this->notifyModule->createModel('NotificationConf');
        $this->notificationConf = $notificationConfModel::findOne(['user_id' => $this->model->user_id]);
        if(empty($this->notificationConf)){
            /** @var NotificationConf $notificationConfModel */
            $notificConf = $this->notifyModule->createModel('NotificationConf');
            $notificConf->user_id = $this->model->user_id;
            $this->notificationConf = $notificConf;
        }
    }
    
    /**
     * @return null
     */
    public function getDefaultValueEmail()
    {
        return $this->defaultValueEmail;
    }

    /**
     * @param null $defaultValueEmail
     */
    public function setDefaultValueEmail(NotificationsConfOpt $defaultValueEmail)
    {
        $this->defaultValueEmail = $defaultValueEmail;
    }
    
    /**
     * @return UserProfile
     */
    public function getModel()
    {
        return $this->model;
    }
    
    /**
     * @param UserProfile $model
     */
    public function setModel($model)
    {
        $this->model = $model;
    }
    
    /**
     * @return array
     */
    public function getContainerOptions()
    {
        return $this->containerOptions;
    }
    
    /**
     * @param array $containerOptions
     */
    public function setContainerOptions($containerOptions)
    {
        $this->containerOptions = $containerOptions;
    }
    
    /**
     * @return array
     */
    public function getEmailContainerOptions()
    {
        return $this->emailContainerOptions;
    }
    
    /**
     * @param array $emailContainerOptions
     */
    public function setEmailContainerOptions($emailContainerOptions)
    {
        $this->emailContainerOptions = $emailContainerOptions;
    }
    
    /**
     * @return array
     */
    public function getSmsContainerOptions()
    {
        return $this->smsContainerOptions;
    }
    
    /**
     * @param array $smsContainerOptions
     */
    public function setSmsContainerOptions($smsContainerOptions)
    {
        $this->smsContainerOptions = $smsContainerOptions;
    }
    
    /**
     * @return string
     */
    public function getLayout()
    {
        return $this->layout;
    }
    
    /**
     * @return string
     */
    public static function emailFrequencySelectorId()
    {
        return 'email-frequency-selector-id';
    }
    
    /**
     * @return string
     */
    public static function emailFrequencySelectorName()
    {
        return 'email_frequency_selector_name';
    }
    
    /**
     * @return string
     */
    public static function smsFrequencySelectorId()
    {
        return 'sms-frequency-selector-id';
    }
    
    /**
     * @return string
     */
    public static function smsFrequencySelectorName()
    {
        return 'sms_frequency_selector_name';
    }
    
    /**
     * @inheritdoc
     */
    public function run()
    {
        $content = Html::beginTag('div', $this->getContainerOptions());
        $content .= preg_replace_callback("/{\\w+}/", function ($matches) {
            $content = $this->renderSection($matches[0]);
            return $content === false ? $matches[0] : $content;
        }, $this->layout);
        $content .= Html::endTag('div');
        return $content;
    }
    
    /**
     * Renders a section of the specified name.
     * If the named section is not supported, false will be returned.
     * @param string $name the section name, e.g., `{summary}`, `{items}`.
     * @return string|boolean the rendering result of the section, or false if the named section is not supported.
     */
    public function renderSection($name)
    {
        switch ($name) {
            case '{emailNotifyFrequency}':
                return $this->renderEmailNotifyFrequencySelector();
            case '{smsNotifyFrequency}':
                return $this->renderSmsNotifyFrequencySelector();
            case '{generalNotifyFrequency}':
                return $this->renderGeneralNotifyFrequencySelector();
            default:
                return false;
        }
    }
    
    /**
     * Render the email notify frequency selector.
     * @return string
     */
    public function renderEmailNotifyFrequencySelector()
    {
        /** @var NotificationsConfOpt $notificationConfOpt */
        $notificationConfOpt = $this->notifyModule->createModel('NotificationsConfOpt');
        $html = Html::beginTag('div', $this->getEmailContainerOptions());
        $widgetConf = [
            'id' => self::emailFrequencySelectorId(),
            'name' => self::emailFrequencySelectorName(),
            'data' => $notificationConfOpt::emailFrequencyValueAndLabels(),
            'options' => [
                'lang' => substr(\Yii::$app->language, 0, 2),
                'multiple' => false,
                'placeholder' => AmosNotify::t('amosnotify', 'Select/Choose') . '...',
            ]
        ];
        if (!is_null($this->notificationConf)  && !$this->notificationConf->isNewRecord) {
            $widgetConf['value'] = $this->notificationConf->email;
        }else{
            if(!empty($this->defaultValueEmail)){
                $widgetConf ['value'] = $this->defaultValueEmail;
            }else{
                $widgetConf ['value'] = NotificationsConfOpt::EMAIL_DAY;
            }

        }

        $html .= Select2::widget($widgetConf);
        $html .= Html::endTag('div');

        return $html;
    }
    
    /**
     * Render the sms notify frequency selector.
     * @return string
     */
    public function renderSmsNotifyFrequencySelector()
    {
        /** @var NotificationsConfOpt $notificationConfOpt */
        $notificationConfOpt = $this->notifyModule->createModel('NotificationsConfOpt');
        $html = Html::beginTag('div', $this->getSmsContainerOptions());
        $widgetConf = [
            'id' => self::smsFrequencySelectorId(),
            'name' => self::smsFrequencySelectorName(),
            'data' => $notificationConfOpt::smsFrequencyValueAndLabels(),
            'options' => [
                'lang' => substr(\Yii::$app->language, 0, 2),
                'multiple' => false,
                'placeholder' => AmosNotify::t('amosnotify', 'Select/Choose') . '...',
            ]
        ];

        if (!is_null($this->notificationConf) &&  !$this->notificationConf->isNewRecord) {
            $widgetConf['value'] = $this->notificationConf->sms;
        }
        $html .= Select2::widget($widgetConf);
        $html .= Html::endTag('div');
        return $html;
    }

    /**
     * Render the email notify frequency selector.
     * @return string
     */
    public function renderGeneralNotifyFrequencySelector()
    {
        /** @var NotificationsConfOpt $notificationConfOpt */
        $notificationConfOpt = $this->notifyModule->createModel('NotificationsConfOpt');
        $widgetConfData = $notificationConfOpt::emailFrequencyValueAndLabels();

        $dataProviderNetwork = null;
        $moduleCommunity = \Yii::$app->getModule('community');
        
        if($moduleCommunity){
waltermazza's avatar
waltermazza committed
331
            $model = new \arter\amos\community\models\Community();
Marcello Pivanti's avatar
Marcello Pivanti committed
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
            $query = $model->getUserNetworkQuery($this->model->user_id);
            $dataProviderNetwork = new ActiveDataProvider([
                'query' => $query,
            ]);
//            foreach ($query->all() as $community){
//                pr($community->name);
//            }
        }
        $notificationNetworkValues = [];
        $modelClassname = ModelsClassname::find()->andWhere(['module' => 'community'])->one();
        if($modelClassname) {
            /** @var NotificationconfNetwork $notificationConfNetworkModel */
            $notificationConfNetworkModel = $this->notifyModule->createModel('NotificationconfNetwork');
            $notificationNetwork = $notificationConfNetworkModel::find()
                ->andWhere(['models_classname_id' => $modelClassname->id])
                ->andWhere(['user_id' => $this->model->user_id])->all();
            foreach ($notificationNetwork as $conf){
                $notificationNetworkValues[$conf->record_id] = $conf->email;
            }
        }

        $htmlFrequencySelector = $this->renderEmailNotifyFrequencySelector();
        return $this->render('general_notify', [
            'widget' => $this,
            'htmlFrequencySelector' => $htmlFrequencySelector,
            'notificationConf' => $this->notificationConf,
            'notificationNetworkValues' => $notificationNetworkValues,
            'dataProviderNetwork' => $dataProviderNetwork,
            'widgetConfData'=> $widgetConfData
        ]);
    }
}