<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('memory_limit', '2048M');

include_once "../config.php";
include_once "../database1.php";
include_once "../../validation.php";
include_once "../../Helper.php";

if(!isset($_GET['start']))
{
    //die("Page Not Found");
}

//$start = $_GET['start'];

class actionCheckImages extends database{
    
    
    public function allProducts()
    {
        return $this->fetchAll('tb_product', ['cl_supplier' => 'action'], 'cl_created_at', 'DESC');
    }
    
    public function updateImageMain($image, $productID)
    {
        $this->update('tb_product', ['cl_image' => $image, 'cl_image_location' => 'Server'], ['cl_id' => $productID]);
    }
    
    public function updateImage($image, $productID)
    {
        
            
        $dataInsert['cl_image_location'] = 'Server';
        $dataInsert['cl_type'] = 'Product';
        $dataInsert['cl_product_id'] = $productID;
        $dataInsert['cl_name'] = $image;
        $dataInsert['cl_user_id'] = 0;
        $this->insert('tb_files', $dataInsert);
           
        
    }
}


$obj = new actionCheckImages();

$headers = array(
    'CustomerId: 73833',
    'UserName: efixx111',
    'ActionApiKey: yTvgO7694yHXhqJZ/TJNDUBN7DZ2ZQEGiIf79docWAk='
);

$productCount = 1;
$count = 1;
$products = $obj->allProducts();
foreach($products AS $product)
{
    if($product['cl_image'] == null && $product['cl_product_id'] != null)
    {
        
          
            
             $url        = 'https://api.action.pl/api/ade/v2/ProductPictures?ProductId=' . $product['cl_product_id'] . '&Language=English';
            $curl       = curl_init();
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
            curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
            $result     = curl_exec($curl);
            $err        = curl_error($curl);
            $errno      = intval(curl_errno($curl));
            curl_close($curl);

           
            // sprawdzamy odpowiedz
            if (!$err) {
                    $productArray = array();
                    $productCheck = [];
                    $productCheckHTML = [];

                    $result = json_decode($result);
                    
                    
                    
                    
                   
                    $imageArray = [];
                    
                    if(isset($result->data) &&isset($result->data->images))
                    {
                        foreach($result->data->images AS $each)
                        {
                            if($each->isMain == true)
                            {
                                
                                $imageArray = explode('.', $each->path);
                                

                                $extension = end($imageArray);
                                 $path = 'https://cdn.action.pl/File.aspx?CID=73833&UID=efix2881&PID=ec4549b100d92e60dd4c768ae1e61033&P=' . $each->path;
    
                                $file = file_get_contents($path);
                                
                                $imageName = 'action/' . 'posty' . $each->productId . '_' . $count . '.' . $extension;
                                file_put_contents(__DIR__ . "/../../../posty/public/uploads/products/" . $imageName, $file);
                                $imagesInsert['images'] = $imageName;
                                
                                $obj->updateImageMain($imageName, $product['cl_id']);

                                
                                $fp = fopen('action_update_images_text.txt', 'a');
                                //opens file in append mode  
                                fwrite($fp, 'Updated Main ' . $productCount . ' - (' . $product['cl_id'] . ')<br/>');  
                                fclose($fp);
                                
                                $count++;
                            }
                            else
                            {
                                $imageArray = explode('.', $each->path);
                                

                                $extension = end($imageArray);
                                 $path = 'https://cdn.action.pl/File.aspx?CID=73833&UID=efix2881&PID=ec4549b100d92e60dd4c768ae1e61033&P=' . $each->path;
    
                                $file = file_get_contents($path);
                                
                                $imageName = 'action/' . 'posty' . $each->productId . '_' . $count . '.' . $extension;
                                file_put_contents(__DIR__ . "/../../../posty/public/uploads/products/" . $imageName, $file);
                                $imagesInsert['images'] = $imageName;
                                
                                $obj->updateImage($imageName, $product['cl_id']);

                                $fp = fopen('action_update_images_text.txt', 'a');
                                //opens file in append mode  
                                fwrite($fp, 'Updated Other ' . $productCount . ' - (' . $product['cl_id'] . ')<br/>');  
                                fclose($fp);
                                
                                $count++;
                            }
                            
                            

                            $productCount++;

                        }
                    }
                    
                    
            

        }
    }
}
