*
{$form.select_contact.label}
*
* {$form.select_contact.html}
*
*
* {/if}
* @param array $profiles ids of profiles that are on the form (to be autofilled)
* @param array $field metadata of field to use as selector including
* - name_field
* - id_field
* - url (for ajax lookup)
*
* @todo add data attributes so we can deal with multiple instances on a form
*/
function addAutoSelector($profiles = array(), $autoCompleteField = array()) {
$autoCompleteField = array_merge(array(
'id_field' => 'select_contact_id',
'placeholder' => ts('Select someone else ...'),
'show_hide' => TRUE,
'api' => array('params' => array('contact_type' => 'Individual'))
), $autoCompleteField);
if($this->canUseAjaxContactLookups()) {
$this->assign('selectable', $autoCompleteField['id_field']);
$this->addEntityRef($autoCompleteField['id_field'], NULL, array('placeholder' => $autoCompleteField['placeholder'], 'api' => $autoCompleteField['api']));
CRM_Core_Resources::singleton()->addScriptFile('civicrm', 'js/AlternateContactSelector.js')
->addSetting(array(
'form' => array('autocompletes' => $autoCompleteField),
'ids' => array('profile' => $profiles),
));
}
}
/**
*
*/
function canUseAjaxContactLookups() {
if (0 < (civicrm_api3('contact', 'getcount', array('check_permissions' => 1))) &&
CRM_Core_Permission::check(array(array('access AJAX API', 'access CiviCRM')))) {
return TRUE;
}
}
/**
* Add the options appropriate to cid = zero - ie. autocomplete
*
* @todo there is considerable code duplication between the contribution forms & event forms. It is apparent
* that small pieces of duplication are not being refactored into separate functions because their only shared parent
* is this form. Inserting a class FrontEndForm.php between the contribution & event & this class would allow functions like this
* and a dozen other small ones to be refactored into a shared parent with the reduction of much code duplication
*/
function addCIDZeroOptions($onlinePaymentProcessorEnabled) {
$this->assign('nocid', TRUE);
$profiles = array();
if($this->_values['custom_pre_id']) {
$profiles[] = $this->_values['custom_pre_id'];
}
if($this->_values['custom_post_id']) {
$profiles[] = $this->_values['custom_post_id'];
}
if($onlinePaymentProcessorEnabled) {
$profiles[] = 'billing';
}
if(!empty($this->_values)) {
$this->addAutoSelector($profiles);
}
}
/**
* Set default values on form for given contact (or no contact defaults)
* @param mixed $profile_id (can be id, or profile name)
* @param integer $contactID
*/
function getProfileDefaults($profile_id = 'Billing', $contactID = NULL) {
try{
$defaults = civicrm_api3('profile', 'getsingle', array(
'profile_id' => (array) $profile_id,
'contact_id' => $contactID,
));
return $defaults;
}
catch (Exception $e) {
// the try catch block gives us silent failure -not 100% sure this is a good idea
// as silent failures are often worse than noisy ones
return array();
}
}
/**
* Sets form attribute
* @see CRM.loadForm
*/
function preventAjaxSubmit() {
$this->setAttribute('data-no-ajax-submit', 'true');
}
/**
* Sets form attribute
* @see CRM.loadForm
*/
function allowAjaxSubmit() {
$this->removeAttribute('data-no-ajax-submit');
}
}