<?php
class ControllerDevMarketplaceHb extends Controller
{
    private function check($category)
    {


        $lastItems=explode(' ',$category['name']);
        $sql="select * from oc_category";

        foreach ($lastItems as $index => $item) {

            if($index==0){
                $sql.=" where name like '%".$item."%'";

            }else {

                $sql.=" and name like '%".$item."%'";

            }

        }

        $query =  $this->db->query($sql);
        if($query->num_rows==1){
            if($query->row['hb']==0)
            {
                $this->db->query("update oc_category SET `hb`='".(int)$category['id']."' where id='".$query->row['id']."'");

            }

        }

    }




    public function match_category()
    {

       // $this->loadLibraries('hb');
        if(isset($this->request->get['page'])){
            $page = $this->request->get['page'];
        } else {
            //$this->db->query("truncate table ".DB_PREFIX."hb_categories ");
            $page = 0;
        }

        $categories= $this->hb->getCategories($page,100);






        foreach ($categories['data']['data'] as $category) {




            if($category['status']=='ACTIVE' && $category['leaf'] && $category['available'] ) {
                $category_data = array(
                    'id' => $category['categoryId'],
                    'path' => $category['paths'],
                    'name' => $category['name'],
                    'leaf' => $category['leaf'],
                    'status' => $category['status'],
                    'available' => $category['available']

                );

                $this->update_category($category_data);

               // $this->check($category_data);

            }

        }



        if($page<950){
            $page++;

            $url= HTTPS_SERVER .'index.php?route=dev/marketplace/hb/match_category&user_token='.$this->request->get['user_token'].'&page='.$page;
            echo '<meta http-equiv="refresh"
   content="0; url='.$url.'">';


        }else {

            echo 'Tüm Kategoriler indirildi';

        }





    }


    private function update_category($category)
    {


        $category_path=implode('>',$category['path']);

        $query = $this->db->query("select * from categories_hb where name like '".$category_path."'");

       if($query->num_rows){
           //udate

           $this->db->query("update categories_hb SET leaf='".$category['leaf']."', status='".$category['status']."' where category_id='".$query->row['category_id']."' ");

           echo "update".'<br>';

       }else {
           //add
           echo "add".'<br>';
           $this->db->query("insert into categories_hb SET leaf='".$category['leaf']."', status='".$category['status']."', `name`='".$this->db->escape($category_path)."', available=1" );

       }

    }


    public function category_attributes()
    {
        $marketplace_id=2;

        $this->load->model('tool/entegrasyon_api');

        if(isset($this->request->get['page'])){
            $page=$this->request->get['page'];
        }else {

            $page=0;
        }

        $token=$this->request->get['user_token'];

        $limit= 1;

        $query = $this->db->query("select category_id from categories_hb limit ".$limit*$page.",  ".$limit."  ");

        $category_id=$query->row['category_id'];

        $result =  $this->hb->category_attributes($category_id, $this->model_tool_entegrasyon_api->getEntegrasyonApiInfo('hb'), $debug = false);
        

            foreach ($result['result'] as $attribute) {

                $query  = $this->db->query("select attribute_id,market_attribute_id from ".DB_PREFIX."attribute where market_attribute_id='".$attribute['id']."' and marketplace_id='".$marketplace_id."' ");

                if(!$query->num_rows){

                    $this->db->query("INSERT INTO ".DB_PREFIX."attribute  SET market_attribute_id ='".$attribute['id']."', marketplace_id='".$marketplace_id."', attribute_name= '".$this->db->escape($attribute['name'])."', required='".$attribute['required']."', varianter='".$attribute['varianter']."'   ");

                /*   if($attribute['type']=='enum'){
                    foreach ($attribute['values'] as $attributeValue) {
                        $this->db->query("INSERT INTO ".DB_PREFIX."attribute_value SET  attribute_id='".$attribute['id']."',  `name`='".$this->db->escape($attributeValue)."' ");
                    }
                   }*/

                    $attribute_id = $attribute['id'];

                }else {

                    $attribute_id = $query->row['market_attribute_id'];
                }


                $query = $this->db->query("select * from ".DB_PREFIX."category_attribute where attribute_id='".$attribute_id."' and category_id='".$category_id."' ");

                $varianter = $attribute['varianter'];


                if (!$query->num_rows) {

                    $this->db->query("INSERT INTO " . DB_PREFIX . "category_attribute SET attribute_id='" . $attribute_id . "', category_id='" . $category_id. "', varianter='" . $varianter . "',required='" . $attribute['required'] . "' ");

                } else {

                    $this->db->query("UPDATE " . DB_PREFIX . "category_attribute SET  varianter='" . $varianter . "', required='" . $attribute['required'] . "' where attribute_id='" . $attribute_id . "' and category_id='" . $category_id . "' ");

                }


        }

         

        $page++;

        echo '<meta http-equiv="refresh" content="0; url=index.php?route=dev/marketplace/hb/category_attributes&user_token='.$token.'&page='.$page.'" />';

        }





}