<?php defined('MW_PATH') || exit('No direct script access allowed');

/**
 * LeftSideNavigationWidget
 * 
 * @package MailWizz EMA
 * @author Serban George Cristian <cristian.serban@mailwizz.com> 
 * @link http://www.mailwizz.com/
 * @copyright 2013-2014 MailWizz EMA (http://www.mailwizz.com)
 * @license http://www.mailwizz.com/license/
 * @since 1.0
 */
 
class LeftSideNavigationWidget extends CWidget
{
    public function run()
    {
        $sections   = array();
        $hooks      = Yii::app()->hooks;
        $controller = $this->controller;
        $route      = $controller->route;
        $priority   = 0;
        $request    = Yii::app()->request;
        
        Yii::import('zii.widgets.CMenu');
        
        $menuItems = array(
            'dashboard' => array(
                'name'      => Yii::t('app', 'Dashboard'),
                'icon'      => 'glyphicon-dashboard',
                'active'    => 'dashboard',
                'route'     => array('dashboard/index'),
            ),
            'articles' => array(
                'name'      => Yii::t('app', 'Articles'),
                'icon'      => 'glyphicon-book',
                'active'    => 'article',
                'route'     => null,
                'items'     => array(
                    array('url' => array('articles/index'), 'label' => Yii::t('app', 'View all articles'), 'active' => strpos($route, 'articles/index') === 0),
                    array('url' => array('article_categories/index'), 'label' => Yii::t('app', 'View all categories'), 'active' => strpos($route, 'article_categories') === 0),
                ),
            ),
            'users' => array(
                'name'      => Yii::t('app', 'Users'),
                'icon'      => 'glyphicon-user',
                'active'    => 'users',
                'route'     => array('users/index'),
            ),
            'customers' => array(
                'name'      => Yii::t('app', 'Customers'),
                'icon'      => 'glyphicon-user',
                'active'    => 'customers',
                'route'     => array('customers/index'),
            ),
            'delivery-servers' => array(
                'name'      => Yii::t('app', 'Delivery servers'),
                'icon'      => 'glyphicon-send',
                'active'    => 'delivery_servers',
                'route'     => array('delivery_servers/index'),
            ),
            'bounce-servers' => array(
                'name'      => Yii::t('app', 'Bounce servers'),
                'icon'      => 'glyphicon-filter',
                'active'    => 'bounce_servers',
                'route'     => array('bounce_servers/index'),
            ),
            'list-page-type' => array(
                'name'      => Yii::t('app', 'List page types'),
                'icon'      => 'glyphicon-list-alt',
                'active'    => 'list_page_type',
                'route'     => array('list_page_type/index'),
            ),
            'blacklist' => array(
                'name'      => Yii::t('app', 'Email blacklist'),
                'icon'      => 'glyphicon-ban-circle',
                'active'    => 'email_blacklist',
                'route'     => array('email_blacklist/index'),
            ),

            'extend' => array(
                'name'      => Yii::t('app', 'Extend'),
                'icon'      => 'glyphicon-plus-sign',
                'active'    => array('extensions', 'theme', 'languages', 'ext'),
                'route'     => null,
                'items'     => array(
                    array('url' => array('extensions/index'), 'label' => Yii::t('app', 'Extensions'), 'active' => strpos($route, 'ext') === 0),
                    array('url' => array('theme/index'), 'label' => Yii::t('app', 'Themes'), 'active' => strpos($route, 'theme') === 0),
                    array('url' => array('languages/index'), 'label' => Yii::t('app', 'Languages'), 'active' => strpos($route, 'languages') === 0),
                ),
            ),
            
            'ip-location-services' => array(
                'name'      => Yii::t('app', 'Ip location services'),
                'icon'      => 'glyphicon-map-marker',
                'active'    => 'ip_location_services',
                'route'     => array('ip_location_services/index'),
            ),
            'settings' => array(
                'name'      => Yii::t('app', 'Settings'),
                'icon'      => 'glyphicon-cog',
                'active'    => 'settings',
                'route'     => null,
                'items'     => array(
                    array('url' => array('settings/index'), 'label' => Yii::t('app', 'Common'), 'active' => strpos($route, 'settings/index') === 0),
                    array('url' => array('settings/import_export'), 'label' => Yii::t('app', 'Import/Export'), 'active' => strpos($route, 'settings/import_export') === 0),
                    array('url' => array('settings/email_templates'), 'label' => Yii::t('app', 'Email templates'), 'active' => strpos($route, 'settings/email_templates') === 0),
                    array('url' => array('settings/cron'), 'label' => Yii::t('app', 'Cron'), 'active' => strpos($route, 'settings/cron') === 0),
                    array('url' => array('settings/email_blacklist'), 'label' => Yii::t('app', 'Email blacklist'), 'active' => strpos($route, 'settings/email_blacklist') === 0),
                    array('url' => array('settings/campaign_attachments'), 'label' => Yii::t('app', 'Campaign attachments'), 'active' => strpos($route, 'settings/campaign_attachments') === 0),
                    array('url' => array('settings/campaign_template_tags'), 'label' => Yii::t('app', 'Campaign template tags'), 'active' => strpos($route, 'settings/campaign_template_tags') === 0),
                    array('url' => array('settings/campaign_options'), 'label' => Yii::t('app', 'Campaign options'), 'active' => strpos($route, 'settings/campaign_options') === 0),
                ),
            ),
            'misc' => array(
                'name'      => Yii::t('app', 'Miscellaneous'),
                'icon'      => 'glyphicon-bookmark',
                'active'    => 'misc',
                'route'     => null,
                'items'     => array(
                    array('url' => array('misc/application_log'), 'label' => Yii::t('app', 'Application log'), 'active' => strpos($route, 'misc/application_log') === 0),
                ),
            ),
        );
        
        $menuItems = (array)Yii::app()->hooks->applyFilters('backend_left_navigation_menu_items', $menuItems);

        foreach ($menuItems as $key => $data) {
            
            $menu = new CMenu();
            
            if (!empty($data['items']) && is_array($data['items'])) {
                $menu->items = $data['items'];
                $data['route'] = '#' . $menu->id;
            }
            
            $menu->htmlOptions = array(
                'class' => 'nav nav-list collapse', 
                'id'    => $menu->id
            );
            
            $active = false;
            if (is_string($data['active']) && strpos($route, $data['active']) === 0) {
                $menu->htmlOptions['class'].=' in';
                $active = true;
            } elseif (is_array($data['active'])) {
                foreach ($data['active'] as $in) {
                    if (strpos($route, $in) === 0) {
                        $menu->htmlOptions['class'].=' in';
                        $active = true;
                        break;
                    }
                }
            }

            $htmlOptions = array('class' => "nav-header".($active ? " active":''));
            if (!empty($menu->items)) {
                $htmlOptions['data-toggle'] = 'collapse';
            }
            
            $_section = array(
                'heading'   => CHtml::link('<i class="glyphicon '.$data['icon'].'"></i> '.$data['name'], $data['route'], $htmlOptions),
                'menu'      => $menu,
                'priority'  => (++$priority) * 10,
            );
            
            $sections[] = $_section;    
        }
        
        // make sure all the sections are correctly formatted.
        $sort = array();
        foreach ($sections as $index => $section) {
            if (!isset($section['heading'], $section['menu']) || !($section['menu'] instanceof CMenu)) {
                unset($sections[$index]);
                continue;
            }
            $sort[] = isset($section['priority']) ? (int)$section['priority'] : 0;
        }
        
        if (empty($sections)) {
            return;
        }
        array_multisort($sort, $sections);
        
        echo '<div class="sidebar-nav">';
        foreach ($sections as $section) {
            echo $section['heading'];
            $section['menu']->run();
        }
        echo '</div>';
    }
}