UFGroup.php - Code cleanup
[civicrm-core.git] / CRM / Core / BAO / UFGroup.php
index f3e05f043317693d82b7418561c156539b6d6951..05fd61d48e280fcdbdaa39587dd3e771b310327e 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.3                                                |
+ | CiviCRM version 4.4                                                |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2013                                |
  +--------------------------------------------------------------------+
@@ -1178,9 +1178,8 @@ class CRM_Core_BAO_UFGroup extends CRM_Core_DAO_UFGroup {
           }
           elseif ($fieldName == 'im') {
             $providerId = $detailName . '-provider_id';
-            $providerName = $imProviders[$details->$providerId];
-            if ($providerName) {
-              $values[$index] = $details->$detailName . " (" . $providerName . ")";
+            if (isset($imProviders[$details->$providerId])) {
+              $values[$index] = $details->$detailName . " (" . $imProviders[$details->$providerId] . ")";
             }
             else {
               $values[$index] = $details->$detailName;
@@ -1576,16 +1575,28 @@ AND    ( entity_id IS NULL OR entity_id <= 0 )
    * Function to get the uf group for a module
    *
    * @param string $moduleName module name
-   * $param int    $count no to increment the weight
-   * $param bool   $skipPermision - whether to add permission clause
-   * $param int    $op - which operation (view, edit, create, etc) to check permission for
+   * @param int    $count no to increment the weight
+   * @param bool   $skipPermision - whether to add permission clause
+   * @param int    $op - which operation (view, edit, create, etc) to check permission for
+   * @param array|NULL $returnFields list of UFGroup fields to return; NULL for default
    *
    * @return array $ufGroups array of ufgroups for a module
    * @access public
    * @static
    */
-  public static function getModuleUFGroup($moduleName = NULL, $count = 0, $skipPermission = TRUE, $op = CRM_Core_Permission::VIEW) {
-    $queryString = 'SELECT civicrm_uf_group.id, title, created_id, description, civicrm_uf_group.is_active, is_reserved, group_type
+  public static function getModuleUFGroup($moduleName = NULL, $count = 0, $skipPermission = TRUE, $op = CRM_Core_Permission::VIEW, $returnFields = NULL) {
+    $selectFields = array('id', 'title', 'created_id', 'is_active', 'is_reserved', 'group_type');
+
+    if (!CRM_Core_Config::isUpgradeMode()) {
+      // CRM-13555, since description field was added later (4.4), and to avoid any problems with upgrade
+      $selectFields[] = 'description';
+    }
+
+    if (!empty($returnFields)) {
+      $selectFields = array_merge($returnFields, array_diff($selectFields, $returnFields));
+    }
+
+    $queryString = 'SELECT civicrm_uf_group.' . implode(', civicrm_uf_group.', $selectFields) . '
                         FROM civicrm_uf_group
                         LEFT JOIN civicrm_uf_join ON (civicrm_uf_group.id = uf_group_id)';
     $p = array();
@@ -1618,13 +1629,16 @@ AND    ( entity_id IS NULL OR entity_id <= 0 )
       ) {
         continue;
       }
-      $ufGroups[$dao->id]['name'] = $dao->title;
-      $ufGroups[$dao->id]['title'] = $dao->title;
-      $ufGroups[$dao->id]['created_id'] = $dao->created_id;
-      $ufGroups[$dao->id]['description'] = $dao->description;
-      $ufGroups[$dao->id]['is_active'] = $dao->is_active;
-      $ufGroups[$dao->id]['group_type'] = $dao->group_type;
-      $ufGroups[$dao->id]['is_reserved'] = $dao->is_reserved;
+      foreach ($selectFields as $key => $field) {
+        if($field == 'id') {
+          continue;
+        }
+        elseif ($field == 'name') {
+          $ufGroups[$dao->id][$field] = $dao->title;
+          continue;
+        }
+        $ufGroups[$dao->id][$field] = $dao->$field;
+      }
     }
 
     // Allow other modules to alter/override the UFGroups.
@@ -1857,16 +1871,10 @@ AND    ( entity_id IS NULL OR entity_id <= 0 )
         $form->addRule($name, ts('%1 is a required field.', array(1 => $title)), 'required');
       }
     }
-    elseif ($fieldName === 'prefix_id') {
-      $form->add('select', $name, $title,
-        array(
-          '' => ts('- select -')) + CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'prefix_id'), $required
-      );
-    }
-    elseif ($fieldName === 'suffix_id') {
+    elseif ($fieldName === 'prefix_id' || $fieldName === 'suffix_id') {
       $form->add('select', $name, $title,
         array(
-          '' => ts('- select -')) + CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'suffix_id'), $required
+          '' => ts('- select -')) + CRM_Core_PseudoConstant::get('CRM_Contact_BAO_Contact', $fieldName), $required
       );
     }
     elseif ($fieldName === 'contact_sub_type') {
@@ -2352,13 +2360,15 @@ AND    ( entity_id IS NULL OR entity_id <= 0 )
                         $defaults[$fldName] = $value['county_id'];
                       }
                       elseif ($fieldName == 'country') {
-                        $defaults[$fldName] = $value['country_id'];
                         if (!isset($value['country_id']) || !$value['country_id']) {
                           $config = CRM_Core_Config::singleton();
                           if ($config->defaultContactCountry) {
                             $defaults[$fldName] = $config->defaultContactCountry;
                           }
                         }
+                        else {
+                          $defaults[$fldName] = $value['country_id'];
+                        }
                       }
                       elseif ($fieldName == 'phone') {
                         if ($phoneTypeId) {
@@ -2661,7 +2671,7 @@ AND    ( entity_id IS NULL OR entity_id <= 0 )
 
     foreach ($emailList as $emailTo) {
       // FIXME: take the below out of the foreach loop
-      CRM_Core_BAO_MessageTemplates::sendTemplate(
+      CRM_Core_BAO_MessageTemplate::sendTemplate(
         array(
           'groupName' => 'msg_tpl_workflow_uf',
           'valueName' => 'uf_notify',
@@ -2812,6 +2822,7 @@ AND    ( entity_id IS NULL OR entity_id <= 0 )
         }
       }
       else {
+        // TODO: prefix, suffix and gender translation may no longer be necessary - check inputs
         if ($key === 'individual_suffix') {
           $data['suffix_id'] = $value;
         }
@@ -3044,7 +3055,7 @@ AND    ( entity_id IS NULL OR entity_id <= 0 )
    *
    * @return void.
    */
-  function setComponentDefaults(&$fields, $componentId, $component, &$defaults, $isStandalone = FALSE) {
+  public static function setComponentDefaults(&$fields, $componentId, $component, &$defaults, $isStandalone = FALSE) {
     if (!$componentId ||
       !in_array($component, array('Contribute', 'Membership', 'Event', 'Activity'))
     ) {