<?php

/* ====================
  [BEGIN_COT_EXT]
  Hooks=header.first
  Order=10
  [END_COT_EXT]
  ==================== */
/**
 * EasyAdmin for Giglance
 *
 * @package Header nav
 * @version 2.6.9
 * @author CoTEMPLATE
 * @copyright Copyright (c) CoTEMPLATE.com
 */
defined('COT_CODE') or die('Wrong URL');

if (!COT_AJAX && defined('COT_ADMIN') && $cfg['admintheme'] == 'easyadmin') {

    require_once cot_incfile('eadmin', 'module');

    $admin_MenuSide = array(
        'module' => array(
            'title' => 'Управление',
            'class' => '<i class="fa fa-puzzle-piece"></i>',
            'items' => array(

            )
        ),
        'ads' => (cot_plugin_active('ads') ? array(
            'title' => 'Реклама',
            'class' => '<i class="fa fa-users"></i>',
            'items' => array(
                array(
                    'title' => 'Объявления',
                    'url' => cot_url('admin', 'm=other&p=ads'),
                    'class' => 'label label-default',
                    'active' => ($m == 'other' && $p == 'ads'),
                ),
            )
        ) : false),
        'users' => (cot_module_active('users') ? array(
            'title' => $L['EA_users'],
            'class' => '<i class="fa fa-users"></i>',
            'items' => array(
              (cot_plugin_active('authbyuser') ? array(
                    'title' => 'Авторизация',
                    'url' => cot_url('admin', 'm=other&p=authbyuser'),
                    'icon_class' => 'fa fa-users',
                    'active' => ($m == 'other' && $p == 'authbyuser'),
                ) : false),
                array(
                    'title' => $L['EA_settings'],
                    'url' => cot_url('admin', 'm=config&n=edit&o=module&p=users'),
                    'class' => 'label label-default',
                    'active' => ($m == 'config' && $o == 'module' && $p == 'users'),
                ),
                array(
                    'title' => $L['EA_groups'],
                    'url' => cot_url('admin', array('m' => 'users')),
                    'icon_class' => 'fa fa-users',
                    'active' => ($m == 'users' && empty($n) && empty($o) && empty($p)),
                ),
                array(
                    'title' => $L['EA_uextra'],
                    'url' => cot_url('admin', 'm=extrafields&n=' . $db_users),
                    'class' => 'label label-default',
                    'active' => ($m == 'extrafields' && $n == $db_users),
                ),
                array(
                    'title' => $L['EA_manage'],
                    'url' => cot_url('eadmin', 'em=users'),
                    'class' => 'label label-default',
                    'active' => ($env['ext'] == 'eadmin' && $em == 'users'),
                ),
                (cot_plugin_active('whosonline') ? array(
                    'title' => 'Кто онлайн',
                    'url' => cot_url('plug', 'e=whosonline'),
                    'icon_class' => 'fa fa-users',
                    'active' => false,
                ) : false),
            )
        ) : false),
        'finance' => (cot_module_active('payments') ? array(
            'title' => $L['EA_finance'],
            'class' => '<i class="fa fa-usd"></i>',
            'items' => array(
                array(
                    'title' => $L['EA_settings'],
                    'url' => cot_url('admin', 'm=config&n=edit&o=module&p=payments'),
                    'class' => 'label label-default',
                    'active' => ($m == 'config' && $o == 'module' && $p == 'payments'),
                ),
                array(
                    'title' => $L['EA_history'],
                    'url' => cot_url('admin', 'm=payments'),
                    'icon_class' => 'fa fa-users',
                    'active' => ($m == 'payments'),
                ),
                (cot_plugin_active('easypay') ? array(
                    'title' => $L['EA_easypay'],
                    'url' => cot_url('admin', 'm=config&n=edit&o=plug&p=easypay'),
                    'icon_class' => 'fa fa-users',
                    'active' => ($m == 'config' && $o == 'plug' && $p == 'easypay'),
                ) : false),
                (cot_plugin_active('payorders') ? array(
                    'title' => $L['EA_payorders'],
                    'url' => cot_url('admin', 'm=other&p=payorders'),
                    'icon_class' => 'fa fa-users',
                    'active' => ($m == 'other' && $p == 'payorders'),
                ) : false),
                (cot_plugin_active('paypro') ? array(
                    'title' => $L['EA_pro'],
                    'url' => cot_url('admin', 'm=other&p=paypro'),
                    'icon_class' => 'fa fa-users',
                    'active' => ($m == 'other' && $p == 'paypro'),
                ) : false),
            )
        ) : false),

        'extras' => array(
            'title' => $L['Ea_extras'],
            'class' => '<i class="fa fa-info-circle"></i>',
            'items' => array(
                array(
                    'title' => $L['Extensions'],
                    'url' => cot_url('admin', 'm=extensions'),
                    'class' => 'label label-default',
                    'active' => ($m == 'extensions'),
                ),
                array(
                    'title' => $L['Log'],
                    'url' => cot_url('admin', 'm=log'),
                    'class' => 'label label-default',
                    'active' => ($m == 'log'),
                ),
                array(
                    'title' => $L['Info'],
                    'url' => cot_url('admin', 'm=infos'),
                    'class' => 'label label-default',
                    'active' => ($m == 'infos'),
                ),
            ),
        ),
        'configuration' => array(
            'title' => $L['EA_system'],
            'class' => '<i class="fa fa-cogs"></i>',
            'items' => array(
                array(
                    'title' => $L['EA_msettings'],
                    'url' => cot_url('admin', 'm=config&n=edit&o=core&p=main'),
                    'class' => 'label label-default',
                    'active' => ($m == 'config' && $o == 'core' && $p == 'main'),
                ),
                array(
                    'title' => $L['EA_titles'],
                    'url' => cot_url('admin', 'm=config&n=edit&o=core&p=title'),
                    'class' => 'label label-default',
                    'active' => ($m == 'config' && $o == 'core' && $p == 'title'),
                ),
                array(
                    'title' => $L['EA_locale'],
                    'url' => cot_url('admin', 'm=config&n=edit&o=core&p=locale'),
                    'class' => 'label label-default',
                    'active' => ($m == 'config' && $o == 'core' && $p == 'locale'),
                ),
                array(
                    'title' => $L['EA_slots'],
                    'url' => cot_url('admin', 'm=config&n=edit&o=core&p=menus'),
                    'class' => 'label label-default',
                    'active' => ($m == 'config' && $o == 'core' && $p == 'menus'),
                ),
                array(
                    'title' => $L['EA_perform'],
                    'url' => cot_url('admin', 'm=config&n=edit&o=core&p=performance'),
                    'class' => 'label label-default',
                    'active' => ($m == 'config' && $o == 'core' && $p == 'performance'),
                ),
                array(
                    'title' => $L['EA_secure'],
                    'url' => cot_url('admin', 'm=config&n=edit&o=core&p=security'),
                    'class' => 'label label-default',
                    'active' => ($m == 'config' && $o == 'core' && $p == 'security'),
                ),
                array(
                    'title' => $L['EA_sessions'],
                    'url' => cot_url('admin', 'm=config&n=edit&o=core&p=sessions'),
                    'class' => 'label label-default',
                    'active' => ($m == 'config' && $o == 'core' && $p == 'sessions'),
                ),
                array(
                    'title' => $L['EA_extra'],
                    'url' => cot_url('admin', 'm=extrafields'),
                    'class' => 'label label-default',
                    'active' => ($m == 'extrafields' && $n != $db_users),
                ),
            ),
            'subitems' => array(
              'themes' => array(
                  'title' => $L['EA_design'],
                  'class' => '<i class="fa fa-paint-brush"></i>',
                  'active' => (in_array($m, array('other', 'cache', 'extrafields')) && empty($p) ),
                  'items' => array(
                      array(
                          'title' => $L['EA_skins'],
                          'url' => cot_url('admin', 'm=config&n=edit&o=core&p=theme'),
                          'class' => 'label label-default',
                          'active' => ($m == 'config' && $o == 'core' && $p == 'theme'),
                      ),
                  ),
              ),
              'structure' => array(
                  'title' => $L['Structure'],
                  'class' => '<i class="fa fa-tasks"></i>',
                  'items' => array(
                      array(
                          'title' => $L['EA_categories'],
                          'url' => cot_url('eadmin', 'em=categories'),
                          'class' => 'label label-default',
                          'active' => ($env['ext'] == 'eadmin' && $em == 'categories'),
                      ),
                      array(
                          'title' => $L['EA_fmenus'],
                          'url' => cot_url('eadmin'),
                          'class' => 'label label-default',
                          'active' => ($env['ext'] == 'eadmin' && $em == 'menus'),
                      ),
                  )
              ),
              'plugins' => array(
                  'title' => 'Плагины',
                  'class' => '<i class="fa fa-puzzle-piece"></i>',
                  'items' => array(

                  )
              ),
            )
        )
    );

    $p = cot_import('p', 'G', 'ALP');
    $target = array();

    function ea_admin_other_cmp($pl_a, $pl_b) {
        if ($pl_a['pl_code'] == $pl_b['pl_code']) {
            return 0;
        }
        return ($pl_a['pl_code'] < $pl_b['pl_code']) ? -1 : 1;
    }

    $parsed_exts = array(
      'payments', 'paypro', 'payorders',
      'ads',
      'authbyuser'
    );

    $firstparsed = array(
      'ds' => 'История переписки',
      'market' => 'Услуги пользователей',
      'page' => 'Страницы сайта',
      'projects' => 'Запросы от покупателей',
      'comments' => 'Комментарии пользователей',
      'rewards' => 'Награды пользователей',
      'marketorders' => 'Администр. Заказов',
      'catcostplans' => 'Конфигурация стоимости услуги',
      'catextratypes' => 'Конфигурация доп параметров',
      'banlist' => 'Блокировка',
      'ds_supportrating' => 'Рейтинг СП',
      'certificates' => 'Сертификаты',
      'ds_report' => 'Жалобы на контент',
      'faq' => 'Вопрос-ответ',
    );

    foreach ($firstparsed as $pl_code => $pl_name) {
      $type = '';
      $pl_code_chk = $pl_code;

      if($pl_code == 'ds_supportrating' || $pl_code == 'ds_report') $pl_code_chk = 'ds';

      if (cot_module_active($pl_code_chk)) {
        $type = 'module';
      } elseif(cot_plugin_active($pl_code_chk)) {
        $type = 'plug';
      }
      if (!empty($type)) {
        $ext_info = ea_get_extensionparams($pl_code_chk, $type == 'module');
        $ext_url = $type == 'plug' ? cot_url('admin', 'm=other&p=' . $pl_code_chk) :
                    cot_url('admin', 'm=' . $pl_code_chk);
        $ext_active = $type == 'plug' ? ($m == 'other' && $p == $pl_code_chk) ? '1' : '' : $m == $pl_code_chk ? '1' : '';

        switch ($pl_code) {
          case 'ds':
            $ext_active = ($ext_active && cot_import('act', 'G', 'TXT') != 'supportrating');
            break;
          case 'ds_supportrating':
            $ext_url = cot_url('admin', 'm=ds&act=supportrating');
            $ext_active = ($ext_active && cot_import('act', 'G', 'TXT') == 'supportrating');
            break;
          case 'ds_report':
            $ext_url = cot_url('ds', 'support=1');
            $ext_active = false;
            break;
        }

        $nli = array(
          'title' => (!empty($pl_name) ? $pl_name : $ext_info['name']),
          'url' => $ext_url,
          'icon' => (!empty($ext_info['icon']) ? $ext_info['icon'] : 'system/admin/img/plugins32.png'),
          'class' => 'label label-default',
          'active' => $ext_active,
        );

        $admin_MenuSide['module']['items'][] = $nli;

        $parsed_exts[] = $pl_code_chk;
      }
    }

    foreach (array('module', 'plug') as $type) {
        if ($type == 'module') {
            $target = $cot_plugins['admin'];
            $title = $L['Modules'];
        } else {
            $target = $cot_plugins['tools'];
            $title = $L['Plugins'];
        }
        if (is_array($target)) {
            usort($target, 'ea_admin_other_cmp');
            foreach ($target as $pl) {
                if (!in_array($pl['pl_code'], $parsed_exts)) {
                    $ext_info = ea_get_extensionparams($pl['pl_code'], $type == 'module');

                    $nli = array(
                      'title' => $ext_info['name'],
                      'url' => $type == 'plug' ? cot_url('admin', 'm=other&p=' . $pl['pl_code']) :
                                cot_url('admin', 'm=' . $pl['pl_code']),
                      'icon' => (!empty($ext_info['icon']) ? $ext_info['icon'] : 'system/admin/img/plugins32.png'),
                      'class' => 'label label-default',
                      'active' => $type == 'plug' ? ($m == 'other' && $p == $pl['pl_code']) ? '1' : '' : $m == $pl['pl_code'] ? '1' : '',
                    );

                    $admin_MenuSide['configuration']['subitems']['plugins']['items'][] = $nli;
                }
            }
        }
    }
}