Test fixes for All kind of Custom Values
[civicrm-core.git] / CRM / Core / BAO / UFField.php
index 3f1c0b333cb1039d4e6bde61d51c172c4e43bd82..83d56486b79d1c523fedc64661f5b903a5b799bd 100644 (file)
@@ -3,7 +3,7 @@
  +--------------------------------------------------------------------+
  | CiviCRM version 4.6                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2014                                |
+ | Copyright CiviCRM LLC (c) 2004-2015                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
  | GNU Affero General Public License or the licensing of CiviCRM,     |
  | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
  +--------------------------------------------------------------------+
-*/
+ */
 
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2014
+ * @copyright CiviCRM LLC (c) 2004-2015
  * $Id$
  *
  */
 class CRM_Core_BAO_UFField extends CRM_Core_DAO_UFField {
 
   /**
-   * Batch entry fields
+   * Batch entry fields.
    */
   private static $_contriBatchEntryFields = NULL;
   private static $_memberBatchEntryFields = NULL;
 
 
   /**
-   * Fetch object based on array of properties
+   * Fetch object based on array of properties.
    *
-   * @param array $params   (reference ) an assoc array of name/value pairs
-   * @param array $defaults (reference ) an assoc array to hold the flattened values
+   * @param array $params
+   *   (reference ) an assoc array of name/value pairs.
+   * @param array $defaults
+   *   (reference ) an assoc array to hold the flattened values.
    *
-   * @return CRM_Core_BAO_UFField object
-   * @static
+   * @return CRM_Core_BAO_UFField
    */
   public static function retrieve(&$params, &$defaults) {
     return CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_UFField', $params, $defaults);
@@ -62,11 +63,11 @@ class CRM_Core_BAO_UFField extends CRM_Core_DAO_UFField {
   /**
    * Get the form title.
    *
-   * @param int $id id of uf_form
-   *
-   * @return string title
+   * @param int $id
+   *   Id of uf_form.
    *
-   * @static
+   * @return string
+   *   title
    *
    */
   public static function getTitle($id) {
@@ -74,13 +75,15 @@ class CRM_Core_BAO_UFField extends CRM_Core_DAO_UFField {
   }
 
   /**
-   * Update the is_active flag in the db
+   * Update the is_active flag in the db.
    *
-   * @param int      $id         id of the database record
-   * @param boolean  $is_active  value we want to set the is_active field
+   * @param int $id
+   *   Id of the database record.
+   * @param bool $is_active
+   *   Value we want to set the is_active field.
    *
-   * @return Object              DAO object on sucess, null otherwise
-   * @static
+   * @return Object
+   *   DAO object on sucess, null otherwise
    */
   public static function setIsActive($id, $is_active) {
     //check if custom data profile field is disabled
@@ -100,11 +103,10 @@ class CRM_Core_BAO_UFField extends CRM_Core_DAO_UFField {
   /**
    * Delete the profile Field.
    *
-   * @param int  $id    Field Id
-   *
-   * @return boolean
+   * @param int $id
+   *   Field Id.
    *
-   * @static
+   * @return bool
    *
    */
   public static function del($id) {
@@ -116,15 +118,15 @@ class CRM_Core_BAO_UFField extends CRM_Core_DAO_UFField {
   }
 
   /**
-   * Check duplicate for duplicate field in a group
+   * Check duplicate for duplicate field in a group.
    *
-   * @param array $params an associative array with field and values
+   * @param array $params
+   *   An associative array with field and values.
    * @param $ids
    *
    * @return mixed
    * @ids   array $ids    array that containd ids
    *
-   * @static
    */
   public static function duplicateField($params, $ids) {
     $ufField = new CRM_Core_DAO_UFField();
@@ -189,14 +191,14 @@ WHERE cf.id IN (" . $customFieldIds . ") AND is_multiple = 1 LIMIT 0,1";
   }
 
   /**
-   * Add the UF Field
-   *
-   * @param array $params (reference) array containing the values submitted by the form
-   * @param array $ids array containing the id
+   * Add the UF Field.
    *
-   * @return CRM_Core_BAO_UFField object
+   * @param array $params
+   *   (reference) array containing the values submitted by the form.
+   * @param array $ids
+   *   Array containing the id.
    *
-   * @static
+   * @return CRM_Core_BAO_UFField
    *
    */
   public static function add(&$params, $ids = array()) {
@@ -245,9 +247,10 @@ WHERE cf.id IN (" . $customFieldIds . ") AND is_multiple = 1 LIMIT 0,1";
   }
 
   /**
-   * Automatically determine one weight and modify others
+   * Automatically determine one weight and modify others.
    *
-   * @param array $params UFField record, e.g. with 'weight', 'uf_group_id', and 'field_id'
+   * @param array $params
+   *   UFField record, e.g. with 'weight', 'uf_group_id', and 'field_id'.
    * @return int
    */
   public static function autoWeight($params) {
@@ -264,11 +267,12 @@ WHERE cf.id IN (" . $customFieldIds . ") AND is_multiple = 1 LIMIT 0,1";
   /**
    * Enable/disable profile field given a custom field id
    *
-   * @param int      $customFieldId     custom field id
-   * @param boolean  $is_active         set the is_active field
+   * @param int $customFieldId
+   *   Custom field id.
+   * @param bool $is_active
+   *   Set the is_active field.
    *
    * @return void
-   * @static
    */
   public static function setUFField($customFieldId, $is_active) {
     //find the profile id given custom field
@@ -286,11 +290,12 @@ WHERE cf.id IN (" . $customFieldIds . ") AND is_multiple = 1 LIMIT 0,1";
    * Copy existing profile fields to
    * new profile from the already built profile
    *
-   * @param int      $old_id  from which we need to copy
-   * @param boolean  $new_id  in which to copy
+   * @param int $old_id
+   *   From which we need to copy.
+   * @param bool $new_id
+   *   In which to copy.
    *
    * @return void
-   * @static
    */
   public static function copy($old_id, $new_id) {
     $ufField = new CRM_Core_DAO_UFField();
@@ -305,13 +310,13 @@ WHERE cf.id IN (" . $customFieldIds . ") AND is_multiple = 1 LIMIT 0,1";
   }
 
   /**
-   * Delete profile field given a custom field
+   * Delete profile field given a custom field.
    *
-   * @param int   $customFieldId      ID of the custom field to be deleted
+   * @param int $customFieldId
+   *   ID of the custom field to be deleted.
    *
    * @return void
    *
-   * @static
    */
   public static function delUFField($customFieldId) {
     //find the profile id given custom field id
@@ -328,11 +333,12 @@ WHERE cf.id IN (" . $customFieldIds . ") AND is_multiple = 1 LIMIT 0,1";
   /**
    * Enable/disable profile field given a custom group id
    *
-   * @param int      $customGroupId custom group id
-   * @param boolean  $is_active value we want to set the is_active field
+   * @param int $customGroupId
+   *   Custom group id.
+   * @param bool $is_active
+   *   Value we want to set the is_active field.
    *
    * @return void
-   * @static
    */
   public static function setUFFieldStatus($customGroupId, $is_active) {
     //find the profile id given custom group id
@@ -350,12 +356,12 @@ WHERE cf.id IN (" . $customFieldIds . ") AND is_multiple = 1 LIMIT 0,1";
   }
 
   /**
-   * Check the status of custom field used in uf fields
+   * Check the status of custom field used in uf fields.
    *
    * @param int $UFFieldId
    *
-   * @return boolean   false if custom field are disabled else true
-   * @static
+   * @return bool
+   *   false if custom field are disabled else true
    */
   public static function checkUFStatus($UFFieldId) {
     $fieldName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFField', $UFFieldId, 'field_name');
@@ -437,16 +443,18 @@ WHERE cf.id IN (" . $customFieldIds . ") AND is_multiple = 1 LIMIT 0,1";
    * Find out whether given profile group uses $required
    * and/or $optional profile types
    *
-   * @param integer $ufGroupId  profile id
-   * @param array   $required   array of types those are required
-   * @param array   $optional   array of types those are optional
+   * @param int $ufGroupId
+   *   Profile id.
+   * @param array $required
+   *   Array of types those are required.
+   * @param array $optional
+   *   Array of types those are optional.
    *
-   * @return boolean $valid
-   * @static
+   * @return bool
    */
   public static function checkValidProfileType($ufGroupId, $required, $optional = NULL) {
     if (!is_array($required) || empty($required)) {
-      return;
+      return FALSE;
     }
 
     $ufGroup = new CRM_Core_DAO_UFGroup();
@@ -488,8 +496,8 @@ WHERE cf.id IN (" . $customFieldIds . ") AND is_multiple = 1 LIMIT 0,1";
    *
    * @param int $ufGroupId
    *
-   * @return  true for mix profile else false
-   * @static
+   * @return bool
+   *   true for mix profile else false
    */
   public static function checkProfileType($ufGroupId) {
     $ufGroup = new CRM_Core_DAO_UFGroup();
@@ -549,16 +557,18 @@ WHERE cf.id IN (" . $customFieldIds . ") AND is_multiple = 1 LIMIT 0,1";
   /**
    * Get the profile type (eg: individual/organization/household)
    *
-   * @param int $ufGroupId     uf group id
-   * @param boolean $returnMixType this is true, then field type of  mix profile field is returned
-   * @param boolean $onlyPure      true if only pure profiles are required
+   * @param int $ufGroupId
+   *   Uf group id.
+   * @param bool $returnMixType
+   *   This is true, then field type of mix profile field is returned.
+   * @param bool $onlyPure
+   *   True if only pure profiles are required.
    *
    * @param bool $skipComponentType
    *
-   * @return string profile group_type
-   * @static
+   * @return string
+   *   profile group_type
    *
-   * TODO Why is this function in this class? It seems to be about the UFGroup.
    */
   public static function getProfileType($ufGroupId, $returnMixType = TRUE, $onlyPure = FALSE, $skipComponentType = FALSE) {
     $ufGroup = new CRM_Core_DAO_UFGroup();
@@ -573,16 +583,16 @@ WHERE cf.id IN (" . $customFieldIds . ") AND is_multiple = 1 LIMIT 0,1";
    * Get the profile type (eg: individual/organization/household)
    *
    * @param string $ufGroupType
-   * @param boolean $returnMixType this is true, then field type of  mix profile field is returned
-   * @param boolean $onlyPure      true if only pure profiles are required
+   * @param bool $returnMixType
+   *   This is true, then field type of mix profile field is returned.
+   * @param bool $onlyPure
+   *   True if only pure profiles are required.
    * @param bool $skipComponentType
    *
-   * @return string profile group_type
-   * @static
+   * @return string  profile group_type
    *
-   * TODO Why is this function in this class? It seems to be about the UFGroup.
    */
-  public static function calculateProfileType($ufGroupType, $returnMixType = TRUE, $onlyPure = FALSE, $skipComponentType= FALSE) {
+  public static function calculateProfileType($ufGroupType, $returnMixType = TRUE, $onlyPure = FALSE, $skipComponentType = FALSE) {
     // profile types
     $contactTypes = array('Contact', 'Individual', 'Household', 'Organization');
     $subTypes = CRM_Contact_BAO_ContactType::subTypes();
@@ -680,8 +690,8 @@ WHERE cf.id IN (" . $customFieldIds . ") AND is_multiple = 1 LIMIT 0,1";
    *
    * @param $ctype
    *
-   * @return  true for mix profile group else false
-   * @static
+   * @return bool
+   *   true for mix profile group else false
    */
   public static function checkProfileGroupType($ctype) {
     $ufGroup = new CRM_Core_DAO_UFGroup();
@@ -715,7 +725,7 @@ SELECT ufg.id as id
    *
    * @param int $profileID
    *
-   * @return boolean $result    true/false.
+   * @return bool
    */
   public static function checkSearchableORInSelector($profileID) {
     $result = FALSE;
@@ -743,7 +753,7 @@ SELECT  id
    *
    * @param int $profileID
    *
-   * @return void.
+   * @return void
    */
   public function resetInSelectorANDSearchable($profileID) {
     if (!$profileID) {
@@ -768,21 +778,29 @@ SELECT  id
    * transfer profile address data to billing fields
    * http://issues.civicrm.org/jira/browse/CRM-5869
    *
-   * @param string $key Field key - e.g. street_address-Primary, first_name
-   * @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
+   * @param string $key
+   *   Field key - e.g. street_address-Primary, first_name.
+   * @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)
+   * @return bool
+   *   Can the address block be hidden safe in the knowledge all fields are elsewhere collected (see CRM-15118)
    */
   public 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, is_required', 'options' => array(
-        'limit' => 0,
-      ))
+      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(
@@ -794,14 +812,14 @@ SELECT  id
       'city',
       'state_province',
       'postal_code',
-      'country'
+      'country',
     );
-    $requiredBillingFields = array_diff($validBillingFields, array('middle_name','supplemental_address_1'));
+    $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)) {
+      if (in_array($field['field_name'], $validBillingFields)) {
         $validProfileFields[] = $field['field_name'];
       }
       if ($field['is_required']) {
@@ -809,37 +827,37 @@ SELECT  id
       }
     }
 
-    if (!in_array($prefixName, $validProfileFields) ) {
-      return;
+    if (!in_array($prefixName, $validProfileFields)) {
+      return FALSE;
     }
 
     if (!empty($index) && (
-      // it's empty so we set it OR
-      !CRM_Utils_array::value($prefixName, $profileAddressFields)
+        // it's empty so we set it OR
+        !CRM_Utils_array::value($prefixName, $profileAddressFields)
         //we are dealing with billing id (precedence)
         || $index == $billing_id
         // we are dealing with primary & billing not set
         || ($index == 'Primary' && $profileAddressFields[$prefixName] != $billing_id)
         || ($index == CRM_Core_BAO_LocationType::getDefault()->id
-        && $profileAddressFields[$prefixName] != $billing_id
-        && $profileAddressFields[$prefixName] != 'Primary'
+          && $profileAddressFields[$prefixName] != $billing_id
+          && $profileAddressFields[$prefixName] != 'Primary'
+        )
       )
-    )
     ) {
       $profileAddressFields[$prefixName] = $index;
     }
 
     $potentiallyMissingRequiredFields = array_diff($requiredBillingFields, $requiredProfileFields);
-    CRM_Core_Resources::singleton()->addSetting(array('billing' => array('billingProfileIsHideable' => empty($potentiallyMissingRequiredFields))));
+    CRM_Core_Resources::singleton()
+      ->addSetting(array('billing' => array('billingProfileIsHideable' => empty($potentiallyMissingRequiredFields))));
   }
 
   /**
-   * Get a list of fields which can be added to profiles
+   * Get a list of fields which can be added to profiles.
    *
-   * @param int $gid: UF group ID
-   * @param array $defaults: Form defaults
+   * @param int $gid : UF group ID
+   * @param array $defaults : Form defaults
    * @return array, multidimensional; e.g. $result['FieldGroup']['field_name']['label']
-   * @static
    */
   public static function getAvailableFields($gid = NULL, $defaults = array()) {
     $fields = array(
@@ -1008,7 +1026,7 @@ SELECT  id
     ));
     //unset selected fields
     foreach ($groupFieldList as $key => $value) {
-      if (is_integer($key)) {
+      if (is_int($key)) {
         unset($fields['Individual'][$value], $fields['Household'][$value], $fields['Organization'][$value]);
         continue;
       }
@@ -1025,12 +1043,11 @@ SELECT  id
   }
 
   /**
-   * Get a list of fields which can be added to profiles
+   * Get a list of fields which can be added to profiles.
    *
    * @param bool $force
    *
    * @return array, multidimensional; e.g. $result['field_name']['label']
-   * @static
    */
   public static function getAvailableFieldsFlat($force = FALSE) {
     // FIXME reset when data model changes
@@ -1051,7 +1068,7 @@ SELECT  id
   }
 
   /**
-   * Determine whether the given field_name is valid
+   * Determine whether the given field_name is valid.
    *
    * @param string $fieldName
    * @return bool
@@ -1134,4 +1151,5 @@ SELECT  id
     }
     return self::$_memberBatchEntryFields;
   }
+
 }