<?php

chdir('../..');
	require_once('api/Simpla.php');
	$simpla = new Simpla();
	

if(isset($_POST['action']) && $_POST['action'] == 'remove'){
	$simpla->db->query("DELETE FROM __article_photo WHERE id=" . (int)$_POST['id']);
	die;
}

if(isset($_POST['action']) && $_POST['action'] == 'reorder'){
	foreach($_POST['ids'] as $pos => $id){
		$simpla->db->query("UPDATE __article_photo SET position='$pos' WHERE id='" . (int)$id ."' " );
	}
	die;
}



$ext = mb_strtolower(substr(strrchr($_FILES['file']['name'], '.'), 1), 'UTF-8');
$name = $_POST['article_id'] . '_' . md5( uniqid( $_POST['article_id'] . rand(1, 10000), true) ) . '.' . $ext;



move_uploaded_file($_FILES['file']['tmp_name'], $_SERVER['DOCUMENT_ROOT'] . '/files/article_photo/' . $name);



$simpla->db->query("INSERT INTO __article_photo SET article_id='" . (int)$_POST['article_id'] ."', img='$name', position=999 " );

echo $simpla->db->insert_id();

//print_r($_POST); print_r($_FILES);
//echo 3;

//echo json_encode(array('id'=>44, 'img'=>'test'));