wikipoints/ground/controllers/JsonController.php

166 lines
4.3 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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->description = 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 (empty($errors))
{
$point = new Point();
$_POST['author'] = App::$user->id;
$_POST['date'] = time();
$point->add($_POST);
if ( $point->id )
{
self::renderPartial('json.php', array(
'data' => $point->id
));
}
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();
}
}
}