AbstractAction.php 2.2 KB

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