Replace CRM_Utils_Array::value with ?? in variable assignments
[civicrm-core.git] / CRM / Contact / Form / Task / Batch.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * This class provides the functionality for batch profile update.
20 */
21 class CRM_Contact_Form_Task_Batch extends CRM_Contact_Form_Task {
22
23 /**
24 * The title of the group.
25 *
26 * @var string
27 */
28 protected $_title;
29
30 /**
31 * Maximum contacts that should be allowed to update.
32 * @var int
33 */
34 protected $_maxContacts = 100;
35
36 /**
37 * Maximum profile fields that will be displayed.
38 * @var int
39 */
40 protected $_maxFields = 9;
41
42 /**
43 * Variable to store redirect path.
44 * @var string
45 */
46 protected $_userContext;
47
48 /**
49 * When not to reset sort_name.
50 * @var bool
51 */
52 protected $_preserveDefault = TRUE;
53
54 /**
55 * Build all the data structures needed to build the form.
56 */
57 public function preProcess() {
58 // initialize the task and row fields
59 parent::preProcess();
60 }
61
62 /**
63 * Build the form object.
64 */
65 public function buildQuickForm() {
66 $ufGroupId = $this->get('ufGroupId');
67
68 if (!$ufGroupId) {
69 CRM_Core_Error::fatal('ufGroupId is missing');
70 }
71 $this->_title = ts('Update multiple contacts') . ' - ' . CRM_Core_BAO_UFGroup::getTitle($ufGroupId);
72 CRM_Utils_System::setTitle($this->_title);
73
74 $this->addDefaultButtons(ts('Save'));
75 $this->_fields = CRM_Core_BAO_UFGroup::getFields($ufGroupId, FALSE, CRM_Core_Action::VIEW);
76
77 // remove file type field and then limit fields
78 $suppressFields = FALSE;
79 $removehtmlTypes = ['File'];
80 foreach ($this->_fields as $name => $field) {
81 if ($cfID = CRM_Core_BAO_CustomField::getKeyID($name) &&
82 in_array($this->_fields[$name]['html_type'], $removehtmlTypes)
83 ) {
84 $suppressFields = TRUE;
85 unset($this->_fields[$name]);
86 }
87 }
88
89 //FIX ME: phone ext field is added at the end and it gets removed because of below code
90 //$this->_fields = array_slice($this->_fields, 0, $this->_maxFields);
91
92 $this->addButtons([
93 [
94 'type' => 'submit',
95 'name' => ts('Update Contact(s)'),
96 'isDefault' => TRUE,
97 ],
98 [
99 'type' => 'cancel',
100 'name' => ts('Cancel'),
101 ],
102 ]);
103
104 $this->assign('profileTitle', $this->_title);
105 $this->assign('componentIds', $this->_contactIds);
106
107 // if below fields are missing we should not reset sort name / display name
108 // CRM-6794
109 $preserveDefaultsArray = [
110 'first_name',
111 'last_name',
112 'middle_name',
113 'organization_name',
114 'prefix_id',
115 'suffix_id',
116 'household_name',
117 ];
118
119 foreach ($this->_contactIds as $contactId) {
120 $profileFields = $this->_fields;
121 CRM_Core_BAO_Address::checkContactSharedAddressFields($profileFields, $contactId);
122 foreach ($profileFields as $name => $field) {
123 CRM_Core_BAO_UFGroup::buildProfile($this, $field, NULL, $contactId);
124
125 if (in_array($field['name'], $preserveDefaultsArray)) {
126 $this->_preserveDefault = FALSE;
127 }
128 }
129 }
130
131 $this->assign('fields', $this->_fields);
132
133 // don't set the status message when form is submitted.
134 $buttonName = $this->controller->getButtonName('submit');
135
136 if ($suppressFields && $buttonName != '_qf_BatchUpdateProfile_next') {
137 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');
138 }
139
140 $this->addDefaultButtons(ts('Update Contacts'));
141 $this->addFormRule(['CRM_Contact_Form_Task_Batch', 'formRule']);
142 }
143
144 /**
145 * Set default values for the form.
146 *
147 *
148 * @return array
149 */
150 public function setDefaultValues() {
151 if (empty($this->_fields)) {
152 return NULL;
153 }
154
155 $defaults = $sortName = [];
156 foreach ($this->_contactIds as $contactId) {
157 $details[$contactId] = [];
158
159 //build sortname
160 $sortName[$contactId] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact',
161 $contactId,
162 'sort_name'
163 );
164
165 CRM_Core_BAO_UFGroup::setProfileDefaults($contactId, $this->_fields, $defaults, FALSE);
166 }
167
168 $this->assign('sortName', $sortName);
169
170 return $defaults;
171 }
172
173 /**
174 * Global form rule.
175 *
176 * @param array $fields
177 * The input form values.
178 *
179 * @return bool|array
180 * true if no errors, else array of errors
181 */
182 public static function formRule($fields) {
183 $errors = [];
184 $externalIdentifiers = [];
185 foreach ($fields['field'] as $componentId => $field) {
186 foreach ($field as $fieldName => $fieldValue) {
187 if ($fieldName == 'external_identifier') {
188 if (in_array($fieldValue, $externalIdentifiers)) {
189 $errors["field[$componentId][external_identifier]"] = ts('Duplicate value for External ID.');
190 }
191 else {
192 $externalIdentifiers[$componentId] = $fieldValue;
193 }
194 }
195 }
196 }
197
198 return $errors;
199 }
200
201 /**
202 * Process the form after the input has been submitted and validated.
203 */
204 public function postProcess() {
205 $params = $this->exportValues();
206
207 // @todo extract submit functions &
208 // extend CRM_Event_Form_Task_BatchTest::testSubmit with a data provider to test
209 // handling of custom data, specifically checkbox fields.
210 $ufGroupId = $this->get('ufGroupId');
211 $notify = NULL;
212 $inValidSubtypeCnt = 0;
213 //send profile notification email if 'notify' field is set
214 $notify = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $ufGroupId, 'notify');
215 foreach ($params['field'] as $key => $value) {
216
217 //CRM-5521
218 //validate subtype before updating
219 if (!empty($value['contact_sub_type']) && !CRM_Contact_BAO_ContactType::isAllowEdit($key)) {
220 unset($value['contact_sub_type']);
221 $inValidSubtypeCnt++;
222 }
223
224 $value['preserveDBName'] = $this->_preserveDefault;
225
226 //parse street address, CRM-7768
227 self::parseStreetAddress($value, $this);
228
229 CRM_Contact_BAO_Contact::createProfileContact($value, $this->_fields, $key, NULL, $ufGroupId, NULL, TRUE);
230 if ($notify) {
231 $values = CRM_Core_BAO_UFGroup::checkFieldsEmptyValues($ufGroupId, $key, NULL);
232 CRM_Core_BAO_UFGroup::commonSendMail($key, $values);
233 }
234 }
235
236 CRM_Core_Session::setStatus('', ts("Updates Saved"), 'success');
237 if ($inValidSubtypeCnt) {
238 CRM_Core_Session::setStatus(ts('Contact Subtype field of 1 contact has not been updated.', [
239 'plural' => 'Contact Subtype field of %count contacts has not been updated.',
240 'count' => $inValidSubtypeCnt,
241 ]), ts('Invalid Subtype'));
242 }
243 }
244
245 /**
246 * Parse street address.
247 *
248 * @param array $contactValues
249 * Contact values.
250 * @param CRM_Core_Form $form
251 * Form object.
252 */
253 public static function parseStreetAddress(&$contactValues, &$form) {
254 if (!is_array($contactValues) || !is_array($form->_fields)) {
255 return;
256 }
257
258 static $parseAddress;
259 $addressFldKey = 'street_address';
260 if (!isset($parseAddress)) {
261 $parseAddress = FALSE;
262 foreach ($form->_fields as $key => $fld) {
263 if (strpos($key, $addressFldKey) !== FALSE) {
264 $parseAddress = CRM_Utils_Array::value('street_address_parsing',
265 CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
266 'address_options'
267 ),
268 FALSE
269 );
270 break;
271 }
272 }
273 }
274
275 if (!$parseAddress) {
276 return;
277 }
278
279 $allParseValues = [];
280 foreach ($contactValues as $key => $value) {
281 if (strpos($key, $addressFldKey) !== FALSE) {
282 $locTypeId = substr($key, strlen($addressFldKey) + 1);
283
284 // parse address field.
285 $parsedFields = CRM_Core_BAO_Address::parseStreetAddress($value);
286
287 //street address consider to be parsed properly,
288 //If we get street_name and street_number.
289 if (empty($parsedFields['street_name']) || empty($parsedFields['street_number'])) {
290 $parsedFields = array_fill_keys(array_keys($parsedFields), '');
291 }
292
293 //merge parse values.
294 foreach ($parsedFields as $fldKey => $parseVal) {
295 if ($locTypeId) {
296 $fldKey .= "-{$locTypeId}";
297 }
298 $allParseValues[$fldKey] = $parseVal;
299 }
300 }
301 }
302
303 //finally merge all parse values
304 if (!empty($allParseValues)) {
305 $contactValues += $allParseValues;
306 }
307 }
308
309 }