Merge remote-tracking branch 'upstream/4.5' into 4.5-master-2015-03-03-23-44-14
[civicrm-core.git] / CRM / Core / BAO / CustomField.php
index 3b8536e8a0c35add12554fa5634f8f64a7adc244..17b461639c7fdea7010d5f95ba51f45573ff316e 100644 (file)
@@ -23,7 +23,7 @@
   | GNU Affero General Public License or the licensing of CiviCRM,     |
   | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
   +--------------------------------------------------------------------+
-*/
+ */
 
 /**
  *
@@ -43,7 +43,6 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
    * Array for valid combinations of data_type & descriptions
    *
    * @var array
-   * @static
    */
   public static $_dataType = NULL;
 
@@ -51,7 +50,6 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
    * Array for valid combinations of data_type & html_type
    *
    * @var array
-   * @static
    */
   public static $_dataToHtml = NULL;
 
@@ -59,17 +57,14 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
    * Array to hold (formatted) fields for import
    *
    * @var array
-   * @static
    */
   public static $_importFields = NULL;
 
   /**
-   * Build and retrieve the list of data types and descriptions
-   *
-   * @param NULL
+   * Build and retrieve the list of data types and descriptions.
    *
-   * @return array        Data type => Description
-   * @static
+   * @return array
+   *   Data type => Description
    */
   public static function &dataType() {
     if (!(self::$_dataType)) {
@@ -123,15 +118,14 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
   }
 
   /**
-   * Takes an associative array and creates a custom field object
+   * Takes an associative array and creates a custom field object.
    *
    * This function is invoked from within the web form layer and also from the api layer
    *
    * @param array $params
    *   (reference) an assoc array of name/value pairs.
    *
-   * @return CRM_Core_DAO_CustomField object
-   * @static
+   * @return CRM_Core_DAO_CustomField
    */
   public static function create(&$params) {
     $origParams = array_merge(array(), $params);
@@ -284,7 +278,8 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
     $customField->is_searchable = CRM_Utils_Array::value('is_searchable', $params, FALSE);
     $customField->in_selector = CRM_Utils_Array::value('in_selector', $params, FALSE);
     $customField->is_search_range = CRM_Utils_Array::value('is_search_range', $params, FALSE);
-    $customField->is_active = CRM_Utils_Array::value('is_active', $params, FALSE);
+    //CRM-15792 - Custom field gets disabled if is_active not set
+    $customField->is_active = CRM_Utils_Array::value('is_active', $params, TRUE);
     $customField->is_view = CRM_Utils_Array::value('is_view', $params, FALSE);
     $customField->save();
 
@@ -319,31 +314,30 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
   }
 
   /**
-   * 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.
    *
-   * @return CRM_Core_DAO_CustomField object
-   * @static
+   * @return CRM_Core_DAO_CustomField
    */
   public static function retrieve(&$params, &$defaults) {
     return CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_CustomField', $params, $defaults);
   }
 
   /**
-   * 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 bool $is_active
    *   Value we want to set the is_active field.
    *
-   * @return Object            DAO object on sucess, null otherwise
+   * @return Object
+   *   DAO object on sucess, null otherwise
    *
-   * @static
    */
   public static function setIsActive($id, $is_active) {
 
@@ -360,9 +354,8 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
    * @param int $id
    *   Id of field.
    *
-   * @return string name
-   *
-   * @static
+   * @return string
+   *   name
    *
    */
   public static function getTitle($id) {
@@ -389,9 +382,9 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
    * @param bool $checkPermission
    *   If false, do not include permissioning clause.
    *
-   * @return array      $fields - an array of active custom fields.
+   * @return array
+   *   an array of active custom fields.
    *
-   * @static
    */
   public static function &getFields(
     $customDataType = 'Individual',
@@ -612,9 +605,9 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
    *
    * @param bool $withMultiple
    *
-   * @return array   $fields -
+   * @return array
+   *
    *
-   * @static
    */
   public static function &getFieldsForImport(
     $contactType = 'Individual',
@@ -674,14 +667,14 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
   }
 
   /**
-   * Get the field id from an import key
+   * Get the field id from an import key.
    *
    * @param string $key
    *   The key to parse.
    *
    * @param bool $all
-   * @return int|null         The id (if exists)
-   * @static
+   * @return int|null
+   *   The id (if exists)
    */
   public static function getKeyID($key, $all = FALSE) {
     $match = array();
@@ -700,14 +693,13 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
   }
 
   /**
-   * Use the cache to get all values of a specific custom field
+   * Use the cache to get all values of a specific custom field.
    *
    * @param int $fieldID
    *   The custom field ID.
    *
-   * @return CRM_Core_DAO_CustomField $field  the field object
-   * @static
-   * public
+   * @return CRM_Core_DAO_CustomField
+   *   The field object.
    */
   public static function getFieldObject($fieldID) {
     $field = new CRM_Core_DAO_CustomField();
@@ -735,7 +727,7 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
   }
 
   /**
-   * This function for building custom fields
+   * This function for building custom fields.
    *
    * @param CRM_Core_Form $qf
    *   Form object (reference).
@@ -751,7 +743,6 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
    * @param string $label
    *   Label for custom field.
    *
-   * @static
    */
   public static function addQuickFormElement(
     &$qf,
@@ -1090,11 +1081,6 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
    *
    * @param object $field
    *   The field object.
-   *
-   * @return boolean
-   *
-   * @static
-   *
    */
   public static function deleteField($field) {
     CRM_Utils_System::flushCache();
@@ -1112,8 +1098,6 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
     $field->delete();
     CRM_Core_BAO_UFField::delUFField($field->id);
     CRM_Utils_Weight::correctDuplicateWeights('CRM_Core_DAO_CustomField');
-
-    return;
   }
 
   /**
@@ -1130,9 +1114,9 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
    * @param int $contactID
    * @param int $fieldID
    *
-   * @return string   the display value
+   * @return string
+   *   the display value
    *
-   * @static
    */
   public static function getDisplayValue($value, $id, &$options, $contactID = NULL, $fieldID = NULL) {
     $option = &$options[$id];
@@ -1162,7 +1146,7 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
    *
    * @return array|mixed|null|string
    */
-  static function getDisplayValueCommon(
+  public static function getDisplayValueCommon(
     $value,
     &$option,
     $html_type,
@@ -1329,11 +1313,9 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
         break;
 
       case 'File':
-        if ($contactID) {
-          $url = self::getFileURL($contactID, $fieldID, $value);
-          if ($url) {
-            $display = $url['file_url'];
-          }
+        if ($value) {
+          $icons = CRM_Core_BAO_File::paperIconAttachment('*', $value);
+          $display = $icons[$value];
         }
         break;
 
@@ -1358,7 +1340,7 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
   }
 
   /**
-   * Set default values for custom data used in profile
+   * Set default values for custom data used in profile.
    *
    * @param int $customFieldId
    *   Custom field id.
@@ -1374,9 +1356,8 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
    *   If passed - dont fetch value from db,.
    *                               just format the given value
    *
-   * @static
    */
-  static function setProfileDefaults(
+  public static function setProfileDefaults(
     $customFieldId,
     $elementName,
     &$defaults,
@@ -1566,7 +1547,7 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
   }
 
   /**
-   * Format custom fields before inserting
+   * Format custom fields before inserting.
    *
    * @param int $customFieldId
    *   Custom field id.
@@ -1587,10 +1568,10 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
    * @param bool $includeViewOnly
    *   If true, fields marked 'View Only' are included. Required for APIv3.
    *
-   * @return array $customFormatted formatted custom field array
-   * @static
+   * @return array|NULL
+   *   formatted custom field array
    */
-  static function formatCustomField(
+  public static function formatCustomField(
     $customFieldId, &$customFormatted, $value,
     $customFieldExtend, $customValueId = NULL,
     $entityId = NULL,
@@ -1625,12 +1606,12 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
     );
 
     if (!array_key_exists($customFieldId, $customFields)) {
-      return;
+      return NULL;
     }
 
     // return if field is a 'code' field
     if (!$includeViewOnly && !empty($customFields[$customFieldId]['is_view'])) {
-      return;
+      return NULL;
     }
 
     list($tableName, $columnName, $groupID) = self::getTableColumnGroup($customFieldId);
@@ -1928,15 +1909,15 @@ SELECT $columnName
   }
 
   /**
-   * Determine whether it would be safe to move a field
+   * Determine whether it would be safe to move a field.
    *
    * @param int $fieldID
    *   FK to civicrm_custom_field.
    * @param int $newGroupID
    *   FK to civicrm_custom_group.
    *
-   * @return array(
-   * string) or TRUE
+   * @return array
+   *   array(string) or TRUE
    */
   public static function _moveFieldValidate($fieldID, $newGroupID) {
     $errors = array();
@@ -2061,15 +2042,15 @@ WHERE  id IN ( %1, %2 )
   }
 
   /**
-   * Get the database table name and column name for a custom field
+   * Get the database table name and column name for a custom field.
    *
    * @param int $fieldID
    *   The fieldID of the custom field.
    * @param bool $force
    *   Force the sql to be run again (primarily used for tests).
    *
-   * @return array           - fatal is fieldID does not exists, else array of tableName, columnName
-   * @static
+   * @return array
+   *   fatal is fieldID does not exists, else array of tableName, columnName
    */
   public static function getTableColumnGroup($fieldID, $force = FALSE) {
     $cacheKey = "CRM_Core_DAO_CustomField_CustomGroup_TableColumn_{$fieldID}";
@@ -2096,17 +2077,17 @@ AND    cf.id = %1";
   }
 
   /**
-   * Get custom option groups
+   * Get custom option groups.
    *
    * @param array $includeFieldIds
    *   Ids of custom fields for which.
-   * option groups must be included.
+   *   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 $customOptionGroup@static
+   * @return mixed
    */
   public static function &customOptionGroup($includeFieldIds = NULL) {
     static $customOptionGroup = NULL;
@@ -2143,7 +2124,7 @@ INNER JOIN  civicrm_custom_field f ON ( g.id = f.option_group_id )
   }
 
   /**
-   * Fix orphan groups
+   * Fix orphan groups.
    *
    * @param int $customFieldId
    *   Custom field id.
@@ -2151,7 +2132,6 @@ INNER JOIN  civicrm_custom_field f ON ( g.id = f.option_group_id )
    *   Option group id.
    *
    * @return void
-   * @static
    */
   public static function fixOptionGroups($customFieldId, $optionGroupId) {
     // check if option group belongs to any custom Field else delete
@@ -2178,7 +2158,6 @@ INNER JOIN  civicrm_custom_field f ON ( g.id = f.option_group_id )
    *   Option group id.
    *
    * @return void
-   * @static
    */
   public static function checkOptionGroup($optionGroupId) {
     $query = "
@@ -2250,7 +2229,7 @@ ORDER BY html_type";
    *
    * @return array
    */
-  static function postProcess(
+  public static function postProcess(
     &$params,
     &$customFields,
     $entityID,
@@ -2379,7 +2358,6 @@ WHERE      ( f.label = %1 OR f.name = %1 )
 
   /**
    * Given ID of a custom field, return its name as well as the name of the custom group it belongs to.
-   *
    */
   public static function getNameFromID($ids) {
     if (is_array($ids)) {
@@ -2409,10 +2387,10 @@ WHERE      f.id IN ($ids)";
    *
    * @param array $params
    *   Custom data submitted.
-   * ie array( 'custom_1' => 'validate me' );
+   *   ie array( 'custom_1' => 'validate me' );
    *
-   * @return array $errors validation errors.
-   * @static
+   * @return array
+   *   validation errors.
    */
   public static function validateCustomData($params) {
     $errors = array();
@@ -2549,4 +2527,5 @@ WHERE cf.id = %1 AND cg.is_multiple = 1";
     // FIXME: Currently the only way to know if data is serialized is by looking at the html_type. It would be cleaner to decouple this.
     return ($field['html_type'] == 'CheckBox' || strpos($field['html_type'], 'Multi') !== FALSE);
   }
+
 }