* 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') {
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'])) {
if (!empty($action) &&
!in_array($action, array('get', 'lookup'))
) {
- CRM_Core_Page_AJAX::autocompleteResults(array('error' => $name));
+ CRM_Utils_System::civiExit('error');
}
}
$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) {
$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();
$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));
}
/**
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);
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',
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,
}
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;
$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'];
- }
}
}
$customValueID
);
}
-}
\ No newline at end of file
+}
}
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;
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';
/**
* Convert IDs to values and format for display
+ *
+ * @param $field HTML_QuickForm_element
*/
static function preProcessEntityRef($field) {
$val = $field->getValue();
$el['html'] = implode('; ', $display) . '<input type="hidden" value="'. $field->getValue() . '" name="' . $field->getAttribute('name') . '">';
}
+ /**
+ * 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
* @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();
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']));
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*}
+{* Js needed to initialize custom field of type ContactReference *}
{literal}
<script type="text/javascript">
-CRM.$(function($) {
- var url = "{/literal}{$customUrls.$element_name}{literal}";
- var custom = "{/literal}#{$element_name|replace:']':''|replace:'[':'_'}{literal}";
- var custom_id = "{/literal}input[name=\"{$element_name|cat:'_id'|regex_replace:'/\]_id$/':'_id]'}\"]{literal}";
+ CRM.$(function($) {
+ var $field = $("{/literal}#{$element_name|replace:']':''|replace:'[':'_'}{literal}");
- var customObj = cj(custom);
- var customIdObj = cj(custom_id);
-
- if ( !customObj.hasClass('ac_input') ) {
- customObj.autocomplete( url,
- { width : 250, selectFirst : false, elementId: custom, matchContains: true, formatResult: {/literal}validate{$element_name|replace:']':''|replace:'[':'_'|replace:'-':'_'}{literal}, max: {/literal}{crmSetting name="search_autocomplete_count" group="Search Preferences"}{literal}
- }).result(
- function(event, data) {
- cj(custom_id).val(data[1]);
- }
- );
- customObj.click( function( ) {
- customIdObj.val('');
- });
- }
+ $field.crmSelect2({
+ placeholder: {/literal}'{ts escape="js"}- select contact -{/ts}'{literal},
+ minimumInputLength: 1,
+ ajax: {
+ url: {/literal}"{$customUrls.$element_name}"{literal},
+ data: function(term) {
+ return {term: term};
+ },
+ results: function(response) {
+ return {results: response};
+ }
+ },
+ initSelection: function($el, callback) {
+ callback($el.data('entity-value'));
+ }
+ });
});
-
-function validate{/literal}{$element_name|replace:']':''|replace:'[':'_'|replace:'-':'_'}{literal}( Data, position ) {
- if ( Data[1] == 'error' ) {
- cj(this.elementId).parent().append("<span id='"+ (this.elementId).substr(1) +"_error' class='hiddenElement messages crm-error'>" + "{/literal}{ts escape='js'}Invalid parameters for contact search.{/ts}{literal}" + "</span>");
- cj(this.elementId + '_error').fadeIn(800).fadeOut(5000, function( ){ cj(this).remove(); });
- Data[1] = '';
- }
-}
</script>
{/literal}