<?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'),
            ),
            'lists' => array(
                'name'      => Yii::t('app', 'Lists'),
                'icon'      => 'glyphicon-list-alt',
                'active'    => 'list',
                'route'     => array('lists/index'),
            ),
            'campaigns' => array(
                'name'      => Yii::t('app', 'Campaigns'),
                'icon'      => 'glyphicon-envelope',
                'active'    => 'campaign',
                'route'     => array('campaigns/index'),
            ),
            'templates' => array(
                'name'      => Yii::t('app', 'Templates'),
                'icon'      => 'glyphicon-text-width',
                'active'    => 'templates',
                'route'     => array('templates/index'),
            ),
            'api-keys' => array(
                'name'      => Yii::t('app', 'Api keys'),
                'icon'      => 'glyphicon-star',
                'active'    => 'api_keys',
                'route'     => array('api_keys/index'),
            ),
        );

        if (!Yii::app()->options->get('system.customer.action_logging_enabled', true)) {
            unset($menuItems['dashboard']);
        }
        
        $menuItems = (array)Yii::app()->hooks->applyFilters('customer_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>';
    }
}