Merge pull request #15372 from civicrm/5.18
[civicrm-core.git] / CRM / Contribute / Form / Task / PickProfile.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
6b83d5bd 31 * @copyright CiviCRM LLC (c) 2004-2019
6a488035
TO
32 */
33
34/**
07f8d162 35 * This class provides the functionality for batch profile update for contributions.
6a488035
TO
36 */
37class CRM_Contribute_Form_Task_PickProfile extends CRM_Contribute_Form_Task {
38
39 /**
100fef9d 40 * The title of the group
6a488035
TO
41 *
42 * @var string
43 */
44 protected $_title;
45
46 /**
100fef9d 47 * Maximum contributions that should be allowed to update
1330f57a 48 * @var int
6a488035
TO
49 */
50 protected $_maxContributions = 100;
51
52 /**
100fef9d 53 * Variable to store redirect path
1330f57a 54 * @var string
6a488035
TO
55 */
56 protected $_userContext;
57
58 /**
fe482240 59 * Build all the data structures needed to build the form.
6a488035 60 */
00be9182 61 public function preProcess() {
d424ffde 62 // initialize the task and row fields
6a488035
TO
63 parent::preProcess();
64 $session = CRM_Core_Session::singleton();
65 $this->_userContext = $session->readUserContext();
66
b581842f 67 CRM_Utils_System::setTitle(ts('Update multiple contributions'));
6a488035
TO
68
69 $validate = FALSE;
70 //validations
71 if (count($this->_contributionIds) > $this->_maxContributions) {
be2fb01f 72 CRM_Core_Session::setStatus(ts("The maximum number of contributions you can select for Update multiple contributions is %1. You have selected %2. Please select fewer contributions from your search results and try again.", [
1330f57a
SL
73 1 => $this->_maxContributions,
74 2 => count($this->_contributionIds),
75 ]), ts('Update multiple records error'), 'error');
6a488035
TO
76 $validate = TRUE;
77 }
78
79 // than redirect
80 if ($validate) {
81 CRM_Utils_System::redirect($this->_userContext);
82 }
83 }
84
85 /**
fe482240 86 * Build the form object.
6a488035 87 */
00be9182 88 public function buildQuickForm() {
6a488035 89
be2fb01f 90 $types = ['Contribution'];
6a488035
TO
91 $profiles = CRM_Core_BAO_UFGroup::getProfiles($types, TRUE);
92
93 if (empty($profiles)) {
be2fb01f 94 CRM_Core_Session::setStatus(ts("You will need to create a Profile containing the %1 fields you want to edit before you can use Update multiple contributions. Navigate to Administer CiviCRM > Customize Data and Screens > CiviCRM Profile to configure a Profile. Consult the online Administrator documentation for more information.", [1 => $types[0]]), ts('Profile Required'), 'error');
6a488035
TO
95 CRM_Utils_System::redirect($this->_userContext);
96 }
97
98 $ufGroupElement = $this->add('select', 'uf_group_id', ts('Select Profile'),
be2fb01f 99 [
389bcebf 100 '' => ts('- select profile -'),
be2fb01f 101 ] + $profiles, TRUE
6a488035 102 );
f212d37d 103 $this->addDefaultButtons(ts('Continue'));
6a488035
TO
104 }
105
106 /**
fe482240 107 * Add local and global form rules.
6a488035 108 */
00be9182 109 public function addRules() {
be2fb01f 110 $this->addFormRule(['CRM_Contribute_Form_Task_PickProfile', 'formRule']);
6a488035
TO
111 }
112
113 /**
fe482240 114 * Global validation rules for the form.
6a488035 115 *
014c4014
TO
116 * @param array $fields
117 * Posted values of the form.
6a488035 118 *
a6c01b45
CW
119 * @return array
120 * list of errors to be posted back to the form
6a488035 121 */
00be9182 122 public static function formRule($fields) {
6a488035
TO
123 return TRUE;
124 }
125
126 /**
fe482240 127 * Process the form after the input has been submitted and validated.
6a488035
TO
128 */
129 public function postProcess() {
130 $params = $this->exportValues();
131
132 $this->set('ufGroupId', $params['uf_group_id']);
133
134 // also reset the batch page so it gets new values from the db
135 $this->controller->resetPage('Batch');
136 }
96025800 137
6a488035 138}