From 61cabf756e6d12d022a9eb95edf34f92d4371d04 Mon Sep 17 00:00:00 2001 From: Krivchikov Dmitry Date: Mon, 2 May 2016 13:38:08 +0300 Subject: [PATCH] =?UTF-8?q?ADD=20=D1=81=D0=BE=D0=BA=D1=80=D0=B0=D1=89?= =?UTF-8?q?=D0=B0=D0=BB=D0=BA=D0=B0=20=D1=81=D1=81=D1=8B=D0=BB=D0=BE=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.php | 7 +++++ classes/x62.php | 47 +++++++++++++++++++++++++++++++++ controllers/IndexController.php | 28 ++++++++++++++++++++ controllers/SController.php | 16 +++++++++++ models/ShortUrl.php | 24 +++++++++++++++++ public/js/map.js | 27 +++++++++++++++++++ views/indexTemplate.php | 4 +++ 7 files changed, 153 insertions(+) create mode 100644 classes/x62.php create mode 100644 controllers/SController.php create mode 100644 models/ShortUrl.php diff --git a/app.php b/app.php index a93cd52..0011814 100644 --- a/app.php +++ b/app.php @@ -57,6 +57,13 @@ class App } else if (isset (self::$url[2])) { $controller = self::$controller; $action = self::$action; + + // Костылёк для сокрощалки ссылок + if ($controller == 'SController') { + self::$urlParams['key'] = self::$url[2]; + self::$action = $action = 'actionIndex'; + } + if (!method_exists($controller, $action)) { self::$urlParams['id'] = (int)self::$url[2]; self::$action = 'actionIndex'; diff --git a/classes/x62.php b/classes/x62.php new file mode 100644 index 0000000..0c82612 --- /dev/null +++ b/classes/x62.php @@ -0,0 +1,47 @@ += 0) { + if ($in >= $len) { + $tmp = floor($in / $len); + $out .= self::$ABC[(int) ($in - $tmp * $len)]; + $in = $tmp; + } else { + $out .= self::$ABC[(int) $in]; + $in = -1; + } + } + + return strrev($out); + } + + public static function decode($in) + { + $out = 0; + $exp = 0; + for ($i = strlen($in) - 1; $i >= 0; $i--) { + $n = strpos(self::$ABC, $in[$i]); + if ($n === false) { + return false; //Хрень какую-то прислали + } + $out += $n * pow(62, $exp); + $exp++; + } + return $out; + } + +} diff --git a/controllers/IndexController.php b/controllers/IndexController.php index 9a1213d..9e4a596 100644 --- a/controllers/IndexController.php +++ b/controllers/IndexController.php @@ -192,4 +192,32 @@ class IndexController extends Controller )); } + function actionShorturlroute() + { + if (!isset($_GET['elements']) || empty($_GET['elements'])) { + App::error404(); + } + + $elements = (array)$_GET['elements']; + + $url = '/?route=['; + foreach ($elements AS $key => $element) { + if ($key != 0) { + $url .= ','; + } + + if (isset($element['id'])) { + $url .= '"'.$element['id'].'"'; + } + + if (isset($element['latLng'])) { + $url .= '"['.$element['latLng']['lat'].','.$element['latLng']['lng'].']"'; + } + } + $url .= ']'; + + $shortUrl = ShortUrl::addNew('https://wikipoints.ru'.$url); + + print 'https://wikipoints.ru/s/'.x62::encode($shortUrl->id); + } } diff --git a/controllers/SController.php b/controllers/SController.php new file mode 100644 index 0000000..d8e9253 --- /dev/null +++ b/controllers/SController.php @@ -0,0 +1,16 @@ +getByPK($key); + + if($shortUrl->id) { + App::redirect($shortUrl->url); + } else { + App::error404(); + } + } +} diff --git a/models/ShortUrl.php b/models/ShortUrl.php new file mode 100644 index 0000000..d3ae07b --- /dev/null +++ b/models/ShortUrl.php @@ -0,0 +1,24 @@ +_tableName_ = 'shortUrl'; + parent::__construct($fromArray); + } + + static function model() + { + return new self(); + } + + static function addNew($url) + { + $tmp = new ShortUrl(array('url' => $url)); + + + return $tmp->save(); + } +} diff --git a/public/js/map.js b/public/js/map.js index 04bf9e5..c1b19e1 100644 --- a/public/js/map.js +++ b/public/js/map.js @@ -523,3 +523,30 @@ window.addEventListener('load', function(){ }, false); }, false); + +function getShortLink() +{ + elements = Array(); + $('#wayPoints div').each(function(i, el) + { + if ($(el).attr('pointId').length <= 10) { + elements.push({id: $(el).attr('pointId')}); + } else { + coords = JSON.parse($(el).attr('pointId')); + lat = coords[0]; + lng = coords[1]; + elements.push({latLng: {lat: lat, lng: lng}}); + } + + }); + + $.ajax({ + url: "/index/shorturlroute/", + data: { + elements: elements + }, + success: function (data) { + $('#shortLink').val(data).select(); + } + }); +} diff --git a/views/indexTemplate.php b/views/indexTemplate.php index d106aba..37b3c5f 100644 --- a/views/indexTemplate.php +++ b/views/indexTemplate.php @@ -69,6 +69,10 @@ if (App::$user) {
+
+ Получить короткую ссылку:
+ +