Differential D13626 Diff 32928 src/applications/badges/controller/PhabricatorBadgesEditController.php
Changeset View
Changeset View
Standalone View
Standalone View
src/applications/badges/controller/PhabricatorBadgesEditController.php
- This file was added.
<?php | |||||
final class PhabricatorBadgesEditController | |||||
extends PhabricatorBadgesController { | |||||
private $id; | |||||
public function willProcessRequest(array $data) { | |||||
$this->id = idx($data, 'id'); | |||||
} | |||||
public function processRequest() { | |||||
$request = $this->getRequest(); | |||||
epriestley: Per elsewhere, prefer getURIData(). | |||||
$viewer = $request->getUser(); | |||||
Done Inline ActionshandleRequest / getViewer() (see below) epriestley: handleRequest / getViewer() (see below) | |||||
if ($this->id) { | |||||
$badge = id(new PhabricatorBadgesQuery()) | |||||
->setViewer($viewer) | |||||
->withIDs(array($this->id)) | |||||
->requireCapabilities( | |||||
array( | |||||
PhabricatorPolicyCapability::CAN_VIEW, | |||||
PhabricatorPolicyCapability::CAN_EDIT, | |||||
)) | |||||
->executeOne(); | |||||
if (!$badge) { | |||||
return new Aphront404Response(); | |||||
} | |||||
$is_new = false; | |||||
} else { | |||||
$badge = PhabricatorBadge::initializeNewBadge($viewer); | |||||
$is_new = true; | |||||
} | |||||
if ($is_new) { | |||||
$title = pht('Create Badge'); | |||||
$button_text = pht('Create Badge'); | |||||
$cancel_uri = $this->getApplicationURI(); | |||||
} else { | |||||
$title = pht( | |||||
'Edit %s', | |||||
$badge->getName()); | |||||
$button_text = pht('Save Changes'); | |||||
$cancel_uri = $this->getApplicationURI().'view/'.$this->id.'/'; | |||||
Done Inline ActionsPrefer getApplicationURI('x') over getApplicationURI().'x'. epriestley: Prefer `getApplicationURI('x')` over `getApplicationURI().'x'`. | |||||
} | |||||
$e_name = true; | |||||
$v_name = $badge->getName(); | |||||
$e_icon = true; | |||||
$v_icon = $badge->getIcon(); | |||||
Done Inline ActionsNever used. epriestley: Never used. | |||||
$v_flav = $badge->getFlavor(); | |||||
$v_desc = $badge->getDescription(); | |||||
$v_qual = $badge->getQuality(); | |||||
$v_stat = $badge->getStatus(); | |||||
$validation_exception = null; | |||||
if ($request->isFormPost()) { | |||||
$v_name = $request->getStr('name'); | |||||
$v_flav = $request->getStr('flavor'); | |||||
$v_desc = $request->getStr('description'); | |||||
$v_icon = $request->getStr('icon'); | |||||
$v_stat = $request->getStr('status'); | |||||
$v_qual = $request->getStr('quality'); | |||||
$v_view = $request->getStr('viewPolicy'); | |||||
$v_edit = $request->getStr('editPolicy'); | |||||
$type_name = PhabricatorBadgesTransaction::TYPE_NAME; | |||||
$type_flav = PhabricatorBadgesTransaction::TYPE_FLAVOR; | |||||
$type_desc = PhabricatorBadgesTransaction::TYPE_DESCRIPTION; | |||||
$type_icon = PhabricatorBadgesTransaction::TYPE_ICON; | |||||
$type_qual = PhabricatorBadgesTransaction::TYPE_QUALITY; | |||||
$type_stat = PhabricatorBadgesTransaction::TYPE_STATUS; | |||||
$type_view = PhabricatorTransactions::TYPE_VIEW_POLICY; | |||||
$type_edit = PhabricatorTransactions::TYPE_EDIT_POLICY; | |||||
$xactions = array(); | |||||
$xactions[] = id(new PhabricatorBadgesTransaction()) | |||||
->setTransactionType($type_name) | |||||
->setNewValue($v_name); | |||||
$xactions[] = id(new PhabricatorBadgesTransaction()) | |||||
->setTransactionType($type_flav) | |||||
->setNewValue($v_flav); | |||||
$xactions[] = id(new PhabricatorBadgesTransaction()) | |||||
->setTransactionType($type_desc) | |||||
->setNewValue($v_desc); | |||||
$xactions[] = id(new PhabricatorBadgesTransaction()) | |||||
->setTransactionType($type_icon) | |||||
->setNewValue($v_icon); | |||||
$xactions[] = id(new PhabricatorBadgesTransaction()) | |||||
->setTransactionType($type_qual) | |||||
->setNewValue($v_qual); | |||||
$xactions[] = id(new PhabricatorBadgesTransaction()) | |||||
->setTransactionType($type_stat) | |||||
->setNewValue($v_stat); | |||||
$xactions[] = id(new PhabricatorBadgesTransaction()) | |||||
->setTransactionType($type_view) | |||||
->setNewValue($v_view); | |||||
$xactions[] = id(new PhabricatorBadgesTransaction()) | |||||
->setTransactionType($type_edit) | |||||
->setNewValue($v_edit); | |||||
$editor = id(new PhabricatorBadgesEditor()) | |||||
->setActor($viewer) | |||||
->setContentSourceFromRequest($request) | |||||
->setContinueOnNoEffect(true); | |||||
try { | |||||
$editor->applyTransactions($badge, $xactions); | |||||
return id(new AphrontRedirectResponse()) | |||||
->setURI('/badges/'); | |||||
} catch (PhabricatorApplicationTransactionValidationException $ex) { | |||||
$validation_exception = $ex; | |||||
$e_name = $ex->getShortMessage($type_name); | |||||
$e_desc = $ex->getShortMessage($type_desc); | |||||
Done Inline ActionsThis is inconsistent with other applications, and should always redirect to the badge detail page (with ID determined by $badge->getID()). epriestley: This is inconsistent with other applications, and should always redirect to the badge detail… | |||||
$badge->setViewPolicy($v_view); | |||||
$badge->setEditPolicy($v_edit); | |||||
} | |||||
} | |||||
Done Inline ActionsNever used. epriestley: Never used. | |||||
if ($is_new) { | |||||
$icon_uri = $this->getApplicationURI('icon/'); | |||||
Done Inline ActionsThese should just be set on the controls instead of on the object. epriestley: These should just be set on the controls instead of on the object. | |||||
} else { | |||||
$icon_uri = $this->getApplicationURI('icon/'.$badge->getID().'/'); | |||||
} | |||||
$icon_display = PhabricatorBadgesIcon::renderIconForChooser($v_icon); | |||||
$policies = id(new PhabricatorPolicyQuery()) | |||||
->setViewer($viewer) | |||||
->setObject($badge) | |||||
->execute(); | |||||
$form = id(new AphrontFormView()) | |||||
->setUser($viewer) | |||||
->appendChild( | |||||
id(new AphrontFormTextControl()) | |||||
->setName('name') | |||||
->setLabel(pht('Name')) | |||||
->setValue($v_name) | |||||
->setError($e_name)) | |||||
->appendChild( | |||||
id(new AphrontFormTextControl()) | |||||
->setName('flavor') | |||||
->setLabel(pht('Flavor Text')) | |||||
->setValue($v_flav)) | |||||
->appendChild( | |||||
id(new AphrontFormChooseButtonControl()) | |||||
->setLabel(pht('Icon')) | |||||
->setName('icon') | |||||
->setDisplayValue($icon_display) | |||||
->setButtonText(pht('Choose Icon...')) | |||||
->setChooseURI($icon_uri) | |||||
->setValue($v_icon)) | |||||
->appendChild( | |||||
id(new AphrontFormSelectControl()) | |||||
->setName('quality') | |||||
->setLabel(pht('Quality')) | |||||
->setValue($v_qual) | |||||
->setOptions($badge->getQualityNameMap())) | |||||
->appendChild( | |||||
id(new AphrontFormSelectControl()) | |||||
->setLabel(pht('Status')) | |||||
->setName('status') | |||||
->setValue($v_stat) | |||||
->setOptions($badge->getStatusNameMap())) | |||||
->appendChild( | |||||
id(new PhabricatorRemarkupControl()) | |||||
->setUser($viewer) | |||||
->setName('description') | |||||
->setLabel(pht('Description')) | |||||
->setValue($v_desc)) | |||||
->appendChild( | |||||
id(new AphrontFormPolicyControl()) | |||||
->setName('viewPolicy') | |||||
->setPolicyObject($badge) | |||||
->setCapability(PhabricatorPolicyCapability::CAN_VIEW) | |||||
->setPolicies($policies)) | |||||
->appendChild( | |||||
id(new AphrontFormPolicyControl()) | |||||
->setName('editPolicy') | |||||
->setPolicyObject($badge) | |||||
->setCapability(PhabricatorPolicyCapability::CAN_EDIT) | |||||
->setPolicies($policies)) | |||||
->appendChild( | |||||
id(new AphrontFormSubmitControl()) | |||||
->setValue($button_text) | |||||
->addCancelButton($cancel_uri)); | |||||
$crumbs = $this->buildApplicationCrumbs(); | |||||
if ($is_new) { | |||||
$crumbs->addTextCrumb(pht('Create Badge')); | |||||
} else { | |||||
$crumbs->addTextCrumb( | |||||
$badge->getName(), | |||||
'/badges/view/'.$badge->getID().'/'); | |||||
$crumbs->addTextCrumb(pht('Edit')); | |||||
} | |||||
$box = id(new PHUIObjectBoxView()) | |||||
->setValidationException($validation_exception) | |||||
->setHeaderText($title) | |||||
->appendChild($form); | |||||
return $this->buildApplicationPage( | |||||
array( | |||||
$crumbs, | |||||
$box, | |||||
), | |||||
array( | |||||
'title' => $title, | |||||
)); | |||||
} | |||||
} |
Per elsewhere, prefer getURIData().