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