<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Редактор (Actions)</title>
    <link href="style/style.css" rel="stylesheet">
    <script src="style/jquery.js" type="text/javascript"></script>
    <script src="style/modal.js" type="text/javascript"></script>
    <script type="text/javascript">
		$(document).ready(function(){
			$('.delete').click(function(){
				$('#modal a.btn').attr('href',$(this).attr('href'));
				$('#modal').arcticmodal();
				return false;
			});
		});
	</script>
</head>
<body>
<div class="container">
<?php

	$uploaddir = './data/images/';
	
	function json_encode_cyr($str) {
		$arr_replace_utf = array('\u0410', '\u0430','\u0411','\u0431','\u0412','\u0432',
		'\u0413','\u0433','\u0414','\u0434','\u0415','\u0435','\u0401','\u0451','\u0416',
		'\u0436','\u0417','\u0437','\u0418','\u0438','\u0419','\u0439','\u041a','\u043a',
		'\u041b','\u043b','\u041c','\u043c','\u041d','\u043d','\u041e','\u043e','\u041f',
		'\u043f','\u0420','\u0440','\u0421','\u0441','\u0422','\u0442','\u0423','\u0443',
		'\u0424','\u0444','\u0425','\u0445','\u0426','\u0446','\u0427','\u0447','\u0428',
		'\u0448','\u0429','\u0449','\u042a','\u044a','\u042b','\u044b','\u042c','\u044c',
		'\u042d','\u044d','\u042e','\u044e','\u042f','\u044f');
		$arr_replace_cyr = array('А', 'а', 'Б', 'б', 'В', 'в', 'Г', 'г', 'Д', 'д', 'Е', 'е',
		'Ё', 'ё', 'Ж','ж','З','з','И','и','Й','й','К','к','Л','л','М','м','Н','н','О','о',
		'П','п','Р','р','С','с','Т','т','У','у','Ф','ф','Х','х','Ц','ц','Ч','ч','Ш','ш',
		'Щ','щ','Ъ','ъ','Ы','ы','Ь','ь','Э','э','Ю','ю','Я','я');
		$str1 = json_encode($str);
		$str2 = str_replace($arr_replace_utf,$arr_replace_cyr,$str1);
		return $str2;
	}

	$json_url = "actions.json";
	$json = file_get_contents($json_url);
	$links = json_decode($json, true);
	
	if(intval($_GET['save']) > 0) {
		
		$id = intval($_GET['save'])-1;
		
		$uploadfile = $uploaddir . basename($_FILES['images']['name']);
		if(move_uploaded_file($_FILES['images']['tmp_name'], $uploadfile)) {
			$links[$id]['images'][0] = $uploadfile.'?'.date('d.m.Y');
		}
		
		if(!empty($_POST['title'])) { 		$links[$id]['title'] = $_POST['title']; }
		if(!empty($_POST['description'])) { $links[$id]['description'] = $_POST['description']; }
		if(!empty($_POST['details'])) { 	$links[$id]['details'] = $_POST['details']; }
		if(!empty($_POST['phone'])) { 		$links[$id]['phone'] = $_POST['phone']; }
		if(!empty($_POST['email'])) { 		$links[$id]['email'] = $_POST['email']; }
		if(!empty($_POST['date_to'])) { 	$links[$id]['date_to'] = $_POST['date_to']; }
		if(!empty($_POST['feedback'])) { 	$links[$id]['feedback'] = $_POST['feedback']; }
		
		$fp = fopen('actions.json', 'w');
		fwrite($fp, json_encode_cyr(array_values($links)));
		fclose($fp);
		?>
			<h1>Запись сохранена!</h1>
			<a href="actions.php">Вернутся к списку файлов</a>
		<?
		
	} else if($_GET['action'] == "add_item") {
		
		$uploadfile = $uploaddir . basename($_FILES['images']['name']);
		if(move_uploaded_file($_FILES['images']['tmp_name'], $uploadfile)) {

			$add['images'][0] = $uploadfile.'?'.date('d.m.Y');
		}
		
		if(!empty($_POST['title'])) { 		$add['title'] = $_POST['title']; }
		if(!empty($_POST['description'])) { $add['description'] = $_POST['description']; }
		if(!empty($_POST['details'])) { 	$add['details'] = $_POST['details']; }
		if(!empty($_POST['phone'])) { 		$add['phone'] = $_POST['phone']; }
		if(!empty($_POST['email'])) { 		$add['email'] = $_POST['email']; }
		if(!empty($_POST['date_to'])) { 	$add['date_to'] = $_POST['date_to']; }
		if(!empty($_POST['feedback'])) { 	$add['feedback'] = $_POST['feedback']; }
		
		array_unshift( $links, $add );
		
		$fp = fopen('actions.json', 'w');
		fwrite($fp, json_encode_cyr(array_values($links)));
		fclose($fp);
?>
		<h1>Запись добавлена!</h1>
		<a href="actions.php">Вернутся к списку файлов</a>
		
<?	
		
	} else if(intval($_GET['delete']) > 0) {
		
		unset($links[intval($_GET['delete'])-1]);
		$fp = fopen('actions.json', 'w');
		fwrite($fp, json_encode(array_values($links)));
		fclose($fp);
		
?>
	<h1>Запись удалена!</h1>
	<a href="actions.php">Вернутся к списку файлов</a>
<?
		
	} else if(intval($_GET['edit']) > 0) {
		
?>
	<h1>Редактирование json (Actions)</h1>
	
	<form method="post" action="actions.php?save=<?=$_GET['edit'];?>" enctype="multipart/form-data">
		<?
			$array = array(
				'title' 		=> $links[intval($_GET['edit'])-1]['title'],
				'description' 	=>  $links[intval($_GET['edit'])-1]['description'],
				'details' 		=>  $links[intval($_GET['edit'])-1]['details'],
				'feedback' 		=> $links[intval($_GET['edit'])-1]['feedback'],
				'phone' 		=>  $links[intval($_GET['edit'])-1]['phone'],
				'email' 		=>  $links[intval($_GET['edit'])-1]['email'],
				'date_to' 		=>  $links[intval($_GET['edit'])-1]['date_to'],
				'images' 		=>  $links[intval($_GET['edit'])-1]['images'],
				
			);
			foreach($array as $key=>$value){
				if($key == 'images') {
					$value = $value[0];
		?>
					<p><?=$key;?>:</p>
					<br/>
					<img src="<?=$value;?>" width="100px" alt="">
					<br/>
					<br/>
					<input type="file" name="<?=$key;?>">
					<br/>
					<br/>
		<?
				} else if($key == 'description' or $key == 'details') {
		?>
			<p><?=$key;?>:</p>
			<textarea name='<?=$key;?>'><?=$value;?></textarea>
		<?
				} else {
		?>
			<p><?=$key;?>:</p>
			<input type='text' value='<?=$value;?>' name='<?=$key;?>'>
		<?
				}
			}
		?>
			<input type="hidden" value="<?=$_GET['edit'];?>" name="id">
			<input type="submit" class="btn" value="Сохранить запись">
	</form>
<?
	} else if($_GET['action'] == "add") {
		
?>
	<h1>Добавить запись (Actions)</h1>
	<form method="post" action="actions.php?action=add_item" enctype="multipart/form-data">
		<p>title:</p>
		<input type="text" value="" name="title">
		<p>description:</p>
		<textarea name="description"></textarea>
		<p>Details:</p>
		<textarea name="details"></textarea>
		<p>Feedback:</p>
		<input type="text" value="" name="feedback">
		<p>phone:</p>
		<input type="text" value="" name="phone">
		<p>email:</p>
		<input type="text" value="" name="email">
		<p>date to:</p>
		<input type="text" value="" name="date_to">
		<p>images:</p>
		<input type="file" name="images">
		<input type="submit" class="btn" value="Добавить запись">
	</form>
<?
		
	} else {
?>
	<h1>Редактирор json (Actions)</h1>
	<table cellpadding="10" cellspacing="0" border="1">
		<tr>
			<td>Фото</td>
			<td>Заголовок</td>
			<td>Описание</td>
			<td>Детали</td>
			<td>Кнопка</td>
			<td>Почта</td>
			<td>Телефон</td>
			<td colspan="2">Опции</td>
		</tr>
	<?php
		$i = 0;
		foreach($links as $val){ 
			$i++;
	?>
		<tr>
			<td><img src="<?php echo $val['images'][0] ?>" width="100px" alt=""></td>
			<td><?php echo $val['title'] ?></td>
			<td><?php echo $val['description'] ?></td>
			<td><?php echo $val['details'] ?></td>
			<td><?php echo $val['feedback'] ?></td>
			<td><?php echo $val['email'] ?></td>
			<td><?php echo $val['phone'] ?></td>
			<td><a href="?edit=<?=$i ?>">Редактировать</a></td>
			<td><a href="?delete=<?=$i ?>" class="delete">Удалить</a></td>
		</tr>
	<?php
			
		}
	?>
	</table>
	<a href="?action=add" class="btn" target="_blank">Добавить запись</a>
	<div style="display: none;">
		<div class="social" id="modal">
			<div class="m-title">Удалить запись</div>
			<div class="close arcticmodal-close"></div>
			<div class="over">
				<h3>Вы действительно хотите удалить запись?</h3>
				<a href="#" class="btn">Удалить</a>
			</div>
		</div>
	</div>
<?
	}
?>
<br/>
<br/>
<a href="actions.php">Список записей</a>
</br>
</div>
</body>