<?php
/////////////////////////////////////////////////////////////////////////////////////////
//
//   this file has been generated by sformi, the clever tool for lazy developers.
//
//

session_start();

if (!session_is_registered("clave_audio")) {
	include("../login.php");
	if (!$continuar) exit;
}

include('../db.php');

	require("../_incl/upload.class.php");

	function uploadImagen($prodID, $size1_w, $size1_h, $size2_w=0, $size2_h=0, $mascara='') {
		$path = "../../_pics/";											// este directorio tiene que tener chmod 777
		$upload_file_name = "prod_imagen";									// el nombre del campo tipo file de nuestro formulario
		$acceptable_file_types = "image/gif|image/jpeg|image/pjpeg";	// acepta GIF et JPEG
		$default_extension = "";
		$mode = 1;		// OPTIONS:
						//	   1 = overwrite mode
						//	   2 = create new with incremental extention
						//	   3 = do nothing if exists, highest protection

		if(strlen($_FILES['prod_imagen']['name'])) {
			$my_uploader = new uploader('es');			// Create a new instance of the class
			$my_uploader->max_filesize(150000);			// OPTIONAL: set the max filesize of uploadable files in bytes
			$my_uploader->max_image_size(1600, 1600);	// OPTIONAL: if you're uploading images, you can set the max pixel dimensions			
				// UPLOAD the file
			if ($my_uploader->upload($upload_file_name, $acceptable_file_types, $default_extension)) {
				$my_uploader->save_file($path, $prodID . '_tmp', $mode);
				if (!$my_uploader->error) {
					$my_uploader->resize($prodID . '_' . $size1_w . '.jpg', $size1_w, $size1_h);
					if($size2_w) {
						$my_uploader->crop($prodID . '_' . $size2_w . '.jpg', $size2_w, $size2_h, 0.4);
					}
					$my_uploader->delete_file;
				}
			}
			if ($my_uploader->error) {
				echo $my_uploader->error;
			}
		}
 	}


if(isset($_REQUEST['op'])){
	$operacion = $_REQUEST['op'];
} else {		// no han pasado un c—digo de operaci—n. bad yuyu
	header('Location: articulos.php');
}

// here I read the variables passed by the calling page.	
$id = $_REQUEST['id'];		
$img = $_REQUEST['img'];		

$conn = db_connect();
if($conn) {
	switch($operacion) {
		case "add":
			$num = '1';
			$sql = 'SELECT galeria_imagen_name FROM edito_galeria_imagenes WHERE galeria_articulo_id = ' . $id . ' ORDER BY galeria_imagen_name DESC LIMIT 1';
			$result = mysql_query($sql, $conn);
			if($result) {
				if(mysql_num_rows($result)) {
					$row = mysql_fetch_array($result);
					$num = str_replace($id . '_', '', $row['galeria_imagen_name']) + 1;
				}
			}
			$nuName = $id . '_' . $num;
			$sql = 'INSERT INTO edito_galeria_imagenes(galeria_articulo_id, galeria_imagen_name)
							VALUES("' . $id . '", "' . $nuName . '")';
			$result = mysql_query($sql, $conn);
			if($result) {
				//  subo la imagen
				uploadImagen($nuName, 348, 248, 108, 108);
				//	y vuelvo a la pantalla correspondiente
				header('Location: articulos_edit.php?op=edit&id=' . $id);
			}
			break;

		case "del":
			$sql = 'DELETE FROM edito_galeria_imagenes WHERE galeria_imagen_name = "' . $img . '" AND galeria_articulo_id = ' . $id;
			mysql_query($sql, $conn);

		default:
			header('Location: articulos_edit.php?op=edit&id=' . $id);
			break;
	}
} else {	// no tengo conexion con el server
	header('Location: articulos.php');
}
?>