3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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-2019
35 * This class provides the functionality for batch profile update for members
37 class CRM_Member_Form_Task_Batch
extends CRM_Member_Form_Task
{
40 * The title of the group.
47 * Maximum profile fields that will be displayed.
50 protected $_maxFields = 9;
53 * Variable to store redirect path.
56 protected $_userContext;
59 * Build all the data structures needed to build the form.
63 public function preProcess() {
64 // initialize the task and row fields
67 //get the contact read only fields to display.
68 $readOnlyFields = array_merge(['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
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
79 $this->assign('contactDetails', $contactDetails);
80 $this->assign('readOnlyFields', $readOnlyFields);
84 * Build the form object.
89 public function buildQuickForm() {
90 $ufGroupId = $this->get('ufGroupId');
93 CRM_Core_Error
::fatal('ufGroupId is missing');
95 $this->_title
= ts('Update multiple memberships') . ' - ' . CRM_Core_BAO_UFGroup
::getTitle($ufGroupId);
96 CRM_Utils_System
::setTitle($this->_title
);
98 $this->addDefaultButtons(ts('Save'));
100 $this->_fields
= CRM_Core_BAO_UFGroup
::getFields($ufGroupId, FALSE, CRM_Core_Action
::VIEW
);
102 // remove file type field and then limit fields
103 $suppressFields = FALSE;
104 $removehtmlTypes = ['File'];
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)
109 $suppressFields = TRUE;
110 unset($this->_fields
[$name]);
113 //fix to reduce size as we are using this field in grid
114 if (is_array($field['attributes']) && !empty($this->_fields
[$name]['attributes']['size']) && $this->_fields
[$name]['attributes']['size'] > 19) {
115 //shrink class to "form-text-medium"
116 $this->_fields
[$name]['attributes']['size'] = 19;
120 $this->_fields
= array_slice($this->_fields
, 0, $this->_maxFields
);
125 'name' => ts('Update Members(s)'),
130 'name' => ts('Cancel'),
134 $this->assign('profileTitle', $this->_title
);
135 $this->assign('componentIds', $this->_memberIds
);
137 //load all campaigns.
138 if (array_key_exists('member_campaign_id', $this->_fields
)) {
139 $this->_componentCampaigns
= [];
140 CRM_Core_PseudoConstant
::populate($this->_componentCampaigns
,
141 'CRM_Member_DAO_Membership',
142 TRUE, 'campaign_id', 'id',
143 ' id IN (' . implode(' , ', array_values($this->_memberIds
)) . ' ) '
147 $customFields = CRM_Core_BAO_CustomField
::getFields('Membership');
148 foreach ($this->_memberIds
as $memberId) {
149 $typeId = CRM_Core_DAO
::getFieldValue("CRM_Member_DAO_Membership", $memberId, 'membership_type_id');
150 foreach ($this->_fields
as $name => $field) {
151 if ($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($name)) {
152 $customValue = CRM_Utils_Array
::value($customFieldID, $customFields);
153 $entityColumnValue = [];
154 if (!empty($customValue['extends_entity_column_value'])) {
155 $entityColumnValue = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
156 $customValue['extends_entity_column_value']
159 if ((CRM_Utils_Array
::value($typeId, $entityColumnValue)) ||
160 CRM_Utils_System
::isNull($entityColumnValue[$typeId])
162 CRM_Core_BAO_UFGroup
::buildProfile($this, $field, NULL, $memberId);
166 // handle non custom fields
167 CRM_Core_BAO_UFGroup
::buildProfile($this, $field, NULL, $memberId);
172 $this->assign('fields', $this->_fields
);
174 // don't set the status message when form is submitted.
175 $buttonName = $this->controller
->getButtonName('submit');
177 if ($suppressFields && $buttonName != '_qf_Batch_next') {
178 CRM_Core_Session
::setStatus(ts("File type field(s) in the selected profile are not supported for Update multiple memberships."), ts('Unsupported Field Type'), 'error');
181 $this->addDefaultButtons(ts('Update Memberships'));
185 * Set default values for the form.
190 public function setDefaultValues() {
191 if (empty($this->_fields
)) {
196 foreach ($this->_memberIds
as $memberId) {
197 CRM_Core_BAO_UFGroup
::setProfileDefaults(NULL, $this->_fields
, $defaults, FALSE, $memberId, 'Membership');
204 * Process the form after the input has been submitted and validated.
209 public function postProcess() {
210 $params = $this->exportValues();
211 // @todo extract submit functions &
212 // extend CRM_Event_Form_Task_BatchTest::testSubmit with a data provider to test
213 // handling of custom data, specifically checkbox fields.
216 'membership_start_date',
217 'membership_end_date',
219 if (isset($params['field'])) {
221 foreach ($params['field'] as $key => $value) {
222 $ids['membership'] = $key;
223 if (!empty($value['membership_source'])) {
224 $value['source'] = $value['membership_source'];
227 if (!empty($value['membership_type'])) {
228 $membershipTypeId = $value['membership_type_id'] = $value['membership_type'][1];
231 unset($value['membership_source']);
232 unset($value['membership_type']);
234 //Get the membership status
235 $value['status_id'] = (CRM_Utils_Array
::value('membership_status', $value)) ?
$value['membership_status'] : CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_Membership', $key, 'status_id');
236 unset($value['membership_status']);
237 foreach ($dates as $val) {
238 if (isset($value[$val])) {
239 $value[$val] = CRM_Utils_Date
::processDate($value[$val]);
242 if (empty($customFields)) {
243 if (empty($value['membership_type_id'])) {
244 $membershipTypeId = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_Membership', $key, 'membership_type_id');
247 // membership type custom data
248 $customFields = CRM_Core_BAO_CustomField
::getFields('Membership', FALSE, FALSE, $membershipTypeId);
250 $customFields = CRM_Utils_Array
::crmArrayMerge($customFields,
251 CRM_Core_BAO_CustomField
::getFields('Membership',
252 FALSE, FALSE, NULL, NULL, TRUE
256 //check for custom data
257 $value['custom'] = CRM_Core_BAO_CustomField
::postProcess($params['field'][$key],
263 $membership = CRM_Member_BAO_Membership
::add($value, $ids);
265 // add custom field values
266 if (!empty($value['custom']) &&
267 is_array($value['custom'])
269 CRM_Core_BAO_CustomValueTable
::store($value['custom'], 'civicrm_membership', $membership->id
);
273 CRM_Core_Session
::setStatus(ts("Your updates have been saved."), ts('Saved'), 'success');
276 CRM_Core_Session
::setStatus(ts("No updates have been saved."), ts('Not Saved'), 'alert');