Action.php 11 KB
Newer Older
llaffont's avatar
llaffont committed
1 2 3 4
<?php
/**
 * Copyright (c) 2012, Agence Française Informatique (AFI). All rights reserved.
 *
Laurent's avatar
Laurent committed
5
 * BOKEH is free software; you can redistribute it and/or modify
llaffont's avatar
llaffont committed
6 7 8 9 10 11
 * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
 * the Free Software Foundation.
 *
 * There are special exceptions to the terms and conditions of the AGPL as it
 * is applied to this software (see README file).
 *
Laurent's avatar
Laurent committed
12
 * BOKEH is distributed in the hope that it will be useful,
llaffont's avatar
llaffont committed
13 14 15 16 17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
 *
 * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
Laurent's avatar
Laurent committed
18
 * along with BOKEH; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
llaffont's avatar
llaffont committed
20 21 22
 */

class ZendAfi_Controller_Action extends Zend_Controller_Action {
Patrick Barroca's avatar
Patrick Barroca committed
23
  use Trait_Translator;
24

25 26 27 28 29 30 31 32 33 34 35 36
  protected
    $_definitions,
    $_plugins;


  protected function _getActionPlugins() {
    if (isset($this->_plugins))
      return $this->_plugins;

    $plugins_helper = new ZendAfi_Controller_Action_Plugins($this);
    return $this->_plugins = $plugins_helper->getPlugins();
  }
37 38


39 40 41
  public function __call($method_name, $args) {
    if ('Action' != substr($method_name, -6))
      return parent::__call($method_name, $args);
42

43
    $plugins = $this->_getActionPlugins()
44
      ->select(function($plugin) use ($method_name)
45
               {
46
                 return $plugin->isActionDefined($method_name);
47 48 49
               });

    if($plugins->isEmpty())
50
      return parent::__call($method_name, $args);
51 52

    if(1 < $plugins->count()) {
53
      $this->_helper->notify($this->_('L\'action %s est déjà définie`', $method_name));
54 55 56
      return $this->_redirectToIndex();
    }

Patrick Barroca's avatar
Patrick Barroca committed
57
    return call_user_func_array([$plugins->first(), $method_name], $args);
58 59
  }

llaffont's avatar
llaffont committed
60

Patrick Barroca's avatar
Patrick Barroca committed
61 62 63
  public function init() {
    $this->_helper->redirector->setExit(false);
  }
64 65


Patrick Barroca's avatar
Patrick Barroca committed
66 67 68 69
  public function switchToPopupMode() {
    $this->getHelper('ViewRenderer')->beRenderPopup();
    Class_ScriptLoader::getInstance()->beAjax();
    $this->view->getHelper('Bouton')->beRenderPopup();
Laurent's avatar
Laurent committed
70
    $this->view->getHelper('formSubmit')->beRenderPopup();
Patrick Barroca's avatar
Patrick Barroca committed
71
  }
72 73


74 75 76 77 78
  public function switchToIframeMode() {
    $this->getHelper('ViewRenderer')->beRenderIframe();
  }


Patrick Barroca's avatar
Patrick Barroca committed
79 80 81
  public function isPopupRequest() {
    return ('popup' == $this->_request->getParam('render'));
  }
82 83


84 85 86 87 88
  public function isIframeRequest() {
    return ('iframe' == $this->_request->getParam('render'));
  }


Patrick Barroca's avatar
Patrick Barroca committed
89
  public function preDispatch() {
90
    $this->view->plugins = $this->_getActionPlugins();
Ghislain Loas's avatar
Ghislain Loas committed
91

Patrick Barroca's avatar
Patrick Barroca committed
92 93
    if ($this->isPopupRequest())
      $this->switchToPopupMode();
94 95 96

    if ($this->isIframeRequest())
      $this->switchToIframeMode();
Patrick Barroca's avatar
Patrick Barroca committed
97
  }
98

99

Patrick Barroca's avatar
Patrick Barroca committed
100 101 102 103 104
  protected function _redirect($url, array $options = array()) {
    if ($this->isPopupRequest() && $this->_request->isPost()) {
      $this
        ->getHelper('flashMessenger')
        ->addMessage([ZendAfi_Controller_Action_Helper_FlashMessenger::POPUP => [
105
                                                                                 'url' => $this->view->absoluteUrl($url).'/render/popup']]);
Patrick Barroca's avatar
Patrick Barroca committed
106 107 108
      $this->_javascriptRedirectToReferrer();
      return;
    }
109

Patrick Barroca's avatar
Patrick Barroca committed
110 111
    return parent::_redirect($url, $options);
  }
112 113


114
  protected function _javascriptRedirectToReferrer($message = null) {
115 116 117 118 119
    $this->_popupJavascriptRedirectTo('', $message);
  }


  protected function _popupJavascriptRedirectTo($url, $message = null) {
120 121 122
    if($message)
      $this->_helper->notify($message);

123
    $this->view->url = $url;
124
    $this->getHelper('ViewRenderer')->renderScript('ajax_return.phtml');
efalcy's avatar
efalcy committed
125
  }
126 127


128
  protected function _redirectClose($url, array $options = []) {
Patrick Barroca's avatar
Patrick Barroca committed
129 130
    if (!$this->isPopupRequest())
      return $this->_redirect($url, $options);
131

132
    $this->_popupJavascriptRedirectTo('');
Patrick Barroca's avatar
Patrick Barroca committed
133
  }
134 135


136 137 138 139 140
  public function setResourceDefinition($definitions) {
    $this->_definitions = $definitions;
  }


141 142 143 144 145
  public function getPlugins() {
    return [];
  }


Patrick Barroca's avatar
Patrick Barroca committed
146
  public function indexAction() {
147 148 149
    if ($this->_response->isRedirect())
      return;

Patrick Barroca's avatar
Patrick Barroca committed
150 151 152 153
    $var_name = $this->_definitions->pluralizeModelName();
    $this->view->$var_name = $this->_definitions->findAll($this->getRequest());
    $this->view->titre = $this->_definitions->indexActionTitle();
  }
154 155


Patrick Barroca's avatar
Patrick Barroca committed
156 157 158 159 160
  public function configAction() {
    $this->view->vars = Class_AdminVar::findAllByController($this->_request
                                                            ->getControllerName());
    $this->renderScript('index/adminvar.phtml');
  }
161 162


Patrick Barroca's avatar
Patrick Barroca committed
163
  protected function _redirectToIndex() {
164 165 166 167
    $url = sprintf('/%s/%s/index',
                   $this->_request->getModuleName(),
                   $this->_request->getControllerName());

Patrick Barroca's avatar
Patrick Barroca committed
168 169 170 171
    $closure = function($item, $value) use (&$url) {
      $url .= '/' . $item . '/' . $value;
    };

172 173 174
    if($this->_definitions)
      $this->_definitions->withScopeDo($closure, $this->_request);

Patrick Barroca's avatar
Patrick Barroca committed
175 176
    $this->_redirect($url);
  }
llaffont's avatar
llaffont committed
177 178


Patrick Barroca's avatar
Patrick Barroca committed
179 180 181 182 183 184 185
  protected function _redirectToParentProfil() {
    $profil = Class_Profil::getCurrentProfil();
    $url = $profil->hasParentProfil() ?
      '/opac/index/index/id_profil/' . $profil->getParentProfil()->getId() :
      '/';
    $this->_redirect($url);
  }
186 187


Patrick Barroca's avatar
Patrick Barroca committed
188 189 190 191 192 193
  protected function _redirectToLogoutProfil($preferences) {
    $target = (int)$preferences['profil_logout_redirect'];
    (0 < $target) ?
      $this->_redirect('/opac/index/index/id_profil/' . $target) :
      $this->_redirectToParentProfil();
  }
194 195


Patrick Barroca's avatar
Patrick Barroca committed
196 197 198
  protected function _getPost() {
    return $this->_request->getPost();
  }
199 200


Patrick Barroca's avatar
Patrick Barroca committed
201 202 203
  protected function _stayOnPage() {
    $this->_redirect($this->_request->getServer('HTTP_REFERER'));
  }
204 205


Patrick Barroca's avatar
Patrick Barroca committed
206
  public function renderPopup($popup_url, $location = null) {
207 208 209
    if ($this->isPopupRequest())
      return $this->_redirect($popup_url);

Patrick Barroca's avatar
Patrick Barroca committed
210
    $this->getHelper('flashMessenger')
211
         ->addMessage([ZendAfi_Controller_Action_Helper_FlashMessenger::POPUP => ['url' => $popup_url]]);
212

213
    parent::_redirect($location ? $location : $this->_getReferer());
Patrick Barroca's avatar
Patrick Barroca committed
214 215
    return $this;
  }
216 217


Patrick Barroca's avatar
Patrick Barroca committed
218
  public function renderPopupResult($title, $content, $attribs = []) {
219 220 221 222 223 224 225 226 227 228
    $result = array_merge($attribs,
                          ['result' => 'CONTENT',
                           'title' => $title,
                           'content' => $content]);

    if ('admin' == $this->_request->getModuleName())
      $result['position'] = ['my' => 'top',
                             'at' => 'bottom+15px',
                             'of' => '.main .barre_nav'];

229
    $this->_helper->json($result);
Patrick Barroca's avatar
Patrick Barroca committed
230
  }
gloas's avatar
gloas committed
231 232


Patrick Barroca's avatar
Patrick Barroca committed
233
  public function _redirectToReferer() {
234 235 236
    return $this->isPopupRequest()
      ? $this->_javascriptRedirectToReferrer()
      : parent::_redirect($this->_getReferer());
Patrick Barroca's avatar
Patrick Barroca committed
237
  }
gloas's avatar
gloas committed
238 239


Ghislain Loas's avatar
Ghislain Loas committed
240 241 242 243 244 245 246
  public function _redirectToUrlOrReferer($url) {
    return $this->_redirect($this->_getParam('redirect') == 'referer'
                            ? $this->_getReferer()
                            : $url);
  }


Patrick Barroca's avatar
Patrick Barroca committed
247 248 249
  public function _getReferer() {
    return $this->_request->getServer('HTTP_REFERER');
  }
250 251


Patrick Barroca's avatar
Patrick Barroca committed
252
  public function _redirectToRefererWithNewParams($params = []) {
253 254 255 256 257
    $router = new Zend_Controller_Router_Rewrite();
    $referer_params = $router
      ->route( new ZendAfi_Controller_Request_Http(Zend_Uri_Http::fromString($this->_getReferer())))
      ->getUrlParams();
    $token_params = array_merge($referer_params, $params);
Ghislain Loas's avatar
Ghislain Loas committed
258
    return $this->_redirect($router->getCurrentRoute()->assemble($token_params));
Patrick Barroca's avatar
Patrick Barroca committed
259
  }
260 261


Patrick Barroca's avatar
Patrick Barroca committed
262 263 264 265
  protected function extractProfilFromUrl() {
    $id_module = $this->_getParam('id_module');
    if (!($id_module) || 1 == count($parts = explode('-', $id_module)))
      return Class_Profil::getCurrentProfil();
266

Patrick Barroca's avatar
Patrick Barroca committed
267 268 269
    return ($profile = Class_Profil::find($parts[0]))
      ? $profile : Class_Profil::getCurrentProfil();
  }
270 271


Patrick Barroca's avatar
Patrick Barroca committed
272 273 274 275
  protected function extractIdModuleFromUrl() {
    $id_module = $this->_getParam('id_module');
    if (!$id_module || 1 ==count($parts = explode('-', $id_module)))
      return $id_module;
276

Patrick Barroca's avatar
Patrick Barroca committed
277 278
    return $parts[1];
  }
279

280

281
  public function getOsmService() {
282 283 284 285 286 287 288
    return (new Class_WebService_OpenStreetMap())
      ->onCoordinatesNotFound(
                              function() {
                                $this->_helper->notify($this->_('Pas de coordonnées (latitude, longitude) trouvées pour l\'adresse fournie. Merci de remplir manuellement'));
                              }
      );
  }
289 290 291 292 293 294 295 296


  protected function checkNotifyMessage($ressource_link, $url) {
    if ('' == $url) {
      $this->_helper->notify($ressource_link->getMessage());
      return $this->_redirect($this->_request->getServer('HTTP_REFERER'));
    }

297 298 299 300 301 302 303 304
    $this->_javascriptRedirectTo($url);
  }


  protected function _javascriptRedirectTo($url, $message = null) {
    if($message)
      $this->_helper->notify($message);

305 306 307
    $viewRenderer = $this->getHelper('ViewRenderer');
    $viewRenderer->setNoRender();
    $this->getResponse()->setHeader('Content-Type', 'text/html;charset=utf-8');
308
    $this->getResponse()->setBody('<script>document.location.href="'.$url.'";</script>');
309 310 311 312 313 314 315
  }


  protected function willRedirectToMe($link) {
    $link->afterLoginRedirectTo(Class_Url::absolute($this->view->url()));
    return $this;
  }
316 317


318 319
  public function acceptVisitor($visitor) {
    $visitor
320 321
      ->visitView($this->view)
      ->visitRequest($this->_request)
efalcy's avatar
efalcy committed
322
      ->visitResponse($this->_response)
323 324
      ->visitHelper($this->_helper)
      ->visitViewRenderer($this->getHelper('ViewRenderer'))
325 326 327 328
      ->visitGetPost(function()
                     {
                       return $this->_getPost();
                     })
Ghislain Loas's avatar
Ghislain Loas committed
329
      ->visitGetParam(function($key, $default)
330
                      {
Ghislain Loas's avatar
Ghislain Loas committed
331
                        return $this->_getParam($key, $default);
332 333 334 335 336
                      })
      ->visitSetParam(function($key, $value, $default)
                      {
                        return $this->_setParam($key, $value, $default);
                      })
337
      ->visitForward(function($action, $controller = null, $module = null, $params = [])
338
                     {
339
                       return $this->_forward($action, $controller, $module, $params);
340
                     })
341 342 343 344 345 346 347 348
      ->visitRedirectToReferer(function()
                               {
                                 $this->_redirectToReferer();
                               })
      ->visitRedirect(function($url)
                      {
                        $this->_redirect($url);
                      })
349 350 351 352 353
      ->visitRedirectClose(function($url, $options = [])
                           {
                             $this->_redirectClose($url, $options);
                           })
      ->visitRedirectToIndex(function()
354
                             {
355
                               $this->_redirectToIndex();
356
                             })
Ghislain Loas's avatar
Ghislain Loas committed
357 358 359 360
      ->visitJavascriptRedirectToReferer(function()
                               {
                                 $this->_javascriptRedirectToReferrer();
                               })
361 362 363 364
      ->visitGetReferer(function()
                        {
                          return $this->_getReferer();
                        })
365
      ;
366

367 368
    return $this;
  }
369 370 371 372 373


  public function getPluginsCollection() {
    return $this->_plugins;
  }
llaffont's avatar
llaffont committed
374
}