|
<?php |
|
|
|
namespace Kanboard\Core\Action; |
|
|
|
use Exception; |
|
use RuntimeException; |
|
use Kanboard\Core\Base; |
|
use Kanboard\Action\Base as ActionBase; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ActionManager extends Base |
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
private $actions = array(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function register(ActionBase $action) |
|
{ |
|
$this->actions[$action->getName()] = $action; |
|
return $this; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function getAction($name) |
|
{ |
|
if (isset($this->actions[$name])) { |
|
return $this->actions[$name]; |
|
} |
|
|
|
throw new RuntimeException('Automatic Action Not Found: '.$name); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function getAvailableActions() |
|
{ |
|
$actions = array(); |
|
|
|
foreach ($this->actions as $action) { |
|
if (count($action->getEvents()) > 0) { |
|
$actions[$action->getName()] = $action->getDescription(); |
|
} |
|
} |
|
|
|
asort($actions); |
|
|
|
return $actions; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function getAvailableParameters(array $actions) |
|
{ |
|
$params = array(); |
|
|
|
foreach ($actions as $action) { |
|
try { |
|
$currentAction = $this->getAction($action['action_name']); |
|
$params[$currentAction->getName()] = $currentAction->getActionRequiredParameters(); |
|
} catch (Exception $e) { |
|
$this->logger->error(__METHOD__.': '.$e->getMessage()); |
|
} |
|
} |
|
|
|
return $params; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function getCompatibleEvents($name) |
|
{ |
|
$events = array(); |
|
$actionEvents = $this->getAction($name)->getEvents(); |
|
|
|
foreach ($this->eventManager->getAll() as $event => $description) { |
|
if (in_array($event, $actionEvents)) { |
|
$events[$event] = $description; |
|
} |
|
} |
|
|
|
return $events; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function attachEvents() |
|
{ |
|
if ($this->userSession->isLogged()) { |
|
$actions = $this->actionModel->getAllByUser($this->userSession->getId()); |
|
} else { |
|
$actions = $this->actionModel->getAll(); |
|
} |
|
|
|
foreach ($actions as $action) { |
|
try { |
|
$listener = clone $this->getAction($action['action_name']); |
|
$listener->setProjectId($action['project_id']); |
|
|
|
foreach ($action['params'] as $param_name => $param_value) { |
|
$listener->setParam($param_name, $param_value); |
|
} |
|
|
|
$this->dispatcher->addListener($action['event_name'], array($listener, 'execute')); |
|
} catch (Exception $e) { |
|
$this->logger->error(__METHOD__.': '.$e->getMessage()); |
|
} |
|
} |
|
|
|
return $this; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
public function removeEvents() |
|
{ |
|
foreach ($this->dispatcher->getListeners() as $eventName => $listeners) { |
|
foreach ($listeners as $listener) { |
|
if (is_array($listener) && $listener[0] instanceof ActionBase) { |
|
$this->dispatcher->removeListener($eventName, $listener); |
|
} |
|
} |
|
} |
|
} |
|
} |
|
|