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    /** @var string holds the name of the action (lowercase class name, no namespace) */
18    protected $actionname;
19
20    /**
21     * AbstractAction constructor.
22     *
23     * @param string $actionname the name of this action (see getActionName() for caveats)
24     */
25    public function __construct($actionname = '')
26    {
27        if ($actionname !== '') {
28            $this->actionname = $actionname;
29        } else {
30            // As of PHP 8 this seems to be the fastest way to get the name:
31            $this->actionname = strtolower((new \ReflectionClass($this))->getShortName());
32        }
33    }
34
35    /**
36     * Return the minimum permission needed
37     *
38     * This needs to return one of the AUTH_* constants. It will be checked against
39     * the current user and page after checkPermissions() ran through. If it fails,
40     * the user will be shown the Denied action.
41     *
42     * @return int
43     */
44    abstract public function minimumPermission();
45
46    /**
47     * Check conditions are met to run this action
48     *
49     * @throws ActionException
50     * @return void
51     */
52    public function checkPreconditions()
53    {
54    }
55
56    /**
57     * Process data
58     *
59     * This runs before any output is sent to the browser.
60     *
61     * Throw an Exception if a different action should be run after this step.
62     *
63     * @throws ActionException
64     * @return void
65     */
66    public function preProcess()
67    {
68    }
69
70    /**
71     * Output whatever content is wanted within tpl_content();
72     *
73     * @fixme we may want to return a Ui class here
74     * @throws FatalException
75     */
76    public function tplContent()
77    {
78        throw new FatalException('No content for Action ' . $this->actionname);
79    }
80
81    /**
82     * Returns the name of this action
83     *
84     * This is usually the lowercased class name, but may differ for some actions.
85     * eg. the export_ modes or for the Plugin action.
86     *
87     * @return string
88     */
89    public function getActionName()
90    {
91        return $this->actionname;
92    }
93}
94