3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
37 * This class provides the functionality for batch profile update for members
39 class CRM_Member_Form_Task_Batch
extends CRM_Member_Form_Task
{
42 * The title of the group
49 * Maximum profile fields that will be displayed
52 protected $_maxFields = 9;
55 * Variable to store redirect path
58 protected $_userContext;
61 * Build all the data structures needed to build the form
65 public function preProcess() {
67 * initialize the task and row fields
71 //get the contact read only fields to display.
72 $readOnlyFields = array_merge(array('sort_name' => ts('Name')),
73 CRM_Core_BAO_Setting
::valueOptions(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
74 'contact_autocomplete_options',
75 TRUE, NULL, FALSE, 'name', TRUE
78 //get the read only field data.
79 $returnProperties = array_fill_keys(array_keys($readOnlyFields), 1);
80 $contactDetails = CRM_Contact_BAO_Contact_Utils
::contactDetails($this->_memberIds
,
81 'CiviMember', $returnProperties
83 $this->assign('contactDetails', $contactDetails);
84 $this->assign('readOnlyFields', $readOnlyFields);
88 * Build the form object
93 public function buildQuickForm() {
94 $ufGroupId = $this->get('ufGroupId');
97 CRM_Core_Error
::fatal('ufGroupId is missing');
99 $this->_title
= ts('Batch Update for Members') . ' - ' . CRM_Core_BAO_UFGroup
::getTitle($ufGroupId);
100 CRM_Utils_System
::setTitle($this->_title
);
102 $this->addDefaultButtons(ts('Save'));
103 $this->_fields
= array();
104 $this->_fields
= CRM_Core_BAO_UFGroup
::getFields($ufGroupId, FALSE, CRM_Core_Action
::VIEW
);
106 // remove file type field and then limit fields
107 $suppressFields = FALSE;
108 $removehtmlTypes = array('File', 'Autocomplete-Select');
109 foreach ($this->_fields
as $name => $field) {
110 if ($cfID = CRM_Core_BAO_CustomField
::getKeyID($name) &&
111 in_array($this->_fields
[$name]['html_type'], $removehtmlTypes)
113 $suppressFields = TRUE;
114 unset($this->_fields
[$name]);
117 //fix to reduce size as we are using this field in grid
118 if (is_array($field['attributes']) && $this->_fields
[$name]['attributes']['size'] > 19) {
119 //shrink class to "form-text-medium"
120 $this->_fields
[$name]['attributes']['size'] = 19;
124 $this->_fields
= array_slice($this->_fields
, 0, $this->_maxFields
);
126 $this->addButtons(array(
129 'name' => ts('Update Members(s)'),
134 'name' => ts('Cancel'),
140 $this->assign('profileTitle', $this->_title
);
141 $this->assign('componentIds', $this->_memberIds
);
142 $fileFieldExists = FALSE;
144 //load all campaigns.
145 if (array_key_exists('member_campaign_id', $this->_fields
)) {
146 $this->_componentCampaigns
= array();
147 CRM_Core_PseudoConstant
::populate($this->_componentCampaigns
,
148 'CRM_Member_DAO_Membership',
149 TRUE, 'campaign_id', 'id',
150 ' id IN (' . implode(' , ', array_values($this->_memberIds
)) . ' ) '
154 $customFields = CRM_Core_BAO_CustomField
::getFields('Membership');
155 foreach ($this->_memberIds
as $memberId) {
156 $typeId = CRM_Core_DAO
::getFieldValue("CRM_Member_DAO_Membership", $memberId, 'membership_type_id');
157 foreach ($this->_fields
as $name => $field) {
158 if ($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($name)) {
159 $customValue = CRM_Utils_Array
::value($customFieldID, $customFields);
160 if (!empty($customValue['extends_entity_column_value'])) {
161 $entityColumnValue = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
162 $customValue['extends_entity_column_value']
165 if ((CRM_Utils_Array
::value($typeId, $entityColumnValue)) ||
166 CRM_Utils_System
::isNull($entityColumnValue[$typeId])
168 CRM_Core_BAO_UFGroup
::buildProfile($this, $field, NULL, $memberId);
172 // handle non custom fields
173 CRM_Core_BAO_UFGroup
::buildProfile($this, $field, NULL, $memberId);
178 $this->assign('fields', $this->_fields
);
180 // don't set the status message when form is submitted.
181 $buttonName = $this->controller
->getButtonName('submit');
183 if ($suppressFields && $buttonName != '_qf_Batch_next') {
184 CRM_Core_Session
::setStatus(ts("File or Autocomplete-Select type field(s) in the selected profile are not supported for Batch Update."), ts('Unsupported Field Type'), 'error');
187 $this->addDefaultButtons(ts('Update Memberships'));
191 * Set default values for the form.
196 public function setDefaultValues() {
197 if (empty($this->_fields
)) {
202 foreach ($this->_memberIds
as $memberId) {
203 $details[$memberId] = array();
204 CRM_Core_BAO_UFGroup
::setProfileDefaults(NULL, $this->_fields
, $defaults, FALSE, $memberId, 'Membership');
211 * Process the form after the input has been submitted and validated
216 public function postProcess() {
217 $params = $this->exportValues();
220 'membership_start_date',
221 'membership_end_date',
223 if (isset($params['field'])) {
224 $customFields = array();
225 foreach ($params['field'] as $key => $value) {
226 $ids['membership'] = $key;
227 if (!empty($value['membership_source'])) {
228 $value['source'] = $value['membership_source'];
231 if (!empty($value['membership_type'])) {
232 $membershipTypeId = $value['membership_type_id'] = $value['membership_type'][1];
235 unset($value['membership_source']);
236 unset($value['membership_type']);
238 //Get the membership status
239 $value['status_id'] = (CRM_Utils_Array
::value('membership_status', $value)) ?
$value['membership_status'] : CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_Membership', $key, 'status_id');
240 unset($value['membership_status']);
241 foreach ($dates as $val) {
242 if (isset($value[$val])) {
243 $value[$val] = CRM_Utils_Date
::processDate($value[$val]);
246 if (empty($customFields)) {
247 if (empty($value['membership_type_id'])) {
248 $membershipTypeId = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_Membership', $key, 'membership_type_id');
251 // membership type custom data
252 $customFields = CRM_Core_BAO_CustomField
::getFields('Membership', FALSE, FALSE, $membershipTypeId);
254 $customFields = CRM_Utils_Array
::crmArrayMerge($customFields,
255 CRM_Core_BAO_CustomField
::getFields('Membership',
256 FALSE, FALSE, NULL, NULL, TRUE
260 //check for custom data
261 $value['custom'] = CRM_Core_BAO_CustomField
::postProcess($params['field'][$key],
268 $membership = CRM_Member_BAO_Membership
::add($value, $ids);
270 // add custom field values
271 if (!empty($value['custom']) &&
272 is_array($value['custom'])
274 CRM_Core_BAO_CustomValueTable
::store($value['custom'], 'civicrm_membership', $membership->id
);
278 CRM_Core_Session
::setStatus(ts("Your updates have been saved."), ts('Saved'), 'success');
281 CRM_Core_Session
::setStatus(ts("No updates have been saved."), ts('Not Saved'), 'alert');