<?php
/**
* 2024 Anvanto
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
*
*  @author    Anvanto <anvantoco@gmail.com>
*  @copyright 2024 Anvanto
*  @license   http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
*/

if (!defined('_PS_VERSION_')) {
    exit;
}
 
require_once _PS_MODULE_DIR_ . 'an_sizeguide/classes/anStatisticsSizeGuide.php';

class AdminAnsizeguideStatisticsController extends ModuleAdminController
{
    protected $_module = null;
    
    protected $position_identifier = 'id_views';

    public function __construct()
    {
        $this->bootstrap = true;
        $this->context = Context::getContext();
        $this->table = 'an_size_guide_widgets_views';
        $this->identifier = 'id_views';
        $this->className = 'anStatisticsSizeGuide';
        $this->lang = false;

        $this->addRowAction('delete');
 
        $this->name = 'AdminAnsizeguideStatisticsController';
        
        parent::__construct();
        
        $this->fields_list = array(

            'id_widget' => array(
                'title' => $this->l('ID Size Guide'), 
                'search'  => false
            ),

            'title' => array(
                'title' => $this->l('Title'), 
                'search'  => false,
                'width' => 150
            ),       

            'views' => array(
                'title' => $this->l('Views'), 
                'search'  => false,
                'width' => 50
            ),        
            
            'date_add' => array(
                'title' => $this->l('Date'), 
                'search'  => false
            ),        
    
        );

    }

    public function l($string, $class = null, $addslashes = false, $htmlentities = true)
    {
        return parent::l($string, pathinfo(__FILE__, PATHINFO_FILENAME), $addslashes, $htmlentities);
    }    
        
    public function renderList()
    {
        return parent::renderList() . $this->module->topPromo();
    } 
    
    public function getList($id_lang, $order_by = null, $order_way = null, $start = 0, $limit = null, $id_lang_shop = false)
    {
        parent::getList($id_lang, $order_by, $order_way, $start, $limit, $id_lang_shop);
        foreach ($this->_list as &$list) {
            $sizeGuide = new AnSizeGuideWidgets($list['id_widget'], $this->context->language->id);
            $list['title'] = $sizeGuide->sizeguide_title;
        }        
    }        

    public function setMedia($isNewTheme = false)
    {
        parent::setMedia($isNewTheme);
    }   

    public function initToolBarTitle()
    {
        $this->toolbar_title[] = $this->l('Size Guide: Statistics');
    }

    public function initToolbar()
    {
        $parent = parent::initToolbar();
        if (array_key_exists('new', $this->toolbar_btn)) {
            unset($this->toolbar_btn['new']);
        }

        return $parent;
    }    

    public function initHeader()
    {
        parent::initHeader();

        $id_lang = $this->context->language->id;
        $link = $this->context->link;
        $tabs = &$this->context->smarty->tpl_vars['tabs']->value;
       
        foreach ($tabs as &$tab0) {
            if ($tab0['class_name'] == 'IMPROVE') {
                foreach ($tab0['sub_tabs'] as &$tab1) {
                    if ($tab1['class_name'] == 'AdminParentModulesSf') {
                        
                        foreach ($tab1['sub_tabs'] as &$tab2) {
                            if ($tab2['class_name'] == 'AdminAnsizeguideStatistics') {
                                $sub_tabs = &$tab2['sub_tabs'];

                                $tab = Tab::getTab($id_lang, Tab::getIdFromClassName('AdminAnsizeguideWidgets'));
                                $tab['current'] = false;
                                $tab['href'] = $link->getAdminLink('AdminAnsizeguideWidgets');
                                $tab['active'] = true;
                                $tab['name'] = $this->l('Size Guide');
                                $sub_tabs[] = $tab;

                                $tab = Tab::getTab($id_lang, Tab::getIdFromClassName('AdminAnsizeguideStatistics'));
                                $tab['current'] = true;
                                $tab['href'] = $link->getAdminLink('AdminAnsizeguideStatistics');
                                $tab['active'] = true;
                                $tab['name'] = $this->l('Statistics');
                                $sub_tabs[] = $tab;

                                $tab = Tab::getTab($id_lang, Tab::getIdFromClassName('AdminAnsizeguideSettings'));
                                $tab['current'] = false;
                                $tab['href'] = $link->getAdminLink('AdminAnsizeguideSettings');
                                $tab['active'] = true;
                                $sub_tabs[] = $tab;
                                
                                break;
                            }
                        }
                    //    break;
                    }
                }
              //  break;
            }
        }
    }  
    
    public function initContent()
    {
        $this->context->smarty->assign('current_tab_level', 3);
        return parent::initContent();
    }

    public function processDelete()
    {
        if (_PS_MODE_DEMO_) {
			$this->errors[] = $this->module->demoModeMessage;
			return false;
		}             

        return parent::processDelete();
    }  
}