renaming-batch-update
[civicrm-core.git] / CRM / Member / Form / Task / Batch.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
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
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * This class provides the functionality for batch profile update for members
38 */
39class CRM_Member_Form_Task_Batch extends CRM_Member_Form_Task {
40
41 /**
fe482240 42 * The title of the group.
6a488035
TO
43 *
44 * @var string
45 */
46 protected $_title;
47
48 /**
fe482240 49 * Maximum profile fields that will be displayed.
6a488035
TO
50 */
51 protected $_maxFields = 9;
52
53 /**
fe482240 54 * Variable to store redirect path.
6a488035
TO
55 */
56 protected $_userContext;
57
58 /**
fe482240 59 * Build all the data structures needed to build the form.
6a488035
TO
60 *
61 * @return void
03e04002 62 */
00be9182 63 public function preProcess() {
d424ffde 64 // initialize the task and row fields
6a488035
TO
65 parent::preProcess();
66
67 //get the contact read only fields to display.
68 $readOnlyFields = array_merge(array('sort_name' => ts('Name')),
69 CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
70 'contact_autocomplete_options',
71 TRUE, NULL, FALSE, 'name', TRUE
72 )
73 );
74 //get the read only field data.
75 $returnProperties = array_fill_keys(array_keys($readOnlyFields), 1);
76 $contactDetails = CRM_Contact_BAO_Contact_Utils::contactDetails($this->_memberIds,
77 'CiviMember', $returnProperties
78 );
79 $this->assign('contactDetails', $contactDetails);
80 $this->assign('readOnlyFields', $readOnlyFields);
81 }
82
83 /**
fe482240 84 * Build the form object.
6a488035 85 *
6a488035
TO
86 *
87 * @return void
88 */
00be9182 89 public function buildQuickForm() {
6a488035
TO
90 $ufGroupId = $this->get('ufGroupId');
91
92 if (!$ufGroupId) {
93 CRM_Core_Error::fatal('ufGroupId is missing');
94 }
b581842f 95 $this->_title = ts('Update multiple memberships') . ' - ' . CRM_Core_BAO_UFGroup::getTitle($ufGroupId);
6a488035
TO
96 CRM_Utils_System::setTitle($this->_title);
97
98 $this->addDefaultButtons(ts('Save'));
99 $this->_fields = array();
100 $this->_fields = CRM_Core_BAO_UFGroup::getFields($ufGroupId, FALSE, CRM_Core_Action::VIEW);
101
102 // remove file type field and then limit fields
103 $suppressFields = FALSE;
104 $removehtmlTypes = array('File', 'Autocomplete-Select');
105 foreach ($this->_fields as $name => $field) {
106 if ($cfID = CRM_Core_BAO_CustomField::getKeyID($name) &&
107 in_array($this->_fields[$name]['html_type'], $removehtmlTypes)
108 ) {
109 $suppressFields = TRUE;
110 unset($this->_fields[$name]);
111 }
112
113 //fix to reduce size as we are using this field in grid
b581842f 114 if (is_array($field['attributes']) && !empty($this->_fields[$name]['attributes']['size']) && $this->_fields[$name]['attributes']['size'] > 19) {
6a488035
TO
115 //shrink class to "form-text-medium"
116 $this->_fields[$name]['attributes']['size'] = 19;
117 }
118 }
119
120 $this->_fields = array_slice($this->_fields, 0, $this->_maxFields);
121
122 $this->addButtons(array(
123 array(
124 'type' => 'submit',
125 'name' => ts('Update Members(s)'),
126 'isDefault' => TRUE,
127 ),
128 array(
129 'type' => 'cancel',
130 'name' => ts('Cancel'),
131 ),
132 )
133 );
134
6a488035
TO
135 $this->assign('profileTitle', $this->_title);
136 $this->assign('componentIds', $this->_memberIds);
137 $fileFieldExists = FALSE;
138
139 //load all campaigns.
140 if (array_key_exists('member_campaign_id', $this->_fields)) {
141 $this->_componentCampaigns = array();
142 CRM_Core_PseudoConstant::populate($this->_componentCampaigns,
143 'CRM_Member_DAO_Membership',
144 TRUE, 'campaign_id', 'id',
145 ' id IN (' . implode(' , ', array_values($this->_memberIds)) . ' ) '
146 );
147 }
148
149 $customFields = CRM_Core_BAO_CustomField::getFields('Membership');
150 foreach ($this->_memberIds as $memberId) {
151 $typeId = CRM_Core_DAO::getFieldValue("CRM_Member_DAO_Membership", $memberId, 'membership_type_id');
152 foreach ($this->_fields as $name => $field) {
153 if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($name)) {
154 $customValue = CRM_Utils_Array::value($customFieldID, $customFields);
a7488080 155 if (!empty($customValue['extends_entity_column_value'])) {
6a488035
TO
156 $entityColumnValue = explode(CRM_Core_DAO::VALUE_SEPARATOR,
157 $customValue['extends_entity_column_value']
158 );
159 }
160 if ((CRM_Utils_Array::value($typeId, $entityColumnValue)) ||
161 CRM_Utils_System::isNull($entityColumnValue[$typeId])
162 ) {
163 CRM_Core_BAO_UFGroup::buildProfile($this, $field, NULL, $memberId);
164 }
165 }
166 else {
167 // handle non custom fields
168 CRM_Core_BAO_UFGroup::buildProfile($this, $field, NULL, $memberId);
169 }
170 }
171 }
172
173 $this->assign('fields', $this->_fields);
174
175 // don't set the status message when form is submitted.
176 $buttonName = $this->controller->getButtonName('submit');
177
178 if ($suppressFields && $buttonName != '_qf_Batch_next') {
b581842f 179 CRM_Core_Session::setStatus(ts("File or Autocomplete-Select type field(s) in the selected profile are not supported for Update multiple memberships."), ts('Unsupported Field Type'), 'error');
6a488035
TO
180 }
181
182 $this->addDefaultButtons(ts('Update Memberships'));
183 }
184
185 /**
c490a46a 186 * Set default values for the form.
6a488035 187 *
6a488035 188 *
355ba699 189 * @return void
6a488035 190 */
00be9182 191 public function setDefaultValues() {
6a488035
TO
192 if (empty($this->_fields)) {
193 return;
194 }
195
196 $defaults = array();
197 foreach ($this->_memberIds as $memberId) {
198 $details[$memberId] = array();
199 CRM_Core_BAO_UFGroup::setProfileDefaults(NULL, $this->_fields, $defaults, FALSE, $memberId, 'Membership');
200 }
201
202 return $defaults;
203 }
204
205 /**
fe482240 206 * Process the form after the input has been submitted and validated.
6a488035 207 *
6a488035 208 *
355ba699 209 * @return void
6a488035
TO
210 */
211 public function postProcess() {
212 $params = $this->exportValues();
213 $dates = array(
214 'join_date',
215 'membership_start_date',
216 'membership_end_date',
353ffa53 217 );
6a488035
TO
218 if (isset($params['field'])) {
219 $customFields = array();
220 foreach ($params['field'] as $key => $value) {
221 $ids['membership'] = $key;
a7488080 222 if (!empty($value['membership_source'])) {
6a488035
TO
223 $value['source'] = $value['membership_source'];
224 }
225
a7488080 226 if (!empty($value['membership_type'])) {
6a488035
TO
227 $membershipTypeId = $value['membership_type_id'] = $value['membership_type'][1];
228 }
229
230 unset($value['membership_source']);
231 unset($value['membership_type']);
232
233 //Get the membership status
234 $value['status_id'] = (CRM_Utils_Array::value('membership_status', $value)) ? $value['membership_status'] : CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $key, 'status_id');
235 unset($value['membership_status']);
236 foreach ($dates as $val) {
237 if (isset($value[$val])) {
238 $value[$val] = CRM_Utils_Date::processDate($value[$val]);
239 }
240 }
241 if (empty($customFields)) {
a7488080 242 if (empty($value['membership_type_id'])) {
6a488035
TO
243 $membershipTypeId = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $key, 'membership_type_id');
244 }
245
246 // membership type custom data
247 $customFields = CRM_Core_BAO_CustomField::getFields('Membership', FALSE, FALSE, $membershipTypeId);
248
249 $customFields = CRM_Utils_Array::crmArrayMerge($customFields,
250 CRM_Core_BAO_CustomField::getFields('Membership',
251 FALSE, FALSE, NULL, NULL, TRUE
252 )
253 );
254 }
255 //check for custom data
256 $value['custom'] = CRM_Core_BAO_CustomField::postProcess($params['field'][$key],
6a488035
TO
257 $key,
258 'Membership',
259 $membershipTypeId
260 );
261
262 $membership = CRM_Member_BAO_Membership::add($value, $ids);
263
264 // add custom field values
a7488080 265 if (!empty($value['custom']) &&
6a488035
TO
266 is_array($value['custom'])
267 ) {
268 CRM_Core_BAO_CustomValueTable::store($value['custom'], 'civicrm_membership', $membership->id);
269 }
270 }
271
272 CRM_Core_Session::setStatus(ts("Your updates have been saved."), ts('Saved'), 'success');
273 }
274 else {
275 CRM_Core_Session::setStatus(ts("No updates have been saved."), ts('Not Saved'), 'alert');
276 }
277 }
96025800 278
6a488035 279}