]> dev.renevier.net Git - syj.git/blob - application/controllers/IdxController.php
fixes: some error were not handled correctly when uploading path as file
[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     }
14
15     public function rawmode(Syj_Model_Path $path) {
16         $this->_helper->SyjReset->resetPlaceHolders();
17         $this->_helper->SyjMedias->addScripts('rawidx');
18
19         $this->view->headLink()->appendStylesheet('css/generic.css', 'all');
20         $this->view->headLink()->appendStylesheet('css/syjraw.css', 'all');
21         $this->view->headTitle($path->displayTitle);
22
23         $this->_jsRawLocaleStrings();
24         if ($path->creator) {
25             $this->view->jslocales['geomAttribution'] =
26                         $this->view->translate('route by <strong>%s</strong>', (string)$path->creator->pseudo);
27         }
28         $jsgeom = new phptojs\JsObject('gInitialGeom', array('data' => (string) $path->geom));
29         $this->view->headScript()->prependScript((string) $jsgeom);
30
31         $this->view->rawmode = true;
32         $this->_helper->ViewRenderer->setViewScriptPathSpec(':controller/raw.:suffix');
33     }
34
35     public function indexAction() {
36         $this->_initForms();
37
38         $url = $this->getRequest()->getUserParam('url');
39         if (isset($url)) {
40             $this->view->geomform->setAction("");
41             $pathMapper = new Syj_Model_PathMapper();
42             $path = new Syj_Model_Path();
43             if (!$pathMapper->findByUrl($url, $path)) {
44                 if (is_numeric($url) and $pathMapper->hasexisted($url)) {
45                     $this->view->message = $this->view->translate("route has been deleted");
46                     throw new Syj_Exception_NotFound('Gone', 410);
47                 } else {
48                     $this->view->message = $this->view->translate("route does not exist");
49                     throw new Syj_Exception_NotFound('Not Found', 404);
50                 }
51             }
52
53             if ($this->getRequest()->getQuery('format') == 'raw') {
54                 $this->rawmode($path);
55                 return;
56             }
57
58             $title = $path->displayTitle;
59             $this->view->path = $path;
60             $jsgeom = new phptojs\JsObject('gInitialGeom', array('data' => (string) $path->geom));
61             $this->view->headScript()->prependScript((string) $jsgeom);
62             $this->view->loginform->login_geom_id->setValue((string)$path->id);
63             $this->view->geomform->geom_title->setValue($path->title);
64         } else {
65             $this->_setInitialPos();
66             $title = "Show your journey";
67         }
68
69         $this->_jsLoggedInfo(isset($url) ? $path: null);
70         $this->_jsLocaleStrings();
71         if (isset ($url) and $path->creator) {
72             $this->view->jslocales['geomAttribution'] =
73                         $this->view->translate('route by <strong>%s</strong>', (string)$path->creator->pseudo);
74         }
75         $this->view->headTitle($title);
76         $this->view->headMeta()->appendName('description', $this->view->translate('website to share routes'));
77
78         $this->view->loggedUser = $this->_helper->SyjUserManager->current();
79     }
80
81     protected function _initForms() {
82         $this->view->geomform = new Syj_Form_Geom(array('name' => 'geomform', 'action' => 'path'));
83         $this->view->loginform = new Syj_Form_Login(array('name' => 'loginform', 'action' => 'login'));
84         $this->view->userform = new Syj_Form_User(array('name' => 'userform', 'action' => 'user'));
85         $this->view->newpwdform = new Syj_Form_Newpwd(array('name' => 'newpwdform', 'action' => 'newpwd'));
86     }
87
88     protected function _setInitialPos() {
89         $lat = $this->getRequest()->getQuery('lat');
90         $lon = $this->getRequest()->getQuery('lon');
91         $zoom = $this->getRequest()->getQuery('zoom');
92         if (is_numeric ($lat) and is_numeric ($lon) and is_numeric ($zoom)) {
93             $initialpos = array('lat' => (float)$lat, 'lon' => (float)$lon, 'zoom' => (int)$zoom);
94         } else {
95             $initialpos =  $this->_helper->syjGeoip($this->getRequest()->getClientIp(true));
96         }
97         $this->view->headScript()->prependScript((string) new phptojs\JsObject('gInitialPos', $initialpos));
98     }
99
100     public function errorAction() {
101         Zend_Controller_Front::getInstance()->getRequest()->setRequestUri($this->_request->getBaseUrl());
102         $this->_initForms();
103         $this->_setInitialPos();
104
105         $this->_jsLoggedInfo(null);
106         $this->_jsLocaleStrings();
107
108         $this->view->headTitle("Show your journey");
109         $this->view->headMeta()->appendName('description', $this->view->translate('website to share routes'));
110         $this->view->loggedUser = $this->_helper->SyjUserManager->current();
111         $this->_helper->ViewRenderer->setViewScriptPathSpec(':controller/index.:suffix');
112
113         $error = $this->_getParam('error_handler');
114         if ($error) {
115             if ($error->exception instanceof Syj_Exception_ToolargeGeomUpload) {
116                 $maxsize = $this->_bytesToString(min($this->_strToBytes(ini_get('upload_max_filesize')),
117                                                     $this->_strToBytes(ini_get('upload_max_filesize'))));
118                 $errorMsg = __('File too large. File size must not exceed %s', $maxsize);
119             } else if ($error->exception instanceof Syj_Exception_InvalidGeomUpload) {
120                 if ($error->exception->getMessage() === 'uniquepath') {
121                     $errorMsg = __("similar path seems to already exist. Please do not create two exactly identical paths");
122                 } else {
123                     $errorMsg = __("Invalid file");
124                 }
125             }
126             $this->view->errorMsg = $errorMsg;
127         }
128     }
129
130     protected function _jsLoggedInfo(Syj_Model_Path $path = null) {
131         $loggedinfo = new phptojs\JsObject('gLoggedInfo', array('connections' => 0));
132
133         $user = $this->_helper->SyjUserManager->current();
134         if ($user) {
135             $loggedinfo->logged = true;
136         } else {
137             $loggedinfo->logged = false;
138         }
139
140         if (isset($path)) {
141             $loggedinfo->iscreator = $path->isCreator($user);
142             $loggedinfo->pathid = (string)$path->id;
143         } else {
144             $loggedinfo->iscreator = true;
145         }
146
147         $this->view->headScript()->prependScript((string) $loggedinfo);
148     }
149
150     protected function _jsRawLocaleStrings() {
151         $this->view->jslocales = array(
152             'osmAttribution' => __("Map by <a href='http://openstreetmap.org/'>OpenStreetMap</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             'uniquePathError' => __("similar path seems to already exist. Please do not create two exactly identical paths"),
162             'notReachedError' => __("server could not be reached"),
163             'serverError' => __("there was a server error"),
164             'unknownError' => __("there was an unknown error"),
165             'userEmptyWarn' => __("you must enter a login name"),
166             'loginSuccess' => __("Login correct"),
167             'loginFailure' => __("Wrong login/password"),
168             'passwordEmptyWarn' => __("you must enter a password"),
169             'passwordNoMatchWarn' => __("Password do not match"),
170             'acceptTermsofuseWarn' => __("You must accept terms of use"),
171             'emailEmptyWarn' => __("you must enter an email"),
172             'emailInvalidWarn' => __("invalid email"),
173             'uniqueEmailError' => __("an user is already registered with this email"),
174             'userSuccess' => __("Account created"),
175             'newpwdSuccess' => __("A link to reset your password has been emailed to you"),
176             'canResubmit' => __("Now, you can retry to save"),
177             'routeBy' => __("route by"),
178             'osmAttribution' => __("Map by <a href='http://openstreetmap.org/'>OpenStreetMap</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 }