Merge pull request #4252 from civicrm/4.4
[civicrm-core.git] / CRM / UF / Form / Field.php
index 12d848060f6998c35aebba58bae4997f63213754..97a936c763ff711f29f985e3f2220decdbd090d2 100644 (file)
@@ -106,7 +106,8 @@ class CRM_UF_Form_Field extends CRM_Core_Form {
     $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
     if ($this->_gid) {
       $this->_title = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $this->_gid, 'title');
-      CRM_Utils_System::setTitle($this->_title . ' - ' . ts('CiviCRM Profile Fields'));
+
+      $this->setPageTitle(ts('Profile Field'));
 
       $url = CRM_Utils_System::url('civicrm/admin/uf/group/field',
         "reset=1&action=browse&gid={$this->_gid}"
@@ -224,7 +225,7 @@ class CRM_UF_Form_Field extends CRM_Core_Form {
       $defaults['field_name'] = array(
         $defaults['field_type'],
         ($defaults['field_type'] == "Formatting" ? "" : $defaults['field_name']),
-        $defaults['location_type_id'],
+        ($defaults['field_name'] == "url") ? $defaults['website_type_id'] : $defaults['location_type_id'],
         CRM_Utils_Array::value('phone_type_id', $defaults),
       );
       $this->_gid = $defaults['uf_group_id'];
@@ -252,7 +253,7 @@ class CRM_UF_Form_Field extends CRM_Core_Form {
 
     $fields = CRM_Core_BAO_UFField::getAvailableFields($this->_gid, $defaults);
 
-    $noSearchable = array();
+    $noSearchable = $hasWebsiteTypes = array();
     $addressCustomFields = array_keys(CRM_Core_BAO_CustomField::getFieldsForImport('Address'));
 
     foreach ($fields as $key => $value) {
@@ -270,7 +271,7 @@ class CRM_UF_Form_Field extends CRM_Core_Form {
           $this->_mapperFields[$key][$key1] = $value1['title'];
         }
         $hasLocationTypes[$key][$key1] = CRM_Utils_Array::value('hasLocationType', $value1);
-
+        $hasWebsiteTypes[$key][$key1] = CRM_Utils_Array::value('hasWebsiteType', $value1);
         // hide the 'is searchable' field for 'File' custom data
         if (isset($value1['data_type']) &&
           isset($value1['html_type']) &&
@@ -288,6 +289,7 @@ class CRM_UF_Form_Field extends CRM_Core_Form {
 
     $this->_location_types = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id');
     $defaultLocationType = CRM_Core_BAO_LocationType::getDefault();
+    $this->_website_types = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Website', 'website_type_id');
 
     /**
      * FIXME: dirty hack to make the default option show up first.  This
@@ -360,6 +362,9 @@ class CRM_UF_Form_Field extends CRM_Core_Form {
             if ($hasLocationTypes[$k][$key]) {
               $sel3[$k][$key] = $this->_location_types;
             }
+            elseif ($hasWebsiteTypes[$k][$key]) {
+              $sel3[$k][$key] = $this->_website_types;
+            }
             else {
               $sel3[$key] = NULL;
             }
@@ -384,21 +389,19 @@ class CRM_UF_Form_Field extends CRM_Core_Form {
 
     if (empty($formValues)) {
       for ($k = 1; $k < 4; $k++) {
-        if (!$defaults['field_name'][$k]) {
+        if (!isset($defaults['field_name'][$k])) {
           $js .= "{$formName}['field_name[$k]'].style.display = 'none';\n";
         }
       }
     }
     else {
       if (!empty($formValues['field_name'])) {
-        foreach ($formValues['field_name'] as $value) {
-          for ($k = 1; $k < 4; $k++) {
-            if (!isset($formValues['field_name'][$k]) || !$formValues['field_name'][$k]) {
-              $js .= "{$formName}['field_name[$k]'].style.display = 'none';\n";
-            }
-            else {
-              $js .= "{$formName}['field_name[$k]'].style.display = '';\n";
-            }
+        for ($key = 1; $key < 4; $key++) {
+          if (!isset($formValues['field_name'][$key])) {
+            $js .= "{$formName}['field_name[$key]'].style.display = 'none';\n";
+          }
+          else {
+            $js .= "{$formName}['field_name[$key]'].style.display = '';\n";
           }
         }
       }
@@ -498,10 +501,6 @@ class CRM_UF_Form_Field extends CRM_Core_Form {
       );
     }
 
-    if (isset($defaults['field_name']) && CRM_Utils_Array::value(1, $defaults['field_name']) == 'url-1') {
-      $defaults['field_name'][1] = 'url';
-    }
-
     $this->setDefaults($defaults);
   }
 
@@ -551,11 +550,6 @@ class CRM_UF_Form_Field extends CRM_Core_Form {
       $params['field_name'][1] = 'formatting_' . rand(1000, 9999);
     }
 
-    // temporary hack to for website
-    if ($params['field_name'][1] == 'url') {
-      $params['field_name'][1] = 'url-1';
-    }
-
     //check for duplicate fields
     if ($params["field_name"][0] != "Formatting" && CRM_Core_BAO_UFField::duplicateField($params, $ids)) {
       CRM_Core_Session::setStatus(ts('The selected field already exists in this profile.'), ts('Field Not Added'), 'error');
@@ -714,7 +708,7 @@ class CRM_UF_Form_Field extends CRM_Core_Form {
   }
 
   /**
-   * validation rule to prevent multiple fields of primary location type and the same communication type.
+   * Validation rule to prevent multiple fields of primary location type within the same communication type.
    *
    * @param Array   $fields            Submitted fields
    * @param String  $profileFieldName  Group Id.
@@ -729,6 +723,7 @@ class CRM_UF_Form_Field extends CRM_Core_Form {
     $checkPrimary = array('phone' => 'civicrm_phone.phone', 'phone_and_ext' => 'civicrm_phone.phone');
     $whereCheck = NULL;
     $primaryOfSameTypeFound = NULL;
+    $fieldID = empty($fields['field_id']) ? 0 : $fields['field_id'];
     // Is this a primary location type field of interest
     if (array_key_exists($profileFieldName, $checkPrimary)) {
       $whereCheck = $checkPrimary[$profileFieldName];
@@ -740,8 +735,9 @@ class CRM_UF_Form_Field extends CRM_Core_Form {
 
       foreach ($groupFields as $groupField) {
         // if it is a phone
-        if ($groupField['where'] == $whereCheck && is_null($groupField['location_type_id'])) {
+        if ($groupField['where'] == $whereCheck && is_null($groupField['location_type_id']) && $groupField['field_id']  != $fieldID) {
           $primaryOfSameTypeFound = $groupField['title'];
+          break;
         }
       }
       if ($primaryOfSameTypeFound) {
@@ -755,6 +751,9 @@ class CRM_UF_Form_Field extends CRM_Core_Form {
    *
    * @param array $fields posted values of the form
    *
+   * @param $files
+   * @param $self
+   *
    * @return array list of errors to be posted back to the form
    * @static
    * @access public
@@ -813,9 +812,7 @@ class CRM_UF_Form_Field extends CRM_Core_Form {
     // Get list of fields already in the group
     $groupFields = CRM_Core_BAO_UFGroup::getFields($fields['group_id'], FALSE, NULL, NULL, NULL, TRUE, NULL, TRUE);
     // Check if we already added a primary field of the same communication type
-    if (empty($fields['field_id'])) {
-      self::formRulePrimaryCheck($fields, $profileFieldName, $groupFields, $errors);
-    }
+    self::formRulePrimaryCheck($fields, $profileFieldName, $groupFields, $errors);
 
     //check profile is configured for double option process
     //adding group field, email field should be present in the group