| Conditions | 40 |
| Paths | 4108 |
| Total Lines | 198 |
| Code Lines | 110 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 203 | public function process($data, $form) |
||
| 204 | { |
||
| 205 | $submittedForm = SubmittedForm::create(); |
||
| 206 | $submittedForm->SubmittedByID = Security::getCurrentUser() ? Security::getCurrentUser()->ID : 0; |
||
| 207 | $submittedForm->ParentClass = get_class($this->data()); |
||
| 208 | $submittedForm->ParentID = $this->ID; |
||
| 209 | |||
| 210 | // if saving is not disabled save now to generate the ID |
||
| 211 | if (!$this->DisableSaveSubmissions) { |
||
| 212 | $submittedForm->write(); |
||
| 213 | } |
||
| 214 | |||
| 215 | $attachments = []; |
||
| 216 | $submittedFields = ArrayList::create(); |
||
| 217 | |||
| 218 | foreach ($this->data()->Fields() as $field) { |
||
| 219 | if (!$field->showInReports()) { |
||
| 220 | continue; |
||
| 221 | } |
||
| 222 | |||
| 223 | $submittedField = $field->getSubmittedFormField(); |
||
| 224 | $submittedField->ParentID = $submittedForm->ID; |
||
| 225 | $submittedField->Name = $field->Name; |
||
| 226 | $submittedField->Title = $field->getField('Title'); |
||
| 227 | |||
| 228 | // save the value from the data |
||
| 229 | if ($field->hasMethod('getValueFromData')) { |
||
| 230 | $submittedField->Value = $field->getValueFromData($data); |
||
| 231 | } else { |
||
| 232 | if (isset($data[$field->Name])) { |
||
| 233 | $submittedField->Value = $data[$field->Name]; |
||
| 234 | } |
||
| 235 | } |
||
| 236 | |||
| 237 | if (!empty($data[$field->Name])) { |
||
| 238 | if (in_array(EditableFileField::class, $field->getClassAncestry())) { |
||
| 239 | if (!empty($_FILES[$field->Name]['name'])) { |
||
| 240 | $foldername = $field->getFormField()->getFolderName(); |
||
| 241 | |||
| 242 | // create the file from post data |
||
| 243 | $upload = Upload::create(); |
||
| 244 | $file = File::create(); |
||
| 245 | $file->ShowInSearch = 0; |
||
| 246 | try { |
||
| 247 | $upload->loadIntoFile($_FILES[$field->Name], $file, $foldername); |
||
| 248 | } catch (ValidationException $e) { |
||
| 249 | $validationResult = $e->getResult(); |
||
| 250 | $form->addErrorMessage($field->Name, $validationResult->message(), 'bad'); |
||
| 251 | Controller::curr()->redirectBack(); |
||
| 252 | return; |
||
| 253 | } |
||
| 254 | |||
| 255 | // write file to form field |
||
| 256 | $submittedField->UploadedFileID = $file->ID; |
||
| 257 | |||
| 258 | // attach a file only if lower than 1MB |
||
| 259 | if ($file->getAbsoluteSize() < 1024 * 1024 * 1) { |
||
| 260 | $attachments[] = $file; |
||
| 261 | } |
||
| 262 | } |
||
| 263 | } |
||
| 264 | } |
||
| 265 | |||
| 266 | $submittedField->extend('onPopulationFromField', $field); |
||
| 267 | |||
| 268 | if (!$this->DisableSaveSubmissions) { |
||
| 269 | $submittedField->write(); |
||
| 270 | } |
||
| 271 | |||
| 272 | $submittedFields->push($submittedField); |
||
| 273 | } |
||
| 274 | |||
| 275 | $emailData = [ |
||
| 276 | 'Sender' => Security::getCurrentUser(), |
||
| 277 | 'HideFormData' => false, |
||
| 278 | 'Fields' => $submittedFields, |
||
| 279 | 'Body' => '', |
||
| 280 | ]; |
||
| 281 | |||
| 282 | $this->extend('updateEmailData', $emailData, $attachments); |
||
| 283 | |||
| 284 | // email users on submit. |
||
| 285 | if ($recipients = $this->FilteredEmailRecipients($data, $form)) { |
||
| 286 | foreach ($recipients as $recipient) { |
||
| 287 | $email = Email::create() |
||
| 288 | ->setHTMLTemplate('email/SubmittedFormEmail') |
||
| 289 | ->setPlainTemplate('email/SubmittedFormEmail'); |
||
| 290 | |||
| 291 | if ($attachments) { |
||
| 292 | foreach ($attachments as $file) { |
||
| 293 | /** @var File $file */ |
||
| 294 | if ((int) $file->ID === 0) { |
||
| 295 | continue; |
||
| 296 | } |
||
| 297 | |||
| 298 | $email->addAttachmentFromData( |
||
| 299 | $file->getString(), |
||
| 300 | $file->getFilename(), |
||
| 301 | $file->getMimeType() |
||
| 302 | ); |
||
| 303 | } |
||
| 304 | } |
||
| 305 | |||
| 306 | if (!$recipient->SendPlain && $recipient->emailTemplateExists()) { |
||
| 307 | $email->setHTMLTemplate($recipient->EmailTemplate); |
||
| 308 | } |
||
| 309 | |||
| 310 | // Add specific template data for the current recipient |
||
| 311 | $emailData['HideFormData'] = (bool) $recipient->HideFormData; |
||
| 312 | $emailData['Body'] = $recipient->getEmailBodyContent(); |
||
| 313 | |||
| 314 | // Push the template data to the Email's data |
||
| 315 | foreach ($emailData as $key => $value) { |
||
| 316 | $email->addData($key, $value); |
||
| 317 | } |
||
| 318 | |||
| 319 | $email->setFrom($recipient->EmailFrom); |
||
| 320 | $email->setTo($recipient->EmailAddress); |
||
| 321 | $email->setSubject($recipient->EmailSubject); |
||
| 322 | |||
| 323 | if ($recipient->EmailReplyTo) { |
||
| 324 | $email->setReplyTo($recipient->EmailReplyTo); |
||
| 325 | } |
||
| 326 | |||
| 327 | // check to see if they are a dynamic reply to. eg based on a email field a user selected |
||
| 328 | if ($recipient->SendEmailFromField()) { |
||
| 329 | $submittedFormField = $submittedFields->find('Name', $recipient->SendEmailFromField()->Name); |
||
| 330 | |||
| 331 | if ($submittedFormField && is_string($submittedFormField->Value)) { |
||
| 332 | $email->setReplyTo($submittedFormField->Value); |
||
| 333 | } |
||
| 334 | } |
||
| 335 | // check to see if they are a dynamic reciever eg based on a dropdown field a user selected |
||
| 336 | if ($recipient->SendEmailToField()) { |
||
| 337 | $submittedFormField = $submittedFields->find('Name', $recipient->SendEmailToField()->Name); |
||
| 338 | |||
| 339 | if ($submittedFormField && is_string($submittedFormField->Value)) { |
||
| 340 | $email->setTo($submittedFormField->Value); |
||
| 341 | } |
||
| 342 | } |
||
| 343 | |||
| 344 | // check to see if there is a dynamic subject |
||
| 345 | if ($recipient->SendEmailSubjectField()) { |
||
| 346 | $submittedFormField = $submittedFields->find('Name', $recipient->SendEmailSubjectField()->Name); |
||
| 347 | |||
| 348 | if ($submittedFormField && trim($submittedFormField->Value)) { |
||
| 349 | $email->setSubject($submittedFormField->Value); |
||
| 350 | } |
||
| 351 | } |
||
| 352 | |||
| 353 | $this->extend('updateEmail', $email, $recipient, $emailData); |
||
| 354 | |||
| 355 | if ((bool)$recipient->SendPlain) { |
||
| 356 | $body = strip_tags($recipient->getEmailBodyContent()) . "\n"; |
||
| 357 | if (isset($emailData['Fields']) && !$emailData['HideFormData']) { |
||
| 358 | foreach ($emailData['Fields'] as $field) { |
||
| 359 | $body .= $field->Title . ': ' . $field->Value . " \n"; |
||
| 360 | } |
||
| 361 | } |
||
| 362 | |||
| 363 | $email->setBody($body); |
||
| 364 | $email->sendPlain(); |
||
| 365 | } else { |
||
| 366 | $email->send(); |
||
| 367 | } |
||
| 368 | } |
||
| 369 | } |
||
| 370 | |||
| 371 | $submittedForm->extend('updateAfterProcess'); |
||
| 372 | |||
| 373 | $session = $this->getRequest()->getSession(); |
||
| 374 | $session->clear("FormInfo.{$form->FormName()}.errors"); |
||
| 375 | $session->clear("FormInfo.{$form->FormName()}.data"); |
||
| 376 | |||
| 377 | $referrer = (isset($data['Referrer'])) ? '?referrer=' . urlencode($data['Referrer']) : ""; |
||
| 378 | |||
| 379 | // set a session variable from the security ID to stop people accessing |
||
| 380 | // the finished method directly. |
||
| 381 | if (!$this->DisableAuthenicatedFinishAction) { |
||
| 382 | if (isset($data['SecurityID'])) { |
||
| 383 | $session->set('FormProcessed', $data['SecurityID']); |
||
| 384 | } else { |
||
| 385 | // if the form has had tokens disabled we still need to set FormProcessed |
||
| 386 | // to allow us to get through the finshed method |
||
| 387 | if (!$this->Form()->getSecurityToken()->isEnabled()) { |
||
| 388 | $randNum = rand(1, 1000); |
||
| 389 | $randHash = md5($randNum); |
||
| 390 | $session->set('FormProcessed', $randHash); |
||
| 391 | $session->set('FormProcessedNum', $randNum); |
||
| 392 | } |
||
| 393 | } |
||
| 394 | } |
||
| 395 | |||
| 396 | if (!$this->DisableSaveSubmissions) { |
||
| 397 | $session->set('userformssubmission'. $this->ID, $submittedForm->ID); |
||
| 398 | } |
||
| 399 | |||
| 400 | return $this->redirect($this->Link('finished') . $referrer . $this->config()->get('finished_anchor')); |
||
| 401 | } |
||
| 487 |