CRM-14165 - Improve option page lookup
[civicrm-core.git] / CRM / Core / PseudoConstant.php
index bb9560614305e91f197b1e131b37b363141e6bd0..30c01bd552927de8e29da5cfd84147ca30b414d7 100644 (file)
@@ -222,7 +222,7 @@ class CRM_Core_PseudoConstant {
    * - fresh      boolean ignore cache entries and go back to DB
    * @param String $context: Context string
    *
-   * @return Array on success, FALSE on error.
+   * @return Array|bool - array on success, FALSE on error.
    *
    * @static
    */
@@ -276,20 +276,12 @@ class CRM_Core_PseudoConstant {
 
     // Core field: load schema
     $dao = new $daoName;
-    $fields = $dao->fields();
-    $fieldKeys = $dao->fieldKeys();
+    $fieldSpec = $dao->getFieldSpec($fieldName);
     $dao->free();
-
-    // Support "unique names" as well as sql names
-    $fieldKey = $fieldName;
-    if (empty($fields[$fieldKey])) {
-      $fieldKey = CRM_Utils_Array::value($fieldName, $fieldKeys);
-    }
     // If neither worked then this field doesn't exist. Return false.
-    if (empty($fields[$fieldKey])) {
+    if (empty($fieldSpec)) {
       return FALSE;
     }
-    $fieldSpec = $fields[$fieldKey];
 
     // If the field is an enum, explode the enum definition and return the array.
     if (isset($fieldSpec['enumValues'])) {
@@ -303,8 +295,7 @@ class CRM_Core_PseudoConstant {
       $pseudoconstant = $fieldSpec['pseudoconstant'];
       // Merge params with schema defaults
       $params += array(
-        // Skip default condition in validate context
-        'condition' => $context == 'validate' ? array() : CRM_Utils_Array::value('condition', $pseudoconstant, array()),
+        'condition' => CRM_Utils_Array::value('condition', $pseudoconstant, array()),
         'keyColumn' => CRM_Utils_Array::value('keyColumn', $pseudoconstant),
         'labelColumn' => CRM_Utils_Array::value('labelColumn', $pseudoconstant),
       );
@@ -502,6 +493,32 @@ class CRM_Core_PseudoConstant {
     return CRM_Utils_Array::key($value, $values);
   }
 
+  /**
+   * Lookup the admin page at which a field's option list can be edited
+   * @param $fieldSpec
+   * @return string|null
+   */
+  static function getOptionEditUrl($fieldSpec) {
+    // If it's an option group, that's easy
+    if (!empty($fieldSpec['pseudoconstant']['optionGroupName'])) {
+      return 'civicrm/admin/options/' . $fieldSpec['pseudoconstant']['optionGroupName'];
+    }
+    // For everything else...
+    elseif (!empty($fieldSpec['pseudoconstant']['table'])) {
+      $daoName = CRM_Core_DAO_AllCoreTables::getClassForTable($fieldSpec['pseudoconstant']['table']);
+      if (!$daoName) {
+        return NULL;
+      }
+      // We don't have good mapping so have to do a bit of guesswork from the menu
+      list(, , , $ent) = explode('_', $daoName);
+      $sql = "SELECT path FROM civicrm_menu
+        WHERE page_callback LIKE '%CRM_Admin_Page_$ent%'
+        LIMIT 1";
+      return CRM_Core_Dao::singleValueQuery($sql);
+    }
+    return NULL;
+  }
+
   /**
    * DEPRECATED generic populate method
    * All pseudoconstant functions that use this method are also deprecated.