Merge pull request #4004 from civicrm/4.4
[civicrm-core.git] / CRM / UF / Form / Field.php
index 5664d5df30518b47ea3cab648b6ea44b1bd5d6a9..97a936c763ff711f29f985e3f2220decdbd090d2 100644 (file)
@@ -1,9 +1,9 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.4                                                |
+ | CiviCRM version 4.5                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2013                                |
+ | Copyright CiviCRM LLC (c) 2004-2014                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -28,7 +28,7 @@
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2013
+ * @copyright CiviCRM LLC (c) 2004-2014
  * $Id$
  *
  */
@@ -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');
@@ -713,11 +707,53 @@ class CRM_UF_Form_Field extends CRM_Core_Form {
     }
   }
 
+  /**
+   * 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.
+   * @param Array   $groupFields       List of fields already in the group
+   * @param Array   $errors            Collect errors
+   *
+   * @static
+   * @access public
+   */
+  static function formRulePrimaryCheck($fields, $profileFieldName, $groupFields, &$errors) {
+    //FIXME: This may need to also apply to website fields if they are refactored to allow more than one per profile
+    $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];
+    }
+    $potentialLocationType = CRM_Utils_Array::value(2, $fields['field_name']);
+
+    if ($whereCheck && $potentialLocationType == 0) {
+      $primaryOfSameTypeFound = '';
+
+      foreach ($groupFields as $groupField) {
+        // if it is a phone
+        if ($groupField['where'] == $whereCheck && is_null($groupField['location_type_id']) && $groupField['field_id']  != $fieldID) {
+          $primaryOfSameTypeFound = $groupField['title'];
+          break;
+        }
+      }
+      if ($primaryOfSameTypeFound) {
+        $errors['field_name'] = ts('You have already added a primary location field of this type: %1', $primaryOfSameTypeFound);
+      }
+    }
+  }
+
   /**
    * global validation rules for the 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
@@ -737,14 +773,14 @@ class CRM_UF_Form_Field extends CRM_Core_Form {
       $errors['is_view'] = ts('A View Only field cannot be required');
     }
 
-    $fieldName = $fields['field_name'][0];
-    if (!$fieldName) {
+    $entityName = $fields['field_name'][0];
+    if (!$entityName) {
       $errors['field_name'] = ts('Please select a field name');
     }
 
-    if ($in_selector && in_array($fieldName, array(
+    if ($in_selector && in_array($entityName, array(
       'Contribution', 'Participant', 'Membership', 'Activity'))) {
-      $errors['in_selector'] = ts("'In Selector' cannot be checked for %1 fields.", array(1 => $fieldName));
+      $errors['in_selector'] = ts("'In Selector' cannot be checked for %1 fields.", array(1 => $entityName));
     }
 
     $isCustomField = FALSE;
@@ -773,6 +809,11 @@ 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
+    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
     //fixed for  issue CRM-2861 & CRM-4153