summaryrefslogtreecommitdiff
path: root/app/Core/Action/ActionManager.php
blob: 3a77084dee4118e7dd3d14bc1af6f61c92b0507e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php

namespace Kanboard\Core\Action;

use Exception;
use RuntimeException;
use Kanboard\Core\Base;
use Kanboard\Action\Base as ActionBase;

/**
 * Action Manager
 *
 * @package  action
 * @author   Frederic Guillot
 */
class ActionManager extends Base
{
    /**
     * List of automatic actions
     *
     * @access private
     * @var ActionBase[]
     */
    private $actions = array();

    /**
     * Register a new automatic action
     *
     * @access public
     * @param  ActionBase $action
     * @return ActionManager
     */
    public function register(ActionBase $action)
    {
        $this->actions[$action->getName()] = $action;
        return $this;
    }

    /**
     * Get automatic action instance
     *
     * @access public
     * @param  string  $name  Absolute class name with namespace
     * @return ActionBase
     */
    public function getAction($name)
    {
        if (isset($this->actions[$name])) {
            return $this->actions[$name];
        }

        throw new RuntimeException('Automatic Action Not Found: '.$name);
    }

    /**
     * Get available automatic actions
     *
     * @access public
     * @return array
     */
    public function getAvailableActions()
    {
        $actions = array();

        foreach ($this->actions as $action) {
            if (count($action->getEvents()) > 0) {
                $actions[$action->getName()] = $action->getDescription();
            }
        }

        asort($actions);

        return $actions;
    }

    /**
     * Get all available action parameters
     *
     * @access public
     * @param  array  $actions
     * @return array
     */
    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;
    }

    /**
     * Get list of compatible events for a given action
     *
     * @access public
     * @param  string $name
     * @return array
     */
    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;
    }

    /**
     * Bind automatic actions to events
     *
     * @access public
     * @return ActionManager
     */
    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;
    }

    /**
     * Remove all listeners for automated actions
     *
     * @access public
     */
    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);
                }
            }
        }
    }
}