<?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 SpmreviewsadvAjaxusersModuleFrontController extends ModuleFrontController
{

    public function postProcess()
    {

        header("Access-Control-Allow-Origin: *");
        /*$HTTP_X_REQUESTED_WITH = isset($_SERVER['HTTP_X_REQUESTED_WITH']) ? $_SERVER['HTTP_X_REQUESTED_WITH'] : '';
        if ($HTTP_X_REQUESTED_WITH != 'XMLHttpRequest') {
            exit;
        }*/


        $name_module = 'spmreviewsadv';
        include_once(_PS_MODULE_DIR_.$name_module.'/spmreviewsadv.php');
        $obj_spmreviewsadv = new spmreviewsadv();


        $token = Tools::getValue('token');
        $token_orig = $obj_spmreviewsadv->getokencron();
        if($token_orig !=$token)
            die('Invalid token.');




        ob_start();
        $status = 'success';
        $message = '';
        $html = '';
        $paging = '';

        $action = Tools::getValue('action');





        switch ($action){
            case 'allusers':




                $obj_spmreviewsadv->setSEOUrls();



                include_once(_PS_MODULE_DIR_.$name_module.'/classes/userprofileg.class.php');
                $obj = new userprofileg();




                $step = (int)Configuration::get($name_module.'rpage_shoppers');

                $start = (int) Tools::getValue('page');
                if($start<0)
                    $start = 0;


                $search = Tools::getValue("search");
                $is_search = 0;

                ### search ###
                if(Tools::strlen($search)>0){
                    $is_search = 1;

                }



                $info_customers = $obj->getShoppersList(array('start' => $start,'step'=>$step,'is_search'=>$is_search,'search'=>$search));


                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'];

                $paging = $obj->paging17(array('start'=>$start,
                        'step'=> $step,
                        'count' => $info_customers['data_count_customers'],
                        'is_search'=>$is_search,
                        'search'=>$search,
                        'action'=>'allusers',
                    )
                );


                $this->context->smarty->assign($name_module.'d_eff_shopu', Configuration::get($name_module.'d_eff_shopu'));

                $this->context->smarty->assign(array(
                    $name_module.'customers' => $info_customers['customers'],
                    $name_module.'data_count_customers' => $info_customers['data_count_customers'],
                    $name_module.'paging' => $paging,
                    $name_module.'users_url'=>$users_url,

                ));


                ob_start();

                echo $obj_spmreviewsadv->renderListUsers();

                $html = ob_get_clean();


            break;
            case 'avatar_init':

                if(Configuration::get($name_module.'is_uprof')) {
                    include_once(_PS_MODULE_DIR_.$name_module . "/classes/userprofileg.class.php");
                    $obj_userprofileg = new userprofileg();

                    $info_customer = $obj_userprofileg->getCustomerInfo();

                    $avatar = $info_customer['avatar_thumb'];



                } else {
                    $status = 'error';
                    $message = 'User profile functional is disabled!';
                }


            break;
            default:
                $status = 'error';
                $message = 'Unknown parameters!';
            break;
        }


        $response = new stdClass();
        $content = ob_get_clean();
        $response->status = $status;
        $response->message = $message;
        if($action == "allusers") {
            $response->params = array('content' => $html, 'page_nav' => $paging);
        }elseif($action == "avatar_init"){
            $response->params = array('content' => $content, 'avatar' => $avatar);
        } else {
            $response->params = array('content' => $content);
        }


        echo json_encode($response);
        exit;
    }
}