3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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-2016
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 * (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 = array();
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
166 protected $unsavedChangesWarn;
169 * What to return to the client if in ajax mode (snippet=json)
173 public $ajaxResponse = array();
176 * Url path used to reach this page
180 public $urlPath = array();
183 * @var CRM_Core_Controller
188 * Constants for attributes for various form elements
189 * attempt to standardize on the number of variations that we
190 * use of the below form elements
194 const ATTR_SPACING
= ' ';
197 * All checkboxes are defined with a common prefix. This allows us to
198 * have the same javascript to check / clear all the checkboxes etc
199 * If u have multiple groups of checkboxes, you will need to give them different
200 * ids to avoid potential name collision
204 const CB_PREFIX
= 'mark_x_', CB_PREFIY
= 'mark_y_', CB_PREFIZ
= 'mark_z_', CB_PREFIX_LEN
= 7;
207 * @internal to keep track of chain-select fields
210 private $_chainSelectFields = array();
213 * Extra input types we support via the "add" method
216 public static $html5Types = array(
223 * Constructor for the basic form page.
225 * We should not use QuickForm directly. This class provides a lot
226 * of default convenient functions, rules and buttons
228 * @param object $state
229 * State associated with this form.
230 * @param \const|\enum|int $action The mode the form is operating in (None/Create/View/Update/Delete)
231 * @param string $method
232 * The type of http method used (GET/POST).
233 * @param string $name
234 * The name of the form if different from class name.
236 * @return \CRM_Core_Form
238 public function __construct(
240 $action = CRM_Core_Action
::NONE
,
246 $this->_name
= $name;
249 // CRM-15153 - FIXME this name translates to a DOM id and is not always unique!
250 $this->_name
= CRM_Utils_String
::getClassName(CRM_Utils_System
::getClassName($this));
253 parent
::__construct($this->_name
, $method);
255 $this->_state
=& $state;
257 $this->_state
->setName($this->_name
);
259 $this->_action
= (int) $action;
261 $this->registerRules();
263 // let the constructor initialize this, should happen only once
264 if (!isset(self
::$_template)) {
265 self
::$_template = CRM_Core_Smarty
::singleton();
267 // Workaround for CRM-15153 - give each form a reasonably unique css class
268 $this->addClass(CRM_Utils_System
::getClassName($this));
270 $this->assign('snippet', CRM_Utils_Array
::value('snippet', $_GET));
274 * Generate ID for some reason & purpose that is unknown & undocumented.
276 public static function generateID() {
280 * Add one or more css classes to the form.
282 * @param string $className
284 public function addClass($className) {
285 $classes = $this->getAttribute('class');
286 $this->setAttribute('class', ($classes ?
"$classes " : '') . $className);
290 * Register all the standard rules that most forms potentially use.
292 public function registerRules() {
293 static $rules = array(
322 foreach ($rules as $rule) {
323 $this->registerRule($rule, 'callback', $rule, 'CRM_Utils_Rule');
328 * Simple easy to use wrapper around addElement.
330 * Deal with simple validation rules.
332 * @param string $type
333 * @param string $name
334 * @param string $label
335 * @param string|array $attributes (options for select elements)
336 * @param bool $required
337 * @param array $extra
338 * (attributes for select elements).
340 * @return HTML_QuickForm_Element
341 * Could be an error object
343 public function &add(
344 $type, $name, $label = '',
345 $attributes = '', $required = FALSE, $extra = NULL
347 // Fudge some extra types that quickform doesn't support
348 if ($type == 'wysiwyg' ||
in_array($type, self
::$html5Types)) {
349 $attributes = ($attributes ?
$attributes : array()) +
array('class' => '');
350 $attributes['class'] = ltrim($attributes['class'] . " crm-form-$type");
351 $type = $type == 'wysiwyg' ?
'textarea' : 'text';
353 // @see http://wiki.civicrm.org/confluence/display/CRMDOC/crmDatepicker
354 if ($type == 'datepicker') {
355 $attributes = ($attributes ?
$attributes : array());
356 $attributes['data-crm-datepicker'] = json_encode((array) $extra);
359 if ($type == 'select' && is_array($extra)) {
360 // Normalize this property
361 if (!empty($extra['multiple'])) {
362 $extra['multiple'] = 'multiple';
365 unset($extra['multiple']);
367 unset($extra['size'], $extra['maxlength']);
368 // Add placeholder option for select
369 if (isset($extra['placeholder'])) {
370 if ($extra['placeholder'] === TRUE) {
371 $extra['placeholder'] = $required ?
ts('- select -') : ts('- none -');
373 if (($extra['placeholder'] ||
$extra['placeholder'] === '') && empty($extra['multiple']) && is_array($attributes) && !isset($attributes[''])) {
374 $attributes = array('' => $extra['placeholder']) +
$attributes;
378 $element = $this->addElement($type, $name, $label, $attributes, $extra);
379 if (HTML_QuickForm
::isError($element)) {
380 CRM_Core_Error
::fatal(HTML_QuickForm
::errorMessage($element));
384 if ($type == 'file') {
385 $error = $this->addRule($name, ts('%1 is a required field.', array(1 => $label)), 'uploadedfile');
388 $error = $this->addRule($name, ts('%1 is a required field.', array(1 => $label)), 'required');
390 if (HTML_QuickForm
::isError($error)) {
391 CRM_Core_Error
::fatal(HTML_QuickForm
::errorMessage($element));
401 * This is called before buildForm. Any pre-processing that
402 * needs to be done for buildForm should be done here.
404 * This is a virtual function and should be redefined if needed.
406 public function preProcess() {
410 * Called after the form is validated.
412 * Any processing of form state etc should be done in this function.
413 * Typically all processing associated with a form should be done
414 * here and relevant state should be stored in the session
416 * This is a virtual function and should be redefined if needed
418 public function postProcess() {
422 * Main process wrapper.
424 * Implemented so that we can call all the hook functions.
426 * @param bool $allowAjax
427 * FIXME: This feels kind of hackish, ideally we would take the json-related code from this function.
428 * and bury it deeper down in the controller
430 public function mainProcess($allowAjax = TRUE) {
431 $this->postProcess();
432 $this->postProcessHook();
434 // Respond with JSON if in AJAX context (also support legacy value '6')
435 if ($allowAjax && !empty($_REQUEST['snippet']) && in_array($_REQUEST['snippet'], array(
436 CRM_Core_Smarty
::PRINT_JSON
,
440 $this->ajaxResponse
['buttonName'] = str_replace('_qf_' . $this->getAttribute('id') . '_', '', $this->controller
->getButtonName());
441 $this->ajaxResponse
['action'] = $this->_action
;
442 if (isset($this->_id
) ||
isset($this->id
)) {
443 $this->ajaxResponse
['id'] = isset($this->id
) ?
$this->id
: $this->_id
;
445 CRM_Core_Page_AJAX
::returnJsonResponse($this->ajaxResponse
);
450 * The postProcess hook is typically called by the framework.
452 * However in a few cases, the form exits or redirects early in which
453 * case it needs to call this function so other modules can do the needful
454 * Calling this function directly should be avoided if possible. In general a
455 * better way is to do setUserContext so the framework does the redirect
457 public function postProcessHook() {
458 CRM_Utils_Hook
::postProcess(get_class($this), $this);
462 * This virtual function is used to build the form.
464 * It replaces the buildForm associated with QuickForm_Page. This allows us to put
465 * preProcess in front of the actual form building routine
467 public function buildQuickForm() {
471 * This virtual function is used to set the default values of various form elements.
474 * reference to the array of default values
476 public function setDefaultValues() {
481 * This is a virtual function that adds group and global rules to the form.
483 * Keeping it distinct from the form to keep code small
484 * and localized in the form building code
486 public function addRules() {
490 * Performs the server side validation.
493 * true if no error found
494 * @throws HTML_QuickForm_Error
496 public function validate() {
497 $error = parent
::validate();
499 $this->validateChainSelectFields();
501 $hookErrors = array();
503 CRM_Utils_Hook
::validateForm(
505 $this->_submitValues
,
511 if (!empty($hookErrors)) {
512 $this->_errors +
= $hookErrors;
515 return (0 == count($this->_errors
));
519 * Core function that builds the form.
521 * We redefine this function here and expect all CRM forms to build their form in the function
524 public function buildForm() {
525 $this->_formBuilt
= TRUE;
529 CRM_Utils_Hook
::preProcess(get_class($this), $this);
531 $this->assign('translatePermission', CRM_Core_Permission
::check('translate CiviCRM'));
534 $this->controller
->_key
&&
535 $this->controller
->_generateQFKey
537 $this->addElement('hidden', 'qfKey', $this->controller
->_key
);
538 $this->assign('qfKey', $this->controller
->_key
);
542 // _generateQFKey suppresses the qfKey generation on form snippets that
543 // are part of other forms, hence we use that to avoid adding entryURL
544 if ($this->controller
->_generateQFKey
&& $this->controller
->_entryURL
) {
545 $this->addElement('hidden', 'entryURL', $this->controller
->_entryURL
);
548 $this->buildQuickForm();
550 $defaults = $this->setDefaultValues();
551 unset($defaults['qfKey']);
553 if (!empty($defaults)) {
554 $this->setDefaults($defaults);
557 // call the form hook
558 // also call the hook function so any modules can set their own custom defaults
559 // the user can do both the form and set default values with this hook
560 CRM_Utils_Hook
::buildForm(get_class($this), $this);
564 //Set html data-attribute to enable warning user of unsaved changes
565 if ($this->unsavedChangesWarn
=== TRUE
566 ||
(!isset($this->unsavedChangesWarn
)
567 && ($this->_action
& CRM_Core_Action
::ADD ||
$this->_action
& CRM_Core_Action
::UPDATE
)
570 $this->setAttribute('data-warn-changes', 'true');
575 * Add default Next / Back buttons.
577 * @param array $params
578 * Array of associative arrays in the order in which the buttons should be
579 * displayed. The associate array has 3 fields: 'type', 'name' and 'isDefault'
580 * The base form class will define a bunch of static arrays for commonly used
583 public function addButtons($params) {
584 $prevnext = $spacing = array();
585 foreach ($params as $button) {
586 $attrs = array('class' => 'crm-form-submit') +
(array) CRM_Utils_Array
::value('js', $button);
588 if (!empty($button['class'])) {
589 $attrs['class'] .= ' ' . $button['class'];
592 if (!empty($button['isDefault'])) {
593 $attrs['class'] .= ' default';
596 if (in_array($button['type'], array('upload', 'next', 'submit', 'done', 'process', 'refresh'))) {
597 $attrs['class'] .= ' validate';
598 $defaultIcon = 'fa-check';
601 $attrs['class'] .= ' cancel';
602 $defaultIcon = $button['type'] == 'back' ?
'fa-chevron-left' : 'fa-times';
605 if ($button['type'] === 'reset') {
606 $prevnext[] = $this->createElement($button['type'], 'reset', $button['name'], $attrs);
609 if (!empty($button['subName'])) {
610 if ($button['subName'] == 'new') {
611 $defaultIcon = 'fa-plus-circle';
613 if ($button['subName'] == 'done') {
614 $defaultIcon = 'fa-check-circle';
616 if ($button['subName'] == 'next') {
617 $defaultIcon = 'fa-chevron-right';
621 if (in_array($button['type'], array('next', 'upload', 'done')) && $button['name'] === ts('Save')) {
622 $attrs['accesskey'] = 'S';
624 $icon = CRM_Utils_Array
::value('icon', $button, $defaultIcon);
626 $attrs['crm-icon'] = $icon;
628 $buttonName = $this->getButtonName($button['type'], CRM_Utils_Array
::value('subName', $button));
629 $prevnext[] = $this->createElement('submit', $buttonName, $button['name'], $attrs);
631 if (!empty($button['isDefault'])) {
632 $this->setDefaultAction($button['type']);
635 // if button type is upload, set the enctype
636 if ($button['type'] == 'upload') {
637 $this->updateAttributes(array('enctype' => 'multipart/form-data'));
638 $this->setMaxFileSize();
641 // hack - addGroup uses an array to express variable spacing, read from the last element
642 $spacing[] = CRM_Utils_Array
::value('spacing', $button, self
::ATTR_SPACING
);
644 $this->addGroup($prevnext, 'buttons', '', $spacing, FALSE);
648 * Getter function for Name.
652 public function getName() {
657 * Getter function for State.
661 public function &getState() {
662 return $this->_state
;
666 * Getter function for StateType.
670 public function getStateType() {
671 return $this->_state
->getType();
675 * Getter function for title.
677 * Should be over-ridden by derived class.
681 public function getTitle() {
682 return $this->_title ?
$this->_title
: ts('ERROR: Title is not Set');
686 * Setter function for title.
688 * @param string $title
689 * The title of the form.
691 public function setTitle($title) {
692 $this->_title
= $title;
696 * Assign billing type id to bltID.
698 * @throws CRM_Core_Exception
700 public function assignBillingType() {
701 $this->_bltID
= CRM_Core_BAO_LocationType
::getBilling();
702 $this->set('bltID', $this->_bltID
);
703 $this->assign('bltID', $this->_bltID
);
707 * This if a front end form function for setting the payment processor.
709 * It would be good to sync it with the back-end function on abstractEditPayment & use one everywhere.
711 * @param bool $is_pay_later_enabled
713 * @throws \CRM_Core_Exception
715 protected function assignPaymentProcessor($is_pay_later_enabled) {
716 $this->_paymentProcessors
= CRM_Financial_BAO_PaymentProcessor
::getPaymentProcessors(
717 array(ucfirst($this->_mode
) . 'Mode'),
718 $this->_paymentProcessorIDs
721 if (!empty($this->_paymentProcessors
)) {
722 if ($is_pay_later_enabled) {
723 $this->_paymentProcessors
[0] = CRM_Financial_BAO_PaymentProcessor
::getPayment(0);
725 foreach ($this->_paymentProcessors
as $paymentProcessorID => $paymentProcessorDetail) {
726 if (empty($this->_paymentProcessor
) && $paymentProcessorDetail['is_default'] == 1 ||
(count($this->_paymentProcessors
) == 1)
728 $this->_paymentProcessor
= $paymentProcessorDetail;
729 $this->assign('paymentProcessor', $this->_paymentProcessor
);
730 // Setting this is a bit of a legacy overhang.
731 $this->_paymentObject
= $paymentProcessorDetail['object'];
734 // It's not clear why we set this on the form.
735 $this->set('paymentProcessors', $this->_paymentProcessors
);
738 throw new CRM_Core_Exception(ts('A payment processor configured for this page might be disabled (contact the site administrator for assistance).'));
744 * Format the fields for the payment processor.
746 * In order to pass fields to the payment processor in a consistent way we add some renamed
749 * @param array $fields
753 protected function formatParamsForPaymentProcessor($fields) {
754 // also add location name to the array
755 $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
);
756 $this->_params
["address_name-{$this->_bltID}"] = trim($this->_params
["address_name-{$this->_bltID}"]);
757 // Add additional parameters that the payment processors are used to receiving.
758 if (!empty($this->_params
["billing_state_province_id-{$this->_bltID}"])) {
759 $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}"]);
761 if (!empty($this->_params
["billing_country_id-{$this->_bltID}"])) {
762 $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}"]);
765 list($hasAddressField, $addressParams) = CRM_Contribute_BAO_Contribution
::getPaymentProcessorReadyAddressParams($this->_params
, $this->_bltID
);
766 if ($hasAddressField) {
767 $this->_params
= array_merge($this->_params
, $addressParams);
770 $nameFields = array('first_name', 'middle_name', 'last_name');
771 foreach ($nameFields as $name) {
773 if (array_key_exists("billing_$name", $this->_params
)) {
774 $this->_params
[$name] = $this->_params
["billing_{$name}"];
775 $this->_params
['preserveDBName'] = TRUE;
782 * Handle Payment Processor switching for contribution and event registration forms.
784 * This function is shared between contribution & event forms & this is their common class.
786 * However, this should be seen as an in-progress refactor, the end goal being to also align the
787 * backoffice forms that action payments.
789 * This function overlaps assignPaymentProcessor, in a bad way.
791 protected function preProcessPaymentOptions() {
792 $this->_paymentProcessorID
= NULL;
793 if ($this->_paymentProcessors
) {
794 if (!empty($this->_submitValues
)) {
795 $this->_paymentProcessorID
= CRM_Utils_Array
::value('payment_processor_id', $this->_submitValues
);
796 $this->_paymentProcessor
= CRM_Utils_Array
::value($this->_paymentProcessorID
, $this->_paymentProcessors
);
797 $this->set('type', $this->_paymentProcessorID
);
798 $this->set('mode', $this->_mode
);
799 $this->set('paymentProcessor', $this->_paymentProcessor
);
801 // Set default payment processor
803 foreach ($this->_paymentProcessors
as $values) {
804 if (!empty($values['is_default']) ||
count($this->_paymentProcessors
) == 1) {
805 $this->_paymentProcessorID
= $values['id'];
810 if ($this->_paymentProcessorID
811 ||
(isset($this->_submitValues
['payment_processor_id']) && $this->_submitValues
['payment_processor_id'] == 0)
813 CRM_Core_Payment_ProcessorForm
::preProcess($this);
816 $this->_paymentProcessor
= array();
818 CRM_Financial_Form_Payment
::addCreditCardJs($this->_paymentProcessorID
);
820 $this->assign('paymentProcessorID', $this->_paymentProcessorID
);
821 // We save the fact that the profile 'billing' is required on the payment form.
822 // Currently pay-later is the only 'processor' that takes notice of this - but ideally
823 // 1) it would be possible to select the minimum_billing_profile_id for the contribution form
824 // 2) that profile_id would be set on the payment processor
825 // 3) the payment processor would return a billing form that combines these user-configured
826 // minimums with the payment processor minimums. This would lead to fields like 'postal_code'
827 // only being on the form if either the admin has configured it as wanted or the processor
829 $this->assign('billing_profile_id', (CRM_Utils_Array
::value('is_billing_required', $this->_values
) ?
'billing' : ''));
833 * Handle pre approval for processors.
835 * This fits with the flow where a pre-approval is done and then confirmed in the next stage when confirm is hit.
837 * This function is shared between contribution & event forms & this is their common class.
839 * However, this should be seen as an in-progress refactor, the end goal being to also align the
840 * backoffice forms that action payments.
842 * @param array $params
844 protected function handlePreApproval(&$params) {
846 $payment = Civi\Payment\System
::singleton()->getByProcessor($this->_paymentProcessor
);
847 $params['component'] = 'contribute';
848 $result = $payment->doPreApproval($params);
849 if (empty($result)) {
850 // This could happen, for example, when paypal looks at the button value & decides it is not paypal express.
854 catch (\Civi\Payment\Exception\PaymentProcessorException
$e) {
855 CRM_Core_Error
::statusBounce(ts('Payment approval failed with message :') . $e->getMessage(), $payment->getCancelUrl($params['qfKey'], CRM_Utils_Array
::value('participant_id', $params)));
858 $this->set('pre_approval_parameters', $result['pre_approval_parameters']);
859 if (!empty($result['redirect_url'])) {
860 CRM_Utils_System
::redirect($result['redirect_url']);
865 * Setter function for options.
867 * @param mixed $options
869 public function setOptions($options) {
870 $this->_options
= $options;
874 * Render form and return contents.
878 public function toSmarty() {
879 $this->preProcessChainSelectFields();
880 $renderer = $this->getRenderer();
881 $this->accept($renderer);
882 $content = $renderer->toArray();
883 $content['formName'] = $this->getName();
885 $content['formClass'] = CRM_Utils_System
::getClassName($this);
890 * Getter function for renderer.
892 * If renderer is not set create one and initialize it.
896 public function &getRenderer() {
897 if (!isset($this->_renderer
)) {
898 $this->_renderer
= CRM_Core_Form_Renderer
::singleton();
900 return $this->_renderer
;
904 * Use the form name to create the tpl file name.
908 public function getTemplateFileName() {
909 $ext = CRM_Extension_System
::singleton()->getMapper();
910 if ($ext->isExtensionClass(CRM_Utils_System
::getClassName($this))) {
911 $filename = $ext->getTemplateName(CRM_Utils_System
::getClassName($this));
912 $tplname = $ext->getTemplatePath(CRM_Utils_System
::getClassName($this)) . DIRECTORY_SEPARATOR
. $filename;
916 CRM_Utils_System
::getClassName($this),
918 '_' => DIRECTORY_SEPARATOR
,
919 '\\' => DIRECTORY_SEPARATOR
,
927 * A wrapper for getTemplateFileName.
929 * This includes calling the hook to prevent us from having to copy & paste the logic of calling the hook.
931 public function getHookedTemplateFileName() {
932 $pageTemplateFile = $this->getTemplateFileName();
933 CRM_Utils_Hook
::alterTemplateFile(get_class($this), $this, 'page', $pageTemplateFile);
934 return $pageTemplateFile;
938 * Default extra tpl file basically just replaces .tpl with .extra.tpl.
940 * i.e. we do not override.
944 public function overrideExtraTemplateFileName() {
949 * Error reporting mechanism.
951 * @param string $message
955 * @param CRM_Core_DAO $dao
956 * A data access object on which we perform a rollback if non - empty.
958 public function error($message, $code = NULL, $dao = NULL) {
960 $dao->query('ROLLBACK');
963 $error = CRM_Core_Error
::singleton();
965 $error->push($code, $message);
969 * Store the variable with the value in the form scope.
971 * @param string $name
972 * Name of the variable.
973 * @param mixed $value
974 * Value of the variable.
976 public function set($name, $value) {
977 $this->controller
->set($name, $value);
981 * Get the variable from the form scope.
983 * @param string $name
984 * Name of the variable
988 public function get($name) {
989 return $this->controller
->get($name);
997 public function getAction() {
998 return $this->_action
;
1002 * Setter for action.
1004 * @param int $action
1005 * The mode we want to set the form.
1007 public function setAction($action) {
1008 $this->_action
= $action;
1012 * Assign value to name in template.
1014 * @param string $var
1016 * @param mixed $value
1017 * Value of variable.
1019 public function assign($var, $value = NULL) {
1020 self
::$_template->assign($var, $value);
1024 * Assign value to name in template by reference.
1026 * @param string $var
1028 * @param mixed $value
1029 * Value of variable.
1031 public function assign_by_ref($var, &$value) {
1032 self
::$_template->assign_by_ref($var, $value);
1036 * Appends values to template variables.
1038 * @param array|string $tpl_var the template variable name(s)
1039 * @param mixed $value
1040 * The value to append.
1041 * @param bool $merge
1043 public function append($tpl_var, $value = NULL, $merge = FALSE) {
1044 self
::$_template->append($tpl_var, $value, $merge);
1048 * Returns an array containing template variables.
1050 * @param string $name
1054 public function get_template_vars($name = NULL) {
1055 return self
::$_template->get_template_vars($name);
1059 * @param string $name
1062 * @param array $attributes
1063 * @param null $separator
1064 * @param bool $required
1066 * @return HTML_QuickForm_group
1068 public function &addRadio($name, $title, $values, $attributes = array(), $separator = NULL, $required = FALSE) {
1070 $attributes = $attributes ?
$attributes : array();
1071 $allowClear = !empty($attributes['allowClear']);
1072 unset($attributes['allowClear']);
1073 $attributes['id_suffix'] = $name;
1074 foreach ($values as $key => $var) {
1075 $options[] = $this->createElement('radio', NULL, NULL, $var, $key, $attributes);
1077 $group = $this->addGroup($options, $name, $title, $separator);
1079 $this->addRule($name, ts('%1 is a required field.', array(1 => $title)), 'required');
1082 $group->setAttribute('allowClear', TRUE);
1090 * @param bool $allowClear
1091 * @param null $required
1092 * @param array $attributes
1094 public function addYesNo($id, $title, $allowClear = FALSE, $required = NULL, $attributes = array()) {
1095 $attributes +
= array('id_suffix' => $id);
1097 $choice[] = $this->createElement('radio', NULL, '11', ts('Yes'), '1', $attributes);
1098 $choice[] = $this->createElement('radio', NULL, '11', ts('No'), '0', $attributes);
1100 $group = $this->addGroup($choice, $id, $title);
1102 $group->setAttribute('allowClear', TRUE);
1105 $this->addRule($id, ts('%1 is a required field.', array(1 => $title)), 'required');
1113 * @param null $other
1114 * @param null $attributes
1115 * @param null $required
1116 * @param null $javascriptMethod
1117 * @param string $separator
1118 * @param bool $flipValues
1120 public function addCheckBox(
1121 $id, $title, $values, $other = NULL,
1122 $attributes = NULL, $required = NULL,
1123 $javascriptMethod = NULL,
1124 $separator = '<br />', $flipValues = FALSE
1128 if ($javascriptMethod) {
1129 foreach ($values as $key => $var) {
1131 $options[] = $this->createElement('checkbox', $var, NULL, $key, $javascriptMethod);
1134 $options[] = $this->createElement('checkbox', $key, NULL, $var, $javascriptMethod);
1139 foreach ($values as $key => $var) {
1141 $options[] = $this->createElement('checkbox', $var, NULL, $key);
1144 $options[] = $this->createElement('checkbox', $key, NULL, $var);
1149 $this->addGroup($options, $id, $title, $separator);
1152 $this->addElement('text', $id . '_other', ts('Other'), $attributes[$id . '_other']);
1157 ts('%1 is a required field.', array(1 => $title)),
1163 public function resetValues() {
1164 $data = $this->controller
->container();
1165 $data['values'][$this->_name
] = array();
1169 * Simple shell that derived classes can call to add buttons to
1170 * the form with a customized title for the main Submit
1172 * @param string $title
1173 * Title of the main button.
1174 * @param string $nextType
1175 * Button type for the form after processing.
1176 * @param string $backType
1177 * @param bool|string $submitOnce If true, add javascript to next button submit which prevents it from being clicked more than once
1179 public function addDefaultButtons($title, $nextType = 'next', $backType = 'back', $submitOnce = FALSE) {
1181 if ($backType != NULL) {
1183 'type' => $backType,
1184 'name' => ts('Previous'),
1187 if ($nextType != NULL) {
1188 $nextButton = array(
1189 'type' => $nextType,
1191 'isDefault' => TRUE,
1194 $nextButton['js'] = array('onclick' => "return submitOnce(this,'{$this->_name}','" . ts('Processing') . "');");
1196 $buttons[] = $nextButton;
1198 $this->addButtons($buttons);
1202 * @param string $name
1203 * @param string $from
1205 * @param string $label
1206 * @param string $dateFormat
1207 * @param bool $required
1208 * @param bool $displayTime
1210 public function addDateRange($name, $from = '_from', $to = '_to', $label = 'From:', $dateFormat = 'searchDate', $required = FALSE, $displayTime = FALSE) {
1212 $this->addDateTime($name . $from, $label, $required, array('formatType' => $dateFormat));
1213 $this->addDateTime($name . $to, ts('To:'), $required, array('formatType' => $dateFormat));
1216 $this->addDate($name . $from, $label, $required, array('formatType' => $dateFormat));
1217 $this->addDate($name . $to, ts('To:'), $required, array('formatType' => $dateFormat));
1222 * Based on form action, return a string representing the api action.
1223 * Used by addField method.
1227 private function getApiAction() {
1228 $action = $this->getAction();
1229 if ($action & (CRM_Core_Action
::UPDATE + CRM_Core_Action
::ADD
)) {
1232 if ($action & (CRM_Core_Action
::BROWSE + CRM_Core_Action
::BASIC + CRM_Core_Action
::ADVANCED + CRM_Core_Action
::PREVIEW
)) {
1235 // If you get this exception try adding more cases above.
1236 throw new Exception("Cannot determine api action for " . get_class($this) . '.' . 'CRM_Core_Action "' . CRM_Core_Action
::description($action) . '" not recognized.');
1240 * Classes extending CRM_Core_Form should implement this method.
1243 public function getDefaultEntity() {
1244 throw new Exception("Cannot determine default entity. " . get_class($this) . " should implement getDefaultEntity().");
1248 * Classes extending CRM_Core_Form should implement this method.
1250 * TODO: Merge with CRM_Core_DAO::buildOptionsContext($context) and add validation.
1253 public function getDefaultContext() {
1254 throw new Exception("Cannot determine default context. " . get_class($this) . " should implement getDefaultContext().");
1258 * Adds a select based on field metadata.
1259 * TODO: This could be even more generic and widget type (select in this case) could also be read from metadata
1260 * Perhaps a method like $form->bind($name) which would look up all metadata for named field
1262 * Field name to go on the form.
1263 * @param array $props
1264 * Mix of html attributes and special properties, namely.
1265 * - entity (api entity name, can usually be inferred automatically from the form class)
1266 * - field (field name - only needed if different from name used on the form)
1267 * - option_url - path to edit this option list - usually retrieved automatically - set to NULL to disable link
1268 * - placeholder - set to NULL to disable
1270 * - context - @see CRM_Core_DAO::buildOptionsContext
1271 * @param bool $required
1272 * @throws CRM_Core_Exception
1273 * @return HTML_QuickForm_Element
1275 public function addSelect($name, $props = array(), $required = FALSE) {
1276 if (!isset($props['entity'])) {
1277 $props['entity'] = $this->getDefaultEntity();
1279 if (!isset($props['field'])) {
1280 $props['field'] = strrpos($name, '[') ?
rtrim(substr($name, 1 +
strrpos($name, '[')), ']') : $name;
1282 if (!isset($props['context'])) {
1284 $props['context'] = $this->getDefaultContext();
1286 // This is not a required param, so we'll ignore if this doesn't exist.
1287 catch (Exception
$e) {}
1289 // Fetch options from the api unless passed explicitly
1290 if (isset($props['options'])) {
1291 $options = $props['options'];
1294 $info = civicrm_api3($props['entity'], 'getoptions', $props);
1295 $options = $info['values'];
1297 if (!array_key_exists('placeholder', $props)) {
1298 $props['placeholder'] = $required ?
ts('- select -') : CRM_Utils_Array
::value('context', $props) == 'search' ?
ts('- any -') : ts('- none -');
1300 // Handle custom field
1301 if (strpos($name, 'custom_') === 0 && is_numeric($name[7])) {
1302 list(, $id) = explode('_', $name);
1303 $label = isset($props['label']) ?
$props['label'] : CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomField', 'label', $id);
1304 $gid = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomField', 'option_group_id', $id);
1305 if (CRM_Utils_Array
::value('context', $props) != 'search') {
1306 $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);
1311 $info = civicrm_api3($props['entity'], 'getfields');
1312 foreach ($info['values'] as $uniqueName => $fieldSpec) {
1314 $uniqueName === $props['field'] ||
1315 CRM_Utils_Array
::value('name', $fieldSpec) === $props['field'] ||
1316 in_array($props['field'], CRM_Utils_Array
::value('api.aliases', $fieldSpec, array()))
1321 $label = isset($props['label']) ?
$props['label'] : $fieldSpec['title'];
1322 if (CRM_Utils_Array
::value('context', $props) != 'search') {
1323 $props['data-option-edit-path'] = array_key_exists('option_url', $props) ?
$props['option_url'] : CRM_Core_PseudoConstant
::getOptionEditUrl($fieldSpec);
1326 $props['class'] = (isset($props['class']) ?
$props['class'] . ' ' : '') . "crm-select2";
1327 $props['data-api-entity'] = $props['entity'];
1328 $props['data-api-field'] = $props['field'];
1329 CRM_Utils_Array
::remove($props, 'label', 'entity', 'field', 'option_url', 'options', 'context');
1330 return $this->add('select', $name, $label, $options, $required, $props);
1334 * Adds a field based on metadata.
1337 * Field name to go on the form.
1338 * @param array $props
1339 * Mix of html attributes and special properties, namely.
1340 * - entity (api entity name, can usually be inferred automatically from the form class)
1341 * - name (field name - only needed if different from name used on the form)
1342 * - option_url - path to edit this option list - usually retrieved automatically - set to NULL to disable link
1343 * - placeholder - set to NULL to disable
1345 * - context - @see CRM_Core_DAO::buildOptionsContext
1346 * @param bool $required
1347 * @throws \CiviCRM_API3_Exception
1348 * @throws \Exception
1349 * @return HTML_QuickForm_Element
1351 public function addField($name, $props = array(), $required = FALSE) {
1353 if (empty($props['context'])) {
1354 $props['context'] = $this->getDefaultContext();
1356 $context = $props['context'];
1358 if (empty($props['entity'])) {
1359 $props['entity'] = $this->getDefaultEntity();
1362 if (empty($props['name'])) {
1363 $props['name'] = strrpos($name, '[') ?
rtrim(substr($name, 1 +
strrpos($name, '[')), ']') : $name;
1366 if (empty($props['action'])) {
1367 $props['action'] = $this->getApiAction();
1370 // Handle custom fields
1371 if (strpos($name, 'custom_') === 0 && is_numeric($name[7])) {
1372 $fieldId = (int) substr($name, 7);
1373 return CRM_Core_BAO_CustomField
::addQuickFormElement($this, $name, $fieldId, $required, $context == 'search', CRM_Utils_Array
::value('label', $props));
1376 // Core field - get metadata.
1377 $fieldSpec = civicrm_api3($props['entity'], 'getfield', $props);
1378 $fieldSpec = $fieldSpec['values'];
1379 $label = CRM_Utils_Array
::value('label', $props, isset($fieldSpec['title']) ?
$fieldSpec['title'] : NULL);
1381 $widget = isset($props['type']) ?
$props['type'] : $fieldSpec['html']['type'];
1382 if ($widget == 'TextArea' && $context == 'search') {
1386 $isSelect = (in_array($widget, array(
1394 // Fetch options from the api unless passed explicitly.
1395 if (isset($props['options'])) {
1396 $options = $props['options'];
1399 $options = isset($fieldSpec['options']) ?
$fieldSpec['options'] : NULL;
1401 if ($context == 'search') {
1403 $props['multiple'] = CRM_Utils_Array
::value('multiple', $props, TRUE);
1406 // Add data for popup link.
1407 if ((!empty($props['option_url']) ||
!array_key_exists('option_url', $props)) && ($context != 'search' && $widget == 'Select' && CRM_Core_Permission
::check('administer CiviCRM'))) {
1408 $props['data-option-edit-path'] = !empty($props['option_url']) ?
$props['option_url'] : CRM_Core_PseudoConstant
::getOptionEditUrl($fieldSpec);
1409 $props['data-api-entity'] = $props['entity'];
1410 $props['data-api-field'] = $props['name'];
1413 $props +
= CRM_Utils_Array
::value('html', $fieldSpec, array());
1414 CRM_Utils_Array
::remove($props, 'entity', 'name', 'context', 'label', 'action', 'type', 'option_url', 'options');
1416 // TODO: refactor switch statement, to separate methods.
1422 //TODO: Autodetect ranges
1423 $props['size'] = isset($props['size']) ?
$props['size'] : 60;
1424 return $this->add(strtolower($widget), $name, $label, $props, $required);
1427 return $this->add('hidden', $name, NULL, $props, $required);
1430 //Set default columns and rows for textarea.
1431 $props['rows'] = isset($props['rows']) ?
$props['rows'] : 4;
1432 $props['cols'] = isset($props['cols']) ?
$props['cols'] : 60;
1433 return $this->add('textarea', $name, $label, $props, $required);
1436 //TODO: add range support
1437 //TODO: Add date formats
1438 //TODO: Add javascript template for dates.
1439 return $this->addDate($name, $label, $required, $props);
1442 $separator = isset($props['separator']) ?
$props['separator'] : NULL;
1443 unset($props['separator']);
1444 if (!isset($props['allowClear'])) {
1445 $props['allowClear'] = !$required;
1447 return $this->addRadio($name, $label, $options, $props, $separator, $required);
1451 'required' => $required,
1453 'multiple' => $context == 'search',
1455 return $this->addChainSelect($name, $props);
1458 $props['class'] = CRM_Utils_Array
::value('class', $props, 'big') . ' crm-select2';
1459 if (!array_key_exists('placeholder', $props)) {
1460 $props['placeholder'] = $required ?
ts('- select -') : ($context == 'search' ?
ts('- any -') : ts('- none -'));
1462 // TODO: Add and/or option for fields that store multiple values
1463 return $this->add('select', $name, $label, $options, $required, $props);
1465 case 'CheckBoxGroup':
1466 return $this->addCheckBox($name, $label, array_flip($options), $required, $props);
1469 return $this->addRadio($name, $label, $options, $props, NULL, $required);
1472 $text = isset($props['text']) ?
$props['text'] : NULL;
1473 unset($props['text']);
1474 return $this->addElement('checkbox', $name, $label, $text, $props);
1476 //add support for 'Advcheckbox' field
1478 $text = isset($props['text']) ?
$props['text'] : NULL;
1479 unset($props['text']);
1480 return $this->addElement('advcheckbox', $name, $label, $text, $props);
1483 // We should not build upload file in search mode.
1484 if ($context == 'search') {
1487 $file = $this->add('file', $name, $label, $props, $required);
1488 $this->addUploadElement($name);
1491 case 'RichTextEditor':
1492 return $this->add('wysiwyg', $name, $label, $props, $required);
1495 return $this->addEntityRef($name, $label, $props, $required);
1497 // Check datatypes of fields
1501 //case read only fields
1503 throw new Exception("Unsupported html-element " . $widget);
1508 * Add a widget for selecting/editing/creating/copying a profile form
1510 * @param string $name
1511 * HTML form-element name.
1512 * @param string $label
1514 * @param string $allowCoreTypes
1515 * Only present a UFGroup if its group_type includes a subset of $allowCoreTypes; e.g. 'Individual', 'Activity'.
1516 * @param string $allowSubTypes
1517 * Only present a UFGroup if its group_type is compatible with $allowSubypes.
1518 * @param array $entities
1519 * @param bool $default
1521 * @param string $usedFor
1523 public function addProfileSelector($name, $label, $allowCoreTypes, $allowSubTypes, $entities, $default = FALSE, $usedFor = NULL) {
1525 // FIXME: Instead of adhoc serialization, use a single json_encode()
1526 CRM_UF_Page_ProfileEditor
::registerProfileScripts();
1527 CRM_UF_Page_ProfileEditor
::registerSchemas(CRM_Utils_Array
::collect('entity_type', $entities));
1528 $this->add('text', $name, $label, array(
1529 'class' => 'crm-profile-selector',
1530 // Note: client treats ';;' as equivalent to \0, and ';;' works better in HTML
1531 'data-group-type' => CRM_Core_BAO_UFGroup
::encodeGroupType($allowCoreTypes, $allowSubTypes, ';;'),
1532 'data-entities' => json_encode($entities),
1534 'data-default' => $default,
1535 'data-usedfor' => json_encode($usedFor),
1542 public function getRootTitle() {
1549 public function getCompleteTitle() {
1550 return $this->getRootTitle() . $this->getTitle();
1554 * @return CRM_Core_Smarty
1556 public static function &getTemplate() {
1557 return self
::$_template;
1561 * @param $elementName
1563 public function addUploadElement($elementName) {
1564 $uploadNames = $this->get('uploadNames');
1565 if (!$uploadNames) {
1566 $uploadNames = array();
1568 if (is_array($elementName)) {
1569 foreach ($elementName as $name) {
1570 if (!in_array($name, $uploadNames)) {
1571 $uploadNames[] = $name;
1576 if (!in_array($elementName, $uploadNames)) {
1577 $uploadNames[] = $elementName;
1580 $this->set('uploadNames', $uploadNames);
1582 $config = CRM_Core_Config
::singleton();
1583 if (!empty($uploadNames)) {
1584 $this->controller
->addUploadAction($config->customFileUploadDir
, $uploadNames);
1593 public function getVar($name) {
1594 return isset($this->$name) ?
$this->$name : NULL;
1601 public function setVar($name, $value) {
1602 $this->$name = $value;
1609 * Use $this->add('datepicker', ...) instead.
1611 * @param string $name
1612 * Name of the element.
1613 * @param string $label
1614 * Label of the element.
1615 * @param bool $required
1617 * @param array $attributes
1620 public function addDate($name, $label, $required = FALSE, $attributes = NULL) {
1621 if (!empty($attributes['formatType'])) {
1622 // get actual format
1623 $params = array('name' => $attributes['formatType']);
1626 // cache date information
1628 $key = "dateFormat_" . str_replace(' ', '_', $attributes['formatType']);
1629 if (empty($dateFormat[$key])) {
1630 CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_PreferencesDate', $params, $values);
1631 $dateFormat[$key] = $values;
1634 $values = $dateFormat[$key];
1637 if ($values['date_format']) {
1638 $attributes['format'] = $values['date_format'];
1641 if (!empty($values['time_format'])) {
1642 $attributes['timeFormat'] = $values['time_format'];
1644 $attributes['startOffset'] = $values['start'];
1645 $attributes['endOffset'] = $values['end'];
1648 $config = CRM_Core_Config
::singleton();
1649 if (empty($attributes['format'])) {
1650 $attributes['format'] = $config->dateInputFormat
;
1653 if (!isset($attributes['startOffset'])) {
1654 $attributes['startOffset'] = 10;
1657 if (!isset($attributes['endOffset'])) {
1658 $attributes['endOffset'] = 10;
1661 $this->add('text', $name, $label, $attributes);
1663 if (!empty($attributes['addTime']) ||
!empty($attributes['timeFormat'])) {
1665 if (!isset($attributes['timeFormat'])) {
1666 $timeFormat = $config->timeInputFormat
;
1669 $timeFormat = $attributes['timeFormat'];
1672 // 1 - 12 hours and 2 - 24 hours, but for jquery widget it is 0 and 1 respectively
1674 $show24Hours = TRUE;
1675 if ($timeFormat == 1) {
1676 $show24Hours = FALSE;
1679 //CRM-6664 -we are having time element name
1680 //in either flat string or an array format.
1681 $elementName = $name . '_time';
1682 if (substr($name, -1) == ']') {
1683 $elementName = substr($name, 0, strlen($name) - 1) . '_time]';
1686 $this->add('text', $elementName, ts('Time'), array('timeFormat' => $show24Hours));
1691 $this->addRule($name, ts('Please select %1', array(1 => $label)), 'required');
1692 if (!empty($attributes['addTime']) && !empty($attributes['addTimeRequired'])) {
1693 $this->addRule($elementName, ts('Please enter a time.'), 'required');
1699 * Function that will add date and time.
1702 * Use $this->add('datepicker', ...) instead.
1704 * @param string $name
1705 * @param string $label
1706 * @param bool $required
1707 * @param null $attributes
1709 public function addDateTime($name, $label, $required = FALSE, $attributes = NULL) {
1710 $addTime = array('addTime' => TRUE);
1711 if (is_array($attributes)) {
1712 $attributes = array_merge($attributes, $addTime);
1715 $attributes = $addTime;
1718 $this->addDate($name, $label, $required, $attributes);
1722 * Add a currency and money element to the form.
1724 * @param string $name
1725 * @param string $label
1726 * @param bool $required
1727 * @param null $attributes
1728 * @param bool $addCurrency
1729 * @param string $currencyName
1730 * @param null $defaultCurrency
1731 * @param bool $freezeCurrency
1733 * @return \HTML_QuickForm_Element
1735 public function addMoney(
1740 $addCurrency = TRUE,
1741 $currencyName = 'currency',
1742 $defaultCurrency = NULL,
1743 $freezeCurrency = FALSE
1745 $element = $this->add('text', $name, $label, $attributes, $required);
1746 $this->addRule($name, ts('Please enter a valid amount.'), 'money');
1749 $ele = $this->addCurrency($currencyName, NULL, TRUE, $defaultCurrency, $freezeCurrency);
1756 * Add currency element to the form.
1758 * @param string $name
1759 * @param null $label
1760 * @param bool $required
1761 * @param string $defaultCurrency
1762 * @param bool $freezeCurrency
1764 public function addCurrency(
1768 $defaultCurrency = NULL,
1769 $freezeCurrency = FALSE
1771 $currencies = CRM_Core_OptionGroup
::values('currencies_enabled');
1772 $options = array('class' => 'crm-select2 eight');
1774 $currencies = array('' => '') +
$currencies;
1775 $options['placeholder'] = ts('- none -');
1777 $ele = $this->add('select', $name, $label, $currencies, $required, $options);
1778 if ($freezeCurrency) {
1781 if (!$defaultCurrency) {
1782 $config = CRM_Core_Config
::singleton();
1783 $defaultCurrency = $config->defaultCurrency
;
1785 $this->setDefaults(array($name => $defaultCurrency));
1789 * Create a single or multiple entity ref field.
1790 * @param string $name
1791 * @param string $label
1792 * @param array $props
1793 * Mix of html and widget properties, including:.
1794 * - select - params to give to select2 widget
1795 * - entity - defaults to contact
1796 * - create - can the user create a new entity on-the-fly?
1797 * Set to TRUE if entity is contact and you want the default profiles,
1798 * or pass in your own set of links. @see CRM_Core_BAO_UFGroup::getCreateLinks for format
1799 * note that permissions are checked automatically
1800 * - api - array of settings for the getlist api wrapper
1801 * note that it accepts a 'params' setting which will be passed to the underlying api
1802 * - placeholder - string
1804 * - class, etc. - other html properties
1805 * @param bool $required
1807 * @return HTML_QuickForm_Element
1809 public function addEntityRef($name, $label = '', $props = array(), $required = FALSE) {
1810 require_once "api/api.php";
1811 $config = CRM_Core_Config
::singleton();
1812 // Default properties
1813 $props['api'] = CRM_Utils_Array
::value('api', $props, array());
1814 $props['entity'] = _civicrm_api_get_entity_name_from_camel(CRM_Utils_Array
::value('entity', $props, 'contact'));
1815 $props['class'] = ltrim(CRM_Utils_Array
::value('class', $props, '') . ' crm-form-entityref');
1817 if ($props['entity'] == 'contact' && isset($props['create']) && !(CRM_Core_Permission
::check('edit all contacts') || CRM_Core_Permission
::check('add contacts'))) {
1818 unset($props['create']);
1821 $props['placeholder'] = CRM_Utils_Array
::value('placeholder', $props, $required ?
ts('- select %1 -', array(1 => ts(str_replace('_', ' ', $props['entity'])))) : ts('- none -'));
1823 $defaults = array();
1824 if (!empty($props['multiple'])) {
1825 $defaults['multiple'] = TRUE;
1827 $props['select'] = CRM_Utils_Array
::value('select', $props, array()) +
$defaults;
1829 $this->formatReferenceFieldAttributes($props);
1830 return $this->add('text', $name, $label, $props, $required);
1836 private function formatReferenceFieldAttributes(&$props) {
1837 $props['data-select-params'] = json_encode($props['select']);
1838 $props['data-api-params'] = $props['api'] ?
json_encode($props['api']) : NULL;
1839 $props['data-api-entity'] = $props['entity'];
1840 if (!empty($props['create'])) {
1841 $props['data-create-links'] = json_encode($props['create']);
1843 CRM_Utils_Array
::remove($props, 'multiple', 'select', 'api', 'entity', 'create');
1847 * Convert all date fields within the params to mysql date ready for the
1848 * BAO layer. In this case fields are checked against the $_datefields defined for the form
1849 * and if time is defined it is incorporated
1851 * @param array $params
1852 * Input params from the form.
1854 * @todo it would probably be better to work on $this->_params than a passed array
1855 * @todo standardise the format which dates are passed to the BAO layer in & remove date
1858 public function convertDateFieldsToMySQL(&$params) {
1859 foreach ($this->_dateFields
as $fieldName => $specs) {
1860 if (!empty($params[$fieldName])) {
1861 $params[$fieldName] = CRM_Utils_Date
::isoToMysql(
1862 CRM_Utils_Date
::processDate(
1863 $params[$fieldName],
1864 CRM_Utils_Array
::value("{$fieldName}_time", $params), TRUE)
1868 if (isset($specs['default'])) {
1869 $params[$fieldName] = date('YmdHis', strtotime($specs['default']));
1876 * @param $elementName
1878 public function removeFileRequiredRules($elementName) {
1879 $this->_required
= array_diff($this->_required
, array($elementName));
1880 if (isset($this->_rules
[$elementName])) {
1881 foreach ($this->_rules
[$elementName] as $index => $ruleInfo) {
1882 if ($ruleInfo['type'] == 'uploadedfile') {
1883 unset($this->_rules
[$elementName][$index]);
1886 if (empty($this->_rules
[$elementName])) {
1887 unset($this->_rules
[$elementName]);
1893 * Function that can be defined in Form to override or.
1894 * perform specific action on cancel action
1896 public function cancelAction() {
1900 * Helper function to verify that required fields have been filled.
1902 * Typically called within the scope of a FormRule function
1904 * @param array $fields
1905 * @param array $values
1906 * @param array $errors
1908 public static function validateMandatoryFields($fields, $values, &$errors) {
1909 foreach ($fields as $name => $fld) {
1910 if (!empty($fld['is_required']) && CRM_Utils_System
::isNull(CRM_Utils_Array
::value($name, $values))) {
1911 $errors[$name] = ts('%1 is a required field.', array(1 => $fld['title']));
1917 * Get contact if for a form object. Prioritise
1918 * - cid in URL if 0 (on behalf on someoneelse)
1919 * (@todo consider setting a variable if onbehalf for clarity of downstream 'if's
1920 * - logged in user id if it matches the one in the cid in the URL
1921 * - contact id validated from a checksum from a checksum
1922 * - cid from the url if the caller has ACL permission to view
1923 * - fallback is logged in user (or ? NULL if no logged in user) (@todo wouldn't 0 be more intuitive?)
1927 protected function setContactID() {
1928 $tempID = CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
1929 if (isset($this->_params
) && !empty($this->_params
['select_contact_id'])) {
1930 $tempID = $this->_params
['select_contact_id'];
1932 if (isset($this->_params
, $this->_params
[0]) && !empty($this->_params
[0]['select_contact_id'])) {
1933 // event form stores as an indexed array, contribution form not so much...
1934 $tempID = $this->_params
[0]['select_contact_id'];
1937 // force to ignore the authenticated user
1938 if ($tempID === '0' ||
$tempID === 0) {
1939 // we set the cid on the form so that this will be retained for the Confirm page
1940 // in the multi-page form & prevent us returning the $userID when this is called
1942 // we don't really need to set it when $tempID is set because the params have that stored
1943 $this->set('cid', 0);
1944 CRM_Core_Resources
::singleton()->addVars('coreForm', array('contact_id' => (int) $tempID));
1945 return (int) $tempID;
1948 $userID = $this->getLoggedInUserContactID();
1950 if (!is_null($tempID) && $tempID === $userID) {
1951 CRM_Core_Resources
::singleton()->addVars('coreForm', array('contact_id' => (int) $tempID));
1952 return (int) $userID;
1955 //check if this is a checksum authentication
1956 $userChecksum = CRM_Utils_Request
::retrieve('cs', 'String', $this);
1957 if ($userChecksum) {
1958 //check for anonymous user.
1959 $validUser = CRM_Contact_BAO_Contact_Utils
::validChecksum($tempID, $userChecksum);
1961 CRM_Core_Resources
::singleton()->addVars('coreForm', array('contact_id' => (int) $tempID));
1962 CRM_Core_Resources
::singleton()->addVars('coreForm', array('checksum' => (int) $tempID));
1966 // check if user has permission, CRM-12062
1967 elseif ($tempID && CRM_Contact_BAO_Contact_Permission
::allow($tempID)) {
1968 CRM_Core_Resources
::singleton()->addVars('coreForm', array('contact_id' => (int) $tempID));
1971 if (is_numeric($userID)) {
1972 CRM_Core_Resources
::singleton()->addVars('coreForm', array('contact_id' => (int) $userID));
1974 return is_numeric($userID) ?
$userID : NULL;
1978 * Get the contact id that the form is being submitted for.
1982 public function getContactID() {
1983 return $this->setContactID();
1987 * Get the contact id of the logged in user.
1989 public function getLoggedInUserContactID() {
1990 // check if the user is logged in and has a contact ID
1991 $session = CRM_Core_Session
::singleton();
1992 return $session->get('userID');
1996 * Add autoselector field -if user has permission to view contacts
1997 * If adding this to a form you also need to add to the tpl e.g
1999 * {if !empty($selectable)}
2000 * <div class="crm-summary-row">
2001 * <div class="crm-label">{$form.select_contact.label}</div>
2002 * <div class="crm-content">
2003 * {$form.select_contact.html}
2008 * @param array $profiles
2009 * Ids of profiles that are on the form (to be autofilled).
2010 * @param array $autoCompleteField
2014 * - url (for ajax lookup)
2016 * @todo add data attributes so we can deal with multiple instances on a form
2018 public function addAutoSelector($profiles = array(), $autoCompleteField = array()) {
2019 $autoCompleteField = array_merge(array(
2020 'id_field' => 'select_contact_id',
2021 'placeholder' => ts('Select someone else ...'),
2022 'show_hide' => TRUE,
2023 'api' => array('params' => array('contact_type' => 'Individual')),
2024 ), $autoCompleteField);
2026 if ($this->canUseAjaxContactLookups()) {
2027 $this->assign('selectable', $autoCompleteField['id_field']);
2028 $this->addEntityRef($autoCompleteField['id_field'], NULL, array(
2029 'placeholder' => $autoCompleteField['placeholder'],
2030 'api' => $autoCompleteField['api'],
2033 CRM_Core_Resources
::singleton()->addScriptFile('civicrm', 'js/AlternateContactSelector.js', 1, 'html-header')
2035 'form' => array('autocompletes' => $autoCompleteField),
2036 'ids' => array('profile' => $profiles),
2043 public function canUseAjaxContactLookups() {
2044 if (0 < (civicrm_api3('contact', 'getcount', array('check_permissions' => 1))) &&
2045 CRM_Core_Permission
::check(array(array('access AJAX API', 'access CiviCRM')))
2052 * Add the options appropriate to cid = zero - ie. autocomplete
2054 * @todo there is considerable code duplication between the contribution forms & event forms. It is apparent
2055 * that small pieces of duplication are not being refactored into separate functions because their only shared parent
2056 * is this form. Inserting a class FrontEndForm.php between the contribution & event & this class would allow functions like this
2057 * and a dozen other small ones to be refactored into a shared parent with the reduction of much code duplication
2059 * @param $onlinePaymentProcessorEnabled
2061 public function addCIDZeroOptions($onlinePaymentProcessorEnabled) {
2062 $this->assign('nocid', TRUE);
2063 $profiles = array();
2064 if ($this->_values
['custom_pre_id']) {
2065 $profiles[] = $this->_values
['custom_pre_id'];
2067 if ($this->_values
['custom_post_id']) {
2068 $profiles = array_merge($profiles, (array) $this->_values
['custom_post_id']);
2070 if ($onlinePaymentProcessorEnabled) {
2071 $profiles[] = 'billing';
2073 if (!empty($this->_values
)) {
2074 $this->addAutoSelector($profiles);
2079 * Set default values on form for given contact (or no contact defaults)
2081 * @param mixed $profile_id
2082 * (can be id, or profile name).
2083 * @param int $contactID
2087 public function getProfileDefaults($profile_id = 'Billing', $contactID = NULL) {
2089 $defaults = civicrm_api3('profile', 'getsingle', array(
2090 'profile_id' => (array) $profile_id,
2091 'contact_id' => $contactID,
2095 catch (Exception
$e) {
2096 // the try catch block gives us silent failure -not 100% sure this is a good idea
2097 // as silent failures are often worse than noisy ones
2103 * Sets form attribute.
2106 public function preventAjaxSubmit() {
2107 $this->setAttribute('data-no-ajax-submit', 'true');
2111 * Sets form attribute.
2114 public function allowAjaxSubmit() {
2115 $this->removeAttribute('data-no-ajax-submit');
2119 * Sets page title based on entity and action.
2120 * @param string $entityLabel
2122 public function setPageTitle($entityLabel) {
2123 switch ($this->_action
) {
2124 case CRM_Core_Action
::ADD
:
2125 CRM_Utils_System
::setTitle(ts('New %1', array(1 => $entityLabel)));
2128 case CRM_Core_Action
::UPDATE
:
2129 CRM_Utils_System
::setTitle(ts('Edit %1', array(1 => $entityLabel)));
2132 case CRM_Core_Action
::VIEW
:
2133 case CRM_Core_Action
::PREVIEW
:
2134 CRM_Utils_System
::setTitle(ts('View %1', array(1 => $entityLabel)));
2137 case CRM_Core_Action
::DELETE
:
2138 CRM_Utils_System
::setTitle(ts('Delete %1', array(1 => $entityLabel)));
2144 * Create a chain-select target field. All settings are optional; the defaults usually work.
2146 * @param string $elementName
2147 * @param array $settings
2149 * @return HTML_QuickForm_Element
2151 public function addChainSelect($elementName, $settings = array()) {
2152 $props = $settings +
= array(
2153 'control_field' => str_replace(array('state_province', 'StateProvince', 'county', 'County'), array(
2159 'data-callback' => strpos($elementName, 'rovince') ?
'civicrm/ajax/jqState' : 'civicrm/ajax/jqCounty',
2160 'label' => strpos($elementName, 'rovince') ?
ts('State/Province') : ts('County'),
2161 'data-empty-prompt' => strpos($elementName, 'rovince') ?
ts('Choose country first') : ts('Choose state first'),
2162 'data-none-prompt' => ts('- N/A -'),
2163 'multiple' => FALSE,
2164 'required' => FALSE,
2165 'placeholder' => empty($settings['required']) ?
ts('- none -') : ts('- select -'),
2167 CRM_Utils_Array
::remove($props, 'label', 'required', 'control_field', 'context');
2168 $props['class'] = (empty($props['class']) ?
'' : "{$props['class']} ") . 'crm-select2';
2169 $props['data-select-prompt'] = $props['placeholder'];
2170 $props['data-name'] = $elementName;
2172 $this->_chainSelectFields
[$settings['control_field']] = $elementName;
2174 // Passing NULL instead of an array of options
2175 // CRM-15225 - normally QF will reject any selected values that are not part of the field's options, but due to a
2176 // quirk in our patched version of HTML_QuickForm_select, this doesn't happen if the options are NULL
2177 // which seems a bit dirty but it allows our dynamically-popuplated select element to function as expected.
2178 return $this->add('select', $elementName, $settings['label'], NULL, $settings['required'], $props);
2182 * Add actions menu to results form.
2184 * @param array $tasks
2186 public function addTaskMenu($tasks) {
2187 if (is_array($tasks) && !empty($tasks)) {
2188 // Set constants means this will always load with an empty value, not reloading any submitted value.
2189 // This is appropriate as it is a pseudofield.
2190 $this->setConstants(array('task' => ''));
2191 $this->assign('taskMetaData', $tasks);
2192 $select = $this->add('select', 'task', NULL, array('' => ts('Actions')), FALSE, array(
2193 'class' => 'crm-select2 crm-action-menu fa-check-circle-o huge crm-search-result-actions')
2195 foreach ($tasks as $key => $task) {
2196 $attributes = array();
2197 if (isset($task['data'])) {
2198 foreach ($task['data'] as $dataKey => $dataValue) {
2199 $attributes['data-' . $dataKey] = $dataValue;
2202 $select->addOption($task['title'], $key, $attributes);
2204 if (empty($this->_actionButtonName
)) {
2205 $this->_actionButtonName
= $this->getButtonName('next', 'action');
2207 $this->assign('actionButtonName', $this->_actionButtonName
);
2208 $this->add('submit', $this->_actionButtonName
, ts('Go'), array('class' => 'hiddenElement crm-search-go-button'));
2210 // Radio to choose "All items" or "Selected items only"
2211 $selectedRowsRadio = $this->addElement('radio', 'radio_ts', NULL, '', 'ts_sel', array('checked' => 'checked'));
2212 $allRowsRadio = $this->addElement('radio', 'radio_ts', NULL, '', 'ts_all');
2213 $this->assign('ts_sel_id', $selectedRowsRadio->_attributes
['id']);
2214 $this->assign('ts_all_id', $allRowsRadio->_attributes
['id']);
2216 CRM_Core_Resources
::singleton()->addScriptFile('civicrm', 'js/crm.searchForm.js', 1, 'html-header');
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);
2294 * Assign billing name to the template.
2296 * @param array $params
2297 * Form input params, default to $this->_params.
2299 public function assignBillingName($params = array()) {
2301 if (empty($params)) {
2302 $params = $this->_params
;
2304 if (!empty($params['billing_first_name'])) {
2305 $name = $params['billing_first_name'];
2308 if (!empty($params['billing_middle_name'])) {
2309 $name .= " {$params['billing_middle_name']}";
2312 if (!empty($params['billing_last_name'])) {
2313 $name .= " {$params['billing_last_name']}";
2315 $name = trim($name);
2316 $this->assign('billingName', $name);