[ Index ] |
PHP Cross Reference of Phabricator |
[Summary view] [Print] [Text view]
1 <?php 2 3 final class PhabricatorProjectBoardReorderController 4 extends PhabricatorProjectBoardController { 5 6 private $projectID; 7 8 public function willProcessRequest(array $data) { 9 $this->projectID = $data['projectID']; 10 } 11 12 public function processRequest() { 13 $request = $this->getRequest(); 14 $viewer = $request->getUser(); 15 16 $project = id(new PhabricatorProjectQuery()) 17 ->setViewer($viewer) 18 ->requireCapabilities( 19 array( 20 PhabricatorPolicyCapability::CAN_VIEW, 21 PhabricatorPolicyCapability::CAN_EDIT, 22 )) 23 ->withIDs(array($this->projectID)) 24 ->executeOne(); 25 if (!$project) { 26 return new Aphront404Response(); 27 } 28 29 $this->setProject($project); 30 31 32 $project_id = $project->getID(); 33 34 $board_uri = $this->getApplicationURI("board/{$project_id}/"); 35 $reorder_uri = $this->getApplicationURI("board/{$project_id}/reorder/"); 36 37 if ($request->isFormPost()) { 38 // User clicked "Done", make sure the page reloads to show the new 39 // column order. 40 return id(new AphrontRedirectResponse())->setURI($board_uri); 41 } 42 43 $columns = id(new PhabricatorProjectColumnQuery()) 44 ->setViewer($viewer) 45 ->withProjectPHIDs(array($project->getPHID())) 46 ->execute(); 47 $columns = msort($columns, 'getSequence'); 48 49 $column_phid = $request->getStr('columnPHID'); 50 if ($column_phid && $request->validateCSRF()) { 51 52 $columns = mpull($columns, null, 'getPHID'); 53 if (empty($columns[$column_phid])) { 54 return new Aphront404Response(); 55 } 56 57 $target_column = $columns[$column_phid]; 58 $new_sequence = $request->getInt('sequence'); 59 if ($new_sequence < 0) { 60 return new Aphront404Response(); 61 } 62 63 // TODO: For now, we're not recording any transactions here. We probably 64 // should, but this sort of edit is extremely trivial. 65 66 // Resequence the columns so that the moved column has the correct 67 // sequence number. Move columns after it up one place in the sequence. 68 $new_map = array(); 69 foreach ($columns as $phid => $column) { 70 $value = $column->getSequence(); 71 if ($column->getPHID() == $column_phid) { 72 $value = $new_sequence; 73 } else if ($column->getSequence() >= $new_sequence) { 74 $value = $value + 1; 75 } 76 $new_map[$phid] = $value; 77 } 78 79 // Sort the columns into their new ordering. 80 asort($new_map); 81 82 // Now, compact the ordering and adjust any columns that need changes. 83 $project->openTransaction(); 84 $sequence = 0; 85 foreach ($new_map as $phid => $ignored) { 86 $new_value = $sequence++; 87 $cur_value = $columns[$phid]->getSequence(); 88 if ($new_value != $cur_value) { 89 $columns[$phid]->setSequence($new_value)->save(); 90 } 91 } 92 $project->saveTransaction(); 93 94 return id(new AphrontAjaxResponse())->setContent( 95 array( 96 'sequenceMap' => mpull($columns, 'getSequence', 'getPHID'), 97 )); 98 } 99 100 $list_id = celerity_generate_unique_node_id(); 101 102 $list = id(new PHUIObjectItemListView()) 103 ->setUser($viewer) 104 ->setID($list_id) 105 ->setFlush(true) 106 ->setStackable(true); 107 108 foreach ($columns as $column) { 109 $item = id(new PHUIObjectItemView()) 110 ->setHeader($column->getDisplayName()) 111 ->addIcon('none', $column->getDisplayType()); 112 113 if ($column->isHidden()) { 114 $item->setDisabled(true); 115 } 116 117 $item->setGrippable(true); 118 $item->addSigil('board-column'); 119 $item->setMetadata( 120 array( 121 'columnPHID' => $column->getPHID(), 122 'columnSequence' => $column->getSequence(), 123 )); 124 125 $list->addItem($item); 126 } 127 128 Javelin::initBehavior( 129 'reorder-columns', 130 array( 131 'listID' => $list_id, 132 'reorderURI' => $reorder_uri, 133 )); 134 135 return $this->newDialog() 136 ->setTitle(pht('Reorder Columns')) 137 ->setWidth(AphrontDialogView::WIDTH_FORM) 138 ->appendParagraph(pht('Drag and drop columns to reorder them.')) 139 ->appendChild($list) 140 ->addSubmitButton(pht('Done')); 141 } 142 143 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sun Nov 30 09:20:46 2014 | Cross-referenced by PHPXref 0.7.1 |