From 3f56ca681f3ea6f34e20ef7b7a4fb0573215adea Mon Sep 17 00:00:00 2001
From: epriestley <git@epriestley.com>
Date: Thu, 7 Mar 2013 17:23:30 -0800
Subject: [PATCH] Implement Maniphest auxiliary field 'default' key

Summary: Fixes T404. Ref T2575. Allows default to be set for any field. Date defaults are interpreted by `strtotime()`. Other defaults are interpreted as expected.

Test Plan:
  - Created a string custom field with default value "Orange".
  - Created a date custom field with a fixed default value (my birthday).
  - Created a date custom field with a relative default value ("today 4:59 PM").
  - Created/edited tasks with these fields, verified everything behaved sensibly.

Reviewers: hach-que, btrahan

Reviewed By: btrahan

CC: aran

Maniphest Tasks: T404, T2575

Differential Revision: https://secure.phabricator.com/D5282
---
 src/__phutil_library_map__.php                |  4 ++++
 ...hestAuxiliaryFieldDefaultSpecification.php | 20 +++++++++++++++----
 .../ManiphestDefaultTaskExtensions.php        |  3 +++
 .../extensions/ManiphestTaskExtensions.php    | 10 ++++++++--
 src/docs/userguide/maniphest_custom.diviner   |  2 ++
 5 files changed, 33 insertions(+), 6 deletions(-)

diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php
index 89f3484930..58aa47b251 100644
--- a/src/__phutil_library_map__.php
+++ b/src/__phutil_library_map__.php
@@ -67,6 +67,7 @@ phutil_register_library_map(array(
     'AphrontMiniPanelView' => 'view/layout/AphrontMiniPanelView.php',
     'AphrontMoreView' => 'view/layout/AphrontMoreView.php',
     'AphrontMySQLDatabaseConnectionTestCase' => 'infrastructure/storage/__tests__/AphrontMySQLDatabaseConnectionTestCase.php',
+    'AphrontNoteView' => 'view/widget/AphrontNoteView.php',
     'AphrontNullView' => 'view/AphrontNullView.php',
     'AphrontPHPHTTPSink' => 'aphront/sink/AphrontPHPHTTPSink.php',
     'AphrontPageView' => 'view/page/AphrontPageView.php',
@@ -1054,6 +1055,7 @@ phutil_register_library_map(array(
     'PhabricatorMustVerifyEmailController' => 'applications/auth/controller/PhabricatorMustVerifyEmailController.php',
     'PhabricatorMySQLConfigOptions' => 'applications/config/option/PhabricatorMySQLConfigOptions.php',
     'PhabricatorMySQLFileStorageEngine' => 'applications/files/engine/PhabricatorMySQLFileStorageEngine.php',
+    'PhabricatorNoteExample' => 'applications/uiexample/examples/PhabricatorNoteExample.php',
     'PhabricatorNotificationBuilder' => 'applications/notification/builder/PhabricatorNotificationBuilder.php',
     'PhabricatorNotificationClearController' => 'applications/notification/controller/PhabricatorNotificationClearController.php',
     'PhabricatorNotificationConfigOptions' => 'applications/config/option/PhabricatorNotificationConfigOptions.php',
@@ -1628,6 +1630,7 @@ phutil_register_library_map(array(
     'AphrontMiniPanelView' => 'AphrontView',
     'AphrontMoreView' => 'AphrontView',
     'AphrontMySQLDatabaseConnectionTestCase' => 'PhabricatorTestCase',
+    'AphrontNoteView' => 'AphrontView',
     'AphrontNullView' => 'AphrontView',
     'AphrontPHPHTTPSink' => 'AphrontHTTPSink',
     'AphrontPageView' => 'AphrontView',
@@ -2550,6 +2553,7 @@ phutil_register_library_map(array(
     'PhabricatorMustVerifyEmailController' => 'PhabricatorAuthController',
     'PhabricatorMySQLConfigOptions' => 'PhabricatorApplicationConfigOptions',
     'PhabricatorMySQLFileStorageEngine' => 'PhabricatorFileStorageEngine',
+    'PhabricatorNoteExample' => 'PhabricatorUIExample',
     'PhabricatorNotificationClearController' => 'PhabricatorNotificationController',
     'PhabricatorNotificationConfigOptions' => 'PhabricatorApplicationConfigOptions',
     'PhabricatorNotificationController' => 'PhabricatorController',
diff --git a/src/applications/maniphest/auxiliaryfield/ManiphestAuxiliaryFieldDefaultSpecification.php b/src/applications/maniphest/auxiliaryfield/ManiphestAuxiliaryFieldDefaultSpecification.php
index 34173807f7..032a9b00a4 100644
--- a/src/applications/maniphest/auxiliaryfield/ManiphestAuxiliaryFieldDefaultSpecification.php
+++ b/src/applications/maniphest/auxiliaryfield/ManiphestAuxiliaryFieldDefaultSpecification.php
@@ -96,7 +96,6 @@ class ManiphestAuxiliaryFieldDefaultSpecification
       case self::TYPE_DATE:
         $control = new AphrontFormDateControl();
         $control->setUser($this->getUser());
-        $control->setValue(time());
         break;
       default:
         $label = $this->getLabel();
@@ -114,9 +113,7 @@ class ManiphestAuxiliaryFieldDefaultSpecification
           (bool)$this->getValue());
         break;
       case self::TYPE_DATE:
-        if ($this->getValue()) {
-          $control->setValue($this->getValue());
-        }
+        $control->setValue($this->getValue());
         $control->setName('auxiliary_date_'.$this->getAuxiliaryKey());
         break;
       default:
@@ -181,6 +178,21 @@ class ManiphestAuxiliaryFieldDefaultSpecification
     }
   }
 
+  public function setDefaultValue($value) {
+    switch ($this->getFieldType()) {
+      case self::TYPE_DATE:
+        $value = strtotime($value);
+        if ($value <= 0) {
+          $value = time();
+        }
+        $this->setValue($value);
+        break;
+      default:
+        $this->setValue((string)$value);
+        break;
+    }
+  }
+
   public function renderForDetailView() {
     switch ($this->getFieldType()) {
       case self::TYPE_BOOL:
diff --git a/src/applications/maniphest/extensions/ManiphestDefaultTaskExtensions.php b/src/applications/maniphest/extensions/ManiphestDefaultTaskExtensions.php
index 98355e4de3..3183019805 100644
--- a/src/applications/maniphest/extensions/ManiphestDefaultTaskExtensions.php
+++ b/src/applications/maniphest/extensions/ManiphestDefaultTaskExtensions.php
@@ -26,6 +26,9 @@ final class ManiphestDefaultTaskExtensions
       }
 
       $spec->setShouldCopyWhenCreatingSimilarTask(idx($info, 'copy'));
+
+      $spec->setDefaultValue(idx($info, 'default'));
+
       $specs[] = $spec;
     }
 
diff --git a/src/applications/maniphest/extensions/ManiphestTaskExtensions.php b/src/applications/maniphest/extensions/ManiphestTaskExtensions.php
index 06e8f4402b..da40b25de2 100644
--- a/src/applications/maniphest/extensions/ManiphestTaskExtensions.php
+++ b/src/applications/maniphest/extensions/ManiphestTaskExtensions.php
@@ -24,12 +24,18 @@ abstract class ManiphestTaskExtensions {
     }
 
     $task->loadAndAttachAuxiliaryAttributes();
+
     foreach ($aux_fields as $aux) {
       $aux->setUser($viewer);
       $aux->setTask($task);
 
-      $key = $aux->getAuxiliaryKey();
-      $aux->setValueFromStorage($task->getAuxiliaryAttribute($key));
+      // If we're creating a new task, we don't bother loading any stored data.
+      // This allows any defaults configured by the Extensions object to
+      // survive.
+      if ($task->getID()) {
+        $key = $aux->getAuxiliaryKey();
+        $aux->setValueFromStorage($task->getAuxiliaryAttribute($key));
+      }
     }
 
     return $aux_fields;
diff --git a/src/docs/userguide/maniphest_custom.diviner b/src/docs/userguide/maniphest_custom.diviner
index edc42f0147..b8f68c35a1 100644
--- a/src/docs/userguide/maniphest_custom.diviner
+++ b/src/docs/userguide/maniphest_custom.diviner
@@ -54,6 +54,8 @@ the field. These options are available:
     show next to the checkbox.
   - **checkbox-value**: If type is set to **bool**, the value to show on
     the detail view when the checkbox is selected.
+  - **default**: Default field value. For **date**, you can use a string like
+    `"July 4, 1990"`, `"5PM today"`, or any other valid input to `strtotime()`.
   - **copy**: When a user creates a task, the UI gives them an option to
     "Create Another Similar Task". Some fields from the original task are copied
     into the new task, while others are not; by default, fields are not copied.