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