@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
1<?php
2
3final class PhabricatorProjectTriggerManiphestOwnerRule
4 extends PhabricatorProjectTriggerRule {
5
6 const TRIGGERTYPE = 'task.owner';
7
8 public function getSelectControlName() {
9 return pht('Assign task to');
10 }
11
12 protected function getValueForEditorField() {
13 return $this->getDatasource()->getWireTokens($this->getValue());
14 }
15
16 private function convertTokenizerValueToOwner($value) {
17 $value = head($value);
18 if ($value === PhabricatorPeopleNoOwnerDatasource::FUNCTION_TOKEN) {
19 $value = null;
20 }
21 if ($value === PhabricatorViewerDatasource::FUNCTION_TOKEN) {
22 $value = $this->getViewer()->getPHID();
23 }
24 return $value;
25 }
26
27 protected function assertValidRuleRecordFormat($value) {
28 if (!is_array($value)) {
29 throw new Exception(
30 pht(
31 'Owner rule value should be a list, but is not (value is "%s").',
32 phutil_describe_type($value)));
33 }
34 }
35
36 protected function assertValidRuleRecordValue($value) {
37 if (!$value) {
38 throw new Exception(
39 pht(
40 'Owner rule value is required. Specify a user to assign tasks '.
41 'to, the token "viewer()" to assign to the user moving tasks, '.
42 'or the token "none()" to unassign tasks.'));
43 }
44
45 if (count($value) > 1) {
46 throw new Exception(
47 pht(
48 'Owner rule value must have only one elmement (value is "%s").',
49 implode(', ', $value)));
50 }
51
52 $owner_phid = $this->convertTokenizerValueToOwner($value);
53 if ($owner_phid !== null) {
54 $user = id(new PhabricatorPeopleQuery())
55 ->setViewer($this->getViewer())
56 ->withPHIDs(array($owner_phid))
57 ->executeOne();
58 if (!$user) {
59 throw new Exception(
60 pht(
61 'User PHID ("%s") is not a valid user.',
62 $owner_phid));
63 }
64 }
65 }
66
67 protected function newDropTransactions($object, $value) {
68 $value = $this->convertTokenizerValueToOwner($value);
69 return array(
70 $this->newTransaction()
71 ->setTransactionType(ManiphestTaskOwnerTransaction::TRANSACTIONTYPE)
72 ->setNewValue($value),
73 );
74 }
75
76 protected function newDropEffects($value) {
77 $owner_value = $this->convertTokenizerValueToOwner($value);
78
79 return array(
80 $this->newEffect()
81 ->setIcon('fa-user')
82 ->setContent($this->getRuleViewDescription($value))
83 ->addCondition('owner', '!=', $owner_value),
84 );
85 }
86
87 protected function getDefaultValue() {
88 return null;
89 }
90
91 protected function getPHUIXControlType() {
92 return 'tokenizer';
93 }
94
95 private function getDatasource() {
96 $datasource = id(new ManiphestAssigneeDatasource())
97 ->setLimit(1);
98
99 if ($this->getViewer()) {
100 $datasource->setViewer($this->getViewer());
101 }
102
103 return $datasource;
104 }
105
106 protected function getPHUIXControlSpecification() {
107 $template = id(new AphrontTokenizerTemplateView())
108 ->setViewer($this->getViewer());
109
110 $template_markup = $template->render();
111 $datasource = $this->getDatasource();
112
113 return array(
114 'markup' => (string)hsprintf('%s', $template_markup),
115 'config' => array(
116 'src' => $datasource->getDatasourceURI(),
117 'browseURI' => $datasource->getBrowseURI(),
118 'placeholder' => $datasource->getPlaceholderText(),
119 'limit' => $datasource->getLimit(),
120 ),
121 'value' => null,
122 );
123 }
124
125 public function getRuleViewLabel() {
126 return pht('Change Owner');
127 }
128
129 public function getRuleViewDescription($value) {
130 if (head($value) === PhabricatorViewerDatasource::FUNCTION_TOKEN) {
131 return pht('Assign task to user moving the task.');
132 } else if ($value) {
133 $value = $this->convertTokenizerValueToOwner($value);
134 return pht(
135 'Assign task to %s.',
136 phutil_tag(
137 'strong',
138 array(),
139 $this->getViewer()
140 ->renderHandle($value)
141 ->render()));
142 } else { // !$value
143 return pht('Unassign task.');
144 }
145 }
146
147 public function getRuleViewIcon($value) {
148 return id(new PHUIIconView())
149 ->setIcon('fa-user', 'green');
150 }
151
152
153}