View file query_process.php

File size: 5.76Kb
<?php
		#################				Автор: redmal								#############
	######################				Сайт: http://srcblog.ru						####################
############################			[email protected]							###########################
include 'system/config.php';
header('Access-Control-Allow-Origin: *');
include 'system/classes/text_valid.php';
include 'system/classes/functions.php';

	$car['brand_car'] = !empty($_POST['brand_car']) ? processText($_POST['brand_car']) : '';
	$car['fuel_consumption'] = isset($_POST['fuel_consumption']) ? floatval($_POST['fuel_consumption']) : 0;
	$car['brand_fuel'] = !empty($_POST['brand_fuel']) ? processText($_POST['brand_fuel']) : '';
	$car['max_speed'] = isset($_POST['max_speed']) ? intval($_POST['max_speed']) : 0;
	
	if(isset($_GET['act']))
	{
			/**************************		Для админа	**************************/
		switch($_GET['act'])
		{
			case 'new_data':
			if($car['brand_car']!='' and $car['fuel_consumption']!=0 and $car['max_speed']!=0)
			{
				
				if(!$sql->get_row('auto_list',array('brand_car'),array('brand_car'),array(strtolower($car['brand_car']) ) ) )
				{
					if($sql->write_data('auto_list',array('brand_car','fuel_consumption','brand_fuel','max_speed'),
					array($car['brand_car'],$car['fuel_consumption'],$car['brand_fuel'],$car['max_speed'])))
					{
						$last_id=mysql_insert_id();
						$row_with_info = $sql->get_row('auto_list',array('brand_car','fuel_consumption','brand_fuel','max_speed'),array('id'),array($last_id) ) ;
						echo '<td>'.$last_id.'</td>
							  <td class="brand_car" onclick="view_form(\'brand_car\','.$last_id.');">'.$row_with_info[0].'</td>
							  <td class="fuel_consumption" onclick="view_form(\'fuel_consumption\','.$last_id.');">'.$row_with_info[1].'</td>
							  <td class="brand_fuel" onclick="view_form(\'brand_fuel\','.$last_id.');">'.$row_with_info[2].'</td>
							  <td class="max_speed" onclick="view_form(\'max_speed\','.$last_id.');">'.$row_with_info[3].'</td>
							  <td><a href="#save" onclick="save_data(\'edit_data\','.$last_id.');">Сохранить изменения</a></td>
							  <td><a href="#" onclick="delete_data('.$last_id.');">Удалить</a></td>';
					}
					else
					echo '<div code="500">Ошибка при записи в БД!</div>';
				}else
				echo '<div code="302">В справочник данное авто уже добавлено!</div>';
			}
			else
			echo '<div code="403">Ошибка заполнения полей!</div>';
			break;
			
			case 'edit_data':
			if(isset($_POST['car_id']) and is_numeric($_POST['car_id']))
			{
				$car['id'] = intval($_POST['car_id']);
				$field = !empty($_POST['field'])?$_POST['field']:null;
				if(is_null($field))//Для сохранения в БД
				{
					if(!$sql->get_row('auto_list',array('brand_car'),array('id'),array($car['id'])) )
					{
						echo '<document><status code="404">Информация об автомобиле не найдена!</status></document>';
					}else
					{
						$data = array();
						$fields = array();
						foreach($car as $key=>$value)
						if($key!='id' and !empty($value))
						{
							$fields[] = $key;
							$data[] = $value;
						}
						if(count($data)>0)
						{
							if($sql->write_data('auto_list',$fields,$data,'`id` = '.$car['id']))
							{
								$row_with_info = $sql->get_row('auto_list',array('brand_car','fuel_consumption','brand_fuel','max_speed'),array('id'),array($car['id']) ) ;
								echo '<td>'.$car['id'].'</td>
								 <td class="brand_car" onclick="view_form(\'brand_car\','.$car['id'].');">'.$row_with_info[0].'</td>
								  <td class="fuel_consumption" onclick="view_form(\'fuel_consumption\','.$car['id'].');">'.$row_with_info[1].'</td>
								  <td class="brand_fuel" onclick="view_form(\'brand_fuel\','.$car['id'].');">'.$row_with_info[2].'</td>
								  <td class="max_speed" onclick="view_form(\'max_speed\','.$car['id'].');">'.$row_with_info[3].'</td>
								  <td><a href="#save" onclick="save_data(\'edit_data\','.$car['id'].');">Сохранить изменения</a></td>
								  <td><a href="#" onclick="delete_data('.$car['id'].');">Удалить</a></td>';
							}
							else
							echo '<document><status code="500">Ошибка при записи в БД!</status></document>';
						}else
						echo '<document><status code="403">Данные не были введены!</status></document>';
					}
					//exit;
				}
				else
				{
					$row_with_info=$sql->get_row('auto_list',array($field),array('id'),array($car['id']));
					if(is_array($row_with_info))
					{
						echo '<input type="text" name="'.$field.'" value="'.$row_with_info[0].'">';
					}
				}
			}
			break;
			
			case 'delete_data':
			if(isset($_POST['car_id']) and is_numeric($_POST['car_id']))
			{
				$car['id'] = intval($_POST['car_id']);
				if(!$sql->get_row('auto_list',array('brand_car'),array('id'),array($car['id']) ) )
				echo '<document><status code="404">Информация об автомобиле не найдена!</status></document>';
				else
				{
					if($sql->delete_row('auto_list',array('id'),array($car['id'])))//Удаление данных
					echo '<document><status code="200">Информация об автомобиле успешно удалена!</status></document>';
					else
					echo '<document><status code="500">Ошибка!</status></document>';
				}
			}else
			echo 'Error!';
			break;
	
		}
	
	}else
	echo 'Вы не имеете доступа к этой странице';
		#################				Автор: redmal								#############
	######################				Сайт: http://srcblog.ru						####################
############################			[email protected]							###########################
?>