From 1dc15df6eb47bb1497d94902bf15dde0d38a0cd5 Mon Sep 17 00:00:00 2001 From: Krivchikov Dmitry Date: Sun, 3 Aug 2014 20:49:26 +0400 Subject: [PATCH] =?UTF-8?q?=D0=92=D1=80=D0=BE=D0=B4=D0=B5=20=D0=B7=D0=B0?= =?UTF-8?q?=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=D0=BB=D0=BE=20=D1=82=D0=B0?= =?UTF-8?q?=D0=BA,=20=D0=BA=D0=B0=D0=BA=20=D0=BD=D0=B0=D0=B4=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 20 ++ ground/app.php | 15 +- ground/classes/Image.php | 29 +-- ground/controllers/JsonController.php | 301 ++++++++++++++++---------- ground/controllers/PageController.php | 8 +- ground/models/Photo.php | 17 ++ ground/models/Point.php | 1 + ground/views/modals/addPoint.php | 40 ++-- ground/views/point.php | 14 +- public/apitest.php | 51 +++++ public/css/style.css | 7 +- public/ico/description | 22 -- public/js/map.js | 44 +++- public/photos/.gitkeep | 0 public/photos/big/.gitkeep | 0 public/photos/middle/.gitkeep | 0 public/photos/small/.gitkeep | 0 17 files changed, 386 insertions(+), 183 deletions(-) create mode 100644 ground/models/Photo.php create mode 100644 public/apitest.php delete mode 100644 public/ico/description create mode 100644 public/photos/.gitkeep create mode 100644 public/photos/big/.gitkeep create mode 100644 public/photos/middle/.gitkeep create mode 100644 public/photos/small/.gitkeep diff --git a/.gitignore b/.gitignore index 6e7e7f4..d5e8674 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,21 @@ ground/config.php + +public/photos/*.jpg +public/photos/*.jpeg +public/photos/*.png +public/photos/*.gif + +public/photos/small/*.jpg +public/photos/small/*.jpeg +public/photos/small/*.png +public/photos/small/*.gif + +public/photos/middle/*.jpg +public/photos/middle/*.jpeg +public/photos/middle/*.png +public/photos/middle/*.gif + +public/photos/big/*.jpg +public/photos/big/*.jpeg +public/photos/big/*.png +public/photos/big/*.gif diff --git a/ground/app.php b/ground/app.php index b37d5b8..5ba595b 100644 --- a/ground/app.php +++ b/ground/app.php @@ -30,7 +30,8 @@ class App private static $config = array(); private static $urlParams = array(); private static $DB = null; - public static $user = null; + public static $user = null; + public static $request = array(); /** * Разбивает урл на контроллер, метод и выбирает параметры. @@ -70,6 +71,8 @@ class App $controller = self::$controller; $action = self::$action; + self::fillRequest(); + if (method_exists($controller, $action)) { $controller::$action(); @@ -139,6 +142,7 @@ class App public static function redirect($url, $httpCode = 307) { header('Location: ' . $url, true, $httpCode); + die; } /** @@ -194,4 +198,13 @@ class App return self::$urlParams; } + private static function fillRequest() + { + self::$request = array( + 'method' => isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : '', + 'ajax' => isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest', + 'refferer' => isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '', + ); + } + } diff --git a/ground/classes/Image.php b/ground/classes/Image.php index f406d91..aacf0a8 100644 --- a/ground/classes/Image.php +++ b/ground/classes/Image.php @@ -28,26 +28,27 @@ class Image } /** - * Ресайзит изображение. На входе запрос вида sizeX_sizeY_imgName.ext - * @param type $name + * Создаёт все требуемые размеры изображения и раскладывает их по папкам. + * @param type $imgName */ - public static function resize($imgName, $sizeX, $sizeY = false, $quality = 85) + public static function createAllSizes($imgName) { - $dir = 'photos/'; - $newDir = 'photos/resize/'; + $sizes = App::getConfig('images'); + $pathInfo = pathinfo($_SERVER['SCRIPT_FILENAME']); + $uploaddir = $pathInfo['dirname'] . '/photos/'; - $sizeY = $sizeY ? $sizeY : $sizeX; - - - $oldImgName = $dir . $imgName; - $newImgName = $newDir . $sizeX . '_' . $sizeY . '_' . $imgName; - - if (file_exists($newImgName) || self::imgResize($oldImgName, $newImgName, $sizeX, $sizeY, $quality)) + foreach ($sizes as $dir => $size) { - return '/' . $newImgName; + self::imgResize( + $uploaddir . $imgName, // Source - original image + $uploaddir . $dir . '/' . $imgName, // Destination + $size[0], // X + $size[1], // Y + $size[2] // Quelity + ); } - return '/' . $oldImgName; + return TRUE; // o_O } /** diff --git a/ground/controllers/JsonController.php b/ground/controllers/JsonController.php index 7f54da5..9ea3af1 100644 --- a/ground/controllers/JsonController.php +++ b/ground/controllers/JsonController.php @@ -3,34 +3,35 @@ class JsonController extends Controller { - /** - * Точки - */ - static function actionPoints() - { - $points = Point::model()->getAll(); - $categories = Category::model()->getAll(array('asArray'=>true)); + /** + * Точки + */ + static function actionPoints() + { + $points = Point::model()->getAll(); + $categories = Category::model()->getAll(array('asArray' => true)); $result = array(); - foreach ($points as $point) { + 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']; - } + $result[$id]['categoryId'] = $point->categoryId; + $result[$id]['categoryIcon'] = $categories[$point->categoryId]['icon']; + } - self::renderPartial('json.php', array( + self::renderPartial('json.php', array( 'data' => $result, )); - } + } - /** - * Точка - */ + /** + * Точка + */ static function actionPoint() { $id = (int) App::getParam('id'); @@ -38,13 +39,31 @@ class JsonController extends Controller { $point = Point::model()->getByPK($id); - $point->descriptionHtml = nl2br($point->description); + $point->descriptionHtml = nl2br($point->description); $point->name = htmlspecialchars($point->name); + $imgs = $point->photos; - $categories = Category::model()->getAll(array('asArray'=>true)); + if ($point->img == '' && !empty($imgs)) + { + $img = array_shift($imgs); + $point->img = '/photos/small/' . $img->name; + } - $point->categoryIcon = $categories[$point->categoryId]['icon']; - $point->categoryName = $categories[$point->categoryId]['name']; + $imgs = $point->photos; + if (!empty($imgs)) + { + $tmp = array(); + foreach ($imgs as $img) + { + $tmp[] = $img->name; + } + $point->images = $tmp; + } + + $categories = Category::model()->getAll(array('asArray' => true)); + + $point->categoryIcon = $categories[$point->categoryId]['icon']; + $point->categoryName = $categories[$point->categoryId]['name']; self::renderPartial('json.php', $point->getValues()); } else @@ -53,114 +72,167 @@ class JsonController extends Controller } } - /** - * Категории - */ - static function actionCategories() - { - $categories = Category::model()->getAll(array('asArray'=>true)); + /** + * Категории + */ + static function actionCategories() + { + $categories = Category::model()->getAll(array('asArray' => true)); - self::renderPartial('json.php', array( + self::renderPartial('json.php', array( 'data' => $categories, )); - } - - /** - * Добавление точки - */ - static function actionAddPoint() - { - $photos = array(); - $temp = pathinfo($_SERVER['SCRIPT_FILENAME']); - $uploaddir = $temp['dirname'] . '/photos/'; -// print_r($_FILES); -// print_r($_REQUEST); -// die; - if (isset($_FILES) && !empty($_FILES)) - { - foreach ($_FILES['photos']['name'] as $key => $fileName) - { - $temp = pathinfo($_FILES['photos']['name'][$key]); - - $foto = strtolower('_' . substr(md5($fileName . time()), 0, 5) . '.' . $temp['extension']); - if (move_uploaded_file($_FILES['photos']['tmp_name'][$key], $uploaddir . $foto)) - { - $photos[] = $foto; - } - } - } - print_r($photos); - die; + } + /** + * Добавление точки + */ + 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 (!App::$user) + $errors[8] = 'Вы не авторизованы! Пожалуйста, войдите.'; + if (isset($_POST['id']) && $_POST['id'] && App::$user->isAdmin == 0) + $errors[9] = 'Редактирование точек достурно только администратору!'; - if (empty($errors)) - { - $point = new Point(); + $photos = $images = array(); + $ordering = $_POST['ordering']; + 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))) + { + $images[] = $photo; + } + } else + { + $images[] = $img; + } + } + } + + $resPhotos = array(); + 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); + } + } + + 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 = $_POST['id']; + // Редактирование существующей точки + $id = (int) $_POST['id']; $point = $point->getByPK($id); $point->setValues($_POST); $point->save(); + } else + { + $_POST['author'] = App::$user->id; + $_POST['date'] = time(); + $point->add($_POST); - self::renderPartial('json.php', array( - 'data' => $point->id - )); + if (!$point->id) + { + header("HTTP/1.0 500 Internal Server Error"); + die('Error 500 - Internal Server Error'); + } - die; + 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, + )); + curl_exec($ch); + curl_close($ch); + } } - $_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); - } - } + self::renderPartial('json.php', array( + 'data' => $point->id + )); + } + } static function actionSaveRoute() { @@ -183,7 +255,7 @@ class JsonController extends Controller if ($id) { $route = new Route(); - $route = $route->getByPK($id); + $route = $route->getByPK($id); self::renderPartial('json.php', $route->points); } else { @@ -197,7 +269,7 @@ class JsonController extends Controller if ($id && App::$user) { $route = new Route(); - $route = $route->getByPK($id); + $route = $route->getByPK($id); if ($route->author == App::$user->id) { @@ -215,11 +287,11 @@ class JsonController extends Controller { $address = isset($_GET['search']) ? $_GET['search'] : ''; - $res = @file_get_contents('http://geocode-maps.yandex.ru/1.x/?format=json&geocode='.urlencode($address)); + $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']); + 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); @@ -229,4 +301,5 @@ class JsonController extends Controller self::renderPartial('json.php', $coords); } + } diff --git a/ground/controllers/PageController.php b/ground/controllers/PageController.php index 6907926..1224b92 100644 --- a/ground/controllers/PageController.php +++ b/ground/controllers/PageController.php @@ -65,14 +65,14 @@ class PageController extends Controller $categories = new Category(); $categories = $categories->getAll(); - $point->categoryIcon = $categories[$point->categoryId]['icon']; - $point->categoryName = $categories[$point->categoryId]['name']; + $point->categoryIcon = $categories[$point->categoryId]->icon; + $point->categoryName = $categories[$point->categoryId]->name; self::render('point.php', array( 'point' => $point, 'categories' => $categories, - ) - ); + 'photos' => $point->photos, + )); } else { App::error404(); diff --git a/ground/models/Photo.php b/ground/models/Photo.php new file mode 100644 index 0000000..47b09f5 --- /dev/null +++ b/ground/models/Photo.php @@ -0,0 +1,17 @@ +_tableName_ = 'photos'; + parent::__construct($fromArray); + $this->setRelation('point', 'pointId', 'Point', 'id', self::TO_ONE); + } + + static function model() + { + return new self(); + } +} + diff --git a/ground/models/Point.php b/ground/models/Point.php index 01afe63..4d97953 100644 --- a/ground/models/Point.php +++ b/ground/models/Point.php @@ -8,6 +8,7 @@ class Point extends Model parent::__construct($fromArray); $this->setRelation('category', 'categoryId', 'Category', 'id', self::TO_ONE); $this->setRelation('authorUser', 'author', 'User', 'id', self::TO_ONE); + $this->setRelation('photos', 'id', 'Photo', 'pointId', self::TO_MANY); } static function model() diff --git a/ground/views/modals/addPoint.php b/ground/views/modals/addPoint.php index 450127f..a94d052 100644 --- a/ground/views/modals/addPoint.php +++ b/ground/views/modals/addPoint.php @@ -20,15 +20,12 @@ +
+
@@ -46,23 +43,22 @@
Координаты: - - + +