<?php
class ControllerToolRedis extends Controller{

    public function index(){


        $this->load->model('catalog/product');

        $redis = new Redis();
        $redis->connect('127.0.0.1', 6379);

       // $redis->sAdd('productss', '45435435');
        $this->load->model('tool/image');

        $products = $this->db->query("SELECT product_id FROM  ps_product LIMIT 140000, 150000 ")->rows;

        foreach ($products as $product){
            //$p_info = $this->model_catalog_product->getProduct($product['product_id']);
            $p_info = $this->db->query("SELECT p.product_id, p.date_modified, p.product_showcase, p.image, pd.name FROM ps_product p 
            LEFT JOIN ps_product_description pd ON (p.product_id = pd.product_id) 
            WHERE p.product_id = '". (int)$product['product_id'] ."' AND pd.language_id = '1' AND p.status = '1' ")->row;

            if (isset($p_info['image'])) {
                $output = '<url>';
                $output .= '<loc>' . $this->url->link('product/product', 'product_id=' . $product['product_id']) . '</loc>';
                $output .= '<changefreq>weekly</changefreq>';
                $output .= '<lastmod>' . date('Y-m-d\TH:i:sP', strtotime($p_info['date_modified'])) . '</lastmod>';
                $output .= '<priority>'. ($p_info['product_showcase'] ? '0.9' : '0.7') .'</priority>';
                $output .= '<image:image>';
                $output .= '<image:loc>' . $this->model_tool_image->resize($p_info['image'], $this->config->get($this->config->get('config_theme') . '_image_popup_width'), $this->config->get($this->config->get('config_theme') . '_image_popup_height')) . '</image:loc>';
                $output .= '<image:caption><![CDATA[' . $p_info['name'] . ']]></image:caption>';
                $output .= '<image:title><![CDATA[' . $p_info['name'] . ']]></image:title>';
                $output .= '</image:image>';
                $output .= '</url>';

                $redis->set('google_sitemap_product_' . $product['product_id'], $output);
                echo 'google_sitemap_product_' . $product['product_id']. '<br>';
                //var_dump($redis->get('google_sitemap_product_' . $product['product_id'])); die();

            }


        }

       // var_dump($products); die();







        /*foreach ($products as $product){
            $redis->sAddArray('products',[[$product]]);
        }*/
        //$redis->set('products',json_encode($products));



        //$redis->set('products', json_encode($products));



        /*foreach ($products as $product){
            $redis->sAdd('products', $product);
        }*/

        //$redis->del('products');

        /*foreach ($redis->sMembers('products') as $item){
            var_dump($item); die();
        }*/

        //var_dump($redis->sMembers('products'));

    }

    public function deleteCache()
    {

        //https://www.deercase.com/index.php?route=tool/redis/deleteCache

        $redis = new Redis();
        $redis->connect('127.0.0.1', 6379);
        $get_url = $redis->get('iphone-x-violet-neon-kus-tuyu-desenli-telefon-kilifi');
        $redis->del('iphone-x-violet-neon-kus-tuyu-desenli-telefon-kilifi');

        var_dump($get_url); die();

    }

    public function ordertest()
    {

        die();
        //https://www.deercase.com/index.php?route=tool/redis/ordertest

        $redis = new Redis();
        $redis->connect('127.0.0.1', 6379);

        /*$orders = $this->db->query("SELECT * FROM ps_order LIMIT 1 ")->rows;

        foreach ($orders as $order){
            $redis->set('order_' . $order['order_id'],json_encode($order, true));

        }*/

       /* $urls = $this->db->query("SELECT * FROM ps_301redirect ")->rows;

        foreach ($urls as $url){
            $redis->set($url['url_from'],json_encode($url, true));

        }*/

        $get_url = $redis->get('apple-telefon-kiliflari');

        var_dump(json_decode($get_url, true)); die();



    }

    public function redis()
    {

        //https://www.deercase.com/index.php?route=tool/redis/redis

        echo phpinfo();

    }

}