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