wikipoints/ground/controllers/JsonController.php

208 lines
5.9 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 = Point::model()->getAll();
$categories = Category::model()->getAll(array('order' => 'ord ASC', 'asArray' => true));
$result = array();
foreach ($points as $point) {
$id = $point->id;
$result[$id]['id'] = $point->id;
$result[$id]['name'] = $point->name;
$result[$id]['lat'] = $point->lat;
$result[$id]['lng'] = $point->lng;
$result[$id]['categoryId'] = $point->categoryId;
$result[$id]['categoryIcon'] = $categories[$point->categoryId]['icon'];
}
self::renderPartial('json.php', array(
'data' => $result,
));
}
/**
* Точка
*/
static function actionPoint()
{
$id = (int) App::getParam('id');
if ($id)
{
$point = Point::model()->getByPK($id);
$point->descriptionHtml = nl2br($point->description);
$point->name = htmlspecialchars($point->name);
$categories = Category::model()->getAll(array('order' => 'ord ASC', 'asArray' => true));
$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 = Category::model()->getAll(array('order' => 'ord ASC', 'asArray' => true));
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 = Route::model()->add($request);
self::renderPartial('json.php', $route->id);
}
static function actionRoute()
{
$id = (int) App::getParam('id');
if ($id)
{
$route = Route::model()->getByPK($id);
self::renderPartial('json.php', $route->points);
} else
{
App::error404();
}
}
static function actionDeleteRoute()
{
$id = (int) App::getParam('id');
if ($id && App::$user)
{
$route = Route::model()->getByPK($id);
if ($route->author == App::$user->id)
{
$route->delete($id);
}
self::renderPartial('json.php', true);
} else
{
App::error404();
}
}
static function actionGeoCoding()
{
$address = isset($_GET['search']) ? $_GET['search'] : '';
$res = @file_get_contents('http://geocode-maps.yandex.ru/1.x/?format=json&geocode='.urlencode($address));
$res = json_decode($res, true);
if (isset ($res['response']['GeoObjectCollection']['featureMember'][0]['GeoObject']['Point']['pos']))
$point = explode (' ', $res['response']['GeoObjectCollection']['featureMember'][0]['GeoObject']['Point']['pos']);
else
$point = array(0, 0);
$coords = array();
$coords['lat'] = $point[1];
$coords['lng'] = $point[0];
self::renderPartial('json.php', $coords);
}
}