<?php

class ControllerModuleNotifyWhenArrives extends Controller {

    private $error = array();

    public function index() {
        $this->load->language('module/notify_when_arrives');

        $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('notify_when_arrives', $this->request->post);

            $this->session->data['success'] = $this->language->get('text_success');

            $this->redirect($this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'));
        }

        $this->data['token'] = $this->session->data['token'];

        $this->data['heading_title'] = $this->language->get('heading_title');

        $this->data['text_enabled'] = $this->language->get('text_enabled');
        $this->data['text_disabled'] = $this->language->get('text_disabled');

        $this->data['text_module'] = $this->language->get('text_module');

        $this->data['text_show_module'] = $this->language->get('text_show_module');
        $this->data['text_show_product'] = $this->language->get('text_show_product');

        $this->data['text_general'] = $this->language->get('text_general');
        $this->data['text_statistics'] = $this->language->get('text_statistics');

        $this->data['text_product_name'] = $this->language->get('text_product_name');
        $this->data['text_product_requested'] = $this->language->get('text_product_requested');
        $this->data['text_product_notified'] = $this->language->get('text_product_notified');
        $this->data['text_product_emails'] = $this->language->get('text_product_emails');

        $this->data['text_none'] = $this->language->get('text_none');

        $this->data['entry_status'] = $this->language->get('entry_status');
        $this->data['entry_sort_order'] = $this->language->get('entry_sort_order');
        $this->data['entry_layout'] = $this->language->get('entry_layout');
        $this->data['entry_position'] = $this->language->get('entry_position');
        $this->data['entry_show_mode'] = $this->language->get('entry_show_mode');

        $this->data['button_save'] = $this->language->get('button_save');
        $this->data['button_cancel'] = $this->language->get('button_cancel');
        $this->data['button_add_module'] = $this->language->get('button_add_module');
        $this->data['button_add_account'] = $this->language->get('button_add_account');
        $this->data['button_remove'] = $this->language->get('button_remove');
        $this->data['button_notify'] = $this->language->get('button_notify');
        
        $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_view_mails'] = $this->language->get('text_view_mails');
        $this->data['text_hide_mails'] = $this->language->get('text_hide_mails');
        $this->data['text_confirm_delete'] = $this->language->get('text_confirm_delete');

        $this->data['text_delete_ok'] = $this->language->get('text_delete_ok');
        $this->data['text_delete_error'] = $this->language->get('text_delete_error');
        
       $this->data['text_updated'] = $this->language->get('text_updated'); 
       
        $this->data['text_delete_statistic'] = $this->language->get('text_delete_statistic');
        
        $this->data['tab_general'] = $this->language->get('tab_general');
        $this->data['tab_statistics'] = $this->language->get('tab_statistics');

        $this->data['text_no_data'] = $this->language->get('text_no_data');

        $this->data['text_tip_layout'] = $this->language->get('text_tip_layout');

        if (isset($this->error['warning'])) {
            $this->data['error_warning'] = $this->error['warning'];
        } else {
            $this->data['error_warning'] = '';
        }

        if (isset($this->request->post['notify_when_arrives'])) {
            $this->data['modules'] = $this->request->post['notify_when_arrives'];
        } elseif ($this->config->get('notify_when_arrives')) {
            $this->data['modules'] = $this->config->get('notify_when_arrives');
        }

        $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/notify_when_arrives', 'token=' . $this->session->data['token'], 'SSL'),
            'separator' => ' :: '
        );

        if (extension_loaded('curl')) {

            $this->data['error_extension_curl'] =  '';
            
        }else{
            
            $this->data['error_extension_curl'] =  $this->language->get('error_extension_curl');

        }
        
        $this->data['action'] = $this->url->link('module/notify_when_arrives', 'token=' . $this->session->data['token'], 'SSL');

        $this->data['cancel'] = $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL');


        if (isset($this->request->post['notify_when_arrives_status'])) {
            $this->data['notify_when_arrives_status'] = $this->request->post['notify_when_arrives_status'];
        } else {
            $this->data['notify_when_arrives_status'] = $this->config->get('notify_when_arrives_status');
        }

        if (isset($this->request->post['notify_when_arrives_show_mode'])) {
            $this->data['notify_when_arrives_show_mode'] = $this->request->post['notify_when_arrives_show_mode'];
        } else {
            $this->data['notify_when_arrives_show_mode'] = $this->config->get('notify_when_arrives_show_mode');
        }


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

        if (isset($this->request->post['notify_when_arrives_module'])) {

            $this->data['modules'] = $this->request->post['notify_when_arrives_module'];
        } elseif ($this->config->get('notify_when_arrives_module')) {
            $this->data['modules'] = $this->config->get('notify_when_arrives_module');
        }

        $this->data['product_statistics'] = $this->getStatistics();

        $this->load->model('design/layout');

        $this->data['layouts'] = $this->model_design_layout->getLayouts();

        $this->template = 'module/notify_when_arrives.tpl';
        $this->children = array(
            'common/header',
            'common/footer',
        );

        $this->response->setOutput($this->render());
    }
    private function getStatistics() {
        
        
        $this->load->model('catalog/product');

        $product_statistics = $this->loadData();

        $products = $this->model_catalog_product->getProducts();

        foreach ($products as $product) {

            if (isset($product_statistics[$product['product_id']])) {

                $product_statistics[$product['product_id']]['name'] = $product['name'];
            }
        }

        foreach ($product_statistics as $product_id => &$product_statistic) {

            if (!isset($product_statistic['name'])) {

                $product_statistic['name'] = sprintf($this->language->get('error_product_name'), $product_id);
            }

            $product_statistic['mails'] = implode(',<br/>', $product_statistic['mails']);
        }
        
        return $product_statistics;
        
    }


    public function notify() {
        
        $json = array();
        
        $this->load->language('module/notify_when_arrives');
        
        if (extension_loaded('curl')) {

            $curl = curl_init();

            curl_setopt($curl, CURLOPT_URL, HTTP_CATALOG.'index.php?route=product/product');
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

            $content = curl_exec($curl);

            curl_close($curl);

        }
        
       $json['statistics'] = $this->getStatistics();        
   

       $this->response->setOutput(json_encode($json));
        
   }


    public function delete() {

        $json = array();
        
        $data = $this->loadData();
        
        if (isset($this->request->post['product_id'])){
            
            $productId = $this->request->post['product_id'];

            if (isset($data[$productId])) {

                unset($data[$productId]);

                $this->saveData($data);

                 $json =  array('success' => 1,'error'=>0) ;

            }else{

                 $json =  array('success' => 0,'error'=>1) ;
            } 

        }
        
       $this->response->setOutput(json_encode($json));

    }

    private function saveData($data) {

        $group = 'nwa_cfg';

        $data = array($group => json_encode($data));

        $this->db->query("DELETE FROM " . DB_PREFIX . "setting WHERE `group` = '" . $this->db->escape($group) . "'");

        foreach ($data as $key => $value) {
            $this->db->query("INSERT INTO " . DB_PREFIX . "setting SET `group` = '" . $this->db->escape($group) . "', `key` = '" . $this->db->escape($key) . "', `value` = '" . $this->db->escape($value) . "'");
        }
    }

    private function loadData() {

        $group = 'nwa_cfg';

        $data = array();

        $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "setting WHERE `group` = '" . $this->db->escape($group) . "'");

        foreach ($query->rows as $result) {
            $data[$result['key']] = $result['value'];
        }

        if (isset($data[$group])) {
            return json_decode($data[$group], 1);
        } else {

            return array();
        }
    }

    private function validate() {
        if (!$this->user->hasPermission('modify', 'module/notify_when_arrives')) {
            $this->error['warning'] = $this->language->get('error_permission');
        }

        if (!$this->error) {
            return true;
        } else {
            return false;
        }
    }

}

?>