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