2 /* This file is part of Syj, Copyright (c) 2010-2011 Arnaud Renevier,
3 and is published under the AGPL license. */
5 class IdxController extends Zend_Controller_Action
8 public function init() {
9 $this->_helper->SyjMedias->addScripts('idx');
10 $this->_helper->SyjMedias->addStyleSheets('idx');
11 $this->view->headMeta()->appendName('viewport', 'width=640');
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');
19 $this->view->headTitle($path->displayTitle);
21 $this->_jsRawLocaleStrings();
23 $this->view->jslocales['geomAttribution'] =
24 $this->view->translate('route by <strong>%s</strong>', (string)$path->creator->pseudo);
26 $jsgeom = new phptojs\JsObject('gInitialGeom', array('data' => (string) $path->geom));
27 $this->view->headScript()->prependScript((string) $jsgeom);
29 $this->view->rawmode = true;
30 $this->_helper->ViewRenderer->setViewScriptPathSpec(':controller/raw.:suffix');
33 public function indexAction() {
36 $url = $this->getRequest()->getUserParam('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);
46 $this->view->message = $this->view->translate("route does not exist");
47 throw new Syj_Exception_NotFound('Not Found', 404);
51 if ($this->getRequest()->getQuery('format') == 'raw') {
52 $this->rawmode($path);
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 $this->view->profileActive = $this->_hasAltiProfile($path);
64 $this->_setInitialPos();
65 $title = "Show your journey";
68 $this->_jsLoggedInfo(isset($url) ? $path: null);
69 $this->_jsLocaleStrings();
70 if (isset ($url) and $path->creator) {
71 $this->view->jslocales['geomAttribution'] =
72 $this->view->translate('route by <strong>%s</strong>', (string)$path->creator->pseudo);
74 $this->view->headTitle($title);
75 $this->view->headMeta()->appendName('description', $this->view->translate('website to share routes'));
77 $this->view->loggedUser = $this->_helper->SyjUserManager->current();
80 protected function _initForms() {
81 $this->view->geomform = new Syj_Form_Geom(array('name' => 'geomform', 'action' => 'path'));
82 $this->view->loginform = new Syj_Form_Login(array('name' => 'loginform', 'action' => 'login'));
83 $this->view->userform = new Syj_Form_User(array('name' => 'userform', 'action' => 'user'));
84 $this->view->newpwdform = new Syj_Form_Newpwd(array('name' => 'newpwdform', 'action' => 'newpwd'));
87 protected function _setInitialPos() {
88 $lat = $this->getRequest()->getQuery('lat');
89 $lon = $this->getRequest()->getQuery('lon');
90 $zoom = $this->getRequest()->getQuery('zoom');
91 if (is_numeric ($lat) and is_numeric ($lon) and is_numeric ($zoom)) {
92 $initialpos = array('lat' => (float)$lat, 'lon' => (float)$lon, 'zoom' => (int)$zoom);
94 $initialpos = $this->_helper->syjGeoip($this->getRequest()->getClientIp(true));
96 $this->view->headScript()->prependScript((string) new phptojs\JsObject('gInitialPos', $initialpos));
99 public function errorAction() {
100 Zend_Controller_Front::getInstance()->getRequest()->setRequestUri($this->_request->getBaseUrl());
102 $this->_setInitialPos();
104 $this->_jsLoggedInfo(null);
105 $this->_jsLocaleStrings();
107 $this->view->headTitle("Show your journey");
108 $this->view->headMeta()->appendName('description', $this->view->translate('website to share routes'));
109 $this->view->loggedUser = $this->_helper->SyjUserManager->current();
110 $this->_helper->ViewRenderer->setViewScriptPathSpec(':controller/index.:suffix');
112 $error = $this->_getParam('error_handler');
114 if ($error->exception instanceof Syj_Exception_ToolargeGeomUpload) {
115 $maxsize = $this->_bytesToString(min($this->_strToBytes(ini_get('upload_max_filesize')),
116 $this->_strToBytes(ini_get('upload_max_filesize'))));
117 $errorMsg = __('File too large. File size must not exceed %s', $maxsize);
118 } else if ($error->exception instanceof Syj_Exception_InvalidGeomUpload) {
119 $errorMsg = __("Invalid file");
121 $this->view->errorMsg = $errorMsg;
125 protected function _jsLoggedInfo(Syj_Model_Path $path = null) {
126 $loggedinfo = new phptojs\JsObject('gLoggedInfo', array('connections' => 0));
128 $user = $this->_helper->SyjUserManager->current();
130 $loggedinfo->logged = true;
132 $loggedinfo->logged = false;
136 $loggedinfo->iscreator = $path->isCreator($user);
137 $loggedinfo->pathid = (string)$path->id;
139 $loggedinfo->iscreator = true;
142 $this->view->headScript()->prependScript((string) $loggedinfo);
145 protected function _jsRawLocaleStrings() {
146 $this->view->jslocales = array(
147 'osmAttribution' => __("Map by <a href='http://openstreetmap.org/'>OpenStreetMap</a>"),
148 '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>"),
152 protected function _jsLocaleStrings() {
153 $this->view->jslocales = array(
154 'saveSuccess' => __("save took place successfully"),
155 'requestError' => __("server did not understood request. That's probably caused by a bug in SYJ"),
156 'gonePathError' => __("route not referenced on the server. It has probably been deleted."),
157 'notReachedError' => __("server could not be reached"),
158 'serverError' => __("there was a server error"),
159 'unknownError' => __("there was an unknown error"),
160 'userEmptyWarn' => __("you must enter a login name"),
161 'loginSuccess' => __("Login correct"),
162 'loginFailure' => __("Wrong login/password"),
163 'passwordEmptyWarn' => __("you must enter a password"),
164 'passwordNoMatchWarn' => __("Password do not match"),
165 'acceptTermsofuseWarn' => __("You must accept terms of use"),
166 'emailEmptyWarn' => __("you must enter an email"),
167 'emailInvalidWarn' => __("invalid email"),
168 'uniqueEmailError' => __("an user is already registered with this email"),
169 'userSuccess' => __("Account created"),
170 'newpwdSuccess' => __("A link to reset your password has been emailed to you"),
171 'canResubmit' => __("Now, you can retry to save"),
172 'routeBy' => __("route by"),
173 'osmAttribution' => __("Map by <a href='http://openstreetmap.org/'>OpenStreetMap</a>"),
174 '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>"),
175 'pseudoChecking' => __("checking availibilty"),
176 'availablePseudo' => __("available pseudo"),
177 'unavailablePseudo' => __("unavailable pseudo"),
178 'editAction' => __("edit"),
179 'createAction' => __("create"),
180 'cloneAction' => __("duplicate"),
181 'unsavedConfirmExit' => __("You have an unsaved route"),
182 'notEmptyField' => __("Value is required"),
183 'noResult' => __("no result"),
184 'uploadFileError' => __("could not analyze file content"),
188 private function _strToBytes($value) {
189 $value = trim($value);
190 if (is_numeric($value)) {
191 return (integer) $value;
193 $last = strtolower($value[strlen($value)-1]);
194 $value = (int)$value;
200 $value *= 1024 * 1024;
203 $value *= 1024 * 1024 * 1024;
211 private function _bytesToString($size) {
212 $sizes = array('B', 'kB', 'MB', 'GB');
213 for ($c=0; $c < (count ($sizes) - 1) && $size >= 1024; $c++) {
214 $size = $size / 1024;
216 return round($size) . $sizes[$c];
219 private function _hasAltiProfile($path) {
220 if (!extension_loaded('gd')) {
223 $cachefile = $path->getProfileCache('small');
224 if (file_exists($cachefile)) {
225 return filesize($cachefile) != 0;
229 $service = $this->_helper->SyjAltiService->service();
230 } catch(Exception $e) {
235 $path->getAltiProfile($service, 2 / 100);
237 } catch(Syj_Exception_NotImplemented $e) {
240 } catch(Exception $e) {