<?php

require_once(__DIR__.'/util.php');

use Vn\Lib;
use Vn\Lib\UserException;

/**
 * Uploads a file creating its corresponding sizes.
 */
class Upload extends Vn\Web\JsonRequest {
	const PARAMS = [
		'name',
		'schema'
	];

	function run($db) {
		$util = new Util($this->app);

		$collection = $_REQUEST['schema'];
		$name = $_REQUEST['name'];

		// Checks schema

		$info = $util->loadInfo($collection);
		
		if (!$info)
			throw new UserException(s('Collection not exists'));

		// Checks file name

		if (preg_match('/[^a-z0-9_]/', $name) !== 0)
			throw new UserException(s('Bad file name'));

		// Checks for file errors
		
		if (empty($_FILES['image']['name']))
			throw new UserException(s('File not choosed'));

		if ($_FILES['image']['error'] != 0) {
			switch ($_FILES['image']['error']) {
		        case UPLOAD_ERR_INI_SIZE:
		            $message = 'ErrIniSize';
		            break;
		        case UPLOAD_ERR_FORM_SIZE:
		            $message = 'ErrFormSize';
		            break;
		        case UPLOAD_ERR_PARTIAL:
		            $message = 'ErrPartial';
		            break;
		        case UPLOAD_ERR_NO_FILE:
		            $message = 'ErrNoFile';
		            break;
		        case UPLOAD_ERR_NO_TMP_DIR:
		            $message = 'ErrNoTmpDir';
		            break;
		        case UPLOAD_ERR_CANT_WRITE:
		            $message = 'ErrCantWrite';
		            break;
		        case UPLOAD_ERR_EXTENSION:
		            $message = 'ErrExtension';
		            break;
		        default:
		            $message = 'ErrDefault';
		            break;
		    }

			throw new Lib\Exception(s($message));
		}
		
		$maxSize = $db->getValue('SELECT maxSize FROM imageConfig');

		if ($_FILES['image']['size'] > $maxSize * 1048576)
			throw new UserException(sprintf(s('File size error'), $maxSize));

		// Updates items with matching id, when option is checked

		if ($_REQUEST['updateMatching'] === 'true') {
			$schema = $db->quote($info['schema']);
			$table = $db->quote($info['table']);
			$column = $db->quote($info['column']);

			if (!($schema && $table && $column))
				throw new UserException(s('Cannot update matching id'));
		
			$pk = $db->getRow ("SHOW INDEX FROM $schema.$table WHERE Key_name = 'PRIMARY'");
			if (!$pk) throw new UserException(s('Cannot update matching id'));

			$pkColumn = $db->quote($pk['Column_name']);
	
			$query = "UPDATE $schema.$table SET $column = #name WHERE $pkColumn = #name LIMIT 1";
			$db->query ($query, ['name' => $name]);
		}

		// Resizes and saves the image

		$tmpName = $_FILES['image']['tmp_name'];
		$fileName = "{$name}.png";
		$collectionPath = "{$util->dataDir}/$collection";
		$fullFile = "$collectionPath/full/$fileName";
		$symbolicSrc = "../full/$fileName";
	
		$image = Image::create($tmpName);
		Image::resizeSave($image, $fullFile, $info['maxHeight'], $info['maxWidth']);

		foreach ($info['sizes'] as $size => $i) {
			$dstFile = "$collectionPath/$size/$fileName";
			Image::resizeSave($image, $dstFile, $i['height'], $i['width'], $i['crop'], $symbolicSrc);
		}

		$query =
			"INSERT INTO `image`
				SET `collectionFk` = #collection,
					`name` = #name,
					`updated` = UNIX_TIMESTAMP()
				ON DUPLICATE KEY UPDATE
					`updated` = VALUES(updated)";

		$db->query ($query, [
			'collection' => $collection,
			'name' => $name
		]);

		imagedestroy($image);
		unlink($tmpName);
		return TRUE;
	}
}
