3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
13 * This is our base form. It is part of the Form/Controller/StateMachine
14 * trifecta. Each form is associated with a specific state in the state
15 * machine. Each form can also operate in various modes
18 * @copyright CiviCRM LLC https://civicrm.org/licensing
21 require_once 'HTML/QuickForm/Page.php';
26 class CRM_Core_Form
extends HTML_QuickForm_Page
{
29 * The state object that this form belongs to
35 * The name of this form
41 * The title of this form
44 protected $_title = NULL;
47 * The default values for the form.
51 public $_defaults = [];
54 * (QUASI-PROTECTED) The options passed into this form
56 * This field should marked `protected` and is not generally
57 * intended for external callers, but some edge-cases do use it.
61 public $_options = NULL;
64 * (QUASI-PROTECTED) The mode of operation for this form
66 * This field should marked `protected` and is not generally
67 * intended for external callers, but some edge-cases do use it.
74 * Available payment processors.
76 * As part of trying to consolidate various payment pages we store processors here & have functions
77 * at this level to manage them.
80 * An array of payment processor details with objects loaded in the 'object' field.
82 protected $_paymentProcessors;
85 * Available payment processors (IDS).
87 * As part of trying to consolidate various payment pages we store processors here & have functions
88 * at this level to manage them. An alternative would be to have a separate Form that is inherited
89 * by all forms that allow payment processing.
92 * An array of the IDS available on this form.
94 public $_paymentProcessorIDs;
97 * Default or selected processor id.
99 * As part of trying to consolidate various payment pages we store processors here & have functions
100 * at this level to manage them. An alternative would be to have a separate Form that is inherited
101 * by all forms that allow payment processing.
105 protected $_paymentProcessorID;
108 * Is pay later enabled for the form.
110 * As part of trying to consolidate various payment pages we store processors here & have functions
111 * at this level to manage them. An alternative would be to have a separate Form that is inherited
112 * by all forms that allow payment processing.
116 protected $_is_pay_later_enabled;
119 * The renderer used for this form
123 protected $_renderer;
126 * An array to hold a list of datefields on the form
127 * so that they can be converted to ISO in a consistent manner
131 * e.g on a form declare $_dateFields = array(
132 * 'receive_date' => array('default' => 'now'),
134 * then in postProcess call $this->convertDateFieldsToMySQL($formValues)
135 * to have the time field re-incorporated into the field & 'now' set if
136 * no value has been passed in
138 protected $_dateFields = [];
141 * Cache the smarty template for efficiency reasons
143 * @var CRM_Core_Smarty
145 static protected $_template;
148 * Indicate if this form should warn users of unsaved changes
151 protected $unsavedChangesWarn;
154 * What to return to the client if in ajax mode (snippet=json)
158 public $ajaxResponse = [];
161 * Url path used to reach this page
165 public $urlPath = [];
168 * Context of the form being loaded.
179 public $submitOnce = FALSE;
184 public function getContext() {
185 return $this->context
;
189 * Set context variable.
191 public function setContext() {
192 $this->context
= CRM_Utils_Request
::retrieve('context', 'Alphanumeric', $this);
196 * @var CRM_Core_Controller
201 * Constants for attributes for various form elements
202 * attempt to standardize on the number of variations that we
203 * use of the below form elements
207 const ATTR_SPACING
= ' ';
210 * All checkboxes are defined with a common prefix. This allows us to
211 * have the same javascript to check / clear all the checkboxes etc
212 * If u have multiple groups of checkboxes, you will need to give them different
213 * ids to avoid potential name collision
217 const CB_PREFIX
= 'mark_x_', CB_PREFIY
= 'mark_y_', CB_PREFIZ
= 'mark_z_', CB_PREFIX_LEN
= 7;
221 * @internal to keep track of chain-select fields
223 private $_chainSelectFields = [];
226 * Extra input types we support via the "add" method
229 public static $html5Types = [
237 * Constructor for the basic form page.
239 * We should not use QuickForm directly. This class provides a lot
240 * of default convenient functions, rules and buttons
242 * @param object $state
243 * State associated with this form.
244 * @param \const|\enum|int $action The mode the form is operating in (None/Create/View/Update/Delete)
245 * @param string $method
246 * The type of http method used (GET/POST).
247 * @param string $name
248 * The name of the form if different from class name.
250 * @return \CRM_Core_Form
252 public function __construct(
254 $action = CRM_Core_Action
::NONE
,
260 $this->_name
= $name;
263 // CRM-15153 - FIXME this name translates to a DOM id and is not always unique!
264 $this->_name
= CRM_Utils_String
::getClassName(CRM_Utils_System
::getClassName($this));
267 parent
::__construct($this->_name
, $method);
269 $this->_state
=& $state;
271 $this->_state
->setName($this->_name
);
273 $this->_action
= (int) $action;
275 $this->registerRules();
277 // let the constructor initialize this, should happen only once
278 if (!isset(self
::$_template)) {
279 self
::$_template = CRM_Core_Smarty
::singleton();
281 // Workaround for CRM-15153 - give each form a reasonably unique css class
282 $this->addClass(CRM_Utils_System
::getClassName($this));
284 $this->assign('snippet', CRM_Utils_Array
::value('snippet', $_GET));
285 $this->setTranslatedFields();
289 * Set translated fields.
291 * This function is called from the class constructor, allowing us to set
292 * fields on the class that can't be set as properties due to need for
293 * translation or other non-input specific handling.
295 protected function setTranslatedFields() {}
298 * Add one or more css classes to the form.
300 * @param string $className
302 public function addClass($className) {
303 $classes = $this->getAttribute('class');
304 $this->setAttribute('class', ($classes ?
"$classes " : '') . $className);
308 * Register all the standard rules that most forms potentially use.
310 public function registerRules() {
340 foreach ($rules as $rule) {
341 $this->registerRule($rule, 'callback', $rule, 'CRM_Utils_Rule');
346 * Simple easy to use wrapper around addElement.
348 * Deal with simple validation rules.
350 * @param string $type
351 * @param string $name
352 * @param string $label
353 * @param string|array $attributes (options for select elements)
354 * @param bool $required
355 * @param array $extra
356 * (attributes for select elements).
357 * For datepicker elements this is consistent with the data
358 * from CRM_Utils_Date::getDatePickerExtra
360 * @return HTML_QuickForm_Element
361 * Could be an error object
363 * @throws \CRM_Core_Exception
365 public function &add(
366 $type, $name, $label = '',
367 $attributes = '', $required = FALSE, $extra = NULL
369 if ($type === 'radio') {
370 CRM_Core_Error
::deprecatedFunctionWarning('CRM_Core_Form::addRadio');
372 // Fudge some extra types that quickform doesn't support
374 if ($type == 'wysiwyg' ||
in_array($type, self
::$html5Types)) {
375 $attributes = ($attributes ?
$attributes : []) +
['class' => ''];
376 $attributes['class'] = ltrim($attributes['class'] . " crm-form-$type");
377 if ($type == 'wysiwyg' && isset($attributes['preset'])) {
378 $attributes['data-preset'] = $attributes['preset'];
379 unset($attributes['preset']);
381 $type = $type == 'wysiwyg' ?
'textarea' : 'text';
383 // Like select but accepts rich array data (with nesting, colors, icons, etc) as option list.
384 if ($inputType == 'select2') {
386 $options = $attributes;
387 $attributes = ($extra ?
$extra : []) +
['class' => ''];
388 $attributes['class'] = ltrim($attributes['class'] . " crm-select2 crm-form-select2");
389 $attributes['data-select-params'] = json_encode(['data' => $options, 'multiple' => !empty($attributes['multiple'])]);
390 unset($attributes['multiple']);
394 // @see https://docs.civicrm.org/dev/en/latest/framework/ui/#date-picker
395 if ($type === 'datepicker') {
396 $attributes = $attributes ?
: [];
397 if (!empty($attributes['formatType'])) {
398 $dateAttributes = CRM_Core_SelectValues
::date($attributes['formatType'], NULL, NULL, NULL, 'Input');
399 if (empty($extra['minDate']) && !empty($dateAttributes['minYear'])) {
400 $extra['minDate'] = $dateAttributes['minYear'] . '-01-01';
402 if (empty($extra['maxDate']) && !empty($dateAttributes['minYear'])) {
403 $extra['maxDate'] = $dateAttributes['maxYear'] . '-12-31';
406 // Support minDate/maxDate properties
407 if (isset($extra['minDate'])) {
408 $extra['minDate'] = date('Y-m-d', strtotime($extra['minDate']));
410 if (isset($extra['maxDate'])) {
411 $extra['maxDate'] = date('Y-m-d', strtotime($extra['maxDate']));
414 $attributes['data-crm-datepicker'] = json_encode((array) $extra);
415 if (!empty($attributes['aria-label']) ||
$label) {
416 $attributes['aria-label'] = CRM_Utils_Array
::value('aria-label', $attributes, $label);
420 if ($type === 'select' && is_array($extra)) {
421 // Normalize this property
422 if (!empty($extra['multiple'])) {
423 $extra['multiple'] = 'multiple';
426 unset($extra['multiple']);
428 unset($extra['size'], $extra['maxlength']);
429 // Add placeholder option for select
430 if (isset($extra['placeholder'])) {
431 if ($extra['placeholder'] === TRUE) {
432 $extra['placeholder'] = $required ?
ts('- select -') : ts('- none -');
434 if (($extra['placeholder'] ||
$extra['placeholder'] === '') && empty($extra['multiple']) && is_array($attributes) && !isset($attributes[''])) {
435 $attributes = ['' => $extra['placeholder']] +
$attributes;
439 $optionContext = NULL;
440 if (!empty($extra['option_context'])) {
441 $optionContext = $extra['option_context'];
442 unset($extra['option_context']);
445 $element = $this->addElement($type, $name, CRM_Utils_String
::purifyHTML($label), $attributes, $extra);
446 if (HTML_QuickForm
::isError($element)) {
447 CRM_Core_Error
::statusBounce(HTML_QuickForm
::errorMessage($element));
450 if ($inputType == 'color') {
451 $this->addRule($name, ts('%1 must contain a color value e.g. #ffffff.', [1 => $label]), 'regex', '/#[0-9a-fA-F]{6}/');
455 if ($type == 'file') {
456 $error = $this->addRule($name, ts('%1 is a required field.', [1 => $label]), 'uploadedfile');
459 $error = $this->addRule($name, ts('%1 is a required field.', [1 => $label]), 'required');
461 if (HTML_QuickForm
::isError($error)) {
462 CRM_Core_Error
::statusBounce(HTML_QuickForm
::errorMessage($element));
466 // Add context for the editing of option groups
467 if ($optionContext) {
468 $element->setAttribute('data-option-edit-context', json_encode($optionContext));
477 * This is called before buildForm. Any pre-processing that
478 * needs to be done for buildForm should be done here.
480 * This is a virtual function and should be redefined if needed.
482 public function preProcess() {
486 * Called after the form is validated.
488 * Any processing of form state etc should be done in this function.
489 * Typically all processing associated with a form should be done
490 * here and relevant state should be stored in the session
492 * This is a virtual function and should be redefined if needed
494 public function postProcess() {
498 * Main process wrapper.
500 * Implemented so that we can call all the hook functions.
502 * @param bool $allowAjax
503 * FIXME: This feels kind of hackish, ideally we would take the json-related code from this function.
504 * and bury it deeper down in the controller
506 public function mainProcess($allowAjax = TRUE) {
507 $this->postProcess();
508 $this->postProcessHook();
510 // Respond with JSON if in AJAX context (also support legacy value '6')
511 if ($allowAjax && !empty($_REQUEST['snippet']) && in_array($_REQUEST['snippet'], [
512 CRM_Core_Smarty
::PRINT_JSON
,
515 $this->ajaxResponse
['buttonName'] = str_replace('_qf_' . $this->getAttribute('id') . '_', '', $this->controller
->getButtonName());
516 $this->ajaxResponse
['action'] = $this->_action
;
517 if (isset($this->_id
) ||
isset($this->id
)) {
518 $this->ajaxResponse
['id'] = $this->id ??
$this->_id
;
520 CRM_Core_Page_AJAX
::returnJsonResponse($this->ajaxResponse
);
525 * The postProcess hook is typically called by the framework.
527 * However in a few cases, the form exits or redirects early in which
528 * case it needs to call this function so other modules can do the needful
529 * Calling this function directly should be avoided if possible. In general a
530 * better way is to do setUserContext so the framework does the redirect
532 public function postProcessHook() {
533 CRM_Utils_Hook
::postProcess(get_class($this), $this);
537 * This virtual function is used to build the form.
539 * It replaces the buildForm associated with QuickForm_Page. This allows us to put
540 * preProcess in front of the actual form building routine
542 public function buildQuickForm() {
546 * This virtual function is used to set the default values of various form elements.
549 * reference to the array of default values
551 public function setDefaultValues() {
556 * This is a virtual function that adds group and global rules to the form.
558 * Keeping it distinct from the form to keep code small
559 * and localized in the form building code
561 public function addRules() {
565 * Performs the server side validation.
568 * true if no error found
569 * @throws HTML_QuickForm_Error
571 public function validate() {
572 $error = parent
::validate();
574 $this->validateChainSelectFields();
578 CRM_Utils_Hook
::validateForm(
580 $this->_submitValues
,
586 if (!empty($hookErrors)) {
587 $this->_errors +
= $hookErrors;
590 return (0 == count($this->_errors
));
594 * Core function that builds the form.
596 * We redefine this function here and expect all CRM forms to build their form in the function
599 public function buildForm() {
600 $this->_formBuilt
= TRUE;
604 CRM_Utils_Hook
::preProcess(get_class($this), $this);
606 $this->assign('translatePermission', CRM_Core_Permission
::check('translate CiviCRM'));
609 $this->controller
->_key
&&
610 $this->controller
->_generateQFKey
612 $this->addElement('hidden', 'qfKey', $this->controller
->_key
);
613 $this->assign('qfKey', $this->controller
->_key
);
617 // _generateQFKey suppresses the qfKey generation on form snippets that
618 // are part of other forms, hence we use that to avoid adding entryURL
619 if ($this->controller
->_generateQFKey
&& $this->controller
->_entryURL
) {
620 $this->addElement('hidden', 'entryURL', $this->controller
->_entryURL
);
623 $this->buildQuickForm();
625 $defaults = $this->setDefaultValues();
626 unset($defaults['qfKey']);
628 if (!empty($defaults)) {
629 $this->setDefaults($defaults);
632 // call the form hook
633 // also call the hook function so any modules can set their own custom defaults
634 // the user can do both the form and set default values with this hook
635 CRM_Utils_Hook
::buildForm(get_class($this), $this);
639 //Set html data-attribute to enable warning user of unsaved changes
640 if ($this->unsavedChangesWarn
=== TRUE
641 ||
(!isset($this->unsavedChangesWarn
)
642 && ($this->_action
& CRM_Core_Action
::ADD ||
$this->_action
& CRM_Core_Action
::UPDATE
)
645 $this->setAttribute('data-warn-changes', 'true');
648 if ($this->submitOnce
) {
649 $this->setAttribute('data-submit-once', 'true');
654 * Add default Next / Back buttons.
656 * @param array $params
657 * Array of associative arrays in the order in which the buttons should be
658 * displayed. The associate array has 3 fields: 'type', 'name' and 'isDefault'
659 * The base form class will define a bunch of static arrays for commonly used
662 public function addButtons($params) {
663 $prevnext = $spacing = [];
664 foreach ($params as $button) {
665 if (!empty($button['submitOnce'])) {
666 $this->submitOnce
= TRUE;
669 $attrs = ['class' => 'crm-form-submit'] +
(array) CRM_Utils_Array
::value('js', $button);
671 if (!empty($button['class'])) {
672 $attrs['class'] .= ' ' . $button['class'];
675 if (!empty($button['isDefault'])) {
676 $attrs['class'] .= ' default';
679 if (in_array($button['type'], ['upload', 'next', 'submit', 'done', 'process', 'refresh'])) {
680 $attrs['class'] .= ' validate';
681 $defaultIcon = 'fa-check';
684 $attrs['class'] .= ' cancel';
685 $defaultIcon = $button['type'] == 'back' ?
'fa-chevron-left' : 'fa-times';
688 if ($button['type'] === 'reset') {
689 $prevnext[] = $this->createElement($button['type'], 'reset', $button['name'], $attrs);
692 if (!empty($button['subName'])) {
693 if ($button['subName'] == 'new') {
694 $defaultIcon = 'fa-plus-circle';
696 if ($button['subName'] == 'done') {
697 $defaultIcon = 'fa-check-circle';
699 if ($button['subName'] == 'next') {
700 $defaultIcon = 'fa-chevron-right';
704 if (in_array($button['type'], ['next', 'upload', 'done']) && $button['name'] === ts('Save')) {
705 $attrs['accesskey'] = 'S';
707 $icon = CRM_Utils_Array
::value('icon', $button, $defaultIcon);
709 $attrs['crm-icon'] = $icon;
711 $buttonName = $this->getButtonName($button['type'], CRM_Utils_Array
::value('subName', $button));
712 $prevnext[] = $this->createElement('submit', $buttonName, $button['name'], $attrs);
714 if (!empty($button['isDefault'])) {
715 $this->setDefaultAction($button['type']);
718 // if button type is upload, set the enctype
719 if ($button['type'] == 'upload') {
720 $this->updateAttributes(['enctype' => 'multipart/form-data']);
721 $this->setMaxFileSize();
724 // hack - addGroup uses an array to express variable spacing, read from the last element
725 $spacing[] = CRM_Utils_Array
::value('spacing', $button, self
::ATTR_SPACING
);
727 $this->addGroup($prevnext, 'buttons', '', $spacing, FALSE);
731 * Getter function for Name.
735 public function getName() {
740 * Getter function for State.
744 public function &getState() {
745 return $this->_state
;
749 * Getter function for StateType.
753 public function getStateType() {
754 return $this->_state
->getType();
758 * Getter function for title.
760 * Should be over-ridden by derived class.
764 public function getTitle() {
765 return $this->_title ?
$this->_title
: ts('ERROR: Title is not Set');
769 * Setter function for title.
771 * @param string $title
772 * The title of the form.
774 public function setTitle($title) {
775 $this->_title
= $title;
776 CRM_Utils_System
::setTitle($title);
780 * Assign billing type id to bltID.
782 * @throws CRM_Core_Exception
784 public function assignBillingType() {
785 $this->_bltID
= CRM_Core_BAO_LocationType
::getBilling();
786 $this->set('bltID', $this->_bltID
);
787 $this->assign('bltID', $this->_bltID
);
793 public function getPaymentProcessorID() {
794 return $this->_paymentProcessorID
;
798 * This if a front end form function for setting the payment processor.
800 * It would be good to sync it with the back-end function on abstractEditPayment & use one everywhere.
802 * @param bool $isPayLaterEnabled
804 * @throws \CRM_Core_Exception
806 protected function assignPaymentProcessor($isPayLaterEnabled) {
807 $this->_paymentProcessors
= CRM_Financial_BAO_PaymentProcessor
::getPaymentProcessors([ucfirst($this->_mode
) . 'Mode'], $this->_paymentProcessorIDs
);
808 if ($isPayLaterEnabled) {
809 $this->_paymentProcessors
[0] = CRM_Financial_BAO_PaymentProcessor
::getPayment(0);
812 if (!empty($this->_paymentProcessors
)) {
813 foreach ($this->_paymentProcessors
as $paymentProcessorID => $paymentProcessorDetail) {
814 if (empty($this->_paymentProcessor
) && $paymentProcessorDetail['is_default'] == 1 ||
(count($this->_paymentProcessors
) == 1)
816 $this->_paymentProcessor
= $paymentProcessorDetail;
817 $this->assign('paymentProcessor', $this->_paymentProcessor
);
818 // Setting this is a bit of a legacy overhang.
819 $this->_paymentObject
= $paymentProcessorDetail['object'];
822 // It's not clear why we set this on the form.
823 $this->set('paymentProcessors', $this->_paymentProcessors
);
826 throw new CRM_Core_Exception(ts('A payment processor configured for this page might be disabled (contact the site administrator for assistance).'));
831 * Assign an array of variables to the form/tpl
833 * @param array $values Array of [key => value] to assign to the form
834 * @param array $keys Array of keys to assign from the values array
836 public function assignVariables($values, $keys) {
837 foreach ($keys as $key) {
838 $this->assign($key, $values[$key] ??
NULL);
843 * Format the fields in $this->_params for the payment processor.
845 * In order to pass fields to the payment processor in a consistent way we add some renamed
848 * @param array $fields
852 protected function formatParamsForPaymentProcessor($fields) {
853 $this->_params
= $this->prepareParamsForPaymentProcessor($this->_params
);
854 $fields = array_merge($fields, ['first_name' => 1, 'middle_name' => 1, 'last_name' => 1]);
859 * Format the fields in $params for the payment processor.
861 * In order to pass fields to the payment processor in a consistent way we add some renamed
864 * @param array $params Payment processor params
866 * @return array $params
868 protected function prepareParamsForPaymentProcessor($params) {
869 // also add location name to the array
870 $params["address_name-{$this->_bltID}"] = CRM_Utils_Array
::value('billing_first_name', $params) . ' ' . CRM_Utils_Array
::value('billing_middle_name', $params) . ' ' . CRM_Utils_Array
::value('billing_last_name', $params);
871 $params["address_name-{$this->_bltID}"] = trim($params["address_name-{$this->_bltID}"]);
872 // Add additional parameters that the payment processors are used to receiving.
873 if (!empty($params["billing_state_province_id-{$this->_bltID}"])) {
874 $params['state_province'] = $params["state_province-{$this->_bltID}"] = $params["billing_state_province-{$this->_bltID}"] = CRM_Core_PseudoConstant
::stateProvinceAbbreviation($params["billing_state_province_id-{$this->_bltID}"]);
876 if (!empty($params["billing_country_id-{$this->_bltID}"])) {
877 $params['country'] = $params["country-{$this->_bltID}"] = $params["billing_country-{$this->_bltID}"] = CRM_Core_PseudoConstant
::countryIsoCode($params["billing_country_id-{$this->_bltID}"]);
880 list($hasAddressField, $addressParams) = CRM_Contribute_BAO_Contribution
::getPaymentProcessorReadyAddressParams($params, $this->_bltID
);
881 if ($hasAddressField) {
882 $params = array_merge($params, $addressParams);
885 // How does this relate to similar code in CRM_Contact_BAO_Contact::addBillingNameFieldsIfOtherwiseNotSet()?
886 $nameFields = ['first_name', 'middle_name', 'last_name'];
887 foreach ($nameFields as $name) {
888 if (array_key_exists("billing_$name", $params)) {
889 $params[$name] = $params["billing_{$name}"];
890 $params['preserveDBName'] = TRUE;
894 // For legacy reasons we set these creditcard expiry fields if present
895 CRM_Contribute_Form_AbstractEditPayment
::formatCreditCardDetails($params);
897 // Assign IP address parameter
898 $params['ip_address'] = CRM_Utils_System
::ipAddress();
904 * Handle Payment Processor switching for contribution and event registration forms.
906 * This function is shared between contribution & event forms & this is their common class.
908 * However, this should be seen as an in-progress refactor, the end goal being to also align the
909 * backoffice forms that action payments.
911 * This function overlaps assignPaymentProcessor, in a bad way.
913 protected function preProcessPaymentOptions() {
914 $this->_paymentProcessorID
= NULL;
915 if ($this->_paymentProcessors
) {
916 if (!empty($this->_submitValues
)) {
917 $this->_paymentProcessorID
= $this->_submitValues
['payment_processor_id'] ??
NULL;
918 $this->_paymentProcessor
= $this->_paymentProcessors
[$this->_paymentProcessorID
] ??
NULL;
919 $this->set('type', $this->_paymentProcessorID
);
920 $this->set('mode', $this->_mode
);
921 $this->set('paymentProcessor', $this->_paymentProcessor
);
923 // Set default payment processor
925 foreach ($this->_paymentProcessors
as $values) {
926 if (!empty($values['is_default']) ||
count($this->_paymentProcessors
) == 1) {
927 $this->_paymentProcessorID
= $values['id'];
932 if ($this->_paymentProcessorID
933 ||
(isset($this->_submitValues
['payment_processor_id']) && $this->_submitValues
['payment_processor_id'] == 0)
935 CRM_Core_Payment_ProcessorForm
::preProcess($this);
938 $this->_paymentProcessor
= [];
942 // We save the fact that the profile 'billing' is required on the payment form.
943 // Currently pay-later is the only 'processor' that takes notice of this - but ideally
944 // 1) it would be possible to select the minimum_billing_profile_id for the contribution form
945 // 2) that profile_id would be set on the payment processor
946 // 3) the payment processor would return a billing form that combines these user-configured
947 // minimums with the payment processor minimums. This would lead to fields like 'postal_code'
948 // only being on the form if either the admin has configured it as wanted or the processor
950 $this->assign('billing_profile_id', (!empty($this->_values
['is_billing_required']) ?
'billing' : ''));
954 * Handle pre approval for processors.
956 * This fits with the flow where a pre-approval is done and then confirmed in the next stage when confirm is hit.
958 * This function is shared between contribution & event forms & this is their common class.
960 * However, this should be seen as an in-progress refactor, the end goal being to also align the
961 * backoffice forms that action payments.
963 * @param array $params
965 protected function handlePreApproval(&$params) {
967 $payment = Civi\Payment\System
::singleton()->getByProcessor($this->_paymentProcessor
);
968 $params['component'] = $params['component'] ??
'contribute';
969 $result = $payment->doPreApproval($params);
970 if (empty($result)) {
971 // This could happen, for example, when paypal looks at the button value & decides it is not paypal express.
975 catch (\Civi\Payment\Exception\PaymentProcessorException
$e) {
976 CRM_Core_Error
::statusBounce(ts('Payment approval failed with message :') . $e->getMessage(), $payment->getCancelUrl($params['qfKey'], CRM_Utils_Array
::value('participant_id', $params)));
979 $this->set('pre_approval_parameters', $result['pre_approval_parameters']);
980 if (!empty($result['redirect_url'])) {
981 CRM_Utils_System
::redirect($result['redirect_url']);
986 * Setter function for options.
988 * @param mixed $options
990 public function setOptions($options) {
991 $this->_options
= $options;
995 * Render form and return contents.
999 public function toSmarty() {
1000 $this->preProcessChainSelectFields();
1001 $renderer = $this->getRenderer();
1002 $this->accept($renderer);
1003 $content = $renderer->toArray();
1004 $content['formName'] = $this->getName();
1006 $content['formClass'] = CRM_Utils_System
::getClassName($this);
1011 * Getter function for renderer.
1013 * If renderer is not set create one and initialize it.
1017 public function &getRenderer() {
1018 if (!isset($this->_renderer
)) {
1019 $this->_renderer
= CRM_Core_Form_Renderer
::singleton();
1021 return $this->_renderer
;
1025 * Use the form name to create the tpl file name.
1029 public function getTemplateFileName() {
1030 $ext = CRM_Extension_System
::singleton()->getMapper();
1031 if ($ext->isExtensionClass(CRM_Utils_System
::getClassName($this))) {
1032 $filename = $ext->getTemplateName(CRM_Utils_System
::getClassName($this));
1033 $tplname = $ext->getTemplatePath(CRM_Utils_System
::getClassName($this)) . DIRECTORY_SEPARATOR
. $filename;
1037 CRM_Utils_System
::getClassName($this),
1039 '_' => DIRECTORY_SEPARATOR
,
1040 '\\' => DIRECTORY_SEPARATOR
,
1048 * A wrapper for getTemplateFileName.
1050 * This includes calling the hook to prevent us from having to copy & paste the logic of calling the hook.
1052 public function getHookedTemplateFileName() {
1053 $pageTemplateFile = $this->getTemplateFileName();
1054 CRM_Utils_Hook
::alterTemplateFile(get_class($this), $this, 'page', $pageTemplateFile);
1055 return $pageTemplateFile;
1059 * Default extra tpl file basically just replaces .tpl with .extra.tpl.
1061 * i.e. we do not override.
1065 public function overrideExtraTemplateFileName() {
1070 * Error reporting mechanism.
1072 * @param string $message
1076 * @param CRM_Core_DAO $dao
1077 * A data access object on which we perform a rollback if non - empty.
1079 public function error($message, $code = NULL, $dao = NULL) {
1081 $dao->query('ROLLBACK');
1084 $error = CRM_Core_Error
::singleton();
1086 $error->push($code, $message);
1090 * Store the variable with the value in the form scope.
1092 * @param string $name
1093 * Name of the variable.
1094 * @param mixed $value
1095 * Value of the variable.
1097 public function set($name, $value) {
1098 $this->controller
->set($name, $value);
1102 * Get the variable from the form scope.
1104 * @param string $name
1105 * Name of the variable
1109 public function get($name) {
1110 return $this->controller
->get($name);
1114 * Getter for action.
1118 public function getAction() {
1119 return $this->_action
;
1123 * Setter for action.
1125 * @param int $action
1126 * The mode we want to set the form.
1128 public function setAction($action) {
1129 $this->_action
= $action;
1133 * Assign value to name in template.
1135 * @param string $var
1137 * @param mixed $value
1138 * Value of variable.
1140 public function assign($var, $value = NULL) {
1141 self
::$_template->assign($var, $value);
1145 * Assign value to name in template by reference.
1147 * @param string $var
1149 * @param mixed $value
1150 * Value of variable.
1152 public function assign_by_ref($var, &$value) {
1153 self
::$_template->assign_by_ref($var, $value);
1157 * Appends values to template variables.
1159 * @param array|string $tpl_var the template variable name(s)
1160 * @param mixed $value
1161 * The value to append.
1162 * @param bool $merge
1164 public function append($tpl_var, $value = NULL, $merge = FALSE) {
1165 self
::$_template->append($tpl_var, $value, $merge);
1169 * Returns an array containing template variables.
1171 * @param string $name
1175 public function get_template_vars($name = NULL) {
1176 return self
::$_template->get_template_vars($name);
1180 * @param string $name
1183 * @param array $attributes
1184 * @param null $separator
1185 * @param bool $required
1186 * @param array $optionAttributes - Option specific attributes
1188 * @return HTML_QuickForm_group
1190 public function &addRadio($name, $title, $values, $attributes = [], $separator = NULL, $required = FALSE, $optionAttributes = []) {
1192 $attributes = $attributes ?
$attributes : [];
1193 $allowClear = !empty($attributes['allowClear']);
1194 unset($attributes['allowClear']);
1195 $attributes['id_suffix'] = $name;
1196 foreach ($values as $key => $var) {
1197 $optAttributes = $attributes;
1198 if (!empty($optionAttributes[$key])) {
1199 foreach ($optionAttributes[$key] as $optAttr => $optVal) {
1200 if (!empty($optAttributes[$optAttr])) {
1201 $optAttributes[$optAttr] .= ' ' . $optVal;
1204 $optAttributes[$optAttr] = $optVal;
1208 // We use a class here to avoid html5 issues with collapsed cutsomfield sets.
1209 $optAttributes['class'] = $optAttributes['class'] ??
'';
1211 $optAttributes['class'] .= ' required';
1213 $element = $this->createElement('radio', NULL, NULL, $var, $key, $optAttributes);
1214 $options[] = $element;
1216 $group = $this->addGroup($options, $name, $title, $separator);
1218 $optionEditKey = 'data-option-edit-path';
1219 if (!empty($attributes[$optionEditKey])) {
1220 $group->setAttribute($optionEditKey, $attributes[$optionEditKey]);
1224 $this->addRule($name, ts('%1 is a required field.', [1 => $title]), 'required');
1227 $group->setAttribute('allowClear', TRUE);
1235 * @param bool $allowClear
1236 * @param null $required
1237 * @param array $attributes
1239 public function addYesNo($id, $title, $allowClear = FALSE, $required = NULL, $attributes = []) {
1240 $attributes +
= ['id_suffix' => $id];
1242 $choice[] = $this->createElement('radio', NULL, '11', ts('Yes'), '1', $attributes);
1243 $choice[] = $this->createElement('radio', NULL, '11', ts('No'), '0', $attributes);
1245 $group = $this->addGroup($choice, $id, $title);
1247 $group->setAttribute('allowClear', TRUE);
1250 $this->addRule($id, ts('%1 is a required field.', [1 => $title]), 'required');
1258 * @param null $other
1259 * @param null $attributes
1260 * @param null $required
1261 * @param null $javascriptMethod
1262 * @param string $separator
1263 * @param bool $flipValues
1265 public function addCheckBox(
1266 $id, $title, $values, $other = NULL,
1267 $attributes = NULL, $required = NULL,
1268 $javascriptMethod = NULL,
1269 $separator = '<br />', $flipValues = FALSE
1273 if ($javascriptMethod) {
1274 foreach ($values as $key => $var) {
1276 $options[] = $this->createElement('checkbox', $var, NULL, $key, $javascriptMethod, $attributes);
1279 $options[] = $this->createElement('checkbox', $key, NULL, $var, $javascriptMethod, $attributes);
1284 foreach ($values as $key => $var) {
1286 $options[] = $this->createElement('checkbox', $var, NULL, $key, $attributes);
1289 $options[] = $this->createElement('checkbox', $key, NULL, $var, $attributes);
1294 $group = $this->addGroup($options, $id, $title, $separator);
1295 $optionEditKey = 'data-option-edit-path';
1296 if (!empty($attributes[$optionEditKey])) {
1297 $group->setAttribute($optionEditKey, $attributes[$optionEditKey]);
1301 $this->addElement('text', $id . '_other', ts('Other'), $attributes[$id . '_other']);
1306 ts('%1 is a required field.', [1 => $title]),
1312 public function resetValues() {
1313 $data = $this->controller
->container();
1314 $data['values'][$this->_name
] = [];
1318 * Simple shell that derived classes can call to add buttons to
1319 * the form with a customized title for the main Submit
1321 * @param string $title
1322 * Title of the main button.
1323 * @param string $nextType
1324 * Button type for the form after processing.
1325 * @param string $backType
1326 * @param bool|string $submitOnce
1328 public function addDefaultButtons($title, $nextType = 'next', $backType = 'back', $submitOnce = FALSE) {
1330 if ($backType != NULL) {
1332 'type' => $backType,
1333 'name' => ts('Previous'),
1336 if ($nextType != NULL) {
1338 'type' => $nextType,
1340 'isDefault' => TRUE,
1343 $this->submitOnce
= TRUE;
1345 $buttons[] = $nextButton;
1347 $this->addButtons($buttons);
1351 * @param string $name
1352 * @param string $from
1354 * @param string $label
1355 * @param string $dateFormat
1356 * @param bool $required
1357 * @param bool $displayTime
1359 public function addDateRange($name, $from = '_from', $to = '_to', $label = 'From:', $dateFormat = 'searchDate', $required = FALSE, $displayTime = FALSE) {
1360 CRM_Core_Error
::deprecatedFunctionWarning('Use CRM_Core_Form::addDatePickerRange insted');
1362 $this->addDateTime($name . $from, $label, $required, ['formatType' => $dateFormat]);
1363 $this->addDateTime($name . $to, ts('To:'), $required, ['formatType' => $dateFormat]);
1366 $this->addDate($name . $from, $label, $required, ['formatType' => $dateFormat]);
1367 $this->addDate($name . $to, ts('To:'), $required, ['formatType' => $dateFormat]);
1372 * Add a search for a range using date picker fields.
1374 * @param string $fieldName
1375 * @param string $label
1376 * @param bool $isDateTime
1377 * Is this a date-time field (not just date).
1378 * @param bool $required
1379 * @param string $fromLabel
1380 * @param string $toLabel
1381 * @param array $additionalOptions
1382 * @param string $to string to append to the to field.
1383 * @param string $from string to append to the from field.
1385 public function addDatePickerRange($fieldName, $label, $isDateTime = FALSE, $required = FALSE, $fromLabel = 'From', $toLabel = 'To', $additionalOptions = [],
1386 $to = '_high', $from = '_low') {
1389 '' => ts('- any -'),
1390 0 => ts('Choose Date Range'),
1391 ] + CRM_Core_OptionGroup
::values('relative_date_filters');
1393 if ($additionalOptions) {
1394 foreach ($additionalOptions as $key => $optionLabel) {
1395 $options[$key] = $optionLabel;
1399 $this->add('select',
1400 "{$fieldName}_relative",
1404 ['class' => 'crm-select2']
1406 $attributes = ['formatType' => 'searchDate'];
1407 $extra = ['time' => $isDateTime];
1408 $this->add('datepicker', $fieldName . $from, ts($fromLabel), $attributes, $required, $extra);
1409 $this->add('datepicker', $fieldName . $to, ts($toLabel), $attributes, $required, $extra);
1413 * Based on form action, return a string representing the api action.
1414 * Used by addField method.
1418 protected function getApiAction() {
1419 $action = $this->getAction();
1420 if ($action & (CRM_Core_Action
::UPDATE + CRM_Core_Action
::ADD
)) {
1423 if ($action & (CRM_Core_Action
::VIEW + CRM_Core_Action
::BROWSE + CRM_Core_Action
::BASIC + CRM_Core_Action
::ADVANCED + CRM_Core_Action
::PREVIEW
)) {
1426 if ($action & (CRM_Core_Action
::DELETE
)) {
1429 // If you get this exception try adding more cases above.
1430 throw new Exception("Cannot determine api action for " . get_class($this) . '.' . 'CRM_Core_Action "' . CRM_Core_Action
::description($action) . '" not recognized.');
1434 * Classes extending CRM_Core_Form should implement this method.
1437 public function getDefaultEntity() {
1438 throw new Exception("Cannot determine default entity. " . get_class($this) . " should implement getDefaultEntity().");
1442 * Classes extending CRM_Core_Form should implement this method.
1444 * TODO: Merge with CRM_Core_DAO::buildOptionsContext($context) and add validation.
1447 public function getDefaultContext() {
1448 throw new Exception("Cannot determine default context. " . get_class($this) . " should implement getDefaultContext().");
1452 * Adds a select based on field metadata.
1453 * TODO: This could be even more generic and widget type (select in this case) could also be read from metadata
1454 * Perhaps a method like $form->bind($name) which would look up all metadata for named field
1456 * Field name to go on the form.
1457 * @param array $props
1458 * Mix of html attributes and special properties, namely.
1459 * - entity (api entity name, can usually be inferred automatically from the form class)
1460 * - field (field name - only needed if different from name used on the form)
1461 * - option_url - path to edit this option list - usually retrieved automatically - set to NULL to disable link
1462 * - placeholder - set to NULL to disable
1464 * - context - @see CRM_Core_DAO::buildOptionsContext
1465 * @param bool $required
1466 * @throws CRM_Core_Exception
1467 * @return HTML_QuickForm_Element
1469 public function addSelect($name, $props = [], $required = FALSE) {
1470 if (!isset($props['entity'])) {
1471 $props['entity'] = $this->getDefaultEntity();
1473 if (!isset($props['field'])) {
1474 $props['field'] = strrpos($name, '[') ?
rtrim(substr($name, 1 +
strrpos($name, '[')), ']') : $name;
1476 if (!isset($props['context'])) {
1478 $props['context'] = $this->getDefaultContext();
1480 // This is not a required param, so we'll ignore if this doesn't exist.
1481 catch (Exception
$e) {
1484 // Fetch options from the api unless passed explicitly
1485 if (isset($props['options'])) {
1486 $options = $props['options'];
1489 $info = civicrm_api3($props['entity'], 'getoptions', $props);
1490 $options = $info['values'];
1492 if (!array_key_exists('placeholder', $props)) {
1493 $props['placeholder'] = $required ?
ts('- select -') : (CRM_Utils_Array
::value('context', $props) == 'search' ?
ts('- any -') : ts('- none -'));
1495 // Handle custom field
1496 if (strpos($name, 'custom_') === 0 && is_numeric($name[7])) {
1497 list(, $id) = explode('_', $name);
1498 $label = $props['label'] ?? CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomField', 'label', $id);
1499 $gid = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomField', 'option_group_id', $id);
1500 if (CRM_Utils_Array
::value('context', $props) != 'search') {
1501 $props['data-option-edit-path'] = array_key_exists('option_url', $props) ?
$props['option_url'] : 'civicrm/admin/options/' . CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', $gid);
1506 $info = civicrm_api3($props['entity'], 'getfields');
1507 foreach ($info['values'] as $uniqueName => $fieldSpec) {
1509 $uniqueName === $props['field'] ||
1510 CRM_Utils_Array
::value('name', $fieldSpec) === $props['field'] ||
1511 in_array($props['field'], CRM_Utils_Array
::value('api.aliases', $fieldSpec, []))
1516 $label = $props['label'] ??
$fieldSpec['title'];
1517 if (CRM_Utils_Array
::value('context', $props) != 'search') {
1518 $props['data-option-edit-path'] = array_key_exists('option_url', $props) ?
$props['option_url'] : CRM_Core_PseudoConstant
::getOptionEditUrl($fieldSpec);
1521 $props['class'] = (isset($props['class']) ?
$props['class'] . ' ' : '') . "crm-select2";
1522 $props['data-api-entity'] = $props['entity'];
1523 $props['data-api-field'] = $props['field'];
1524 CRM_Utils_Array
::remove($props, 'label', 'entity', 'field', 'option_url', 'options', 'context');
1525 return $this->add('select', $name, $label, $options, $required, $props);
1529 * Adds a field based on metadata.
1532 * Field name to go on the form.
1533 * @param array $props
1534 * Mix of html attributes and special properties, namely.
1535 * - entity (api entity name, can usually be inferred automatically from the form class)
1536 * - name (field name - only needed if different from name used on the form)
1537 * - option_url - path to edit this option list - usually retrieved automatically - set to NULL to disable link
1538 * - placeholder - set to NULL to disable
1540 * - context - @see CRM_Core_DAO::buildOptionsContext
1541 * @param bool $required
1542 * @param bool $legacyDate
1543 * Temporary param to facilitate the conversion of fields to use the datepicker in
1544 * a controlled way. To convert the field the jcalendar code needs to be removed from the
1545 * tpl as well. That file is intended to be EOL.
1547 * @throws \CiviCRM_API3_Exception
1548 * @throws \Exception
1550 * HTML_QuickForm_Element
1553 public function addField($name, $props = [], $required = FALSE, $legacyDate = TRUE) {
1555 if (empty($props['context'])) {
1556 $props['context'] = $this->getDefaultContext();
1558 $context = $props['context'];
1560 if (empty($props['entity'])) {
1561 $props['entity'] = $this->getDefaultEntity();
1564 if (empty($props['name'])) {
1565 $props['name'] = strrpos($name, '[') ?
rtrim(substr($name, 1 +
strrpos($name, '[')), ']') : $name;
1568 if (empty($props['action'])) {
1569 $props['action'] = $this->getApiAction();
1572 // Handle custom fields
1573 if (strpos($name, 'custom_') === 0 && is_numeric($name[7])) {
1574 $fieldId = (int) substr($name, 7);
1575 return CRM_Core_BAO_CustomField
::addQuickFormElement($this, $name, $fieldId, $required, $context == 'search', CRM_Utils_Array
::value('label', $props));
1578 // Core field - get metadata.
1579 $fieldSpec = civicrm_api3($props['entity'], 'getfield', $props);
1580 $fieldSpec = $fieldSpec['values'];
1581 $fieldSpecLabel = $fieldSpec['html']['label'] ?? CRM_Utils_Array
::value('title', $fieldSpec);
1582 $label = CRM_Utils_Array
::value('label', $props, $fieldSpecLabel);
1584 $widget = $props['type'] ??
$fieldSpec['html']['type'];
1585 if ($widget == 'TextArea' && $context == 'search') {
1589 $isSelect = (in_array($widget, [
1598 // Fetch options from the api unless passed explicitly.
1599 if (isset($props['options'])) {
1600 $options = $props['options'];
1603 $options = $fieldSpec['options'] ??
NULL;
1605 if ($context == 'search') {
1606 $widget = $widget == 'Select2' ?
$widget : 'Select';
1607 $props['multiple'] = CRM_Utils_Array
::value('multiple', $props, TRUE);
1610 // Add data for popup link.
1611 $canEditOptions = CRM_Core_Permission
::check('administer CiviCRM');
1612 $hasOptionUrl = !empty($props['option_url']);
1613 $optionUrlKeyIsSet = array_key_exists('option_url', $props);
1614 $shouldAdd = $context !== 'search' && $isSelect && $canEditOptions;
1616 // Only add if key is not set, or if non-empty option url is provided
1617 if (($hasOptionUrl ||
!$optionUrlKeyIsSet) && $shouldAdd) {
1618 $optionUrl = $hasOptionUrl ?
$props['option_url'] :
1619 CRM_Core_PseudoConstant
::getOptionEditUrl($fieldSpec);
1620 $props['data-option-edit-path'] = $optionUrl;
1621 $props['data-api-entity'] = $props['entity'];
1622 $props['data-api-field'] = $props['name'];
1625 $props +
= CRM_Utils_Array
::value('html', $fieldSpec, []);
1626 CRM_Utils_Array
::remove($props, 'entity', 'name', 'context', 'label', 'action', 'type', 'option_url', 'options');
1628 // TODO: refactor switch statement, to separate methods.
1634 //TODO: Autodetect ranges
1635 $props['size'] = $props['size'] ??
60;
1636 return $this->add(strtolower($widget), $name, $label, $props, $required);
1639 return $this->add('hidden', $name, NULL, $props, $required);
1642 //Set default columns and rows for textarea.
1643 $props['rows'] = $props['rows'] ??
4;
1644 $props['cols'] = $props['cols'] ??
60;
1645 if (empty($props['maxlength']) && isset($fieldSpec['length'])) {
1646 $props['maxlength'] = $fieldSpec['length'];
1648 return $this->add('textarea', $name, $label, $props, $required);
1651 // This is a white list for fields that have been tested with
1652 // date picker. We should be able to remove the other
1654 //TODO: add range support
1655 //TODO: Add date formats
1656 //TODO: Add javascript template for dates.
1657 return $this->addDate($name, $label, $required, $props);
1660 $fieldSpec = CRM_Utils_Date
::addDateMetadataToField($fieldSpec, $fieldSpec);
1661 $attributes = ['format' => $fieldSpec['date_format']];
1662 return $this->add('datepicker', $name, $label, $attributes, $required, $fieldSpec['datepicker']['extra']);
1666 $separator = $props['separator'] ??
NULL;
1667 unset($props['separator']);
1668 if (!isset($props['allowClear'])) {
1669 $props['allowClear'] = !$required;
1671 return $this->addRadio($name, $label, $options, $props, $separator, $required);
1675 'required' => $required,
1677 'multiple' => $context == 'search',
1679 return $this->addChainSelect($name, $props);
1683 $props['class'] = CRM_Utils_Array
::value('class', $props, 'big') . ' crm-select2';
1684 if (!array_key_exists('placeholder', $props)) {
1685 $props['placeholder'] = $required ?
ts('- select -') : ($context == 'search' ?
ts('- any -') : ts('- none -'));
1687 // TODO: Add and/or option for fields that store multiple values
1688 return $this->add(strtolower($widget), $name, $label, $options, $required, $props);
1690 case 'CheckBoxGroup':
1691 return $this->addCheckBox($name, $label, array_flip($options), $required, $props);
1694 return $this->addRadio($name, $label, $options, $props, NULL, $required);
1697 if ($context === 'search') {
1698 $this->addYesNo($name, $label, TRUE, FALSE, $props);
1701 $text = $props['text'] ??
NULL;
1702 unset($props['text']);
1703 return $this->addElement('checkbox', $name, $label, $text, $props);
1705 //add support for 'Advcheckbox' field
1707 $text = $props['text'] ??
NULL;
1708 unset($props['text']);
1709 return $this->addElement('advcheckbox', $name, $label, $text, $props);
1712 // We should not build upload file in search mode.
1713 if ($context == 'search') {
1716 $file = $this->add('file', $name, $label, $props, $required);
1717 $this->addUploadElement($name);
1720 case 'RichTextEditor':
1721 return $this->add('wysiwyg', $name, $label, $props, $required);
1724 return $this->addEntityRef($name, $label, $props, $required);
1727 $props['size'] = $props['size'] ??
60;
1728 return $this->add('password', $name, $label, $props, $required);
1730 // Check datatypes of fields
1734 //case read only fields
1736 throw new Exception("Unsupported html-element " . $widget);
1741 * Add a widget for selecting/editing/creating/copying a profile form
1743 * @param string $name
1744 * HTML form-element name.
1745 * @param string $label
1747 * @param string $allowCoreTypes
1748 * Only present a UFGroup if its group_type includes a subset of $allowCoreTypes; e.g. 'Individual', 'Activity'.
1749 * @param string $allowSubTypes
1750 * Only present a UFGroup if its group_type is compatible with $allowSubypes.
1751 * @param array $entities
1752 * @param bool $default
1754 * @param string $usedFor
1756 public function addProfileSelector($name, $label, $allowCoreTypes, $allowSubTypes, $entities, $default = FALSE, $usedFor = NULL) {
1758 // FIXME: Instead of adhoc serialization, use a single json_encode()
1759 CRM_UF_Page_ProfileEditor
::registerProfileScripts();
1760 CRM_UF_Page_ProfileEditor
::registerSchemas(CRM_Utils_Array
::collect('entity_type', $entities));
1761 $this->add('text', $name, $label, [
1762 'class' => 'crm-profile-selector',
1763 // Note: client treats ';;' as equivalent to \0, and ';;' works better in HTML
1764 'data-group-type' => CRM_Core_BAO_UFGroup
::encodeGroupType($allowCoreTypes, $allowSubTypes, ';;'),
1765 'data-entities' => json_encode($entities),
1767 'data-default' => $default,
1768 'data-usedfor' => json_encode($usedFor),
1775 public function getRootTitle() {
1782 public function getCompleteTitle() {
1783 return $this->getRootTitle() . $this->getTitle();
1787 * @return CRM_Core_Smarty
1789 public static function &getTemplate() {
1790 return self
::$_template;
1794 * @param $elementName
1796 public function addUploadElement($elementName) {
1797 $uploadNames = $this->get('uploadNames');
1798 if (!$uploadNames) {
1801 if (is_array($elementName)) {
1802 foreach ($elementName as $name) {
1803 if (!in_array($name, $uploadNames)) {
1804 $uploadNames[] = $name;
1809 if (!in_array($elementName, $uploadNames)) {
1810 $uploadNames[] = $elementName;
1813 $this->set('uploadNames', $uploadNames);
1815 $config = CRM_Core_Config
::singleton();
1816 if (!empty($uploadNames)) {
1817 $this->controller
->addUploadAction($config->customFileUploadDir
, $uploadNames);
1826 public function getVar($name) {
1827 return $this->$name ??
NULL;
1834 public function setVar($name, $value) {
1835 $this->$name = $value;
1842 * Use $this->add('datepicker', ...) instead.
1844 * @param string $name
1845 * Name of the element.
1846 * @param string $label
1847 * Label of the element.
1848 * @param bool $required
1850 * @param array $attributes
1853 public function addDate($name, $label, $required = FALSE, $attributes = NULL) {
1854 if (!empty($attributes['formatType'])) {
1855 // get actual format
1856 $params = ['name' => $attributes['formatType']];
1859 // cache date information
1861 $key = "dateFormat_" . str_replace(' ', '_', $attributes['formatType']);
1862 if (empty($dateFormat[$key])) {
1863 CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_PreferencesDate', $params, $values);
1864 $dateFormat[$key] = $values;
1867 $values = $dateFormat[$key];
1870 if ($values['date_format']) {
1871 $attributes['format'] = $values['date_format'];
1874 if (!empty($values['time_format'])) {
1875 $attributes['timeFormat'] = $values['time_format'];
1877 $attributes['startOffset'] = $values['start'];
1878 $attributes['endOffset'] = $values['end'];
1881 $config = CRM_Core_Config
::singleton();
1882 if (empty($attributes['format'])) {
1883 $attributes['format'] = $config->dateInputFormat
;
1886 if (!isset($attributes['startOffset'])) {
1887 $attributes['startOffset'] = 10;
1890 if (!isset($attributes['endOffset'])) {
1891 $attributes['endOffset'] = 10;
1894 $this->add('text', $name, $label, $attributes);
1896 if (!empty($attributes['addTime']) ||
!empty($attributes['timeFormat'])) {
1898 if (!isset($attributes['timeFormat'])) {
1899 $timeFormat = $config->timeInputFormat
;
1902 $timeFormat = $attributes['timeFormat'];
1905 // 1 - 12 hours and 2 - 24 hours, but for jquery widget it is 0 and 1 respectively
1907 $show24Hours = TRUE;
1908 if ($timeFormat == 1) {
1909 $show24Hours = FALSE;
1912 //CRM-6664 -we are having time element name
1913 //in either flat string or an array format.
1914 $elementName = $name . '_time';
1915 if (substr($name, -1) == ']') {
1916 $elementName = substr($name, 0, strlen($name) - 1) . '_time]';
1919 $this->add('text', $elementName, ts('Time'), ['timeFormat' => $show24Hours]);
1924 $this->addRule($name, ts('Please select %1', [1 => $label]), 'required');
1925 if (!empty($attributes['addTime']) && !empty($attributes['addTimeRequired'])) {
1926 $this->addRule($elementName, ts('Please enter a time.'), 'required');
1932 * Function that will add date and time.
1935 * Use $this->add('datepicker', ...) instead.
1937 * @param string $name
1938 * @param string $label
1939 * @param bool $required
1940 * @param null $attributes
1942 public function addDateTime($name, $label, $required = FALSE, $attributes = NULL) {
1943 $addTime = ['addTime' => TRUE];
1944 if (is_array($attributes)) {
1945 $attributes = array_merge($attributes, $addTime);
1948 $attributes = $addTime;
1951 $this->addDate($name, $label, $required, $attributes);
1955 * Add a currency and money element to the form.
1957 * @param string $name
1958 * @param string $label
1959 * @param bool $required
1960 * @param null $attributes
1961 * @param bool $addCurrency
1962 * @param string $currencyName
1963 * @param null $defaultCurrency
1964 * @param bool $freezeCurrency
1966 * @return \HTML_QuickForm_Element
1968 public function addMoney(
1973 $addCurrency = TRUE,
1974 $currencyName = 'currency',
1975 $defaultCurrency = NULL,
1976 $freezeCurrency = FALSE
1978 $element = $this->add('text', $name, $label, $attributes, $required);
1979 $this->addRule($name, ts('Please enter a valid amount.'), 'money');
1982 $ele = $this->addCurrency($currencyName, NULL, TRUE, $defaultCurrency, $freezeCurrency);
1989 * Add currency element to the form.
1991 * @param string $name
1992 * @param null $label
1993 * @param bool $required
1994 * @param string $defaultCurrency
1995 * @param bool $freezeCurrency
1996 * @param bool $setDefaultCurrency
1998 public function addCurrency(
2002 $defaultCurrency = NULL,
2003 $freezeCurrency = FALSE,
2004 $setDefaultCurrency = TRUE
2006 $currencies = CRM_Core_OptionGroup
::values('currencies_enabled');
2007 if (!empty($defaultCurrency) && !array_key_exists($defaultCurrency, $currencies)) {
2008 Civi
::log()->warning('addCurrency: Currency ' . $defaultCurrency . ' is disabled but still in use!');
2009 $currencies[$defaultCurrency] = $defaultCurrency;
2011 $options = ['class' => 'crm-select2 eight'];
2013 $currencies = ['' => ''] +
$currencies;
2014 $options['placeholder'] = ts('- none -');
2016 $ele = $this->add('select', $name, $label, $currencies, $required, $options);
2017 if ($freezeCurrency) {
2020 if (!$defaultCurrency) {
2021 $config = CRM_Core_Config
::singleton();
2022 $defaultCurrency = $config->defaultCurrency
;
2024 // In some case, setting currency field by default might override the default value
2025 // as encountered in CRM-20527 for batch data entry
2026 if ($setDefaultCurrency) {
2027 $this->setDefaults([$name => $defaultCurrency]);
2032 * Create a single or multiple entity ref field.
2033 * @param string $name
2034 * @param string $label
2035 * @param array $props
2036 * Mix of html and widget properties, including:.
2037 * - select - params to give to select2 widget
2038 * - entity - defaults to Contact
2039 * - create - can the user create a new entity on-the-fly?
2040 * Set to TRUE if entity is contact and you want the default profiles,
2041 * or pass in your own set of links. @see CRM_Campaign_BAO_Campaign::getEntityRefCreateLinks for format
2042 * note that permissions are checked automatically
2043 * - api - array of settings for the getlist api wrapper
2044 * note that it accepts a 'params' setting which will be passed to the underlying api
2045 * - placeholder - string
2047 * - class, etc. - other html properties
2048 * @param bool $required
2050 * @return HTML_QuickForm_Element
2052 public function addEntityRef($name, $label = '', $props = [], $required = FALSE) {
2053 // Default properties
2054 $props['api'] = CRM_Utils_Array
::value('api', $props, []);
2055 $props['entity'] = CRM_Core_DAO_AllCoreTables
::convertEntityNameToCamel(CRM_Utils_Array
::value('entity', $props, 'Contact'));
2056 $props['class'] = ltrim(CRM_Utils_Array
::value('class', $props, '') . ' crm-form-entityref');
2058 if (array_key_exists('create', $props) && empty($props['create'])) {
2059 unset($props['create']);
2062 $props['placeholder'] = CRM_Utils_Array
::value('placeholder', $props, $required ?
ts('- select %1 -', [1 => ts(str_replace('_', ' ', $props['entity']))]) : ts('- none -'));
2065 if (!empty($props['multiple'])) {
2066 $defaults['multiple'] = TRUE;
2068 $props['select'] = CRM_Utils_Array
::value('select', $props, []) +
$defaults;
2070 $this->formatReferenceFieldAttributes($props, get_class($this));
2071 return $this->add('text', $name, $label, $props, $required);
2075 * @param array $props
2076 * @param string $formName
2078 private function formatReferenceFieldAttributes(&$props, $formName) {
2079 CRM_Utils_Hook
::alterEntityRefParams($props, $formName);
2080 $props['data-select-params'] = json_encode($props['select']);
2081 $props['data-api-params'] = $props['api'] ?
json_encode($props['api']) : NULL;
2082 $props['data-api-entity'] = $props['entity'];
2083 if (!empty($props['create'])) {
2084 $props['data-create-links'] = json_encode($props['create']);
2086 CRM_Utils_Array
::remove($props, 'multiple', 'select', 'api', 'entity', 'create');
2090 * Convert all date fields within the params to mysql date ready for the
2091 * BAO layer. In this case fields are checked against the $_datefields defined for the form
2092 * and if time is defined it is incorporated
2094 * @param array $params
2095 * Input params from the form.
2097 * @todo it would probably be better to work on $this->_params than a passed array
2098 * @todo standardise the format which dates are passed to the BAO layer in & remove date
2101 public function convertDateFieldsToMySQL(&$params) {
2102 foreach ($this->_dateFields
as $fieldName => $specs) {
2103 if (!empty($params[$fieldName])) {
2104 $params[$fieldName] = CRM_Utils_Date
::isoToMysql(
2105 CRM_Utils_Date
::processDate(
2106 $params[$fieldName],
2107 CRM_Utils_Array
::value("{$fieldName}_time", $params), TRUE)
2111 if (isset($specs['default'])) {
2112 $params[$fieldName] = date('YmdHis', strtotime($specs['default']));
2119 * @param $elementName
2121 public function removeFileRequiredRules($elementName) {
2122 $this->_required
= array_diff($this->_required
, [$elementName]);
2123 if (isset($this->_rules
[$elementName])) {
2124 foreach ($this->_rules
[$elementName] as $index => $ruleInfo) {
2125 if ($ruleInfo['type'] == 'uploadedfile') {
2126 unset($this->_rules
[$elementName][$index]);
2129 if (empty($this->_rules
[$elementName])) {
2130 unset($this->_rules
[$elementName]);
2136 * Function that can be defined in Form to override or.
2137 * perform specific action on cancel action
2139 public function cancelAction() {
2143 * Helper function to verify that required fields have been filled.
2145 * Typically called within the scope of a FormRule function
2147 * @param array $fields
2148 * @param array $values
2149 * @param array $errors
2151 public static function validateMandatoryFields($fields, $values, &$errors) {
2152 foreach ($fields as $name => $fld) {
2153 if (!empty($fld['is_required']) && CRM_Utils_System
::isNull(CRM_Utils_Array
::value($name, $values))) {
2154 $errors[$name] = ts('%1 is a required field.', [1 => $fld['title']]);
2160 * Get contact if for a form object. Prioritise
2161 * - cid in URL if 0 (on behalf on someoneelse)
2162 * (@todo consider setting a variable if onbehalf for clarity of downstream 'if's
2163 * - logged in user id if it matches the one in the cid in the URL
2164 * - contact id validated from a checksum from a checksum
2165 * - cid from the url if the caller has ACL permission to view
2166 * - fallback is logged in user (or ? NULL if no logged in user) (@todo wouldn't 0 be more intuitive?)
2170 protected function setContactID() {
2171 $tempID = CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
2172 if (isset($this->_params
) && !empty($this->_params
['select_contact_id'])) {
2173 $tempID = $this->_params
['select_contact_id'];
2175 if (isset($this->_params
, $this->_params
[0]) && !empty($this->_params
[0]['select_contact_id'])) {
2176 // event form stores as an indexed array, contribution form not so much...
2177 $tempID = $this->_params
[0]['select_contact_id'];
2180 // force to ignore the authenticated user
2181 if ($tempID === '0' ||
$tempID === 0) {
2182 // we set the cid on the form so that this will be retained for the Confirm page
2183 // in the multi-page form & prevent us returning the $userID when this is called
2185 // we don't really need to set it when $tempID is set because the params have that stored
2186 $this->set('cid', 0);
2187 CRM_Core_Resources
::singleton()->addVars('coreForm', ['contact_id' => (int) $tempID]);
2188 return (int) $tempID;
2191 $userID = $this->getLoggedInUserContactID();
2193 if (!is_null($tempID) && $tempID === $userID) {
2194 CRM_Core_Resources
::singleton()->addVars('coreForm', ['contact_id' => (int) $tempID]);
2195 return (int) $userID;
2198 //check if this is a checksum authentication
2199 $userChecksum = CRM_Utils_Request
::retrieve('cs', 'String', $this);
2200 if ($userChecksum) {
2201 //check for anonymous user.
2202 $validUser = CRM_Contact_BAO_Contact_Utils
::validChecksum($tempID, $userChecksum);
2204 CRM_Core_Resources
::singleton()->addVars('coreForm', ['contact_id' => (int) $tempID]);
2205 CRM_Core_Resources
::singleton()->addVars('coreForm', ['checksum' => $userChecksum]);
2209 // check if user has permission, CRM-12062
2210 elseif ($tempID && CRM_Contact_BAO_Contact_Permission
::allow($tempID)) {
2211 CRM_Core_Resources
::singleton()->addVars('coreForm', ['contact_id' => (int) $tempID]);
2214 if (is_numeric($userID)) {
2215 CRM_Core_Resources
::singleton()->addVars('coreForm', ['contact_id' => (int) $userID]);
2217 return is_numeric($userID) ?
$userID : NULL;
2221 * Get the contact id that the form is being submitted for.
2225 public function getContactID() {
2226 return $this->setContactID();
2230 * Get the contact id of the logged in user.
2234 public function getLoggedInUserContactID() {
2235 // check if the user is logged in and has a contact ID
2236 $session = CRM_Core_Session
::singleton();
2237 return $session->get('userID') ?
(int) $session->get('userID') : FALSE;
2241 * Add autoselector field -if user has permission to view contacts
2242 * If adding this to a form you also need to add to the tpl e.g
2244 * {if !empty($selectable)}
2245 * <div class="crm-summary-row">
2246 * <div class="crm-label">{$form.select_contact.label}</div>
2247 * <div class="crm-content">
2248 * {$form.select_contact.html}
2253 * @param array $profiles
2254 * Ids of profiles that are on the form (to be autofilled).
2255 * @param array $autoCompleteField
2259 * - url (for ajax lookup)
2261 * @throws \CRM_Core_Exception
2262 * @todo add data attributes so we can deal with multiple instances on a form
2264 public function addAutoSelector($profiles = [], $autoCompleteField = []) {
2265 $autoCompleteField = array_merge([
2266 'id_field' => 'select_contact_id',
2267 'placeholder' => ts('Select someone else ...'),
2268 'show_hide' => TRUE,
2269 'api' => ['params' => ['contact_type' => 'Individual']],
2270 ], $autoCompleteField);
2272 if ($this->canUseAjaxContactLookups()) {
2273 $this->assign('selectable', $autoCompleteField['id_field']);
2274 $this->addEntityRef($autoCompleteField['id_field'], NULL, [
2275 'placeholder' => $autoCompleteField['placeholder'],
2276 'api' => $autoCompleteField['api'],
2279 CRM_Core_Resources
::singleton()->addScriptFile('civicrm', 'js/AlternateContactSelector.js', 1, 'html-header')
2281 'form' => ['autocompletes' => $autoCompleteField],
2282 'ids' => ['profile' => $profiles],
2289 public function canUseAjaxContactLookups() {
2290 if (0 < (civicrm_api3('contact', 'getcount', ['check_permissions' => 1])) &&
2291 CRM_Core_Permission
::check([['access AJAX API', 'access CiviCRM']])
2298 * Add the options appropriate to cid = zero - ie. autocomplete
2300 * @todo there is considerable code duplication between the contribution forms & event forms. It is apparent
2301 * that small pieces of duplication are not being refactored into separate functions because their only shared parent
2302 * is this form. Inserting a class FrontEndForm.php between the contribution & event & this class would allow functions like this
2303 * and a dozen other small ones to be refactored into a shared parent with the reduction of much code duplication
2305 public function addCIDZeroOptions() {
2306 $this->assign('nocid', TRUE);
2308 if ($this->_values
['custom_pre_id']) {
2309 $profiles[] = $this->_values
['custom_pre_id'];
2311 if ($this->_values
['custom_post_id']) {
2312 $profiles = array_merge($profiles, (array) $this->_values
['custom_post_id']);
2314 $profiles[] = 'billing';
2315 if (!empty($this->_values
)) {
2316 $this->addAutoSelector($profiles);
2321 * Set default values on form for given contact (or no contact defaults)
2323 * @param mixed $profile_id
2324 * (can be id, or profile name).
2325 * @param int $contactID
2329 public function getProfileDefaults($profile_id = 'Billing', $contactID = NULL) {
2331 $defaults = civicrm_api3('profile', 'getsingle', [
2332 'profile_id' => (array) $profile_id,
2333 'contact_id' => $contactID,
2337 catch (Exception
$e) {
2338 // the try catch block gives us silent failure -not 100% sure this is a good idea
2339 // as silent failures are often worse than noisy ones
2345 * Sets form attribute.
2348 public function preventAjaxSubmit() {
2349 $this->setAttribute('data-no-ajax-submit', 'true');
2353 * Sets form attribute.
2356 public function allowAjaxSubmit() {
2357 $this->removeAttribute('data-no-ajax-submit');
2361 * Sets page title based on entity and action.
2362 * @param string $entityLabel
2364 public function setPageTitle($entityLabel) {
2365 switch ($this->_action
) {
2366 case CRM_Core_Action
::ADD
:
2367 CRM_Utils_System
::setTitle(ts('New %1', [1 => $entityLabel]));
2370 case CRM_Core_Action
::UPDATE
:
2371 CRM_Utils_System
::setTitle(ts('Edit %1', [1 => $entityLabel]));
2374 case CRM_Core_Action
::VIEW
:
2375 case CRM_Core_Action
::PREVIEW
:
2376 CRM_Utils_System
::setTitle(ts('View %1', [1 => $entityLabel]));
2379 case CRM_Core_Action
::DELETE
:
2380 CRM_Utils_System
::setTitle(ts('Delete %1', [1 => $entityLabel]));
2386 * Create a chain-select target field. All settings are optional; the defaults usually work.
2388 * @param string $elementName
2389 * @param array $settings
2391 * @return HTML_QuickForm_Element
2393 public function addChainSelect($elementName, $settings = []) {
2394 $props = $settings +
= [
2395 'control_field' => str_replace(['state_province', 'StateProvince', 'county', 'County'], [
2401 'data-callback' => strpos($elementName, 'rovince') ?
'civicrm/ajax/jqState' : 'civicrm/ajax/jqCounty',
2402 'label' => strpos($elementName, 'rovince') ?
ts('State/Province') : ts('County'),
2403 'data-empty-prompt' => strpos($elementName, 'rovince') ?
ts('Choose country first') : ts('Choose state first'),
2404 'data-none-prompt' => ts('- N/A -'),
2405 'multiple' => FALSE,
2406 'required' => FALSE,
2407 'placeholder' => empty($settings['required']) ?
ts('- none -') : ts('- select -'),
2409 CRM_Utils_Array
::remove($props, 'label', 'required', 'control_field', 'context');
2410 $props['class'] = (empty($props['class']) ?
'' : "{$props['class']} ") . 'crm-select2';
2411 $props['data-select-prompt'] = $props['placeholder'];
2412 $props['data-name'] = $elementName;
2414 $this->_chainSelectFields
[$settings['control_field']] = $elementName;
2416 // Passing NULL instead of an array of options
2417 // CRM-15225 - normally QF will reject any selected values that are not part of the field's options, but due to a
2418 // quirk in our patched version of HTML_QuickForm_select, this doesn't happen if the options are NULL
2419 // which seems a bit dirty but it allows our dynamically-popuplated select element to function as expected.
2420 return $this->add('select', $elementName, $settings['label'], NULL, $settings['required'], $props);
2424 * Add actions menu to results form.
2426 * @param array $tasks
2428 public function addTaskMenu($tasks) {
2429 if (is_array($tasks) && !empty($tasks)) {
2430 // Set constants means this will always load with an empty value, not reloading any submitted value.
2431 // This is appropriate as it is a pseudofield.
2432 $this->setConstants(['task' => '']);
2433 $this->assign('taskMetaData', $tasks);
2434 $select = $this->add('select', 'task', NULL, ['' => ts('Actions')], FALSE, [
2435 'class' => 'crm-select2 crm-action-menu fa-check-circle-o huge crm-search-result-actions',
2438 foreach ($tasks as $key => $task) {
2440 if (isset($task['data'])) {
2441 foreach ($task['data'] as $dataKey => $dataValue) {
2442 $attributes['data-' . $dataKey] = $dataValue;
2445 $select->addOption($task['title'], $key, $attributes);
2447 if (empty($this->_actionButtonName
)) {
2448 $this->_actionButtonName
= $this->getButtonName('next', 'action');
2450 $this->assign('actionButtonName', $this->_actionButtonName
);
2451 $this->add('submit', $this->_actionButtonName
, ts('Go'), ['class' => 'hiddenElement crm-search-go-button']);
2453 // Radio to choose "All items" or "Selected items only"
2454 $selectedRowsRadio = $this->addElement('radio', 'radio_ts', NULL, '', 'ts_sel', ['checked' => 'checked']);
2455 $allRowsRadio = $this->addElement('radio', 'radio_ts', NULL, '', 'ts_all');
2456 $this->assign('ts_sel_id', $selectedRowsRadio->_attributes
['id']);
2457 $this->assign('ts_all_id', $allRowsRadio->_attributes
['id']);
2459 CRM_Core_Resources
::singleton()->addScriptFile('civicrm', 'js/crm.searchForm.js', 1, 'html-header');
2464 * Set options and attributes for chain select fields based on the controlling field's value
2466 private function preProcessChainSelectFields() {
2467 foreach ($this->_chainSelectFields
as $control => $target) {
2468 // The 'target' might get missing if extensions do removeElement() in a form hook.
2469 if ($this->elementExists($target)) {
2470 $targetField = $this->getElement($target);
2471 $targetType = $targetField->getAttribute('data-callback') == 'civicrm/ajax/jqCounty' ?
'county' : 'stateProvince';
2473 // If the control field is on the form, setup chain-select and dynamically populate options
2474 if ($this->elementExists($control)) {
2475 $controlField = $this->getElement($control);
2476 $controlType = $targetType == 'county' ?
'stateProvince' : 'country';
2478 $targetField->setAttribute('class', $targetField->getAttribute('class') . ' crm-chain-select-target');
2480 $css = (string) $controlField->getAttribute('class');
2481 $controlField->updateAttributes([
2482 'class' => ($css ?
"$css " : 'crm-select2 ') . 'crm-chain-select-control',
2483 'data-target' => $target,
2485 $controlValue = $controlField->getValue();
2486 if ($controlValue) {
2487 $options = CRM_Core_BAO_Location
::getChainSelectValues($controlValue, $controlType, TRUE);
2489 $targetField->setAttribute('placeholder', $targetField->getAttribute('data-none-prompt'));
2493 $targetField->setAttribute('placeholder', $targetField->getAttribute('data-empty-prompt'));
2494 $targetField->setAttribute('disabled', 'disabled');
2497 // Control field not present - fall back to loading default options
2499 $options = CRM_Core_PseudoConstant
::$targetType();
2501 if (!$targetField->getAttribute('multiple')) {
2502 $options = ['' => $targetField->getAttribute('placeholder')] +
$options;
2503 $targetField->removeAttribute('placeholder');
2505 $targetField->_options
= [];
2506 $targetField->loadArray($options);
2512 * Validate country / state / county match and suppress unwanted "required" errors
2514 private function validateChainSelectFields() {
2515 foreach ($this->_chainSelectFields
as $control => $target) {
2516 if ($this->elementExists($control) && $this->elementExists($target)) {
2517 $controlValue = (array) $this->getElementValue($control);
2518 $targetField = $this->getElement($target);
2519 $controlType = $targetField->getAttribute('data-callback') == 'civicrm/ajax/jqCounty' ?
'stateProvince' : 'country';
2520 $targetValue = array_filter((array) $targetField->getValue());
2521 if ($targetValue ||
$this->getElementError($target)) {
2522 $options = CRM_Core_BAO_Location
::getChainSelectValues($controlValue, $controlType, TRUE);
2524 if (!array_intersect($targetValue, array_keys($options))) {
2525 $this->setElementError($target, $controlType == 'country' ?
ts('State/Province does not match the selected Country') : ts('County does not match the selected State/Province'));
2528 // Suppress "required" error for field if it has no options
2529 elseif (!$options) {
2530 $this->setElementError($target, NULL);
2538 * Assign billing name to the template.
2540 * @param array $params
2541 * Form input params, default to $this->_params.
2545 public function assignBillingName($params = []) {
2547 if (empty($params)) {
2548 $params = $this->_params
;
2550 if (!empty($params['billing_first_name'])) {
2551 $name = $params['billing_first_name'];
2554 if (!empty($params['billing_middle_name'])) {
2555 $name .= " {$params['billing_middle_name']}";
2558 if (!empty($params['billing_last_name'])) {
2559 $name .= " {$params['billing_last_name']}";
2561 $name = trim($name);
2562 $this->assign('billingName', $name);
2567 * Get the currency for the form.
2569 * @todo this should be overriden on the forms rather than having this
2570 * historic, possible handling in here. As we clean that up we should
2571 * add deprecation notices into here.
2573 * @param array $submittedValues
2574 * Array allowed so forms inheriting this class do not break.
2575 * Ideally we would make a clear standard around how submitted values
2576 * are stored (is $this->_values consistently doing that?).
2580 public function getCurrency($submittedValues = []) {
2581 $currency = $this->_values
['currency'] ??
NULL;
2582 // For event forms, currency is in a different spot
2583 if (empty($currency)) {
2584 $currency = CRM_Utils_Array
::value('currency', CRM_Utils_Array
::value('event', $this->_values
));
2586 if (empty($currency)) {
2587 $currency = CRM_Utils_Request
::retrieveValue('currency', 'String');
2589 // @todo If empty there is a problem - we should probably put in a deprecation notice
2590 // to warn if that seems to be happening.
2595 * Is the form in view or edit mode.
2597 * The 'addField' function relies on the form action being one of a set list
2598 * of actions. Checking for these allows for an early return.
2602 protected function isFormInViewOrEditMode() {
2603 return $this->isFormInViewMode() ||
$this->isFormInEditMode();
2607 * Is the form in edit mode.
2609 * Helper function, notably for extensions implementing the buildForm hook,
2610 * so that they can return early.
2614 public function isFormInEditMode() {
2615 return in_array($this->_action
, [
2616 CRM_Core_Action
::UPDATE
,
2617 CRM_Core_Action
::ADD
,
2618 CRM_Core_Action
::BROWSE
,
2619 CRM_Core_Action
::BASIC
,
2620 CRM_Core_Action
::ADVANCED
,
2621 CRM_Core_Action
::PREVIEW
,
2626 * Is the form in view mode.
2628 * Helper function, notably for extensions implementing the buildForm hook,
2629 * so that they can return early.
2633 public function isFormInViewMode() {
2634 return $this->_action
== CRM_Core_Action
::VIEW
;
2638 * Set the active tab
2640 * @param string $default
2642 * @throws \CRM_Core_Exception
2644 public function setSelectedChild($default = NULL) {
2645 $selectedChild = CRM_Utils_Request
::retrieve('selectedChild', 'Alphanumeric', $this, FALSE, $default);
2646 if (!empty($selectedChild)) {
2647 $this->set('selectedChild', $selectedChild);
2648 $this->assign('selectedChild', $selectedChild);
2653 * Get the contact if from the url, using the checksum or the cid if it is the logged in user.
2655 * This function returns the user being validated. It is not intended to get another user
2656 * they have permission to (setContactID does do that) and can be used to check if the user is
2657 * accessing their own record.
2660 * @throws \CRM_Core_Exception
2662 protected function getContactIDIfAccessingOwnRecord() {
2663 $contactID = (int) CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
2667 if ($contactID === $this->getLoggedInUserContactID()) {
2670 $userChecksum = CRM_Utils_Request
::retrieve('cs', 'String', $this);
2671 return CRM_Contact_BAO_Contact_Utils
::validChecksum($contactID, $userChecksum) ?
$contactID : FALSE;