CRM-13883 - permanently delete contact should not remove activities if connected...
[civicrm-core.git] / CRM / Core / PseudoConstant.php
index 32a177bac88b87aaaf4bba046a39bb78ab897060..1a2630d3beebe313b587a8a3e9104c4d1ed4684f 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.3                                                |
+ | CiviCRM version 4.4                                                |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2013                                |
  +--------------------------------------------------------------------+
@@ -233,26 +233,44 @@ class CRM_Core_PseudoConstant {
     $params += array(
       'grouping' => FALSE,
       'localize' => FALSE,
-      'onlyActive' => ($context == 'validate' || $context == 'create') ? FALSE : TRUE,
+      'onlyActive' => ($context == 'validate' || $context == 'get') ? FALSE : TRUE,
       'fresh' => FALSE,
     );
 
     // Custom fields are not in the schema
     if (strpos($fieldName, 'custom_') === 0 && is_numeric($fieldName[7])) {
-      $customFieldID = (int) substr($fieldName, 7);
-      $optionGroupID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $customFieldID, 'option_group_id');
-
-      $options = CRM_Core_OptionGroup::valuesByID($optionGroupID,
-        $flip,
-        $params['grouping'],
-        $params['localize'],
-        // Note: for custom fields the 'name' column is NULL
-        CRM_Utils_Array::value('labelColumn', $params, 'label'),
-        $params['onlyActive'],
-        $params['fresh']
-      );
+      $customField = new CRM_Core_DAO_CustomField();
+      $customField->id = (int) substr($fieldName, 7);
+      $customField->find(TRUE);
+      $options = FALSE;
 
-      CRM_Utils_Hook::customFieldOptions($customFieldID, $options, FALSE);
+      if (!empty($customField->option_group_id)) {
+        $options = CRM_Core_OptionGroup::valuesByID($customField->option_group_id,
+          $flip,
+          $params['grouping'],
+          $params['localize'],
+          // Note: for custom fields the 'name' column is NULL
+          CRM_Utils_Array::value('labelColumn', $params, 'label'),
+          $params['onlyActive'],
+          $params['fresh']
+        );
+      }
+      else {
+        if ($customField->data_type === 'StateProvince') {
+          $options = self::stateProvince();
+        }
+        elseif ($customField->data_type === 'Country') {
+          $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 = $options && $flip ? array_flip($options) : $options;
+      }
+      if ($options !== FALSE) {
+        CRM_Utils_Hook::customFieldOptions($customField->id, $options, FALSE);
+      }
+      $customField->free();
       return $options;
     }
 
@@ -265,7 +283,7 @@ class CRM_Core_PseudoConstant {
     // Support "unique names" as well as sql names
     $fieldKey = $fieldName;
     if (empty($fields[$fieldKey])) {
-      $fieldKey = $fieldKeys[$fieldName];
+      $fieldKey = CRM_Utils_Array::value($fieldName, $fieldKeys);
     }
     // If neither worked then this field doesn't exist. Return false.
     if (empty($fields[$fieldKey])) {
@@ -390,9 +408,17 @@ class CRM_Core_PseudoConstant {
             $output[$dao->id] = $dao->label;
           }
           $dao->free();
-          if (!empty($params['localize'])) {
+          // Localize results
+          if (!empty($params['localize']) || $pseudoconstant['table'] == 'civicrm_country' || $pseudoconstant['table'] == 'civicrm_state_province') {
+            $I18nParams = array();
+            if ($pseudoconstant['table'] == 'civicrm_country') {
+              $I18nParams['context'] = 'country';
+            }
+            if ($pseudoconstant['table'] == 'civicrm_state_province') {
+              $I18nParams['context'] = 'province';
+            }
             $i18n = CRM_Core_I18n::singleton();
-            $i18n->localizeArray($output);
+            $i18n->localizeArray($output, $I18nParams);
             // Maintain sort by label
             if ($order == "ORDER BY %2") {
               CRM_Utils_Array::asort($output);
@@ -403,25 +429,32 @@ class CRM_Core_PseudoConstant {
         return $flip ? array_flip($output) : $output;
       }
     }
+
+    // Return "Yes" and "No" for boolean fields
+    elseif (CRM_Utils_Array::value('type', $fieldSpec) === CRM_Utils_Type::T_BOOLEAN) {
+      $output = $context == 'validate' ? array(0, 1) : array(1 => ts('Yes'), 0 => ts('No'));
+      return $flip ? array_flip($output) : $output;
+    }
     // If we're still here, it's an error. Return FALSE.
     return FALSE;
   }
 
   /**
-   * Fetch the label (or other value) for a field given its key
+   * Fetch the translated label for a field given its key
    *
-   * @param String $daoName
+   * @param String $baoName
    * @param String $fieldName
    * @param String|Int $key
-   * @param Array $params will be passed into self::get
+   *
+   * TODO: Accept multivalued input?
    *
    * @return bool|null|string
    *   FALSE if the given field has no associated option list
    *   NULL if the given key has no corresponding option
    *   String if label is found
    */
-  static function getValue($daoName, $fieldName, $key, $params = array()) {
-    $values = self::get($daoName, $fieldName, $params);
+  static function getLabel($baoName, $fieldName, $key) {
+    $values = $baoName::buildOptions($fieldName, 'get');
     if ($values === FALSE) {
       return FALSE;
     }
@@ -429,20 +462,39 @@ class CRM_Core_PseudoConstant {
   }
 
   /**
-   * Fetch the key for a field option given its label/name
+   * Fetch the machine name for a field given its key
    *
-   * @param String $daoName
+   * @param String $baoName
+   * @param String $fieldName
+   * @param String|Int $key
+   *
+   * @return bool|null|string
+   *   FALSE if the given field has no associated option list
+   *   NULL if the given key has no corresponding option
+   *   String if label is found
+   */
+  static function getName($baoName, $fieldName, $key) {
+    $values = $baoName::buildOptions($fieldName, 'validate');
+    if ($values === FALSE) {
+      return FALSE;
+    }
+    return CRM_Utils_Array::value($key, $values);
+  }
+
+  /**
+   * Fetch the key for a field option given its name
+   *
+   * @param String $baoName
    * @param String $fieldName
    * @param String|Int $value
-   * @param Array $params will be passed into self::get
    *
    * @return bool|null|string|number
    *   FALSE if the given field has no associated option list
    *   NULL if the given key has no corresponding option
    *   String|Number if key is found
    */
-  static function getKey($daoName, $fieldName, $value, $params = array()) {
-    $values = self::get($daoName, $fieldName, $params);
+  static function getKey($baoName, $fieldName, $value) {
+    $values = $baoName::buildOptions($fieldName, 'validate');
     if ($values === FALSE) {
       return FALSE;
     }
@@ -991,6 +1043,7 @@ WHERE  id = %1";
       while ($relationshipTypeDAO->fetch()) {
 
         self::$relationshipType[$valueColumnName][$relationshipTypeDAO->id] = array(
+          'id' => $relationshipTypeDAO->id,
           $column_a_b => $relationshipTypeDAO->$column_a_b,
           $column_b_a => $relationshipTypeDAO->$column_b_a,
           'contact_type_a' => "$relationshipTypeDAO->contact_type_a",