]> dev.renevier.net Git - syj.git/blob - application/controllers/IdxController.php
add mapquest layer
[syj.git] / application / controllers / IdxController.php
1 <?php
2 /*  This file is part of Syj, Copyright (c) 2010-2011 Arnaud Renevier,
3     and is published under the AGPL license. */
4
5 class IdxController extends Zend_Controller_Action
6 {
7
8     public function init() {
9         $this->_helper->SyjMedias->addScripts('idx');
10         $this->view->headLink()->appendStylesheet('css/olstyle.css', "all");
11         $this->view->headLink()->appendStylesheet('css/generic.css', "all");
12         $this->view->headLink()->appendStylesheet('css/syj.css', "all");
13         $this->view->headLink()->appendStylesheet('css/syjprint.css', "print");
14         $this->view->headMeta()->appendName('viewport', 'width=640px');
15     }
16
17     public function rawmode(Syj_Model_Path $path) {
18         $this->_helper->SyjReset->resetPlaceHolders();
19         $this->_helper->SyjMedias->addScripts('rawidx');
20
21         $this->view->headLink()->appendStylesheet('css/generic.css', "screen");
22         $this->view->headLink()->appendStylesheet('css/syjraw.css', "screen");
23         $this->view->headLink()->appendStylesheet('css/syjprint.css', "print");
24         $this->view->headTitle($path->displayTitle);
25
26         $this->_jsRawLocaleStrings();
27         if ($path->creator) {
28             $this->view->jslocales['geomAttribution'] =
29                         $this->view->translate('route by <strong>%s</strong>', (string)$path->creator->pseudo);
30         }
31         $jsgeom = new phptojs\JsObject('gInitialGeom', array('data' => (string) $path->geom));
32         $this->view->headScript()->prependScript((string) $jsgeom);
33
34         $this->view->rawmode = true;
35         $this->_helper->ViewRenderer->setViewScriptPathSpec(':controller/raw.:suffix');
36     }
37
38     public function indexAction() {
39         $this->_initForms();
40
41         $url = $this->getRequest()->getUserParam('url');
42         if (isset($url)) {
43             $this->view->geomform->setAction("");
44             $pathMapper = new Syj_Model_PathMapper();
45             $path = new Syj_Model_Path();
46             if (!$pathMapper->findByUrl($url, $path)) {
47                 if (is_numeric($url) and $pathMapper->hasexisted($url)) {
48                     $this->view->message = $this->view->translate("route has been deleted");
49                     throw new Syj_Exception_NotFound('Gone', 410);
50                 } else {
51                     $this->view->message = $this->view->translate("route does not exist");
52                     throw new Syj_Exception_NotFound('Not Found', 404);
53                 }
54             }
55
56             if ($this->getRequest()->getQuery('format') == 'raw') {
57                 $this->rawmode($path);
58                 return;
59             }
60
61             $title = $path->displayTitle;
62             $this->view->path = $path;
63             $jsgeom = new phptojs\JsObject('gInitialGeom', array('data' => (string) $path->geom));
64             $this->view->headScript()->prependScript((string) $jsgeom);
65             $this->view->loginform->login_geom_id->setValue((string)$path->id);
66             $this->view->geomform->geom_title->setValue($path->title);
67         } else {
68             $this->_setInitialPos();
69             $title = "Show your journey";
70         }
71
72         $this->_jsLoggedInfo(isset($url) ? $path: null);
73         $this->_jsLocaleStrings();
74         if (isset ($url) and $path->creator) {
75             $this->view->jslocales['geomAttribution'] =
76                         $this->view->translate('route by <strong>%s</strong>', (string)$path->creator->pseudo);
77         }
78         $this->view->headTitle($title);
79         $this->view->headMeta()->appendName('description', $this->view->translate('website to share routes'));
80
81         $this->view->loggedUser = $this->_helper->SyjUserManager->current();
82     }
83
84     protected function _initForms() {
85         $this->view->geomform = new Syj_Form_Geom(array('name' => 'geomform', 'action' => 'path'));
86         $this->view->loginform = new Syj_Form_Login(array('name' => 'loginform', 'action' => 'login'));
87         $this->view->userform = new Syj_Form_User(array('name' => 'userform', 'action' => 'user'));
88         $this->view->newpwdform = new Syj_Form_Newpwd(array('name' => 'newpwdform', 'action' => 'newpwd'));
89     }
90
91     protected function _setInitialPos() {
92         $lat = $this->getRequest()->getQuery('lat');
93         $lon = $this->getRequest()->getQuery('lon');
94         $zoom = $this->getRequest()->getQuery('zoom');
95         if (is_numeric ($lat) and is_numeric ($lon) and is_numeric ($zoom)) {
96             $initialpos = array('lat' => (float)$lat, 'lon' => (float)$lon, 'zoom' => (int)$zoom);
97         } else {
98             $initialpos =  $this->_helper->syjGeoip($this->getRequest()->getClientIp(true));
99         }
100         $this->view->headScript()->prependScript((string) new phptojs\JsObject('gInitialPos', $initialpos));
101     }
102
103     public function errorAction() {
104         Zend_Controller_Front::getInstance()->getRequest()->setRequestUri($this->_request->getBaseUrl());
105         $this->_initForms();
106         $this->_setInitialPos();
107
108         $this->_jsLoggedInfo(null);
109         $this->_jsLocaleStrings();
110
111         $this->view->headTitle("Show your journey");
112         $this->view->headMeta()->appendName('description', $this->view->translate('website to share routes'));
113         $this->view->loggedUser = $this->_helper->SyjUserManager->current();
114         $this->_helper->ViewRenderer->setViewScriptPathSpec(':controller/index.:suffix');
115
116         $error = $this->_getParam('error_handler');
117         if ($error) {
118             if ($error->exception instanceof Syj_Exception_ToolargeGeomUpload) {
119                 $maxsize = $this->_bytesToString(min($this->_strToBytes(ini_get('upload_max_filesize')),
120                                                     $this->_strToBytes(ini_get('upload_max_filesize'))));
121                 $errorMsg = __('File too large. File size must not exceed %s', $maxsize);
122             } else if ($error->exception instanceof Syj_Exception_InvalidGeomUpload) {
123                 $errorMsg = __("Invalid file");
124             }
125             $this->view->errorMsg = $errorMsg;
126         }
127     }
128
129     protected function _jsLoggedInfo(Syj_Model_Path $path = null) {
130         $loggedinfo = new phptojs\JsObject('gLoggedInfo', array('connections' => 0));
131
132         $user = $this->_helper->SyjUserManager->current();
133         if ($user) {
134             $loggedinfo->logged = true;
135         } else {
136             $loggedinfo->logged = false;
137         }
138
139         if (isset($path)) {
140             $loggedinfo->iscreator = $path->isCreator($user);
141             $loggedinfo->pathid = (string)$path->id;
142         } else {
143             $loggedinfo->iscreator = true;
144         }
145
146         $this->view->headScript()->prependScript((string) $loggedinfo);
147     }
148
149     protected function _jsRawLocaleStrings() {
150         $this->view->jslocales = array(
151             'osmAttribution' => __("Map by <a href='http://openstreetmap.org/'>OpenStreetMap</a>"),
152             'mapquestAttribution' => __("Map by MapQuest, <a href='http://www.openstreetmap.org/'>OpenStreetMap</a> and contributors, <a href='http://creativecommons.org/licenses/by-sa/2.0/'>CC-BY-SA</a>"),
153             );
154     }
155
156     protected function _jsLocaleStrings() {
157         $this->view->jslocales = array(
158             'saveSuccess' => __("save took place successfully"),
159             'requestError' => __("server did not understood request. That's probably caused by a bug in SYJ"),
160             'gonePathError' => __("route not referenced on the server. It has probably been deleted."),
161             'notReachedError' => __("server could not be reached"),
162             'serverError' => __("there was a server error"),
163             'unknownError' => __("there was an unknown error"),
164             'userEmptyWarn' => __("you must enter a login name"),
165             'loginSuccess' => __("Login correct"),
166             'loginFailure' => __("Wrong login/password"),
167             'passwordEmptyWarn' => __("you must enter a password"),
168             'passwordNoMatchWarn' => __("Password do not match"),
169             'acceptTermsofuseWarn' => __("You must accept terms of use"),
170             'emailEmptyWarn' => __("you must enter an email"),
171             'emailInvalidWarn' => __("invalid email"),
172             'uniqueEmailError' => __("an user is already registered with this email"),
173             'userSuccess' => __("Account created"),
174             'newpwdSuccess' => __("A link to reset your password has been emailed to you"),
175             'canResubmit' => __("Now, you can retry to save"),
176             'routeBy' => __("route by"),
177             'osmAttribution' => __("Map by <a href='http://openstreetmap.org/'>OpenStreetMap</a>"),
178             'mapquestAttribution' => __("Map by MapQuest, <a href='http://www.openstreetmap.org/'>OpenStreetMap</a> and contributors, <a href='http://creativecommons.org/licenses/by-sa/2.0/'>CC-BY-SA</a>"),
179             'pseudoChecking' => __("checking availibilty"),
180             'availablePseudo' => __("available pseudo"),
181             'unavailablePseudo' => __("unavailable pseudo"),
182             'editAction' => __("edit"),
183             'createAction' => __("create"),
184             'cloneAction' => __("duplicate"),
185             'unsavedConfirmExit' => __("You have an unsaved route"),
186             'notEmptyField' => __("Value is required"),
187             'noResult' => __("no result"),
188             'uploadFileError' => __("could not analyze file content"),
189             );
190     }
191
192     private function _strToBytes($value) {
193         $value = trim($value);
194         if (is_numeric($value)) {
195             return (integer) $value;
196         }
197         $last = strtolower($value[strlen($value)-1]);
198         $value = (int)$value;
199         switch ($last) {
200             case 'k' :
201                 $value *= 1024;
202             break;
203             case 'm' :
204                 $value *= 1024 * 1024;
205             break;
206             case 'g' :
207                 $value *= 1024 * 1024 * 1024;
208             break;
209             default :
210             break;
211         }
212         return $value;
213     }
214
215     private function _bytesToString($size) {
216         $sizes = array('B', 'kB', 'MB', 'GB');
217         for ($c=0; $c < (count ($sizes) - 1) && $size >= 1024; $c++) {
218             $size = $size / 1024;
219         }
220         return round($size) . $sizes[$c];
221     }
222
223 }