1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-12-04 12:42:43 +01:00
phorge-phorge/src/applications/project/trigger/PhabricatorProjectTriggerManiphestOwnerRule.php
epriestley 870b01f2d0 Distinguish between "bad record format" and "bad record value" when validating Trigger rules
Summary:
Depends on D20416. Ref T13269. See D20329.

If you try to save an "Assign to" rule with no assignee, we currently replace the control with an "InvalidRule" control that isn't editable. We'd prefer to give you an empty field back and let you pick a different value.

Differentiate between "bad record format" (i.e., we can't really do anything with this) and "bad record value" (i.e., everything is structurally fine, you just typed the wrong thing). In the latter case, we still build a properly typed rule for the UI, we just refuse to update storage until you fix the problem.

Test Plan:
First, hit the original issue and got a nicer UI with a more consistent control width (note full-width error):

{F6374205}

Then, applied the rest of the patch and got a normal "fix the issue" form state instead of a dead-end:

{F6374211}

Reviewers: amckinley

Reviewed By: amckinley

Maniphest Tasks: T13269

Differential Revision: https://secure.phabricator.com/D20417
2019-04-17 12:40:55 -07:00

148 lines
3.7 KiB
PHP

<?php
final class PhabricatorProjectTriggerManiphestOwnerRule
extends PhabricatorProjectTriggerRule {
const TRIGGERTYPE = 'task.owner';
public function getSelectControlName() {
return pht('Assign task to');
}
protected function getValueForEditorField() {
return $this->getDatasource()->getWireTokens($this->getValue());
}
private function convertTokenizerValueToOwner($value) {
$value = head($value);
if ($value === PhabricatorPeopleNoOwnerDatasource::FUNCTION_TOKEN) {
$value = null;
}
return $value;
}
protected function assertValidRuleRecordFormat($value) {
if (!is_array($value)) {
throw new Exception(
pht(
'Owner rule value should be a list, but is not (value is "%s").',
phutil_describe_type($value)));
}
}
protected function assertValidRuleRecordValue($value) {
if (!$value) {
throw new Exception(
pht(
'Owner rule value is required. Specify a user to assign tasks '.
'to, or the token "none()" to unassign tasks.'));
}
if (count($value) > 1) {
throw new Exception(
pht(
'Owner rule value must have only one elmement (value is "%s").',
implode(', ', $value)));
}
$owner_phid = $this->convertTokenizerValueToOwner($value);
if ($owner_phid !== null) {
$user = id(new PhabricatorPeopleQuery())
->setViewer($this->getViewer())
->withPHIDs(array($owner_phid))
->executeOne();
if (!$user) {
throw new Exception(
pht(
'User PHID ("%s") is not a valid user.',
$owner_phid));
}
}
}
protected function newDropTransactions($object, $value) {
$value = $this->convertTokenizerValueToOwner($value);
return array(
$this->newTransaction()
->setTransactionType(ManiphestTaskOwnerTransaction::TRANSACTIONTYPE)
->setNewValue($value),
);
}
protected function newDropEffects($value) {
$owner_value = $this->convertTokenizerValueToOwner($value);
return array(
$this->newEffect()
->setIcon('fa-user')
->setContent($this->getRuleViewDescription($value))
->addCondition('owner', '!=', $owner_value),
);
}
protected function getDefaultValue() {
return null;
}
protected function getPHUIXControlType() {
return 'tokenizer';
}
private function getDatasource() {
$datasource = id(new ManiphestAssigneeDatasource())
->setLimit(1);
if ($this->getViewer()) {
$datasource->setViewer($this->getViewer());
}
return $datasource;
}
protected function getPHUIXControlSpecification() {
$template = id(new AphrontTokenizerTemplateView())
->setViewer($this->getViewer());
$template_markup = $template->render();
$datasource = $this->getDatasource();
return array(
'markup' => (string)hsprintf('%s', $template_markup),
'config' => array(
'src' => $datasource->getDatasourceURI(),
'browseURI' => $datasource->getBrowseURI(),
'placeholder' => $datasource->getPlaceholderText(),
'limit' => $datasource->getLimit(),
),
'value' => null,
);
}
public function getRuleViewLabel() {
return pht('Change Owner');
}
public function getRuleViewDescription($value) {
$value = $this->convertTokenizerValueToOwner($value);
if (!$value) {
return pht('Unassign task.');
} else {
return pht(
'Assign task to %s.',
phutil_tag(
'strong',
array(),
$this->getViewer()
->renderHandle($value)
->render()));
}
}
public function getRuleViewIcon($value) {
return id(new PHUIIconView())
->setIcon('fa-user', 'green');
}
}