<?php
final class DModelgaleriapagegpproduto extends Model
{

  public function hasGpInfo($product_key_id, $type_product){
    $sql = "select product_key_id from oc_galeriapage_infos where product_key_id = $product_key_id and tipo_produto = $type_product";
    $query = $this->db->query($sql);
    return ($query->num_rows) == true;
  }

  public function updateProductGpInfo($data, $product_id, $type_product){
    if($this->hasGpInfo($product_id, $type_product)){
      $this->db->query( "UPDATE oc_galeriapage_infos SET "
        . " gp_department_id = '"  . $this->db->escape($data['gp_department_id']) . "',"
        . " gp_category_id = '"    . $this->db->escape($data['gp_category_id']) . "',"
        . " gp_subcategory_id = '" . $this->db->escape($data['gp_subcategory_id']) . "',"
        . " gp_custom_title = '" . $this->db->escape($data['custom_title']) . "'"
        . " WHERE product_key_id = '" . (int)$product_id . "' and tipo_produto = " . $type_product);
    }else{
      $sql = "insert into oc_galeriapage_infos set "
        . " gp_department_id = '"  . $this->db->escape($data['gp_department_id']) . "',"
        . " gp_category_id = '"    . $this->db->escape($data['gp_category_id']) . "',"
        . " gp_subcategory_id = '" . $this->db->escape($data['gp_subcategory_id']) . "',"
        . " gp_custom_title = '"   . $this->db->escape($data['custom_title']) . "',"
        . " product_key_id = '" . (int)$product_id . "',"
        . " tipo_produto = " . $type_product;
      $this->db->query($sql);
    }
  }

  public function getAdtionalFields($product, $type_product, $data){
    $param = array();

    if (!empty($data['gp_department_id'])){
      $param['gp_department_id'] = $data['gp_department_id'];
    } else if (!empty($product['gp_subcategory_id'])){
      $param['gp_department_id'] = $product['gp_department_id'];
    } else {
      $param['gp_department_id'] = 0;
    }

    if (!empty($data['gp_category_id'])){
      $param['gp_category_id'] = $data['gp_category_id'];
    } else if (!empty($product['gp_subcategory_id'])){
      $param['gp_category_id'] = $product['gp_category_id'];
    } else {
      $param['gp_category_id'] = 0;
    }

    if (!empty($data['gp_subcategory_id'])){
      $param['gp_subcategory_id'] = $data['gp_subcategory_id'];
    } else if (!empty($product['gp_subcategory_id'])){
      $param['gp_subcategory_id'] = $product['gp_subcategory_id'];
    } else {
      $param['gp_subcategory_id'] = 0;
    }

    return $param;
  }

}