X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=CRM%2FCore%2FForm.php;h=91728c21bec8a1d0959ae46f6a96f461e72241bb;hb=35071ccc53f3bce7ce3414871d61059a90c24604;hp=9a7efa08b02505bbc7f6f21d38f21c65363d05f6;hpb=a8ebb99c949abe85c1274ca5ac28e80144392b10;p=civicrm-core.git diff --git a/CRM/Core/Form.php b/CRM/Core/Form.php index 9a7efa08b0..91728c21be 100644 --- a/CRM/Core/Form.php +++ b/CRM/Core/Form.php @@ -1,7 +1,7 @@ assign('qfKey', $this->controller->_key); } + if ($this->controller->_entryURL) { + $this->addElement('hidden', 'entryURL', $this->controller->_entryURL); + } $this->buildQuickForm(); @@ -717,6 +720,28 @@ class CRM_Core_Form extends HTML_QuickForm_Page { self::$_template->assign_by_ref($var, $value); } + /** + * appends values to template variables + * + * @param array|string $tpl_var the template variable name(s) + * @param mixed $value the value to append + * @param bool $merge + */ + function append($tpl_var, $value=NULL, $merge=FALSE) { + self::$_template->append($tpl_var, $value, $merge); + } + + /** + * Returns an array containing template variables + * + * @param string $name + * @param string $type + * @return array + */ + function get_template_vars($name=null) { + return self::$_template->get_template_vars($name); + } + function &addRadio($name, $title, &$values, $attributes = NULL, $separator = NULL, $required = FALSE) { $options = array(); if (empty($attributes)) { @@ -1318,15 +1343,19 @@ 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']; + } + if(isset($this->_params, $this->_params[0]) && !empty($this->_params[0]['select_contact_id'])) { + // event form stores as an indexed array, contribution form not so much... + $tempID = $this->_params[0]['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 +1377,107 @@ 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\'s information, 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', 'js/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); + } + } + + /** + * 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(); + } + } }