<?php
/**
 * Copyright since 2007 PrestaShop SA and Contributors
 * PrestaShop is an International Registered Trademark & Property of PrestaShop SA
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License version 3.0
 * that is bundled with this package in the file LICENSE.md.
 * It is also available through the world-wide-web at this URL:
 * https://opensource.org/licenses/AFL-3.0
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@prestashop.com so we can send you a copy immediately.
 *
 * @author    PrestaShop SA and Contributors <contact@prestashop.com>
 * @copyright Since 2007 PrestaShop SA and Contributors
 * @license   https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
 */
if (!defined('_PS_VERSION_')) {
    exit;
}

class SpmreviewsadvuserModuleFrontController extends ModuleFrontController
{
    public $php_self;
	public function init()
	{
    	parent::init();
	}
	
	public function setMedia()
	{
		parent::setMedia();
        $this->addJqueryPlugin(array('fancybox'));

        $name_module = 'spmreviewsadv';


        if(
            Configuration::get($name_module.'d_eff_rev_u') != "disable_all_effects"
        ) {

            $this->context->controller->addJs(__PS_BASE_URI__ . 'modules/' . $name_module . '/views/js/wow.js');
            $this->context->controller->addCSS(__PS_BASE_URI__ . 'modules/' . $name_module . '/views/css/animate.css');

        }

        $this->context->controller->addCSS(__PS_BASE_URI__ . 'modules/' . $name_module . '/views/css/font-custom.min.css');


    }


	/**
	 * @see FrontController::initContent()
	 */
	public function initContent()
	{

        $name_module = 'spmreviewsadv';
        $this->php_self = 'module-'.$name_module.'-user';


		parent::initContent();





        $is_uprof = Configuration::get($name_module.'is_uprof');
        if (!$is_uprof)
            Tools::redirect('index.php');

        include_once(_PS_MODULE_DIR_.$name_module.'/spmreviewsadv.php');
        $obj_spmreviewsadv = new spmreviewsadv();

        include_once(_PS_MODULE_DIR_.$name_module.'/classes/userprofileg.class.php');
        $obj = new userprofileg();

        $user_id = (int)Tools::getValue('uid');


        include_once(_PS_MODULE_DIR_.$name_module.'/classes/spmreviewsadvhelp.class.php');
        $obj_spmreviewsadvhelp = new spmreviewsadvhelp();


        $data_urls = $obj_spmreviewsadvhelp->getSEOURLs();
        $users_url = $data_urls['users_url'];

        $rev_url = $data_urls['rev_url'];

        $my_account = $data_urls['my_account'];


        if(!$user_id)
            Tools::redirect($users_url);

        $info = $obj->getShopperInfo(array('shopper_id' => $user_id));


        if(sizeof($info['customer'])==0)
            Tools::redirect($users_url);



        $this->context->smarty->assign(array(
            $name_module.'customer' => $info['customer']
        ));

        $_data_translate = $obj_spmreviewsadv->translateCustom();


        $title = @$info['customer'][0]['firstname']." " .@$info['customer'][0]['lastname']. " ".$_data_translate['profile'];

        //$title = @$info['customer'][0]['firstname']." " .Tools::substr(@$info['customer'][0]['lastname'], 0, 1). " ".$_data_translate['profile'];



        $obj_spmreviewsadv->setSEOUrls();
        $obj_spmreviewsadv->settingsHooks();

        ## product reviews for customer ##

        $_prefix_product = $obj_spmreviewsadv->getPrefixProductReviews();

        $gp = (int)Tools::getValue('gp');
        $step = (int)Configuration::get($name_module.'perpageu'.$_prefix_product);


        $start = (int)(($gp - 1)*$step);
        if($start<0)
            $start = 0;


        $frat = Tools::getValue('frat');

        $search = Tools::getValue("search");
        $is_search = 0;

        ### search ###
        if(Tools::strlen($search)>0){
            $is_search = 1;

        }



        $this->context->smarty->assign($name_module.'is_search', $is_search);
        $this->context->smarty->assign($name_module.'search', $search);
        $this->context->smarty->assign($name_module.'frat', $frat);
        $this->context->smarty->assign($name_module.'gp', $gp);
        $this->context->smarty->assign($name_module.'isgp', Tools::isSubmit('gp'));




        $data_translate = $obj_spmreviewsadv->translateCustom();

        $data_my_reviews = $obj_spmreviewsadvhelp->getMyReviews(array('id_customer'=>$user_id,'start'=>$start,'step'=>$step,'frat'=>$frat,'is_search'=>$is_search,'search'=>$search));

        $count_reviews = $data_my_reviews['count_all'];



        $paging = $obj_spmreviewsadvhelp->paging17(array('start'=>$start,
                'step'=> $step,
                'count' => $count_reviews,
                'frat'=>$frat,
                'is_search'=>$is_search,
                'search'=>$search,
                'action'=>'userpagereviews',
                'id_customer'=>$user_id
            )
        );

        $this->context->smarty->assign($name_module.'d_eff_rev' , Configuration::get($name_module.'d_eff_rev_u'));

        $this->context->smarty->assign(array(
            $name_module.'user_id' => $user_id,
            'reviews_all' => $data_my_reviews['reviews'],

            $name_module.'paging' => $paging,
            $name_module.'page_text' => $data_translate['page'],

            $name_module.'my_a_link'=> $my_account,

            $name_module.'rev_url' => $rev_url,

            $name_module.'is_gender' => Configuration::get($name_module.'is_gender'),
            $name_module.'is_age' => Configuration::get($name_module.'is_age'),



        ));

        ## product reviews for customer ##





        if(version_compare(_PS_VERSION_, '1.7', '>')) {
            $this->context->smarty->tpl_vars['page']->value['meta']['title'] = $title;
            $this->context->smarty->tpl_vars['page']->value['meta']['description'] = $title;
            $this->context->smarty->tpl_vars['page']->value['meta']['keywords'] = $title;
        }

        $this->context->smarty->assign('meta_title' , $title);
        $this->context->smarty->assign('meta_description' , $title);
        $this->context->smarty->assign('meta_keywords' , $title);


        $this->context->smarty->assign($name_module.'is_proftab', Configuration::get($name_module.'is_proftab'));



        if(version_compare(_PS_VERSION_, '1.7', '>')) {
            $this->setTemplate('module:' . $name_module . '/views/templates/front/user17.tpl');
        }else {
            $this->setTemplate('user.tpl');
        }


    }
}