<?php
/**
* 2007-2017 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* 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.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
*  @author    PrestaShop SA <contact@prestashop.com>
*  @copyright 2007-2017 PrestaShop SA
*  @license   http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
*  International Registered Trademark & Property of PrestaShop SA
*/

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

class Migrationproserver extends Module
{
    protected $config_form = false;

    public function __construct()
    {
        $this->name = 'migrationproserver';
        $this->tab = 'administration';
        $this->version = '1.0.0';
        $this->author = 'MigrationPro';
        $this->need_instance = 1;

        /**
         * Set $this->bootstrap to true if your module is compliant with bootstrap (PrestaShop 1.6)
         */
        $this->bootstrap = true;

        parent::__construct();

        $this->displayName = $this->l('MigrationProServer');
        $this->description = $this->l('Server module for Client MigrationPro module');
    }

    /**
     * Don't forget to create update methods if needed:
     * http://doc.prestashop.com/display/PS16/Enabling+the+Auto-Update
     */
    public function install()
    {
        return parent::install() &&
            $this->registerHook('header') &&
            $this->registerHook('backOfficeHeader');
    }

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

    /**
     * Load the configuration form
     */
    public function getContent()
    {


        if (version_compare(_PS_VERSION_, '1.5', '<')) {

            $output = '<h2>'.$this->displayName.'</h2>';

            if (Tools::isSubmit('submitMigrationToken'))
            {
                $migration_token = Tools::getValue('migration_token');
                Configuration::updateValue('migration_token', $migration_token);
                Configuration::updateValue('migration_token', $migration_token);
            }

            return $output . $this->displayForm();

        } else {
            if (((bool)Tools::isSubmit('submitMigrationproserverModule')) == true) {
                $this->postProcess();
            }

            $this->context->smarty->assign('module_dir', $this->_path);

            $output = $this->context->smarty->fetch($this->local_path . 'views/templates/admin/configure.tpl');

            return $this->renderForm();
        }
    }

    /**
     * Create the form that will be displayed in the configuration of your module.
     */
    protected function renderForm()
    {
        $helper = new HelperForm();

        $helper->show_toolbar = false;
        $helper->table = $this->table;
        $helper->module = $this;
        $helper->default_form_language = $this->context->language->id;
        $helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG', 0);

        $helper->identifier = $this->identifier;
        $helper->submit_action = 'submitMigrationproserverModule';
        $helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false)
            .'&configure='.$this->name.'&tab_module='.$this->tab.'&module_name='.$this->name;
        $helper->token = Tools::getAdminTokenLite('AdminModules');
        $helper->field_values  = $this->getConfigFormValues();

        $helper->tpl_vars = array(
            'fields_value' => $this->getConfigFormValues(), /* Add values for your inputs */
            'languages' => $this->context->controller->getLanguages(),
            'id_language' => $this->context->language->id,
        );

        return $helper->generateForm(array($this->getConfigForm()));
    }

    /**
     * Create the structure of your form.
     */
    protected function getConfigForm()
    {
            return array(
                'form' => array(
                    'id_form' => 'step_migrationpro_connection',
                    'legend'  => array(
                        'title' => $this->l('Create Token'),
                        'icon'  => 'icon-AdminTools'
                    ),
                    'input' => array(
                        array(
                            'type'     => 'text',
                            'label'    => $this->l('Token'),
                            'name'     => 'migration_token',
                            'required' => true,
                            'hint'     => $this->l('The access Token for the Connection with Client Module'),
                            'desc'     => $this->l('Please use only numbers and letters (0-9 , a-z, A-Z)')
                        )
                    ),
                    'submit' => array(
                        'title' => $this->l('Save'),
                    ),
                ),
            );

    }

    public function displayForm(){
        return '
                <form action="'.$_SERVER['REQUEST_URI'].'" method="post">
                    <fieldset>
                        <legend>'.$this->l('Settings').'</legend>
                        <label>'.$this->l('Token').'</label>
                        <div class="margin-form">
                            <input type="text" name="migration_token" value="'. Configuration::get('migration_token').'" />
                            <p class="clear">'.$this->l('The access Token for the Connection with Client Module, Please use only numbers and letters (0-9 , a-z, A-Z)').'</p>
                        </div>
                    </fieldset>
                     <center><input type="submit" name="submitMigrationToken" value="'.$this->l('Save').'" class="button" /></center>
                </form>';
    }

    /**
     * Set values for the inputs.
     */
    protected function getConfigFormValues()
    {
        return array(
            'migration_token' => Configuration::get('migration_token', null),
        );
    }

    /**
     * Save form data.
     */
    protected function postProcess()
    {
        $form_values = $this->getConfigFormValues();

        foreach (array_keys($form_values) as $key) {
            Configuration::updateValue($key, Tools::getValue($key));
        }
    }

}
