<?php
class ControllerModuleAttributes extends Controller {
	private $error = array(); 
	 
	public function index() {
                // module name
                $this->data['module_name'] = 'attributes';
                
		$this->load->language('module/' . $this->data['module_name']);
                
		$this->document->setTitle($this->language->get('heading_title'));
		
		$this->load->model('setting/setting');
				
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
			$this->model_setting_setting->editSetting($this->data['module_name'], $this->request->post);		
			
			$this->session->data['success'] = $this->language->get('text_success');
						
			$this->redirect($this->url->link('module/' . $this->data['module_name'], 'token=' . $this->session->data['token'], 'SSL'));
		}
				
		$this->data['heading_title'] = $this->language->get('heading_title');
		
                // Description
                $this->data['description']['enabled']          = $this->language->get('description_enabled');
                $this->data['description']['layout']           = $this->language->get('description_layout');
                $this->data['description']['position']         = $this->language->get('description_position');
                $this->data['description']['color']            = $this->language->get('description_color');
                $this->data['description']['attribute_group']  = $this->language->get('description_attribute_group');
                $this->data['description']['attribute']        = $this->language->get('description_attribute');
                $this->data['description']['label']            = $this->language->get('description_label');
                $this->data['description']['description']      = $this->language->get('description_description');
                $this->data['description']['view']             = $this->language->get('description_view');
                $this->data['description']['initial_view']     = $this->language->get('description_initial_view');
                $this->data['description']['wishlist']         = $this->language->get('description_wishlist');
                
                $this->data['description']['modules_show']     = $this->language->get('description_modules_show');
                
                // Entry
                $this->data['entry_title']              = $this->language->get('entry_title');
                $this->data['entry_enabled']            = $this->language->get('entry_enabled');
		$this->data['entry_layout']             = $this->language->get('entry_layout');
		$this->data['entry_status']             = $this->language->get('entry_status');
                $this->data['entry_wishlist']           = $this->language->get('entry_wishlist');
                $this->data['entry_initial_view']       = $this->language->get('entry_initial_view');
                
                $this->data['title_group_label']         = $this->language->get('title_group_label');
                $this->data['title_label']              = $this->language->get('title_label');
                $this->data['title_value']              = $this->language->get('title_value');
		$this->data['title_list']               = $this->language->get('title_list');
		$this->data['title_grid']               = $this->language->get('title_grid');
                $this->data['title_attributes']         = $this->language->get('title_attributes');
                
                $this->data['entry_view']               = $this->language->get('entry_view');
                $this->data['entry_position']           = $this->language->get('entry_position');
                $this->data['entry_description']        = $this->language->get('entry_description');
                $this->data['entry_group_label']        = $this->language->get('entry_group_label');
                $this->data['entry_label']              = $this->language->get('entry_label');
                $this->data['entry_brand']              = $this->language->get('entry_brand');
                $this->data['entry_model']              = $this->language->get('entry_model');
                $this->data['entry_available']          = $this->language->get('entry_available');
                $this->data['entry_attributes']         = $this->language->get('entry_attributes');
                
                $this->data['entry_color']              = $this->language->get('entry_color');
                $this->data['entry_weight']             = $this->language->get('entry_weight');
                $this->data['entry_size']               = $this->language->get('entry_size');
		
                // Text
                $this->data['text_module']              = $this->language->get('text_module');
                $this->data['text_add_module']          = $this->language->get('text_add_module');
                $this->data['text_false']               = $this->language->get('text_false');
                $this->data['text_true']                = $this->language->get('text_true');
                $this->data['text_top']                 = $this->language->get('text_top');
                $this->data['text_content_top']         = $this->language->get('text_content_top');
		$this->data['text_content_bottom']      = $this->language->get('text_content_bottom');		
		$this->data['text_column_left']         = $this->language->get('text_column_left');
		$this->data['text_column_right']        = $this->language->get('text_column_right');
                $this->data['text_select_all']          = $this->language->get('text_select_all');
		$this->data['text_unselect_all']        = $this->language->get('text_unselect_all');
                $this->data['text_success']             = $this->language->get('text_success');
                $this->data['text_name']                = $this->language->get('text_name');
                $this->data['text_description']         = $this->language->get('text_description');
                $this->data['text_price']               = $this->language->get('text_price');
                $this->data['text_rating']              = $this->language->get('text_rating');
                $this->data['text_button']              = $this->language->get('text_button');
                $this->data['text_after_button']        = $this->language->get('text_after_button');
                $this->data['text_normal']              = $this->language->get('text_normal');
                $this->data['text_bold']                = $this->language->get('text_bold');
                $this->data['text_none']                = $this->language->get('text_none');
                $this->data['text_grid']                = $this->language->get('text_grid');
                $this->data['text_list']                = $this->language->get('text_list');
                
                // Clear Cache
                $this->data['clear_cache'] = $this->url->link('module/' . $this->data['module_name'] . '/clear_cache', 'token=' . $this->session->data['token'], 'SSL');
                
                // Refresh Cache
                $this->data['set_cache'] = $this->url->link('module/' . $this->data['module_name'] . '/set_cache', 'token=' . $this->session->data['token'], 'SSL');
                
		$this->data['button_back']        = $this->language->get('button_back');
		$this->data['button_save']          = $this->language->get('button_save');
                $this->data['button_add_module']    = $this->language->get('button_add_module');
		$this->data['button_remove']        = $this->language->get('button_remove');
                
                $this->data['tab_module'] = $this->language->get('tab_module');
                
		$this->data['token'] = $this->session->data['token'];
                
                // add scripts
                
                // colorpicker
                $this->document->addScript('view/javascript/color-picker/js/colorpicker.js');
                $this->document->addScript('view/javascript/color-picker/js/eye.js');
                $this->document->addScript('view/javascript/color-picker/js/layout.js');
                $this->document->addScript('view/javascript/color-picker/js/utils.js');
                $this->document->addStyle('view/javascript/color-picker/css/colorpicker.css');
                
                // cluetip
                $this->document->addScript('view/javascript/cluetip/jquery.cluetip.all.min.js');
                $this->document->addStyle('view/javascript/cluetip/jquery.cluetip.css');
                
 		// Success
		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];
			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}
                
                // Error
 		if (isset($this->session->data['error'])) {
			$this->data['error_warning'] = $this->session->data['error'];
                        unset($this->session->data['error']);
		} else {
			$this->data['error_warning'] = '';
		}

  		$this->data['breadcrumbs'] = array();

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_module'),
			'href'      => $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
		
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('module/' . $this->data['module_name'], 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
		
                $this->data['action'] = $this->url->link('module/' . $this->data['module_name'], 'token=' . $this->session->data['token'], 'SSL');
                
		$this->data['cancel'] = $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL');

		$this->data['modules'] = array();
                
                
                if (isset($this->request->post[$this->data['module_name']])) {
			$this->data['modules'] = $this->request->post[$this->data['module_name']];
		} elseif ($this->config->get($this->data['module_name'])) { 
			$this->data['modules'] = $this->config->get($this->data['module_name']);
		} else {
                    $this->data['modules'] = array();
                }
                
                // getAttributeGroup by id
                $this->load->model('catalog/attribute_group');
                $this->load->model('catalog/attribute');
                
                foreach ($this->data['modules'] as $mk => $module) {
                    $this->data['modules'][$mk]['attributes'] = array();
                    if (!isset($this->data['modules'][$mk]['attribute'])) {
                        $this->data['modules'][$mk]['attribute'] = array();
                    }
                    $this->data['modules'][$mk]['attribute_groups'] = array();
                    
                    if (isset($module['attribute_groups'])) {
                        foreach ($module['attribute_groups'] as $agk => $group) {
                            $data = array();
                            $data['filter_attribute_group_id'] = $group['attribute_group_id'];
                            $attributes = $this->model_catalog_attribute->getAttributesByAttributeGroupId($data);
                            foreach ($attributes as $attribute) {
                                $this->data['modules'][$mk]['attributes'][] = array($attribute['attribute_id'], $attribute['name']);
                            }
                                
                            $attribute_group = $this->model_catalog_attribute_group->getAttributeGroupDescriptions($group);
                            $this->data['modules'][$mk]['attribute_groups'][$agk] = array(
                                'label' => $attribute_group[$this->config->get('config_language_id')]['name'],
                                'value' => $group
                            );
                        }
                    }
                }
                
                // category layouts
                $this->load->model('catalog/attributes');
                $this->data['layouts'] = $this->model_catalog_attributes->getCategoryLayouts();
                
		$this->load->model('localisation/language');
		$this->data['languages'] = $this->model_localisation_language->getLanguages();
                
		$this->template = 'module/' . $this->data['module_name'] . '.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
				
		$this->response->setOutput($this->render());
	}
        
	private function validate() {
		if (!$this->user->hasPermission('modify', 'module/' . $this->data['module_name'])) {
			$this->error['warning'] = $this->language->get('error_permission');
		}
		
		if (!$this->error) {
			return true;
		} else {
			return false;
		}	
	}
        
        public function autocompleteAttr() {
		$json = array();
		
		if (isset($this->request->get['filter'])) {
			// get attributes
                        $this->load->model('catalog/attributes');
			
			if (isset($this->request->get['filter'])) {
				$filter = $this->request->get['filter'];
			} else {
				$filter = '';
			}			
			
                        $data = array(
                            'filter'  => $filter
                        );
                        $attribute_groups = $this->model_catalog_attributes->getAttributeGroups($data);
                        
                        $this->load->model('catalog/attribute');
		
                        foreach ($attribute_groups as $attribute_group) {
                                $data = array();
                                $data['filter_attribute_group_id'] = $attribute_group['attribute_group_id'];
                                $attributes = $this->model_catalog_attribute->getAttributesByAttributeGroupId($data);
                                $attr = array();
                                foreach ($attributes as $attribute) {
                                    $attr[] = array($attribute['attribute_group_id'], $attribute['attribute_id'], $attribute['name']);
                                }
                                
                                $json[] = array(
					'attribute_group_id' => $attribute_group['attribute_group_id'],
                                        'name' => $attribute_group['name'],
                                        'attributes' => $attr
				);	
                        }
		}
                
		$this->response->setOutput(json_encode($json));
	}
}
?>