ADD ApiController

This commit is contained in:
Krivchikov Dmitry 2016-04-24 00:00:05 +03:00
parent 04d065b774
commit 9b7653bd94
1 changed files with 141 additions and 0 deletions

View File

@ -0,0 +1,141 @@
<?php
class ApiController extends Controller
{
static function actionIndex()
{
$request = json_decode(file_get_contents('php://input'), 1);
if (!is_array($request)) {
App::error404();
}
$method = isset($request['method']) ? $request['method'] : NULL;
$data = isset($request['data']) ? $request['data'] : NULL;
$environment = isset($request['environment']) ? $request['environment'] : NULL;
$answer = NULL;
App::log('apiController', $array($method, $data, $environment));
switch ($method) {
case 'categories':
$answer = self::getCategories();
break;
case 'points':
$answer = self::getPoints($data);
break;
case 'point':
$answer = self::getPoint($data);
break;
default:
App::error404();
break;
}
self::renderPartial('json.php', $answer);
}
/**
* Точки
*/
static private function getPoints($data)
{
$minEditDate = isset($data['time']) ? intval($data['time']) : 0;
$points = Point::model()->getAllPublished(false, $minEditDate);
$categories = Category::model()->getAll(array('order' => 'ord ASC', 'asArray' => true));
$result = array();
foreach ($points as $point) {
$tempPoint['id'] = $point->id;
$tempPoint['name'] = $point->name;
$tempPoint['lat'] = $point->lat;
$tempPoint['lng'] = $point->lng;
$tempPoint['categoryId'] = $point->categoryId;
$tempPoint['status'] = $point->status;
$result[] = $tempPoint;
}
return array('data' => $result);
}
/**
* Точка
*/
static private function getPoint($data)
{
$id = isset($data['id']) ? intval($data['id']) : NULL;
if (!$id) {
return FALSE;
}
$point = Point::model()->getByPK($id);
if (!$point) {
return FALSE;
}
$point->descriptionHtml = nl2br($point->description);
$point->descriptionHtml = Helper::replaceLinks($point->descriptionHtml, 'js');
$point->name = htmlspecialchars($point->name);
$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;
}
$extraParams = $point->getExtraParams();
if (!empty($extraParams)) {
$tmp = array();
foreach ($extraParams as $extraParam) {
$tmp[] = array($extraParam->name, $extraParam->value);
}
$point->extraParams = $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);
return array('data' => $point->getValues());
}
/**
* Категории
*/
static private function getCategories()
{
$categories = Category::model()->getAll(array('order' => 'ord DESC', 'asArray' => true, 'noKeys' => true));
$categories[] = array(
"id" => "0",
"name" => "Все точки",
"icon" => "/ico/all.png",
"ord" => "0"
);
$categories = array_reverse($categories);
return array('data' => $categories);
}
}