@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
at recaptime-dev/main 95 lines 2.7 kB view raw
1<?php 2 3final class AlmanacPropertyEditController 4 extends AlmanacPropertyController { 5 6 public function handleRequest(AphrontRequest $request) { 7 $viewer = $request->getViewer(); 8 9 $response = $this->loadPropertyObject(); 10 if ($response) { 11 return $response; 12 } 13 14 $object = $this->getPropertyObject(); 15 16 $cancel_uri = $object->getURI(); 17 $property_key = $request->getStr('key'); 18 19 if (!phutil_nonempty_string($property_key)) { 20 return $this->buildPropertyKeyResponse($cancel_uri, null); 21 } else { 22 $error = null; 23 try { 24 AlmanacNames::validateName($property_key); 25 } catch (Exception $ex) { 26 $error = $ex->getMessage(); 27 } 28 29 // NOTE: If you enter an existing name, we're just treating it as an 30 // edit operation. This might be a little confusing. 31 32 if ($error !== null) { 33 if ($request->isFormPost()) { 34 // The user is creating a new property and picked a bad name. Give 35 // them an opportunity to fix it. 36 return $this->buildPropertyKeyResponse($cancel_uri, $error); 37 } else { 38 // The user is editing an invalid property. 39 return $this->newDialog() 40 ->setTitle(pht('Invalid Property')) 41 ->appendParagraph( 42 pht( 43 'The property name "%s" is invalid. This property can not '. 44 'be edited.', 45 $property_key)) 46 ->appendParagraph($error) 47 ->addCancelButton($cancel_uri); 48 } 49 } 50 } 51 52 return $object->newAlmanacPropertyEditEngine() 53 ->addContextParameter('objectPHID') 54 ->addContextParameter('key') 55 ->setTargetObject($object) 56 ->setPropertyKey($property_key) 57 ->setController($this) 58 ->buildResponse(); 59 } 60 61 private function buildPropertyKeyResponse($cancel_uri, $error) { 62 $viewer = $this->getViewer(); 63 $request = $this->getRequest(); 64 $v_key = $request->getStr('key'); 65 66 if ($error !== null) { 67 $e_key = pht('Invalid'); 68 } else { 69 $e_key = true; 70 } 71 72 $form = id(new AphrontFormView()) 73 ->setUser($viewer) 74 ->appendChild( 75 id(new AphrontFormTextControl()) 76 ->setName('key') 77 ->setLabel(pht('Name')) 78 ->setValue($v_key) 79 ->setError($e_key)); 80 81 $errors = array(); 82 if ($error !== null) { 83 $errors[] = $error; 84 } 85 86 return $this->newDialog() 87 ->setTitle(pht('Add Property')) 88 ->addHiddenInput('objectPHID', $request->getStr('objectPHID')) 89 ->setErrors($errors) 90 ->appendForm($form) 91 ->addSubmitButton(pht('Continue')) 92 ->addCancelButton($cancel_uri); 93 } 94 95}