1<?php
2
3namespace Kanboard\Controller;
4
5/**
6 * Action Creation Controller
7 *
8 * @package Kanboard\Controller
9 * @author  Frederic Guillot
10 */
11class ActionCreationController extends BaseController
12{
13    /**
14     * Show the form (step 1)
15     *
16     * @access public
17     */
18    public function create()
19    {
20        $project = $this->getProject();
21
22        $this->response->html($this->template->render('action_creation/create', array(
23            'project' => $project,
24            'values' => array('project_id' => $project['id']),
25            'available_actions' => $this->actionManager->getAvailableActions(),
26        )));
27    }
28
29    /**
30     * Choose the event according to the action (step 2)
31     *
32     * @access public
33     */
34    public function event()
35    {
36        $project = $this->getProject();
37        $values = $this->request->getValues();
38        $values['project_id'] = $project['id'];
39
40        if (empty($values['action_name'])) {
41            return $this->create();
42        }
43
44        return $this->response->html($this->template->render('action_creation/event', array(
45            'values' => $values,
46            'project' => $project,
47            'available_actions' => $this->actionManager->getAvailableActions(),
48            'events' => $this->actionManager->getCompatibleEvents($values['action_name']),
49        )));
50    }
51
52    /**
53     * Define action parameters (step 3)
54     *
55     * @access public
56     */
57    public function params()
58    {
59        $project = $this->getProject();
60        $values = $this->request->getValues();
61        $values['project_id'] = $project['id'];
62
63        if (empty($values['action_name']) || empty($values['event_name'])) {
64            $this->create();
65            return;
66        }
67
68        $action = $this->actionManager->getAction($values['action_name']);
69        $action_params = $action->getActionRequiredParameters();
70
71        if (empty($action_params)) {
72            $this->doCreation($project, $values + array('params' => array()));
73        }
74
75        $projects_list = $this->projectUserRoleModel->getActiveProjectsByUser($this->userSession->getId());
76        unset($projects_list[$project['id']]);
77
78        $this->response->html($this->template->render('action_creation/params', array(
79            'values' => $values,
80            'action_params' => $action_params,
81            'columns_list' => $this->columnModel->getList($project['id']),
82            'users_list' => $this->projectUserRoleModel->getAssignableUsersList($project['id']),
83            'projects_list' => $projects_list,
84            'colors_list' => $this->colorModel->getList(),
85            'categories_list' => $this->categoryModel->getList($project['id']),
86            'links_list' => $this->linkModel->getList(0, false),
87            'priorities_list' => $this->projectTaskPriorityModel->getPriorities($project),
88            'project' => $project,
89            'available_actions' => $this->actionManager->getAvailableActions(),
90            'swimlane_list' => $this->swimlaneModel->getList($project['id']),
91            'events' => $this->actionManager->getCompatibleEvents($values['action_name']),
92        )));
93    }
94
95    /**
96     * Save the action (last step)
97     *
98     * @access public
99     */
100    public function save()
101    {
102        $this->doCreation($this->getProject(), $this->request->getValues());
103    }
104
105    /**
106     * Common method to save the action
107     *
108     * @access private
109     * @param  array     $project   Project properties
110     * @param  array     $values    Form values
111     */
112    private function doCreation(array $project, array $values)
113    {
114        $values['project_id'] = $project['id'];
115        list($valid, ) = $this->actionValidator->validateCreation($values);
116
117        if ($valid) {
118            if ($this->actionModel->create($values) !== false) {
119                $this->flash->success(t('Your automatic action has been created successfully.'));
120            } else {
121                $this->flash->failure(t('Unable to create your automatic action.'));
122            }
123        }
124
125        $this->response->redirect($this->helper->url->to('ActionController', 'index', array('project_id' => $project['id'])));
126    }
127}
128