|
| 1 | +<?php |
| 2 | + |
| 3 | +final class PhabricatorProjectBoardEditController |
| 4 | + extends PhabricatorProjectController { |
| 5 | + |
| 6 | + private $id; |
| 7 | + private $projectID; |
| 8 | + |
| 9 | + public function willProcessRequest(array $data) { |
| 10 | + $this->projectID = $data['projectID']; |
| 11 | + $this->id = idx($data, 'id'); |
| 12 | + } |
| 13 | + |
| 14 | + public function processRequest() { |
| 15 | + $request = $this->getRequest(); |
| 16 | + $viewer = $request->getUser(); |
| 17 | + |
| 18 | + $project = id(new PhabricatorProjectQuery()) |
| 19 | + ->setViewer($viewer) |
| 20 | + ->requireCapabilities( |
| 21 | + array( |
| 22 | + PhabricatorPolicyCapability::CAN_VIEW, |
| 23 | + PhabricatorPolicyCapability::CAN_EDIT, |
| 24 | + )) |
| 25 | + ->withIDs(array($this->projectID)) |
| 26 | + ->executeOne(); |
| 27 | + |
| 28 | + if (!$project) { |
| 29 | + return new Aphront404Response(); |
| 30 | + } |
| 31 | + |
| 32 | + $is_new = ($this->id ? false : true); |
| 33 | + |
| 34 | + if (!$is_new) { |
| 35 | + // TODO: LATER! |
| 36 | + throw new Exception("When I'm ready!"); |
| 37 | + } else { |
| 38 | + $column = new PhabricatorProjectColumn(); |
| 39 | + } |
| 40 | + |
| 41 | + $errors = array(); |
| 42 | + $e_name = true; |
| 43 | + $error_view = null; |
| 44 | + $view_uri = $this->getApplicationURI('/board/'.$this->projectID.'/'); |
| 45 | + |
| 46 | + if ($request->isFormPost()) { |
| 47 | + $new_name = $request->getStr('name'); |
| 48 | + $column->setName($new_name); |
| 49 | + |
| 50 | + if (!strlen($column->getName())) { |
| 51 | + $errors[] = pht('Column name is required.'); |
| 52 | + $e_name = pht('Required'); |
| 53 | + } else { |
| 54 | + $e_name = null; |
| 55 | + } |
| 56 | + |
| 57 | + $column->setProjectPHID($project->getPHID()); |
| 58 | + $column->attachProject($project); |
| 59 | + $column->setSequence(0); |
| 60 | + |
| 61 | + if (!$errors) { |
| 62 | + $column->save(); |
| 63 | + return id(new AphrontRedirectResponse())->setURI($view_uri); |
| 64 | + } |
| 65 | + } |
| 66 | + if ($errors) { |
| 67 | + $error_view = new AphrontErrorView(); |
| 68 | + $error_view->setTitle(pht('Form Errors')); |
| 69 | + $error_view->setErrors($errors); |
| 70 | + } |
| 71 | + |
| 72 | + $form = new AphrontFormView(); |
| 73 | + $form->setUser($request->getUser()) |
| 74 | + ->appendChild( |
| 75 | + id(new AphrontFormTextControl()) |
| 76 | + ->setValue($column->getName()) |
| 77 | + ->setLabel(pht('Name')) |
| 78 | + ->setName('name') |
| 79 | + ->setError($e_name) |
| 80 | + ->setCaption( |
| 81 | + pht('This will be displayed as the header of the column.'))); |
| 82 | + |
| 83 | + if ($is_new) { |
| 84 | + $title = pht('Create Column'); |
| 85 | + $submit = pht('Create Column'); |
| 86 | + } else { |
| 87 | + $title = pht('Edit %s', $column->getName()); |
| 88 | + $submit = pht('Save Column'); |
| 89 | + } |
| 90 | + |
| 91 | + $form->appendChild( |
| 92 | + id(new AphrontFormSubmitControl()) |
| 93 | + ->setValue($submit) |
| 94 | + ->addCancelButton($view_uri)); |
| 95 | + |
| 96 | + $crumbs = $this->buildApplicationCrumbs(); |
| 97 | + $crumbs->addTextCrumb($title); |
| 98 | + |
| 99 | + $form_box = id(new PHUIObjectBoxView()) |
| 100 | + ->setHeaderText($title) |
| 101 | + ->setFormError($errors) |
| 102 | + ->setForm($form); |
| 103 | + |
| 104 | + return $this->buildApplicationPage( |
| 105 | + array( |
| 106 | + $crumbs, |
| 107 | + $form_box, |
| 108 | + ), |
| 109 | + array( |
| 110 | + 'title' => $title, |
| 111 | + 'device' => true, |
| 112 | + )); |
| 113 | + } |
| 114 | +} |
0 commit comments