<?php
/**
 * app/plugins/notifications/plugin.php
 *
 * This houses all of the main plugin info and functionality.
 *
 * @package TP Notifications
 * @version 3.0
 * @author  Joey Kimsey <Joey@thetempusproject.com>
 * @link    https://TheTempusProject.com
 * @license https://opensource.org/licenses/MIT [MIT LICENSE]
 */
namespace TheTempusProject\Plugins;

use TheTempusProject\TheTempusProject as App;
use TheTempusProject\Classes\Plugin;
use TheTempusProject\Models\Notification;
use TheTempusProject\Houdini\Classes\Components;
use TheTempusProject\Houdini\Classes\Views;

class Notifications extends Plugin {
    public $pluginName = 'TP Notifications';
    public $pluginAuthor = 'JoeyK';
    public $pluginWebsite = 'https://TheTempusProject.com';
    public $modelVersion = '1.0';
    public $pluginVersion = '3.0';
    public $pluginDescription = 'A simple plugin which adds a site wide notification system.';
    public $permissionMatrix = [
        'sendNotifications' => [
            'pretty' => 'Can send notifications',
            'default' => false,
        ],
    ];
    public $admin_links = [
        [
            'text' => '<i class="fa fa-fw fa-bell"></i> Notify',
            'url' => '{ROOT_URL}admin/notifications',
        ],
    ];
    private static $loaded = false;
    public function __construct( $load = false ) {
        parent::__construct( $load );
        if ( $this->checkEnabled() && App::$isLoggedIn ) {
            $notifications = new Notification;
            Components::set( 'notificationCount', $notifications->getUnreadCount( App::$activeUser->ID ) );
            if ( $notifications->getUnreadCount( App::$activeUser->ID ) > 0 ) {
                $messageBadge = Views::simpleView( 'notifications.badge' );
            } else {
                $messageBadge = '';
            }
            Components::set( 'NBADGE', $messageBadge );
            if ( ! self::$loaded ) {
                if ( App::$isLoggedIn ) {
                    Components::set( 'recentNotifications', Views::simpleView( 'notifications.nav.recentNotificationsDropdown', $notifications->getByUser( 10 ) ) );
                } else {
                    Components::set( 'recentNotifications', '' );
                }
                App::$topNavRight .= '{recentNotifications}';
                App::$topNavRightDropdown .= '<li><a href="{ROOT_URL}notifications" class="dropdown-item"><i class="fa fa-fw fa-bell"></i> Notifications {NBADGE}</a></li>';
                self::$loaded = true;
            }
        }
    }
}
