Merge pull request #7253 from jitendrapurohit/CRM-17520
[civicrm-core.git] / CRM / Core / BAO / CustomField.php
index 3d5b8b6eaf31c558bdcc91f2a6c8e02e7e688e9b..08ac5c9c02f346c1c9807e65eedd82e54e44b40f 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /*
   +--------------------------------------------------------------------+
-  | CiviCRM version 4.6                                                |
+  | CiviCRM version 4.7                                                |
   +--------------------------------------------------------------------+
   | Copyright CiviCRM LLC (c) 2004-2015                                |
   +--------------------------------------------------------------------+
  *
  * @package CRM
  * @copyright CiviCRM LLC (c) 2004-2015
- * $Id$
- *
  */
 
 /**
  * Business objects for managing custom data fields.
- *
  */
 class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
 
   /**
-   * Array for valid combinations of data_type & descriptions
+   * Array for valid combinations of data_type & descriptions.
    *
    * @var array
    */
   public static $_dataType = NULL;
 
   /**
-   * Array for valid combinations of data_type & html_type
+   * Array for valid combinations of data_type & html_type.
    *
    * @var array
    */
@@ -87,6 +84,10 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
   }
 
   /**
+   * Get data to html array.
+   *
+   * (Does this caching achieve anything?)
+   *
    * @return array
    */
   public static function dataToHtml() {
@@ -337,8 +338,7 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
    *   Value we want to set the is_active field.
    *
    * @return Object
-   *   DAO object on sucess, null otherwise
-   *
+   *   DAO object on success, null otherwise
    */
   public static function setIsActive($id, $is_active) {
 
@@ -357,7 +357,6 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
    *
    * @return string
    *   name
-   *
    */
   public static function getTitle($id) {
     return CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $id, 'label');
@@ -385,7 +384,6 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
    *
    * @return array
    *   an array of active custom fields.
-   *
    */
   public static function &getFields(
     $customDataType = 'Individual',
@@ -611,10 +609,8 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
    * @param bool $withMultiple
    *
    * @return array
-   *
-   *
    */
-  public static function &getFieldsForImport(
+  public static function getFieldsForImport(
     $contactType = 'Individual',
     $showAll = FALSE,
     $onlyParent = FALSE,
@@ -678,6 +674,7 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
    *   The key to parse.
    *
    * @param bool $all
+   *
    * @return int|null
    *   The id (if exists)
    */
@@ -747,7 +744,6 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
    *   True if used for search else false.
    * @param string $label
    *   Label for custom field.
-   *
    */
   public static function addQuickFormElement(
     &$qf,
@@ -822,9 +818,7 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
       $label = $field->label;
     }
 
-    /**
-     * at some point in time we might want to split the below into small functions
-     **/
+    // at some point in time we might want to split the below into small functions
 
     switch ($widget) {
       case 'Text':
@@ -990,7 +984,7 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
         if ($field->text_length) {
           $attributes['maxlength'] = $field->text_length;
         }
-        $qf->addWysiwyg($elementName, $label, $attributes, $search);
+        $qf->add('wysiwyg', $elementName, $label, $attributes, $search);
         break;
 
       case 'Autocomplete-Select':
@@ -1070,10 +1064,8 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
         break;
 
       case 'Link':
-        $element->setAttribute('onfocus', "if (!this.value) {this.value='http://';}");
-        $element->setAttribute('onblur', "if (this.value == 'http://') {this.value='';}");
         $element->setAttribute('class', "url");
-        $qf->addRule($elementName, ts('Enter a valid Website.'), 'wikiURL');
+        $qf->addRule($elementName, ts('Enter a valid web address beginning with \'http://\' or \'https://\'.'), 'wikiURL');
         break;
     }
     if ($field->is_view && !$search) {
@@ -1106,8 +1098,7 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
   }
 
   /**
-   * Given a custom field value, its id and the set of options
-   * find the display value for this field
+   * Find the display value for this field.
    *
    * @param mixed $value
    *   The custom field value.
@@ -1121,31 +1112,29 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
    *
    * @return string
    *   the display value
-   *
    */
   public static function getDisplayValue($value, $id, &$options, $contactID = NULL, $fieldID = NULL) {
     $option = &$options[$id];
     $attributes = &$option['attributes'];
     $html_type = $attributes['html_type'];
     $data_type = $attributes['data_type'];
-    $format = CRM_Utils_Array::value('format', $attributes);
 
     return self::getDisplayValueCommon($value,
       $option,
       $html_type,
       $data_type,
-      $format,
       $contactID,
       $fieldID
     );
   }
 
   /**
-   * @param $value
-   * @param $option
-   * @param $html_type
-   * @param $data_type
-   * @param null $format
+   * Get display value.
+   *
+   * @param string $value
+   * @param array $option
+   * @param string $html_type
+   * @param string $data_type
    * @param int $contactID
    * @param int $fieldID
    *
@@ -1156,7 +1145,6 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
     &$option,
     $html_type,
     $data_type,
-    $format = NULL,
     $contactID = NULL,
     $fieldID = NULL
   ) {
@@ -1177,16 +1165,19 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
     switch ($html_type) {
       case 'Radio':
         if ($data_type == 'Boolean') {
-          $option = array('No', 'Yes');
+          $options = array('No', 'Yes');
+        }
+        else {
+          $options = $option;
         }
         if (is_array($value)) {
           $display = NULL;
           foreach ($value as $data) {
-            $display .= $display ? ', ' . $option[$data] : $option[$data];
+            $display .= $display ? ', ' . $options[$data] : $options[$data];
           }
         }
         else {
-          $display = CRM_Utils_Array::value($value, $option);
+          $display = CRM_Utils_Array::value($value, $options);
         }
         break;
 
@@ -1223,6 +1214,11 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
       case 'AdvMulti-Select':
       case 'Multi-Select':
         if (is_array($value)) {
+          if ($html_type == 'CheckBox') {
+            // CRM-12989 fix
+            CRM_Utils_Array::formatArrayKeys($value);
+          }
+
           $checkedData = $value;
         }
         else {
@@ -1336,7 +1332,6 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
    * @param mixed $value
    *   If passed - dont fetch value from db,.
    *                               just format the given value
-   *
    */
   public static function setProfileDefaults(
     $customFieldId,
@@ -1439,19 +1434,15 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
   }
 
   /**
-   * @param int $contactID
-   * @param int $cfID
-   * @param int $fileID
-   * @param bool $absolute
+   * Get file url.
    *
-   * @return array
-   */
-  /**
    * @param int $contactID
    * @param int $cfID
    * @param int $fileID
    * @param bool $absolute
    *
+   * @param string $multiRecordWhereClause
+   *
    * @return array
    */
   public static function getFileURL($contactID, $cfID, $fileID = NULL, $absolute = FALSE, $multiRecordWhereClause = NULL) {
@@ -1534,7 +1525,7 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
    *   Custom field id.
    * @param array $customFormatted
    *   Formatted array.
-   * @param mix $value
+   * @param mixed $value
    *   Value of custom field.
    * @param string $customFieldExtend
    *   Custom field extends.
@@ -1637,13 +1628,7 @@ SELECT id
       $customFields[$customFieldId]['html_type'] == 'AdvMulti-Select'
     ) {
       if ($value) {
-        // Note that only during merge this is not an array,
-        // and you can directly use value, CRM-4385
-        if (is_array($value)) {
-          $value = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR,
-              array_values($value)
-            ) . CRM_Core_DAO::VALUE_SEPARATOR;
-        }
+        $value = CRM_Utils_Array::implodePadded($value);
       }
       else {
         $value = '';
@@ -1782,11 +1767,13 @@ SELECT $columnName
   }
 
   /**
+   * Get default custom table schema.
+   *
    * @param array $params
    *
    * @return array
    */
-  public static function &defaultCustomTableSchema(&$params) {
+  public static function defaultCustomTableSchema($params) {
     // add the id and extends_id
     $table = array(
       'name' => $params['name'],
@@ -1825,8 +1812,10 @@ SELECT $columnName
   }
 
   /**
-   * @param $field
-   * @param $operation
+   * Create custom field.
+   *
+   * @param CRM_Core_DAO_CustomField $field
+   * @param string $operation
    * @param bool $indexExist
    * @param bool $triggerRebuild
    */
@@ -1977,14 +1966,12 @@ WHERE  id IN ( %1, %2 )
   }
 
   /**
-   * Move a custom data field from one group (table) to another
+   * Move a custom data field from one group (table) to another.
    *
    * @param int $fieldID
    *   FK to civicrm_custom_field.
    * @param int $newGroupID
    *   FK to civicrm_custom_group.
-   *
-   * @return void
    */
   public static function moveField($fieldID, $newGroupID) {
     $validation = self::_moveFieldValidate($fieldID, $newGroupID);
@@ -2061,16 +2048,14 @@ AND    cf.id = %1";
    * Get custom option groups.
    *
    * @param array $includeFieldIds
-   *   Ids of custom fields for which.
-   *   option groups must be included.
+   *   Ids of custom fields for which option groups must be included.
    *
    * Currently this is required in the cases where option groups are to be included
    * for inactive fields : CRM-5369
    *
-   *
    * @return mixed
    */
-  public static function &customOptionGroup($includeFieldIds = NULL) {
+  public static function customOptionGroup($includeFieldIds = NULL) {
     static $customOptionGroup = NULL;
 
     $cacheKey = (empty($includeFieldIds)) ? 'onlyActive' : 'force';
@@ -2111,8 +2096,6 @@ INNER JOIN  civicrm_custom_field f ON ( g.id = f.option_group_id )
    *   Custom field id.
    * @param int $optionGroupId
    *   Option group id.
-   *
-   * @return void
    */
   public static function fixOptionGroups($customFieldId, $optionGroupId) {
     // check if option group belongs to any custom Field else delete
@@ -2132,13 +2115,10 @@ INNER JOIN  civicrm_custom_field f ON ( g.id = f.option_group_id )
   }
 
   /**
-   * Check if option group is related to more than one
-   * custom field
+   * Check if option group is related to more than one custom field.
    *
    * @param int $optionGroupId
    *   Option group id.
-   *
-   * @return void
    */
   public static function checkOptionGroup($optionGroupId) {
     $query = "
@@ -2155,8 +2135,10 @@ WHERE  option_group_id = {$optionGroupId}";
   }
 
   /**
+   * Get option group default.
+   *
    * @param int $optionGroupId
-   * @param $htmlType
+   * @param string $htmlType
    *
    * @return null|string
    */
@@ -2202,17 +2184,17 @@ ORDER BY html_type";
   }
 
   /**
+   * Post process function.
+   *
    * @param array $params
-   * @param $customFields
    * @param int $entityID
-   * @param $customFieldExtends
+   * @param string $customFieldExtends
    * @param bool $inline
    *
    * @return array
    */
   public static function postProcess(
     &$params,
-    &$customFields,
     $entityID,
     $customFieldExtends,
     $inline = FALSE
@@ -2246,8 +2228,10 @@ ORDER BY html_type";
   }
 
   /**
-   * @param $field
-   * @param $options
+   * Build option.
+   *
+   * @param array $field
+   * @param array $options
    *
    * @throws Exception
    */
@@ -2301,10 +2285,12 @@ SELECT label, value
   }
 
   /**
-   * @param $fieldLabel
+   * Get custom field ID.
+   *
+   * @param string $fieldLabel
    * @param null $groupTitle
    *
-   * @return null
+   * @return int|null
    */
   public static function getCustomFieldID($fieldLabel, $groupTitle = NULL) {
     $params = array(1 => array($fieldLabel, 'String'));
@@ -2474,6 +2460,8 @@ WHERE      f.id IN ($ids)";
   }
 
   /**
+   * Is this field a multi record field.
+   *
    * @param int $customId
    *
    * @return bool
@@ -2503,7 +2491,9 @@ WHERE cf.id = %1 AND cg.is_multiple = 1";
 
   /**
    * Does this field store a serialized string?
-   * @param CRM_Core_DAO_CustomField|array $field
+   *
+   * @param array $field
+   *
    * @return bool
    */
   public static function isSerialized($field) {
@@ -2514,6 +2504,8 @@ WHERE cf.id = %1 AND cg.is_multiple = 1";
   }
 
   /**
+   * Get options for field.
+   *
    * @param array $field
    * @param string|null $optionGroupName
    */