Skip to content

Commit ce16353

Browse files
author
epriestley
committedAug 8, 2013
Add a normal "view" page for Herald rules
Summary: Ref T2769. This will house the transaction list and replace the "edit log" stuff. The UI is a little bit rough and can probably share more code with the transaction history, but seems mostly-reasonable. Test Plan: {F53253} Reviewers: btrahan Reviewed By: btrahan CC: aran Maniphest Tasks: T2769 Differential Revision: https://secure.phabricator.com/D6690
1 parent b767bd3 commit ce16353

6 files changed

+199
-11
lines changed
 

‎src/__phutil_library_map__.php

+2
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,7 @@
628628
'HeraldRuleTransactionComment' => 'applications/herald/storage/HeraldRuleTransactionComment.php',
629629
'HeraldRuleTranscript' => 'applications/herald/storage/transcript/HeraldRuleTranscript.php',
630630
'HeraldRuleTypeConfig' => 'applications/herald/config/HeraldRuleTypeConfig.php',
631+
'HeraldRuleViewController' => 'applications/herald/controller/HeraldRuleViewController.php',
631632
'HeraldTestConsoleController' => 'applications/herald/controller/HeraldTestConsoleController.php',
632633
'HeraldTranscript' => 'applications/herald/storage/transcript/HeraldTranscript.php',
633634
'HeraldTranscriptController' => 'applications/herald/controller/HeraldTranscriptController.php',
@@ -2640,6 +2641,7 @@
26402641
'HeraldRuleSearchEngine' => 'PhabricatorApplicationSearchEngine',
26412642
'HeraldRuleTransaction' => 'PhabricatorApplicationTransaction',
26422643
'HeraldRuleTransactionComment' => 'PhabricatorApplicationTransactionComment',
2644+
'HeraldRuleViewController' => 'HeraldController',
26432645
'HeraldTestConsoleController' => 'HeraldController',
26442646
'HeraldTranscript' => 'HeraldDAO',
26452647
'HeraldTranscriptController' => 'HeraldController',

‎src/applications/herald/adapter/HeraldAdapter.php

+70
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,76 @@ public static function getEnabledAdapterMap() {
663663
}
664664

665665

666+
public function renderRuleAsText(HeraldRule $rule) {
667+
$out = array();
668+
669+
if ($rule->getMustMatchAll()) {
670+
$out[] = pht('When all of these conditions are met:');
671+
} else {
672+
$out[] = pht('When any of these conditions are met:');
673+
}
674+
675+
$out[] = null;
676+
foreach ($rule->getConditions() as $condition) {
677+
$out[] = " ".$this->renderConditionAsText($condition);
678+
}
679+
$out[] = null;
680+
681+
if ($rule->getRepetitionPolicy() == HeraldRepetitionPolicyConfig::EVERY) {
682+
$out[] = pht('Take these actions every time this rule matches:');
683+
} else {
684+
$out[] = pht('Take these actions the first time this rule matches:');
685+
}
686+
687+
$out[] = null;
688+
foreach ($rule->getActions() as $action) {
689+
$out[] = " ".$this->renderActionAsText($action);
690+
}
691+
692+
return implode("\n", $out);
693+
}
694+
695+
private function renderConditionAsText(HeraldCondition $condition) {
696+
$field_type = $condition->getFieldName();
697+
$field_name = idx($this->getFieldNameMap(), $field_type);
698+
699+
$condition_type = $condition->getFieldCondition();
700+
$condition_name = idx($this->getConditionNameMap(), $condition_type);
701+
702+
$value = $this->renderConditionValueAsText($condition);
703+
704+
return "{$field_name} {$condition_name} {$value}";
705+
}
706+
707+
private function renderActionAsText(HeraldAction $action) {
708+
$rule_global = HeraldRuleTypeConfig::RULE_TYPE_GLOBAL;
709+
710+
$action_type = $action->getAction();
711+
$action_name = idx($this->getActionNameMap($rule_global), $action_type);
712+
713+
$target = $this->renderActionTargetAsText($action);
714+
715+
return "{$action_name} {$target}";
716+
}
717+
718+
private function renderConditionValueAsText(HeraldCondition $condition) {
719+
// TODO: This produces sketchy results for many conditions.
720+
$value = $condition->getValue();
721+
if (is_array($value)) {
722+
$value = implode(', ', $value);
723+
}
724+
return $value;
725+
}
726+
727+
private function renderActionTargetAsText(HeraldAction $action) {
728+
// TODO: This produces sketchy results for Flags and PHIDs.
729+
$target = $action->getTarget();
730+
if (is_array($target)) {
731+
$target = implode(', ', $target);
732+
}
733+
734+
return $target;
735+
}
666736

667737
}
668738

‎src/applications/herald/application/PhabricatorApplicationHerald.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ public function getRoutes() {
3636
'(?:query/(?P<queryKey>[^/]+)/)?' => 'HeraldRuleListController',
3737
'new/(?:(?P<type>[^/]+)/(?:(?P<rule_type>[^/]+)/)?)?'
3838
=> 'HeraldNewController',
39-
'rule/(?:(?P<id>[1-9]\d*)/)?' => 'HeraldRuleController',
39+
'rule/(?P<id>[1-9]\d*)/' => 'HeraldRuleViewController',
40+
'edit/(?:(?P<id>[1-9]\d*)/)?' => 'HeraldRuleController',
4041
'history/(?:(?P<id>[1-9]\d*)/)?' => 'HeraldRuleEditHistoryController',
4142
'delete/(?P<id>[1-9]\d*)/' => 'HeraldDeleteController',
4243
'test/' => 'HeraldTestConsoleController',

‎src/applications/herald/controller/HeraldNewController.php

+3-5
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function processRequest() {
5656

5757
$form = id(new AphrontFormView())
5858
->setUser($user)
59-
->setAction('/herald/rule/')
59+
->setAction('/herald/edit/')
6060
->setFlexible(true)
6161
->appendChild(
6262
id(new AphrontFormSelectControl())
@@ -68,15 +68,13 @@ public function processRequest() {
6868
->appendChild(
6969
id(new AphrontFormSubmitControl())
7070
->setValue(pht('Create Rule'))
71-
->addCancelButton('/herald/view/'.$this->contentType.'/'));
71+
->addCancelButton($this->getApplicationURI()));
7272

7373
$crumbs = $this
7474
->buildApplicationCrumbs()
7575
->addCrumb(
7676
id(new PhabricatorCrumbView())
77-
->setName(pht('Create Herald Rule'))
78-
->setHref($this->getApplicationURI(
79-
'view/'.$this->contentType.'/'.$this->ruleType)));
77+
->setName(pht('Create Rule')));
8078

8179
return $this->buildApplicationPage(
8280
array(

‎src/applications/herald/controller/HeraldRuleController.php

+8-5
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ public function processRequest() {
1818
$rule_type_map = HeraldRuleTypeConfig::getRuleTypeMap();
1919

2020
if ($this->id) {
21+
$id = $this->id;
2122
$rule = id(new HeraldRuleQuery())
2223
->setViewer($user)
23-
->withIDs(array($this->id))
24+
->withIDs(array($id))
2425
->requireCapabilities(
2526
array(
2627
PhabricatorPolicyCapability::CAN_VIEW,
@@ -30,6 +31,7 @@ public function processRequest() {
3031
if (!$rule) {
3132
return new Aphront404Response();
3233
}
34+
$cancel_uri = $this->getApplicationURI("rule/{$id}/");
3335
} else {
3436
$rule = new HeraldRule();
3537
$rule->setAuthorPHID($user->getPHID());
@@ -43,6 +45,8 @@ public function processRequest() {
4345
$rule_type = HeraldRuleTypeConfig::RULE_TYPE_GLOBAL;
4446
}
4547
$rule->setRuleType($rule_type);
48+
49+
$cancel_uri = $this->getApplicationURI();
4650
}
4751

4852
$adapter = HeraldAdapter::getAdapterForContentType($rule->getContentType());
@@ -70,9 +74,8 @@ public function processRequest() {
7074
if ($request->isFormPost() && $request->getStr('save')) {
7175
list($e_name, $errors) = $this->saveRule($adapter, $rule, $request);
7276
if (!$errors) {
73-
$uri = '/herald/view/'.
74-
$rule->getContentType().'/'.
75-
$rule->getRuleType().'/';
77+
$id = $rule->getID();
78+
$uri = $this->getApplicationURI("rule/{$id}/");
7679
return id(new AphrontRedirectResponse())->setURI($uri);
7780
}
7881
}
@@ -171,7 +174,7 @@ public function processRequest() {
171174
->appendChild(
172175
id(new AphrontFormSubmitControl())
173176
->setValue(pht('Save Rule'))
174-
->addCancelButton('/herald/view/'.$rule->getContentType().'/'));
177+
->addCancelButton($cancel_uri));
175178

176179
$this->setupEditorBehavior($rule, $handles, $adapter);
177180

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php
2+
3+
final class HeraldRuleViewController extends HeraldController {
4+
5+
private $id;
6+
7+
public function willProcessRequest(array $data) {
8+
$this->id = $data['id'];
9+
}
10+
11+
public function processRequest() {
12+
$request = $this->getRequest();
13+
$viewer = $request->getUser();
14+
15+
$rule = id(new HeraldRuleQuery())
16+
->setViewer($viewer)
17+
->withIDs(array($this->id))
18+
->needConditionsAndActions(true)
19+
->executeOne();
20+
if (!$rule) {
21+
return new Aphront404Response();
22+
}
23+
24+
$header = id(new PhabricatorHeaderView())
25+
->setHeader($rule->getName());
26+
27+
$actions = $this->buildActionView($rule);
28+
$properties = $this->buildPropertyView($rule);
29+
30+
$crumbs = $this->buildApplicationCrumbs();
31+
$crumbs->addCrumb(
32+
id(new PhabricatorCrumbView())
33+
->setName(pht('Rule %d', $rule->getID())));
34+
35+
return $this->buildApplicationPage(
36+
array(
37+
$crumbs,
38+
$header,
39+
$actions,
40+
$properties,
41+
),
42+
array(
43+
'title' => $rule->getName(),
44+
'device' => true,
45+
'dust' => true,
46+
));
47+
}
48+
49+
private function buildActionView(HeraldRule $rule) {
50+
$viewer = $this->getRequest()->getUser();
51+
$id = $rule->getID();
52+
53+
$view = id(new PhabricatorActionListView())
54+
->setUser($viewer)
55+
->setObject($rule)
56+
->setObjectURI($this->getApplicationURI("rule/{$id}/"));
57+
58+
$can_edit = PhabricatorPolicyFilter::hasCapability(
59+
$viewer,
60+
$rule,
61+
PhabricatorPolicyCapability::CAN_EDIT);
62+
63+
$view->addAction(
64+
id(new PhabricatorActionView())
65+
->setName(pht('Edit Rule'))
66+
->setHref($this->getApplicationURI("edit/{$id}/"))
67+
->setIcon('edit')
68+
->setDisabled(!$can_edit)
69+
->setWorkflow(!$can_edit));
70+
71+
return $view;
72+
}
73+
74+
private function buildPropertyView(HeraldRule $rule) {
75+
$viewer = $this->getRequest()->getUser();
76+
77+
$this->loadHandles(array($rule->getAuthorPHID()));
78+
79+
$view = id(new PhabricatorPropertyListView())
80+
->setUser($viewer)
81+
->setObject($rule);
82+
83+
$view->addProperty(
84+
pht('Rule Type'),
85+
idx(HeraldRuleTypeConfig::getRuleTypeMap(), $rule->getRuleType()));
86+
87+
if ($rule->isPersonalRule()) {
88+
$view->addProperty(
89+
pht('Author'),
90+
$this->getHandle($rule->getAuthorPHID())->renderLink());
91+
}
92+
93+
$adapter = HeraldAdapter::getAdapterForContentType($rule->getContentType());
94+
if ($adapter) {
95+
$view->addProperty(
96+
pht('Applies To'),
97+
idx(HeraldAdapter::getEnabledAdapterMap(), $rule->getContentType()));
98+
99+
$view->invokeWillRenderEvent();
100+
101+
$view->addSectionHeader(pht('Rule Description'));
102+
$view->addTextContent(
103+
phutil_tag(
104+
'div',
105+
array(
106+
'style' => 'white-space: pre-wrap;',
107+
),
108+
$adapter->renderRuleAsText($rule)));
109+
}
110+
111+
return $view;
112+
}
113+
114+
}

0 commit comments

Comments
 (0)
Failed to load comments.