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