| Conditions | 40 |
| Paths | 4108 |
| Total Lines | 196 |
| Code Lines | 110 |
| Lines | 21 |
| Ratio | 10.71 % |
| 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->ParentID = $this->ID; |
||
| 208 | |||
| 209 | // if saving is not disabled save now to generate the ID |
||
| 210 | if (!$this->DisableSaveSubmissions) { |
||
| 211 | $submittedForm->write(); |
||
| 212 | } |
||
| 213 | |||
| 214 | $attachments = array(); |
||
| 215 | $submittedFields = ArrayList::create(); |
||
| 216 | |||
| 217 | foreach ($this->Fields() as $field) { |
||
| 218 | if (!$field->showInReports()) { |
||
| 219 | continue; |
||
| 220 | } |
||
| 221 | |||
| 222 | $submittedField = $field->getSubmittedFormField(); |
||
| 223 | $submittedField->ParentID = $submittedForm->ID; |
||
| 224 | $submittedField->Name = $field->Name; |
||
| 225 | $submittedField->Title = $field->getField('Title'); |
||
| 226 | |||
| 227 | // save the value from the data |
||
| 228 | if ($field->hasMethod('getValueFromData')) { |
||
| 229 | $submittedField->Value = $field->getValueFromData($data); |
||
| 230 | } else { |
||
| 231 | if (isset($data[$field->Name])) { |
||
| 232 | $submittedField->Value = $data[$field->Name]; |
||
| 233 | } |
||
| 234 | } |
||
| 235 | |||
| 236 | if (!empty($data[$field->Name])) { |
||
| 237 | if (in_array(EditableFileField::class, $field->getClassAncestry())) { |
||
| 238 | if (!empty($_FILES[$field->Name]['name'])) { |
||
| 239 | $foldername = $field->getFormField()->getFolderName(); |
||
| 240 | |||
| 241 | // create the file from post data |
||
| 242 | $upload = Upload::create(); |
||
| 243 | $file = File::create(); |
||
| 244 | $file->ShowInSearch = 0; |
||
| 245 | try { |
||
| 246 | $upload->loadIntoFile($_FILES[$field->Name], $file, $foldername); |
||
| 247 | } catch (ValidationException $e) { |
||
| 248 | $validationResult = $e->getResult(); |
||
| 249 | $form->addErrorMessage($field->Name, $validationResult->message(), 'bad'); |
||
| 250 | Controller::curr()->redirectBack(); |
||
| 251 | return; |
||
| 252 | } |
||
| 253 | |||
| 254 | // write file to form field |
||
| 255 | $submittedField->UploadedFileID = $file->ID; |
||
| 256 | |||
| 257 | // attach a file only if lower than 1MB |
||
| 258 | if ($file->getAbsoluteSize() < 1024 * 1024 * 1) { |
||
| 259 | $attachments[] = $file; |
||
| 260 | } |
||
| 261 | } |
||
| 262 | } |
||
| 263 | } |
||
| 264 | |||
| 265 | $submittedField->extend('onPopulationFromField', $field); |
||
| 266 | |||
| 267 | if (!$this->DisableSaveSubmissions) { |
||
| 268 | $submittedField->write(); |
||
| 269 | } |
||
| 270 | |||
| 271 | $submittedFields->push($submittedField); |
||
| 272 | } |
||
| 273 | |||
| 274 | $emailData = [ |
||
| 275 | 'Sender' => Security::getCurrentUser(), |
||
| 276 | 'Fields' => $submittedFields |
||
| 277 | ]; |
||
| 278 | |||
| 279 | $this->extend('updateEmailData', $emailData, $attachments); |
||
| 280 | |||
| 281 | // email users on submit. |
||
| 282 | if ($recipients = $this->FilteredEmailRecipients($data, $form)) { |
||
| 283 | foreach ($recipients as $recipient) { |
||
| 284 | $email = Email::create() |
||
| 285 | ->setHTMLTemplate('email/SubmittedFormEmail.ss') |
||
| 286 | ->setPlainTemplate('email/SubmittedFormEmail.ss'); |
||
| 287 | |||
| 288 | $mergeFields = $this->getMergeFieldsMap($emailData['Fields']); |
||
| 289 | |||
| 290 | if ($attachments) { |
||
| 291 | foreach ($attachments as $file) { |
||
| 292 | if (!$file->ID != 0) { |
||
| 293 | continue; |
||
| 294 | } |
||
| 295 | |||
| 296 | $email->attachFile( |
||
| 297 | $file->Filename, |
||
| 298 | $file->Filename, |
||
| 299 | HTTP::get_mime_type($file->Filename) |
||
| 300 | ); |
||
| 301 | } |
||
| 302 | } |
||
| 303 | |||
| 304 | $parsedBody = SSViewer::execute_string($recipient->getEmailBodyContent(), $mergeFields); |
||
| 305 | |||
| 306 | if (!$recipient->SendPlain && $recipient->emailTemplateExists()) { |
||
| 307 | $email->setHTMLTemplate($recipient->EmailTemplate); |
||
| 308 | } |
||
| 309 | |||
| 310 | $email->setData($recipient); |
||
| 311 | foreach ($emailData as $key => $value) { |
||
| 312 | $email->addData($key, $value); |
||
| 313 | } |
||
| 314 | |||
| 315 | $email->setFrom($recipient->EmailFrom); |
||
| 316 | $email->setBody($parsedBody); |
||
| 317 | $email->setTo($recipient->EmailAddress); |
||
| 318 | $email->setSubject($recipient->EmailSubject); |
||
| 319 | |||
| 320 | if ($recipient->EmailReplyTo) { |
||
| 321 | $email->setReplyTo($recipient->EmailReplyTo); |
||
| 322 | } |
||
| 323 | |||
| 324 | // check to see if they are a dynamic reply to. eg based on a email field a user selected |
||
| 325 | View Code Duplication | if ($recipient->SendEmailFromField()) { |
|
| 326 | $submittedFormField = $submittedFields->find('Name', $recipient->SendEmailFromField()->Name); |
||
| 327 | |||
| 328 | if ($submittedFormField && is_string($submittedFormField->Value)) { |
||
| 329 | $email->setReplyTo($submittedFormField->Value); |
||
| 330 | } |
||
| 331 | } |
||
| 332 | // check to see if they are a dynamic reciever eg based on a dropdown field a user selected |
||
| 333 | View Code Duplication | if ($recipient->SendEmailToField()) { |
|
| 334 | $submittedFormField = $submittedFields->find('Name', $recipient->SendEmailToField()->Name); |
||
| 335 | |||
| 336 | if ($submittedFormField && is_string($submittedFormField->Value)) { |
||
| 337 | $email->setTo($submittedFormField->Value); |
||
| 338 | } |
||
| 339 | } |
||
| 340 | |||
| 341 | // check to see if there is a dynamic subject |
||
| 342 | View Code Duplication | if ($recipient->SendEmailSubjectField()) { |
|
| 343 | $submittedFormField = $submittedFields->find('Name', $recipient->SendEmailSubjectField()->Name); |
||
| 344 | |||
| 345 | if ($submittedFormField && trim($submittedFormField->Value)) { |
||
| 346 | $email->setSubject($submittedFormField->Value); |
||
| 347 | } |
||
| 348 | } |
||
| 349 | |||
| 350 | $this->extend('updateEmail', $email, $recipient, $emailData); |
||
| 351 | |||
| 352 | if ($recipient->SendPlain) { |
||
| 353 | $body = strip_tags($recipient->getEmailBodyContent()) . "\n"; |
||
| 354 | if (isset($emailData['Fields']) && !$recipient->HideFormData) { |
||
| 355 | foreach ($emailData['Fields'] as $Field) { |
||
| 356 | $body .= $Field->Title . ': ' . $Field->Value . " \n"; |
||
| 357 | } |
||
| 358 | } |
||
| 359 | |||
| 360 | $email->setBody($body); |
||
| 361 | $email->sendPlain(); |
||
| 362 | } else { |
||
| 363 | $email->send(); |
||
| 364 | } |
||
| 365 | } |
||
| 366 | } |
||
| 367 | |||
| 368 | $submittedForm->extend('updateAfterProcess'); |
||
| 369 | |||
| 370 | $session = $this->getRequest()->getSession(); |
||
| 371 | $session->clear("FormInfo.{$form->FormName()}.errors"); |
||
| 372 | $session->clear("FormInfo.{$form->FormName()}.data"); |
||
| 373 | |||
| 374 | $referrer = (isset($data['Referrer'])) ? '?referrer=' . urlencode($data['Referrer']) : ""; |
||
| 375 | |||
| 376 | // set a session variable from the security ID to stop people accessing |
||
| 377 | // the finished method directly. |
||
| 378 | if (!$this->DisableAuthenicatedFinishAction) { |
||
| 379 | if (isset($data['SecurityID'])) { |
||
| 380 | $session->set('FormProcessed', $data['SecurityID']); |
||
| 381 | } else { |
||
| 382 | // if the form has had tokens disabled we still need to set FormProcessed |
||
| 383 | // to allow us to get through the finshed method |
||
| 384 | if (!$this->Form()->getSecurityToken()->isEnabled()) { |
||
| 385 | $randNum = rand(1, 1000); |
||
| 386 | $randHash = md5($randNum); |
||
| 387 | $session->set('FormProcessed', $randHash); |
||
| 388 | $session->set('FormProcessedNum', $randNum); |
||
| 389 | } |
||
| 390 | } |
||
| 391 | } |
||
| 392 | |||
| 393 | if (!$this->DisableSaveSubmissions) { |
||
| 394 | $session->set('userformssubmission'. $this->ID, $submittedForm->ID); |
||
| 395 | } |
||
| 396 | |||
| 397 | return $this->redirect($this->Link('finished') . $referrer . $this->config()->get('finished_anchor')); |
||
| 398 | } |
||
| 399 | |||
| 501 |
This check marks private properties in classes that are never used. Those properties can be removed.