<?php
function obtenerEntityIDcategorias($conn){
	$stmt = $conn->prepare("SELECT entity_type_id FROM fceav_entity_type  WHERE entity_type_code='catalog_category'");
	$stmt->execute();
	$result = $stmt->get_result();
	if($row_m = $result->fetch_assoc()) return $row_m['entity_type_id'];
		else return -1;

}



function existeCategoria($nombrecategoria, $conn){

	$stmt = $conn->prepare("SELECT v.*, e.* FROM fccatalog_category_entity_varchar v, fccatalog_category_entity e  WHERE e.entity_id=v.entity_id AND v.value='$nombrecategoria'");
	$stmt->execute();
	$result = $stmt->get_result();
	if($row_m = $result->fetch_assoc()) return true;
		else return false;

}


function obtenerIDCategoria($nombrecategoria, $conn){
	$stmt = $conn->prepare("SELECT e.entity_id FROM fccatalog_category_entity_varchar v, fccatalog_category_entity e  WHERE e.entity_id=v.entity_id AND v.value='$nombrecategoria'");
	$stmt->execute();
	$result = $stmt->get_result();
	if($row_m = $result->fetch_assoc()) return $row_m['entity_id'];
		else return -1;
}


function aplanarNombreCategoria($nombrecategoria){
	$str = preg_replace("/[^a-zA-Z0-9\s\-Ññ]/", "", $nombrecategoria);
	$str = preg_replace("/[\s]/", "-", $str);
	return strtolower($str);
}


function htmlNombreCategoria($nombrecategoria){
	return aplanarNombreCategoria($nombrecategoria).".html";
}


function altaCategoria($pathcategoria, $entity_categorias, $conn){
	$pathinicial="1/2";
	$level=2;
	$parent=2;


	$att_is_active=obtenerIDAtributo('is_active', $entity_categorias, $conn);
	$att_is_anchor=obtenerIDAtributo('is_anchor', $entity_categorias, $conn);
	$att_include_in_menu=obtenerIDAtributo('include_in_menu', $entity_categorias, $conn);
	$att_cname=obtenerIDAtributo('name', $entity_categorias, $conn);
	$att_curl_key=obtenerIDAtributo('url_key', $entity_categorias, $conn);
	$att_curl_path=obtenerIDAtributo('url_path', $entity_categorias, $conn);
	
	$path_categorias=explode("*",$pathcategoria);
	
	foreach($path_categorias as $categ){
		$arr_categorias=explode("/",$categ);

		foreach($arr_categorias as $item){
			$id_categoria=obtenerIDCategoria($item, $conn);/*$id_categoria=obtenerIDCategoria($pathcategoria, $conn);*/
			if($id_categoria==-1){
				$stmt = $conn->prepare("SELECT IFNULL(MAX(position),0) position FROM fccatalog_category_entity WHERE parent_id=$parent");
				$stmt->execute();
				$result = $stmt->get_result();		
				$row_m = $result->fetch_assoc();
				$position=$row_m['position']+1;
				$stmt = $conn->prepare("INSERT INTO fccatalog_category_entity (entity_type_id, attribute_set_id, parent_id, created_at, updated_at, path, position, level, children_count) VALUES (3, 3, $parent, NOW(), NOW(), '$pathinicial', $position, $level, 0)");
				$stmt->execute();
				$id_categoria=$stmt->insert_id;
				$pathinicial=$pathinicial.'/'.$id_categoria;
				$stmt = $conn->prepare("UPDATE fccatalog_category_entity SET path='$pathinicial' WHERE entity_id=$id_categoria");
				$stmt->execute();
				$stmt = $conn->prepare("UPDATE fccatalog_category_entity SET children_count=children_count+1 WHERE entity_id=$parent");
				$stmt->execute();

				$stmt = $conn->prepare("INSERT INTO fccatalog_category_entity_int (entity_type_id, attribute_id, store_id, entity_id, value) VALUES ($entity_categorias, $att_is_active, 0, $id_categoria, 1)");
				$stmt->execute();
				$stmt = $conn->prepare("INSERT INTO fccatalog_category_entity_int (entity_type_id, attribute_id, store_id, entity_id, value) VALUES ($entity_categorias, $att_is_anchor, 0, $id_categoria, 1)");
				$stmt->execute();
				$stmt = $conn->prepare("INSERT INTO fccatalog_category_entity_int (entity_type_id, attribute_id, store_id, entity_id, value) VALUES ($entity_categorias, $att_include_in_menu, 0, $id_categoria, 1)");
				$stmt->execute();


				$stmt = $conn->prepare("INSERT INTO fccatalog_category_entity_varchar (entity_type_id, attribute_id, store_id, entity_id, value) VALUES ($entity_categorias, $att_cname, 0, $id_categoria, '$item')");
				$stmt->execute();
				$stmt = $conn->prepare("INSERT INTO fccatalog_category_entity_varchar (entity_type_id, attribute_id, store_id, entity_id, value) VALUES ($entity_categorias, $att_curl_key, 0, $id_categoria, '".aplanarNombreCategoria($item)."')");
				$stmt->execute();
				$stmt = $conn->prepare("INSERT INTO fccatalog_category_entity_varchar (entity_type_id, attribute_id, store_id, entity_id, value) VALUES ($entity_categorias, $att_curl_path, 0, $id_categoria, '".htmlNombreCategoria($item)."')");
				$stmt->execute();

			}
			$level++;
			$parent=$id_categoria;
		
		}
	}

	return $id_categoria;

}







function obtenerIDPadrePadres($path_categoria, $nombre, $conn){
	$arr_todas_categorias=explode("/",$path_categoria);
	$count_array=count($arr_todas_categorias);
	/*Eliminamos el hijo*/
	unset($arr_todas_categorias[$count_array-1]);
	$cont=0;
	$encontrado=0;
	foreach($arr_todas_categorias as $item){
		
		if($encontrado==1){
			unset($arr_todas_categorias[$cont]);
		}
		
		if($item==$nombre){
			$encontrado=1;	
		}
		$cont++;
	}
	
	
	
	$padre=-2;
	if(count($arr_todas_categorias)>4){
		
		
		foreach($arr_todas_categorias as $item){$padre++;}
		$tataraabuelo=$padre-3;
		$stmt = $conn->prepare("SELECT c.id_category as categoria FROM fccategory c, fccategory_lang l  WHERE c.id_category=l.id_category and  l.link_rewrite='$arr_todas_categorias[$tataraabuelo]'");
		$stmt->execute();
		$result = $stmt->get_result();
		$row_m = $result->fetch_assoc();
		$id_aux_tatarabuelo=$row_m['categoria'];
		
		
		$bisabuelo=$padre-2;
		/*id del abuelo padre para asegurarnos de que no hay dos categorias con ese nombre*/
		$stmt = $conn->prepare("SELECT c.id_category as categoria FROM fccategory c, fccategory_lang l  WHERE c.id_category=l.id_category and  l.link_rewrite='$arr_todas_categorias[$bisabuelo]' AND c.id_parent='$id_aux_tatarabuelo'");
		$stmt->execute();
		$result = $stmt->get_result();
		$row_m = $result->fetch_assoc();
		$id_aux_bisabuelo=$row_m['categoria'];
		
		$padre_anterior=$padre-1;
			/*id del padre del padre para asegurarnos de que no hay dos categorias en ese nivel con ese nombre*/
			$stmt = $conn->prepare("SELECT c.id_category as categoria FROM fccategory c, fccategory_lang l  WHERE c.id_category=l.id_category and  l.link_rewrite='$arr_todas_categorias[$padre_anterior]' AND c.id_parent='$id_aux_bisabuelo'");
			$stmt->execute();
			$result = $stmt->get_result();
			$row_m = $result->fetch_assoc();
			$id_aux=$row_m['categoria'];
			
			$stmt = $conn->prepare("SELECT c.id_category as categoria FROM fccategory c, fccategory_lang l  WHERE c.id_category=l.id_category and  l.link_rewrite='$arr_todas_categorias[$padre]' AND c.id_parent='$id_aux'");
			$stmt->execute();
			
			$result = $stmt->get_result();
			if($row_m = $result->fetch_assoc()) return $row_m['categoria'];
				else return -1;
		
		}
		
		
		else if(count($arr_todas_categorias)==4){
			
			
			foreach($arr_todas_categorias as $item){$padre++;}
			
			$bisabuelo=$padre-2;
			/*id del abuelo padre para asegurarnos de que no hay dos categorias con ese nombre*/
			$stmt = $conn->prepare("SELECT c.id_category as categoria FROM fccategory c, fccategory_lang l  WHERE c.id_category=l.id_category and  l.link_rewrite='$arr_todas_categorias[$bisabuelo]'");
			$stmt->execute();
			$result = $stmt->get_result();
			$row_m = $result->fetch_assoc();
			$id_aux_bisabuelo=$row_m['categoria'];
			
			$padre_anterior=$padre-1;
			/*id del padre del padre para asegurarnos de que no hay dos categorias en ese nivel con ese nombre*/
			$stmt = $conn->prepare("SELECT c.id_category categoria FROM fccategory c, fccategory_lang l  WHERE c.id_category=l.id_category and  l.link_rewrite='$arr_todas_categorias[$padre_anterior]' AND c.id_parent='$id_aux_bisabuelo'");
			$stmt->execute();
			$result = $stmt->get_result();
			$row_m = $result->fetch_assoc();
			$id_aux=$row_m['categoria'];
			
			$stmt = $conn->prepare("SELECT c.id_category as categoria FROM fccategory c, fccategory_lang l  WHERE c.id_category=l.id_category and  l.link_rewrite='$arr_todas_categorias[$padre]' AND c.id_parent='$id_aux'");
			$stmt->execute();
			
			$result = $stmt->get_result();
			if($row_m = $result->fetch_assoc()) return $row_m['categoria'];
				else return -1;
			
			
		}
		
		else if(count($arr_todas_categorias)==3){
		
			foreach($arr_todas_categorias as $item){$padre++;}
			$padre_anterior=$padre-1;
			/*id del padre del padre para asegurarnos de que no hay dos categorias en ese nivel con ese nombre*/
			$stmt = $conn->prepare("SELECT c.id_category as categoria FROM fccategory c, fccategory_lang l  WHERE c.id_category=l.id_category and  l.link_rewrite='$arr_todas_categorias[$padre_anterior]'");
			$stmt->execute();
			$result = $stmt->get_result();
			$row_m = $result->fetch_assoc();
			$id_aux=$row_m['categoria'];
			
			$stmt = $conn->prepare("SELECT c.id_category as categoria FROM fccategory c, fccategory_lang l  WHERE c.id_category=l.id_category and  l.link_rewrite='$arr_todas_categorias[$padre]' AND c.id_parent='$id_aux'");
			$stmt->execute();
			
			$result = $stmt->get_result();
			if($row_m = $result->fetch_assoc()) return $row_m['categoria'];
				else return -1;
		
		}
		
		
		
		
		else if(count($arr_todas_categorias)==2){
		
			foreach($arr_todas_categorias as $item){$padre++;}
									
			$stmt = $conn->prepare("SELECT c.id_category as categoria FROM fccategory c, fccategory_lang l  WHERE c.id_category=l.id_category and  l.link_rewrite='$arr_todas_categorias[$padre]'");
			$stmt->execute();
			
			$result = $stmt->get_result();
			if($row_m = $result->fetch_assoc()) return $row_m['categoria'];
				else return -1;
		}
		
		else if(count($arr_todas_categorias)==1){
			
			return 1;
		}
			
	
	
}


function obtenerIDArrayporHijo($id_categoria, $arrays_ids,$conn){



	//print_r($arrays_ids);
	$id=0;
	foreach($arrays_ids as $array){
		
	//	print_r($array);
	//	echo "estamos en el array ".$id."\n";
		
		if(empty($array)){
			return $id;
			
			
		}
		
		
		foreach($array as $categoria_hijo){
			
			
		
		$sql="SELECT * from fccategory WHERE id_category='".$categoria_hijo."' and id_parent='".$id_categoria."'";
						
				//echo $sql."\n";
				$stmtPrestashop = $conn->prepare($sql);
				$stmtPrestashop->execute();
				$resultPrestashop = $stmtPrestashop->get_result();
						
				if($rowPrestashop = $resultPrestashop->fetch_assoc()){
					
					return $id;
					
				}
			
			
			
		
		
		}
		$id++;
		
	
	}
	
	return $id;
	
	
	
}



function obtenerIDPadre($path_categoria, $conn){
	
	//	echo "Entramos en obtener ID Padre \n\n";
		

		$padre=-2;
		$arr_categorias=explode("/",$path_categoria);
	
	//	echo "El array tiene ".count($arr_categorias)." posiciones \n\n";
		if(count($arr_categorias)>4){
		
		
		foreach($arr_categorias as $item){$padre++;}
		$tataraabuelo=$padre-3;
		$stmt = $conn->prepare("SELECT c.id_category categoria FROM fccategory c, fccategory_lang l  WHERE c.id_category=l.id_category and  l.link_rewrite='$arr_categorias[$tataraabuelo]'");
		$stmt->execute();
		$result = $stmt->get_result();
		$row_m = $result->fetch_assoc();
		$id_aux_tatarabuelo=$row_m['categoria'];
		
		
		$bisabuelo=$padre-2;
		/*id del abuelo padre para asegurarnos de que no hay dos categorias con ese nombre*/
		$stmt = $conn->prepare("SELECT c.id_category as categoria FROM fccategory c, fccategory_lang l  WHERE c.id_category=l.id_category and  l.link_rewrite='$arr_categorias[$bisabuelo]' AND c.id_parent='$id_aux_tatarabuelo'");
		$stmt->execute();
		$result = $stmt->get_result();
		$row_m = $result->fetch_assoc();
		$id_aux_bisabuelo=$row_m['categoria'];
		
		$padre_anterior=$padre-1;
			/*id del padre del padre para asegurarnos de que no hay dos categorias en ese nivel con ese nombre*/
			$stmt = $conn->prepare("SELECT c.id_category as categoria FROM fccategory c, fccategory_lang l  WHERE c.id_category=l.id_category and  l.link_rewrite='$arr_categorias[$padre_anterior]' AND c.id_parent='$id_aux_bisabuelo'");
			$stmt->execute();
			$result = $stmt->get_result();
			$row_m = $result->fetch_assoc();
			$id_aux=$row_m['categoria'];
			
			$stmt = $conn->prepare("SELECT c.id_category as categoria FROM fccategory c, fccategory_lang l  WHERE c.id_category=l.id_category and l.link_rewrite='$arr_categorias[$padre]' AND c.id_parent='$id_aux'");
			$stmt->execute();
			
			$result = $stmt->get_result();
			if($row_m = $result->fetch_assoc()) return $row_m['categoria'];
				else return -1;
		
		}
		
		
		else if(count($arr_categorias)==4){
			
			
			foreach($arr_categorias as $item){$padre++;}
			
			$bisabuelo=$padre-2;
			
			/*id del abuelo padre para asegurarnos de que no hay dos categorias con ese nombre*/
			$stmt = $conn->prepare("SELECT c.id_category as categoria FROM fccategory c, fccategory_lang l WHERE c.id_category=l.id_category and l.link_rewrite='$arr_categorias[$bisabuelo]'");
			$stmt->execute();
			$result = $stmt->get_result();
			$row_m = $result->fetch_assoc();
			$id_aux_bisabuelo=$row_m['categoria'];
			
			$padre_anterior=$padre-1;
			/*id del padre del padre para asegurarnos de que no hay dos categorias en ese nivel con ese nombre*/
			$stmt = $conn->prepare("SELECT c.id_category as categoria FROM fccategory c, fccategory_lang l WHERE c.id_category=l.id_category and l.link_rewrite='$arr_categorias[$padre_anterior]' AND c.id_parent='$id_aux_bisabuelo'");
			$stmt->execute();
			$result = $stmt->get_result();
			$row_m = $result->fetch_assoc();
			$id_aux=$row_m['categoria'];
			
			$stmt = $conn->prepare("SELECT c.id_category as categoria FROM fccategory c, fccategory_lang l  WHERE c.id_category=l.id_category and  l.link_rewrite='$arr_categorias[$padre]' AND c.id_parent='$id_aux'");
			$stmt->execute();
			
			$result = $stmt->get_result();
			if($row_m = $result->fetch_assoc()) return $row_m['categoria'];
				else return -1;
			
			
		}
		
		else if(count($arr_categorias)==3){
		
			foreach($arr_categorias as $item){$padre++;}
			$padre_anterior=$padre-1;
			/*id del padre del padre para asegurarnos de que no hay dos categorias en ese nivel con ese nombre*/
			$stmt = $conn->prepare("SELECT c.id_category as categoria FROM fccategory c, fccategory_lang l  WHERE c.id_category=l.id_category and  l.link_rewrite='$arr_categorias[$padre_anterior]'");
			$stmt->execute();
			$result = $stmt->get_result();
			$row_m = $result->fetch_assoc();
			$id_aux=$row_m['categoria'];
			
			$stmt = $conn->prepare("SELECT c.id_category as categoria FROM fccategory c, fccategory_lang l  WHERE c.id_category=l.id_category and  l.link_rewrite='$arr_categorias[$padre]' AND c.id_parent='$id_aux'");
			$stmt->execute();
			
			$result = $stmt->get_result();
			if($row_m = $result->fetch_assoc()) return $row_m['categoria'];
				else return -1;
		
		}
		
		
		
		else if(count($arr_categorias)==2){

			foreach($arr_categorias as $item){$padre++;}
			
			if($padre==0){
				$stmt = $conn->prepare("SELECT c.id_category as categoria FROM fccategory c, fccategory_lang l  WHERE c.id_category=l.id_category and  l.link_rewrite='$arr_categorias[$padre]' and c.id_parent=2");
				$stmt->execute();
			
			}
			
			else{
			
				$stmt = $conn->prepare("SELECT c.id_category as categoria FROM fccategory c, fccategory_lang l  WHERE c.id_category=l.id_category and  l.link_rewrite='$arr_categorias[$padre]'");
				$stmt->execute();
			
			}
			
			$result = $stmt->get_result();
			if($row_m = $result->fetch_assoc()) return $row_m['categoria'];
				else return -1;
		}
		
		else if(count($arr_categorias)==1){
			return 1;
		}
		

	
	
}



function obtenerAnchorTextCategoria($path_categoria, $conn){
	
	$arr_categorias=explode("/",$path_categoria);
	return end($arr_categorias);
	

}


?>