Merge pull request #67 from dpradeep/merge-20140827
[civicrm-core.git] / CRM / Core / BAO / UFField.php
index 5baa73671e399e9a492f0813042e0e83704b22ff..6c1189caca553b07b67a23b0c2ccecc80bea3d86 100644 (file)
@@ -142,7 +142,7 @@ class CRM_Core_BAO_UFField extends CRM_Core_DAO_UFField {
     $ufField->field_name = $params['field_name'][1];
     if ($params['field_name'][1] == 'url') {
       $ufField->website_type_id = CRM_Utils_Array::value(2, $params['field_name'], NULL);
-    } 
+    }
     else {
       $ufField->location_type_id = (CRM_Utils_Array::value(2, $params['field_name'])) ? $params['field_name'][2] : 'NULL';
     }
@@ -219,7 +219,7 @@ WHERE cf.id IN (" . $customFieldIds . ") AND is_multiple = 1 LIMIT 0,1";
     $locationTypeId = NULL;
     if ($params['field_name'][1] == 'url') {
       $ufField->website_type_id = CRM_Utils_Array::value(2, $params['field_name']);
-    } 
+    }
     else {
       $locationTypeId = CRM_Utils_Array::value(2, $params['field_name']);
       $ufField->website_type_id = NULL;
@@ -348,7 +348,7 @@ WHERE cf.id IN (" . $customFieldIds . ") AND is_multiple = 1 LIMIT 0,1";
    * @static
    * @access public
    */
-  function setUFFieldStatus($customGroupId, $is_active) {
+  static function setUFFieldStatus($customGroupId, $is_active) {
     //find the profile id given custom group id
     $queryString = "SELECT civicrm_custom_field.id as custom_field_id
                         FROM   civicrm_custom_field, civicrm_custom_group
@@ -803,12 +803,18 @@ SELECT  id
    * @param array $profileAddressFields array of profile fields that relate to address fields
    * @param array $profileFilter filter to apply to profile fields - expected usage is to only fill based on
    * the bottom profile per CRM-13726
+   *
+   * @return bool Can the address block be hidden safe in the knowledge all fields are elsewhere collected (see CRM-15118)
    */
   static function assignAddressField($key, &$profileAddressFields, $profileFilter) {
     $billing_id = CRM_Core_BAO_LocationType::getBilling();
     list($prefixName, $index) = CRM_Utils_System::explode('-', $key, 2);
 
-    $profileFields = civicrm_api3('uf_field', 'get', array_merge($profileFilter, array('is_active' => 1, 'return' => 'field_name')));
+    $profileFields = civicrm_api3('uf_field', 'get', array_merge($profileFilter,
+      array('is_active' => 1, 'return' => 'field_name, is_required', 'options' => array(
+        'limit' => 0,
+      ))
+    ));
     //check for valid fields ( fields that are present in billing block )
     $validBillingFields = array(
       'first_name',
@@ -821,12 +827,17 @@ SELECT  id
       'postal_code',
       'country'
     );
+    $requiredBillingFields = array_diff($validBillingFields, array('middle_name','supplemental_address_1'));
     $validProfileFields = array();
+    $requiredProfileFields = array();
 
     foreach ($profileFields['values'] as $field) {
       if(in_array($field['field_name'], $validBillingFields)) {
         $validProfileFields[] = $field['field_name'];
       }
+      if ($field['is_required']) {
+        $requiredProfileFields[] = $field['field_name'];
+      }
     }
 
     if (!in_array($prefixName, $validProfileFields) ) {
@@ -848,6 +859,9 @@ SELECT  id
     ) {
       $profileAddressFields[$prefixName] = $index;
     }
+    
+    $potentiallyMissingRequiredFields = array_diff($requiredBillingFields, $requiredProfileFields);    
+    CRM_Core_Resources::singleton()->addSetting(array('billing' => array('billingProfileIsHideable' => empty($potentiallyMissingRequiredFields))));
   }
 
   /**
@@ -929,12 +943,7 @@ SELECT  id
           'name' => 'contribution_note',
           'title' => ts('Contribution Note'),
         );
-        if ($gid && CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $gid, 'name') == 'contribution_batch_entry') {
-          $fields['Contribution'] = array_merge($contribFields, self::getContribBatchEntryFields());
-        }
-        else {
-          $fields['Contribution'] = $contribFields;
-        }
+        $fields['Contribution'] = array_merge($contribFields, self::getContribBatchEntryFields());
       }
     }
 
@@ -1083,6 +1092,9 @@ SELECT  id
     return isset($availableFields[$fieldName]);
   }
 
+  /**
+   * @return array|null
+   */
   static function getContribBatchEntryFields() {
     if (self::$_contriBatchEntryFields === NULL) {
       self::$_contriBatchEntryFields = array(
@@ -1094,6 +1106,10 @@ SELECT  id
           'name' => 'soft_credit',
           'title' => ts('Soft Credit'),
         ),
+        'soft_credit_type' => array(
+          'name' => 'soft_credit_type',
+          'title' => ts('Soft Credit Type'),
+        ),
         'product_name' => array(
           'name' => 'product_name',
           'title' => ts('Premiums'),
@@ -1107,6 +1123,9 @@ SELECT  id
     return self::$_contriBatchEntryFields;
   }
 
+  /**
+   * @return array|null
+   */
   public static function getMemberBatchEntryFields() {
     if (self::$_memberBatchEntryFields === NULL) {
       self::$_memberBatchEntryFields = array(