xref: /dokuwiki/inc/Action/AbstractAction.php (revision 81f8bc8ba4d6365fdb39eedb22a420a15623fcd9)
164ab5140SAndreas Gohr<?php
264ab5140SAndreas Gohr
364ab5140SAndreas Gohrnamespace dokuwiki\Action;
464ab5140SAndreas Gohr
564ab5140SAndreas Gohruse dokuwiki\Action\Exception\ActionException;
6f21dad39SAndreas Gohruse dokuwiki\Action\Exception\FatalException;
764ab5140SAndreas Gohr
8ab583a1bSAndreas Gohr/**
9ab583a1bSAndreas Gohr * Class AbstractAction
10ab583a1bSAndreas Gohr *
11ab583a1bSAndreas Gohr * Base class for all actions
12ab583a1bSAndreas Gohr *
13ab583a1bSAndreas Gohr * @package dokuwiki\Action
14ab583a1bSAndreas Gohr */
158c7c53b0SAndreas Gohrabstract class AbstractAction
168c7c53b0SAndreas Gohr{
1764ab5140SAndreas Gohr    /** @var string holds the name of the action (lowercase class name, no namespace) */
1864ab5140SAndreas Gohr    protected $actionname;
1964ab5140SAndreas Gohr
2064ab5140SAndreas Gohr    /**
2164ab5140SAndreas Gohr     * AbstractAction constructor.
2273522543SAndreas Gohr     *
23225d36a1SAndreas Gohr     * @param string $actionname the name of this action (see getActionName() for caveats)
2464ab5140SAndreas Gohr     */
25d868eb89SAndreas Gohr    public function __construct($actionname = '')
26d868eb89SAndreas Gohr    {
276e4bf08eSAndreas Gohr        if ($actionname !== '') {
2873522543SAndreas Gohr            $this->actionname = $actionname;
296e4bf08eSAndreas Gohr        } else {
30*81f8bc8bSfiwswe            // As of PHP 8 this seems to be the fastest way to get the name:
31*81f8bc8bSfiwswe            $n = explode('\\', get_class($this));
32*81f8bc8bSfiwswe            $this->actionname = array_pop($n);
336e4bf08eSAndreas Gohr        }
3464ab5140SAndreas Gohr    }
3564ab5140SAndreas Gohr
3664ab5140SAndreas Gohr    /**
3764ab5140SAndreas Gohr     * Return the minimum permission needed
3864ab5140SAndreas Gohr     *
3964ab5140SAndreas Gohr     * This needs to return one of the AUTH_* constants. It will be checked against
4064ab5140SAndreas Gohr     * the current user and page after checkPermissions() ran through. If it fails,
4164ab5140SAndreas Gohr     * the user will be shown the Denied action.
4264ab5140SAndreas Gohr     *
4364ab5140SAndreas Gohr     * @return int
4464ab5140SAndreas Gohr     */
45ec701221SAndreas Gohr    abstract public function minimumPermission();
4664ab5140SAndreas Gohr
4764ab5140SAndreas Gohr    /**
48b2c9cd19SAndreas Gohr     * Check conditions are met to run this action
4964ab5140SAndreas Gohr     *
5064ab5140SAndreas Gohr     * @throws ActionException
5164ab5140SAndreas Gohr     * @return void
5264ab5140SAndreas Gohr     */
53d868eb89SAndreas Gohr    public function checkPreconditions()
54d868eb89SAndreas Gohr    {
5564ab5140SAndreas Gohr    }
5664ab5140SAndreas Gohr
5764ab5140SAndreas Gohr    /**
5864ab5140SAndreas Gohr     * Process data
5964ab5140SAndreas Gohr     *
6064ab5140SAndreas Gohr     * This runs before any output is sent to the browser.
6164ab5140SAndreas Gohr     *
6264ab5140SAndreas Gohr     * Throw an Exception if a different action should be run after this step.
6364ab5140SAndreas Gohr     *
6464ab5140SAndreas Gohr     * @throws ActionException
6564ab5140SAndreas Gohr     * @return void
6664ab5140SAndreas Gohr     */
67d868eb89SAndreas Gohr    public function preProcess()
68d868eb89SAndreas Gohr    {
6964ab5140SAndreas Gohr    }
7064ab5140SAndreas Gohr
7164ab5140SAndreas Gohr    /**
7264ab5140SAndreas Gohr     * Output whatever content is wanted within tpl_content();
73f21dad39SAndreas Gohr     *
74f21dad39SAndreas Gohr     * @fixme we may want to return a Ui class here
7579a2d784SGerrit Uitslag     * @throws FatalException
7664ab5140SAndreas Gohr     */
77d868eb89SAndreas Gohr    public function tplContent()
78d868eb89SAndreas Gohr    {
79f21dad39SAndreas Gohr        throw new FatalException('No content for Action ' . $this->actionname);
80f21dad39SAndreas Gohr    }
81f21dad39SAndreas Gohr
82f21dad39SAndreas Gohr    /**
8373522543SAndreas Gohr     * Returns the name of this action
8473522543SAndreas Gohr     *
8573522543SAndreas Gohr     * This is usually the lowercased class name, but may differ for some actions.
8673522543SAndreas Gohr     * eg. the export_ modes or for the Plugin action.
8773522543SAndreas Gohr     *
88f21dad39SAndreas Gohr     * @return string
89f21dad39SAndreas Gohr     */
90d868eb89SAndreas Gohr    public function getActionName()
91d868eb89SAndreas Gohr    {
92f21dad39SAndreas Gohr        return $this->actionname;
9364ab5140SAndreas Gohr    }
9464ab5140SAndreas Gohr}
95