xref: /dokuwiki/inc/Action/AbstractAction.php (revision 73522543bc6b95590563b07feeb1b3d0113a2018)
1<?php
2
3namespace dokuwiki\Action;
4
5use dokuwiki\Action\Exception\ActionDisabledException;
6use dokuwiki\Action\Exception\ActionException;
7use dokuwiki\Action\Exception\FatalException;
8
9/**
10 * Class AbstractAction
11 *
12 * Base class for all actions
13 *
14 * @package dokuwiki\Action
15 */
16abstract class AbstractAction {
17
18    /** @var string holds the name of the action (lowercase class name, no namespace) */
19    protected $actionname;
20
21    /**
22     * AbstractAction constructor.
23     *
24     * @param string $actionname the name of this action (see getActioName() for caveats)
25     */
26    public function __construct($actionname) {
27        $this->actionname = $actionname;
28    }
29
30    /**
31     * Return the minimum permission needed
32     *
33     * This needs to return one of the AUTH_* constants. It will be checked against
34     * the current user and page after checkPermissions() ran through. If it fails,
35     * the user will be shown the Denied action.
36     *
37     * @return int
38     */
39    abstract function minimumPermission();
40
41    /**
42     * Check permissions are correct to run this action
43     *
44     * @throws ActionException
45     * @return void
46     */
47    public function checkPermissions() {
48        if(!actionOK($this->actionname)) throw new ActionDisabledException();
49    }
50
51    /**
52     * Process data
53     *
54     * This runs before any output is sent to the browser.
55     *
56     * Throw an Exception if a different action should be run after this step.
57     *
58     * @throws ActionException
59     * @return void
60     */
61    public function preProcess() {
62    }
63
64    /**
65     * Output whatever content is wanted within tpl_content();
66     *
67     * @fixme we may want to return a Ui class here
68     */
69    public function tplContent() {
70        throw new FatalException('No content for Action ' . $this->actionname);
71    }
72
73    /**
74     * Returns the name of this action
75     *
76     * This is usually the lowercased class name, but may differ for some actions.
77     * eg. the export_ modes or for the Plugin action.
78     *
79     * @return string
80     */
81    public function getActionName() {
82        return $this->actionname;
83    }
84}
85