xref: /dokuwiki/inc/Action/Exception/ActionException.php (revision d4f83172d9533c4d84f450fe22ef630816b21d75)
164ab5140SAndreas Gohr<?php
264ab5140SAndreas Gohr
364ab5140SAndreas Gohrnamespace dokuwiki\Action\Exception;
464ab5140SAndreas Gohr
5e8aa6739SAndreas Gohr/**
6e8aa6739SAndreas Gohr * Class ActionException
7e8aa6739SAndreas Gohr *
8e8aa6739SAndreas Gohr * This exception and its subclasses signal that the current action should be
9e8aa6739SAndreas Gohr * aborted and a different action should be used instead. The new action can
10e8aa6739SAndreas Gohr * be given as parameter in the constructor. Defaults to 'show'
11e8aa6739SAndreas Gohr *
12e8aa6739SAndreas Gohr * The message will NOT be shown to the enduser
13e8aa6739SAndreas Gohr *
14e8aa6739SAndreas Gohr * @package dokuwiki\Action\Exception
15e8aa6739SAndreas Gohr */
168c7c53b0SAndreas Gohrclass ActionException extends \Exception
178c7c53b0SAndreas Gohr{
1881f9e22bSAndreas Gohr    /** @var string the new action */
1964ab5140SAndreas Gohr    protected $newaction;
2064ab5140SAndreas Gohr
2181f9e22bSAndreas Gohr    /** @var bool should the exception's message be shown to the user? */
2281f9e22bSAndreas Gohr    protected $displayToUser = false;
2381f9e22bSAndreas Gohr
24e8aa6739SAndreas Gohr    /**
25e8aa6739SAndreas Gohr     * ActionException constructor.
26e8aa6739SAndreas Gohr     *
2758528803SAndreas Gohr     * When no new action is given 'show' is assumed. For requests that originated in a POST,
2858528803SAndreas Gohr     * a 'redirect' is used which will cause a redirect to the 'show' action.
2958528803SAndreas Gohr     *
3058528803SAndreas Gohr     * @param string|null $newaction the action that should be used next
31e8aa6739SAndreas Gohr     * @param string $message optional message, will not be shown except for some dub classes
32e8aa6739SAndreas Gohr     */
33*d868eb89SAndreas Gohr    public function __construct($newaction = null, $message = '')
34*d868eb89SAndreas Gohr    {
3558528803SAndreas Gohr        global $INPUT;
3664ab5140SAndreas Gohr        parent::__construct($message);
3758528803SAndreas Gohr        if (is_null($newaction)) {
3858528803SAndreas Gohr            if (strtolower($INPUT->server->str('REQUEST_METHOD')) == 'post') {
3958528803SAndreas Gohr                $newaction = 'redirect';
4058528803SAndreas Gohr            } else {
4158528803SAndreas Gohr                $newaction = 'show';
4258528803SAndreas Gohr            }
4358528803SAndreas Gohr        }
4458528803SAndreas Gohr
4564ab5140SAndreas Gohr        $this->newaction = $newaction;
4664ab5140SAndreas Gohr    }
4764ab5140SAndreas Gohr
48e8aa6739SAndreas Gohr    /**
49e8aa6739SAndreas Gohr     * Returns the action to use next
50e8aa6739SAndreas Gohr     *
51e8aa6739SAndreas Gohr     * @return string
52e8aa6739SAndreas Gohr     */
53*d868eb89SAndreas Gohr    public function getNewAction()
54*d868eb89SAndreas Gohr    {
5564ab5140SAndreas Gohr        return $this->newaction;
5664ab5140SAndreas Gohr    }
5781f9e22bSAndreas Gohr
5881f9e22bSAndreas Gohr    /**
5981f9e22bSAndreas Gohr     * Should this Exception's message be shown to the user?
6081f9e22bSAndreas Gohr     *
6181f9e22bSAndreas Gohr     * @param null|bool $set when null is given, the current setting is not changed
6281f9e22bSAndreas Gohr     * @return bool
6381f9e22bSAndreas Gohr     */
64*d868eb89SAndreas Gohr    public function displayToUser($set = null)
65*d868eb89SAndreas Gohr    {
6681f9e22bSAndreas Gohr        if (!is_null($set)) $this->displayToUser = $set;
6781f9e22bSAndreas Gohr        return $set;
6881f9e22bSAndreas Gohr    }
6964ab5140SAndreas Gohr}
70