X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=CRM%2FCore%2FForm.php;h=257230f96920a6f2ce87ea01781b7a1d0ff6a50d;hb=fd31fa4c65b766d62df21f578772960955322576;hp=97e53f67c520e976f1bd15e5fa4e607979db803a;hpb=6e440e42c648284b1763a82adb793caad1d7c7b7;p=civicrm-core.git diff --git a/CRM/Core/Form.php b/CRM/Core/Form.php index 97e53f67c5..257230f969 100644 --- a/CRM/Core/Form.php +++ b/CRM/Core/Form.php @@ -1,9 +1,9 @@ assign('snippet', (int) CRM_Utils_Array::value('snippet', $_REQUEST)); + $this->assign('snippet', CRM_Utils_Array::value('snippet', $_GET)); } static function generateID() { @@ -208,21 +213,31 @@ class CRM_Core_Form extends HTML_QuickForm_Page { * Simple easy to use wrapper around addElement. Deal with * simple validation rules * - * @param string type of html element to be added - * @param string name of the html element - * @param string display label for the html element - * @param string attributes used for this element. + * @param $type + * @param $name + * @param string $label + * @param string $attributes + * @param bool $required + * @param null $extra + * + * @internal param \type $string of html element to be added + * @internal param \name $string of the html element + * @internal param \display $string label for the html element + * @internal param \attributes $string used for this element. * These are not default values - * @param bool is this a required field + * @internal param \is $bool this a required field * * @return HTML_QuickForm_Element could be an error object * @access public - * */ function &add($type, $name, $label = '', - $attributes = '', $required = FALSE, $javascript = NULL + $attributes = '', $required = FALSE, $extra = NULL ) { - $element = $this->addElement($type, $name, $label, $attributes, $javascript); + // Normalize this property + if ($type == 'select' && is_array($extra) && !empty($extra['multiple'])) { + $extra['multiple'] = 'multiple'; + } + $element = $this->addElement($type, $name, $label, $attributes, $extra); if (HTML_QuickForm::isError($element)) { CRM_Core_Error::fatal(HTML_QuickForm::errorMessage($element)); } @@ -407,6 +422,15 @@ class CRM_Core_Form extends HTML_QuickForm_Page { CRM_Utils_Hook::buildForm(get_class($this), $this); $this->addRules(); + + //Set html data-attribute to enable warning user of unsaved changes + if ($this->unsavedChangesWarn === true + || (!isset($this->unsavedChangesWarn) + && ($this->_action & CRM_Core_Action::ADD || $this->_action & CRM_Core_Action::UPDATE) + ) + ) { + $this->setAttribute('data-warn-changes', 'true'); + } } /** @@ -439,6 +463,10 @@ class CRM_Core_Form extends HTML_QuickForm_Page { $attrs = array_merge($js, $attrs); } + if ($button['type'] === 'cancel') { + $attrs['class'] .= ' cancel'; + } + if ($button['type'] === 'reset') { $prevnext[] = $this->createElement($button['type'], 'reset', $button['name'], $attrs); } @@ -450,8 +478,7 @@ class CRM_Core_Form extends HTML_QuickForm_Page { $buttonName = $this->getButtonName($button['type']); } - if (in_array($button['type'], array( - 'next', 'upload')) && $button['name'] === 'Save') { + if (in_array($button['type'], array('next', 'upload', 'done')) && $button['name'] === ts('Save')) { $attrs = array_merge($attrs, (array('accesskey' => 'S'))); } $prevnext[] = $this->createElement('submit', $buttonName, $button['name'], $attrs); @@ -773,8 +800,8 @@ class CRM_Core_Form extends HTML_QuickForm_Page { function &addRadio($name, $title, $values, $attributes = array(), $separator = NULL, $required = FALSE) { $options = array(); $attributes = $attributes ? $attributes : array(); - $unselectable = !empty($attributes['unselectable']); - unset($attributes['unselectable']); + $allowClear = !empty($attributes['allowClear']); + unset($attributes['allowClear']); $attributes += array('id_suffix' => $name); foreach ($values as $key => $var) { $options[] = $this->createElement('radio', NULL, NULL, $var, $key, $attributes); @@ -783,21 +810,21 @@ class CRM_Core_Form extends HTML_QuickForm_Page { if ($required) { $this->addRule($name, ts('%1 is a required field.', array(1 => $title)), 'required'); } - if ($unselectable) { - $group->setAttribute('unselectable', TRUE); + if ($allowClear) { + $group->setAttribute('allowClear', TRUE); } return $group; } - function addYesNo($id, $title, $unselectable = FALSE, $required = NULL, $attributes = array()) { + function addYesNo($id, $title, $allowClear = FALSE, $required = NULL, $attributes = array()) { $attributes += array('id_suffix' => $id); $choice = array(); $choice[] = $this->createElement('radio', NULL, '11', ts('Yes'), '1', $attributes); $choice[] = $this->createElement('radio', NULL, '11', ts('No'), '0', $attributes); $group = $this->addGroup($choice, $id, $title); - if ($unselectable) { - $group->setAttribute('unselectable', TRUE); + if ($allowClear) { + $group->setAttribute('allowClear', TRUE); } if ($required) { $this->addRule($id, ts('%1 is a required field.', array(1 => $title)), 'required'); @@ -856,9 +883,11 @@ class CRM_Core_Form extends HTML_QuickForm_Page { * the form with a customized title for the main Submit * * @param string $title title of the main button - * @param string $type button type for the form after processing - * @param string $submitOnce If true, add javascript to next button submit which prevents it from being clicked more than once + * @param string $nextType + * @param string $backType + * @param bool|string $submitOnce If true, add javascript to next button submit which prevents it from being clicked more than once * + * @internal param string $type button type for the form after processing * @return void * @access public */ @@ -904,6 +933,7 @@ class CRM_Core_Form extends HTML_QuickForm_Page { * - field (field name - only needed if different from name used on the form) * - option_url - path to edit this option list - usually retrieved automatically - set to NULL to disable link * - placeholder - set to NULL to disable + * - multiple - bool * @param bool $required * @throws CRM_Core_Exception * @return HTML_QuickForm_Element @@ -1250,7 +1280,9 @@ class CRM_Core_Form extends HTML_QuickForm_Page { * - create - can the user create a new entity on-the-fly? * Set to TRUE if entity is contact and you want the default profiles, * or pass in your own set of links. @see CRM_Core_BAO_UFGroup::getCreateLinks for format - * - api - array of settings for the getlist api + * note that permissions are checked automatically + * - api - array of settings for the getlist api wrapper + * note that it accepts a 'params' setting which will be passed to the underlying api * - placeholder - string * - multiple - bool * - class, etc. - other html properties @@ -1433,35 +1465,30 @@ class CRM_Core_Form extends HTML_QuickForm_Page { * * * {/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 + * @param array $autoCompleteField + * + * @internal 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 + * @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'), + 'placeholder' => ts('Select someone else ...'), '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', - )) + 'api' => array('params' => array('contact_type' => 'Individual')) ), $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'])); + 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/AutoComplete.js') + CRM_Core_Resources::singleton()->addScriptFile('civicrm', 'js/AlternateContactSelector.js') ->addSetting(array( 'form' => array('autocompletes' => $autoCompleteField), 'ids' => array('profile' => $profiles), @@ -1469,6 +1496,16 @@ class CRM_Core_Form extends HTML_QuickForm_Page { } } + /** + * + */ + 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 * @@ -1496,8 +1533,11 @@ class CRM_Core_Form extends HTML_QuickForm_Page { /** * 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 + * + * @return array */ function getProfileDefaults($profile_id = 'Billing', $contactID = NULL) { try{ @@ -1513,5 +1553,21 @@ class CRM_Core_Form extends HTML_QuickForm_Page { 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'); + } }