1<?php 2 3namespace Kanboard\Controller; 4 5use Kanboard\Core\Controller\PageNotFoundException; 6 7/** 8 * Task Creation Controller 9 * 10 * @package Kanboard\Controller 11 * @author Frederic Guillot 12 */ 13class TaskCreationController extends BaseController 14{ 15 /** 16 * Display a form to create a new task 17 * 18 * @access public 19 * @param array $values 20 * @param array $errors 21 * @throws PageNotFoundException 22 */ 23 public function show(array $values = array(), array $errors = array()) 24 { 25 $project = $this->getProject(); 26 $swimlanesList = $this->swimlaneModel->getList($project['id'], false, true); 27 $values += $this->prepareValues($project['is_private'], $swimlanesList); 28 29 $values = $this->hook->merge('controller:task:form:default', $values, array('default_values' => $values)); 30 $values = $this->hook->merge('controller:task-creation:form:default', $values, array('default_values' => $values)); 31 32 $this->response->html($this->template->render('task_creation/show', array( 33 'project' => $project, 34 'errors' => $errors, 35 'values' => $values + array('project_id' => $project['id']), 36 'columns_list' => $this->columnModel->getList($project['id']), 37 'users_list' => $this->projectUserRoleModel->getAssignableUsersList($project['id'], true, false, $project['is_private'] == 1), 38 'categories_list' => $this->categoryModel->getList($project['id']), 39 'swimlanes_list' => $swimlanesList, 40 ))); 41 } 42 43 /** 44 * Validate and save a new task 45 * 46 * @access public 47 */ 48 public function save() 49 { 50 $project = $this->getProject(); 51 $values = $this->request->getValues(); 52 $values['project_id'] = $project['id']; 53 54 list($valid, $errors) = $this->taskValidator->validateCreation($values); 55 56 if (! $valid) { 57 $this->flash->failure(t('Unable to create your task.')); 58 $this->show($values, $errors); 59 } else if (! $this->helper->projectRole->canCreateTaskInColumn($project['id'], $values['column_id'])) { 60 $this->flash->failure(t('You cannot create tasks in this column.')); 61 $this->response->redirect($this->helper->url->to('BoardViewController', 'show', array('project_id' => $project['id'])), true); 62 } else { 63 $task_id = $this->taskCreationModel->create($values); 64 65 if ($task_id > 0) { 66 $this->flash->success(t('Task created successfully.')); 67 $this->afterSave($project, $values, $task_id); 68 } else { 69 $this->flash->failure(t('Unable to create this task.')); 70 $this->response->redirect($this->helper->url->to('BoardViewController', 'show', array('project_id' => $project['id'])), true); 71 } 72 } 73 } 74 75 /** 76 * Duplicate created tasks to multiple projects 77 * 78 * @throws PageNotFoundException 79 */ 80 public function duplicateProjects() 81 { 82 $project = $this->getProject(); 83 $values = $this->request->getValues(); 84 85 if (isset($values['project_ids'])) { 86 foreach ($values['project_ids'] as $project_id) { 87 $this->taskProjectDuplicationModel->duplicateToProject($values['task_id'], $project_id); 88 } 89 } 90 91 $this->response->redirect($this->helper->url->to('BoardViewController', 'show', array('project_id' => $project['id'])), true); 92 } 93 94 /** 95 * Executed after the task is saved 96 * 97 * @param array $project 98 * @param array $values 99 * @param integer $task_id 100 */ 101 protected function afterSave(array $project, array &$values, $task_id) 102 { 103 if (isset($values['duplicate_multiple_projects']) && $values['duplicate_multiple_projects'] == 1) { 104 $this->chooseProjects($project, $task_id); 105 } elseif (isset($values['another_task']) && $values['another_task'] == 1) { 106 $this->show(array( 107 'owner_id' => $values['owner_id'], 108 'color_id' => $values['color_id'], 109 'category_id' => isset($values['category_id']) ? $values['category_id'] : 0, 110 'column_id' => $values['column_id'], 111 'swimlane_id' => isset($values['swimlane_id']) ? $values['swimlane_id'] : 0, 112 'another_task' => 1, 113 )); 114 } else { 115 $this->response->redirect($this->helper->url->to('BoardViewController', 'show', array('project_id' => $project['id'])), true); 116 } 117 } 118 119 /** 120 * Prepare form values 121 * 122 * @access protected 123 * @param bool $isPrivateProject 124 * @param array $swimlanesList 125 * @return array 126 */ 127 protected function prepareValues($isPrivateProject, array $swimlanesList) 128 { 129 $values = array( 130 'swimlane_id' => $this->request->getIntegerParam('swimlane_id', key($swimlanesList)), 131 'column_id' => $this->request->getIntegerParam('column_id'), 132 'color_id' => $this->colorModel->getDefaultColor(), 133 ); 134 135 if ($isPrivateProject) { 136 $values['owner_id'] = $this->userSession->getId(); 137 } 138 139 return $values; 140 } 141 142 /** 143 * Choose projects 144 * 145 * @param array $project 146 * @param integer $task_id 147 */ 148 protected function chooseProjects(array $project, $task_id) 149 { 150 $task = $this->taskFinderModel->getById($task_id); 151 $projects = $this->projectUserRoleModel->getActiveProjectsByUser($this->userSession->getId()); 152 unset($projects[$project['id']]); 153 154 $this->response->html($this->template->render('task_creation/duplicate_projects', array( 155 'project' => $project, 156 'task' => $task, 157 'projects_list' => $projects, 158 'values' => array('task_id' => $task['id']) 159 ))); 160 } 161} 162