<?php
define('_PS_ADMIN_DIR_', getcwd());
include_once(_PS_ADMIN_DIR_.'/../classes/AdminTab.php');
class AdminWebRotate360 extends AdminTab
{
    public function __construct()
    {
        $this->table = 'webrotate360';
        $this->className = 'WebRotate360';
        $this->lang = false;
        $this->edit = true;
        $this->delete = true;

        $this->fieldsDisplay = array(
            'id_product' => array(
            'title' => $this->l('Product ID'),
            'align' => 'center',
            'width' => 25),
            'view_name' => array(
            'title' => $this->l('View Name'),
            'align' => 'center',
            'width' => 200),
            'config_file_url' => array(
            'title' => $this->l('Config File URL'),
            'width' => 200),
            'root_path' => array(
            'title' => $this->l('Root Path'),
            'width' => 200)
        );

        $this->identifier = 'id_view';
        parent::__construct();
    }

    public function displayForm()
    {
        global $currentIndex;
        $defaultLanguageId = (int)(Configuration::get('PS_LANG_DEFAULT'));
        $products = ProductCore::getProducts($defaultLanguageId, 0, 0, "name", "ASC");

        $obj = $this->loadObject(true);
        $idProduct = $this->getFieldValue($obj, "id_product");

        echo '
        <form action="' . $currentIndex . '&submitAdd' .  $this->table . '=1&token=' . $this->token . '" method="post" class="width3">'.($obj->id ? '<input type="hidden" name="id_view" value="' . $obj->id . '" />' : '');
            echo '
            <fieldset><legend><img src="../img/admin/profiles.png" />'.$this->l('Product View Setup').'</legend>
            <ul style="margin: 0 0 30px 0; font-size:11px;">
                <li>Select your product from the Product list.</li>
                <li>Give your view a unique name (optional).</li>
                <li>Enter URL to WebRotate 360 configuration XML file on your server for this product. Not required if using Root Path and Master Config XML in the module settings.</li>
                <li>Root Path is optional. If set, it will be used as a base URL for loading image assets specified in the configuration XML file. It can also point to an external CDN server. Use it in conjunction with Master Config in the plugin settings to have a single configuration xml for all your product views.</li>
            </ul>
                <label>'.$this->l('Product:').'</label>
                <div class="margin-form">
                    <div id="id_product" style="display: block; float: left;">
                        <select name="id_product"><option value="-1"> --- Select Product --- </option>';
                            foreach ($products as $product)
                            {
                                if ($idProduct == $product["id_product"])
                                {
                                    echo "<option selected='selected' value=".$product["id_product"].">".$product["name"]."</option>";
                                }
                                else
                                {
                                    echo "<option value=".$product["id_product"].">".$product["name"]."</option>";
                                }
                            }
                        echo '
                        </select>
                        <sup> *</sup>
                    </div>
                    <div class="clear"></div>
                </div>

                <label>'.$this->l('View Name:').'</label>
                <div class="margin-form">
                    <div id="view_name" style="display: block; float: left;">
                        <input size="33" type="text" name="view_name" value="'.htmlentities($this->getFieldValue($obj, 'view_name'), ENT_COMPAT, 'UTF-8').'" />
                    </div>
                    <div class="clear"></div>
                </div>

                <label>'.$this->l('Config URL:').'</label>
                <div class="margin-form">
                    <div id="config_file_url" style="display: block; float: left;">
                        <input size="120" type="text" name="config_file_url" value="'.htmlentities($this->getFieldValue($obj, 'config_file_url'), ENT_COMPAT, 'UTF-8').'" />
                    </div>
                    <div class="clear"></div>
                </div>

                <label>'.$this->l('Root Path (PRO only):').'</label>
                <div class="margin-form">
                    <div id="root_path" style="display: block; float: left;">
                        <input size="120" type="text" name="root_path" value="'.htmlentities($this->getFieldValue($obj, 'root_path'), ENT_COMPAT, 'UTF-8').'" />
                    </div>
                    <div class="clear"></div>
                </div>

                <div class="margin-form">
                   <input type="submit" value="'.$this->l('Save').'" name="submitAdd'.$this->table.'" class="button" />
                </div>
                <div class="small"><sup>*</sup> '.$this->l('Required field').'</div>
            </fieldset>
        </form> ';
    }
}