|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Kanboard\Plugin\AutoSubtasks\Action; |
| 4 | + |
| 5 | +use Kanboard\Model\TaskModel; |
| 6 | +use Kanboard\Action\Base; |
| 7 | + |
| 8 | +class AutoCreateSubtaskVanilla extends Base |
| 9 | +{ |
| 10 | + |
| 11 | + public function getDescription() |
| 12 | + { |
| 13 | + return t('Create one or more Subtasks Automatically'); |
| 14 | + } |
| 15 | + |
| 16 | + public function getCompatibleEvents() |
| 17 | + { |
| 18 | + |
| 19 | + return array( |
| 20 | + TaskModel::EVENT_CREATE, |
| 21 | + TaskModel::EVENT_MOVE_COLUMN, |
| 22 | + ); |
| 23 | + } |
| 24 | + |
| 25 | + public function getActionRequiredParameters() |
| 26 | + { |
| 27 | + //changed 'titles' to 'multitasktitles' to have a clean way to render the title-textfield as a textarea |
| 28 | + return array( |
| 29 | + 'column_id' => t('Column'), |
| 30 | + 'user_id' => t('Assignee'), |
| 31 | + 'multitasktitles' => t('Subtask Title(s)'), |
| 32 | + 'time_estimated' => t('Estimated Time in Hours'), |
| 33 | + 'check_box' => t('Apply to all Columns'), |
| 34 | + ); |
| 35 | + } |
| 36 | + |
| 37 | + public function getEventRequiredParameters() |
| 38 | + { |
| 39 | + return array( |
| 40 | + 'task_id', |
| 41 | + 'task' => array( |
| 42 | + 'project_id', |
| 43 | + 'column_id', |
| 44 | + 'title', |
| 45 | + ), |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + public function doAction(array $data) |
| 50 | + { |
| 51 | + //get the value of 'multitasktitles' in stead of the original 'titles' |
| 52 | + $title_test = $this->getParam('multitasktitles'); |
| 53 | + $title_test = preg_replace("/^\s+/m", $data['task']['title'] . "\r\n", $title_test); |
| 54 | + |
| 55 | + $values = array( |
| 56 | + 'title' => $title_test, |
| 57 | + 'task_id' => $data['task_id'], |
| 58 | + 'user_id' => $this->getParam('user_id'), |
| 59 | + 'time_estimated' => $this->getParam('time_estimated'), |
| 60 | + 'time_spent' => 0, |
| 61 | + 'status' => 0, |
| 62 | + ); |
| 63 | + |
| 64 | + $subtasks = explode("\r\n", isset($values['title']) ? $values['title'] : ''); |
| 65 | + $subtasksAdded = 0; |
| 66 | + |
| 67 | + foreach ($subtasks as $subtask) { |
| 68 | + $subtask = trim($subtask); |
| 69 | + |
| 70 | + if (! empty($subtask)) { |
| 71 | + $subtaskValues = $values; |
| 72 | + $subtaskValues['title'] = $subtask; |
| 73 | + |
| 74 | + list($valid, $errors) = $this->subtaskValidator->validateCreation($subtaskValues); |
| 75 | + |
| 76 | + if (! $valid) { |
| 77 | + $this->create($values, $errors); |
| 78 | + return false; |
| 79 | + } |
| 80 | + |
| 81 | + if (! $this->subtaskModel->create($subtaskValues)) { |
| 82 | + $this->flash->failure(t('Unable to create your sub-task.')); |
| 83 | + $this->response->redirect($this->helper->url->to('TaskViewController', 'show', array('project_id' => $task['project_id'], 'task_id' => $task['id']), 'subtasks'), true); |
| 84 | + return false; |
| 85 | + } |
| 86 | + |
| 87 | + $subtasksAdded++; |
| 88 | + } |
| 89 | + } |
| 90 | + //restore the messaging with a flash but this message doesn't seem to appear in the flash area. Only the create message from (kanboard/app/Controller/ActionCreationController.php). |
| 91 | + if ($subtasksAdded > 0) { |
| 92 | + if ($subtasksAdded === 1) { |
| 93 | + $this->flash->success(t('Subtask added successfully.')); |
| 94 | + } else { |
| 95 | + $this->flash->success(t('%d subtasks added successfully.', $subtasksAdded)); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + public function hasRequiredCondition(array $data) |
| 101 | + { |
| 102 | + |
| 103 | + if ($this->getParam('check_box')) { |
| 104 | + return $data['task']['column_id'] == $data['task']['column_id']; |
| 105 | + } else { |
| 106 | + return $data['task']['column_id'] == $this->getParam('column_id'); |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments