Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/infrastructure/customfield/standard/PhabricatorStandardCustomFieldDate.php
12242 views
1
<?php
2
3
final class PhabricatorStandardCustomFieldDate
4
extends PhabricatorStandardCustomField {
5
6
public function getFieldType() {
7
return 'date';
8
}
9
10
public function buildFieldIndexes() {
11
$indexes = array();
12
13
$value = $this->getFieldValue();
14
if (strlen($value)) {
15
$indexes[] = $this->newNumericIndex((int)$value);
16
}
17
18
return $indexes;
19
}
20
21
public function buildOrderIndex() {
22
return $this->newNumericIndex(0);
23
}
24
25
public function getValueForStorage() {
26
$value = $this->getFieldValue();
27
if (strlen($value)) {
28
return (int)$value;
29
} else {
30
return null;
31
}
32
}
33
34
public function setValueFromStorage($value) {
35
if (strlen($value)) {
36
$value = (int)$value;
37
} else {
38
$value = null;
39
}
40
return $this->setFieldValue($value);
41
}
42
43
public function renderEditControl(array $handles) {
44
return $this->newDateControl();
45
}
46
47
public function readValueFromRequest(AphrontRequest $request) {
48
$control = $this->newDateControl();
49
$control->setUser($request->getUser());
50
$value = $control->readValueFromRequest($request);
51
52
$this->setFieldValue($value);
53
}
54
55
public function renderPropertyViewValue(array $handles) {
56
$value = $this->getFieldValue();
57
if (!$value) {
58
return null;
59
}
60
61
return phabricator_datetime($value, $this->getViewer());
62
}
63
64
private function newDateControl() {
65
$control = id(new AphrontFormDateControl())
66
->setLabel($this->getFieldName())
67
->setName($this->getFieldKey())
68
->setUser($this->getViewer())
69
->setCaption($this->getCaption())
70
->setAllowNull(!$this->getRequired());
71
72
// If the value is already numeric, treat it as an epoch timestamp and set
73
// it directly. Otherwise, it's likely a field default, which we let users
74
// specify as a string. Parse the string into an epoch.
75
76
$value = $this->getFieldValue();
77
if (!ctype_digit($value)) {
78
$value = PhabricatorTime::parseLocalTime($value, $this->getViewer());
79
}
80
81
// If we don't have anything valid, make sure we pass `null`, since the
82
// control special-cases that.
83
$control->setValue(nonempty($value, null));
84
85
return $control;
86
}
87
88
public function readApplicationSearchValueFromRequest(
89
PhabricatorApplicationSearchEngine $engine,
90
AphrontRequest $request) {
91
92
$key = $this->getFieldKey();
93
94
return array(
95
'min' => $request->getStr($key.'.min'),
96
'max' => $request->getStr($key.'.max'),
97
);
98
}
99
100
public function applyApplicationSearchConstraintToQuery(
101
PhabricatorApplicationSearchEngine $engine,
102
PhabricatorCursorPagedPolicyAwareQuery $query,
103
$value) {
104
105
$viewer = $this->getViewer();
106
107
if (!is_array($value)) {
108
$value = array();
109
}
110
111
$min_str = idx($value, 'min', '');
112
if (strlen($min_str)) {
113
$min = PhabricatorTime::parseLocalTime($min_str, $viewer);
114
} else {
115
$min = null;
116
}
117
118
$max_str = idx($value, 'max', '');
119
if (strlen($max_str)) {
120
$max = PhabricatorTime::parseLocalTime($max_str, $viewer);
121
} else {
122
$max = null;
123
}
124
125
if (($min !== null) || ($max !== null)) {
126
$query->withApplicationSearchRangeConstraint(
127
$this->newNumericIndex(null),
128
$min,
129
$max);
130
}
131
}
132
133
public function appendToApplicationSearchForm(
134
PhabricatorApplicationSearchEngine $engine,
135
AphrontFormView $form,
136
$value) {
137
138
if (!is_array($value)) {
139
$value = array();
140
}
141
142
$form
143
->appendChild(
144
id(new AphrontFormTextControl())
145
->setLabel(pht('%s After', $this->getFieldName()))
146
->setName($this->getFieldKey().'.min')
147
->setValue(idx($value, 'min', '')))
148
->appendChild(
149
id(new AphrontFormTextControl())
150
->setLabel(pht('%s Before', $this->getFieldName()))
151
->setName($this->getFieldKey().'.max')
152
->setValue(idx($value, 'max', '')));
153
}
154
155
public function getApplicationTransactionTitle(
156
PhabricatorApplicationTransaction $xaction) {
157
$author_phid = $xaction->getAuthorPHID();
158
$old = $xaction->getOldValue();
159
$new = $xaction->getNewValue();
160
161
$viewer = $this->getViewer();
162
163
$old_date = null;
164
if ($old) {
165
$old_date = phabricator_datetime($old, $viewer);
166
}
167
168
$new_date = null;
169
if ($new) {
170
$new_date = phabricator_datetime($new, $viewer);
171
}
172
173
if (!$old) {
174
return pht(
175
'%s set %s to %s.',
176
$xaction->renderHandleLink($author_phid),
177
$this->getFieldName(),
178
$new_date);
179
} else if (!$new) {
180
return pht(
181
'%s removed %s.',
182
$xaction->renderHandleLink($author_phid),
183
$this->getFieldName());
184
} else {
185
return pht(
186
'%s changed %s from %s to %s.',
187
$xaction->renderHandleLink($author_phid),
188
$this->getFieldName(),
189
$old_date,
190
$new_date);
191
}
192
}
193
194
public function getApplicationTransactionTitleForFeed(
195
PhabricatorApplicationTransaction $xaction) {
196
197
$viewer = $this->getViewer();
198
199
$author_phid = $xaction->getAuthorPHID();
200
$object_phid = $xaction->getObjectPHID();
201
202
$old = $xaction->getOldValue();
203
$new = $xaction->getNewValue();
204
205
if (!$old) {
206
return pht(
207
'%s set %s to %s on %s.',
208
$xaction->renderHandleLink($author_phid),
209
$this->getFieldName(),
210
phabricator_datetime($new, $viewer),
211
$xaction->renderHandleLink($object_phid));
212
} else if (!$new) {
213
return pht(
214
'%s removed %s on %s.',
215
$xaction->renderHandleLink($author_phid),
216
$this->getFieldName(),
217
$xaction->renderHandleLink($object_phid));
218
} else {
219
return pht(
220
'%s changed %s from %s to %s on %s.',
221
$xaction->renderHandleLink($author_phid),
222
$this->getFieldName(),
223
phabricator_datetime($old, $viewer),
224
phabricator_datetime($new, $viewer),
225
$xaction->renderHandleLink($object_phid));
226
}
227
}
228
229
protected function newConduitSearchParameterType() {
230
// TODO: Build a new "pair<epoch|null, epoch|null>" type or similar.
231
return null;
232
}
233
234
protected function newConduitEditParameterType() {
235
return id(new ConduitEpochParameterType())
236
->setAllowNull(!$this->getRequired());
237
}
238
239
protected function newExportFieldType() {
240
return new PhabricatorEpochExportField();
241
}
242
243
}
244
245