Merge pull request #3679 from yashodha/CRM-14951
[civicrm-core.git] / api / v3 / CustomField.php
index 0b65242c577dbcb92bea5f423b9bd1612676e637..e14a9b0f97d4375552858ddedd9ebc95f984a161 100644 (file)
@@ -1,11 +1,10 @@
 <?php
-// $Id$
 
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.3                                                |
+ | CiviCRM version 4.5                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2013                                |
+ | Copyright CiviCRM LLC (c) 2004-2014                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -33,7 +32,7 @@
  * @package CiviCRM_APIv3
  * @subpackage API_CustomField
  *
- * @copyright CiviCRM LLC (c) 2004-2013
+ * @copyright CiviCRM LLC (c) 2004-2014
  * @version $Id: CustomField.php 30879 2010-11-22 15:45:55Z shot $
  */
 
  */
 function civicrm_api3_custom_field_create($params) {
 
-  if (!(CRM_Utils_Array::value('option_type', $params))) {
-    if (CRM_Utils_Array::value('id', $params)) {
-      $params['option_type'] = 2;
-    }
-    else {
-      $params['option_type'] = 1;
-    }
-  }
-
-
   // Array created for passing options in params
   if (isset($params['option_values']) && is_array($params['option_values'])) {
     foreach ($params['option_values'] as $key => $value) {
@@ -85,26 +74,44 @@ function civicrm_api3_custom_field_create($params) {
       $params['option_weight'][$key] = $value['weight'];
     }
   }
+  $values = array();
   $customField = CRM_Core_BAO_CustomField::create($params);
-  civicrm_api('custom_field', 'getfields', array('version' => 3, 'cache_clear' => 1));
   _civicrm_api3_object_to_array_unique_fields($customField, $values[$customField->id]);
+  _civicrm_api3_custom_field_flush_static_caches();
   return civicrm_api3_create_success($values, $params, 'custom_field', $customField);
 }
 
+/**
+ * Flush static caches in functions that might have stored available custom fields
+ */
+function _civicrm_api3_custom_field_flush_static_caches(){
+  civicrm_api('custom_field', 'getfields', array('version' => 3, 'cache_clear' => 1));
+  CRM_Core_BAO_UFField::getAvailableFieldsFlat(TRUE);
+}
 /**
  * Adjust Metadata for Create action
- * 
+ *
  * @param array $params array or parameters determined by getfields
  */
 function _civicrm_api3_custom_field_create_spec(&$params) {
   $params['label']['api.required'] = 1;
   $params['custom_group_id']['api.required'] = 1;
+  $params['is_active']['api.default'] = 1;
+  $params['option_type'] = array(
+    'title' => 'This (boolean) field tells the BAO to create an option group for the field if the field type is appropriate',
+    'api.default' => 1,
+    'type' => CRM_Utils_Type::T_BOOLEAN,
+  );
+  $params['data_type']['api.default'] = 'String';
+  $params['is_active']['api.default'] = 1;
 }
 
 /**
  * Use this API to delete an existing custom group field.
  *
  * @param $params     Array id of the field to be deleted
+ *
+ * @return array
  * @example CustomFieldDelete.php
  *
  * {@example CustomFieldDelete.php 0}
@@ -125,7 +132,10 @@ function civicrm_api3_custom_field_delete($params) {
  * Use this API to get existing custom fields.
  *
  * @param array $params Array to search on
- *{@getfields CustomField_get}
+ *{*
+ *
+ * @return array
+@getfields CustomField_get}
  * @access public
  *
  **/
@@ -134,11 +144,11 @@ function civicrm_api3_custom_field_get($params) {
 }
 
 /*
- * Helper function to validate custom field values 
- * 
+ * Helper function to validate custom field values
+ *
  * @params Array   $params             Custom fields with values
- * @params Array   $errors             Reference fields to be check with 
- * @params Boolean $checkForDisallowed Check for disallowed elements 
+ * @params Array   $errors             Reference fields to be check with
+ * @params Boolean $checkForDisallowed Check for disallowed elements
  *                                     in params
  * @params Boolean $checkForRequired   Check for non present required elements
  *                                     in params
@@ -147,12 +157,17 @@ function civicrm_api3_custom_field_get($params) {
 
 /**
  * Helper function to validate custom field value
- * 
+ *
  * @params String $fieldName    Custom field name (eg: custom_8 )
  * @params Mixed  $value        Field value to be validate
  * @params Array  $fieldDetails Field Details
  * @params Array  $errors       Collect validation  errors
  *
+ * @param $fieldName
+ * @param $value
+ * @param $fieldDetails
+ * @param array $errors
+ *
  * @return Array  Validation errors
  * @todo remove this function - not in use but need to review functionality before
  * removing as it might be useful in wrapper layer
@@ -233,7 +248,7 @@ function _civicrm_api3_custom_field_validate_field($fieldName, $value, $fieldDet
       }
 
       $query = "
-SELECT count(*) 
+SELECT count(*)
   FROM civicrm_state_province
  WHERE id IN ('" . implode("','", $value) . "')";
       if (CRM_Core_DAO::singleValueQuery($query) < count($value)) {