<?xml version="1.0" encoding="utf-8"?>
<modification>
    <name>DearSoft Group</name>
	<code>DearSoft_attribute_group</code>
	<version>3.0</version>
	<author>DearSoft</author>
	<link>https://dearsoft.bd</link>

 	<file path="admin/controller/catalog/product.php">
		<operation>
		<search index="0"><![CDATA[$this->load->model('catalog/attribute');]]></search>
			<add position="after"><![CDATA[
$this->load->model('catalog/attribute_group');
$data['attribute_groups'] = $this->model_catalog_attribute_group->getAttributeGroups();
  			]]></add>
		</operation>
	  	<operation>
		<search index="0"><![CDATA[public function index() {]]></search>
			<add position="before"><![CDATA[
public function getattributesbygroup() {
	$json = array();
	
	if (isset($this->request->get['attribute_group_id'])) {
		$this->load->model('catalog/attribute');
		$json = $this->model_catalog_attribute->getAttributes(array('filter_attribute_group_id' => $this->request->get['attribute_group_id']));
	}
	$this->response->addHeader('Content-Type: application/json');
	$this->response->setOutput(json_encode($json));
}
			]]></add>
		</operation>
	   
       <operation>
        <search index="0"><![CDATA[if (isset($this->request->post['product_attribute'])) {]]></search>
                <add position="before"><![CDATA[
                    $this->load->model('extension/module/attribute_group_category');
                    $data['attribute_group_categories'] = $this->model_extension_module_attribute_group_category->getAttributeGroupCategories(array('sort'=>'sort_order'));
                    $data['attribute_group_category_status'] = $this->config->get('module_attribute_group_category_status'); 
                    
                ]]></add>
        </operation>

	</file>
  	<file path="admin/view/template/catalog/product_form.twig">
		<operation>
		<search index="0"><![CDATA[<div class="tab-pane" id="tab-attribute">]]></search>
			<add position="after"><![CDATA[
<div class="form-group">
  <label class="col-sm-2 control-label" for="input-attribute_group_id">{{ entry_attribute_group }}</label>
  <div class="col-sm-10">
	<select name="attribute_group_id" id="input-attribute_group_id" class="form-control" onchange="addAttributeByGroupID($(this).val());">
	<option value="">{{ text_select }}</option>
	{% for attribute_group in attribute_groups %}
 <option value="{{ attribute_group.attribute_group_id }}">{{ attribute_group.name }}</option>
	{% endfor %}
	</select>
  </div>
</div>
			]]></add>
		</operation>
        <operation>
        <search><![CDATA[<div class="tab-pane" id="tab-attribute">]]></search>
                <add position="after"><![CDATA[
                    {% if attribute_group_category_status %}
                    <div class="form-group">
                      <label class="col-sm-2 control-label" for="input-attribute-group-category">{{ entry_attribute_group_category }}</label>
                      <div class="col-sm-10">
                            <select name="attribute_group_category_id" id="input-attribute-group-category" class="form-control">
                            <option value="">{{ text_select }}</option>
                            {% for attribute_group_category in attribute_group_categories %}
                                <option value="{{ attribute_group_category.attribute_group_category_id }}">{{ attribute_group_category.name }}</option>
                            {% endfor %}
                            </select>
                      </div>
                    </div>
                    {% endif %}
                ]]></add>
        </operation>
	  	<operation>
		<search index="0"><![CDATA[function addAttribute() {]]></search>
			<add position="before"><![CDATA[
function addAttributeByGroupID(attribute_group_id) {
$.ajax({
url: 'index.php?route=catalog/product/getattributesbygroup&user_token={{ user_token }}&attribute_group_id=' +  encodeURIComponent(attribute_group_id),
dataType: 'json',
beforeSend: function(){
	$('#input-attribute_group_id').after('<i class="fa fa-spinner fa-spin" id="attribute-group-loading"></i>');
},
success: function(json) {
	if(json.length > 0){	
		for(var i=0;i<json.length;i++){
		html  = '<tr id="attribute-row' + attribute_row + '">';
	  	html += '<td class="text-left" style="width: 20%;"><input type="text" name="product_attribute[' + attribute_row + '][name]" value="'+json[i].name+'" placeholder="{{ entry_attribute }}" class="form-control" /><input type="hidden" name="product_attribute[' + attribute_row + '][attribute_id]" value="'+json[i].attribute_id+'" /></td>';
	 	html += '  <td class="text-left">';
	   {% for language in languages %}
	  html += '<div class="input-group"><span class="input-group-addon"><img src="language/{{ language.code }}/{{ language.code }}.png" title="{{ language.name }}" /></span><textarea name="product_attribute[' + attribute_row + '][product_attribute_description][{{ language.language_id }}][text]" rows="5" placeholder="{{ entry_text }}" class="form-control"></textarea></div>';
	   {% endfor %}
	  	html += '  </td>';
	  	html += '  <td class="text-right"><button type="button" onclick="$(\'#attribute-row' + attribute_row + '\').remove();" data-toggle="tooltip" title="{{ button_remove }}" class="btn btn-danger"><i class="fa fa-minus-circle"></i></button></td>';
	  	html += '</tr>';
	  	$('#attribute tbody').append(html);
	  	attributeautocomplete(attribute_row);
	  	attribute_row++;
		}
	}
	$('#attribute-group-loading').remove();
}
});
}
			]]></add>
		</operation>
        <operation>
        <search><![CDATA[{{ footer }}]]></search>
                <add position="before"><![CDATA[
                    <script type="text/javascript">
                        $('#input-attribute-group-category').on('change', function(){
                            let attribute_group_category_id = $(this).val();
                            let html;                    
                            if (attribute_group_category_id) {
                                $.ajax({
                                    url: 'index.php?route=extension/module/attribute_group_category/attribute&user_token={{ user_token }}&attribute_group_category_id=' +  encodeURIComponent(attribute_group_category_id),
                                    dataType: 'json',
                                    beforeSend: function(){
                                            $('#input-attribute-group-category').after('<i class="fa fa-spinner fa-spin" id="attribute-group-category-loading"></i>');
                                    },
                                    success: function(json) {
                                        if(json['attributes'].length > 0){  
                                            $.each(json['attributes'], function(index, item){
                                                html = '';
                                                html += '<tr id="attribute-row' + attribute_row + '">';
                                                    html += '<td class="text-left" style="width: 20%;">';
                                                        html += '<input type="text" name="product_attribute[' + attribute_row + '][name]" value="' + item.name + '" placeholder="{{ entry_attribute }}" class="form-control" />';
                                                        html += '<input type="hidden" name="product_attribute[' + attribute_row + '][attribute_id]" value="' + item.attribute_id + '" />';
                                                    html += '</td>';
                                                    html += '<td class="text-left">';
                                                        {% for language in languages %}
                                                        html += '<div class="input-group">';
                                                            html += '<span class="input-group-addon"><img src="language/{{ language.code }}/{{ language.code }}.png" title="{{ language.name }}" /></span>';
                                                            html += '<textarea name="product_attribute[' + attribute_row + '][product_attribute_description][{{ language.language_id }}][text]" rows="5" placeholder="{{ entry_text }}" class="form-control"></textarea>';
                                                        html += '</div>';
                                                        {% endfor %}                            
                                                    html += '</td>';
                                                    html += '<td class="text-right">';
                                                        html += '<button type="button" onclick="$(\'#attribute-row' + attribute_row + '\').remove();" data-toggle="tooltip" title="{{ button_remove }}" class="btn btn-danger"><i class="fa fa-minus-circle"></i></button>';
                                                    html += '</td>';
                                                html += '</tr>';
                                                $('#attribute tbody').append(html);
                                                attributeautocomplete(attribute_row);
                                                attribute_row++;
                                            });    
                                        }
                                        $('#attribute-group-category-loading').remove();
                                    }
                                });
                            }
                        });
                    </script>                            
                ]]></add>
        </operation>
	</file>

<!-- Attribute Group Category -->
<file path="admin/controller/common/column_left.php">
        <operation error="skip">
            <search><![CDATA[if ($attribute) {]]></search>
            <add position="before">
            <![CDATA[
                if ($this->user->hasPermission('access', 'extension/module/attribute_group_category') && $this->config->get('module_attribute_group_category_status')) {
                        $attribute[] = array(
                                'name'	   => 'Group Category',
                                'href'     => $this->url->link('extension/module/attribute_group_category/category', 'user_token=' . $this->session->data['user_token'], true),
                                'children' => array()
                        );
                }                                            
            ]]>
            </add>
        </operation>
    </file>
    
    <file path="admin/language/en-gb/catalog/attribute_group.php">
        <operation error="skip">
            <search><![CDATA[// Entry]]></search>
            <add position="after">
            <![CDATA[$_['entry_attribute_group_category'] = 'Attribute Group Category';
            $_['text_autocomplete'] = 'Autocomplete';
]]>
            </add>
        </operation>
    </file>
    <file path="admin/controller/catalog/attribute_group.php">
        <operation error="skip">
            <search><![CDATA[$data['sort_order'] = '';]]></search>
            <add position="after" offset="1">
            <![CDATA[
            $data['attribute_group_category_status'] = $this->config->get('module_attribute_group_category_status');  
            if ($data['attribute_group_category_status']) {
                $this->load->model('extension/module/attribute_group_category');    
                if (isset($this->request->post['attribute_group_category'])) {
                    $attribute_group_category = $this->request->post['attribute_group_category'];
                } elseif (!empty($attribute_group_info)) {
                    $attribute_group_category = $this->model_extension_module_attribute_group_category->getAttributeGroupAttributeGroupCategories($this->request->get['attribute_group_id']);
                } else {
                    $attribute_group_category = array();
                }        
                $data['attribute_group_categories'] = array();
                foreach ($attribute_group_category as $attribute_group_category_id) {
                    $attribute_group_category_info = $this->model_extension_module_attribute_group_category->getAttributeGroupCategory($attribute_group_category_id);
                    if ($attribute_group_category_info) {
                        $data['attribute_group_categories'][] = array(
                            'attribute_group_category_id' => $attribute_group_category_id,
                            'name' => $attribute_group_category_info['name'],
                        );
                    }
                }
                $data['user_token'] = $this->session->data['user_token'];
            }
            ]]>
            </add>
        </operation>
    </file>
    <file path="admin/model/catalog/attribute_group.php">
        <operation error="skip">
            <search><![CDATA[$this->db->query("INSERT INTO " . DB_PREFIX . "attribute_group_description]]></search>
            <add position="after" offset="1">
            <![CDATA[
        if ($this->config->get('module_attribute_group_category_status')) {    
            $this->db->query("DELETE FROM " . DB_PREFIX . "attribute_group_to_category WHERE attribute_group_id = '" . (int)$attribute_group_id . "'");
            if (!empty($data['attribute_group_category'])) {
                foreach ($data['attribute_group_category'] as $attribute_group_category_id) {
                    $this->db->query("INSERT INTO " . DB_PREFIX . "attribute_group_to_category SET"
                            . " attribute_group_id = '" . (int)$attribute_group_id . "',"
                            . " attribute_group_category_id = '" . (int)$attribute_group_category_id . "'"
                            . "");
                }
            }
        }
            ]]>
            </add>
        </operation>
        <operation error="skip">
            <search><![CDATA[public function deleteAttributeGroup($attribute_group_id) {]]></search>
            <add position="after">
            <![CDATA[
            if ($this->config->get('module_attribute_group_category_status')) {
                $this->db->query("DELETE FROM " . DB_PREFIX . "attribute_group_to_category WHERE attribute_group_id = '" . (int)$attribute_group_id . "'");
            }    
            ]]>
            </add>
        </operation>
    </file>
    <file path="admin/view/template/catalog/attribute_group_form.twig">
        <operation error="skip">
            <search><![CDATA[</form>]]></search>
            <add position="before">
            <![CDATA[
            {% if (attribute_group_category_status) %}
                <div class="form-group">
                  <label class="col-sm-2 control-label" for="input-attribute-group-category"><span data-toggle="tooltip" title="{{ text_autocomplete }}">{{ entry_attribute_group_category }}</span></label>
                  <div class="col-sm-10">
                    <input type="text" name="attribute_group_category_name" value="" placeholder="{{ text_autocomplete }}" id="input-attribute-group-category" class="form-control"/>
                    <div id="attribute-group-category" class="well well-sm" style="height: 150px; overflow: auto;"> {% for attribute_group_category in attribute_group_categories %}
                        <div id="attribute-group-category{{ attribute_group_category.attribute_group_category_id }}"><i class="fa fa-minus-circle"></i> {{ attribute_group_category.name }}
                          <input type="hidden" name="attribute_group_category[]" value="{{ attribute_group_category.attribute_group_category_id }}"/>
                        </div>
                      {% endfor %}</div>
                  </div>
                </div>  
            {% endif %}    
            ]]>
            </add>
        </operation>
        <operation error="skip">
            <search><![CDATA[{{ footer }}]]></search>
            <add position="before">
            <![CDATA[
{% if (attribute_group_category_status) %}            
<script type="text/javascript">
      $('#input-attribute-group-category').autocomplete({
                'source': function(request, response) {
                        $.ajax({
                                url: 'index.php?route=extension/module/attribute_group_category/autocompleteCategory&user_token={{ user_token }}&filter_name=' + encodeURIComponent(request),
                                dataType: 'json',
                                success: function(json) {
                                        response($.map(json, function(item) {
                                                return {
                                                        label: item['name'],
                                                        value: item['attribute_group_category_id']
                                                }
                                        }));
                                }
                        });
                },
                'select': function(item) {
                        $('#input-attribute-group-category').val('');
                        $('#attribute-group-category' + item['value']).remove();
                        $('#attribute-group-category').append('<div id="attribute-group-category' + item['value'] + '"><i class="fa fa-minus-circle"></i> ' + item['label'] + '<input type="hidden" name="attribute_group_category[]" value="' + item['value'] + '" /></div>');
                }
        });

        $('#attribute-group-category').delegate('.fa-minus-circle', 'click', function() {
                $(this).parent().remove();
        });
</script> 
{% endif %}  
            ]]>
            </add>
        </operation>
    </file>
    
    <!-- Attribute -->
    <file path="admin/language/en-gb/catalog/attribute.php">
        <operation error="skip">
            <search><![CDATA[// Entry]]></search>
            <add position="after">
            <![CDATA[$_['entry_attribute_group_category'] = 'Attribute Group Category';
            $_['text_autocomplete'] = 'Autocomplete';
]]>
            </add>
        </operation>
    </file>   
    <file path="admin/controller/catalog/attribute.php">
        <operation error="skip">
            <search><![CDATA[$data['sort_order'] = '';]]></search>
            <add position="after" offset="1">
            <![CDATA[
            $data['attribute_group_category_status'] = $this->config->get('module_attribute_group_category_status');  
            if ($data['attribute_group_category_status']) {
                $this->load->model('extension/module/attribute_group_category');    
                if (isset($this->request->post['attribute_group_category'])) {
                    $attribute_group_category = $this->request->post['attribute_group_category'];
                } elseif (!empty($attribute_info)) {
                    $attribute_group_category = $this->model_extension_module_attribute_group_category->getAttributeAttributeGroupCategories($this->request->get['attribute_id']);
                } else {
                    $attribute_group_category = array();
                }        
                $data['attribute_group_categories'] = array();
                foreach ($attribute_group_category as $attribute_group_category_id) {
                    $attribute_group_category_info = $this->model_extension_module_attribute_group_category->getAttributeGroupCategory($attribute_group_category_id);
                    if ($attribute_group_category_info) {
                        $data['attribute_group_categories'][] = array(
                            'attribute_group_category_id' => $attribute_group_category_id,
                            'name' => $attribute_group_category_info['name'],
                        );
                    }
                }
                $data['user_token'] = $this->session->data['user_token'];
            }
            ]]>
            </add>
        </operation>
    </file>     
    <file path="admin/model/catalog/attribute.php">
        <operation error="skip">
            <search><![CDATA[$this->db->query("INSERT INTO " . DB_PREFIX . "attribute_description]]></search>
            <add position="after" offset="1">
            <![CDATA[
        if ($this->config->get('module_attribute_group_category_status')) {    
            $this->db->query("DELETE FROM " . DB_PREFIX . "attribute_to_group_category WHERE attribute_id = '" . (int)$attribute_id . "'");
            if (!empty($data['attribute_group_category'])) {
                foreach ($data['attribute_group_category'] as $attribute_group_category_id) {
                    $this->db->query("INSERT INTO " . DB_PREFIX . "attribute_to_group_category SET"
                            . " attribute_id = '" . (int)$attribute_id . "',"
                            . " attribute_group_category_id = '" . (int)$attribute_group_category_id . "'"
                            . "");
                }
            }
        }
            ]]>
            </add>
        </operation>
        <operation error="skip">
            <search><![CDATA[public function deleteAttribute($attribute_id) {]]></search>
            <add position="after">
            <![CDATA[
            if ($this->config->get('module_attribute_group_category_status')) {
                $this->db->query("DELETE FROM " . DB_PREFIX . "attribute_to_group_category WHERE attribute_id = '" . (int)$attribute_id . "'");
            }    
            ]]>
            </add>
        </operation>
    </file>    
    <file path="admin/view/template/catalog/attribute_form.twig">
        <operation error="skip">
            <search><![CDATA[</form>]]></search>
            <add position="before">
            <![CDATA[
            {% if (attribute_group_category_status) %}
                <div class="form-group">
                  <label class="col-sm-2 control-label" for="input-attribute-group-category"><span data-toggle="tooltip" title="{{ text_autocomplete }}">{{ entry_attribute_group_category }}</span></label>
                  <div class="col-sm-10">
                    <input type="text" name="attribute_group_category_name" value="" placeholder="{{ text_autocomplete }}" id="input-attribute-group-category" class="form-control"/>
                    <div id="attribute-group-category" class="well well-sm" style="height: 150px; overflow: auto;"> {% for attribute_group_category in attribute_group_categories %}
                        <div id="attribute-group-category{{ attribute_group_category.attribute_group_category_id }}"><i class="fa fa-minus-circle"></i> {{ attribute_group_category.name }}
                          <input type="hidden" name="attribute_group_category[]" value="{{ attribute_group_category.attribute_group_category_id }}"/>
                        </div>
                      {% endfor %}</div>
                  </div>
                </div>  
            {% endif %}    
            ]]>
            </add>
        </operation>
        <operation error="skip">
            <search><![CDATA[{{ footer }}]]></search>
            <add position="before">
            <![CDATA[
{% if (attribute_group_category_status) %}            
<script type="text/javascript">
      $('#input-attribute-group-category').autocomplete({
                'source': function(request, response) {
                        $.ajax({
                                url: 'index.php?route=extension/module/attribute_group_category/autocompleteCategory&user_token={{ user_token }}&filter_name=' + encodeURIComponent(request),
                                dataType: 'json',
                                success: function(json) {
                                        response($.map(json, function(item) {
                                                return {
                                                        label: item['name'],
                                                        value: item['attribute_group_category_id']
                                                }
                                        }));
                                }
                        });
                },
                'select': function(item) {
                        $('#input-attribute-group-category').val('');
                        $('#attribute-group-category' + item['value']).remove();
                        $('#attribute-group-category').append('<div id="attribute-group-category' + item['value'] + '"><i class="fa fa-minus-circle"></i> ' + item['label'] + '<input type="hidden" name="attribute_group_category[]" value="' + item['value'] + '" /></div>');
                }
        });

        $('#attribute-group-category').delegate('.fa-minus-circle', 'click', function() {
                $(this).parent().remove();
        });
</script> 
{% endif %}  
            ]]>
            </add>
        </operation>
    </file>
    <!-- End of Attribute -->

    <file path="admin/language/en-gb/catalog/product.php">
        <operation error="skip">
            <search><![CDATA[// Entry]]></search>
            <add position="after">
            <![CDATA[$_['entry_attribute_group_category'] = 'Attribute Group Category';]]>
            </add>
        </operation>
    </file>    
        
    <file path="admin/model/catalog/product.php">
        <operation>
        <search><![CDATA[foreach ($data['product_attribute'] as $product_attribute) {]]></search>
                <add position="before"><![CDATA[
                   $data['product_attribute'] = array_filter($data['product_attribute'], function($item){ return !empty($item['product_attribute_description'][$this->config->get('config_language_id')]['text']);}); 
                ]]></add>
        </operation>
    </file>        
    

</modification>