1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-28 01:32:42 +01:00
phorge-phorge/src/applications/almanac/controller/AlmanacPropertyEditController.php

96 lines
2.6 KiB
PHP
Raw Normal View History

2014-11-06 00:27:16 +01:00
<?php
final class AlmanacPropertyEditController
extends AlmanacPropertyController {
2014-11-06 00:27:16 +01:00
public function handleRequest(AphrontRequest $request) {
$viewer = $request->getViewer();
$response = $this->loadPropertyObject();
if ($response) {
return $response;
}
2014-11-06 00:27:16 +01:00
$object = $this->getPropertyObject();
$cancel_uri = $object->getURI();
$property_key = $request->getStr('key');
if (!strlen($property_key)) {
return $this->buildPropertyKeyResponse($cancel_uri, null);
2014-11-06 00:27:16 +01:00
} else {
$error = null;
try {
AlmanacNames::validateName($property_key);
} catch (Exception $ex) {
$error = $ex->getMessage();
}
2014-11-06 00:27:16 +01:00
// NOTE: If you enter an existing name, we're just treating it as an
// edit operation. This might be a little confusing.
2014-11-06 00:27:16 +01:00
if ($error !== null) {
if ($request->isFormPost()) {
// The user is creating a new property and picked a bad name. Give
// them an opportunity to fix it.
return $this->buildPropertyKeyResponse($cancel_uri, $error);
2014-11-06 00:27:16 +01:00
} else {
// The user is editing an invalid property.
return $this->newDialog()
->setTitle(pht('Invalid Property'))
->appendParagraph(
pht(
'The property name "%s" is invalid. This property can not '.
'be edited.',
$property_key))
->appendParagraph($error)
->addCancelButton($cancel_uri);
2014-11-06 00:27:16 +01:00
}
}
}
return $object->newAlmanacPropertyEditEngine()
->addContextParameter('objectPHID')
->addContextParameter('key')
->setTargetObject($object)
->setPropertyKey($property_key)
->setController($this)
->buildResponse();
}
2014-11-06 00:27:16 +01:00
private function buildPropertyKeyResponse($cancel_uri, $error) {
$viewer = $this->getViewer();
$request = $this->getRequest();
$v_key = $request->getStr('key');
2014-11-06 00:27:16 +01:00
if ($error !== null) {
$e_key = pht('Invalid');
} else {
$e_key = true;
2014-11-06 00:27:16 +01:00
}
$form = id(new AphrontFormView())
->setUser($viewer)
->appendChild(
id(new AphrontFormTextControl())
->setName('key')
->setLabel(pht('Name'))
->setValue($v_key)
->setError($e_key));
$errors = array();
if ($error !== null) {
$errors[] = $error;
}
2014-11-06 00:27:16 +01:00
return $this->newDialog()
->setTitle(pht('Add Property'))
->addHiddenInput('objectPHID', $request->getStr('objectPHID'))
->setErrors($errors)
2014-11-06 00:27:16 +01:00
->appendForm($form)
->addSubmitButton(pht('Continue'))
2014-11-06 00:27:16 +01:00
->addCancelButton($cancel_uri);
}
}