2012-02-24 22:00:48 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group maniphest
|
|
|
|
*/
|
|
|
|
final class ManiphestBatchEditController extends ManiphestController {
|
|
|
|
|
|
|
|
public function processRequest() {
|
|
|
|
|
|
|
|
$request = $this->getRequest();
|
|
|
|
$user = $request->getUser();
|
|
|
|
|
|
|
|
$task_ids = $request->getArr('batch');
|
|
|
|
$tasks = id(new ManiphestTask())->loadAllWhere(
|
|
|
|
'id IN (%Ld)',
|
|
|
|
$task_ids);
|
|
|
|
|
|
|
|
$actions = $request->getStr('actions');
|
|
|
|
if ($actions) {
|
|
|
|
$actions = json_decode($actions, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($request->isFormPost() && is_array($actions)) {
|
|
|
|
foreach ($tasks as $task) {
|
|
|
|
$xactions = $this->buildTransactions($actions, $task);
|
|
|
|
if ($xactions) {
|
|
|
|
$editor = new ManiphestTransactionEditor();
|
2012-10-10 19:18:23 +02:00
|
|
|
$editor->setActor($user);
|
2012-02-24 22:00:48 +01:00
|
|
|
$editor->applyTransactions($task, $xactions);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-21 22:01:04 +01:00
|
|
|
$task_ids = implode(',', mpull($tasks, 'getID'));
|
|
|
|
|
2012-02-24 22:00:48 +01:00
|
|
|
return id(new AphrontRedirectResponse())
|
2012-03-21 22:01:04 +01:00
|
|
|
->setURI('/maniphest/view/custom/?s=oc&tasks='.$task_ids);
|
2012-02-24 22:00:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$panel = new AphrontPanelView();
|
2013-01-20 18:14:51 +01:00
|
|
|
$panel->setHeader(pht('Maniphest Batch Editor'));
|
|
|
|
$panel->setNoBackground();
|
2012-02-24 22:00:48 +01:00
|
|
|
|
|
|
|
$handle_phids = mpull($tasks, 'getOwnerPHID');
|
2012-09-05 04:02:56 +02:00
|
|
|
$handles = $this->loadViewerHandles($handle_phids);
|
2012-02-24 22:00:48 +01:00
|
|
|
|
|
|
|
$list = new ManiphestTaskListView();
|
|
|
|
$list->setTasks($tasks);
|
|
|
|
$list->setUser($user);
|
|
|
|
$list->setHandles($handles);
|
|
|
|
|
|
|
|
$template = new AphrontTokenizerTemplateView();
|
|
|
|
$template = $template->render();
|
|
|
|
|
|
|
|
require_celerity_resource('maniphest-batch-editor');
|
|
|
|
Javelin::initBehavior(
|
|
|
|
'maniphest-batch-editor',
|
|
|
|
array(
|
|
|
|
'root' => 'maniphest-batch-edit-form',
|
|
|
|
'tokenizerTemplate' => $template,
|
|
|
|
'sources' => array(
|
2012-03-21 22:01:04 +01:00
|
|
|
'project' => array(
|
|
|
|
'src' => '/typeahead/common/projects/',
|
2013-03-13 07:30:03 +01:00
|
|
|
'placeholder' => pht('Type a project name...'),
|
2012-03-21 22:01:04 +01:00
|
|
|
),
|
|
|
|
'owner' => array(
|
|
|
|
'src' => '/typeahead/common/searchowner/',
|
2013-03-13 07:30:03 +01:00
|
|
|
'placeholder' => pht('Type a user name...'),
|
2012-03-21 22:01:04 +01:00
|
|
|
'limit' => 1,
|
|
|
|
),
|
2013-04-05 19:34:43 +02:00
|
|
|
'cc' => array(
|
|
|
|
'src' => '/typeahead/common/mailable/',
|
|
|
|
'placeholder' => pht('Type a user name...'),
|
|
|
|
)
|
2012-02-24 22:00:48 +01:00
|
|
|
),
|
|
|
|
'input' => 'batch-form-actions',
|
2012-03-21 22:01:04 +01:00
|
|
|
'priorityMap' => ManiphestTaskPriority::getTaskPriorityMap(),
|
|
|
|
'statusMap' => ManiphestTaskStatus::getTaskStatusMap(),
|
2012-02-24 22:00:48 +01:00
|
|
|
));
|
|
|
|
|
|
|
|
$form = new AphrontFormView();
|
|
|
|
$form->setUser($user);
|
|
|
|
$form->setID('maniphest-batch-edit-form');
|
|
|
|
|
|
|
|
foreach ($tasks as $task) {
|
|
|
|
$form->appendChild(
|
2013-01-18 03:39:02 +01:00
|
|
|
phutil_tag(
|
2012-02-24 22:00:48 +01:00
|
|
|
'input',
|
|
|
|
array(
|
|
|
|
'type' => 'hidden',
|
|
|
|
'name' => 'batch[]',
|
|
|
|
'value' => $task->getID(),
|
2013-01-18 03:39:02 +01:00
|
|
|
)));
|
2012-02-24 22:00:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$form->appendChild(
|
2013-01-18 03:39:02 +01:00
|
|
|
phutil_tag(
|
2012-02-24 22:00:48 +01:00
|
|
|
'input',
|
|
|
|
array(
|
|
|
|
'type' => 'hidden',
|
|
|
|
'name' => 'actions',
|
|
|
|
'id' => 'batch-form-actions',
|
2013-01-18 03:39:02 +01:00
|
|
|
)));
|
2013-03-13 07:30:03 +01:00
|
|
|
$form->appendChild(
|
|
|
|
phutil_tag('p', array(), pht('These tasks will be edited:')));
|
2012-02-24 22:00:48 +01:00
|
|
|
$form->appendChild($list);
|
|
|
|
$form->appendChild(
|
2012-03-16 01:10:19 +01:00
|
|
|
id(new AphrontFormInsetView())
|
|
|
|
->setTitle('Actions')
|
2013-01-25 21:57:17 +01:00
|
|
|
->setRightButton(javelin_tag(
|
2012-02-24 22:00:48 +01:00
|
|
|
'a',
|
|
|
|
array(
|
|
|
|
'href' => '#',
|
|
|
|
'class' => 'button green',
|
|
|
|
'sigil' => 'add-action',
|
|
|
|
'mustcapture' => true,
|
|
|
|
),
|
2013-03-13 07:30:03 +01:00
|
|
|
pht('Add Another Action')))
|
2013-01-25 21:57:17 +01:00
|
|
|
->setContent(javelin_tag(
|
2012-02-24 22:00:48 +01:00
|
|
|
'table',
|
|
|
|
array(
|
|
|
|
'sigil' => 'maniphest-batch-actions',
|
|
|
|
'class' => 'maniphest-batch-actions-table',
|
|
|
|
),
|
2012-03-16 01:10:19 +01:00
|
|
|
'')))
|
2012-02-24 22:00:48 +01:00
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormSubmitControl())
|
2013-03-13 07:30:03 +01:00
|
|
|
->setValue(pht('Update Tasks'))
|
2012-02-24 22:00:48 +01:00
|
|
|
->addCancelButton('/maniphest/', 'Done'));
|
|
|
|
|
|
|
|
$panel->appendChild($form);
|
|
|
|
|
|
|
|
|
|
|
|
return $this->buildStandardPageResponse(
|
|
|
|
$panel,
|
|
|
|
array(
|
2013-03-13 07:30:03 +01:00
|
|
|
'title' => pht('Batch Editor'),
|
2012-02-24 22:00:48 +01:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
private function buildTransactions($actions, ManiphestTask $task) {
|
2012-03-21 22:01:04 +01:00
|
|
|
$value_map = array();
|
|
|
|
$type_map = array(
|
|
|
|
'add_comment' => ManiphestTransactionType::TYPE_NONE,
|
|
|
|
'assign' => ManiphestTransactionType::TYPE_OWNER,
|
|
|
|
'status' => ManiphestTransactionType::TYPE_STATUS,
|
|
|
|
'priority' => ManiphestTransactionType::TYPE_PRIORITY,
|
|
|
|
'add_project' => ManiphestTransactionType::TYPE_PROJECTS,
|
|
|
|
'remove_project' => ManiphestTransactionType::TYPE_PROJECTS,
|
2013-04-05 19:34:43 +02:00
|
|
|
'add_ccs' => ManiphestTransactionType::TYPE_CCS,
|
|
|
|
'remove_ccs' => ManiphestTransactionType::TYPE_CCS,
|
2012-03-21 22:01:04 +01:00
|
|
|
);
|
2012-03-21 12:37:01 +01:00
|
|
|
|
2012-07-28 00:25:32 +02:00
|
|
|
$edge_edit_types = array(
|
|
|
|
'add_project' => true,
|
|
|
|
'remove_project' => true,
|
2013-04-05 19:34:43 +02:00
|
|
|
'add_ccs' => true,
|
|
|
|
'remove_ccs' => true,
|
2012-07-28 00:25:32 +02:00
|
|
|
);
|
|
|
|
|
2012-02-24 22:00:48 +01:00
|
|
|
$xactions = array();
|
|
|
|
foreach ($actions as $action) {
|
2012-03-21 22:01:04 +01:00
|
|
|
if (empty($type_map[$action['action']])) {
|
|
|
|
throw new Exception("Unknown batch edit action '{$action}'!");
|
|
|
|
}
|
2012-02-24 22:00:48 +01:00
|
|
|
|
2012-03-21 22:01:04 +01:00
|
|
|
$type = $type_map[$action['action']];
|
2012-02-24 22:00:48 +01:00
|
|
|
|
2012-03-21 22:01:04 +01:00
|
|
|
// Figure out the current value, possibly after modifications by other
|
|
|
|
// batch actions of the same type. For example, if the user chooses to
|
|
|
|
// "Add Comment" twice, we should add both comments. More notably, if the
|
|
|
|
// user chooses "Remove Project..." and also "Add Project...", we should
|
|
|
|
// avoid restoring the removed project in the second transaction.
|
2012-03-21 12:37:01 +01:00
|
|
|
|
2012-03-21 22:01:04 +01:00
|
|
|
if (array_key_exists($type, $value_map)) {
|
|
|
|
$current = $value_map[$type];
|
|
|
|
} else {
|
|
|
|
switch ($type) {
|
|
|
|
case ManiphestTransactionType::TYPE_NONE:
|
|
|
|
$current = null;
|
|
|
|
break;
|
|
|
|
case ManiphestTransactionType::TYPE_OWNER:
|
|
|
|
$current = $task->getOwnerPHID();
|
|
|
|
break;
|
|
|
|
case ManiphestTransactionType::TYPE_STATUS:
|
|
|
|
$current = $task->getStatus();
|
|
|
|
break;
|
|
|
|
case ManiphestTransactionType::TYPE_PRIORITY:
|
|
|
|
$current = $task->getPriority();
|
|
|
|
break;
|
|
|
|
case ManiphestTransactionType::TYPE_PROJECTS:
|
2012-03-21 12:37:01 +01:00
|
|
|
$current = $task->getProjectPHIDs();
|
2012-03-21 22:01:04 +01:00
|
|
|
break;
|
2013-04-05 19:34:43 +02:00
|
|
|
case ManiphestTransactionType::TYPE_CCS:
|
|
|
|
$current = $task->getCCPHIDs();
|
|
|
|
break;
|
2012-03-21 22:01:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the value is meaningful / provided, and normalize it if
|
|
|
|
// necessary. This discards, e.g., empty comments and empty owner
|
|
|
|
// changes.
|
|
|
|
|
|
|
|
$value = $action['value'];
|
|
|
|
switch ($type) {
|
|
|
|
case ManiphestTransactionType::TYPE_NONE:
|
|
|
|
if (!strlen($value)) {
|
|
|
|
continue 2;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ManiphestTransactionType::TYPE_OWNER:
|
|
|
|
if (empty($value)) {
|
|
|
|
continue 2;
|
|
|
|
}
|
|
|
|
$value = head($value);
|
|
|
|
if ($value === ManiphestTaskOwner::OWNER_UP_FOR_GRABS) {
|
|
|
|
$value = null;
|
2012-03-21 12:37:01 +01:00
|
|
|
}
|
2012-03-21 22:01:04 +01:00
|
|
|
break;
|
|
|
|
case ManiphestTransactionType::TYPE_PROJECTS:
|
|
|
|
if (empty($value)) {
|
|
|
|
continue 2;
|
|
|
|
}
|
|
|
|
break;
|
2013-04-05 19:34:43 +02:00
|
|
|
case ManiphestTransactionType::TYPE_CCS:
|
|
|
|
if (empty($value)) {
|
|
|
|
continue 2;
|
|
|
|
}
|
|
|
|
break;
|
2012-03-21 22:01:04 +01:00
|
|
|
}
|
|
|
|
|
2012-07-28 00:25:32 +02:00
|
|
|
// If the edit doesn't change anything, go to the next action. This
|
|
|
|
// check is only valid for changes like "owner", "status", etc, not
|
|
|
|
// for edge edits, because we should still apply an edit like
|
|
|
|
// "Remove Projects: A, B" to a task with projects "A, B".
|
2012-03-21 22:01:04 +01:00
|
|
|
|
2012-07-28 00:25:32 +02:00
|
|
|
if (empty($edge_edit_types[$action['action']])) {
|
|
|
|
if ($value == $current) {
|
|
|
|
continue;
|
|
|
|
}
|
2012-03-21 22:01:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Apply the value change; for most edits this is just replacement, but
|
|
|
|
// some need to merge the current and edited values (add/remove project).
|
|
|
|
|
|
|
|
switch ($type) {
|
|
|
|
case ManiphestTransactionType::TYPE_NONE:
|
|
|
|
if (strlen($current)) {
|
|
|
|
$value = $current."\n\n".$value;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ManiphestTransactionType::TYPE_PROJECTS:
|
2013-04-05 19:34:43 +02:00
|
|
|
case ManiphestTransactionType::TYPE_CCS:
|
|
|
|
$remove_actions = array(
|
|
|
|
'remove_project' => true,
|
|
|
|
'remove_ccs' => true,
|
|
|
|
);
|
|
|
|
$is_remove = isset($remove_actions[$action['action']]);
|
2012-03-21 12:37:01 +01:00
|
|
|
|
|
|
|
$current = array_fill_keys($current, true);
|
2012-02-24 22:00:48 +01:00
|
|
|
$value = array_fill_keys($value, true);
|
|
|
|
|
|
|
|
$new = $current;
|
|
|
|
$did_something = false;
|
|
|
|
|
|
|
|
if ($is_remove) {
|
|
|
|
foreach ($value as $phid => $ignored) {
|
|
|
|
if (isset($new[$phid])) {
|
|
|
|
unset($new[$phid]);
|
|
|
|
$did_something = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
foreach ($value as $phid => $ignored) {
|
|
|
|
if (empty($new[$phid])) {
|
|
|
|
$new[$phid] = true;
|
|
|
|
$did_something = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$did_something) {
|
2012-03-21 22:01:04 +01:00
|
|
|
continue 2;
|
2012-02-24 22:00:48 +01:00
|
|
|
}
|
|
|
|
|
2012-03-21 22:01:04 +01:00
|
|
|
$value = array_keys($new);
|
|
|
|
break;
|
|
|
|
}
|
2012-03-21 12:37:01 +01:00
|
|
|
|
2012-03-21 22:01:04 +01:00
|
|
|
$value_map[$type] = $value;
|
|
|
|
}
|
2012-03-21 12:37:01 +01:00
|
|
|
|
2012-03-21 22:01:04 +01:00
|
|
|
$template = new ManiphestTransaction();
|
|
|
|
$template->setAuthorPHID($this->getRequest()->getUser()->getPHID());
|
|
|
|
|
|
|
|
// TODO: Set content source to "batch edit".
|
2012-03-21 12:37:01 +01:00
|
|
|
|
2012-03-21 22:01:04 +01:00
|
|
|
foreach ($value_map as $type => $value) {
|
|
|
|
$xaction = clone $template;
|
|
|
|
$xaction->setTransactionType($type);
|
|
|
|
|
|
|
|
switch ($type) {
|
|
|
|
case ManiphestTransactionType::TYPE_NONE:
|
|
|
|
$xaction->setComments($value);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$xaction->setNewValue($value);
|
2012-02-24 22:00:48 +01:00
|
|
|
break;
|
|
|
|
}
|
2012-03-21 22:01:04 +01:00
|
|
|
|
|
|
|
$xactions[] = $xaction;
|
2012-02-24 22:00:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $xactions;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|