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