You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
1.6 KiB

  1. <?php
  2. /**
  3. * @copyright NetMonsters <team@netmonsters.ru>
  4. * @link http://netmonsters.ru
  5. * @package Majestic
  6. * @subpackage app
  7. * @since 2010-02-25
  8. * @version SVN: $Id$
  9. * @filesource $URL$
  10. */
  11. abstract class Action
  12. {
  13. protected $template;
  14. /**
  15. * @var PHPView
  16. */
  17. protected $view;
  18. public function __construct()
  19. {
  20. $this->view = FrontController::getInstance()->getView();
  21. $this->extractParams();
  22. $this->execute();
  23. }
  24. protected function extractParams()
  25. {
  26. foreach (Env::getParam() as $name => $value) {
  27. if (is_string($name)) {
  28. $this->$name = $value;
  29. }
  30. }
  31. }
  32. abstract protected function execute();
  33. /**
  34. * Redirect
  35. *
  36. * @param mixed $url
  37. * @param mixed $relative Default to true
  38. */
  39. protected function redirect($url = null, $relative = true)
  40. {
  41. $url = ($url) ? $url : Env::getRequestUri();
  42. if ($relative) {
  43. $url = FrontController::getInstance()->getBaseUrl() . $url;
  44. }
  45. header('Location: ' . $url);
  46. exit();
  47. }
  48. protected function getTemplate()
  49. {
  50. $class = get_class($this);
  51. $template = ($this->template) ? $this->template : substr($class, 0, -6/*strlen('Action')*/);
  52. $dir = array_slice(explode('/', Load::getFilePath($class)), -2, 1);
  53. return '/actions/' . array_pop($dir) . '/' . $template;
  54. }
  55. public function fetch()
  56. {
  57. $this->view->assignObject($this);
  58. return $this->view->fetch($this->getTemplate());
  59. }
  60. }