CRM-16077 - Expose optionEditPath to api getfields
authorColeman Watts <coleman@civicrm.org>
Mon, 9 Mar 2015 14:22:42 +0000 (10:22 -0400)
committerColeman Watts <coleman@civicrm.org>
Mon, 9 Mar 2015 20:22:48 +0000 (16:22 -0400)
CRM/Core/BAO/CustomField.php
CRM/Core/CodeGen/Specification.php
CRM/Core/PseudoConstant.php
CRM/Core/SelectValues.php

index b869dc34e7cc1ada95eb50f7efcd54220715877a..b73b2d511414347785a1347e47c1c4b4cdcb6165 100644 (file)
@@ -509,10 +509,13 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
                             $cfTable.option_group_id,
                             $cfTable.date_format,
                             $cfTable.time_format,
-                            $cgTable.is_multiple
+                            $cgTable.is_multiple,
+                            og.name as option_group_name
                      FROM $cfTable
                      INNER JOIN $cgTable
-                     ON $cfTable.custom_group_id = $cgTable.id
+                       ON $cfTable.custom_group_id = $cgTable.id
+                     LEFT JOIN civicrm_option_group og
+                       ON $cfTable.option_group_id = og.id
                      WHERE ( 1 ) ";
 
         if (!$showAll) {
@@ -577,6 +580,7 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
           $fields[$dao->id]['date_format'] = $dao->date_format;
           $fields[$dao->id]['time_format'] = $dao->time_format;
           $fields[$dao->id]['is_required'] = $dao->is_required;
+          self::getOptionsForField($fields[$dao->id], $dao->option_group_name);
         }
 
         CRM_Core_BAO_Cache::setItem($fields,
@@ -2532,4 +2536,37 @@ WHERE cf.id = %1 AND cg.is_multiple = 1";
     return ($field['html_type'] == 'CheckBox' || strpos($field['html_type'], 'Multi') !== FALSE);
   }
 
+  /**
+   * @param array $field
+   * @param string|null $optionGroupName
+   */
+  private static function getOptionsForField(&$field, $optionGroupName) {
+    if ($optionGroupName) {
+      $field['pseudoconstant'] = array(
+        'optionGroupName' => $optionGroupName,
+        'optionEditPath' => 'civicrm/admin/options/' . $optionGroupName,
+      );
+    }
+    elseif ($field['data_type'] == 'Boolean') {
+      $field['pseudoconstant'] = array(
+        'callback' => 'CRM_Core_SelectValues::boolean',
+      );
+    }
+    elseif ($field['data_type'] == 'Country') {
+      $field['pseudoconstant'] = array(
+        'table' => 'civicrm_country',
+        'keyColumn' => 'id',
+        'labelColumn' => 'name',
+        'nameColumn' => 'iso_code',
+      );
+    }
+    elseif ($field['data_type'] == 'StateProvince') {
+      $field['pseudoconstant'] = array(
+        'table' => 'civicrm_state_province',
+        'keyColumn' => 'id',
+        'labelColumn' => 'name',
+      );
+    }
+  }
+
 }
index a9063cb5fc2e90580a848ad1e08731b11efaf600..2e197539694b93c8d0f2e5f8ea48be1bab6b32fc 100644 (file)
@@ -399,12 +399,17 @@ class CRM_Core_CodeGen_Specification {
         'condition',
         // callback funtion incase of static arrays
         'callback',
+        // Path to options edit form
+        'optionEditPath',
       );
       foreach ($validOptions as $pseudoOption) {
         if (!empty($fieldXML->pseudoconstant->$pseudoOption)) {
           $field['pseudoconstant'][$pseudoOption] = $this->value($pseudoOption, $fieldXML->pseudoconstant);
         }
       }
+      if (!isset($field['pseudoconstant']['optionEditPath']) && !empty($field['pseudoconstant']['optionGroupName'])) {
+        $field['pseudoconstant']['optionEditPath'] = 'civicrm/admin/options/' . $field['pseudoconstant']['optionGroupName'];
+      }
       // For now, fields that have option lists that are not in the db can simply
       // declare an empty pseudoconstant tag and we'll add this placeholder.
       // That field's BAO::buildOptions fn will need to be responsible for generating the option list
index b5c9e5c2b9f6fc4e46b3a0b0ebb4fe8bfcd0ce5b..9d9b4802532488d2ceb1f8d540adbc3ee50f953b 100644 (file)
@@ -249,7 +249,7 @@ class CRM_Core_PseudoConstant {
           $options = $context == 'validate' ? self::countryIsoCode() : self::country();
         }
         elseif ($customField->data_type === 'Boolean') {
-          $options = $context == 'validate' ? array(0, 1) : array(1 => ts('Yes'), 0 => ts('No'));
+          $options = $context == 'validate' ? array(0, 1) : CRM_Core_SelectValues::boolean();
         }
       }
       CRM_Utils_Hook::customFieldOptions($customField->id, $options, FALSE);
index fff91f827426df8df3bfd5a068047dd102920ab6..721ba43ea4b4754f4c68d9c94f1908dff9afd6c0 100644 (file)
  */
 class CRM_Core_SelectValues {
 
+  /**
+   * Yes/No options
+   *
+   * @return array
+   */
+  public static function boolean() {
+    return array(
+      1 => ts('Yes'),
+      0 => ts('No'),
+    );
+  }
+
   /**
    * Preferred mail format.
    *
@@ -231,7 +243,6 @@ class CRM_Core_SelectValues {
    * Styles for displaying the custom data group.
    *
    * @return array
-   *
    */
   public static function customGroupStyle() {
     return array(
@@ -265,7 +276,6 @@ class CRM_Core_SelectValues {
    * The status of a contact within a group.
    *
    * @return array
-   *
    */
   public static function groupContactStatus() {
     return array(