wikipoints/ground/controllers/JsonController.php

410 lines
11 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()
{
if (App::$user && App::$user->isAdmin == 1) {
$points = Point::model()->getAll();
} else {
$points = Point::model()->getAllPublished();
}
$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]['moderate'] = $point->moderate ? 'show' : 'hide';
}
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->descriptionHtml = Helper::replaceLinks($point->descriptionHtml, '/?point=');
$point->name = htmlspecialchars($point->name);
$point->moderate = $point->moderate ? 'show' : 'hide';
$imgs = $point->photos;
$photosCount = 1;
if ($point->img == '' && !empty($imgs))
{
$img = array_shift($imgs);
$point->img = $img->name;
}
$imgs = $point->photos;
if (!empty($imgs))
{
$tmp = array();
$photosCount = count($imgs);
foreach ($imgs as $img)
{
$tmp[] = $img->name;
}
$point->images = $tmp;
}
$categories = Category::model()->getAll(array('order' => 'ord ASC', 'asArray' => true));
$point->categoryIcon = $categories[$point->categoryId]['icon'];
$point->categoryName = $categories[$point->categoryId]['name'];
$point->photosCount = $photosCount;
$point->inFavorites = Favorite::model()->checkIsMy($id);
$point->iLike = Like::model()->checkIsMy($id);
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 (!App::$user)
{
$errors[8] = 'Вы не авторизованы! Пожалуйста, войдите.';
}
if (isset($_POST['id']) && $_POST['id'] && App::$user->isAdmin == 0)
{
$point = new Point();
$id = (int) $_POST['id'];
$point = $point->getByPK($id);
if ($point->author != App::$user->id || ($point->date + 1800 < time() ))
{
$errors[9] = 'Редактирование точек достурно только администратору!';
}
}
$photos = $images = array();
if (empty($errors) && !App::$request['ajax'] && isset($_POST['id']) && (int) $_POST['id'])
{
$id = (int) $_POST['id'];
$pathInfo = pathinfo($_SERVER['SCRIPT_FILENAME']);
$uploaddir = $pathInfo['dirname'] . '/photos/';
if (isset($_FILES) && !empty($_FILES))
{
foreach ($_FILES['photos']['name'] as $key => $fileName)
{
$temp = pathinfo($_FILES['photos']['name'][$key]);
$extension = strtolower($temp['extension']) ? strtolower($temp['extension']) : 'jpg';
$photo = $id . strtolower('_' . substr(md5($fileName . time()), 0, 5) . '.' . $extension);
if (move_uploaded_file($_FILES['photos']['tmp_name'][$key], $uploaddir . $photo))
{
$photos[] = $photo;
}
}
}
if (isset($_POST['imgs']) && !empty($_POST['imgs']))
{
foreach ($_POST['imgs'] as $key => $img)
{
if (mb_strpos($img, '://') !== false)
{
$temp = pathinfo($img);
$extension = strtolower($temp['extension']) ? strtolower($temp['extension']) : 'jpg';
$photo = $id . strtolower('_' . substr(md5($img . time()), 0, 5) . '.' . $extension);
if (file_put_contents($uploaddir . $photo, file_get_contents($img)))
{
PhotoLinks::addLink($photo, $img);
$images[] = $photo;
}
} else
{
$images[] = $img;
}
}
}
$resPhotos = array();
$ordering = $_POST['ordering'];
foreach ($ordering as $order)
{
$resPhotos[] = $order == 'FILE' ? array_shift($photos) : array_shift($images);
}
if (!empty($resPhotos))
{
App::DB()->query('DELETE FROM `poi`.`photos` WHERE `pointId` = ' . $id . ';');
$P = new Photo();
foreach ($resPhotos as $key => $photo)
{
$P->add(array(
'pointId' => $id,
'name' => $photo,
'ord' => $key,
));
Image::createAllSizes($photo);
if ($ordering[$key] == 'FILE') {
PhotoLinks::addLink($photo, $_POST['photoLinks'][$key]);
}
}
$point = Point::model()->getByPK($id);
$point->img = '';
$point->save();
}
App::redirect('/?point=' . $id);
}
if (!( isset($_POST['name']) && $_POST['name'] && strlen($_POST['name']) > 1 ))
$errors[1] = 'Неверное название точки';
if (!( isset($_POST['img']) && (int) $_POST['img']))
$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 (!empty($errors))
{
header("HTTP/1.0 400 Bad Request");
header('Content-Type: application/json');
print json_encode($errors);
return false;
}
if (App::$request['ajax'])
{
$point = new Point();
unset($_POST['img']);
if (isset($_POST['id']) && $_POST['id'])
{
// Редактирование существующей точки
$id = (int) $_POST['id'];
$point = $point->getByPK($id);
$point->setValues($_POST);
$point->save();
} else
{
$_POST['author'] = App::$user->id;
$_POST['date'] = time();
$_POST['moderate'] = (App::$user && App::$user->isAdmin == 1) ? $_POST['moderate'] : 0;
$point->add($_POST);
if (!$point->id)
{
header("HTTP/1.0 500 Internal Server Error");
die('Error 500 - Internal Server Error');
}
if (!App::getConfig('disableSMS'))
{
$ch = curl_init("http://sms.ru/sms/send");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
"api_id" => "2c2af6a7-a935-0e84-5952-599f5cd61905",
"to" => "79046106141, 79046106144",
"text" => "#" . $point->id . ": " . $point->name,
"from" => "WIKIPOINTS",
));
curl_exec($ch);
curl_close($ch);
}
}
self::renderPartial('json.php', array(
'data' => $point->id
));
}
}
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);
}
static function actionAddRemoveFavorites()
{
$id = (int) App::getParam('id');
if (!$id || !App::$user)
{
App::error404();
}
self::renderPartial('json.php', Helper::boolval(Favorite::addRemove($id)));
}
static function actionAddRemoveLike()
{
$id = (int) App::getParam('id');
if (!$id || !App::$user)
{
App::error404();
}
self::renderPartial('json.php', Helper::boolval(Like::addRemove($id)));
}
static function actionSearch()
{
$search = isset($_GET['query']) ? $_GET['query'] : '';
$results = array();
if ($search)
{
// ====== POINTS ============
$points = Point::model()->searchByString($search);
if (!empty($points))
{
foreach ($points as $point)
{
$results[] = array('value' => $point->name, 'data' => '/?point='.$point->id);
}
}
if (count($results) < 10)
{
// ====== YANDEX ============
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://geocode-maps.yandex.ru/1.x/?geocode=' . urlencode($search));
curl_setopt($ch, CURLOPT_TIMEOUT, 3);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$out = null;
$out = curl_exec($ch); // выполняем запрос curl - обращаемся к сервера php.su
if (!empty($out))
{
$obj = simplexml_load_string($out);
if (isset($obj->GeoObjectCollection->metaDataProperty->GeocoderResponseMetaData->found))
{
$count = (int) $obj->GeoObjectCollection->metaDataProperty->GeocoderResponseMetaData->found;
if ($count > 0)
{
for ($index = 0; $index < $count && $index < 5 && count($results) < 25; $index++)
{
$name = (string) $obj->GeoObjectCollection->featureMember[$index]->GeoObject->name;
$coords = explode(' ', (string) $obj->GeoObjectCollection->featureMember[$index]->GeoObject->Point->pos);
$lat = $coords[1];
$lng = $coords[0];
$results[] = array('value' => $name, 'data' => "/?lat=$lat&lng=$lng&zoom=13");
}
}
}
}
curl_close($ch);
}
}
self::renderPartial('json.php', array('query' => $search, 'suggestions' => $results));
}
}