@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 NuanceItem
4 extends NuanceDAO
5 implements
6 PhabricatorPolicyInterface,
7 PhabricatorApplicationTransactionInterface {
8
9 const STATUS_IMPORTING = 'importing';
10 const STATUS_ROUTING = 'routing';
11 const STATUS_OPEN = 'open';
12 const STATUS_CLOSED = 'closed';
13
14 protected $status;
15 protected $ownerPHID;
16 protected $requestorPHID;
17 protected $sourcePHID;
18 protected $queuePHID;
19 protected $itemType;
20 protected $itemKey;
21 protected $itemContainerKey;
22 protected $data = array();
23 protected $mailKey;
24
25 private $queue = self::ATTACHABLE;
26 private $source = self::ATTACHABLE;
27 private $implementation = self::ATTACHABLE;
28
29 public static function initializeNewItem($item_type) {
30
31 // TODO: Validate that the type is valid, and construct and attach it.
32
33 return id(new NuanceItem())
34 ->setItemType($item_type)
35 ->setStatus(self::STATUS_OPEN);
36 }
37
38 protected function getConfiguration() {
39 return array(
40 self::CONFIG_AUX_PHID => true,
41 self::CONFIG_SERIALIZATION => array(
42 'data' => self::SERIALIZATION_JSON,
43 ),
44 self::CONFIG_COLUMN_SCHEMA => array(
45 'ownerPHID' => 'phid?',
46 'requestorPHID' => 'phid?',
47 'queuePHID' => 'phid?',
48 'itemType' => 'text64',
49 'itemKey' => 'text64?',
50 'itemContainerKey' => 'text64?',
51 'status' => 'text32',
52 'mailKey' => 'bytes20',
53 ),
54 self::CONFIG_KEY_SCHEMA => array(
55 'key_source' => array(
56 'columns' => array('sourcePHID', 'status'),
57 ),
58 'key_owner' => array(
59 'columns' => array('ownerPHID', 'status'),
60 ),
61 'key_requestor' => array(
62 'columns' => array('requestorPHID', 'status'),
63 ),
64 'key_queue' => array(
65 'columns' => array('queuePHID', 'status'),
66 ),
67 'key_container' => array(
68 'columns' => array('sourcePHID', 'itemContainerKey'),
69 ),
70 'key_item' => array(
71 'columns' => array('sourcePHID', 'itemKey'),
72 'unique' => true,
73 ),
74 ),
75 ) + parent::getConfiguration();
76 }
77
78 public function generatePHID() {
79 return PhabricatorPHID::generateNewPHID(
80 NuanceItemPHIDType::TYPECONST);
81 }
82
83 public function save() {
84 if (!$this->getMailKey()) {
85 $this->setMailKey(Filesystem::readRandomCharacters(20));
86 }
87 return parent::save();
88 }
89
90 public function getURI() {
91 return '/nuance/item/view/'.$this->getID().'/';
92 }
93
94 public function getSource() {
95 return $this->assertAttached($this->source);
96 }
97
98 public function attachSource(NuanceSource $source) {
99 $this->source = $source;
100 }
101
102 public function getItemProperty($key, $default = null) {
103 return idx($this->data, $key, $default);
104 }
105
106 public function setItemProperty($key, $value) {
107 $this->data[$key] = $value;
108 return $this;
109 }
110
111 public function getCapabilities() {
112 return array(
113 PhabricatorPolicyCapability::CAN_VIEW,
114 PhabricatorPolicyCapability::CAN_EDIT,
115 );
116 }
117
118 public function getPolicy($capability) {
119 // TODO - this should be based on the queues the item currently resides in
120 return PhabricatorPolicies::POLICY_USER;
121 }
122
123 public function hasAutomaticCapability($capability, PhabricatorUser $viewer) {
124 // TODO - requestors should get auto access too!
125 return $viewer->getPHID() == $this->ownerPHID;
126 }
127
128 public function describeAutomaticCapability($capability) {
129 switch ($capability) {
130 case PhabricatorPolicyCapability::CAN_VIEW:
131 return pht('Owners of an item can always view it.');
132 case PhabricatorPolicyCapability::CAN_EDIT:
133 return pht('Owners of an item can always edit it.');
134 }
135 return null;
136 }
137
138 public function getDisplayName() {
139 return $this->getImplementation()->getItemDisplayName($this);
140 }
141
142 public function scheduleUpdate() {
143 PhabricatorWorker::scheduleTask(
144 'NuanceItemUpdateWorker',
145 array(
146 'itemPHID' => $this->getPHID(),
147 ),
148 array(
149 'objectPHID' => $this->getPHID(),
150 ));
151 }
152
153 public function issueCommand(
154 $author_phid,
155 $command,
156 array $parameters = array()) {
157
158 $command = id(NuanceItemCommand::initializeNewCommand())
159 ->setItemPHID($this->getPHID())
160 ->setAuthorPHID($author_phid)
161 ->setCommand($command)
162 ->setParameters($parameters)
163 ->save();
164
165 $this->scheduleUpdate();
166
167 return $this;
168 }
169
170 public function getImplementation() {
171 return $this->assertAttached($this->implementation);
172 }
173
174 public function attachImplementation(NuanceItemType $type) {
175 $this->implementation = $type;
176 return $this;
177 }
178
179 public function getQueue() {
180 return $this->assertAttached($this->queue);
181 }
182
183 public function attachQueue(?NuanceQueue $queue = null) {
184 $this->queue = $queue;
185 return $this;
186 }
187
188
189/* -( PhabricatorApplicationTransactionInterface )------------------------- */
190
191
192 public function getApplicationTransactionEditor() {
193 return new NuanceItemEditor();
194 }
195
196 public function getApplicationTransactionTemplate() {
197 return new NuanceItemTransaction();
198 }
199
200}