xref: /dokuwiki/inc/Action/Exception/ActionException.php (revision 177d6836e2f75d0e404be9c566e61725852a1e07)
1<?php
2
3namespace dokuwiki\Action\Exception;
4
5/**
6 * Class ActionException
7 *
8 * This exception and its subclasses signal that the current action should be
9 * aborted and a different action should be used instead. The new action can
10 * be given as parameter in the constructor. Defaults to 'show'
11 *
12 * The message will NOT be shown to the enduser
13 *
14 * @package dokuwiki\Action\Exception
15 */
16class ActionException extends \Exception
17{
18
19    /** @var string the new action */
20    protected $newaction;
21
22    /** @var bool should the exception's message be shown to the user? */
23    protected $displayToUser = false;
24
25    /**
26     * ActionException constructor.
27     *
28     * When no new action is given 'show' is assumed. For requests that originated in a POST,
29     * a 'redirect' is used which will cause a redirect to the 'show' action.
30     *
31     * @param string|null $newaction the action that should be used next
32     * @param string $message optional message, will not be shown except for some dub classes
33     */
34    public function __construct($newaction = null, $message = '')
35    {
36        global $INPUT;
37        parent::__construct($message);
38        if (is_null($newaction)) {
39            if (strtolower($INPUT->server->str('REQUEST_METHOD')) == 'post') {
40                $newaction = 'redirect';
41            } else {
42                $newaction = 'show';
43            }
44        }
45
46        $this->newaction = $newaction;
47    }
48
49    /**
50     * Returns the action to use next
51     *
52     * @return string
53     */
54    public function getNewAction()
55    {
56        return $this->newaction;
57    }
58
59    /**
60     * Should this Exception's message be shown to the user?
61     *
62     * @param null|bool $set when null is given, the current setting is not changed
63     * @return bool
64     */
65    public function displayToUser($set = null)
66    {
67        if (!is_null($set)) $this->displayToUser = $set;
68        return $set;
69    }
70}
71