1 | 2 | simandl | <?php |
2 | | | |
3 | | | namespace Phem\Controllers; |
4 | | | |
5 | | | use Phem\Core\Object; |
6 | | | use Phem\Environment\ApplicationException; |
7 | | | use Phem\Environment\EnvironmentManager; |
8 | | | use ReflectionMethod; |
9 | | | |
10 | | | abstract class Controller extends Object |
11 | | | { |
12 | | | |
13 | | | protected $defaultTask = DEFAULT_TASK; |
14 | | | protected $entityManager; |
15 | | | protected $redirectToController; |
16 | | | protected $redirectToTask; |
17 | | | protected $redirectHiddenArgs; |
18 | | | protected $redirectArgs; |
19 | | | protected $loggedUser; |
20 | | | |
21 | | | function __construct() |
22 | | | { |
23 | | | $this->loggedUser = EnvironmentManager::getSession()->getVar(ROOT_NAMESPACE . ".loggedUser"); |
24 | | | } |
25 | | | |
26 | | | function execute($task) |
27 | | | { |
28 | | | |
29 | | | $args = EnvironmentManager::getSession()->getVar(ROOT_NAMESPACE . "_redirectedArgs"); |
30 | | | |
31 | | | if (($task != null) && (method_exists($this, $task))) |
32 | | | { |
33 | | | $taskToExecute = $task; |
34 | | | } |
35 | | | else if (method_exists($this, $this->defaultTask)) |
36 | | | { |
37 | | | $taskToExecute = $this->defaultTask; |
38 | | | } |
39 | | | else |
40 | | | { |
41 | | | throw new ApplicationException("neither custom or default task exists for given controller"); |
42 | | | } |
43 | | | |
44 | | | |
45 | | | $reflector = new ReflectionMethod($this, $taskToExecute); |
46 | | | $params = array(); |
47 | | | $reflectedArgs = $reflector->getParameters(); |
48 | | | |
49 | | | foreach ($reflectedArgs as $arg) |
50 | | | { |
51 | | | if (EnvironmentManager::getRequestVar($arg->name) != null) |
52 | | | { |
53 | | | $params[] = EnvironmentManager::getRequestVar($arg->name); |
54 | | | } |
55 | | | elseif ((is_array($args) && array_key_exists($arg->name, $args))) |
56 | | | { |
57 | | | $params[] = $args[$arg->name]; |
58 | | | } |
59 | | | else if ($arg->isDefaultValueAvailable()) |
60 | | | { |
61 | | | $params[] = $arg->getDefaultValue(); |
62 | | | } |
63 | | | else |
64 | | | { |
65 | | | $params[] = null; |
66 | | | } |
67 | | | } |
68 | | | |
69 | | | call_user_func_array(array($this, $taskToExecute), $params); |
70 | | | } |
71 | | | |
72 | | | function redirect() |
73 | | | { |
74 | | | if ($this->redirectToController == null) |
75 | | | return; |
76 | | | |
77 | | | /** Perform the Request task */ |
78 | | | $task = DEFAULT_TASK; |
79 | | | if ($this->redirectToTask != null) |
80 | | | $task = $this->redirectToTask; |
81 | | | |
82 | | | $args['controller'] = $this->redirectToController; |
83 | | | $args['task'] = $task; |
84 | | | |
85 | | | |
86 | | | EnvironmentManager::getSession()->setVar(ROOT_NAMESPACE . "_redirectedArgs", $this->redirectHiddenArgs); |
87 | | | |
88 | | | $linkArgs = ($this->redirectArgs == null) ? $args : array_merge($args, $this->redirectArgs); |
89 | | | |
90 | | | $link = EnvironmentManager::getLinkBuilder() |
91 | | | ->link($linkArgs); |
92 | | | |
93 | | | header('Location:' . $link); |
94 | | | } |
95 | | | |
96 | | | } |