H
H
hollanditkzn2017-07-20 15:25:05
Yii
hollanditkzn, 2017-07-20 15:25:05

How to make a notification widget?

How to implement a widget that showed the number of records of unread notifications, when you click on it, then there are records of unread notifications. And when you click on some notification, you go to the order. I have already roughly implemented this so that I specified the entire selection in the controller, and built the appearance in the template. But too bulky code in controllers comes out. But it becomes inconvenient when it is created by an action. For example, this is how VKontakte or other social networks implemented it, you need almost the same effect ae71a68423f74db0a5460478105d335d.png
.
I googled something sensible, I did not find a solution to my own problem

class Notifications extends Widget
{
    public function run()
    {
        return $this->renderItems();
    }

    /**
     * Creates a window for the notification
     * @method string renderCount()
     * @return string
     */
    public function renderItems()
    {
//Берем записи не просмотренные уведомлений у определенного пользователя
        $notifModel = Notification::find();
        $notifications = $notifModel->where(['id_user' == Yii::$app->user->id, 'active' => true])->all();
//добавляем количество записей
        $this->renderCount();
//под поределенную категория уведомлений делаем свой логотип вначале
        foreach($notifications as $notification){
            $date = date('Y-m-d H:i:s', time());
            if ($notification->category == 0) {
                $notif = '<span class="glyphicon glyphicon-road"></span> '.$notification->name.'<br>';
            } elseif ($notification->category == 1) {
                $notif = '<span class="glyphicon glyphicon-ok"></span> '.$notification->name.'<br>';
            } elseif ($notification->category == 2) {
                $notif = '<span class="glyphicon glyphicon-file"></span> '.$notification->name.'<br>';
            } elseif ($notification->category == 4 && $notification->srok <= $date){
                $notif = 'Напоминание о заказе №'.$notification->id_zakaz.' '.$notification->srok;
            } elseif ($notification->category == 4 && $notification->srok >= $date){
                $notif = '';
            }
//При нажатие на запись идет редирект на заказ 
            echo Html::a($notif.'<br>', ['notification/notification', 'id' => $notification->id_zakaz], ['id' => $notification->id_zakaz, 'class' => 'zakaz', 'data-key' => $notification->id_zakaz]);
        };
//отрисовываем окно уведомлений
        $formNotif = '<div class="notification-container hidden" id="notification-container">
                    <div class="notification-content">'.$notifications.'</div>
                <div class="notification-footer">'.Html::a('Прочитать все напоминание', ['notification/index']).'</div>
            </div>';
        return $formNotif;
    }

    /**
     * Notification count row
     * @return string
     */
    public function renderCount()
    {
//Получаем количество не просмотренные уведомлений у определенного пользователя
        $notifModel = Notification::find();
        $notification = $notifModel->where(['id_user' == Yii::$app->user->id, 'active' => true])->count();если количество больше 50, то указывается 50+
        if ($notification > 50) {
            $notifications = '50+';
        } elseif ($notification < 1) {
            $notifications = '';
        } else {
            $notifications = $notification;
        }
//отрисовываем логотип и полученное число
        return '<span class="glyphicon glyphicon-bell" style="font-size:21px"></span><span class="badge pull-right">'.$notifications.'</span>';
    }
}
Here is the code on github
https://github.com/hollandit/crm/blob/development/...

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question