Skip to content

Commit ba46496

Browse files
author
epriestley
committed
Implement Maniphest "date" auxiliary field type
Summary: Ref T404. Ref T2575. Adds a "date" type to Maniphest. This doesn't let you default the date to anything other than `time()`; I'll do that in the next diff. Test Plan: Created and edited a task with date fields. Reviewers: hach-que, btrahan Reviewed By: btrahan CC: aran, mbishopim3 Maniphest Tasks: T404, T2575 Differential Revision: https://secure.phabricator.com/D5281
1 parent a3099e2 commit ba46496

File tree

2 files changed

+62
-16
lines changed

2 files changed

+62
-16
lines changed

src/applications/maniphest/auxiliaryfield/ManiphestAuxiliaryFieldDefaultSpecification.php

Lines changed: 60 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class ManiphestAuxiliaryFieldDefaultSpecification
1919
const TYPE_STRING = 'string';
2020
const TYPE_INT = 'int';
2121
const TYPE_BOOL = 'bool';
22+
const TYPE_DATE = 'date';
2223

2324
public function getFieldType() {
2425
return $this->fieldType;
@@ -92,22 +93,36 @@ public function renderControl() {
9293
case self::TYPE_BOOL:
9394
$control = new AphrontFormCheckboxControl();
9495
break;
96+
case self::TYPE_DATE:
97+
$control = new AphrontFormDateControl();
98+
$control->setUser($this->getUser());
99+
$control->setValue(time());
100+
break;
95101
default:
96102
$label = $this->getLabel();
97103
throw new ManiphestAuxiliaryFieldTypeException(
98104
"Field type '{$type}' is not a valid type (for field '{$label}').");
99105
break;
100106
}
101107

102-
if ($type == self::TYPE_BOOL) {
103-
$control->addCheckbox(
104-
'auxiliary['.$this->getAuxiliaryKey().']',
105-
1,
106-
$this->getCheckboxLabel(),
107-
(bool)$this->getValue());
108-
} else {
109-
$control->setValue($this->getValue());
110-
$control->setName('auxiliary['.$this->getAuxiliaryKey().']');
108+
switch ($type) {
109+
case self::TYPE_BOOL:
110+
$control->addCheckbox(
111+
'auxiliary['.$this->getAuxiliaryKey().']',
112+
1,
113+
$this->getCheckboxLabel(),
114+
(bool)$this->getValue());
115+
break;
116+
case self::TYPE_DATE:
117+
if ($this->getValue()) {
118+
$control->setValue($this->getValue());
119+
}
120+
$control->setName('auxiliary_date_'.$this->getAuxiliaryKey());
121+
break;
122+
default:
123+
$control->setValue($this->getValue());
124+
$control->setName('auxiliary['.$this->getAuxiliaryKey().']');
125+
break;
111126
}
112127

113128
$control->setLabel($this->getLabel());
@@ -117,9 +132,18 @@ public function renderControl() {
117132
return $control;
118133
}
119134

120-
public function setValueFromRequest($request) {
121-
$aux_post_values = $request->getArr('auxiliary');
122-
return $this->setValue(idx($aux_post_values, $this->getAuxiliaryKey(), ''));
135+
public function setValueFromRequest(AphrontRequest $request) {
136+
switch ($this->getFieldType()) {
137+
case self::TYPE_DATE:
138+
$control = $this->renderControl();
139+
$value = $control->readValueFromRequest($request);
140+
break;
141+
default:
142+
$aux_post_values = $request->getArr('auxiliary');
143+
$value = idx($aux_post_values, $this->getAuxiliaryKey(), '');
144+
break;
145+
}
146+
return $this->setValue($value);
123147
}
124148

125149
public function getValueForStorage() {
@@ -135,8 +159,9 @@ public function validate() {
135159
case self::TYPE_INT:
136160
if (!is_numeric($this->getValue())) {
137161
throw new ManiphestAuxiliaryFieldValidationException(
138-
$this->getLabel().' must be an integer value.'
139-
);
162+
pht(
163+
'%s must be an integer value.',
164+
$this->getLabel()));
140165
}
141166
break;
142167
case self::TYPE_BOOL:
@@ -145,6 +170,14 @@ public function validate() {
145170
return true;
146171
case self::TYPE_SELECT:
147172
return true;
173+
case self::TYPE_DATE:
174+
if ($this->getValue() <= 0) {
175+
throw new ManiphestAuxiliaryFieldValidationException(
176+
pht(
177+
'%s must be a valid date.',
178+
$this->getLabel()));
179+
}
180+
break;
148181
}
149182
}
150183

@@ -159,11 +192,13 @@ public function renderForDetailView() {
159192
case self::TYPE_SELECT:
160193
$display = idx($this->getSelectOptions(), $this->getValue());
161194
return $display;
195+
case self::TYPE_DATE:
196+
$display = phabricator_datetime($this->getValue(), $this->getUser());
197+
return $display;
162198
}
163199
return parent::renderForDetailView();
164200
}
165201

166-
167202
public function renderTransactionDescription(
168203
ManiphestTransaction $transaction,
169204
$target) {
@@ -190,6 +225,16 @@ public function renderTransactionDescription(
190225
"from '{$old_display}' to '{$new_display}'";
191226
}
192227
break;
228+
case self::TYPE_DATE:
229+
$new_display = phabricator_datetime($new, $this->getUser());
230+
if ($old === null) {
231+
$desc = "set field '{$label}' to '{$new_display}'";
232+
} else {
233+
$old_display = phabricator_datetime($old, $this->getUser());
234+
$desc = "changed field '{$label}' ".
235+
"from '{$old_display}' to '{$new_display}'";
236+
}
237+
break;
193238
default:
194239
if (!strlen($old)) {
195240
if (!strlen($new)) {

src/docs/userguide/maniphest_custom.diviner

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ Each array key must be unique, and is used to organize the internal storage of
4444
the field. These options are available:
4545

4646
- **label**: Display label for the field on the edit and detail interfaces.
47-
- **type**: Field type, one of **int**, **string**, **bool** or **select**.
47+
- **type**: Field type, one of **int**, **string**, **bool**, **select** or
48+
**date**.
4849
- **caption**: A caption to display underneath the field (optional).
4950
- **required**: True if the user should be required to provide a value.
5051
- **options**: If type is set to **select**, provide options for the dropdown

0 commit comments

Comments
 (0)