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.
37 class CRM_Contact_Form_Task_Batch
extends CRM_Contact_Form_Task
{
40 * The title of the group.
47 * Maximum contacts that should be allowed to update.
50 protected $_maxContacts = 100;
53 * Maximum profile fields that will be displayed.
56 protected $_maxFields = 9;
59 * Variable to store redirect path.
62 protected $_userContext;
65 * When not to reset sort_name.
68 protected $_preserveDefault = TRUE;
71 * Build all the data structures needed to build the form.
73 public function preProcess() {
74 // initialize the task and row fields
79 * Build the form object.
81 public function buildQuickForm() {
82 $ufGroupId = $this->get('ufGroupId');
85 CRM_Core_Error
::fatal('ufGroupId is missing');
87 $this->_title
= ts('Update multiple contacts') . ' - ' . CRM_Core_BAO_UFGroup
::getTitle($ufGroupId);
88 CRM_Utils_System
::setTitle($this->_title
);
90 $this->addDefaultButtons(ts('Save'));
91 $this->_fields
= CRM_Core_BAO_UFGroup
::getFields($ufGroupId, FALSE, CRM_Core_Action
::VIEW
);
93 // remove file type field and then limit fields
94 $suppressFields = FALSE;
95 $removehtmlTypes = ['File'];
96 foreach ($this->_fields
as $name => $field) {
97 if ($cfID = CRM_Core_BAO_CustomField
::getKeyID($name) &&
98 in_array($this->_fields
[$name]['html_type'], $removehtmlTypes)
100 $suppressFields = TRUE;
101 unset($this->_fields
[$name]);
105 //FIX ME: phone ext field is added at the end and it gets removed because of below code
106 //$this->_fields = array_slice($this->_fields, 0, $this->_maxFields);
111 'name' => ts('Update Contact(s)'),
116 'name' => ts('Cancel'),
120 $this->assign('profileTitle', $this->_title
);
121 $this->assign('componentIds', $this->_contactIds
);
123 // if below fields are missing we should not reset sort name / display name
125 $preserveDefaultsArray = [
135 foreach ($this->_contactIds
as $contactId) {
136 $profileFields = $this->_fields
;
137 CRM_Core_BAO_Address
::checkContactSharedAddressFields($profileFields, $contactId);
138 foreach ($profileFields as $name => $field) {
139 CRM_Core_BAO_UFGroup
::buildProfile($this, $field, NULL, $contactId);
141 if (in_array($field['name'], $preserveDefaultsArray)) {
142 $this->_preserveDefault
= FALSE;
147 $this->assign('fields', $this->_fields
);
149 // don't set the status message when form is submitted.
150 $buttonName = $this->controller
->getButtonName('submit');
152 if ($suppressFields && $buttonName != '_qf_BatchUpdateProfile_next') {
153 CRM_Core_Session
::setStatus(ts("File type field(s) in the selected profile are not supported for Update multiple contacts."), ts('Some Fields Excluded'), 'info');
156 $this->addDefaultButtons(ts('Update Contacts'));
157 $this->addFormRule(['CRM_Contact_Form_Task_Batch', 'formRule']);
161 * Set default values for the form.
166 public function setDefaultValues() {
167 if (empty($this->_fields
)) {
171 $defaults = $sortName = [];
172 foreach ($this->_contactIds
as $contactId) {
173 $details[$contactId] = [];
176 $sortName[$contactId] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact',
181 CRM_Core_BAO_UFGroup
::setProfileDefaults($contactId, $this->_fields
, $defaults, FALSE);
184 $this->assign('sortName', $sortName);
192 * @param array $fields
193 * The input form values.
196 * true if no errors, else array of errors
198 public static function formRule($fields) {
200 $externalIdentifiers = [];
201 foreach ($fields['field'] as $componentId => $field) {
202 foreach ($field as $fieldName => $fieldValue) {
203 if ($fieldName == 'external_identifier') {
204 if (in_array($fieldValue, $externalIdentifiers)) {
205 $errors["field[$componentId][external_identifier]"] = ts('Duplicate value for External ID.');
208 $externalIdentifiers[$componentId] = $fieldValue;
218 * Process the form after the input has been submitted and validated.
220 public function postProcess() {
221 $params = $this->exportValues();
223 // @todo extract submit functions &
224 // extend CRM_Event_Form_Task_BatchTest::testSubmit with a data provider to test
225 // handling of custom data, specifically checkbox fields.
226 $ufGroupId = $this->get('ufGroupId');
228 $inValidSubtypeCnt = 0;
229 //send profile notification email if 'notify' field is set
230 $notify = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFGroup', $ufGroupId, 'notify');
231 foreach ($params['field'] as $key => $value) {
234 //validate subtype before updating
235 if (!empty($value['contact_sub_type']) && !CRM_Contact_BAO_ContactType
::isAllowEdit($key)) {
236 unset($value['contact_sub_type']);
237 $inValidSubtypeCnt++
;
240 $value['preserveDBName'] = $this->_preserveDefault
;
242 //parse street address, CRM-7768
243 self
::parseStreetAddress($value, $this);
245 CRM_Contact_BAO_Contact
::createProfileContact($value, $this->_fields
, $key, NULL, $ufGroupId, NULL, TRUE);
247 $values = CRM_Core_BAO_UFGroup
::checkFieldsEmptyValues($ufGroupId, $key, NULL);
248 CRM_Core_BAO_UFGroup
::commonSendMail($key, $values);
252 CRM_Core_Session
::setStatus('', ts("Updates Saved"), 'success');
253 if ($inValidSubtypeCnt) {
254 CRM_Core_Session
::setStatus(ts('Contact Subtype field of 1 contact has not been updated.', [
255 'plural' => 'Contact Subtype field of %count contacts has not been updated.',
256 'count' => $inValidSubtypeCnt,
257 ]), ts('Invalid Subtype'));
262 * Parse street address.
264 * @param array $contactValues
266 * @param CRM_Core_Form $form
269 public static function parseStreetAddress(&$contactValues, &$form) {
270 if (!is_array($contactValues) ||
!is_array($form->_fields
)) {
274 static $parseAddress;
275 $addressFldKey = 'street_address';
276 if (!isset($parseAddress)) {
277 $parseAddress = FALSE;
278 foreach ($form->_fields
as $key => $fld) {
279 if (strpos($key, $addressFldKey) !== FALSE) {
280 $parseAddress = CRM_Utils_Array
::value('street_address_parsing',
281 CRM_Core_BAO_Setting
::valueOptions(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
291 if (!$parseAddress) {
295 $allParseValues = [];
296 foreach ($contactValues as $key => $value) {
297 if (strpos($key, $addressFldKey) !== FALSE) {
298 $locTypeId = substr($key, strlen($addressFldKey) +
1);
300 // parse address field.
301 $parsedFields = CRM_Core_BAO_Address
::parseStreetAddress($value);
303 //street address consider to be parsed properly,
304 //If we get street_name and street_number.
305 if (empty($parsedFields['street_name']) ||
empty($parsedFields['street_number'])) {
306 $parsedFields = array_fill_keys(array_keys($parsedFields), '');
309 //merge parse values.
310 foreach ($parsedFields as $fldKey => $parseVal) {
312 $fldKey .= "-{$locTypeId}";
314 $allParseValues[$fldKey] = $parseVal;
319 //finally merge all parse values
320 if (!empty($allParseValues)) {
321 $contactValues +
= $allParseValues;