From 596bff78478513ac3eed164f6d99f948cf6863f7 Mon Sep 17 00:00:00 2001 From: eileen Date: Wed, 14 Aug 2013 18:14:14 +1200 Subject: [PATCH] CRM-13179 make autoselect available for logged in users --- CRM/Contribute/Form/Contribution/Confirm.php | 1 + CRM/Contribute/Form/Contribution/Main.php | 12 +- CRM/Core/Form.php | 88 ++++++++++- CRM/Event/Form/Registration/Register.php | 14 +- .../CRM/Contribute/Form/Contribution/Main.tpl | 3 +- .../CRM/Event/Form/Registration/Register.tpl | 3 +- templates/CRM/common/AutoComplete.js | 141 ++++++++++++++++++ templates/CRM/common/cidzero.tpl | 16 ++ 8 files changed, 266 insertions(+), 12 deletions(-) create mode 100644 templates/CRM/common/AutoComplete.js create mode 100644 templates/CRM/common/cidzero.tpl diff --git a/CRM/Contribute/Form/Contribution/Confirm.php b/CRM/Contribute/Form/Contribution/Confirm.php index 2bf4640ffb..dfd6771ffd 100644 --- a/CRM/Contribute/Form/Contribution/Confirm.php +++ b/CRM/Contribute/Form/Contribution/Confirm.php @@ -798,6 +798,7 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr // Make the contact ID associated with the contribution available at the Class level. // Also make available to the session. + //@todo consider handling this in $this->getContactID(); $this->set('contactID', $contactID); $this->_contactID = $contactID; diff --git a/CRM/Contribute/Form/Contribution/Main.php b/CRM/Contribute/Form/Contribution/Main.php index c5a0058604..f28cccee90 100644 --- a/CRM/Contribute/Form/Contribution/Main.php +++ b/CRM/Contribute/Form/Contribution/Main.php @@ -393,9 +393,12 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu ); $this->addRule("email-{$this->_bltID}", ts('Email is not valid.'), 'email'); $pps = array(); + $onlinePaymentProcessorEnabled = FALSE; if (!empty($this->_paymentProcessors)) { - $pps = $this->_paymentProcessors; - foreach ($pps as $key => & $name) { + foreach ($this->_paymentProcessors as $key => $name) { + if($name['billing_mode'] == 1) { + $onlinePaymentProcessorEnabled = TRUE; + } $pps[$key] = $name['name']; } } @@ -417,6 +420,11 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu $this->assign('pay_later_text', $this->_values['pay_later_text']); } } + + $contactID = $this->getContactID(); + if($this->getContactID() === '0') { + $this->addCidZeroOptions($onlinePaymentProcessorEnabled); + } //build pledge block. $this->_useForMember = 0; //don't build membership block when pledge_id is passed diff --git a/CRM/Core/Form.php b/CRM/Core/Form.php index 074889a958..ad6b4730e9 100644 --- a/CRM/Core/Form.php +++ b/CRM/Core/Form.php @@ -1318,15 +1318,16 @@ class CRM_Core_Form extends HTML_QuickForm_Page { */ function getContactID() { $tempID = CRM_Utils_Request::retrieve('cid', 'Positive', $this); + if(isset($this->_params) && isset($this->_params['select_contact_id '])) { + $tempID = $this->_params['select_contact_id']; + } // force to ignore the authenticated user if ($tempID === '0') { return $tempID; } - // check if the user is logged in and has a contact ID - $session = CRM_Core_Session::singleton(); - $userID = $session->get('userID'); + $userID = $this->getLoggedInUserContactID(); if ($tempID == $userID) { return $userID; @@ -1348,5 +1349,86 @@ class CRM_Core_Form extends HTML_QuickForm_Page { return $userID; } + + /** + * Get the contact id of the logged in user + */ + function getLoggedInUserContactID() { + // check if the user is logged in and has a contact ID + $session = CRM_Core_Session::singleton(); + return $session->get('userID'); + } + + /** + * add autoselector field -if user has permission to view contacts + * If adding this to a form you also need to add to the tpl e.g + * + * {if !empty($selectable)} + *
+ *
{$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( + 'name_field' => 'select_contact', + 'id_field' => 'select_contact_id', + 'field_text' => ts('Select Contact'), + 'show_hide' => TRUE, + 'show_text' => ts('to select someone already in our database'), + 'hide_text' => ts('to clear this person and fill the form in for someone else'), + 'url' => array('civicrm/ajax/rest', 'className=CRM_Contact_Page_AJAX&fnName=getContactList&json=1'), + 'max' => civicrm_api3('setting', 'getvalue', array( + 'name' => 'search_autocomplete_count', + 'group' => 'Search Preferences', + )) + ), $autoCompleteField); + + if(0 < (civicrm_api3('contact', 'getcount', array('check_permissions' => 1)))) { + $this->addElement('text', $autoCompleteField['name_field'] , $autoCompleteField['field_text']); + $this->addElement('hidden', $autoCompleteField['id_field'], '', array('id' => $autoCompleteField['id_field'])); $this->assign('selectable', $autoCompleteField['id_field']); + + CRM_Core_Resources::singleton()->addScriptFile('civicrm', 'templates/CRM/common/AutoComplete.js') + ->addSetting(array( + 'form' => array('autocompletes' => $autoCompleteField), + 'ids' => array('profile' => $profiles), + )); + } + } + + /** + * 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); + } + } } diff --git a/CRM/Event/Form/Registration/Register.php b/CRM/Event/Form/Registration/Register.php index b4d526b3dd..be8e3ca65a 100644 --- a/CRM/Event/Form/Registration/Register.php +++ b/CRM/Event/Form/Registration/Register.php @@ -426,14 +426,20 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration { self::buildAmount($this); } - $pps = NULL; + $pps = array(); + //@todo this processor adding fn is another one duplicated on contribute - a shared + // common class would make this sort of thing extractable if (!empty($this->_paymentProcessors)) { - $pps = $this->_paymentProcessors; - foreach ($pps as $key => & $name) { + foreach ($this->_paymentProcessors as $key => $name) { + if($name['billing_mode'] == 1) { + $onlinePaymentProcessorEnabled = TRUE; + } $pps[$key] = $name['name']; } } - + if($this->getContactID() === '0') { + $this->addCidZeroOptions($onlinePaymentProcessorEnabled); + } if (CRM_Utils_Array::value('is_pay_later', $this->_values['event']) && ($this->_allowConfirmation || (!$this->_requireApproval && !$this->_allowWaitlist)) ) { diff --git a/templates/CRM/Contribute/Form/Contribution/Main.tpl b/templates/CRM/Contribute/Form/Contribution/Main.tpl index 4195822e4c..a64ccd3cc2 100644 --- a/templates/CRM/Contribute/Form/Contribution/Main.tpl +++ b/templates/CRM/Contribute/Form/Contribution/Main.tpl @@ -53,7 +53,7 @@ {/if} {/if} -{* Main Form *} +{* Main Form *} {else} {literal}