<?php
/**
 * NOTICE OF LICENSE.
 *
 * This source file is subject to a commercial license from Agence Malttt SAS
 * Use, copy, modification or distribution of this source file without written
 * license agreement from the Agence Malttt SAS is strictly forbidden.
 * INFORMATION SUR LA LICENCE D'UTILISATION
 * L'utilisation de ce fichier source est soumise a une licence commerciale
 * concedee par la societe Agence Malttt SAS
 * Toute utilisation, reproduction, modification ou distribution du present
 * fichier source sans contrat de licence ecrit de la part d'Agence Malttt SAS est expressement interdite.
 *
 * @author    Matthieu Deroubaix
 * @copyright Copyright (c) 2015-2016 Agence Malttt SAS - 90 Rue faubourg saint martin - 75010 Paris
 * @license   Commercial license
 * Support by mail  :  support@agence-malttt.fr
 * Phone : +33.972535133
 */

if (!defined('_PS_VERSION_')) {
    exit;
}

class AutoGooglePlace extends Module
{
    public function __construct()
    {
        $this->name = 'autogoogleplace';
        $this->tab = 'front_office_features';
        $this->bootstrap = true;
        $this->version = '1.4.2';
        $this->author = 'Agence Malttt';

        $this->lang = true;
        $this->need_instance = 0;
        $this->ps_version_compliancy['min'] = '1.5.2.0';
        $this->ps_version_compliancy['max'] = _PS_VERSION_;
        $this->module_key = 'a10a69130ef93bcf7d1be6f3e11a4a42';
        $this->author_address = '0xc1dcC59643a63D65e05F1F9d8e985dbb9CDe344f';
        $this->displayName = $this->l('Google Address Suggest');
        $this->description = $this->l('Give users an automatic suggestion and completion when they type their address !');
        parent::__construct();
    }

    public function install()
    {
        if (parent::install() && $this->registerHook('displayHeader')) {

            // Default configuration
            $default_pages = array('authentication', 'address', 'order', 'order-opc');
            $ids = array();

            if (isset($this->context->cookie->id_lang)) {
                $metas = Meta::getMetasByIdLang((int) $this->context->cookie->id_lang);

                if (!empty($metas)) {
                    foreach ($metas as $meta) {
                        if (in_array($meta['page'], $default_pages)) {
                            $ids[] = $meta['id_meta'];
                        }
                    }
                }

                if (!empty($ids)) {
                    $implode = implode(', ', $ids);
                    Configuration::updateValue('AUTOGOOGLEPLACE_ENABLED_LINKS', $implode);
                }
            }

            return true;
        } else {
            return false;
        }
    }

    public function uninstall()
    {
        return parent::uninstall();
    }

    public function getContent()
    {
        $return = "";
        $results = array();

        if (Tools::isSubmit('SubmitAutogoogleplace')) {
            $meta = '';
            if (Tools::getValue('autogoogleplace_meta')) {
                $meta .= implode(', ', Tools::getValue('autogoogleplace_meta'));
            }
            $results[] = Configuration::updateValue('AUTOGOOGLEPLACE_ENABLED_LINKS', $meta);

            if (Tools::getValue('autogoogleplace_key')) {
                $api_key = Tools::getValue('autogoogleplace_key');
                
                if (!empty($api_key)) {
                    $results[] = Configuration::updateValue('AUTOGOOGLEPLACE_KEY', trim($api_key));
                }
            }

            if (Tools::getValue('autogoogleplace_force_15')) {
                $results[] = Configuration::updateValue('AUTOGOOGLEPLACE_FORCE_15', Tools::getValue('autogoogleplace_force_15'));
            } else {
                // Store false
                $results[] = Configuration::updateValue('AUTOGOOGLEPLACE_FORCE_15', '0');
            }

            if (Tools::getValue('autogoogleplace_load_maps')) {
                $results[] = Configuration::updateValue('AUTOGOOGLEPLACE_LOAD_MAPS', Tools::getValue('autogoogleplace_load_maps'));
            } else {
                // Store false
                $results[] = Configuration::updateValue('AUTOGOOGLEPLACE_LOAD_MAPS', '0');
            }

            if (Tools::getValue('autogoogleplace_country_filter')) {
                $results[] = Configuration::updateValue('AUTOGOOGLEPLACE_COUNTRY_FILTER', Tools::getValue('autogoogleplace_country_filter'));
            } else {
                // Store false
                $results[] = Configuration::updateValue('AUTOGOOGLEPLACE_COUNTRY_FILTER', '0');
            }

            if (Tools::getValue('autogoogleplace_active_address2')) {
                $results[] = Configuration::updateValue('AUTOGOOGLEPLACE_ACTIVE_ADDRESS2', Tools::getValue('autogoogleplace_active_address2'));
            } else {
                // Store false
                $results[] = Configuration::updateValue('AUTOGOOGLEPLACE_ACTIVE_ADDRESS2', '0');
            }
        }

        $context = Context::getContext();
        if (isset($context->shop) && Validate::isLoadedObject($context->shop)) {
            $secure_shop_url = (Configuration::get('PS_SSL_ENABLED') && Configuration::get('PS_SSL_ENABLED_EVERYWHERE')) ? 'https://'.$context->shop->domain_ssl.'*' : 'http://'.$context->shop->domain.'*';
        } else {
            $secure_shop_url = 'https://www.domain.tld*';
        }

        $this->context->smarty->assign(
            array(
                'link_form' => './index.php?tab=AdminModules&configure=autogoogleplace&token='.Tools::getAdminTokenLite('AdminModules').'&tab_module='.$this->tab.'&module_name=autogoogleplace',
                'metas' => Meta::getMetasByIdLang((int) $this->context->cookie->id_lang),
                'included_metas' => explode(',', Configuration::get('AUTOGOOGLEPLACE_ENABLED_LINKS')),
                'key' => Configuration::get('AUTOGOOGLEPLACE_KEY'),
                'secure_shop_url' => $secure_shop_url,
                'results' => $results,
            )
        );

        return $this->display(__FILE__, 'views/templates/admin/configuration.tpl');
    }

    public function hookDisplayHeader($params)
    {
        $metas = Configuration::get('AUTOGOOGLEPLACE_ENABLED_LINKS');

        if (!empty($metas)) {
            $page_name = $this->getPageName();

            $req = 'SELECT page FROM `'._DB_PREFIX_.'meta` WHERE `id_meta` IN ( '.pSQL($metas).') AND page="'.pSQL($page_name).'"';
            $sql = Db::getInstance()->getRow($req);

            if (!empty($sql)) {
                $this->context->controller->addJS(_MODULE_DIR_.$this->name.'/views/js/'.$this->name.'.js');
                
                $countries = Country::getCountries($this->context->cookie->id_lang, true);

                $load_mapsapi = (bool) Configuration::get('AUTOGOOGLEPLACE_LOAD_MAPS');

                if ((((bool) Configuration::get('AUTOGOOGLEPLACE_FORCE_15') == true || !class_exists('Media')) || version_compare(_PS_VERSION_, "1.6.0.2", "<")) && !in_array(Tools::getValue('ajax'), array('true', true, 1, "1"))) {
                    $countries_iso = "";

                    if (!empty($countries)) {
                        if ((int) Configuration::get('AUTOGOOGLEPLACE_COUNTRY_FILTER') == 1) {
                            if (!empty($countries)) {
                                $count_countries = 0;
                                foreach ($countries as $country) {
                                    if ($count_countries < 4) {
                                        $countries_iso .= '"'.Tools::strtolower($country['iso_code']).'",';
                                        $count_countries++;
                                    }
                                }
                                $countries_iso = '['.ltrim(rtrim($countries_iso, ','), ',').']';
                            }
                        }
                    }
                    
                    echo "<script type='text/javascript'>var mapsapikey = '".Configuration::get('AUTOGOOGLEPLACE_KEY')."'; var autogoogleplace_active_address2 = '".(int) Configuration::get('AUTOGOOGLEPLACE_COUNTRY_FILTER')."'; var autogoogleplace_country_filter = '".(int) Configuration::get('AUTOGOOGLEPLACE_COUNTRY_FILTER')."'; var autogoogleplace_countries = ".$countries_iso.";</script>";

                    if ($load_mapsapi === true) {
                        echo '<script defer async type="text/javascript" src="//maps.googleapis.com/maps/api/js?sensor=false&amp;libraries=places&amp;key='.Configuration::get('AUTOGOOGLEPLACE_KEY').'"></script>';
                    }
                } else {
                    $countries_iso = array();

                    if ((int) Configuration::get('AUTOGOOGLEPLACE_COUNTRY_FILTER') == 1) {
                        if (!empty($countries)) {
                            foreach ($countries as $country) {
                                if (count($countries_iso) < 4) {
                                    $countries_iso[] = Tools::strtolower($country['iso_code']);
                                }
                            }
                        }
                    }

                    Media::addJsDef(
                        array(
                            'mapsapikey' => Configuration::get('AUTOGOOGLEPLACE_KEY'),
                            'autogoogleplace_country_filter' => (int)Configuration::get('AUTOGOOGLEPLACE_COUNTRY_FILTER'),
                            'autogoogleplace_countries' => $countries_iso,
                            'autogoogleplace_active_address2' => (int) Configuration::get('AUTOGOOGLEPLACE_ACTIVE_ADDRESS2')
                        )
                    );

                    if ($load_mapsapi === true) {
                        $maps_url = '//maps.googleapis.com/maps/api/js?sensor=false&amp;libraries=places&amp;key='.Configuration::get('AUTOGOOGLEPLACE_KEY');

                        if (version_compare(_PS_VERSION_, '1.7', ">=")) {
                            $this->context->controller->registerJavascript('modules-autogoogleplace', $maps_url, array('position' => 'bottom', 'priority' => 160));
                        } else {
                            $this->context->controller->addJS($maps_url);
                        }
                    }
                }
            }
        }
    }

    public function getPageName()
    {
        $context = Context::getContext();
        $smarty = $context->smarty;

        if (!empty($smarty->tpl_vars['page_name']->value)) {
            $page_name = $smarty->tpl_vars['page_name']->value;
        } elseif (!empty($this->page_name)) {
            $page_name = $this->page_name;
        } elseif (!empty($this->php_self)) {
            $page_name = $this->php_self;
        } elseif (preg_match('#^'.preg_quote($context->shop->physical_uri, '#').'modules/([a-zA-Z0-9_-]+?)/(.*)$#', $_SERVER['REQUEST_URI'], $m)) {
            $page_name = 'module-'.$m[1].'-'.str_replace(array('.php', '/'), array('', '-'), $m[2]);
        } else {
            $page_name = Dispatcher::getInstance()->getController();
            $page_name = (preg_match('/^[0-9]/', $page_name) ? 'page_'.$page_name : $page_name);
        }

        return $page_name;
    }
}
