summaryrefslogtreecommitdiff
path: root/app/Api/Action.php
blob: bdb5b26eefa904865117cb22a4e5e3046eaeccdc (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
<?php

namespace Kanboard\Api;

/**
 * Action API controller
 *
 * @package  api
 * @author   Frederic Guillot
 */
class Action extends \Kanboard\Core\Base
{
    public function getAvailableActions()
    {
        return $this->actionManager->getAvailableActions();
    }

    public function getAvailableActionEvents()
    {
        return $this->eventManager->getAll();
    }

    public function getCompatibleActionEvents($action_name)
    {
        return $this->actionManager->getCompatibleEvents($action_name);
    }

    public function removeAction($action_id)
    {
        return $this->action->remove($action_id);
    }

    public function getActions($project_id)
    {
        return $this->action->getAllByProject($project_id);
    }

    public function createAction($project_id, $event_name, $action_name, array $params)
    {
        $values = array(
            'project_id' => $project_id,
            'event_name' => $event_name,
            'action_name' => $action_name,
            'params' => $params,
        );

        list($valid, ) = $this->action->validateCreation($values);

        if (! $valid) {
            return false;
        }

        // Check if the action exists
        $actions = $this->actionManager->getAvailableActions();

        if (! isset($actions[$action_name])) {
            return false;
        }

        // Check the event
        $action = $this->actionManager->getAction($action_name);

        if (! in_array($event_name, $action->getEvents())) {
            return false;
        }

        $required_params = $action->getActionRequiredParameters();

        // Check missing parameters
        foreach ($required_params as $param => $value) {
            if (! isset($params[$param])) {
                return false;
            }
        }

        // Check extra parameters
        foreach ($params as $param => $value) {
            if (! isset($required_params[$param])) {
                return false;
            }
        }

        return $this->action->create($values);
    }
}