Skip to content

Commit

Permalink
(REF) Standardize UFField create function
Browse files Browse the repository at this point in the history
  • Loading branch information
colemanw committed Apr 19, 2019
1 parent cca658b commit 3963d91
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 69 deletions.
107 changes: 48 additions & 59 deletions CRM/Core/BAO/UFField.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,85 +52,74 @@ class CRM_Core_BAO_UFField extends CRM_Core_DAO_UFField {
* @return \CRM_Core_BAO_UFField
* @throws \API_Exception
*/
public static function create(&$params) {
// CRM-14756: kind of a hack-ish fix. If the user gives the id, uf_group_id is retrieved and then set.
if (isset($params['id'])) {
$groupId = civicrm_api3('UFField', 'getvalue', [
'return' => 'uf_group_id',
'id' => $params['id'],
]);
}
else {
$groupId = CRM_Utils_Array::value('uf_group_id', $params);
}
public static function create($params) {
$id = CRM_Utils_Array::value('id', $params);

$field_name = CRM_Utils_Array::value('field_name', $params);
// Merge in data from existing field
if (!empty($id)) {
$UFField = new CRM_Core_BAO_UFField();
$UFField->id = $params['id'];
if ($UFField->find(TRUE)) {
$defaults = $UFField->toArray();
// This will be calculated based on field name
unset($defaults['field_type']);
$params += $defaults;
}
else {
throw new API_Exception("UFFIeld id {$params['id']} not found.");
}
}

if (strpos($field_name, 'formatting') !== 0 && !CRM_Core_BAO_UFField::isValidFieldName($field_name)) {
// Validate field_name
if (strpos($params['field_name'], 'formatting') !== 0 && !CRM_Core_BAO_UFField::isValidFieldName($params['field_name'])) {
throw new API_Exception('The field_name is not valid');
}

if (!(CRM_Utils_Array::value('group_id', $params))) {
$params['group_id'] = $groupId;
// Supply default label if not set
if (empty($id) && !isset($params['label'])) {
$params['label'] = self::getAvailableFieldTitles()[$params['field_name']];
}

$fieldId = CRM_Utils_Array::value('id', $params);
if (!empty($fieldId)) {
$UFField = new CRM_Core_BAO_UFField();
$UFField->id = $fieldId;
if ($UFField->find(TRUE)) {
if (!(CRM_Utils_Array::value('group_id', $params))) {
// this copied here from previous api function - not sure if required
$params['group_id'] = $UFField->uf_group_id;
}
}
else {
throw new API_Exception("there is no field for this fieldId");
}
// Supply field_type if not set
if (empty($params['field_type']) && strpos($params['field_name'], 'formatting') !== 0) {
$params['field_type'] = CRM_Utils_Array::pathGet(self::getAvailableFieldsFlat(), [$params['field_name'], 'field_type']);
}
elseif (empty($params['field_type'])) {
$params['field_type'] = 'Formatting';
}

// Generate unique name for formatting fields
if ($params['field_name'] === 'formatting') {
$params['field_name'] = 'formatting_' . substr(uniqid(), -4);
}
$params['uf_group_id'] = $params['group_id'];

if (CRM_Core_BAO_UFField::duplicateField($params)) {
if (self::duplicateField($params)) {
throw new API_Exception("The field was not added. It already exists in this profile.");
}

// @todo fix BAO to be less weird.
$field_type = CRM_Utils_Array::value('field_type', $params);
$location_type_id = CRM_Utils_Array::value('location_type_id', $params, CRM_Utils_Array::value('website_type_id', $params));
$phone_type = CRM_Utils_Array::value('phone_type_id', $params, CRM_Utils_Array::value('phone_type', $params));
$params['field_name'] = [$field_type, $field_name, $location_type_id, $phone_type];
//@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);
}
// set values for uf field properties and save

// Set values for uf field properties and save
$ufField = new CRM_Core_DAO_UFField();
$ufField->copyValues($params);
$ufField->field_type = $params['field_name'][0];
$ufField->field_name = $params['field_name'][1];

//should not set location type id for Primary
$locationTypeId = NULL;
if ($params['field_name'][1] == 'url') {
$ufField->website_type_id = CRM_Utils_Array::value(2, $params['field_name']);
if ($params['field_name'] == 'url') {
$ufField->location_type_id = 'null';
}
else {
$locationTypeId = CRM_Utils_Array::value(2, $params['field_name']);
$ufField->website_type_id = NULL;
}
if ($locationTypeId) {
$ufField->location_type_id = $locationTypeId;
$ufField->website_type_id = 'null';
}
else {
$ufField->location_type_id = 'null';
if (!strstr($params['field_name'], 'phone')) {
$ufField->phone_type_id = 'null';
}

$ufField->phone_type_id = CRM_Utils_Array::value(3, $params['field_name'], 'NULL');

$ufField->save();

$fieldsType = CRM_Core_BAO_UFGroup::calculateGroupType($groupId, TRUE);
CRM_Core_BAO_UFGroup::updateGroupTypes($groupId, $fieldsType);
$fieldsType = CRM_Core_BAO_UFGroup::calculateGroupType($ufField->uf_group_id, TRUE);
CRM_Core_BAO_UFGroup::updateGroupTypes($ufField->uf_group_id, $fieldsType);

civicrm_api3('profile', 'getfields', ['cache_clear' => TRUE]);
return $ufField;
Expand Down Expand Up @@ -204,8 +193,8 @@ public static function del($id) {
public static function duplicateField($params) {
$ufField = new CRM_Core_DAO_UFField();
$ufField->uf_group_id = CRM_Utils_Array::value('uf_group_id', $params);
$ufField->field_type = $params['field_type'];
$ufField->field_name = $params['field_name'];
$ufField->field_type = CRM_Utils_Array::value('field_type', $params);
$ufField->field_name = CRM_Utils_Array::value('field_name', $params);
$ufField->website_type_id = CRM_Utils_Array::value('website_type_id', $params);
if (is_null(CRM_Utils_Array::value('location_type_id', $params, ''))) {
// primary location type have NULL value in DB
Expand All @@ -220,7 +209,7 @@ public static function duplicateField($params) {
$ufField->whereAdd("id <> " . $params['id']);
}

return ($ufField->find(TRUE) ? 1 : 0);
return (bool) $ufField->find(TRUE);
}

/**
Expand Down Expand Up @@ -280,10 +269,10 @@ public static function autoWeight($params) {
// fix for CRM-316
$oldWeight = NULL;

if (!empty($params['field_id'])) {
$oldWeight = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFField', $params['field_id'], 'weight', 'id');
if (!empty($params['field_id']) || !empty($params['id'])) {
$oldWeight = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFField', !empty($params['id']) ? $params['id'] : $params['field_id'], 'weight', 'id');
}
$fieldValues = ['uf_group_id' => $params['group_id']];
$fieldValues = ['uf_group_id' => !empty($params['uf_group_id']) ? $params['uf_group_id'] : $params['group_id']];
return CRM_Utils_Weight::updateOtherWeights('CRM_Core_DAO_UFField', $oldWeight, CRM_Utils_Array::value('weight', $params, 0), $fieldValues);
}

Expand Down
9 changes: 2 additions & 7 deletions CRM/UF/Form/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -531,13 +531,8 @@ public function postProcess() {
$name = $this->_selectFields[$params['field_name'][1]];
}

//Hack for Formatting Field Name
if ($params['field_name'][0] == 'Formatting') {
$fieldName = 'formatting_' . rand(1000, 9999);
}
else {
$fieldName = $params['field_name'][1];
}
// If field_name is missing, it's formatting
$fieldName = CRM_Utils_Array::value(1, $params['field_name'], 'formatting');

//check for duplicate fields
$apiFormattedParams = $params;
Expand Down
4 changes: 1 addition & 3 deletions js/model/crm.designer.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@
label: this.getLabel(),
entity_name: this.get('entityName'),
field_type: this.getFieldSchema().civiFieldType,
// For some reason the 'formatting' field gets a random number appended in core so we mimic that here.
// TODO: Why?
field_name: this.get('fieldName') == 'formatting' ? 'formatting_' + (Math.floor(Math.random() * 8999) + 1000) : this.get('fieldName')
field_name: this.get('fieldName')
});
return model;
}
Expand Down

0 comments on commit 3963d91

Please sign in to comment.