3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
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 = array();
70 * The options passed into this form
73 protected $_options = NULL;
76 * The mode of operation for this form
82 * Available payment processors.
84 * As part of trying to consolidate various payment pages we store processors here & have functions
85 * at this level to manage them.
88 * An array of payment processor details with objects loaded in the 'object' field.
90 protected $_paymentProcessors;
93 * Available payment processors (IDS).
95 * As part of trying to consolidate various payment pages we store processors here & have functions
96 * at this level to manage them.
99 * An array of the IDS available on this form.
101 public $_paymentProcessorIDs;
104 * The renderer used for this form
108 protected $_renderer;
111 * An array to hold a list of datefields on the form
112 * so that they can be converted to ISO in a consistent manner
116 * e.g on a form declare $_dateFields = array(
117 * 'receive_date' => array('default' => 'now'),
119 * then in postProcess call $this->convertDateFieldsToMySQL($formValues)
120 * to have the time field re-incorporated into the field & 'now' set if
121 * no value has been passed in
123 protected $_dateFields = array();
126 * Cache the smarty template for efficiency reasons
128 * @var CRM_Core_Smarty
130 static protected $_template;
133 * Indicate if this form should warn users of unsaved changes
135 protected $unsavedChangesWarn;
138 * What to return to the client if in ajax mode (snippet=json)
142 public $ajaxResponse = array();
145 * Url path used to reach this page
149 public $urlPath = array();
152 * @var CRM_Core_Controller
157 * Constants for attributes for various form elements
158 * attempt to standardize on the number of variations that we
159 * use of the below form elements
163 const ATTR_SPACING
= ' ';
166 * All checkboxes are defined with a common prefix. This allows us to
167 * have the same javascript to check / clear all the checkboxes etc
168 * If u have multiple groups of checkboxes, you will need to give them different
169 * ids to avoid potential name collision
173 const CB_PREFIX
= 'mark_x_', CB_PREFIY
= 'mark_y_', CB_PREFIZ
= 'mark_z_', CB_PREFIX_LEN
= 7;
176 * @internal to keep track of chain-select fields
179 private $_chainSelectFields = array();
182 * Constructor for the basic form page.
184 * We should not use QuickForm directly. This class provides a lot
185 * of default convenient functions, rules and buttons
187 * @param object $state
188 * State associated with this form.
189 * @param \const|\enum|int $action The mode the form is operating in (None/Create/View/Update/Delete)
190 * @param string $method
191 * The type of http method used (GET/POST).
192 * @param string $name
193 * The name of the form if different from class name.
195 * @return \CRM_Core_Form
197 public function __construct(
199 $action = CRM_Core_Action
::NONE
,
205 $this->_name
= $name;
208 // CRM-15153 - FIXME this name translates to a DOM id and is not always unique!
209 $this->_name
= CRM_Utils_String
::getClassName(CRM_Utils_System
::getClassName($this));
212 $this->HTML_QuickForm_Page($this->_name
, $method);
214 $this->_state
=& $state;
216 $this->_state
->setName($this->_name
);
218 $this->_action
= (int) $action;
220 $this->registerRules();
222 // let the constructor initialize this, should happen only once
223 if (!isset(self
::$_template)) {
224 self
::$_template = CRM_Core_Smarty
::singleton();
226 // Workaround for CRM-15153 - give each form a reasonably unique css class
227 $this->addClass(CRM_Utils_System
::getClassName($this));
229 $this->assign('snippet', CRM_Utils_Array
::value('snippet', $_GET));
232 public static function generateID() {
236 * Add one or more css classes to the form
237 * @param string $className
239 public function addClass($className) {
240 $classes = $this->getAttribute('class');
241 $this->setAttribute('class', ($classes ?
"$classes " : '') . $className);
245 * Register all the standard rules that most forms potentially use.
247 public function registerRules() {
248 static $rules = array(
277 foreach ($rules as $rule) {
278 $this->registerRule($rule, 'callback', $rule, 'CRM_Utils_Rule');
283 * Simple easy to use wrapper around addElement. Deal with
284 * simple validation rules
286 * @param string $type
287 * @param string $name
288 * @param string $label
289 * @param string|array $attributes (options for select elements)
290 * @param bool $required
291 * @param array $extra
292 * (attributes for select elements).
294 * @return HTML_QuickForm_Element could be an error object
296 public function &add(
297 $type, $name, $label = '',
298 $attributes = '', $required = FALSE, $extra = NULL
300 if ($type == 'wysiwyg') {
301 $attributes = ($attributes ?
$attributes : array()) +
array('class' => '');
302 $attributes['class'] .= ' crm-form-wysiwyg';
305 if ($type == 'select' && is_array($extra)) {
306 // Normalize this property
307 if (!empty($extra['multiple'])) {
308 $extra['multiple'] = 'multiple';
311 unset($extra['multiple']);
313 // Add placeholder option for select
314 if (isset($extra['placeholder'])) {
315 if ($extra['placeholder'] === TRUE) {
316 $extra['placeholder'] = $required ?
ts('- select -') : ts('- none -');
318 if (($extra['placeholder'] ||
$extra['placeholder'] === '') && empty($extra['multiple']) && is_array($attributes) && !isset($attributes[''])) {
319 $attributes = array('' => $extra['placeholder']) +
$attributes;
323 $element = $this->addElement($type, $name, $label, $attributes, $extra);
324 if (HTML_QuickForm
::isError($element)) {
325 CRM_Core_Error
::fatal(HTML_QuickForm
::errorMessage($element));
329 if ($type == 'file') {
330 $error = $this->addRule($name, ts('%1 is a required field.', array(1 => $label)), 'uploadedfile');
333 $error = $this->addRule($name, ts('%1 is a required field.', array(1 => $label)), 'required');
335 if (HTML_QuickForm
::isError($error)) {
336 CRM_Core_Error
::fatal(HTML_QuickForm
::errorMessage($element));
344 * Add an element for inputting a month+day (partial date).
346 * @param string $name
347 * @param string $label
348 * @return HTML_QuickForm_Element
350 public function addMonthDay($name, $label) {
351 return $this->add('date', $name, $label,
352 CRM_Core_SelectValues
::date(NULL, 'M d')
357 * called before buildForm. Any pre-processing that
358 * needs to be done for buildForm should be done here
360 * This is a virtual function and should be redefined if needed.
362 public function preProcess() {
366 * Called after the form is validated.
368 * Any processing of form state etc should be done in this function.
369 * Typically all processing associated with a form should be done
370 * here and relevant state should be stored in the session
372 * This is a virtual function and should be redefined if needed
374 public function postProcess() {
378 * just a wrapper, so that we can call all the hook functions
379 * @param bool $allowAjax
380 * FIXME: This feels kind of hackish, ideally we would take the json-related code from this function.
381 * and bury it deeper down in the controller
383 public function mainProcess($allowAjax = TRUE) {
384 $this->postProcess();
385 $this->postProcessHook();
387 // Respond with JSON if in AJAX context (also support legacy value '6')
388 if ($allowAjax && !empty($_REQUEST['snippet']) && in_array($_REQUEST['snippet'], array(
389 CRM_Core_Smarty
::PRINT_JSON
,
393 $this->ajaxResponse
['buttonName'] = str_replace('_qf_' . $this->getAttribute('id') . '_', '', $this->controller
->getButtonName());
394 $this->ajaxResponse
['action'] = $this->_action
;
395 if (isset($this->_id
) ||
isset($this->id
)) {
396 $this->ajaxResponse
['id'] = isset($this->id
) ?
$this->id
: $this->_id
;
398 CRM_Core_Page_AJAX
::returnJsonResponse($this->ajaxResponse
);
403 * The postProcess hook is typically called by the framework.
404 * However in a few cases, the form exits or redirects early in which
405 * case it needs to call this function so other modules can do the needful
406 * Calling this function directly should be avoided if possible. In general a
407 * better way is to do setUserContext so the framework does the redirect
409 public function postProcessHook() {
410 CRM_Utils_Hook
::postProcess(get_class($this), $this);
414 * This virtual function is used to build the form.
416 * It replaces the buildForm associated with QuickForm_Page. This allows us to put
417 * preProcess in front of the actual form building routine
419 public function buildQuickForm() {
423 * This virtual function is used to set the default values of various form elements.
426 * reference to the array of default values
428 public function setDefaultValues() {
433 * This is a virtual function that adds group and global rules to the form.
435 * Keeping it distinct from the form to keep code small
436 * and localized in the form building code
438 public function addRules() {
442 * Performs the server side validation.
445 * true if no error found
446 * @throws HTML_QuickForm_Error
448 public function validate() {
449 $error = parent
::validate();
451 $this->validateChainSelectFields();
453 $hookErrors = array();
455 CRM_Utils_Hook
::validateForm(
457 $this->_submitValues
,
463 if (!empty($hookErrors)) {
464 $this->_errors +
= $hookErrors;
467 return (0 == count($this->_errors
));
471 * Core function that builds the form.
473 * We redefine this function here and expect all CRM forms to build their form in the function
476 public function buildForm() {
477 $this->_formBuilt
= TRUE;
481 CRM_Utils_Hook
::preProcess(get_class($this), $this);
483 $this->assign('translatePermission', CRM_Core_Permission
::check('translate CiviCRM'));
486 $this->controller
->_key
&&
487 $this->controller
->_generateQFKey
489 $this->addElement('hidden', 'qfKey', $this->controller
->_key
);
490 $this->assign('qfKey', $this->controller
->_key
);
494 // _generateQFKey suppresses the qfKey generation on form snippets that
495 // are part of other forms, hence we use that to avoid adding entryURL
496 if ($this->controller
->_generateQFKey
&& $this->controller
->_entryURL
) {
497 $this->addElement('hidden', 'entryURL', $this->controller
->_entryURL
);
500 $this->buildQuickForm();
502 $defaults = $this->setDefaultValues();
503 unset($defaults['qfKey']);
505 if (!empty($defaults)) {
506 $this->setDefaults($defaults);
509 // call the form hook
510 // also call the hook function so any modules can set their own custom defaults
511 // the user can do both the form and set default values with this hook
512 CRM_Utils_Hook
::buildForm(get_class($this), $this);
516 //Set html data-attribute to enable warning user of unsaved changes
517 if ($this->unsavedChangesWarn
=== TRUE
518 ||
(!isset($this->unsavedChangesWarn
)
519 && ($this->_action
& CRM_Core_Action
::ADD ||
$this->_action
& CRM_Core_Action
::UPDATE
)
522 $this->setAttribute('data-warn-changes', 'true');
527 * Add default Next / Back buttons.
529 * @param array $params
530 * Array of associative arrays in the order in which the buttons should be
531 * displayed. The associate array has 3 fields: 'type', 'name' and 'isDefault'
532 * The base form class will define a bunch of static arrays for commonly used
535 public function addButtons($params) {
536 $prevnext = $spacing = array();
537 foreach ($params as $button) {
538 $attrs = array('class' => 'crm-form-submit') +
(array) CRM_Utils_Array
::value('js', $button);
540 if (!empty($button['class'])) {
541 $attrs['class'] .= ' ' . $button['class'];
544 if (!empty($button['isDefault'])) {
545 $attrs['class'] .= ' default';
548 if (in_array($button['type'], array('upload', 'next', 'submit', 'done', 'process', 'refresh'))) {
549 $attrs['class'] .= ' validate';
550 $defaultIcon = 'fa-check';
553 $attrs['class'] .= ' cancel';
554 $defaultIcon = $button['type'] == 'back' ?
'fa-chevron-left' : 'fa-times';
557 if ($button['type'] === 'reset') {
558 $prevnext[] = $this->createElement($button['type'], 'reset', $button['name'], $attrs);
561 if (!empty($button['subName'])) {
562 if ($button['subName'] == 'new') {
563 $defaultIcon = 'fa-plus-circle';
565 if ($button['subName'] == 'done') {
566 $defaultIcon = 'fa-check-circle';
568 if ($button['subName'] == 'next') {
569 $defaultIcon = 'fa-chevron-right';
573 if (in_array($button['type'], array('next', 'upload', 'done')) && $button['name'] === ts('Save')) {
574 $attrs['accesskey'] = 'S';
576 $icon = CRM_Utils_Array
::value('icon', $button, $defaultIcon);
578 $attrs['crm-icon'] = $icon;
580 $buttonName = $this->getButtonName($button['type'], CRM_Utils_Array
::value('subName', $button));
581 $prevnext[] = $this->createElement('submit', $buttonName, $button['name'], $attrs);
583 if (!empty($button['isDefault'])) {
584 $this->setDefaultAction($button['type']);
587 // if button type is upload, set the enctype
588 if ($button['type'] == 'upload') {
589 $this->updateAttributes(array('enctype' => 'multipart/form-data'));
590 $this->setMaxFileSize();
593 // hack - addGroup uses an array to express variable spacing, read from the last element
594 $spacing[] = CRM_Utils_Array
::value('spacing', $button, self
::ATTR_SPACING
);
596 $this->addGroup($prevnext, 'buttons', '', $spacing, FALSE);
600 * Getter function for Name.
604 public function getName() {
609 * Getter function for State.
613 public function &getState() {
614 return $this->_state
;
618 * Getter function for StateType.
622 public function getStateType() {
623 return $this->_state
->getType();
627 * Getter function for title.
629 * Should be over-ridden by derived class.
633 public function getTitle() {
634 return $this->_title ?
$this->_title
: ts('ERROR: Title is not Set');
638 * Setter function for title.
640 * @param string $title
641 * The title of the form.
643 public function setTitle($title) {
644 $this->_title
= $title;
648 * Assign billing type id to bltID.
650 * @throws CRM_Core_Exception
652 public function assignBillingType() {
653 $this->_bltID
= CRM_Core_BAO_LocationType
::getBilling();
654 $this->set('bltID', $this->_bltID
);
655 $this->assign('bltID', $this->_bltID
);
659 * This if a front end form function for setting the payment processor.
661 * It would be good to sync it with the back-end function on abstractEditPayment & use one everywhere.
663 * @throws \CRM_Core_Exception
665 protected function assignPaymentProcessor() {
666 $this->_paymentProcessors
= CRM_Financial_BAO_PaymentProcessor
::getPaymentProcessors(
667 array(ucfirst($this->_mode
) . 'Mode'),
668 $this->_paymentProcessorIDs
671 if (!empty($this->_paymentProcessors
)) {
672 foreach ($this->_paymentProcessors
as $paymentProcessorID => $paymentProcessorDetail) {
673 if (empty($this->_paymentProcessor
) && $paymentProcessorDetail['is_default'] == 1 ||
(count($this->_paymentProcessors
) == 1)
675 $this->_paymentProcessor
= $paymentProcessorDetail;
676 $this->assign('paymentProcessor', $this->_paymentProcessor
);
677 // Setting this is a bit of a legacy overhang.
678 $this->_paymentObject
= $paymentProcessorDetail['object'];
681 // It's not clear why we set this on the form.
682 $this->set('paymentProcessors', $this->_paymentProcessors
);
685 throw new CRM_Core_Exception(ts('A payment processor configured for this page might be disabled (contact the site administrator for assistance).'));
691 * Format the fields for the payment processor.
693 * In order to pass fields to the payment processor in a consistent way we add some renamed
696 * @param array $fields
700 protected function formatParamsForPaymentProcessor($fields) {
701 // also add location name to the array
702 $this->_params
["address_name-{$this->_bltID}"] = CRM_Utils_Array
::value('billing_first_name', $this->_params
) . ' ' . CRM_Utils_Array
::value('billing_middle_name', $this->_params
) . ' ' . CRM_Utils_Array
::value('billing_last_name', $this->_params
);
703 $this->_params
["address_name-{$this->_bltID}"] = trim($this->_params
["address_name-{$this->_bltID}"]);
704 // Add additional parameters that the payment processors are used to receiving.
705 if (!empty($this->_params
["billing_state_province_id-{$this->_bltID}"])) {
706 $this->_params
['state_province'] = $this->_params
["state_province-{$this->_bltID}"] = $this->_params
["billing_state_province-{$this->_bltID}"] = CRM_Core_PseudoConstant
::stateProvinceAbbreviation($this->_params
["billing_state_province_id-{$this->_bltID}"]);
708 if (!empty($this->_params
["billing_country_id-{$this->_bltID}"])) {
709 $this->_params
['country'] = $this->_params
["country-{$this->_bltID}"] = $this->_params
["billing_country-{$this->_bltID}"] = CRM_Core_PseudoConstant
::countryIsoCode($this->_params
["billing_country_id-{$this->_bltID}"]);
712 list($hasAddressField, $addressParams) = CRM_Contribute_BAO_Contribution
::getPaymentProcessorReadyAddressParams($this->_params
, $this->_bltID
);
713 if ($hasAddressField) {
714 $this->_params
= array_merge($this->_params
, $addressParams);
717 $nameFields = array('first_name', 'middle_name', 'last_name');
718 foreach ($nameFields as $name) {
720 if (array_key_exists("billing_$name", $this->_params
)) {
721 $this->_params
[$name] = $this->_params
["billing_{$name}"];
722 $this->_params
['preserveDBName'] = TRUE;
729 * Handle Payment Processor switching for contribution and event registration forms.
731 * This function is shared between contribution & event forms & this is their common class.
733 * However, this should be seen as an in-progress refactor, the end goal being to also align the
734 * backoffice forms that action payments.
736 * This function overlaps assignPaymentProcessor, in a bad way.
738 protected function preProcessPaymentOptions() {
739 $this->_paymentProcessorID
= NULL;
740 $this->_paymentProcessors
[0] = CRM_Financial_BAO_PaymentProcessor
::getPayment(0);
741 if ($this->_paymentProcessors
) {
742 if (!empty($this->_submitValues
)) {
743 $this->_paymentProcessorID
= CRM_Utils_Array
::value('payment_processor_id', $this->_submitValues
);
744 $this->_paymentProcessor
= CRM_Utils_Array
::value($this->_paymentProcessorID
, $this->_paymentProcessors
);
745 $this->set('type', $this->_paymentProcessorID
);
746 $this->set('mode', $this->_mode
);
747 $this->set('paymentProcessor', $this->_paymentProcessor
);
749 // Set default payment processor
751 foreach ($this->_paymentProcessors
as $values) {
752 if (!empty($values['is_default']) ||
count($this->_paymentProcessors
) == 1) {
753 $this->_paymentProcessorID
= $values['id'];
758 if ($this->_paymentProcessorID
759 ||
(isset($this->_submitValues
['payment_processor_id']) && $this->_submitValues
['payment_processor_id'] == 0)
761 CRM_Core_Payment_ProcessorForm
::preProcess($this);
764 $this->_paymentProcessor
= array();
766 CRM_Financial_Form_Payment
::addCreditCardJs();
768 $this->assign('paymentProcessorID', $this->_paymentProcessorID
);
769 // We save the fact that the profile 'billing' is required on the payment form.
770 // Currently pay-later is the only 'processor' that takes notice of this - but ideally
771 // 1) it would be possible to select the minimum_billing_profile_id for the contribution form
772 // 2) that profile_id would be set on the payment processor
773 // 3) the payment processor would return a billing form that combines these user-configured
774 // minimums with the payment processor minimums. This would lead to fields like 'postal_code'
775 // only being on the form if either the admin has configured it as wanted or the processor
777 $this->assign('billing_profile_id', (CRM_Utils_Array
::value('is_billing_required', $this->_values
) ?
'billing' : ''));
781 * Handle pre approval for processors.
783 * This fits with the flow where a pre-approval is done and then confirmed in the next stage when confirm is hit.
785 * This function is shared between contribution & event forms & this is their common class.
787 * However, this should be seen as an in-progress refactor, the end goal being to also align the
788 * backoffice forms that action payments.
790 * @param array $params
792 protected function handlePreApproval(&$params) {
794 $payment = Civi\Payment\System
::singleton()->getByProcessor($this->_paymentProcessor
);
795 $params['component'] = 'contribute';
796 $result = $payment->doPreApproval($params);
797 if (empty($result)) {
798 // This could happen, for example, when paypal looks at the button value & decides it is not paypal express.
802 catch (\Civi\Payment\Exception\PaymentProcessorException
$e) {
803 CRM_Core_Error
::displaySessionError($e->getMessage());
804 CRM_Utils_System
::redirect($params['cancelURL']);
807 $this->set('pre_approval_parameters', $result['pre_approval_parameters']);
808 if (!empty($result['redirect_url'])) {
809 CRM_Utils_System
::redirect($result['redirect_url']);
814 * Setter function for options.
816 * @param mixed $options
818 public function setOptions($options) {
819 $this->_options
= $options;
823 * Getter function for link.
827 public function getLink() {
828 $config = CRM_Core_Config
::singleton();
829 return CRM_Utils_System
::url($_GET[$config->userFrameworkURLVar
],
830 '_qf_' . $this->_name
. '_display=true'
835 * Boolean function to determine if this is a one form page.
839 public function isSimpleForm() {
840 return $this->_state
->getType() & (CRM_Core_State
::START | CRM_Core_State
::FINISH
);
844 * Getter function for Form Action.
848 public function getFormAction() {
849 return $this->_attributes
['action'];
853 * Setter function for Form Action.
855 * @param string $action
857 public function setFormAction($action) {
858 $this->_attributes
['action'] = $action;
862 * Render form and return contents.
866 public function toSmarty() {
867 $this->preProcessChainSelectFields();
868 $renderer = $this->getRenderer();
869 $this->accept($renderer);
870 $content = $renderer->toArray();
871 $content['formName'] = $this->getName();
873 $content['formClass'] = CRM_Utils_System
::getClassName($this);
878 * Getter function for renderer.
880 * If renderer is not set create one and initialize it.
884 public function &getRenderer() {
885 if (!isset($this->_renderer
)) {
886 $this->_renderer
= CRM_Core_Form_Renderer
::singleton();
888 return $this->_renderer
;
892 * Use the form name to create the tpl file name.
896 public function getTemplateFileName() {
897 $ext = CRM_Extension_System
::singleton()->getMapper();
898 if ($ext->isExtensionClass(CRM_Utils_System
::getClassName($this))) {
899 $filename = $ext->getTemplateName(CRM_Utils_System
::getClassName($this));
900 $tplname = $ext->getTemplatePath(CRM_Utils_System
::getClassName($this)) . DIRECTORY_SEPARATOR
. $filename;
904 CRM_Utils_System
::getClassName($this),
906 '_' => DIRECTORY_SEPARATOR
,
907 '\\' => DIRECTORY_SEPARATOR
,
915 * A wrapper for getTemplateFileName.
917 * This includes calling the hook to prevent us from having to copy & paste the logic of calling the hook.
919 public function getHookedTemplateFileName() {
920 $pageTemplateFile = $this->getTemplateFileName();
921 CRM_Utils_Hook
::alterTemplateFile(get_class($this), $this, 'page', $pageTemplateFile);
922 return $pageTemplateFile;
926 * Default extra tpl file basically just replaces .tpl with .extra.tpl.
928 * i.e. we do not override.
932 public function overrideExtraTemplateFileName() {
937 * Error reporting mechanism.
939 * @param string $message
943 * @param CRM_Core_DAO $dao
944 * A data access object on which we perform a rollback if non - empty.
946 public function error($message, $code = NULL, $dao = NULL) {
948 $dao->query('ROLLBACK');
951 $error = CRM_Core_Error
::singleton();
953 $error->push($code, $message);
957 * Store the variable with the value in the form scope.
959 * @param string $name
960 * Name of the variable.
961 * @param mixed $value
962 * Value of the variable.
964 public function set($name, $value) {
965 $this->controller
->set($name, $value);
969 * Get the variable from the form scope.
971 * @param string $name
972 * Name of the variable
976 public function get($name) {
977 return $this->controller
->get($name);
985 public function getAction() {
986 return $this->_action
;
993 * The mode we want to set the form.
995 public function setAction($action) {
996 $this->_action
= $action;
1000 * Assign value to name in template.
1002 * @param string $var
1004 * @param mixed $value
1005 * Value of variable.
1007 public function assign($var, $value = NULL) {
1008 self
::$_template->assign($var, $value);
1012 * Assign value to name in template by reference.
1014 * @param string $var
1016 * @param mixed $value
1017 * Value of variable.
1019 public function assign_by_ref($var, &$value) {
1020 self
::$_template->assign_by_ref($var, $value);
1024 * Appends values to template variables.
1026 * @param array|string $tpl_var the template variable name(s)
1027 * @param mixed $value
1028 * The value to append.
1029 * @param bool $merge
1031 public function append($tpl_var, $value = NULL, $merge = FALSE) {
1032 self
::$_template->append($tpl_var, $value, $merge);
1036 * Returns an array containing template variables.
1038 * @param string $name
1042 public function get_template_vars($name = NULL) {
1043 return self
::$_template->get_template_vars($name);
1047 * @param string $name
1050 * @param array $attributes
1051 * @param null $separator
1052 * @param bool $required
1054 * @return HTML_QuickForm_group
1056 public function &addRadio($name, $title, $values, $attributes = array(), $separator = NULL, $required = FALSE) {
1058 $attributes = $attributes ?
$attributes : array();
1059 $allowClear = !empty($attributes['allowClear']);
1060 unset($attributes['allowClear']);
1061 $attributes['id_suffix'] = $name;
1062 foreach ($values as $key => $var) {
1063 $options[] = $this->createElement('radio', NULL, NULL, $var, $key, $attributes);
1065 $group = $this->addGroup($options, $name, $title, $separator);
1067 $this->addRule($name, ts('%1 is a required field.', array(1 => $title)), 'required');
1070 $group->setAttribute('allowClear', TRUE);
1078 * @param bool $allowClear
1079 * @param null $required
1080 * @param array $attributes
1082 public function addYesNo($id, $title, $allowClear = FALSE, $required = NULL, $attributes = array()) {
1083 $attributes +
= array('id_suffix' => $id);
1085 $choice[] = $this->createElement('radio', NULL, '11', ts('Yes'), '1', $attributes);
1086 $choice[] = $this->createElement('radio', NULL, '11', ts('No'), '0', $attributes);
1088 $group = $this->addGroup($choice, $id, $title);
1090 $group->setAttribute('allowClear', TRUE);
1093 $this->addRule($id, ts('%1 is a required field.', array(1 => $title)), 'required');
1101 * @param null $other
1102 * @param null $attributes
1103 * @param null $required
1104 * @param null $javascriptMethod
1105 * @param string $separator
1106 * @param bool $flipValues
1108 public function addCheckBox(
1109 $id, $title, $values, $other = NULL,
1110 $attributes = NULL, $required = NULL,
1111 $javascriptMethod = NULL,
1112 $separator = '<br />', $flipValues = FALSE
1116 if ($javascriptMethod) {
1117 foreach ($values as $key => $var) {
1119 $options[] = $this->createElement('checkbox', $var, NULL, $key, $javascriptMethod);
1122 $options[] = $this->createElement('checkbox', $key, NULL, $var, $javascriptMethod);
1127 foreach ($values as $key => $var) {
1129 $options[] = $this->createElement('checkbox', $var, NULL, $key);
1132 $options[] = $this->createElement('checkbox', $key, NULL, $var);
1137 $this->addGroup($options, $id, $title, $separator);
1140 $this->addElement('text', $id . '_other', ts('Other'), $attributes[$id . '_other']);
1145 ts('%1 is a required field.', array(1 => $title)),
1151 public function resetValues() {
1152 $data = $this->controller
->container();
1153 $data['values'][$this->_name
] = array();
1157 * Simple shell that derived classes can call to add buttons to
1158 * the form with a customized title for the main Submit
1160 * @param string $title
1161 * Title of the main button.
1162 * @param string $nextType
1163 * Button type for the form after processing.
1164 * @param string $backType
1165 * @param bool|string $submitOnce If true, add javascript to next button submit which prevents it from being clicked more than once
1167 public function addDefaultButtons($title, $nextType = 'next', $backType = 'back', $submitOnce = FALSE) {
1169 if ($backType != NULL) {
1171 'type' => $backType,
1172 'name' => ts('Previous'),
1175 if ($nextType != NULL) {
1176 $nextButton = array(
1177 'type' => $nextType,
1179 'isDefault' => TRUE,
1182 $nextButton['js'] = array('onclick' => "return submitOnce(this,'{$this->_name}','" . ts('Processing') . "');");
1184 $buttons[] = $nextButton;
1186 $this->addButtons($buttons);
1190 * @param string $name
1191 * @param string $from
1193 * @param string $label
1194 * @param string $dateFormat
1195 * @param bool $required
1196 * @param bool $displayTime
1198 public function addDateRange($name, $from = '_from', $to = '_to', $label = 'From:', $dateFormat = 'searchDate', $required = FALSE, $displayTime = FALSE) {
1200 $this->addDateTime($name . $from, $label, $required, array('formatType' => $dateFormat));
1201 $this->addDateTime($name . $to, ts('To:'), $required, array('formatType' => $dateFormat));
1204 $this->addDate($name . $from, $label, $required, array('formatType' => $dateFormat));
1205 $this->addDate($name . $to, ts('To:'), $required, array('formatType' => $dateFormat));
1210 * Based on form action, return a string representing the api action.
1211 * Used by addField method.
1215 private function getApiAction() {
1216 $action = $this->getAction();
1217 if ($action & (CRM_Core_Action
::UPDATE + CRM_Core_Action
::ADD
)) {
1220 if ($action & (CRM_Core_Action
::BROWSE
)) {
1223 // If you get this exception try adding more cases above.
1224 throw new Exception("Cannot determine api action for " . __CLASS__
);
1228 * Classes extending CRM_Core_Form should implement this method.
1231 public function getDefaultEntity() {
1232 throw new Exception("Cannot determine default entity. The form class should implement getDefaultEntity().");
1236 * Classes extending CRM_Core_Form should implement this method.
1238 * TODO: Merge with CRM_Core_DAO::buildOptionsContext($context) and add validation.
1241 public function getDefaultContext() {
1242 throw new Exception("Cannot determine default context. The form class should implement getDefaultContext().");
1246 * Adds a select based on field metadata.
1247 * TODO: This could be even more generic and widget type (select in this case) could also be read from metadata
1248 * Perhaps a method like $form->bind($name) which would look up all metadata for named field
1250 * Field name to go on the form.
1251 * @param array $props
1252 * Mix of html attributes and special properties, namely.
1253 * - entity (api entity name, can usually be inferred automatically from the form class)
1254 * - field (field name - only needed if different from name used on the form)
1255 * - option_url - path to edit this option list - usually retrieved automatically - set to NULL to disable link
1256 * - placeholder - set to NULL to disable
1258 * - context - @see CRM_Core_DAO::buildOptionsContext
1259 * @param bool $required
1260 * @throws CRM_Core_Exception
1261 * @return HTML_QuickForm_Element
1263 public function addSelect($name, $props = array(), $required = FALSE) {
1264 if (!isset($props['entity'])) {
1265 $props['entity'] = $this->getDefaultEntity();
1267 if (!isset($props['field'])) {
1268 $props['field'] = strrpos($name, '[') ?
rtrim(substr($name, 1 +
strrpos($name, '[')), ']') : $name;
1270 // Fetch options from the api unless passed explicitly
1271 if (isset($props['options'])) {
1272 $options = $props['options'];
1275 $info = civicrm_api3($props['entity'], 'getoptions', $props);
1276 $options = $info['values'];
1278 if (!array_key_exists('placeholder', $props)) {
1279 $props['placeholder'] = $required ?
ts('- select -') : CRM_Utils_Array
::value('context', $props) == 'search' ?
ts('- any -') : ts('- none -');
1281 // Handle custom field
1282 if (strpos($name, 'custom_') === 0 && is_numeric($name[7])) {
1283 list(, $id) = explode('_', $name);
1284 $label = isset($props['label']) ?
$props['label'] : CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomField', 'label', $id);
1285 $gid = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomField', 'option_group_id', $id);
1286 if (CRM_Utils_Array
::value('context', $props) != 'search') {
1287 $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);
1292 $info = civicrm_api3($props['entity'], 'getfields');
1293 foreach ($info['values'] as $uniqueName => $fieldSpec) {
1295 $uniqueName === $props['field'] ||
1296 CRM_Utils_Array
::value('name', $fieldSpec) === $props['field'] ||
1297 in_array($props['field'], CRM_Utils_Array
::value('api.aliases', $fieldSpec, array()))
1302 $label = isset($props['label']) ?
$props['label'] : $fieldSpec['title'];
1303 if (CRM_Utils_Array
::value('context', $props) != 'search') {
1304 $props['data-option-edit-path'] = array_key_exists('option_url', $props) ?
$props['option_url'] : $props['data-option-edit-path'] = CRM_Core_PseudoConstant
::getOptionEditUrl($fieldSpec);
1307 $props['class'] = (isset($props['class']) ?
$props['class'] . ' ' : '') . "crm-select2";
1308 $props['data-api-entity'] = $props['entity'];
1309 $props['data-api-field'] = $props['field'];
1310 CRM_Utils_Array
::remove($props, 'label', 'entity', 'field', 'option_url', 'options', 'context');
1311 return $this->add('select', $name, $label, $options, $required, $props);
1315 * Adds a field based on metadata.
1318 * Field name to go on the form.
1319 * @param array $props
1320 * Mix of html attributes and special properties, namely.
1321 * - entity (api entity name, can usually be inferred automatically from the form class)
1322 * - name (field name - only needed if different from name used on the form)
1323 * - option_url - path to edit this option list - usually retrieved automatically - set to NULL to disable link
1324 * - placeholder - set to NULL to disable
1326 * - context - @see CRM_Core_DAO::buildOptionsContext
1327 * @param bool $required
1328 * @throws \CiviCRM_API3_Exception
1329 * @throws \Exception
1330 * @return HTML_QuickForm_Element
1332 public function addField($name, $props = array(), $required = FALSE) {
1333 // TODO: Handle custom field
1334 if (strpos($name, 'custom_') === 0 && is_numeric($name[7])) {
1335 throw new Exception("Custom fields are not supported by the addField method. ");
1338 if (!isset($props['context'])) {
1339 $props['context'] = $this->getDefaultContext();
1342 if (!isset($props['entity'])) {
1343 $props['entity'] = $this->getDefaultEntity();
1346 if (!isset($props['name'])) {
1347 $props['name'] = strrpos($name, '[') ?
rtrim(substr($name, 1 +
strrpos($name, '[')), ']') : $name;
1350 if (!isset($props['action'])) {
1351 $props['action'] = $this->getApiAction();
1353 // Get field metadata.
1354 $fieldSpec = civicrm_api3($props['entity'], 'getfield', $props);
1355 $fieldSpec = $fieldSpec['values'];
1356 $label = CRM_Utils_Array
::value('label', $props, isset($fieldSpec['title']) ?
$fieldSpec['title'] : NULL);
1358 $widget = isset($props['type']) ?
$props['type'] : $fieldSpec['html']['type'];
1359 if ($widget == 'TextArea' && $props['context'] == 'search') {
1363 $isSelect = (in_array($widget, array(
1366 'Select State/Province',
1367 'Multi-Select State/Province',
1369 'Multi-Select Country',
1377 // Fetch options from the api unless passed explicitly.
1378 if (isset($props['options'])) {
1379 $options = $props['options'];
1380 // Else this get passed to the form->add method.
1381 unset($props['options']);
1384 $options = isset($fieldSpec['options']) ?
$fieldSpec['options'] : NULL;
1386 //@TODO AdvMulti-Select is deprecated, drop support.
1387 if ($props['context'] == 'search' ||
($widget !== 'AdvMulti-Select' && strpos($widget, 'Select') !== FALSE)) {
1390 // Set default options-url value.
1391 if ((!isset($props['options-url']))) {
1392 $props['options-url'] = TRUE;
1395 // Add data for popup link.
1396 if ((isset($props['options-url']) && $props['options-url']) && ($props['context'] != 'search' && $widget == 'Select' && CRM_Core_Permission
::check('administer CiviCRM'))) {
1397 $props['data-option-edit-path'] = array_key_exists('option_url', $props) ?
$props['option_url'] : $props['data-option-edit-path'] = CRM_Core_PseudoConstant
::getOptionEditUrl($fieldSpec);
1398 $props['data-api-entity'] = $props['entity'];
1399 $props['data-api-field'] = $props['name'];
1400 if (isset($props['options-url'])) {
1401 unset($props['options-url']);
1405 //Use select2 library for following widgets.
1406 $isSelect2 = (in_array($widget, array(
1409 'Select State/Province',
1410 'Multi-Select State/Province',
1412 'Multi-Select Country',
1415 $props['class'] = (isset($props['class']) ?
$props['class'] . ' ' : '') . "crm-select2";
1416 if ($props['context'] == 'search' ||
strpos($widget, 'Multi') !== FALSE) {
1417 $props['class'] .= ' huge';
1418 $props['multiple'] = 'multiple';
1420 // The placeholder is only used for select-elements.
1421 if (!array_key_exists('placeholder', $props)) {
1422 $props['placeholder'] = $required ?
ts('- select -') : $props['context'] == 'search' ?
ts('- any -') : ts('- none -');
1425 $props +
= CRM_Utils_Array
::value('html', $fieldSpec, array());
1426 CRM_Utils_Array
::remove($props, 'entity', 'name', 'context', 'label', 'action', 'type');
1427 // TODO: refactor switch statement, to separate methods.
1431 //TODO: Autodetect ranges
1432 $props['size'] = isset($props['size']) ?
$props['size'] : 60;
1433 return $this->add('text', $name, $label, $props, $required);
1436 return $this->add('hidden', $name, NULL, $props, $required);
1439 //Set default columns and rows for textarea.
1440 $props['rows'] = isset($props['rows']) ?
$props['rows'] : 4;
1441 $props['cols'] = isset($props['cols']) ?
$props['cols'] : 60;
1442 return $this->addElement('textarea', $name, $label, $props, $required);
1445 //TODO: add range support
1446 //TODO: Add date formats
1447 //TODO: Add javascript template for dates.
1448 return $this->addDate($name, $label, $required, $props);
1451 $separator = isset($props['separator']) ?
$props['separator'] : NULL;
1452 unset($props['separator']);
1453 if (!isset($props['allowClear'])) {
1454 $props['allowClear'] = !$required;
1456 return $this->addRadio($name, $label, $options, $props, $separator, $required);
1459 if (empty($props['multiple'])) {
1460 $options = array('' => $props['placeholder']) +
$options;
1462 if (!empty($props['data-api-field']) && (in_array($props['data-api-field'], array('state_province_id', 'county_id')))) {
1463 return $this->addChainSelect($name, $props);
1465 // TODO: Add and/or option for fields that store multiple values
1466 return $this->add('select', $name, $label, $options, $required, $props);
1468 case 'CheckBoxGroup':
1469 return $this->addCheckBox($name, $label, array_flip($options), $required, $props);
1472 return $this->addRadio($name, $label, $options, $props, NULL, $required);
1474 //case 'AdvMulti-Select':
1476 $text = isset($props['text']) ?
$props['text'] : NULL;
1477 unset($props['text']);
1478 return $this->addElement('checkbox', $name, $label, $text, $props);
1480 //add support for 'Advcheckbox' field
1482 $text = isset($props['text']) ?
$props['text'] : NULL;
1483 unset($props['text']);
1484 return $this->addElement('advcheckbox', $name, $label, $text, $props);
1487 // We should not build upload file in search mode.
1488 if (isset($props['context']) && $props['context'] == 'search') {
1491 $file = $this->add('file', $name, $label, $props, $required);
1492 $this->addUploadElement($name);
1495 //case 'RichTextEditor':
1496 //TODO: Add javascript template for wysiwyg.
1497 case 'Autocomplete-Select':
1499 return $this->addEntityRef($name, $label, $props, $required);
1501 // Check datatypes of fields
1506 //case read only fields
1508 throw new Exception("Unsupported html-element " . $widget);
1513 * Add a widget for selecting/editing/creating/copying a profile form
1515 * @param string $name
1516 * HTML form-element name.
1517 * @param string $label
1519 * @param string $allowCoreTypes
1520 * Only present a UFGroup if its group_type includes a subset of $allowCoreTypes; e.g. 'Individual', 'Activity'.
1521 * @param string $allowSubTypes
1522 * Only present a UFGroup if its group_type is compatible with $allowSubypes.
1523 * @param array $entities
1524 * @param bool $default
1527 public function addProfileSelector($name, $label, $allowCoreTypes, $allowSubTypes, $entities, $default = FALSE, $usedFor = NULL) {
1529 // FIXME: Instead of adhoc serialization, use a single json_encode()
1530 CRM_UF_Page_ProfileEditor
::registerProfileScripts();
1531 CRM_UF_Page_ProfileEditor
::registerSchemas(CRM_Utils_Array
::collect('entity_type', $entities));
1532 $this->add('text', $name, $label, array(
1533 'class' => 'crm-profile-selector',
1534 // Note: client treats ';;' as equivalent to \0, and ';;' works better in HTML
1535 'data-group-type' => CRM_Core_BAO_UFGroup
::encodeGroupType($allowCoreTypes, $allowSubTypes, ';;'),
1536 'data-entities' => json_encode($entities),
1538 'data-default' => $default,
1539 'data-usedfor' => json_encode($usedFor),
1546 * @param null $required
1547 * @param null $extra
1549 public function addCountry($id, $title, $required = NULL, $extra = NULL) {
1550 $this->addElement('select', $id, $title,
1552 '' => ts('- select -'),
1553 ) + CRM_Core_PseudoConstant
::country(), $extra
1556 $this->addRule($id, ts('Please select %1', array(1 => $title)), 'required');
1561 * @param string $name
1564 * @param $attributes
1565 * @param null $required
1566 * @param null $javascriptMethod
1568 public function addSelectOther($name, $label, $options, $attributes, $required = NULL, $javascriptMethod = NULL) {
1570 $this->addElement('select', $name . '_id', $label, $options, $javascriptMethod);
1573 $this->addRule($name . '_id', ts('Please select %1', array(1 => $label)), 'required');
1580 public function getRootTitle() {
1587 public function getCompleteTitle() {
1588 return $this->getRootTitle() . $this->getTitle();
1592 * @return CRM_Core_Smarty
1594 public static function &getTemplate() {
1595 return self
::$_template;
1599 * @param $elementName
1601 public function addUploadElement($elementName) {
1602 $uploadNames = $this->get('uploadNames');
1603 if (!$uploadNames) {
1604 $uploadNames = array();
1606 if (is_array($elementName)) {
1607 foreach ($elementName as $name) {
1608 if (!in_array($name, $uploadNames)) {
1609 $uploadNames[] = $name;
1614 if (!in_array($elementName, $uploadNames)) {
1615 $uploadNames[] = $elementName;
1618 $this->set('uploadNames', $uploadNames);
1620 $config = CRM_Core_Config
::singleton();
1621 if (!empty($uploadNames)) {
1622 $this->controller
->addUploadAction($config->customFileUploadDir
, $uploadNames);
1629 public function buttonType() {
1630 $uploadNames = $this->get('uploadNames');
1631 $buttonType = (is_array($uploadNames) && !empty($uploadNames)) ?
'upload' : 'next';
1632 $this->assign('buttonType', $buttonType);
1641 public function getVar($name) {
1642 return isset($this->$name) ?
$this->$name : NULL;
1649 public function setVar($name, $value) {
1650 $this->$name = $value;
1657 * // if you need time
1658 * $attributes = array(
1659 * 'addTime' => true,
1660 * 'formatType' => 'relative' or 'birth' etc check advanced date settings
1664 * @param string $name
1665 * Name of the element.
1666 * @param string $label
1667 * Label of the element.
1668 * @param bool $required
1670 * @param array $attributes
1673 public function addDate($name, $label, $required = FALSE, $attributes = NULL) {
1674 if (!empty($attributes['formatType'])) {
1675 // get actual format
1676 $params = array('name' => $attributes['formatType']);
1679 // cache date information
1681 $key = "dateFormat_" . str_replace(' ', '_', $attributes['formatType']);
1682 if (empty($dateFormat[$key])) {
1683 CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_PreferencesDate', $params, $values);
1684 $dateFormat[$key] = $values;
1687 $values = $dateFormat[$key];
1690 if ($values['date_format']) {
1691 $attributes['format'] = $values['date_format'];
1694 if (!empty($values['time_format'])) {
1695 $attributes['timeFormat'] = $values['time_format'];
1697 $attributes['startOffset'] = $values['start'];
1698 $attributes['endOffset'] = $values['end'];
1701 $config = CRM_Core_Config
::singleton();
1702 if (empty($attributes['format'])) {
1703 $attributes['format'] = $config->dateInputFormat
;
1706 if (!isset($attributes['startOffset'])) {
1707 $attributes['startOffset'] = 10;
1710 if (!isset($attributes['endOffset'])) {
1711 $attributes['endOffset'] = 10;
1714 $this->add('text', $name, $label, $attributes);
1716 if (!empty($attributes['addTime']) ||
!empty($attributes['timeFormat'])) {
1718 if (!isset($attributes['timeFormat'])) {
1719 $timeFormat = $config->timeInputFormat
;
1722 $timeFormat = $attributes['timeFormat'];
1725 // 1 - 12 hours and 2 - 24 hours, but for jquery widget it is 0 and 1 respectively
1727 $show24Hours = TRUE;
1728 if ($timeFormat == 1) {
1729 $show24Hours = FALSE;
1732 //CRM-6664 -we are having time element name
1733 //in either flat string or an array format.
1734 $elementName = $name . '_time';
1735 if (substr($name, -1) == ']') {
1736 $elementName = substr($name, 0, strlen($name) - 1) . '_time]';
1739 $this->add('text', $elementName, ts('Time'), array('timeFormat' => $show24Hours));
1744 $this->addRule($name, ts('Please select %1', array(1 => $label)), 'required');
1745 if (!empty($attributes['addTime']) && !empty($attributes['addTimeRequired'])) {
1746 $this->addRule($elementName, ts('Please enter a time.'), 'required');
1752 * Function that will add date and time.
1754 public function addDateTime($name, $label, $required = FALSE, $attributes = NULL) {
1755 $addTime = array('addTime' => TRUE);
1756 if (is_array($attributes)) {
1757 $attributes = array_merge($attributes, $addTime);
1760 $attributes = $addTime;
1763 $this->addDate($name, $label, $required, $attributes);
1767 * Add a currency and money element to the form.
1769 * @param string $name
1770 * @param string $label
1771 * @param bool $required
1772 * @param null $attributes
1773 * @param bool $addCurrency
1774 * @param string $currencyName
1775 * @param null $defaultCurrency
1776 * @param bool $freezeCurrency
1778 * @return \HTML_QuickForm_Element
1780 public function addMoney(
1785 $addCurrency = TRUE,
1786 $currencyName = 'currency',
1787 $defaultCurrency = NULL,
1788 $freezeCurrency = FALSE
1790 $element = $this->add('text', $name, $label, $attributes, $required);
1791 $this->addRule($name, ts('Please enter a valid amount.'), 'money');
1794 $ele = $this->addCurrency($currencyName, NULL, TRUE, $defaultCurrency, $freezeCurrency);
1801 * Add currency element to the form.
1803 public function addCurrency(
1807 $defaultCurrency = NULL,
1808 $freezeCurrency = FALSE
1810 $currencies = CRM_Core_OptionGroup
::values('currencies_enabled');
1811 $options = array('class' => 'crm-select2 eight');
1813 $currencies = array('' => '') +
$currencies;
1814 $options['placeholder'] = ts('- none -');
1816 $ele = $this->add('select', $name, $label, $currencies, $required, $options);
1817 if ($freezeCurrency) {
1820 if (!$defaultCurrency) {
1821 $config = CRM_Core_Config
::singleton();
1822 $defaultCurrency = $config->defaultCurrency
;
1824 $this->setDefaults(array($name => $defaultCurrency));
1828 * Create a single or multiple entity ref field.
1829 * @param string $name
1830 * @param string $label
1831 * @param array $props
1832 * Mix of html and widget properties, including:.
1833 * - select - params to give to select2 widget
1834 * - entity - defaults to contact
1835 * - create - can the user create a new entity on-the-fly?
1836 * Set to TRUE if entity is contact and you want the default profiles,
1837 * or pass in your own set of links. @see CRM_Core_BAO_UFGroup::getCreateLinks for format
1838 * note that permissions are checked automatically
1839 * - api - array of settings for the getlist api wrapper
1840 * note that it accepts a 'params' setting which will be passed to the underlying api
1841 * - placeholder - string
1843 * - class, etc. - other html properties
1844 * @param bool $required
1846 * @return HTML_QuickForm_Element
1848 public function addEntityRef($name, $label = '', $props = array(), $required = FALSE) {
1849 require_once "api/api.php";
1850 $config = CRM_Core_Config
::singleton();
1851 // Default properties
1852 $props['api'] = CRM_Utils_Array
::value('api', $props, array());
1853 $props['entity'] = _civicrm_api_get_entity_name_from_camel(CRM_Utils_Array
::value('entity', $props, 'contact'));
1854 $props['class'] = ltrim(CRM_Utils_Array
::value('class', $props, '') . ' crm-form-entityref');
1856 if ($props['entity'] == 'contact' && isset($props['create']) && !(CRM_Core_Permission
::check('edit all contacts') || CRM_Core_Permission
::check('add contacts'))) {
1857 unset($props['create']);
1860 $props['placeholder'] = CRM_Utils_Array
::value('placeholder', $props, $required ?
ts('- select %1 -', array(1 => ts(str_replace('_', ' ', $props['entity'])))) : ts('- none -'));
1862 $defaults = array();
1863 if (!empty($props['multiple'])) {
1864 $defaults['multiple'] = TRUE;
1866 $props['select'] = CRM_Utils_Array
::value('select', $props, array()) +
$defaults;
1868 $this->formatReferenceFieldAttributes($props);
1869 return $this->add('text', $name, $label, $props, $required);
1875 private function formatReferenceFieldAttributes(&$props) {
1876 $props['data-select-params'] = json_encode($props['select']);
1877 $props['data-api-params'] = $props['api'] ?
json_encode($props['api']) : NULL;
1878 $props['data-api-entity'] = $props['entity'];
1879 if (!empty($props['create'])) {
1880 $props['data-create-links'] = json_encode($props['create']);
1882 CRM_Utils_Array
::remove($props, 'multiple', 'select', 'api', 'entity', 'create');
1886 * Convert all date fields within the params to mysql date ready for the
1887 * BAO layer. In this case fields are checked against the $_datefields defined for the form
1888 * and if time is defined it is incorporated
1890 * @param array $params
1891 * Input params from the form.
1893 * @todo it would probably be better to work on $this->_params than a passed array
1894 * @todo standardise the format which dates are passed to the BAO layer in & remove date
1897 public function convertDateFieldsToMySQL(&$params) {
1898 foreach ($this->_dateFields
as $fieldName => $specs) {
1899 if (!empty($params[$fieldName])) {
1900 $params[$fieldName] = CRM_Utils_Date
::isoToMysql(
1901 CRM_Utils_Date
::processDate(
1902 $params[$fieldName],
1903 CRM_Utils_Array
::value("{$fieldName}_time", $params), TRUE)
1907 if (isset($specs['default'])) {
1908 $params[$fieldName] = date('YmdHis', strtotime($specs['default']));
1915 * @param $elementName
1917 public function removeFileRequiredRules($elementName) {
1918 $this->_required
= array_diff($this->_required
, array($elementName));
1919 if (isset($this->_rules
[$elementName])) {
1920 foreach ($this->_rules
[$elementName] as $index => $ruleInfo) {
1921 if ($ruleInfo['type'] == 'uploadedfile') {
1922 unset($this->_rules
[$elementName][$index]);
1925 if (empty($this->_rules
[$elementName])) {
1926 unset($this->_rules
[$elementName]);
1932 * Function that can be defined in Form to override or.
1933 * perform specific action on cancel action
1935 public function cancelAction() {
1939 * Helper function to verify that required fields have been filled.
1941 * Typically called within the scope of a FormRule function
1943 * @param array $fields
1944 * @param array $values
1945 * @param array $errors
1947 public static function validateMandatoryFields($fields, $values, &$errors) {
1948 foreach ($fields as $name => $fld) {
1949 if (!empty($fld['is_required']) && CRM_Utils_System
::isNull(CRM_Utils_Array
::value($name, $values))) {
1950 $errors[$name] = ts('%1 is a required field.', array(1 => $fld['title']));
1956 * Get contact if for a form object. Prioritise
1957 * - cid in URL if 0 (on behalf on someoneelse)
1958 * (@todo consider setting a variable if onbehalf for clarity of downstream 'if's
1959 * - logged in user id if it matches the one in the cid in the URL
1960 * - contact id validated from a checksum from a checksum
1961 * - cid from the url if the caller has ACL permission to view
1962 * - fallback is logged in user (or ? NULL if no logged in user) (@todo wouldn't 0 be more intuitive?)
1966 protected function setContactID() {
1967 $tempID = CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
1968 if (isset($this->_params
) && isset($this->_params
['select_contact_id'])) {
1969 $tempID = $this->_params
['select_contact_id'];
1971 if (isset($this->_params
, $this->_params
[0]) && !empty($this->_params
[0]['select_contact_id'])) {
1972 // event form stores as an indexed array, contribution form not so much...
1973 $tempID = $this->_params
[0]['select_contact_id'];
1976 // force to ignore the authenticated user
1977 if ($tempID === '0' ||
$tempID === 0) {
1978 // we set the cid on the form so that this will be retained for the Confirm page
1979 // in the multi-page form & prevent us returning the $userID when this is called
1981 // we don't really need to set it when $tempID is set because the params have that stored
1982 $this->set('cid', 0);
1983 CRM_Core_Resources
::singleton()->addVars('coreForm', array('contact_id' => (int) $tempID));
1984 return (int) $tempID;
1987 $userID = $this->getLoggedInUserContactID();
1989 if (!is_null($tempID) && $tempID === $userID) {
1990 CRM_Core_Resources
::singleton()->addVars('coreForm', array('contact_id' => (int) $tempID));
1991 return (int) $userID;
1994 //check if this is a checksum authentication
1995 $userChecksum = CRM_Utils_Request
::retrieve('cs', 'String', $this);
1996 if ($userChecksum) {
1997 //check for anonymous user.
1998 $validUser = CRM_Contact_BAO_Contact_Utils
::validChecksum($tempID, $userChecksum);
2000 CRM_Core_Resources
::singleton()->addVars('coreForm', array('contact_id' => (int) $tempID));
2001 CRM_Core_Resources
::singleton()->addVars('coreForm', array('checksum' => (int) $tempID));
2005 // check if user has permission, CRM-12062
2006 elseif ($tempID && CRM_Contact_BAO_Contact_Permission
::allow($tempID)) {
2007 CRM_Core_Resources
::singleton()->addVars('coreForm', array('contact_id' => (int) $tempID));
2010 if (is_numeric($userID)) {
2011 CRM_Core_Resources
::singleton()->addVars('coreForm', array('contact_id' => (int) $userID));
2013 return is_numeric($userID) ?
$userID : NULL;
2017 * Get the contact id that the form is being submitted for.
2021 public function getContactID() {
2022 return $this->setContactID();
2026 * Get the contact id of the logged in user.
2028 public function getLoggedInUserContactID() {
2029 // check if the user is logged in and has a contact ID
2030 $session = CRM_Core_Session
::singleton();
2031 return $session->get('userID');
2035 * Add autoselector field -if user has permission to view contacts
2036 * If adding this to a form you also need to add to the tpl e.g
2038 * {if !empty($selectable)}
2039 * <div class="crm-summary-row">
2040 * <div class="crm-label">{$form.select_contact.label}</div>
2041 * <div class="crm-content">
2042 * {$form.select_contact.html}
2047 * @param array $profiles
2048 * Ids of profiles that are on the form (to be autofilled).
2049 * @param array $autoCompleteField
2053 * - url (for ajax lookup)
2055 * @todo add data attributes so we can deal with multiple instances on a form
2057 public function addAutoSelector($profiles = array(), $autoCompleteField = array()) {
2058 $autoCompleteField = array_merge(array(
2059 'id_field' => 'select_contact_id',
2060 'placeholder' => ts('Select someone else ...'),
2061 'show_hide' => TRUE,
2062 'api' => array('params' => array('contact_type' => 'Individual')),
2063 ), $autoCompleteField);
2065 if ($this->canUseAjaxContactLookups()) {
2066 $this->assign('selectable', $autoCompleteField['id_field']);
2067 $this->addEntityRef($autoCompleteField['id_field'], NULL, array(
2068 'placeholder' => $autoCompleteField['placeholder'],
2069 'api' => $autoCompleteField['api'],
2072 CRM_Core_Resources
::singleton()->addScriptFile('civicrm', 'js/AlternateContactSelector.js', 1, 'html-header')
2074 'form' => array('autocompletes' => $autoCompleteField),
2075 'ids' => array('profile' => $profiles),
2082 public function canUseAjaxContactLookups() {
2083 if (0 < (civicrm_api3('contact', 'getcount', array('check_permissions' => 1))) &&
2084 CRM_Core_Permission
::check(array(array('access AJAX API', 'access CiviCRM')))
2091 * Add the options appropriate to cid = zero - ie. autocomplete
2093 * @todo there is considerable code duplication between the contribution forms & event forms. It is apparent
2094 * that small pieces of duplication are not being refactored into separate functions because their only shared parent
2095 * is this form. Inserting a class FrontEndForm.php between the contribution & event & this class would allow functions like this
2096 * and a dozen other small ones to be refactored into a shared parent with the reduction of much code duplication
2098 * @param $onlinePaymentProcessorEnabled
2100 public function addCIDZeroOptions($onlinePaymentProcessorEnabled) {
2101 $this->assign('nocid', TRUE);
2102 $profiles = array();
2103 if ($this->_values
['custom_pre_id']) {
2104 $profiles[] = $this->_values
['custom_pre_id'];
2106 if ($this->_values
['custom_post_id']) {
2107 $profiles = array_merge($profiles, (array) $this->_values
['custom_post_id']);
2109 if ($onlinePaymentProcessorEnabled) {
2110 $profiles[] = 'billing';
2112 if (!empty($this->_values
)) {
2113 $this->addAutoSelector($profiles);
2118 * Set default values on form for given contact (or no contact defaults)
2120 * @param mixed $profile_id
2121 * (can be id, or profile name).
2122 * @param int $contactID
2126 public function getProfileDefaults($profile_id = 'Billing', $contactID = NULL) {
2128 $defaults = civicrm_api3('profile', 'getsingle', array(
2129 'profile_id' => (array) $profile_id,
2130 'contact_id' => $contactID,
2134 catch (Exception
$e) {
2135 // the try catch block gives us silent failure -not 100% sure this is a good idea
2136 // as silent failures are often worse than noisy ones
2142 * Sets form attribute.
2145 public function preventAjaxSubmit() {
2146 $this->setAttribute('data-no-ajax-submit', 'true');
2150 * Sets form attribute.
2153 public function allowAjaxSubmit() {
2154 $this->removeAttribute('data-no-ajax-submit');
2158 * Sets page title based on entity and action.
2159 * @param string $entityLabel
2161 public function setPageTitle($entityLabel) {
2162 switch ($this->_action
) {
2163 case CRM_Core_Action
::ADD
:
2164 CRM_Utils_System
::setTitle(ts('New %1', array(1 => $entityLabel)));
2167 case CRM_Core_Action
::UPDATE
:
2168 CRM_Utils_System
::setTitle(ts('Edit %1', array(1 => $entityLabel)));
2171 case CRM_Core_Action
::VIEW
:
2172 case CRM_Core_Action
::PREVIEW
:
2173 CRM_Utils_System
::setTitle(ts('View %1', array(1 => $entityLabel)));
2176 case CRM_Core_Action
::DELETE
:
2177 CRM_Utils_System
::setTitle(ts('Delete %1', array(1 => $entityLabel)));
2183 * Create a chain-select target field. All settings are optional; the defaults usually work.
2185 * @param string $elementName
2186 * @param array $settings
2188 * @return HTML_QuickForm_Element
2190 public function addChainSelect($elementName, $settings = array()) {
2191 $props = $settings +
= array(
2192 'control_field' => str_replace(array('state_province', 'StateProvince', 'county', 'County'), array(
2198 'data-callback' => strpos($elementName, 'rovince') ?
'civicrm/ajax/jqState' : 'civicrm/ajax/jqCounty',
2199 'label' => strpos($elementName, 'rovince') ?
ts('State/Province') : ts('County'),
2200 'data-empty-prompt' => strpos($elementName, 'rovince') ?
ts('Choose country first') : ts('Choose state first'),
2201 'data-none-prompt' => ts('- N/A -'),
2202 'multiple' => FALSE,
2203 'required' => FALSE,
2204 'placeholder' => empty($settings['required']) ?
ts('- none -') : ts('- select -'),
2206 CRM_Utils_Array
::remove($props, 'label', 'required', 'control_field');
2207 $props['class'] = (empty($props['class']) ?
'' : "{$props['class']} ") . 'crm-select2';
2208 $props['data-select-prompt'] = $props['placeholder'];
2209 $props['data-name'] = $elementName;
2211 $this->_chainSelectFields
[$settings['control_field']] = $elementName;
2213 // Passing NULL instead of an array of options
2214 // CRM-15225 - normally QF will reject any selected values that are not part of the field's options, but due to a
2215 // quirk in our patched version of HTML_QuickForm_select, this doesn't happen if the options are NULL
2216 // which seems a bit dirty but it allows our dynamically-popuplated select element to function as expected.
2217 return $this->add('select', $elementName, $settings['label'], NULL, $settings['required'], $props);
2221 * Set options and attributes for chain select fields based on the controlling field's value
2223 private function preProcessChainSelectFields() {
2224 foreach ($this->_chainSelectFields
as $control => $target) {
2225 // The 'target' might get missing if extensions do removeElement() in a form hook.
2226 if ($this->elementExists($target)) {
2227 $targetField = $this->getElement($target);
2228 $targetType = $targetField->getAttribute('data-callback') == 'civicrm/ajax/jqCounty' ?
'county' : 'stateProvince';
2230 // If the control field is on the form, setup chain-select and dynamically populate options
2231 if ($this->elementExists($control)) {
2232 $controlField = $this->getElement($control);
2233 $controlType = $targetType == 'county' ?
'stateProvince' : 'country';
2235 $targetField->setAttribute('class', $targetField->getAttribute('class') . ' crm-chain-select-target');
2237 $css = (string) $controlField->getAttribute('class');
2238 $controlField->updateAttributes(array(
2239 'class' => ($css ?
"$css " : 'crm-select2 ') . 'crm-chain-select-control',
2240 'data-target' => $target,
2242 $controlValue = $controlField->getValue();
2243 if ($controlValue) {
2244 $options = CRM_Core_BAO_Location
::getChainSelectValues($controlValue, $controlType, TRUE);
2246 $targetField->setAttribute('placeholder', $targetField->getAttribute('data-none-prompt'));
2250 $targetField->setAttribute('placeholder', $targetField->getAttribute('data-empty-prompt'));
2251 $targetField->setAttribute('disabled', 'disabled');
2254 // Control field not present - fall back to loading default options
2256 $options = CRM_Core_PseudoConstant
::$targetType();
2258 if (!$targetField->getAttribute('multiple')) {
2259 $options = array('' => $targetField->getAttribute('placeholder')) +
$options;
2260 $targetField->removeAttribute('placeholder');
2262 $targetField->_options
= array();
2263 $targetField->loadArray($options);
2269 * Validate country / state / county match and suppress unwanted "required" errors
2271 private function validateChainSelectFields() {
2272 foreach ($this->_chainSelectFields
as $control => $target) {
2273 if ($this->elementExists($control) && $this->elementExists($target)) {
2274 $controlValue = (array) $this->getElementValue($control);
2275 $targetField = $this->getElement($target);
2276 $controlType = $targetField->getAttribute('data-callback') == 'civicrm/ajax/jqCounty' ?
'stateProvince' : 'country';
2277 $targetValue = array_filter((array) $targetField->getValue());
2278 if ($targetValue ||
$this->getElementError($target)) {
2279 $options = CRM_Core_BAO_Location
::getChainSelectValues($controlValue, $controlType, TRUE);
2281 if (!array_intersect($targetValue, array_keys($options))) {
2282 $this->setElementError($target, $controlType == 'country' ?
ts('State/Province does not match the selected Country') : ts('County does not match the selected State/Province'));
2284 } // Suppress "required" error for field if it has no options
2285 elseif (!$options) {
2286 $this->setElementError($target, NULL);