about summary refs log tree commit diff stats
path: root/wiki/inc/Action/Exception/ActionException.php
blob: 381584c15b483c7901ba2efd07d156c7e5d74eaf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php

namespace dokuwiki\Action\Exception;

/**
 * Class ActionException
 *
 * This exception and its subclasses signal that the current action should be
 * aborted and a different action should be used instead. The new action can
 * be given as parameter in the constructor. Defaults to 'show'
 *
 * The message will NOT be shown to the enduser
 *
 * @package dokuwiki\Action\Exception
 */
class ActionException extends \Exception {

    /** @var string the new action */
    protected $newaction;

    /** @var bool should the exception's message be shown to the user? */
    protected $displayToUser = false;

    /**
     * ActionException constructor.
     *
     * When no new action is given 'show' is assumed. For requests that originated in a POST,
     * a 'redirect' is used which will cause a redirect to the 'show' action.
     *
     * @param string|null $newaction the action that should be used next
     * @param string $message optional message, will not be shown except for some dub classes
     */
    public function __construct($newaction = null, $message = '') {
        global $INPUT;
        parent::__construct($message);
        if(is_null($newaction)) {
            if(strtolower($INPUT->server->str('REQUEST_METHOD')) == 'post') {
                $newaction = 'redirect';
            } else {
                $newaction = 'show';
            }
        }

        $this->newaction = $newaction;
    }

    /**
     * Returns the action to use next
     *
     * @return string
     */
    public function getNewAction() {
        return $this->newaction;
    }

    /**
     * Should this Exception's message be shown to the user?
     *
     * @param null|bool $set when null is given, the current setting is not changed
     * @return bool
     */
    public function displayToUser($set = null) {
        if(!is_null($set)) $this->displayToUser = $set;
        return $set;
    }
}