196 lines
5.3 KiB
PHP
196 lines
5.3 KiB
PHP
<?php
|
||
|
||
class JsonController extends Controller
|
||
{
|
||
|
||
/**
|
||
* Точки
|
||
*/
|
||
static function actionPoints()
|
||
{
|
||
$points = new Point();
|
||
$points = $points->getAll();
|
||
|
||
$categories = new Category();
|
||
$categories = $categories->getAll();
|
||
|
||
$result = array();
|
||
|
||
foreach ($points as $point) {
|
||
$result[$point['id']]['id'] = $point['id'];
|
||
$result[$point['id']]['name'] = $point['name'];
|
||
$result[$point['id']]['lat'] = $point['lat'];
|
||
$result[$point['id']]['lng'] = $point['lng'];
|
||
$result[$point['id']]['categoryIcon'] = $categories[$point['categoryId']]['icon'];
|
||
}
|
||
|
||
self::renderPartial('json.php', array(
|
||
'data' => $result,
|
||
));
|
||
}
|
||
|
||
/**
|
||
* Точка
|
||
*/
|
||
static function actionPoint()
|
||
{
|
||
$id = (int) App::getParam('id');
|
||
if ($id)
|
||
{
|
||
$point = new Point();
|
||
$point = $point->getByPK($id);
|
||
$point->descriptionHtml = nl2br($point->description);
|
||
|
||
$categories = new Category();
|
||
$categories = $categories->getAll();
|
||
|
||
$point->categoryIcon = $categories[$point->categoryId]['icon'];
|
||
$point->categoryName = $categories[$point->categoryId]['name'];
|
||
|
||
self::renderPartial('json.php', $point->getValues());
|
||
} else
|
||
{
|
||
App::error404();
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Категории
|
||
*/
|
||
static function actionCategories()
|
||
{
|
||
$categories = new Category();
|
||
$categories = $categories->getAll();
|
||
|
||
self::renderPartial('json.php', array(
|
||
'data' => $categories,
|
||
));
|
||
}
|
||
|
||
/**
|
||
* Добавление точки
|
||
*/
|
||
static function actionAddPoint()
|
||
{
|
||
$errors = array();
|
||
|
||
if(!( isset($_POST['name']) && $_POST['name'] && strlen($_POST['name']) > 1 )) $errors[1]='Неверное название точки';
|
||
if(!( isset($_POST['img']) && $_POST['img'] && strlen($_POST['img']) > 13 )) $errors[2]='Плохая ссылка на изображение';
|
||
if(!( isset($_POST['description']) && $_POST['description'] && strlen($_POST['description']) > 25 )) $errors[3]='Слишком короткое описание';
|
||
if(!( isset($_POST['categoryId']) && $_POST['categoryId'] )) $errors[4]='Не задана категория точки';
|
||
if(!( isset($_POST['source']) )) $errors[5]='Не передано поле источкика информации. Допускается пустое значение.';
|
||
if(!( isset($_POST['lat']) && $_POST['lat'] )) $errors[6]='Ошибочное значение широты';
|
||
if(!( isset($_POST['lng']) && $_POST['lng'] )) $errors[7]='Ошибочное значение долготы';
|
||
if(!App::$user ) $errors[8]='Вы не авторизованы! Пожалуйста, войдите.';
|
||
if(isset($_POST['id']) && $_POST['id'] && App::$user->isAdmin == 0 ) $errors[9]='Редактирование точек достурно только администратору!';
|
||
|
||
|
||
if (empty($errors))
|
||
{
|
||
$point = new Point();
|
||
|
||
// Редактирование существующей точки
|
||
if (isset($_POST['id']) && $_POST['id'])
|
||
{
|
||
|
||
$id = $_POST['id'];
|
||
$point = $point->getByPK($id);
|
||
|
||
$point->setValues($_POST);
|
||
$point->save();
|
||
|
||
self::renderPartial('json.php', array(
|
||
'data' => $point->id
|
||
));
|
||
|
||
die;
|
||
}
|
||
|
||
$_POST['author'] = App::$user->id;
|
||
$_POST['date'] = time();
|
||
$point->add($_POST);
|
||
|
||
if ( $point->id )
|
||
{
|
||
self::renderPartial('json.php', array(
|
||
'data' => $point->id
|
||
));
|
||
|
||
$ch = curl_init("http://sms.ru/sms/send");
|
||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
||
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
|
||
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
|
||
"api_id" => "2c2af6a7-a935-0e84-5952-599f5cd61905",
|
||
"to" => "79046106141, 79046106144",
|
||
"text" => "#".$point->id.": ".$point->name,
|
||
));
|
||
curl_exec($ch);
|
||
curl_close($ch);
|
||
}
|
||
else
|
||
{
|
||
header("HTTP/1.0 500 Internal Server Error");
|
||
die('Error 500 - Internal Server Error');
|
||
}
|
||
|
||
}
|
||
else
|
||
{
|
||
header("HTTP/1.0 400 Bad Request");
|
||
header('Content-Type: application/json');
|
||
print json_encode($errors);
|
||
}
|
||
}
|
||
|
||
static function actionSaveRoute()
|
||
{
|
||
$request = array(
|
||
'author' => App::$user->id,
|
||
'date' => time(),
|
||
'name' => $_POST['name'],
|
||
'points' => json_encode($_POST['points']),
|
||
);
|
||
|
||
$route = new Route();
|
||
$route = $route->add($request);
|
||
|
||
self::renderPartial('json.php', $route->id);
|
||
}
|
||
|
||
static function actionRoute()
|
||
{
|
||
$id = (int) App::getParam('id');
|
||
if ($id)
|
||
{
|
||
$route = new Route();
|
||
$route = $route->getByPK($id);
|
||
self::renderPartial('json.php', $route->points);
|
||
} else
|
||
{
|
||
App::error404();
|
||
}
|
||
}
|
||
|
||
static function actionDeleteRoute()
|
||
{
|
||
$id = (int) App::getParam('id');
|
||
if ($id && App::$user)
|
||
{
|
||
$route = new Route();
|
||
$route = $route->getByPK($id);
|
||
|
||
if ($route->author == App::$user->id)
|
||
{
|
||
$route->delete($id);
|
||
}
|
||
|
||
self::renderPartial('json.php', true);
|
||
} else
|
||
{
|
||
App::error404();
|
||
}
|
||
}
|
||
|
||
|
||
}
|