Merge pull request #3679 from yashodha/CRM-14951
[civicrm-core.git] / api / v3 / UFField.php
index 86522551e66858d92e1de5af5dc4ffe7f5fa7a6d..2e9cf6794462ad5eacaa7191717a023bd4cafc5e 100644 (file)
@@ -2,9 +2,9 @@
 
 /*
  +--------------------------------------------------------------------+
- | 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.                                    |
  |                                                                    |
@@ -32,7 +32,7 @@
  * @package CiviCRM_APIv3
  * @subpackage API_UF
  *
- * @copyright CiviCRM LLC (c) 2004-2013
+ * @copyright CiviCRM LLC (c) 2004-2014
  * @version $Id: UFField.php 30171 2010-10-14 09:11:27Z mover $
  *
  */
 /**
  * Defines 'uf field' within a group.
  *
- * @param $groupId int Valid uf_group id
- *
  * @param $params  array  Associative array of property name/value pairs to create new uf field.
  *
+ * @throws API_Exception
+ * @internal param int $groupId Valid uf_group id
+ *
  * @return Newly created $ufFieldArray array
  *
  * @access public
@@ -54,7 +55,7 @@ function civicrm_api3_uf_field_create($params) {
   civicrm_api3_verify_one_mandatory($params, NULL, array('field_name', 'uf_group_id'));
   $groupId = CRM_Utils_Array::value('uf_group_id', $params);
   if ((int) $groupId < 1) {
-    return civicrm_api3_create_error('Params must be a field_name-carrying array and a positive integer.');
+    throw new API_Exception('Params must be a field_name-carrying array and a positive integer.');
   }
 
   $field_type       = CRM_Utils_Array::value('field_type', $params);
@@ -63,7 +64,7 @@ function civicrm_api3_uf_field_create($params) {
   $phone_type       = CRM_Utils_Array::value('phone_type_id', $params, CRM_Utils_Array::value('phone_type', $params));
 
   if (! CRM_Core_BAO_UFField::isValidFieldName($field_name)) {
-    return civicrm_api3_create_error('The field_name is not valid');
+    throw new API_Exception('The field_name is not valid');
   }
   $params['field_name'] = array($field_type, $field_name, $location_type_id, $phone_type);
 
@@ -71,12 +72,12 @@ function civicrm_api3_uf_field_create($params) {
     $params['group_id'] = $groupId;
   }
 
-  $ids = array();
+  $ids = $ufFieldArray = array();
   $ids['uf_group'] = $groupId;
 
   $fieldId = CRM_Utils_Array::value('id', $params);
   if (!empty($fieldId)) {
-    $UFField = new CRM_core_BAO_UFField();
+    $UFField = new CRM_Core_BAO_UFField();
     $UFField->id = $fieldId;
     if ($UFField->find(TRUE)) {
       $ids['uf_group'] = $UFField->uf_group_id;
@@ -86,14 +87,15 @@ function civicrm_api3_uf_field_create($params) {
       }
     }
     else {
-      return civicrm_api3_create_error("there is no field for this fieldId");
+      throw new API_Exception("there is no field for this fieldId");
     }
     $ids['uf_field'] = $fieldId;
   }
 
   if (CRM_Core_BAO_UFField::duplicateField($params, $ids)) {
-    return civicrm_api3_create_error("The field was not added. It already exists in this profile.");
+    throw new API_Exception("The field was not added. It already exists in this profile.");
   }
+  //@todo why is this even optional? Surely weight should just be 'managed' ??
   if (CRM_Utils_Array::value('option.autoweight', $params, TRUE)) {
     $params['weight'] = CRM_Core_BAO_UFField::autoWeight($params);
   }
@@ -103,19 +105,25 @@ function civicrm_api3_uf_field_create($params) {
   CRM_Core_BAO_UFGroup::updateGroupTypes($groupId, $fieldsType);
 
   _civicrm_api3_object_to_array($ufField, $ufFieldArray[$ufField->id]);
+  civicrm_api3('profile', 'getfields', array('cache_clear' => TRUE));
   return civicrm_api3_create_success($ufFieldArray, $params);
 }
 
 /**
  * Gets field for civicrm_uf_field create
  *
+ * @param $params
+ *
  * @return array fields valid for other functions
  */
 function _civicrm_api3_uf_field_create_spec(&$params) {
   $params['option.autoweight'] = array(
     'title' => "Automatically adjust weights in UFGroup to align with UFField",
-    'type' => CRM_Utils_Type::T_BOOLEAN
+    'type' => CRM_Utils_Type::T_BOOLEAN,
+    'api.default' => TRUE,
   );
+  $params['created_id']['api.default'] = 'user_contact_id';
+  $params['is_active']['api.default'] = TRUE;
 }
 
 /**
@@ -137,7 +145,10 @@ function civicrm_api3_uf_field_get($params) {
 /**
  * Delete uf field
  *
- * @param $fieldId int  Valid uf_field id that to be deleted
+ * @param $params
+ *
+ * @throws API_Exception
+ * @internal param int $fieldId Valid uf_field id that to be deleted
  *
  * @return true on successful delete or return error
  *
@@ -150,7 +161,7 @@ function civicrm_api3_uf_field_delete($params) {
 
   $ufGroupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFField', $fieldId, 'uf_group_id');
   if (!$ufGroupId) {
-    return civicrm_api3_create_error('Invalid value for field_id.');
+    throw new API_Exception('Invalid value for field_id.');
   }
 
   $result = CRM_Core_BAO_UFField::del($fieldId);
@@ -160,8 +171,8 @@ function civicrm_api3_uf_field_delete($params) {
 
   return civicrm_api3_create_success($result, $params);
 }
-/*
- * field id accepted for backward compat - unset required on id
+/**
+ * field id accepted for backward compatibility - unset required on id
  */
 function _civicrm_api3_uf_field_delete_spec(&$params) {
   // legacy support for field_id