{
function listItems()
{
- $component = $this->getComponent(array('view'));
- $context = $component->listItems();
- if ($context instanceof IPF_HTTP_Response)
- return $context;
- return $this->render($component->listTemplate(), $context);
+ $this->checkPermissions('view');
+ return array(
+ $this->component->listTemplate(),
+ $this->component->listItems(),
+ );
}
function addItem()
{
- $component = $this->getComponent(array('view', 'add'));
- $context = $component->addItem();
- if ($context instanceof IPF_HTTP_Response)
- return $context;
- return $this->render($component->addTemplate(), $context);
+ $this->checkPermissions('view', 'add');
+ return array(
+ $this->component->addTemplate(),
+ $this->component->addItem(),
+ );
}
function editItem()
{
- $component = $this->getComponent(array('view', 'change'));
- $context = $component->editItem($this->params[3]);
- if ($context instanceof IPF_HTTP_Response)
- return $context;
- return $this->render($component->editTemplate(), $context);
+ $this->checkPermissions('view', 'change');
+ return array(
+ $this->component->editTemplate(),
+ $this->component->editItem($this->params[3]),
+ );
}
function deleteItem()
{
- $component = $this->getComponent(array('view', 'delete'));
- $context = $component->deleteItem($this->params[3]);
- if ($context instanceof IPF_HTTP_Response)
- return $context;
- return $this->render($component->deleteTemplate(), $context);
+ $this->checkPermissions('view', 'delete');
+ return array(
+ $this->component->deleteTemplate(),
+ $this->component->deleteItem($this->params[3]),
+ );
}
function reorder()
{
- $component = $this->getComponent(array('view', 'change'));
+ $this->checkPermissions('view', 'change');
if (!isset($this->request->POST['ids']) || !is_array($this->request->POST['ids']))
return new IPF_HTTP_Response_NotFound($request);
- if ($component->reorder($this->request->POST['ids']))
+ if ($this->component->reorder($this->request->POST['ids']))
return new IPF_HTTP_Response_Json("Ok");
else
return new IPF_HTTP_Response_Json('Cannot find model');
}
- protected function getComponent($requiredPermissions)
+ // protected
+
+ protected function before($action)
{
$this->ensureUserIsStaff();
- $component = IPF_Admin_App::getComponentBySlugs($this->request->params[1], $this->request->params[2]);
- if (!$component)
+ $this->component = IPF_Admin_App::getComponentBySlugs($this->request->params[1], $this->request->params[2]);
+ if (!$this->component)
throw new IPF_HTTP_Error404;
- if (!$component->isAccessible($requiredPermissions, $this->request))
+ $this->component->request = $this->request;
+ }
+
+ protected function checkPermissions()
+ {
+ $requiredPermissions = func_get_args();
+ if (!$this->component->isAccessible($requiredPermissions, $this->request))
throw new IPF_Admin_AccessDenied;
+ }
- $component->request = $this->request;
- return $component;
+ protected function after__listItems__addItem__editItem__deleteItem($action, $response)
+ {
+ list($template, $context) = $response;
+ if ($context instanceof IPF_HTTP_Response)
+ return $context;
+ else
+ return $this->render($template, $context);
}
}
return $name;
}
- function index()
+ protected function before($action)
{
$this->ensureUserIsStaff();
$this->setCurrentDir();
+ }
+ function index()
+ {
$request = $this->request;
$match = $this->params;
$upload_path = IPF::getUploadPath();
function rename()
{
- $this->ensureUserIsStaff();
- $this->setCurrentDir();
-
$old_name = $this->validateName($this->request->POST['old_name']);
$new_name = $this->validateName($this->request->POST['new_name']);
rename($this->dir . $old_name, $this->dir . $new_name);
function move()
{
- $this->ensureUserIsStaff();
- $this->setCurrentDir();
-
$destination = $this->validatePath(IPF::getUploadPath() . DIRECTORY_SEPARATOR . \PFF\Arr::get($this->request->POST, 'destination', ''));
$name = $this->validateName($this->request->POST['name']);
rename($this->dir . $name, $destination . $name);
function mkdir()
{
- $this->ensureUserIsStaff();
- $this->setCurrentDir();
-
$name = $this->validateName($this->request->POST['name']);
mkdir($this->dir . $name);
function delete()
{
- $this->ensureUserIsStaff();
- $this->setCurrentDir();
-
$name = $this->validateName($this->request->POST['name']);
IPF_Utils::removeDirectories($this->dir . $name);
function upload()
{
- $this->ensureUserIsStaff();
- $this->setCurrentDir();
-
$count = count($this->request->FILES['files']['name']);
for ($i = 0; $i < $count; ++$i) {
$uploadfile = $this->dir . basename($this->request->FILES['files']['name'][$i]);
{
$this->request = $request;
$this->params = $matches;
- return $this->$action();
+
+ $response = $this->before($action);
+ if ($response instanceof IPF_HTTP_Response)
+ return $response;
+
+ $response = $this->$action();
+
+ return $this->after($action, $response);
+ }
+
+ protected function before($action)
+ {
+ foreach ($this->filtersForAction($action, 'before') as $method) {
+ $response = $this->$method($action);
+ if ($response)
+ return $response;
+ }
+ }
+
+ protected function after($action, $response)
+ {
+ foreach ($this->filtersForAction($action, 'after') as $method) {
+ $response = $this->$method($action, $response);
+ }
+ return $response;
+ }
+
+ private function filtersForAction($action, $when)
+ {
+ $filters = array();
+ foreach (get_class_methods($this) as $method) {
+ if (preg_match('/^'.$when.'((?:__\w+?)+)$/', $method, $actions)) {
+ $actions = array_filter(explode('__', $actions[1]));
+ if (in_array($action, $actions))
+ $filters[] = $method;
+ }
+ }
+ return $filters;
}
}