<?xml version="1.0" encoding="utf-8"?>
<modification>
    <name>Google Merchant Feed SL</name>
    <code>gmf_sl_ill</code>
    <version>4.0</version>
    <author>illiats</author>
    <link>https://dev.automint.com.ua</link>
    <file path="admin/view/template/catalog/product_form.tpl">
        <operation error="skip">
        <search><![CDATA[<div class="tab-pane" id="tab-design">]]></search>
        <add position="before"><![CDATA[<div class="tab-pane" id="tab-merchant">
              <div class="table-responsive">

                <!-- СТАТУС CЕЛЕКТ -->
                <div class="form-group">
                  <label class="col-sm-2 control-label" for="input-merchant-status">Статус</label>
                  <div class="col-sm-10">
                    <select name="merchant[enable]" id="input-tax-class" class="form-control">
                      <option value="1"<?php if (!isset($merchant['enable']) or empty($merchant['enable']) or $merchant['enable'] == 1) { ?> selected="selected" <?php } ?>>Включено</option>
                      <option value="0"<?php if (isset($merchant['enable']) and $merchant['enable'] == '0') { ?> selected="selected" <?php } ?>>Отключено</option>
                    </select>
                  </div>
                </div>
                <br/>

                <div class="jumbotron jumbotron-fluid">
                  <div class="container">
                    <p><strong>Настройки для одежды/обуви</strong></p>

                <!-- МАТЕРИАЛ ИНПУТ -->



                <div class="form-group">
                  <label class="col-sm-2 control-label" for="input-merchant-size">Размер</label>
                  <div class="col-sm-10">
                  <input type="text" name="merchant[size_text]" value="<?php if(isset($merchant['size_text'])){ echo $merchant['size_text']; } ?>" placeholder="Размер" id="input-merchant-size_text" class="form-control" />
                  </div>
                    </div>

                <!-- ПОЛ CЕЛЕКТ -->
                <div class="form-group">
                  <label class="col-sm-2 control-label" for="input-merchant-sex">Пол</label>
                  <div class="col-sm-10">
                    <select name="merchant[sex]" id="input-tax-class" class="form-control">
                      <option value="0"<?php if (!isset($merchant['sex']) or $merchant['sex'] == '0') { ?> selected="selected" <?php } ?>><?php echo $text_none; ?></option>
                      <option value="3"<?php if (isset($merchant['sex']) && $merchant['sex'] == '3') { ?> selected="selected" <?php } ?>>male [мужской]</option>
                      <option value="1"<?php if (isset($merchant['sex']) && $merchant['sex'] == '1') { ?> selected="selected" <?php } ?>>female [женский]</option>
                      <option value="2"<?php if (isset($merchant['sex']) && $merchant['sex'] == '2') { ?> selected="selected" <?php } ?>>unisex [унисекс]</option>
                    </select>
                  </div>
                </div>

               <!-- МАТЕРИАЛ ИНПУТ -->
                <div class="form-group">
                  <label class="col-sm-2 control-label" for="input-merchant-material">Материал</label>
                  <div class="col-sm-10">
                    <input type="text" name="merchant[material]" value="<?php if(isset($merchant['material'])){ echo $merchant['material']; } ?>" placeholder="Материал" id="input-merchant-material" class="form-control" />
                  </div>
                </div>

                <!-- СОСТОЯНИЕ ИНПУТ -->
                <div class="form-group">
                  <label class="col-sm-2 control-label" for="input-merchant-material">Состояние</label>
                  <div class="col-sm-10">
                    <select name="merchant[state]" id="input-tax-class" class="form-control">
                    <option value="0"<?php if (!isset($merchant['state']) or $merchant['state'] == '0') { ?> selected="selected" <?php } ?>>new [новый]</option>
                    <option value="1"<?php if (isset($merchant['state']) && $merchant['state'] == '1') { ?> selected="selected" <?php } ?>>refurbished [восстановленный]</option>
                    <option value="2"<?php if (isset($merchant['state']) && $merchant['state'] == '2') { ?> selected="selected" <?php } ?>>used [б/у]</option>
                    </select>
                  </div>
                </div>
                  </div>
                </div>
                <div class="jumbotron jumbotron-fluid">
                  <div class="container">
                    <p><strong>Произвольные теги</strong></p>
                    <div class="table-responsive">
                      <table id="merchant-tags" class="table table-striped table-bordered table-hover">
                        <thead>
                        <tr>
                          <td class="text-left" style="width: 40%;">Название</td>
                          <td class="text-left" style="width: 50%;">Значение</td>
                          <td></td>
                        </tr>
                        </thead>
                        <tbody>

                        <?php $merchant_row = 0; ?>
                        <?php foreach ($merchant_tags as $tag) { ?>
                        <tr id="merchant-row<?php echo $merchant_row; ?>">
                          <td class="text-left" style="width: 40%;">
                            <input type="text" name="merchant_tags[<?php echo $merchant_row; ?>][name]" value="<?php echo $tag['name']; ?>" placeholder="<?php echo $entry_attribute; ?>" class="form-control" />
                          </td>
                          <td class="text-left" style="width: 50%;">
                            <textarea name="merchant_tags[<?php echo $merchant_row; ?>][value]" rows="3" placeholder="<?php echo $entry_text; ?>" class="form-control"><?php echo $tag['value']; ?></textarea>
                          </td>
                          <td class="text-left"><button type="button" onclick="$('#merchant-row<?php echo $merchant_row; ?>').remove();" data-toggle="tooltip" title="<?php echo $button_remove; ?>" class="btn btn-danger"><i class="fa fa-minus-circle"></i></button></td>
                        </tr>
                        <?php $merchant_row++; ?>
                        <?php } ?>
                        </tbody>
                        <tfoot>
                        <tr>
                          <td colspan="2"></td>
                          <td class="text-left"><button type="button" onclick="addMerchantTag();" data-toggle="tooltip" title="<?php echo $button_attribute_add; ?>" class="btn btn-primary"><i class="fa fa-plus-circle"></i></button></td>
                        </tr>
                        </tfoot>
                      </table>
                    </div>
                  </div>
                </div>
              </div>
            </div>]]></add>
          </operation>
        <operation error="skip">
        <search><![CDATA[<li><a href="#tab-design" data-toggle="tab"><?php echo $tab_design; ?></a></li>]]></search>
        <add position="after"><![CDATA[<li><a href="#tab-merchant" data-toggle="tab">Google Merchant</a></li>]]></add>
      </operation>
      <operation error="skip">
        <search><![CDATA[var attribute_row = <?php echo $attribute_row; ?>;]]></search>
        <add position="before"><![CDATA[    var merchant_row = <?php echo $merchant_row; ?>;
    function addMerchantTag() {
    html  = '<tr id="merchant-row' + merchant_row + '">';
    html += '<td class="text-left" style="width: 40%;"><input type="text" name="merchant_tags[' + merchant_row + '][name]" value="" placeholder="Название тега" class="form-control"/></td>';
    html += '<td class="text-left" style="width: 50%;">';
    html += '<textarea name="merchant_tags[' + merchant_row + '][value]" rows="3" placeholder="Значение" class="form-control"></textarea>';
    html += '  </td>';
    html += '  <td class="text-left"><button type="button" onclick="$(\'#merchant-row' + merchant_row + '\').remove();" data-toggle="tooltip" title="<?php echo $button_remove; ?>" class="btn btn-danger"><i class="fa fa-minus-circle"></i></button></td>';
    html += '</tr>';

    $('#merchant-tags tbody').append(html);

    merchant_row++;
    }]]></add>
      </operation>
  </file>
    <file path="admin/model/catalog/product.php">
      <operation error="skip">
      <search><![CDATA[if (isset($data['product_attribute'])) {]]></search>
      <add position="before"><![CDATA[// Merchant insert
        $merch_count = 0;
        foreach ($data['merchant'] as $item){
            if(!empty($item)){
                $merch_count++;
            }
        }
        if($data['merchant']['enable'] == 0 or $merch_count >= 1){
            $sql =  "INSERT INTO " . DB_PREFIX . "product_merchant SET product_id = '" . (int)$product_id . "', enable = '" . (int)$data['merchant']['enable'] . "', sex = '" . (int)$data['merchant']['sex'] . "', material = '" . $data['merchant']['material'] . "', state = '" . $data['merchant']['state'] . "'";
            if(isset($data['merchant']['size_type'])){
                $sql .= ", size_type = '" . $data['merchant']['size_type'] . "'";
            }
            if(isset($data['merchant']['size_text'])){
                $sql .= ", size_text = '" . $data['merchant']['size_text'] . "'";
            }
            if(isset($data['merchant']['size_option_id'])){
                $sql .= ", size_option_id = '" . $data['merchant']['option_id'] . "'";
            }
            $this->db->query($sql);
        }]]></add> 
      </operation>     
      <operation error="skip">
      <search><![CDATA[$this->cache->delete('product');]]></search>
      <add position="before"><![CDATA[$this->db->query("DELETE FROM " . DB_PREFIX . "merchant_tags WHERE product_id = '" . (int)$product_id . "'");]]></add>
    </operation>

    <operation error="skip">
      <search><![CDATA[if (!empty($data['product_attribute'])) {]]></search>
      <add position="before"><![CDATA[
                // Merchant update
        $merch_count = 0;
        foreach ($data['merchant'] as $item){
            if(!empty($item)){
                $merch_count++;
            }
        }
         if($data['merchant']['enable'] == 0 or $merch_count >= 1){
            $this->db->query("DELETE FROM " . DB_PREFIX . "product_merchant WHERE product_id = '" . (int)$product_id . "'");
            $sql =  "INSERT INTO " . DB_PREFIX . "product_merchant SET product_id = '" . (int)$product_id . "', enable = '" . (int)$data['merchant']['enable'] . "', sex = '" . (int)$data['merchant']['sex'] . "', material = '" . $data['merchant']['material'] . "', state = '" . $data['merchant']['state'] . "'";
            if(isset($data['merchant']['size_type'])){
                $sql .= ", size_type = '" . $data['merchant']['size_type'] . "'";
            }
            if(isset($data['merchant']['size_text'])){
                $sql .= ", size_text = '" . $data['merchant']['size_text'] . "'";
            }
            if(isset($data['merchant']['size_option_id'])){
                $sql .= ", size_option_id = '" . $data['merchant']['option_id'] . "'";
            }
            $this->db->query($sql);
        }]]></add>
      </operation>
      <operation error="skip">
          <search><![CDATA[   $query = $this->db->query("SELECT DISTINCT *, (SELECT keyword FROM " . DB_PREFIX . "url_alias WHERE query = 'product_id=" . (int)$product_id . "' LIMIT 1) AS keyword FROM " . DB_PREFIX . "product p LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) WHERE p.product_id = '" . (int)$product_id . "' AND pd.language_id = '" . (int)$this->config->get('config_language_id') . "'");
]]></search>
          <add position="replace"><![CDATA[    $query = $this->db->query("SELECT DISTINCT *, (SELECT keyword FROM " . DB_PREFIX . "url_alias WHERE query = 'product_id=" . (int)$product_id . "' LIMIT 1) AS keyword FROM " . DB_PREFIX . "product p LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) LEFT JOIN " . DB_PREFIX . "product_merchant pm ON (p.product_id = pm.product_id) WHERE p.product_id = '" . (int)$product_id . "' AND pd.language_id = '" . (int)$this->config->get('config_language_id') . "'");
]]></add>
        </operation>
  </file>


<file path="admin/controller/catalog/product.php">
    <operation error="skip">
      <search><![CDATA[$this->load->model('design/layout');]]></search>
      <add position="before"><![CDATA[
// Merchant
        if(isset($this->request->post['merchant_tags'])){
            $data['merchant_tags'] = $this->request->post['merchant_tags'];
        }elseif(isset($this->request->get['product_id'])){
            $this->load->model('extension/feed/google_base');
            $data['merchant_tags'] = $this->model_extension_feed_google_base->getMerchantTagsByProductID($this->request->get['product_id']);
        }else{
            $data['merchant_tags'] = array();
        }
        if(isset($product_info['size_type'])){
            $data['merchant']['size_type'] = $product_info['size_type'];
        }elseif(isset($this->request->post['merchant']['size_type'])){
            $data['merchant']['size_type'] = $this->request->post['merchant']['size_type'];
        }
        if(isset($product_info['size_text'])) {
            $data['merchant']['size_text'] = $product_info['size_text'];
        }elseif(isset($this->request->post['merchant']['size_text'])) {
            $data['merchant']['size_text'] = $this->request->post['merchant']['size_text'];
        }
        if(isset($product_info['size_text'])) {
            $data['merchant']['size_option_id'] = $product_info['size_option_id'];
        }elseif(isset($this->request->post['merchant']['size_option_id'])) {
            $data['merchant']['size_option_id'] = $this->request->post['merchant']['size_option_id'];
        }
        if(isset($product_info['sex'])) {
            $data['merchant']['sex'] = $product_info['sex'];
        }elseif(isset($this->request->post['merchant']['sex'])) {
            $data['merchant']['sex'] = $this->request->post['merchant']['sex'];
        }
        if(isset($product_info['material'])) {
            $data['merchant']['material'] = $product_info['material'];
        }elseif(isset($this->request->post['merchant']['sex'])) {
            $data['merchant']['material'] = $this->request->post['merchant']['material'];
        }
        if(isset($product_info['state'])) {
            $data['merchant']['state'] = $product_info['state'];
        }elseif(isset($this->request->post['merchant']['state'])) {
            $data['merchant']['state'] = $this->request->post['merchant']['state'];
        }
        if(isset($product_info['enable'])) {
            $data['merchant']['enable'] = $product_info['enable'];
        }elseif(isset($this->request->post['merchant']['enable'])) {
            $data['merchant']['enable'] = $this->request->post['merchant']['enable'];
        }
        ]]></add>
    </operation>
    <operation error="skip">
      <search><![CDATA[$this->model_catalog_product->addProduct($this->request->post);]]></search>
      <add position="replace"><![CDATA[$last_product_id = $this->model_catalog_product->addProduct($this->request->post);]]></add>
    </operation>
    <operation error="skip">
      <search><![CDATA[$this->model_catalog_product->editProduct($this->request->get['product_id'], $this->request->post);]]></search>
      <add position="after"><![CDATA[//merchant3
            $this->load->model('extension/feed/google_base');
            $this->model_extension_feed_google_base->deleteAllMerchantTags($this->request->get['product_id']);
            if(isset($this->request->post['merchant_tags'])){
                foreach ($this->request->post['merchant_tags'] as $tag){
                    $this->model_extension_feed_google_base->setMerchantTag($this->request->get['product_id'], $tag);
                }

            }]]></add>
    </operation>
  <operation error="skip">
      <search><![CDATA[$this->model_catalog_product->addProduct($this->request->post);]]></search>
      <add position="after"><![CDATA[$this->load->model('extension/feed/google_base');
                if(isset($this->request->post['merchant_tags'])){
                    foreach ($this->request->post['merchant_tags'] as $tag){
                        $this->model_extension_feed_google_base->setMerchantTag($last_product_id, $tag);
                    }
                    }]]></add>
    </operation>
</file>
</modification>