xref: /dokuwiki/inc/Action/AbstractAction.php (revision f21dad3906d4ec6b3d86685599409894630abdc1)
164ab5140SAndreas Gohr<?php
264ab5140SAndreas Gohr
364ab5140SAndreas Gohrnamespace dokuwiki\Action;
464ab5140SAndreas Gohr
564ab5140SAndreas Gohruse dokuwiki\Action\Exception\ActionDisabledException;
664ab5140SAndreas Gohruse dokuwiki\Action\Exception\ActionException;
7*f21dad39SAndreas Gohruse dokuwiki\Action\Exception\FatalException;
864ab5140SAndreas Gohr
964ab5140SAndreas Gohrabstract class AbstractAction {
1064ab5140SAndreas Gohr
1164ab5140SAndreas Gohr    /** @var string holds the name of the action (lowercase class name, no namespace) */
1264ab5140SAndreas Gohr    protected $actionname;
1364ab5140SAndreas Gohr
1464ab5140SAndreas Gohr    /**
1564ab5140SAndreas Gohr     * AbstractAction constructor.
1664ab5140SAndreas Gohr     */
1764ab5140SAndreas Gohr    public function __construct() {
1864ab5140SAndreas Gohr        // http://stackoverflow.com/a/27457689/172068
1964ab5140SAndreas Gohr        $this->actionname = strtolower(substr(strrchr(get_class($this), '\\'), 1));
2064ab5140SAndreas Gohr    }
2164ab5140SAndreas Gohr
2264ab5140SAndreas Gohr    /**
2364ab5140SAndreas Gohr     * Return the minimum permission needed
2464ab5140SAndreas Gohr     *
2564ab5140SAndreas Gohr     * This needs to return one of the AUTH_* constants. It will be checked against
2664ab5140SAndreas Gohr     * the current user and page after checkPermissions() ran through. If it fails,
2764ab5140SAndreas Gohr     * the user will be shown the Denied action.
2864ab5140SAndreas Gohr     *
2964ab5140SAndreas Gohr     * @return int
3064ab5140SAndreas Gohr     */
3164ab5140SAndreas Gohr    abstract function minimumPermission();
3264ab5140SAndreas Gohr
3364ab5140SAndreas Gohr    /**
3464ab5140SAndreas Gohr     * Check permissions are correct to run this action
3564ab5140SAndreas Gohr     *
3664ab5140SAndreas Gohr     * @throws ActionException
3764ab5140SAndreas Gohr     * @return void
3864ab5140SAndreas Gohr     */
3964ab5140SAndreas Gohr    public function checkPermissions() {
4064ab5140SAndreas Gohr        if(!actionOK($this->actionname)) throw new ActionDisabledException();
4164ab5140SAndreas Gohr    }
4264ab5140SAndreas Gohr
4364ab5140SAndreas Gohr    /**
4464ab5140SAndreas Gohr     * Process data
4564ab5140SAndreas Gohr     *
4664ab5140SAndreas Gohr     * This runs before any output is sent to the browser.
4764ab5140SAndreas Gohr     *
4864ab5140SAndreas Gohr     * Throw an Exception if a different action should be run after this step.
4964ab5140SAndreas Gohr     *
5064ab5140SAndreas Gohr     * @throws ActionException
5164ab5140SAndreas Gohr     * @return void
5264ab5140SAndreas Gohr     */
5364ab5140SAndreas Gohr    public function preProcess() {
5464ab5140SAndreas Gohr    }
5564ab5140SAndreas Gohr
5664ab5140SAndreas Gohr    /**
5764ab5140SAndreas Gohr     * Output whatever content is wanted within tpl_content();
58*f21dad39SAndreas Gohr     *
59*f21dad39SAndreas Gohr     * @fixme we may want to return a Ui class here
6064ab5140SAndreas Gohr     */
6164ab5140SAndreas Gohr    public function tplContent() {
62*f21dad39SAndreas Gohr        throw new FatalException('No content for Action ' . $this->actionname);
63*f21dad39SAndreas Gohr    }
64*f21dad39SAndreas Gohr
65*f21dad39SAndreas Gohr    /**
66*f21dad39SAndreas Gohr     * @return string
67*f21dad39SAndreas Gohr     */
68*f21dad39SAndreas Gohr    public function getActionName() {
69*f21dad39SAndreas Gohr        return $this->actionname;
7064ab5140SAndreas Gohr    }
7164ab5140SAndreas Gohr}
72