From 0650862871a8a1ce97c2f66930255df04d007293 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Fri, 2 May 2014 17:02:48 -0700 Subject: [PATCH] CRM-13966 - Migrate custom ContactReference fields to use select2 --- CRM/Contact/Page/AJAX.php | 54 ++++++------------- CRM/Core/BAO/CustomField.php | 33 +++++------- CRM/Core/BAO/CustomGroup.php | 16 ------ CRM/Core/BAO/CustomValue.php | 8 +-- CRM/Core/BAO/UFGroup.php | 12 ----- CRM/Core/Form/Renderer.php | 36 +++++++++++++ CRM/Core/Page/AJAX.php | 1 + CRM/Custom/Form/Field.php | 9 +--- .../CRM/Custom/Form/ContactReference.tpl | 46 +++++++--------- 9 files changed, 89 insertions(+), 126 deletions(-) diff --git a/CRM/Contact/Page/AJAX.php b/CRM/Contact/Page/AJAX.php index 610e2b0efd..806486b5ee 100644 --- a/CRM/Contact/Page/AJAX.php +++ b/CRM/Contact/Page/AJAX.php @@ -36,6 +36,9 @@ * This class contains all contact related functions that are called using AJAX (jQuery) */ class CRM_Contact_Page_AJAX { + /** + * @deprecated + */ static function getContactList() { // if context is 'customfield' if (CRM_Utils_Array::value('context', $_GET) == 'customfield') { @@ -90,18 +93,23 @@ class CRM_Contact_Page_AJAX { CRM_Core_Page_AJAX::autocompleteResults(CRM_Utils_Array::value('values', $result), 'data'); } + /** + * Ajax callback for custom fields of type ContactReference + * + * Todo: Migrate contact reference fields to use EntityRef + */ static function contactReference() { - $name = CRM_Utils_Array::value('s', $_GET); + $name = CRM_Utils_Array::value('term', $_GET); $name = CRM_Utils_Type::escape($name, 'String'); $cfID = CRM_Utils_Type::escape($_GET['id'], 'Positive'); // check that this is a valid, active custom field of Contact Reference type - $params = array('id' => $cfID); + $params = array('id' => $cfID); $returnProperties = array('filter', 'data_type', 'is_active'); - $fldValues = $cf = array(); + $cf = array(); CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_CustomField', $params, $cf, $returnProperties); if (!$cf['id'] || !$cf['is_active'] || $cf['data_type'] != 'ContactReference') { - CRM_Core_Page_AJAX::autocompleteResults(array('error' => $name)); + CRM_Utils_System::civiExit('error'); } if (!empty($cf['filter'])) { @@ -113,7 +121,7 @@ class CRM_Contact_Page_AJAX { if (!empty($action) && !in_array($action, array('get', 'lookup')) ) { - CRM_Core_Page_AJAX::autocompleteResults(array('error' => $name)); + CRM_Utils_System::civiExit('error'); } } @@ -123,12 +131,7 @@ class CRM_Contact_Page_AJAX { $return = array_unique(array_merge(array('sort_name'), $list)); - $config = CRM_Core_Config::singleton(); - - $limit = 10; - if (!empty($_GET['limit'])) { - $limit = CRM_Utils_Type::escape($_GET['limit'], 'Positive'); - } + $limit = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'search_autocomplete_count', NULL, 10); $params = array('offset' => 0, 'rowCount' => $limit, 'version' => 3); foreach ($return as $fld) { @@ -166,7 +169,7 @@ class CRM_Contact_Page_AJAX { $contact = civicrm_api('Contact', 'Get', $params); if (!empty($contact['is_error'])) { - CRM_Core_Page_AJAX::autocompleteResults(array('error' => $name)); + CRM_Utils_System::civiExit('error'); } $contactList = array(); @@ -177,14 +180,10 @@ class CRM_Contact_Page_AJAX { $view[] = $value[$fld]; } } - $contactList[$value['id']] = implode(' :: ', $view); + $contactList[] = array('id' => $value['id'], 'text' => implode(' :: ', $view)); } - if (!$contactList) { - $contactList = array($name => $name); - } - - CRM_Core_Page_AJAX::autocompleteResults($contactList); + CRM_Utils_System::civiExit(json_encode($contactList)); } /** @@ -324,25 +323,6 @@ class CRM_Contact_Page_AJAX { CRM_Utils_System::civiExit(); } - /** - * Function to obtain list of permissioned employer for the given contact-id. - */ - static function getPermissionedEmployer() { - $cid = CRM_Utils_Type::escape($_GET['cid'], 'Integer'); - $name = trim(CRM_Utils_Type::escape($_GET['s'], 'String')); - $name = str_replace('*', '%', $name); - - $elements = CRM_Contact_BAO_Relationship::getPermissionedEmployer($cid, $name); - $results = array(); - if (!empty($elements)) { - foreach ($elements as $cid => $name) { - $results[$cid] = $name['name']; - } - } - CRM_Core_Page_AJAX::autocompleteResults($results); - } - - static function groupTree() { $gids = CRM_Utils_Type::escape($_GET['gids'], 'String'); echo CRM_Contact_BAO_GroupNestingCache::json($gids); diff --git a/CRM/Core/BAO/CustomField.php b/CRM/Core/BAO/CustomField.php index 21c0c79d6f..9efe8abf62 100644 --- a/CRM/Core/BAO/CustomField.php +++ b/CRM/Core/BAO/CustomField.php @@ -1015,18 +1015,23 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField { case 'Autocomplete-Select': static $customUrls = array(); + // Fixme: why is this a string in the first place?? + $attributes = array(); + if ($field->attributes) { + foreach(explode(' ', $field->attributes) as $at) { + if (strpos($at, '=')) { + list($k, $v) = explode('=', $at); + $attributes[$k] = trim($v, ' "'); + } + } + } if ($field->data_type == 'ContactReference') { - $qf->add('text', $elementName, $label, $field->attributes, + $attributes['class'] = (isset($attributes['class']) ? $attributes['class'] . ' ' : '') . 'crm-form-contact-reference huge'; + $attributes['data-api-entity'] = 'contact'; + $qf->add('text', $elementName, $label, $attributes, $useRequired && !$search ); - $hiddenEleName = $elementName . '_id'; - if (substr($elementName, -1) == ']') { - $hiddenEleName = substr($elementName, 0, -1) . '_id]'; - } - $qf->addElement('hidden', $hiddenEleName, '', array('id' => str_replace(array(']', '['), array('', '_'), $hiddenEleName))); - - //$urlParams = "className=CRM_Contact_Page_AJAX&fnName=getContactList&json=1&reset=1&context=customfield&id={$field->id}"; $urlParams = "context=customfield&id={$field->id}"; $customUrls[$elementName] = CRM_Utils_System::url('civicrm/ajax/contactref', @@ -1034,21 +1039,9 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField { FALSE, NULL, FALSE ); - $actualElementValue = $qf->getSubmitValue($hiddenEleName); - $qf->addRule($elementName, ts('Select a valid contact for %1.', array(1 => $label)), 'validContact', $actualElementValue); } else { // FIXME: This won't work with customFieldOptions hook - $attributes = array(); - // Fixme: why is this a string in the first place??? - if ($field->attributes) { - foreach(explode(' ', $field->attributes) as $at) { - if (strpos($at, '=')) { - list($k, $v) = explode('=', $at); - $attributes[$k] = trim($v, ' "'); - } - } - } $attributes += array( 'entity' => 'option_value', 'placeholder' => $placeholder, diff --git a/CRM/Core/BAO/CustomGroup.php b/CRM/Core/BAO/CustomGroup.php index df28a53afc..b36e861a05 100644 --- a/CRM/Core/BAO/CustomGroup.php +++ b/CRM/Core/BAO/CustomGroup.php @@ -1321,22 +1321,6 @@ ORDER BY civicrm_custom_group.weight, } break; - case 'Autocomplete-Select': - $hiddenEleName = $elementName . '_id'; - if (substr($elementName, -1) == ']') { - $hiddenEleName = substr($elementName, 0, -1) . '_id]'; - } - if ($field['data_type'] == "ContactReference") { - if (is_numeric($value)) { - $defaults[$hiddenEleName] = $value; - $defaults[$elementName] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $value, 'sort_name'); - } - } - else { - $defaults[$elementName] = $value; - } - break; - default: if ($field['data_type'] == "Float") { $defaults[$elementName] = (float)$value; diff --git a/CRM/Core/BAO/CustomValue.php b/CRM/Core/BAO/CustomValue.php index 32e56cbd7a..d0af961c21 100644 --- a/CRM/Core/BAO/CustomValue.php +++ b/CRM/Core/BAO/CustomValue.php @@ -183,12 +183,6 @@ class CRM_Core_BAO_CustomValue extends CRM_Core_DAO { $formValues[$key] = '%' . $formValues[$key] . '%'; } - $dataType = CRM_Core_DAO::getFieldValue('CRM_Core_BAO_CustomField', - substr($key, 7), 'data_type' - ); - if (($dataType == 'ContactReference') && ($htmlType == 'Autocomplete-Select')) { - $formValues[$key] = $formValues[$key . '_id']; - } } } @@ -215,4 +209,4 @@ class CRM_Core_BAO_CustomValue extends CRM_Core_DAO { $customValueID ); } -} \ No newline at end of file +} diff --git a/CRM/Core/BAO/UFGroup.php b/CRM/Core/BAO/UFGroup.php index 700a5eeb90..6e1c8038cf 100644 --- a/CRM/Core/BAO/UFGroup.php +++ b/CRM/Core/BAO/UFGroup.php @@ -2312,18 +2312,6 @@ AND ( entity_id IS NULL OR entity_id <= 0 ) } break; - case 'Autocomplete-Select': - if ($customFields[$customFieldId]['data_type'] == 'ContactReference') { - if (is_numeric($details[$name])) { - $defaults[$fldName . '_id'] = $details[$name]; - $defaults[$fldName] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $details[$name], 'sort_name'); - } - } - else { - $defaults[$fldName] = $details[$name]; - } - break; - case 'Select Date': // CRM-6681, set defult values according to date and time format (if any). $dateFormat = NULL; diff --git a/CRM/Core/Form/Renderer.php b/CRM/Core/Form/Renderer.php index 19297c8173..d72ae72197 100644 --- a/CRM/Core/Form/Renderer.php +++ b/CRM/Core/Form/Renderer.php @@ -186,6 +186,9 @@ class CRM_Core_Form_Renderer extends HTML_QuickForm_Renderer_ArraySmarty { elseif (strpos($class, 'crm-form-entityref') !== FALSE) { self::preProcessEntityRef($element); } + elseif (strpos($class, 'crm-form-contact-reference') !== FALSE) { + self::preprocessContactReference($element); + } if ($required) { $class .= ' required'; @@ -201,6 +204,8 @@ class CRM_Core_Form_Renderer extends HTML_QuickForm_Renderer_ArraySmarty { /** * Convert IDs to values and format for display + * + * @param $field HTML_QuickForm_element */ static function preProcessEntityRef($field) { $val = $field->getValue(); @@ -250,6 +255,37 @@ class CRM_Core_Form_Renderer extends HTML_QuickForm_Renderer_ArraySmarty { $el['html'] = implode('; ', $display) . ''; } + /** + * Pre-fill contact name for a custom field of type ContactReference + * + * Todo: Migrate contact reference fields to use EntityRef + * + * @param $field HTML_QuickForm_element + */ + static function preprocessContactReference($field) { + $val = $field->getValue(); + if ($val && is_numeric($val)) { + + $list = array_keys(CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, + 'contact_reference_options' + ), '1'); + + $return = array_unique(array_merge(array('sort_name'), $list)); + + $contact = civicrm_api('contact', 'getsingle', array('id' => $val, 'return' => $return, 'version' => 3)); + + if (!empty($contact['id'])) { + $view = array(); + foreach ($return as $fld) { + if (!empty($contact[$fld])) { + $view[] = $contact[$fld]; + } + } + $field->setAttribute('data-entity-value', json_encode(array('id' => $contact['id'], 'text' => implode(' :: ', $view)))); + } + } + } + /** * @param array $el * @param HTML_QuickForm_element $field diff --git a/CRM/Core/Page/AJAX.php b/CRM/Core/Page/AJAX.php index a6f2ef6c18..b9abf02faa 100644 --- a/CRM/Core/Page/AJAX.php +++ b/CRM/Core/Page/AJAX.php @@ -202,6 +202,7 @@ class CRM_Core_Page_AJAX { * @param array $results - If nested array, also provide: * @param string $val - array key to use as the value * @param string $key - array key to use as the key + * @deprecated */ static function autocompleteResults($results, $val='label', $key='id') { $output = array(); diff --git a/CRM/Custom/Form/Field.php b/CRM/Custom/Form/Field.php index 65199c2ec3..e7d043cda6 100644 --- a/CRM/Custom/Form/Field.php +++ b/CRM/Custom/Form/Field.php @@ -674,13 +674,8 @@ SELECT count(*) if (strpos($fields['filter'], 'entity=') !== FALSE) { $errors['filter'] = ts("Please do not include entity parameter (entity is always 'contact')"); } - elseif (strpos($fields['filter'], 'action=') === FALSE) { - $errors['filter'] = ts("Please specify 'action' parameter, it should be 'lookup' or 'get'"); - } - elseif (strpos($fields['filter'], 'action=get') === FALSE && - strpos($fields['filter'], 'action=lookup') === FALSE - ) { - $errors['filter'] = ts("Only 'get' and 'lookup' actions are supported."); + elseif (strpos($fields['filter'], 'action=get') === FALSE) { + $errors['filter'] = ts("Only 'get' action is supported."); } } $self->setDefaults(array('filter_selected', $fields['filter_selected'])); diff --git a/templates/CRM/Custom/Form/ContactReference.tpl b/templates/CRM/Custom/Form/ContactReference.tpl index 4a25f4e984..44f7f001a4 100644 --- a/templates/CRM/Custom/Form/ContactReference.tpl +++ b/templates/CRM/Custom/Form/ContactReference.tpl @@ -23,36 +23,28 @@ | see the CiviCRM license FAQ at http://civicrm.org/licensing | +--------------------------------------------------------------------+ *} +{* Js needed to initialize custom field of type ContactReference *} {literal} {/literal} -- 2.25.1