3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
29 * This is our base form. It is part of the Form/Controller/StateMachine
30 * trifecta. Each form is associated with a specific state in the state
31 * machine. Each form can also operate in various modes
34 * @copyright CiviCRM LLC (c) 2004-2019
37 require_once 'HTML/QuickForm/Page.php';
42 class CRM_Core_Form
extends HTML_QuickForm_Page
{
45 * The state object that this form belongs to
51 * The name of this form
57 * The title of this form
60 protected $_title = NULL;
63 * The default values for the form.
67 public $_defaults = [];
70 * (QUASI-PROTECTED) The options passed into this form
72 * This field should marked `protected` and is not generally
73 * intended for external callers, but some edge-cases do use it.
77 public $_options = NULL;
80 * (QUASI-PROTECTED) The mode of operation for this form
82 * This field should marked `protected` and is not generally
83 * intended for external callers, but some edge-cases do use it.
90 * Available payment processors.
92 * As part of trying to consolidate various payment pages we store processors here & have functions
93 * at this level to manage them.
96 * An array of payment processor details with objects loaded in the 'object' field.
98 protected $_paymentProcessors;
101 * Available payment processors (IDS).
103 * As part of trying to consolidate various payment pages we store processors here & have functions
104 * at this level to manage them. An alternative would be to have a separate Form that is inherited
105 * by all forms that allow payment processing.
108 * An array of the IDS available on this form.
110 public $_paymentProcessorIDs;
113 * Default or selected processor id.
115 * As part of trying to consolidate various payment pages we store processors here & have functions
116 * at this level to manage them. An alternative would be to have a separate Form that is inherited
117 * by all forms that allow payment processing.
121 protected $_paymentProcessorID;
124 * Is pay later enabled for the form.
126 * As part of trying to consolidate various payment pages we store processors here & have functions
127 * at this level to manage them. An alternative would be to have a separate Form that is inherited
128 * by all forms that allow payment processing.
132 protected $_is_pay_later_enabled;
135 * The renderer used for this form
139 protected $_renderer;
142 * An array to hold a list of datefields on the form
143 * so that they can be converted to ISO in a consistent manner
147 * e.g on a form declare $_dateFields = array(
148 * 'receive_date' => array('default' => 'now'),
150 * then in postProcess call $this->convertDateFieldsToMySQL($formValues)
151 * to have the time field re-incorporated into the field & 'now' set if
152 * no value has been passed in
154 protected $_dateFields = [];
157 * Cache the smarty template for efficiency reasons
159 * @var CRM_Core_Smarty
161 static protected $_template;
164 * Indicate if this form should warn users of unsaved changes
167 protected $unsavedChangesWarn;
170 * What to return to the client if in ajax mode (snippet=json)
174 public $ajaxResponse = [];
177 * Url path used to reach this page
181 public $urlPath = [];
184 * Context of the form being loaded.
195 public $submitOnce = FALSE;
200 public function getContext() {
201 return $this->context
;
205 * Set context variable.
207 public function setContext() {
208 $this->context
= CRM_Utils_Request
::retrieve('context', 'Alphanumeric', $this);
212 * @var CRM_Core_Controller
217 * Constants for attributes for various form elements
218 * attempt to standardize on the number of variations that we
219 * use of the below form elements
223 const ATTR_SPACING
= ' ';
226 * All checkboxes are defined with a common prefix. This allows us to
227 * have the same javascript to check / clear all the checkboxes etc
228 * If u have multiple groups of checkboxes, you will need to give them different
229 * ids to avoid potential name collision
233 const CB_PREFIX
= 'mark_x_', CB_PREFIY
= 'mark_y_', CB_PREFIZ
= 'mark_z_', CB_PREFIX_LEN
= 7;
237 * @internal to keep track of chain-select fields
239 private $_chainSelectFields = [];
242 * Extra input types we support via the "add" method
245 public static $html5Types = [
253 * Constructor for the basic form page.
255 * We should not use QuickForm directly. This class provides a lot
256 * of default convenient functions, rules and buttons
258 * @param object $state
259 * State associated with this form.
260 * @param \const|\enum|int $action The mode the form is operating in (None/Create/View/Update/Delete)
261 * @param string $method
262 * The type of http method used (GET/POST).
263 * @param string $name
264 * The name of the form if different from class name.
266 * @return \CRM_Core_Form
268 public function __construct(
270 $action = CRM_Core_Action
::NONE
,
276 $this->_name
= $name;
279 // CRM-15153 - FIXME this name translates to a DOM id and is not always unique!
280 $this->_name
= CRM_Utils_String
::getClassName(CRM_Utils_System
::getClassName($this));
283 parent
::__construct($this->_name
, $method);
285 $this->_state
=& $state;
287 $this->_state
->setName($this->_name
);
289 $this->_action
= (int) $action;
291 $this->registerRules();
293 // let the constructor initialize this, should happen only once
294 if (!isset(self
::$_template)) {
295 self
::$_template = CRM_Core_Smarty
::singleton();
297 // Workaround for CRM-15153 - give each form a reasonably unique css class
298 $this->addClass(CRM_Utils_System
::getClassName($this));
300 $this->assign('snippet', CRM_Utils_Array
::value('snippet', $_GET));
301 $this->setTranslatedFields();
305 * Set translated fields.
307 * This function is called from the class constructor, allowing us to set
308 * fields on the class that can't be set as properties due to need for
309 * translation or other non-input specific handling.
311 protected function setTranslatedFields() {}
314 * Add one or more css classes to the form.
316 * @param string $className
318 public function addClass($className) {
319 $classes = $this->getAttribute('class');
320 $this->setAttribute('class', ($classes ?
"$classes " : '') . $className);
324 * Register all the standard rules that most forms potentially use.
326 public function registerRules() {
356 foreach ($rules as $rule) {
357 $this->registerRule($rule, 'callback', $rule, 'CRM_Utils_Rule');
362 * Simple easy to use wrapper around addElement.
364 * Deal with simple validation rules.
366 * @param string $type
367 * @param string $name
368 * @param string $label
369 * @param string|array $attributes (options for select elements)
370 * @param bool $required
371 * @param array $extra
372 * (attributes for select elements).
373 * For datepicker elements this is consistent with the data
374 * from CRM_Utils_Date::getDatePickerExtra
376 * @return HTML_QuickForm_Element
377 * Could be an error object
379 public function &add(
380 $type, $name, $label = '',
381 $attributes = '', $required = FALSE, $extra = NULL
383 // Fudge some extra types that quickform doesn't support
385 if ($type == 'wysiwyg' ||
in_array($type, self
::$html5Types)) {
386 $attributes = ($attributes ?
$attributes : []) +
['class' => ''];
387 $attributes['class'] = ltrim($attributes['class'] . " crm-form-$type");
388 if ($type == 'wysiwyg' && isset($attributes['preset'])) {
389 $attributes['data-preset'] = $attributes['preset'];
390 unset($attributes['preset']);
392 $type = $type == 'wysiwyg' ?
'textarea' : 'text';
394 // Like select but accepts rich array data (with nesting, colors, icons, etc) as option list.
395 if ($inputType == 'select2') {
397 $options = $attributes;
398 $attributes = ($extra ?
$extra : []) +
['class' => ''];
399 $attributes['class'] = ltrim($attributes['class'] . " crm-select2 crm-form-select2");
400 $attributes['data-select-params'] = json_encode(['data' => $options, 'multiple' => !empty($attributes['multiple'])]);
401 unset($attributes['multiple']);
404 // @see http://wiki.civicrm.org/confluence/display/CRMDOC/crmDatepicker
405 if ($type == 'datepicker') {
406 $attributes = ($attributes ?
$attributes : []);
407 $attributes['data-crm-datepicker'] = json_encode((array) $extra);
408 if (!empty($attributes['aria-label']) ||
$label) {
409 $attributes['aria-label'] = CRM_Utils_Array
::value('aria-label', $attributes, $label);
413 if ($type == 'select' && is_array($extra)) {
414 // Normalize this property
415 if (!empty($extra['multiple'])) {
416 $extra['multiple'] = 'multiple';
419 unset($extra['multiple']);
421 unset($extra['size'], $extra['maxlength']);
422 // Add placeholder option for select
423 if (isset($extra['placeholder'])) {
424 if ($extra['placeholder'] === TRUE) {
425 $extra['placeholder'] = $required ?
ts('- select -') : ts('- none -');
427 if (($extra['placeholder'] ||
$extra['placeholder'] === '') && empty($extra['multiple']) && is_array($attributes) && !isset($attributes[''])) {
428 $attributes = ['' => $extra['placeholder']] +
$attributes;
432 $element = $this->addElement($type, $name, $label, $attributes, $extra);
433 if (HTML_QuickForm
::isError($element)) {
434 CRM_Core_Error
::fatal(HTML_QuickForm
::errorMessage($element));
437 if ($inputType == 'color') {
438 $this->addRule($name, ts('%1 must contain a color value e.g. #ffffff.', [1 => $label]), 'regex', '/#[0-9a-fA-F]{6}/');
442 if ($type == 'file') {
443 $error = $this->addRule($name, ts('%1 is a required field.', [1 => $label]), 'uploadedfile');
446 $error = $this->addRule($name, ts('%1 is a required field.', [1 => $label]), 'required');
448 if (HTML_QuickForm
::isError($error)) {
449 CRM_Core_Error
::fatal(HTML_QuickForm
::errorMessage($element));
453 // Add context for the editing of option groups
454 if (isset($extra['option_context'])) {
455 $context = json_encode($extra['option_context']);
456 $element->setAttribute('data-option-edit-context', $context);
465 * This is called before buildForm. Any pre-processing that
466 * needs to be done for buildForm should be done here.
468 * This is a virtual function and should be redefined if needed.
470 public function preProcess() {
474 * Called after the form is validated.
476 * Any processing of form state etc should be done in this function.
477 * Typically all processing associated with a form should be done
478 * here and relevant state should be stored in the session
480 * This is a virtual function and should be redefined if needed
482 public function postProcess() {
486 * Main process wrapper.
488 * Implemented so that we can call all the hook functions.
490 * @param bool $allowAjax
491 * FIXME: This feels kind of hackish, ideally we would take the json-related code from this function.
492 * and bury it deeper down in the controller
494 public function mainProcess($allowAjax = TRUE) {
495 $this->postProcess();
496 $this->postProcessHook();
498 // Respond with JSON if in AJAX context (also support legacy value '6')
499 if ($allowAjax && !empty($_REQUEST['snippet']) && in_array($_REQUEST['snippet'], [
500 CRM_Core_Smarty
::PRINT_JSON
,
503 $this->ajaxResponse
['buttonName'] = str_replace('_qf_' . $this->getAttribute('id') . '_', '', $this->controller
->getButtonName());
504 $this->ajaxResponse
['action'] = $this->_action
;
505 if (isset($this->_id
) ||
isset($this->id
)) {
506 $this->ajaxResponse
['id'] = isset($this->id
) ?
$this->id
: $this->_id
;
508 CRM_Core_Page_AJAX
::returnJsonResponse($this->ajaxResponse
);
513 * The postProcess hook is typically called by the framework.
515 * However in a few cases, the form exits or redirects early in which
516 * case it needs to call this function so other modules can do the needful
517 * Calling this function directly should be avoided if possible. In general a
518 * better way is to do setUserContext so the framework does the redirect
520 public function postProcessHook() {
521 CRM_Utils_Hook
::postProcess(get_class($this), $this);
525 * This virtual function is used to build the form.
527 * It replaces the buildForm associated with QuickForm_Page. This allows us to put
528 * preProcess in front of the actual form building routine
530 public function buildQuickForm() {
534 * This virtual function is used to set the default values of various form elements.
537 * reference to the array of default values
539 public function setDefaultValues() {
544 * This is a virtual function that adds group and global rules to the form.
546 * Keeping it distinct from the form to keep code small
547 * and localized in the form building code
549 public function addRules() {
553 * Performs the server side validation.
556 * true if no error found
557 * @throws HTML_QuickForm_Error
559 public function validate() {
560 $error = parent
::validate();
562 $this->validateChainSelectFields();
566 CRM_Utils_Hook
::validateForm(
568 $this->_submitValues
,
574 if (!empty($hookErrors)) {
575 $this->_errors +
= $hookErrors;
578 return (0 == count($this->_errors
));
582 * Core function that builds the form.
584 * We redefine this function here and expect all CRM forms to build their form in the function
587 public function buildForm() {
588 $this->_formBuilt
= TRUE;
592 CRM_Utils_Hook
::preProcess(get_class($this), $this);
594 $this->assign('translatePermission', CRM_Core_Permission
::check('translate CiviCRM'));
597 $this->controller
->_key
&&
598 $this->controller
->_generateQFKey
600 $this->addElement('hidden', 'qfKey', $this->controller
->_key
);
601 $this->assign('qfKey', $this->controller
->_key
);
605 // _generateQFKey suppresses the qfKey generation on form snippets that
606 // are part of other forms, hence we use that to avoid adding entryURL
607 if ($this->controller
->_generateQFKey
&& $this->controller
->_entryURL
) {
608 $this->addElement('hidden', 'entryURL', $this->controller
->_entryURL
);
611 $this->buildQuickForm();
613 $defaults = $this->setDefaultValues();
614 unset($defaults['qfKey']);
616 if (!empty($defaults)) {
617 $this->setDefaults($defaults);
620 // call the form hook
621 // also call the hook function so any modules can set their own custom defaults
622 // the user can do both the form and set default values with this hook
623 CRM_Utils_Hook
::buildForm(get_class($this), $this);
627 //Set html data-attribute to enable warning user of unsaved changes
628 if ($this->unsavedChangesWarn
=== TRUE
629 ||
(!isset($this->unsavedChangesWarn
)
630 && ($this->_action
& CRM_Core_Action
::ADD ||
$this->_action
& CRM_Core_Action
::UPDATE
)
633 $this->setAttribute('data-warn-changes', 'true');
636 if ($this->submitOnce
) {
637 $this->setAttribute('data-submit-once', 'true');
642 * Add default Next / Back buttons.
644 * @param array $params
645 * Array of associative arrays in the order in which the buttons should be
646 * displayed. The associate array has 3 fields: 'type', 'name' and 'isDefault'
647 * The base form class will define a bunch of static arrays for commonly used
650 public function addButtons($params) {
651 $prevnext = $spacing = [];
652 foreach ($params as $button) {
653 if (!empty($button['submitOnce'])) {
654 $this->submitOnce
= TRUE;
657 $attrs = ['class' => 'crm-form-submit'] +
(array) CRM_Utils_Array
::value('js', $button);
659 if (!empty($button['class'])) {
660 $attrs['class'] .= ' ' . $button['class'];
663 if (!empty($button['isDefault'])) {
664 $attrs['class'] .= ' default';
667 if (in_array($button['type'], ['upload', 'next', 'submit', 'done', 'process', 'refresh'])) {
668 $attrs['class'] .= ' validate';
669 $defaultIcon = 'fa-check';
672 $attrs['class'] .= ' cancel';
673 $defaultIcon = $button['type'] == 'back' ?
'fa-chevron-left' : 'fa-times';
676 if ($button['type'] === 'reset') {
677 $prevnext[] = $this->createElement($button['type'], 'reset', $button['name'], $attrs);
680 if (!empty($button['subName'])) {
681 if ($button['subName'] == 'new') {
682 $defaultIcon = 'fa-plus-circle';
684 if ($button['subName'] == 'done') {
685 $defaultIcon = 'fa-check-circle';
687 if ($button['subName'] == 'next') {
688 $defaultIcon = 'fa-chevron-right';
692 if (in_array($button['type'], ['next', 'upload', 'done']) && $button['name'] === ts('Save')) {
693 $attrs['accesskey'] = 'S';
695 $icon = CRM_Utils_Array
::value('icon', $button, $defaultIcon);
697 $attrs['crm-icon'] = $icon;
699 $buttonName = $this->getButtonName($button['type'], CRM_Utils_Array
::value('subName', $button));
700 $prevnext[] = $this->createElement('submit', $buttonName, $button['name'], $attrs);
702 if (!empty($button['isDefault'])) {
703 $this->setDefaultAction($button['type']);
706 // if button type is upload, set the enctype
707 if ($button['type'] == 'upload') {
708 $this->updateAttributes(['enctype' => 'multipart/form-data']);
709 $this->setMaxFileSize();
712 // hack - addGroup uses an array to express variable spacing, read from the last element
713 $spacing[] = CRM_Utils_Array
::value('spacing', $button, self
::ATTR_SPACING
);
715 $this->addGroup($prevnext, 'buttons', '', $spacing, FALSE);
719 * Getter function for Name.
723 public function getName() {
728 * Getter function for State.
732 public function &getState() {
733 return $this->_state
;
737 * Getter function for StateType.
741 public function getStateType() {
742 return $this->_state
->getType();
746 * Getter function for title.
748 * Should be over-ridden by derived class.
752 public function getTitle() {
753 return $this->_title ?
$this->_title
: ts('ERROR: Title is not Set');
757 * Setter function for title.
759 * @param string $title
760 * The title of the form.
762 public function setTitle($title) {
763 $this->_title
= $title;
764 CRM_Utils_System
::setTitle($title);
768 * Assign billing type id to bltID.
770 * @throws CRM_Core_Exception
772 public function assignBillingType() {
773 $this->_bltID
= CRM_Core_BAO_LocationType
::getBilling();
774 $this->set('bltID', $this->_bltID
);
775 $this->assign('bltID', $this->_bltID
);
781 public function getPaymentProcessorID() {
782 return $this->_paymentProcessorID
;
786 * This if a front end form function for setting the payment processor.
788 * It would be good to sync it with the back-end function on abstractEditPayment & use one everywhere.
790 * @param bool $isPayLaterEnabled
792 * @throws \CRM_Core_Exception
794 protected function assignPaymentProcessor($isPayLaterEnabled) {
795 $this->_paymentProcessors
= CRM_Financial_BAO_PaymentProcessor
::getPaymentProcessors(
796 [ucfirst($this->_mode
) . 'Mode'],
797 $this->_paymentProcessorIDs
799 if ($isPayLaterEnabled) {
800 $this->_paymentProcessors
[0] = CRM_Financial_BAO_PaymentProcessor
::getPayment(0);
803 if (!empty($this->_paymentProcessors
)) {
804 foreach ($this->_paymentProcessors
as $paymentProcessorID => $paymentProcessorDetail) {
805 if (empty($this->_paymentProcessor
) && $paymentProcessorDetail['is_default'] == 1 ||
(count($this->_paymentProcessors
) == 1)
807 $this->_paymentProcessor
= $paymentProcessorDetail;
808 $this->assign('paymentProcessor', $this->_paymentProcessor
);
809 // Setting this is a bit of a legacy overhang.
810 $this->_paymentObject
= $paymentProcessorDetail['object'];
813 // It's not clear why we set this on the form.
814 $this->set('paymentProcessors', $this->_paymentProcessors
);
817 throw new CRM_Core_Exception(ts('A payment processor configured for this page might be disabled (contact the site administrator for assistance).'));
823 * Format the fields in $this->_params for the payment processor.
825 * In order to pass fields to the payment processor in a consistent way we add some renamed
828 * @param array $fields
832 protected function formatParamsForPaymentProcessor($fields) {
833 $this->_params
= $this->prepareParamsForPaymentProcessor($this->_params
);
834 $fields = array_merge($fields, ['first_name' => 1, 'middle_name' => 1, 'last_name' => 1]);
839 * Format the fields in $params for the payment processor.
841 * In order to pass fields to the payment processor in a consistent way we add some renamed
844 * @param array $params Payment processor params
846 * @return array $params
848 protected function prepareParamsForPaymentProcessor($params) {
849 // also add location name to the array
850 $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);
851 $params["address_name-{$this->_bltID}"] = trim($params["address_name-{$this->_bltID}"]);
852 // Add additional parameters that the payment processors are used to receiving.
853 if (!empty($params["billing_state_province_id-{$this->_bltID}"])) {
854 $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}"]);
856 if (!empty($params["billing_country_id-{$this->_bltID}"])) {
857 $params['country'] = $params["country-{$this->_bltID}"] = $params["billing_country-{$this->_bltID}"] = CRM_Core_PseudoConstant
::countryIsoCode($params["billing_country_id-{$this->_bltID}"]);
860 list($hasAddressField, $addressParams) = CRM_Contribute_BAO_Contribution
::getPaymentProcessorReadyAddressParams($params, $this->_bltID
);
861 if ($hasAddressField) {
862 $params = array_merge($params, $addressParams);
865 // How does this relate to similar code in CRM_Contact_BAO_Contact::addBillingNameFieldsIfOtherwiseNotSet()?
866 $nameFields = ['first_name', 'middle_name', 'last_name'];
867 foreach ($nameFields as $name) {
868 if (array_key_exists("billing_$name", $params)) {
869 $params[$name] = $params["billing_{$name}"];
870 $params['preserveDBName'] = TRUE;
874 // For legacy reasons we set these creditcard expiry fields if present
875 if (isset($params['credit_card_exp_date'])) {
876 $params['year'] = CRM_Core_Payment_Form
::getCreditCardExpirationYear($params);
877 $params['month'] = CRM_Core_Payment_Form
::getCreditCardExpirationMonth($params);
880 // Assign IP address parameter
881 $params['ip_address'] = CRM_Utils_System
::ipAddress();
887 * Handle Payment Processor switching for contribution and event registration forms.
889 * This function is shared between contribution & event forms & this is their common class.
891 * However, this should be seen as an in-progress refactor, the end goal being to also align the
892 * backoffice forms that action payments.
894 * This function overlaps assignPaymentProcessor, in a bad way.
896 protected function preProcessPaymentOptions() {
897 $this->_paymentProcessorID
= NULL;
898 if ($this->_paymentProcessors
) {
899 if (!empty($this->_submitValues
)) {
900 $this->_paymentProcessorID
= CRM_Utils_Array
::value('payment_processor_id', $this->_submitValues
);
901 $this->_paymentProcessor
= CRM_Utils_Array
::value($this->_paymentProcessorID
, $this->_paymentProcessors
);
902 $this->set('type', $this->_paymentProcessorID
);
903 $this->set('mode', $this->_mode
);
904 $this->set('paymentProcessor', $this->_paymentProcessor
);
906 // Set default payment processor
908 foreach ($this->_paymentProcessors
as $values) {
909 if (!empty($values['is_default']) ||
count($this->_paymentProcessors
) == 1) {
910 $this->_paymentProcessorID
= $values['id'];
915 if ($this->_paymentProcessorID
916 ||
(isset($this->_submitValues
['payment_processor_id']) && $this->_submitValues
['payment_processor_id'] == 0)
918 CRM_Core_Payment_ProcessorForm
::preProcess($this);
921 $this->_paymentProcessor
= [];
925 // We save the fact that the profile 'billing' is required on the payment form.
926 // Currently pay-later is the only 'processor' that takes notice of this - but ideally
927 // 1) it would be possible to select the minimum_billing_profile_id for the contribution form
928 // 2) that profile_id would be set on the payment processor
929 // 3) the payment processor would return a billing form that combines these user-configured
930 // minimums with the payment processor minimums. This would lead to fields like 'postal_code'
931 // only being on the form if either the admin has configured it as wanted or the processor
933 $this->assign('billing_profile_id', (CRM_Utils_Array
::value('is_billing_required', $this->_values
) ?
'billing' : ''));
937 * Handle pre approval for processors.
939 * This fits with the flow where a pre-approval is done and then confirmed in the next stage when confirm is hit.
941 * This function is shared between contribution & event forms & this is their common class.
943 * However, this should be seen as an in-progress refactor, the end goal being to also align the
944 * backoffice forms that action payments.
946 * @param array $params
948 protected function handlePreApproval(&$params) {
950 $payment = Civi\Payment\System
::singleton()->getByProcessor($this->_paymentProcessor
);
951 $params['component'] = 'contribute';
952 $result = $payment->doPreApproval($params);
953 if (empty($result)) {
954 // This could happen, for example, when paypal looks at the button value & decides it is not paypal express.
958 catch (\Civi\Payment\Exception\PaymentProcessorException
$e) {
959 CRM_Core_Error
::statusBounce(ts('Payment approval failed with message :') . $e->getMessage(), $payment->getCancelUrl($params['qfKey'], CRM_Utils_Array
::value('participant_id', $params)));
962 $this->set('pre_approval_parameters', $result['pre_approval_parameters']);
963 if (!empty($result['redirect_url'])) {
964 CRM_Utils_System
::redirect($result['redirect_url']);
969 * Setter function for options.
971 * @param mixed $options
973 public function setOptions($options) {
974 $this->_options
= $options;
978 * Render form and return contents.
982 public function toSmarty() {
983 $this->preProcessChainSelectFields();
984 $renderer = $this->getRenderer();
985 $this->accept($renderer);
986 $content = $renderer->toArray();
987 $content['formName'] = $this->getName();
989 $content['formClass'] = CRM_Utils_System
::getClassName($this);
994 * Getter function for renderer.
996 * If renderer is not set create one and initialize it.
1000 public function &getRenderer() {
1001 if (!isset($this->_renderer
)) {
1002 $this->_renderer
= CRM_Core_Form_Renderer
::singleton();
1004 return $this->_renderer
;
1008 * Use the form name to create the tpl file name.
1012 public function getTemplateFileName() {
1013 $ext = CRM_Extension_System
::singleton()->getMapper();
1014 if ($ext->isExtensionClass(CRM_Utils_System
::getClassName($this))) {
1015 $filename = $ext->getTemplateName(CRM_Utils_System
::getClassName($this));
1016 $tplname = $ext->getTemplatePath(CRM_Utils_System
::getClassName($this)) . DIRECTORY_SEPARATOR
. $filename;
1020 CRM_Utils_System
::getClassName($this),
1022 '_' => DIRECTORY_SEPARATOR
,
1023 '\\' => DIRECTORY_SEPARATOR
,
1031 * A wrapper for getTemplateFileName.
1033 * This includes calling the hook to prevent us from having to copy & paste the logic of calling the hook.
1035 public function getHookedTemplateFileName() {
1036 $pageTemplateFile = $this->getTemplateFileName();
1037 CRM_Utils_Hook
::alterTemplateFile(get_class($this), $this, 'page', $pageTemplateFile);
1038 return $pageTemplateFile;
1042 * Default extra tpl file basically just replaces .tpl with .extra.tpl.
1044 * i.e. we do not override.
1048 public function overrideExtraTemplateFileName() {
1053 * Error reporting mechanism.
1055 * @param string $message
1059 * @param CRM_Core_DAO $dao
1060 * A data access object on which we perform a rollback if non - empty.
1062 public function error($message, $code = NULL, $dao = NULL) {
1064 $dao->query('ROLLBACK');
1067 $error = CRM_Core_Error
::singleton();
1069 $error->push($code, $message);
1073 * Store the variable with the value in the form scope.
1075 * @param string $name
1076 * Name of the variable.
1077 * @param mixed $value
1078 * Value of the variable.
1080 public function set($name, $value) {
1081 $this->controller
->set($name, $value);
1085 * Get the variable from the form scope.
1087 * @param string $name
1088 * Name of the variable
1092 public function get($name) {
1093 return $this->controller
->get($name);
1097 * Getter for action.
1101 public function getAction() {
1102 return $this->_action
;
1106 * Setter for action.
1108 * @param int $action
1109 * The mode we want to set the form.
1111 public function setAction($action) {
1112 $this->_action
= $action;
1116 * Assign value to name in template.
1118 * @param string $var
1120 * @param mixed $value
1121 * Value of variable.
1123 public function assign($var, $value = NULL) {
1124 self
::$_template->assign($var, $value);
1128 * Assign value to name in template by reference.
1130 * @param string $var
1132 * @param mixed $value
1133 * Value of variable.
1135 public function assign_by_ref($var, &$value) {
1136 self
::$_template->assign_by_ref($var, $value);
1140 * Appends values to template variables.
1142 * @param array|string $tpl_var the template variable name(s)
1143 * @param mixed $value
1144 * The value to append.
1145 * @param bool $merge
1147 public function append($tpl_var, $value = NULL, $merge = FALSE) {
1148 self
::$_template->append($tpl_var, $value, $merge);
1152 * Returns an array containing template variables.
1154 * @param string $name
1158 public function get_template_vars($name = NULL) {
1159 return self
::$_template->get_template_vars($name);
1163 * @param string $name
1166 * @param array $attributes
1167 * @param null $separator
1168 * @param bool $required
1170 * @return HTML_QuickForm_group
1172 public function &addRadio($name, $title, $values, $attributes = [], $separator = NULL, $required = FALSE) {
1174 $attributes = $attributes ?
$attributes : [];
1175 $allowClear = !empty($attributes['allowClear']);
1176 unset($attributes['allowClear']);
1177 $attributes['id_suffix'] = $name;
1178 foreach ($values as $key => $var) {
1179 $options[] = $this->createElement('radio', NULL, NULL, $var, $key, $attributes);
1181 $group = $this->addGroup($options, $name, $title, $separator);
1183 $optionEditKey = 'data-option-edit-path';
1184 if (!empty($attributes[$optionEditKey])) {
1185 $group->setAttribute($optionEditKey, $attributes[$optionEditKey]);
1189 $this->addRule($name, ts('%1 is a required field.', [1 => $title]), 'required');
1192 $group->setAttribute('allowClear', TRUE);
1200 * @param bool $allowClear
1201 * @param null $required
1202 * @param array $attributes
1204 public function addYesNo($id, $title, $allowClear = FALSE, $required = NULL, $attributes = []) {
1205 $attributes +
= ['id_suffix' => $id];
1207 $choice[] = $this->createElement('radio', NULL, '11', ts('Yes'), '1', $attributes);
1208 $choice[] = $this->createElement('radio', NULL, '11', ts('No'), '0', $attributes);
1210 $group = $this->addGroup($choice, $id, $title);
1212 $group->setAttribute('allowClear', TRUE);
1215 $this->addRule($id, ts('%1 is a required field.', [1 => $title]), 'required');
1223 * @param null $other
1224 * @param null $attributes
1225 * @param null $required
1226 * @param null $javascriptMethod
1227 * @param string $separator
1228 * @param bool $flipValues
1230 public function addCheckBox(
1231 $id, $title, $values, $other = NULL,
1232 $attributes = NULL, $required = NULL,
1233 $javascriptMethod = NULL,
1234 $separator = '<br />', $flipValues = FALSE
1238 if ($javascriptMethod) {
1239 foreach ($values as $key => $var) {
1241 $options[] = $this->createElement('checkbox', $var, NULL, $key, $javascriptMethod, $attributes);
1244 $options[] = $this->createElement('checkbox', $key, NULL, $var, $javascriptMethod, $attributes);
1249 foreach ($values as $key => $var) {
1251 $options[] = $this->createElement('checkbox', $var, NULL, $key, $attributes);
1254 $options[] = $this->createElement('checkbox', $key, NULL, $var, $attributes);
1259 $group = $this->addGroup($options, $id, $title, $separator);
1260 $optionEditKey = 'data-option-edit-path';
1261 if (!empty($attributes[$optionEditKey])) {
1262 $group->setAttribute($optionEditKey, $attributes[$optionEditKey]);
1266 $this->addElement('text', $id . '_other', ts('Other'), $attributes[$id . '_other']);
1271 ts('%1 is a required field.', [1 => $title]),
1277 public function resetValues() {
1278 $data = $this->controller
->container();
1279 $data['values'][$this->_name
] = [];
1283 * Simple shell that derived classes can call to add buttons to
1284 * the form with a customized title for the main Submit
1286 * @param string $title
1287 * Title of the main button.
1288 * @param string $nextType
1289 * Button type for the form after processing.
1290 * @param string $backType
1291 * @param bool|string $submitOnce
1293 public function addDefaultButtons($title, $nextType = 'next', $backType = 'back', $submitOnce = FALSE) {
1295 if ($backType != NULL) {
1297 'type' => $backType,
1298 'name' => ts('Previous'),
1301 if ($nextType != NULL) {
1303 'type' => $nextType,
1305 'isDefault' => TRUE,
1308 $this->submitOnce
= TRUE;
1310 $buttons[] = $nextButton;
1312 $this->addButtons($buttons);
1316 * @param string $name
1317 * @param string $from
1319 * @param string $label
1320 * @param string $dateFormat
1321 * @param bool $required
1322 * @param bool $displayTime
1324 public function addDateRange($name, $from = '_from', $to = '_to', $label = 'From:', $dateFormat = 'searchDate', $required = FALSE, $displayTime = FALSE) {
1326 $this->addDateTime($name . $from, $label, $required, ['formatType' => $dateFormat]);
1327 $this->addDateTime($name . $to, ts('To:'), $required, ['formatType' => $dateFormat]);
1330 $this->addDate($name . $from, $label, $required, ['formatType' => $dateFormat]);
1331 $this->addDate($name . $to, ts('To:'), $required, ['formatType' => $dateFormat]);
1336 * Add a search for a range using date picker fields.
1338 * @param string $fieldName
1339 * @param string $label
1340 * @param bool $isDateTime
1341 * Is this a date-time field (not just date).
1342 * @param bool $required
1343 * @param string $fromLabel
1344 * @param string $toLabel
1346 public function addDatePickerRange($fieldName, $label, $isDateTime = FALSE, $required = FALSE, $fromLabel = 'From', $toLabel = 'To') {
1349 '' => ts('- any -'),
1350 0 => ts('Choose Date Range'),
1351 ] + CRM_Core_OptionGroup
::values('relative_date_filters');
1353 $this->add('select',
1354 "{$fieldName}_relative",
1358 ['class' => 'crm-select2']
1360 $attributes = ['format' => 'searchDate'];
1361 $extra = ['time' => $isDateTime];
1362 $this->add('datepicker', $fieldName . '_low', ts($fromLabel), $attributes, $required, $extra);
1363 $this->add('datepicker', $fieldName . '_high', ts($toLabel), $attributes, $required, $extra);
1367 * Based on form action, return a string representing the api action.
1368 * Used by addField method.
1372 protected function getApiAction() {
1373 $action = $this->getAction();
1374 if ($action & (CRM_Core_Action
::UPDATE + CRM_Core_Action
::ADD
)) {
1377 if ($action & (CRM_Core_Action
::VIEW + CRM_Core_Action
::BROWSE + CRM_Core_Action
::BASIC + CRM_Core_Action
::ADVANCED + CRM_Core_Action
::PREVIEW
)) {
1380 if ($action & (CRM_Core_Action
::DELETE
)) {
1383 // If you get this exception try adding more cases above.
1384 throw new Exception("Cannot determine api action for " . get_class($this) . '.' . 'CRM_Core_Action "' . CRM_Core_Action
::description($action) . '" not recognized.');
1388 * Classes extending CRM_Core_Form should implement this method.
1391 public function getDefaultEntity() {
1392 throw new Exception("Cannot determine default entity. " . get_class($this) . " should implement getDefaultEntity().");
1396 * Classes extending CRM_Core_Form should implement this method.
1398 * TODO: Merge with CRM_Core_DAO::buildOptionsContext($context) and add validation.
1401 public function getDefaultContext() {
1402 throw new Exception("Cannot determine default context. " . get_class($this) . " should implement getDefaultContext().");
1406 * Adds a select based on field metadata.
1407 * TODO: This could be even more generic and widget type (select in this case) could also be read from metadata
1408 * Perhaps a method like $form->bind($name) which would look up all metadata for named field
1410 * Field name to go on the form.
1411 * @param array $props
1412 * Mix of html attributes and special properties, namely.
1413 * - entity (api entity name, can usually be inferred automatically from the form class)
1414 * - field (field name - only needed if different from name used on the form)
1415 * - option_url - path to edit this option list - usually retrieved automatically - set to NULL to disable link
1416 * - placeholder - set to NULL to disable
1418 * - context - @see CRM_Core_DAO::buildOptionsContext
1419 * @param bool $required
1420 * @throws CRM_Core_Exception
1421 * @return HTML_QuickForm_Element
1423 public function addSelect($name, $props = [], $required = FALSE) {
1424 if (!isset($props['entity'])) {
1425 $props['entity'] = $this->getDefaultEntity();
1427 if (!isset($props['field'])) {
1428 $props['field'] = strrpos($name, '[') ?
rtrim(substr($name, 1 +
strrpos($name, '[')), ']') : $name;
1430 if (!isset($props['context'])) {
1432 $props['context'] = $this->getDefaultContext();
1434 // This is not a required param, so we'll ignore if this doesn't exist.
1435 catch (Exception
$e) {
1438 // Fetch options from the api unless passed explicitly
1439 if (isset($props['options'])) {
1440 $options = $props['options'];
1443 $info = civicrm_api3($props['entity'], 'getoptions', $props);
1444 $options = $info['values'];
1446 if (!array_key_exists('placeholder', $props)) {
1447 $props['placeholder'] = $required ?
ts('- select -') : CRM_Utils_Array
::value('context', $props) == 'search' ?
ts('- any -') : ts('- none -');
1449 // Handle custom field
1450 if (strpos($name, 'custom_') === 0 && is_numeric($name[7])) {
1451 list(, $id) = explode('_', $name);
1452 $label = isset($props['label']) ?
$props['label'] : CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomField', 'label', $id);
1453 $gid = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomField', 'option_group_id', $id);
1454 if (CRM_Utils_Array
::value('context', $props) != 'search') {
1455 $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);
1460 $info = civicrm_api3($props['entity'], 'getfields');
1461 foreach ($info['values'] as $uniqueName => $fieldSpec) {
1463 $uniqueName === $props['field'] ||
1464 CRM_Utils_Array
::value('name', $fieldSpec) === $props['field'] ||
1465 in_array($props['field'], CRM_Utils_Array
::value('api.aliases', $fieldSpec, []))
1470 $label = isset($props['label']) ?
$props['label'] : $fieldSpec['title'];
1471 if (CRM_Utils_Array
::value('context', $props) != 'search') {
1472 $props['data-option-edit-path'] = array_key_exists('option_url', $props) ?
$props['option_url'] : CRM_Core_PseudoConstant
::getOptionEditUrl($fieldSpec);
1475 $props['class'] = (isset($props['class']) ?
$props['class'] . ' ' : '') . "crm-select2";
1476 $props['data-api-entity'] = $props['entity'];
1477 $props['data-api-field'] = $props['field'];
1478 CRM_Utils_Array
::remove($props, 'label', 'entity', 'field', 'option_url', 'options', 'context');
1479 return $this->add('select', $name, $label, $options, $required, $props);
1483 * Adds a field based on metadata.
1486 * Field name to go on the form.
1487 * @param array $props
1488 * Mix of html attributes and special properties, namely.
1489 * - entity (api entity name, can usually be inferred automatically from the form class)
1490 * - name (field name - only needed if different from name used on the form)
1491 * - option_url - path to edit this option list - usually retrieved automatically - set to NULL to disable link
1492 * - placeholder - set to NULL to disable
1494 * - context - @see CRM_Core_DAO::buildOptionsContext
1495 * @param bool $required
1496 * @param bool $legacyDate
1497 * Temporary param to facilitate the conversion of fields to use the datepicker in
1498 * a controlled way. To convert the field the jcalendar code needs to be removed from the
1499 * tpl as well. That file is intended to be EOL.
1501 * @throws \CiviCRM_API3_Exception
1502 * @throws \Exception
1504 * HTML_QuickForm_Element
1507 public function addField($name, $props = [], $required = FALSE, $legacyDate = TRUE) {
1509 if (empty($props['context'])) {
1510 $props['context'] = $this->getDefaultContext();
1512 $context = $props['context'];
1514 if (empty($props['entity'])) {
1515 $props['entity'] = $this->getDefaultEntity();
1518 if (empty($props['name'])) {
1519 $props['name'] = strrpos($name, '[') ?
rtrim(substr($name, 1 +
strrpos($name, '[')), ']') : $name;
1522 if (empty($props['action'])) {
1523 $props['action'] = $this->getApiAction();
1526 // Handle custom fields
1527 if (strpos($name, 'custom_') === 0 && is_numeric($name[7])) {
1528 $fieldId = (int) substr($name, 7);
1529 return CRM_Core_BAO_CustomField
::addQuickFormElement($this, $name, $fieldId, $required, $context == 'search', CRM_Utils_Array
::value('label', $props));
1532 // Core field - get metadata.
1533 $fieldSpec = civicrm_api3($props['entity'], 'getfield', $props);
1534 $fieldSpec = $fieldSpec['values'];
1535 $fieldSpecLabel = isset($fieldSpec['html']['label']) ?
$fieldSpec['html']['label'] : CRM_Utils_Array
::value('title', $fieldSpec);
1536 $label = CRM_Utils_Array
::value('label', $props, $fieldSpecLabel);
1538 $widget = isset($props['type']) ?
$props['type'] : $fieldSpec['html']['type'];
1539 if ($widget == 'TextArea' && $context == 'search') {
1543 $isSelect = (in_array($widget, [
1552 // Fetch options from the api unless passed explicitly.
1553 if (isset($props['options'])) {
1554 $options = $props['options'];
1557 $options = isset($fieldSpec['options']) ?
$fieldSpec['options'] : NULL;
1559 if ($context == 'search') {
1560 $widget = $widget == 'Select2' ?
$widget : 'Select';
1561 $props['multiple'] = CRM_Utils_Array
::value('multiple', $props, TRUE);
1564 // Add data for popup link.
1565 $canEditOptions = CRM_Core_Permission
::check('administer CiviCRM');
1566 $hasOptionUrl = !empty($props['option_url']);
1567 $optionUrlKeyIsSet = array_key_exists('option_url', $props);
1568 $shouldAdd = $context !== 'search' && $isSelect && $canEditOptions;
1570 // Only add if key is not set, or if non-empty option url is provided
1571 if (($hasOptionUrl ||
!$optionUrlKeyIsSet) && $shouldAdd) {
1572 $optionUrl = $hasOptionUrl ?
$props['option_url'] :
1573 CRM_Core_PseudoConstant
::getOptionEditUrl($fieldSpec);
1574 $props['data-option-edit-path'] = $optionUrl;
1575 $props['data-api-entity'] = $props['entity'];
1576 $props['data-api-field'] = $props['name'];
1579 $props +
= CRM_Utils_Array
::value('html', $fieldSpec, []);
1580 CRM_Utils_Array
::remove($props, 'entity', 'name', 'context', 'label', 'action', 'type', 'option_url', 'options');
1582 // TODO: refactor switch statement, to separate methods.
1588 //TODO: Autodetect ranges
1589 $props['size'] = isset($props['size']) ?
$props['size'] : 60;
1590 return $this->add(strtolower($widget), $name, $label, $props, $required);
1593 return $this->add('hidden', $name, NULL, $props, $required);
1596 //Set default columns and rows for textarea.
1597 $props['rows'] = isset($props['rows']) ?
$props['rows'] : 4;
1598 $props['cols'] = isset($props['cols']) ?
$props['cols'] : 60;
1599 if (empty($props['maxlength']) && isset($fieldSpec['length'])) {
1600 $props['maxlength'] = $fieldSpec['length'];
1602 return $this->add('textarea', $name, $label, $props, $required);
1605 // This is a white list for fields that have been tested with
1606 // date picker. We should be able to remove the other
1608 //TODO: add range support
1609 //TODO: Add date formats
1610 //TODO: Add javascript template for dates.
1611 return $this->addDate($name, $label, $required, $props);
1614 $fieldSpec = CRM_Utils_Date
::addDateMetadataToField($fieldSpec, $fieldSpec);
1615 $attributes = ['format' => $fieldSpec['date_format']];
1616 return $this->add('datepicker', $name, $label, $attributes, $required, $fieldSpec['datepicker']['extra']);
1620 $separator = isset($props['separator']) ?
$props['separator'] : NULL;
1621 unset($props['separator']);
1622 if (!isset($props['allowClear'])) {
1623 $props['allowClear'] = !$required;
1625 return $this->addRadio($name, $label, $options, $props, $separator, $required);
1629 'required' => $required,
1631 'multiple' => $context == 'search',
1633 return $this->addChainSelect($name, $props);
1637 $props['class'] = CRM_Utils_Array
::value('class', $props, 'big') . ' crm-select2';
1638 if (!array_key_exists('placeholder', $props)) {
1639 $props['placeholder'] = $required ?
ts('- select -') : ($context == 'search' ?
ts('- any -') : ts('- none -'));
1641 // TODO: Add and/or option for fields that store multiple values
1642 return $this->add(strtolower($widget), $name, $label, $options, $required, $props);
1644 case 'CheckBoxGroup':
1645 return $this->addCheckBox($name, $label, array_flip($options), $required, $props);
1648 return $this->addRadio($name, $label, $options, $props, NULL, $required);
1651 if ($context === 'search') {
1652 $this->addYesNo($name, $label, TRUE, FALSE, $props);
1655 $text = isset($props['text']) ?
$props['text'] : NULL;
1656 unset($props['text']);
1657 return $this->addElement('checkbox', $name, $label, $text, $props);
1659 //add support for 'Advcheckbox' field
1661 $text = isset($props['text']) ?
$props['text'] : NULL;
1662 unset($props['text']);
1663 return $this->addElement('advcheckbox', $name, $label, $text, $props);
1666 // We should not build upload file in search mode.
1667 if ($context == 'search') {
1670 $file = $this->add('file', $name, $label, $props, $required);
1671 $this->addUploadElement($name);
1674 case 'RichTextEditor':
1675 return $this->add('wysiwyg', $name, $label, $props, $required);
1678 return $this->addEntityRef($name, $label, $props, $required);
1681 $props['size'] = isset($props['size']) ?
$props['size'] : 60;
1682 return $this->add('password', $name, $label, $props, $required);
1684 // Check datatypes of fields
1688 //case read only fields
1690 throw new Exception("Unsupported html-element " . $widget);
1695 * Add a widget for selecting/editing/creating/copying a profile form
1697 * @param string $name
1698 * HTML form-element name.
1699 * @param string $label
1701 * @param string $allowCoreTypes
1702 * Only present a UFGroup if its group_type includes a subset of $allowCoreTypes; e.g. 'Individual', 'Activity'.
1703 * @param string $allowSubTypes
1704 * Only present a UFGroup if its group_type is compatible with $allowSubypes.
1705 * @param array $entities
1706 * @param bool $default
1708 * @param string $usedFor
1710 public function addProfileSelector($name, $label, $allowCoreTypes, $allowSubTypes, $entities, $default = FALSE, $usedFor = NULL) {
1712 // FIXME: Instead of adhoc serialization, use a single json_encode()
1713 CRM_UF_Page_ProfileEditor
::registerProfileScripts();
1714 CRM_UF_Page_ProfileEditor
::registerSchemas(CRM_Utils_Array
::collect('entity_type', $entities));
1715 $this->add('text', $name, $label, [
1716 'class' => 'crm-profile-selector',
1717 // Note: client treats ';;' as equivalent to \0, and ';;' works better in HTML
1718 'data-group-type' => CRM_Core_BAO_UFGroup
::encodeGroupType($allowCoreTypes, $allowSubTypes, ';;'),
1719 'data-entities' => json_encode($entities),
1721 'data-default' => $default,
1722 'data-usedfor' => json_encode($usedFor),
1729 public function getRootTitle() {
1736 public function getCompleteTitle() {
1737 return $this->getRootTitle() . $this->getTitle();
1741 * @return CRM_Core_Smarty
1743 public static function &getTemplate() {
1744 return self
::$_template;
1748 * @param $elementName
1750 public function addUploadElement($elementName) {
1751 $uploadNames = $this->get('uploadNames');
1752 if (!$uploadNames) {
1755 if (is_array($elementName)) {
1756 foreach ($elementName as $name) {
1757 if (!in_array($name, $uploadNames)) {
1758 $uploadNames[] = $name;
1763 if (!in_array($elementName, $uploadNames)) {
1764 $uploadNames[] = $elementName;
1767 $this->set('uploadNames', $uploadNames);
1769 $config = CRM_Core_Config
::singleton();
1770 if (!empty($uploadNames)) {
1771 $this->controller
->addUploadAction($config->customFileUploadDir
, $uploadNames);
1780 public function getVar($name) {
1781 return isset($this->$name) ?
$this->$name : NULL;
1788 public function setVar($name, $value) {
1789 $this->$name = $value;
1796 * Use $this->add('datepicker', ...) instead.
1798 * @param string $name
1799 * Name of the element.
1800 * @param string $label
1801 * Label of the element.
1802 * @param bool $required
1804 * @param array $attributes
1807 public function addDate($name, $label, $required = FALSE, $attributes = NULL) {
1808 if (!empty($attributes['formatType'])) {
1809 // get actual format
1810 $params = ['name' => $attributes['formatType']];
1813 // cache date information
1815 $key = "dateFormat_" . str_replace(' ', '_', $attributes['formatType']);
1816 if (empty($dateFormat[$key])) {
1817 CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_PreferencesDate', $params, $values);
1818 $dateFormat[$key] = $values;
1821 $values = $dateFormat[$key];
1824 if ($values['date_format']) {
1825 $attributes['format'] = $values['date_format'];
1828 if (!empty($values['time_format'])) {
1829 $attributes['timeFormat'] = $values['time_format'];
1831 $attributes['startOffset'] = $values['start'];
1832 $attributes['endOffset'] = $values['end'];
1835 $config = CRM_Core_Config
::singleton();
1836 if (empty($attributes['format'])) {
1837 $attributes['format'] = $config->dateInputFormat
;
1840 if (!isset($attributes['startOffset'])) {
1841 $attributes['startOffset'] = 10;
1844 if (!isset($attributes['endOffset'])) {
1845 $attributes['endOffset'] = 10;
1848 $this->add('text', $name, $label, $attributes);
1850 if (!empty($attributes['addTime']) ||
!empty($attributes['timeFormat'])) {
1852 if (!isset($attributes['timeFormat'])) {
1853 $timeFormat = $config->timeInputFormat
;
1856 $timeFormat = $attributes['timeFormat'];
1859 // 1 - 12 hours and 2 - 24 hours, but for jquery widget it is 0 and 1 respectively
1861 $show24Hours = TRUE;
1862 if ($timeFormat == 1) {
1863 $show24Hours = FALSE;
1866 //CRM-6664 -we are having time element name
1867 //in either flat string or an array format.
1868 $elementName = $name . '_time';
1869 if (substr($name, -1) == ']') {
1870 $elementName = substr($name, 0, strlen($name) - 1) . '_time]';
1873 $this->add('text', $elementName, ts('Time'), ['timeFormat' => $show24Hours]);
1878 $this->addRule($name, ts('Please select %1', [1 => $label]), 'required');
1879 if (!empty($attributes['addTime']) && !empty($attributes['addTimeRequired'])) {
1880 $this->addRule($elementName, ts('Please enter a time.'), 'required');
1886 * Function that will add date and time.
1889 * Use $this->add('datepicker', ...) instead.
1891 * @param string $name
1892 * @param string $label
1893 * @param bool $required
1894 * @param null $attributes
1896 public function addDateTime($name, $label, $required = FALSE, $attributes = NULL) {
1897 $addTime = ['addTime' => TRUE];
1898 if (is_array($attributes)) {
1899 $attributes = array_merge($attributes, $addTime);
1902 $attributes = $addTime;
1905 $this->addDate($name, $label, $required, $attributes);
1909 * Add a currency and money element to the form.
1911 * @param string $name
1912 * @param string $label
1913 * @param bool $required
1914 * @param null $attributes
1915 * @param bool $addCurrency
1916 * @param string $currencyName
1917 * @param null $defaultCurrency
1918 * @param bool $freezeCurrency
1920 * @return \HTML_QuickForm_Element
1922 public function addMoney(
1927 $addCurrency = TRUE,
1928 $currencyName = 'currency',
1929 $defaultCurrency = NULL,
1930 $freezeCurrency = FALSE
1932 $element = $this->add('text', $name, $label, $attributes, $required);
1933 $this->addRule($name, ts('Please enter a valid amount.'), 'money');
1936 $ele = $this->addCurrency($currencyName, NULL, TRUE, $defaultCurrency, $freezeCurrency);
1943 * Add currency element to the form.
1945 * @param string $name
1946 * @param null $label
1947 * @param bool $required
1948 * @param string $defaultCurrency
1949 * @param bool $freezeCurrency
1950 * @param bool $setDefaultCurrency
1952 public function addCurrency(
1956 $defaultCurrency = NULL,
1957 $freezeCurrency = FALSE,
1958 $setDefaultCurrency = TRUE
1960 $currencies = CRM_Core_OptionGroup
::values('currencies_enabled');
1961 if (!empty($defaultCurrency) && !array_key_exists($defaultCurrency, $currencies)) {
1962 Civi
::log()->warning('addCurrency: Currency ' . $defaultCurrency . ' is disabled but still in use!');
1963 $currencies[$defaultCurrency] = $defaultCurrency;
1965 $options = ['class' => 'crm-select2 eight'];
1967 $currencies = ['' => ''] +
$currencies;
1968 $options['placeholder'] = ts('- none -');
1970 $ele = $this->add('select', $name, $label, $currencies, $required, $options);
1971 if ($freezeCurrency) {
1974 if (!$defaultCurrency) {
1975 $config = CRM_Core_Config
::singleton();
1976 $defaultCurrency = $config->defaultCurrency
;
1978 // In some case, setting currency field by default might override the default value
1979 // as encountered in CRM-20527 for batch data entry
1980 if ($setDefaultCurrency) {
1981 $this->setDefaults([$name => $defaultCurrency]);
1986 * Create a single or multiple entity ref field.
1987 * @param string $name
1988 * @param string $label
1989 * @param array $props
1990 * Mix of html and widget properties, including:.
1991 * - select - params to give to select2 widget
1992 * - entity - defaults to Contact
1993 * - create - can the user create a new entity on-the-fly?
1994 * Set to TRUE if entity is contact and you want the default profiles,
1995 * or pass in your own set of links. @see CRM_Campaign_BAO_Campaign::getEntityRefCreateLinks for format
1996 * note that permissions are checked automatically
1997 * - api - array of settings for the getlist api wrapper
1998 * note that it accepts a 'params' setting which will be passed to the underlying api
1999 * - placeholder - string
2001 * - class, etc. - other html properties
2002 * @param bool $required
2004 * @return HTML_QuickForm_Element
2006 public function addEntityRef($name, $label = '', $props = [], $required = FALSE) {
2007 // Default properties
2008 $props['api'] = CRM_Utils_Array
::value('api', $props, []);
2009 $props['entity'] = CRM_Utils_String
::convertStringToCamel(CRM_Utils_Array
::value('entity', $props, 'Contact'));
2010 $props['class'] = ltrim(CRM_Utils_Array
::value('class', $props, '') . ' crm-form-entityref');
2012 if (array_key_exists('create', $props) && empty($props['create'])) {
2013 unset($props['create']);
2016 $props['placeholder'] = CRM_Utils_Array
::value('placeholder', $props, $required ?
ts('- select %1 -', [1 => ts(str_replace('_', ' ', $props['entity']))]) : ts('- none -'));
2019 if (!empty($props['multiple'])) {
2020 $defaults['multiple'] = TRUE;
2022 $props['select'] = CRM_Utils_Array
::value('select', $props, []) +
$defaults;
2024 $this->formatReferenceFieldAttributes($props, get_class($this));
2025 return $this->add('text', $name, $label, $props, $required);
2029 * @param array $props
2030 * @param string $formName
2032 private function formatReferenceFieldAttributes(&$props, $formName) {
2033 CRM_Utils_Hook
::alterEntityRefParams($props, $formName);
2034 $props['data-select-params'] = json_encode($props['select']);
2035 $props['data-api-params'] = $props['api'] ?
json_encode($props['api']) : NULL;
2036 $props['data-api-entity'] = $props['entity'];
2037 if (!empty($props['create'])) {
2038 $props['data-create-links'] = json_encode($props['create']);
2040 CRM_Utils_Array
::remove($props, 'multiple', 'select', 'api', 'entity', 'create');
2044 * Convert all date fields within the params to mysql date ready for the
2045 * BAO layer. In this case fields are checked against the $_datefields defined for the form
2046 * and if time is defined it is incorporated
2048 * @param array $params
2049 * Input params from the form.
2051 * @todo it would probably be better to work on $this->_params than a passed array
2052 * @todo standardise the format which dates are passed to the BAO layer in & remove date
2055 public function convertDateFieldsToMySQL(&$params) {
2056 foreach ($this->_dateFields
as $fieldName => $specs) {
2057 if (!empty($params[$fieldName])) {
2058 $params[$fieldName] = CRM_Utils_Date
::isoToMysql(
2059 CRM_Utils_Date
::processDate(
2060 $params[$fieldName],
2061 CRM_Utils_Array
::value("{$fieldName}_time", $params), TRUE)
2065 if (isset($specs['default'])) {
2066 $params[$fieldName] = date('YmdHis', strtotime($specs['default']));
2073 * @param $elementName
2075 public function removeFileRequiredRules($elementName) {
2076 $this->_required
= array_diff($this->_required
, [$elementName]);
2077 if (isset($this->_rules
[$elementName])) {
2078 foreach ($this->_rules
[$elementName] as $index => $ruleInfo) {
2079 if ($ruleInfo['type'] == 'uploadedfile') {
2080 unset($this->_rules
[$elementName][$index]);
2083 if (empty($this->_rules
[$elementName])) {
2084 unset($this->_rules
[$elementName]);
2090 * Function that can be defined in Form to override or.
2091 * perform specific action on cancel action
2093 public function cancelAction() {
2097 * Helper function to verify that required fields have been filled.
2099 * Typically called within the scope of a FormRule function
2101 * @param array $fields
2102 * @param array $values
2103 * @param array $errors
2105 public static function validateMandatoryFields($fields, $values, &$errors) {
2106 foreach ($fields as $name => $fld) {
2107 if (!empty($fld['is_required']) && CRM_Utils_System
::isNull(CRM_Utils_Array
::value($name, $values))) {
2108 $errors[$name] = ts('%1 is a required field.', [1 => $fld['title']]);
2114 * Get contact if for a form object. Prioritise
2115 * - cid in URL if 0 (on behalf on someoneelse)
2116 * (@todo consider setting a variable if onbehalf for clarity of downstream 'if's
2117 * - logged in user id if it matches the one in the cid in the URL
2118 * - contact id validated from a checksum from a checksum
2119 * - cid from the url if the caller has ACL permission to view
2120 * - fallback is logged in user (or ? NULL if no logged in user) (@todo wouldn't 0 be more intuitive?)
2124 protected function setContactID() {
2125 $tempID = CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
2126 if (isset($this->_params
) && !empty($this->_params
['select_contact_id'])) {
2127 $tempID = $this->_params
['select_contact_id'];
2129 if (isset($this->_params
, $this->_params
[0]) && !empty($this->_params
[0]['select_contact_id'])) {
2130 // event form stores as an indexed array, contribution form not so much...
2131 $tempID = $this->_params
[0]['select_contact_id'];
2134 // force to ignore the authenticated user
2135 if ($tempID === '0' ||
$tempID === 0) {
2136 // we set the cid on the form so that this will be retained for the Confirm page
2137 // in the multi-page form & prevent us returning the $userID when this is called
2139 // we don't really need to set it when $tempID is set because the params have that stored
2140 $this->set('cid', 0);
2141 CRM_Core_Resources
::singleton()->addVars('coreForm', ['contact_id' => (int) $tempID]);
2142 return (int) $tempID;
2145 $userID = $this->getLoggedInUserContactID();
2147 if (!is_null($tempID) && $tempID === $userID) {
2148 CRM_Core_Resources
::singleton()->addVars('coreForm', ['contact_id' => (int) $tempID]);
2149 return (int) $userID;
2152 //check if this is a checksum authentication
2153 $userChecksum = CRM_Utils_Request
::retrieve('cs', 'String', $this);
2154 if ($userChecksum) {
2155 //check for anonymous user.
2156 $validUser = CRM_Contact_BAO_Contact_Utils
::validChecksum($tempID, $userChecksum);
2158 CRM_Core_Resources
::singleton()->addVars('coreForm', ['contact_id' => (int) $tempID]);
2159 CRM_Core_Resources
::singleton()->addVars('coreForm', ['checksum' => $userChecksum]);
2163 // check if user has permission, CRM-12062
2164 elseif ($tempID && CRM_Contact_BAO_Contact_Permission
::allow($tempID)) {
2165 CRM_Core_Resources
::singleton()->addVars('coreForm', ['contact_id' => (int) $tempID]);
2168 if (is_numeric($userID)) {
2169 CRM_Core_Resources
::singleton()->addVars('coreForm', ['contact_id' => (int) $userID]);
2171 return is_numeric($userID) ?
$userID : NULL;
2175 * Get the contact id that the form is being submitted for.
2179 public function getContactID() {
2180 return $this->setContactID();
2184 * Get the contact id of the logged in user.
2186 public function getLoggedInUserContactID() {
2187 // check if the user is logged in and has a contact ID
2188 $session = CRM_Core_Session
::singleton();
2189 return $session->get('userID');
2193 * Add autoselector field -if user has permission to view contacts
2194 * If adding this to a form you also need to add to the tpl e.g
2196 * {if !empty($selectable)}
2197 * <div class="crm-summary-row">
2198 * <div class="crm-label">{$form.select_contact.label}</div>
2199 * <div class="crm-content">
2200 * {$form.select_contact.html}
2205 * @param array $profiles
2206 * Ids of profiles that are on the form (to be autofilled).
2207 * @param array $autoCompleteField
2211 * - url (for ajax lookup)
2213 * @todo add data attributes so we can deal with multiple instances on a form
2215 public function addAutoSelector($profiles = [], $autoCompleteField = []) {
2216 $autoCompleteField = array_merge([
2217 'id_field' => 'select_contact_id',
2218 'placeholder' => ts('Select someone else ...'),
2219 'show_hide' => TRUE,
2220 'api' => ['params' => ['contact_type' => 'Individual']],
2221 ], $autoCompleteField);
2223 if ($this->canUseAjaxContactLookups()) {
2224 $this->assign('selectable', $autoCompleteField['id_field']);
2225 $this->addEntityRef($autoCompleteField['id_field'], NULL, [
2226 'placeholder' => $autoCompleteField['placeholder'],
2227 'api' => $autoCompleteField['api'],
2230 CRM_Core_Resources
::singleton()->addScriptFile('civicrm', 'js/AlternateContactSelector.js', 1, 'html-header')
2232 'form' => ['autocompletes' => $autoCompleteField],
2233 'ids' => ['profile' => $profiles],
2240 public function canUseAjaxContactLookups() {
2241 if (0 < (civicrm_api3('contact', 'getcount', ['check_permissions' => 1])) &&
2242 CRM_Core_Permission
::check([['access AJAX API', 'access CiviCRM']])
2249 * Add the options appropriate to cid = zero - ie. autocomplete
2251 * @todo there is considerable code duplication between the contribution forms & event forms. It is apparent
2252 * that small pieces of duplication are not being refactored into separate functions because their only shared parent
2253 * is this form. Inserting a class FrontEndForm.php between the contribution & event & this class would allow functions like this
2254 * and a dozen other small ones to be refactored into a shared parent with the reduction of much code duplication
2256 public function addCIDZeroOptions() {
2257 $this->assign('nocid', TRUE);
2259 if ($this->_values
['custom_pre_id']) {
2260 $profiles[] = $this->_values
['custom_pre_id'];
2262 if ($this->_values
['custom_post_id']) {
2263 $profiles = array_merge($profiles, (array) $this->_values
['custom_post_id']);
2265 $profiles[] = 'billing';
2266 if (!empty($this->_values
)) {
2267 $this->addAutoSelector($profiles);
2272 * Set default values on form for given contact (or no contact defaults)
2274 * @param mixed $profile_id
2275 * (can be id, or profile name).
2276 * @param int $contactID
2280 public function getProfileDefaults($profile_id = 'Billing', $contactID = NULL) {
2282 $defaults = civicrm_api3('profile', 'getsingle', [
2283 'profile_id' => (array) $profile_id,
2284 'contact_id' => $contactID,
2288 catch (Exception
$e) {
2289 // the try catch block gives us silent failure -not 100% sure this is a good idea
2290 // as silent failures are often worse than noisy ones
2296 * Sets form attribute.
2299 public function preventAjaxSubmit() {
2300 $this->setAttribute('data-no-ajax-submit', 'true');
2304 * Sets form attribute.
2307 public function allowAjaxSubmit() {
2308 $this->removeAttribute('data-no-ajax-submit');
2312 * Sets page title based on entity and action.
2313 * @param string $entityLabel
2315 public function setPageTitle($entityLabel) {
2316 switch ($this->_action
) {
2317 case CRM_Core_Action
::ADD
:
2318 CRM_Utils_System
::setTitle(ts('New %1', [1 => $entityLabel]));
2321 case CRM_Core_Action
::UPDATE
:
2322 CRM_Utils_System
::setTitle(ts('Edit %1', [1 => $entityLabel]));
2325 case CRM_Core_Action
::VIEW
:
2326 case CRM_Core_Action
::PREVIEW
:
2327 CRM_Utils_System
::setTitle(ts('View %1', [1 => $entityLabel]));
2330 case CRM_Core_Action
::DELETE
:
2331 CRM_Utils_System
::setTitle(ts('Delete %1', [1 => $entityLabel]));
2337 * Create a chain-select target field. All settings are optional; the defaults usually work.
2339 * @param string $elementName
2340 * @param array $settings
2342 * @return HTML_QuickForm_Element
2344 public function addChainSelect($elementName, $settings = []) {
2345 $props = $settings +
= [
2346 'control_field' => str_replace(['state_province', 'StateProvince', 'county', 'County'], [
2352 'data-callback' => strpos($elementName, 'rovince') ?
'civicrm/ajax/jqState' : 'civicrm/ajax/jqCounty',
2353 'label' => strpos($elementName, 'rovince') ?
ts('State/Province') : ts('County'),
2354 'data-empty-prompt' => strpos($elementName, 'rovince') ?
ts('Choose country first') : ts('Choose state first'),
2355 'data-none-prompt' => ts('- N/A -'),
2356 'multiple' => FALSE,
2357 'required' => FALSE,
2358 'placeholder' => empty($settings['required']) ?
ts('- none -') : ts('- select -'),
2360 CRM_Utils_Array
::remove($props, 'label', 'required', 'control_field', 'context');
2361 $props['class'] = (empty($props['class']) ?
'' : "{$props['class']} ") . 'crm-select2';
2362 $props['data-select-prompt'] = $props['placeholder'];
2363 $props['data-name'] = $elementName;
2365 $this->_chainSelectFields
[$settings['control_field']] = $elementName;
2367 // Passing NULL instead of an array of options
2368 // CRM-15225 - normally QF will reject any selected values that are not part of the field's options, but due to a
2369 // quirk in our patched version of HTML_QuickForm_select, this doesn't happen if the options are NULL
2370 // which seems a bit dirty but it allows our dynamically-popuplated select element to function as expected.
2371 return $this->add('select', $elementName, $settings['label'], NULL, $settings['required'], $props);
2375 * Add actions menu to results form.
2377 * @param array $tasks
2379 public function addTaskMenu($tasks) {
2380 if (is_array($tasks) && !empty($tasks)) {
2381 // Set constants means this will always load with an empty value, not reloading any submitted value.
2382 // This is appropriate as it is a pseudofield.
2383 $this->setConstants(['task' => '']);
2384 $this->assign('taskMetaData', $tasks);
2385 $select = $this->add('select', 'task', NULL, ['' => ts('Actions')], FALSE, [
2386 'class' => 'crm-select2 crm-action-menu fa-check-circle-o huge crm-search-result-actions',
2389 foreach ($tasks as $key => $task) {
2391 if (isset($task['data'])) {
2392 foreach ($task['data'] as $dataKey => $dataValue) {
2393 $attributes['data-' . $dataKey] = $dataValue;
2396 $select->addOption($task['title'], $key, $attributes);
2398 if (empty($this->_actionButtonName
)) {
2399 $this->_actionButtonName
= $this->getButtonName('next', 'action');
2401 $this->assign('actionButtonName', $this->_actionButtonName
);
2402 $this->add('submit', $this->_actionButtonName
, ts('Go'), ['class' => 'hiddenElement crm-search-go-button']);
2404 // Radio to choose "All items" or "Selected items only"
2405 $selectedRowsRadio = $this->addElement('radio', 'radio_ts', NULL, '', 'ts_sel', ['checked' => 'checked']);
2406 $allRowsRadio = $this->addElement('radio', 'radio_ts', NULL, '', 'ts_all');
2407 $this->assign('ts_sel_id', $selectedRowsRadio->_attributes
['id']);
2408 $this->assign('ts_all_id', $allRowsRadio->_attributes
['id']);
2410 CRM_Core_Resources
::singleton()->addScriptFile('civicrm', 'js/crm.searchForm.js', 1, 'html-header');
2415 * Set options and attributes for chain select fields based on the controlling field's value
2417 private function preProcessChainSelectFields() {
2418 foreach ($this->_chainSelectFields
as $control => $target) {
2419 // The 'target' might get missing if extensions do removeElement() in a form hook.
2420 if ($this->elementExists($target)) {
2421 $targetField = $this->getElement($target);
2422 $targetType = $targetField->getAttribute('data-callback') == 'civicrm/ajax/jqCounty' ?
'county' : 'stateProvince';
2424 // If the control field is on the form, setup chain-select and dynamically populate options
2425 if ($this->elementExists($control)) {
2426 $controlField = $this->getElement($control);
2427 $controlType = $targetType == 'county' ?
'stateProvince' : 'country';
2429 $targetField->setAttribute('class', $targetField->getAttribute('class') . ' crm-chain-select-target');
2431 $css = (string) $controlField->getAttribute('class');
2432 $controlField->updateAttributes([
2433 'class' => ($css ?
"$css " : 'crm-select2 ') . 'crm-chain-select-control',
2434 'data-target' => $target,
2436 $controlValue = $controlField->getValue();
2437 if ($controlValue) {
2438 $options = CRM_Core_BAO_Location
::getChainSelectValues($controlValue, $controlType, TRUE);
2440 $targetField->setAttribute('placeholder', $targetField->getAttribute('data-none-prompt'));
2444 $targetField->setAttribute('placeholder', $targetField->getAttribute('data-empty-prompt'));
2445 $targetField->setAttribute('disabled', 'disabled');
2448 // Control field not present - fall back to loading default options
2450 $options = CRM_Core_PseudoConstant
::$targetType();
2452 if (!$targetField->getAttribute('multiple')) {
2453 $options = ['' => $targetField->getAttribute('placeholder')] +
$options;
2454 $targetField->removeAttribute('placeholder');
2456 $targetField->_options
= [];
2457 $targetField->loadArray($options);
2463 * Validate country / state / county match and suppress unwanted "required" errors
2465 private function validateChainSelectFields() {
2466 foreach ($this->_chainSelectFields
as $control => $target) {
2467 if ($this->elementExists($control) && $this->elementExists($target)) {
2468 $controlValue = (array) $this->getElementValue($control);
2469 $targetField = $this->getElement($target);
2470 $controlType = $targetField->getAttribute('data-callback') == 'civicrm/ajax/jqCounty' ?
'stateProvince' : 'country';
2471 $targetValue = array_filter((array) $targetField->getValue());
2472 if ($targetValue ||
$this->getElementError($target)) {
2473 $options = CRM_Core_BAO_Location
::getChainSelectValues($controlValue, $controlType, TRUE);
2475 if (!array_intersect($targetValue, array_keys($options))) {
2476 $this->setElementError($target, $controlType == 'country' ?
ts('State/Province does not match the selected Country') : ts('County does not match the selected State/Province'));
2479 // Suppress "required" error for field if it has no options
2480 elseif (!$options) {
2481 $this->setElementError($target, NULL);
2489 * Assign billing name to the template.
2491 * @param array $params
2492 * Form input params, default to $this->_params.
2496 public function assignBillingName($params = []) {
2498 if (empty($params)) {
2499 $params = $this->_params
;
2501 if (!empty($params['billing_first_name'])) {
2502 $name = $params['billing_first_name'];
2505 if (!empty($params['billing_middle_name'])) {
2506 $name .= " {$params['billing_middle_name']}";
2509 if (!empty($params['billing_last_name'])) {
2510 $name .= " {$params['billing_last_name']}";
2512 $name = trim($name);
2513 $this->assign('billingName', $name);
2518 * Get the currency for the form.
2520 * @todo this should be overriden on the forms rather than having this
2521 * historic, possible handling in here. As we clean that up we should
2522 * add deprecation notices into here.
2524 * @param array $submittedValues
2525 * Array allowed so forms inheriting this class do not break.
2526 * Ideally we would make a clear standard around how submitted values
2527 * are stored (is $this->_values consistently doing that?).
2531 public function getCurrency($submittedValues = []) {
2532 $currency = CRM_Utils_Array
::value('currency', $this->_values
);
2533 // For event forms, currency is in a different spot
2534 if (empty($currency)) {
2535 $currency = CRM_Utils_Array
::value('currency', CRM_Utils_Array
::value('event', $this->_values
));
2537 if (empty($currency)) {
2538 $currency = CRM_Utils_Request
::retrieveValue('currency', 'String');
2540 // @todo If empty there is a problem - we should probably put in a deprecation notice
2541 // to warn if that seems to be happening.
2546 * Is the form in view or edit mode.
2548 * The 'addField' function relies on the form action being one of a set list
2549 * of actions. Checking for these allows for an early return.
2553 protected function isFormInViewOrEditMode() {
2554 return in_array($this->_action
, [
2555 CRM_Core_Action
::UPDATE
,
2556 CRM_Core_Action
::ADD
,
2557 CRM_Core_Action
::VIEW
,
2558 CRM_Core_Action
::BROWSE
,
2559 CRM_Core_Action
::BASIC
,
2560 CRM_Core_Action
::ADVANCED
,
2561 CRM_Core_Action
::PREVIEW
,