NotificationNavDecorator.php 2.38 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\notify
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\views\widgets;
Marcello Pivanti's avatar
Marcello Pivanti committed
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

use yii\bootstrap\Widget;
use yii\bootstrap\Nav;
use yii\base\InvalidConfigException;
use Yii;
use yii\helpers\Html;
use yii\db\Query;

class NotificationNavDecorator extends Widget {

    /**
     *
     * @var Nav 
     */
    protected $nav;
    protected $position = 0;
    public $icon = 'glyphicon glyphicon-bell';

    public function init() {
        parent::init();

        if (empty($this->nav) === true) {
            throw new InvalidConfigException('Nav property must be provided');
        }

        if ($this->position > count($this->nav->items) - 1) {
            throw new InvalidConfigException('Position must be in range with items');
        }

        $this->decorate();
    }

    protected function decorate() {
        $item = $this->createItem();
        array_splice($this->nav->items, $this->position, 0, [$item]);
    }

    protected function createItem() {
        $nQuery = (new Query())->from(Notification::tableName())->where(['user_id' => Yii::$app->user->id]);
        $nCount = $nQuery->count();
        $notifications = $nQuery->select(['text', 'n' => 'COUNT(*)'])->groupBy(['path_info'])->all();
        $label = Html::tag('i', '', ['class' => $this->icon]);
        $item['label'] = &$label;
        if ($nCount > 0) {
            $label .= Html::tag('span', $nCount, ['class' => 'badge']);
            $items = [];

            foreach ($notifications as $notification) {
                $items[] = ['label' => Yii::t('notifications', $notification['text'], [
                        'count' => $notification['n']
                    ]), 'url' => $notification['path_info']];
            }
            $item['items'] = $items;
        }
        return $item;
    }

    public function setNav(Nav $nav) {
        $this->nav = $nav;
    }

    public function setPosition($pos) {
        if (is_numeric($pos) === false) {
            throw new InvalidConfigException('Position must be a numeric value');
        }
        $this->position = $pos;
    }

}