| Conditions | 28 |
| Paths | 1729 |
| Total Lines | 244 |
| Code Lines | 131 |
| Lines | 0 |
| Ratio | 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 |
||
| 355 | public function action_sendinvitemails(){ |
||
| 356 | global $db; |
||
| 357 | global $sugar_config; |
||
| 358 | global $mod_strings; |
||
| 359 | |||
| 360 | $id = $_GET['record']; |
||
| 361 | //get event |
||
| 362 | $event = new FP_events(); |
||
| 363 | $event->retrieve($id); |
||
| 364 | |||
| 365 | $event->load_relationship('fp_events_contacts'); // get related contacts |
||
| 366 | $event->load_relationship('fp_events_prospects_1'); //get related targets |
||
| 367 | $event->load_relationship('fp_events_leads_1'); //get related leads |
||
| 368 | |||
| 369 | //Count the number of delegates linked to the event that have not yet been invited |
||
| 370 | $query = "SELECT * FROM fp_events_contacts_c WHERE fp_events_contactsfp_events_ida='".$event->id."' AND (invite_status='Not Invited' OR invite_status='' OR invite_status IS NULL) AND deleted='0'"; |
||
| 371 | $result = $db->query($query); |
||
| 372 | $contact_count = $db->getRowCount($result);//count contacts |
||
| 373 | |||
| 374 | $query = "SELECT * FROM fp_events_prospects_1_c WHERE fp_events_prospects_1fp_events_ida='".$event->id."' AND (invite_status='Not Invited' OR invite_status='' OR invite_status IS NULL) AND deleted='0'"; |
||
| 375 | $result = $db->query($query); |
||
| 376 | $prospect_count = $db->getRowCount($result);//count targets |
||
| 377 | |||
| 378 | $query = "SELECT * FROM fp_events_leads_1_c WHERE fp_events_leads_1fp_events_ida='".$event->id."' AND (invite_status='Not Invited' OR invite_status='' OR invite_status IS NULL) AND deleted='0'"; |
||
| 379 | $result = $db->query($query); |
||
| 380 | $lead_count = $db->getRowCount($result);//count leads |
||
| 381 | |||
| 382 | $delegate_count = $contact_count + $prospect_count + $lead_count;//Total up delegates |
||
| 383 | $invite_count = 0; //used to count the number of emails sent |
||
| 384 | $error_count = 0; //used to count the number of failed email attempts |
||
| 385 | |||
| 386 | |||
| 387 | //loop through related contacts |
||
| 388 | foreach ($event->fp_events_contacts->getBeans() as $contact) { |
||
| 389 | |||
| 390 | //Get accept status of contact |
||
| 391 | $query = 'SELECT invite_status FROM fp_events_contacts_c WHERE fp_events_contactsfp_events_ida="'.$event->id.'" AND fp_events_contactscontacts_idb="'.$contact->id.'"'; |
||
| 392 | $status = $db->getOne($query); |
||
| 393 | |||
| 394 | if($status == null || $status == '' || $status == 'Not Invited'){ |
||
| 395 | |||
| 396 | $invite_count ++; |
||
| 397 | //set email links |
||
| 398 | $event->link = "<a href='".$sugar_config['site_url']."/index.php?entryPoint=responseEntryPoint&event=".$event->id."&delegate=".$contact->id."&type=c&response=accept'>Accept</a>"; |
||
| 399 | $event->link_declined = "<a href='".$sugar_config['site_url']."/index.php?entryPoint=responseEntryPoint&event=".$event->id."&delegate=".$contact->id."&type=c&response=decline'>Decline</a>"; |
||
| 400 | |||
| 401 | //Get the TO name and e-mail address for the message |
||
| 402 | $rcpt_name = $contact->first_name . ' ' . $contact->last_name; |
||
| 403 | $rcpt_email = $contact->email1; |
||
| 404 | |||
| 405 | $emailTemp = new EmailTemplate(); |
||
| 406 | $emailTemp->disable_row_level_security = true; |
||
| 407 | $emailTemp->retrieve($event->invite_templates); //Use the ID value of the email template record |
||
| 408 | |||
| 409 | //check email template is set, if not return error |
||
| 410 | if($emailTemp->id == '') |
||
| 411 | { |
||
| 412 | SugarApplication::appendErrorMessage($mod_strings['LBL_ERROR_MSG_5']); |
||
| 413 | SugarApplication::redirect("index.php?module=FP_events&return_module=FP_events&action=DetailView&record=".$event->id); |
||
| 414 | die(); |
||
| 415 | } |
||
| 416 | |||
| 417 | //parse the lead varibales first |
||
| 418 | $firstpass = $emailTemp->parse_template_bean($emailTemp->body_html, 'Contacts', $contact); |
||
| 419 | |||
| 420 | $email_subject = $emailTemp->parse_template_bean($emailTemp->subject, 'FP_events', $event); |
||
| 421 | $email_body = from_html($emailTemp->parse_template_bean($firstpass, 'FP_events', $event)); |
||
| 422 | $alt_emailbody = wordwrap($emailTemp->parse_template_bean($firstpass, 'FP_events', $event), 900); |
||
| 423 | |||
| 424 | //get attachments |
||
| 425 | $attachmentBean = new Note(); |
||
| 426 | $attachment_list = $attachmentBean->get_full_list('',"parent_type = 'Emails' AND parent_id = '".$event->invite_templates."'"); |
||
| 427 | |||
| 428 | $attachments = array(); |
||
| 429 | |||
| 430 | if($attachment_list != null){ |
||
| 431 | |||
| 432 | foreach ($attachment_list as $attachment) { |
||
| 433 | $attachments[] = $attachment; |
||
| 434 | } |
||
| 435 | } |
||
| 436 | |||
| 437 | //send the email |
||
| 438 | $send_invite = $this->sendEmail($rcpt_email, $email_subject, $rcpt_name, $email_body, $alt_emailbody, $contact, $attachments); |
||
| 439 | |||
| 440 | |||
| 441 | //Send the message, log if error occurs |
||
| 442 | if (!$send_invite){ |
||
| 443 | $GLOBALS['log']->fatal('ERROR: Invite email failed to send to: '.$rcpt_name.' at '.$rcpt_email); |
||
| 444 | $error_count ++; |
||
| 445 | } |
||
| 446 | else { |
||
| 447 | //update contact to invites |
||
| 448 | $query = 'UPDATE fp_events_contacts_c SET invite_status="Invited" WHERE fp_events_contactsfp_events_ida="'.$event->id.'" AND fp_events_contactscontacts_idb="'.$contact->id.'"'; |
||
| 449 | $res = $db->query($query); |
||
| 450 | } |
||
| 451 | } |
||
| 452 | } |
||
| 453 | |||
| 454 | //loop through related targets |
||
| 455 | foreach ($event->fp_events_prospects_1->getBeans() as $target) { |
||
| 456 | |||
| 457 | //Get accept status of contact |
||
| 458 | $query = 'SELECT invite_status FROM fp_events_prospects_1_c WHERE fp_events_prospects_1fp_events_ida="'.$event->id.'" AND fp_events_prospects_1prospects_idb="'.$target->id.'"'; |
||
| 459 | $status = $db->getOne($query); |
||
| 460 | |||
| 461 | if($status == null || $status == '' || $status == 'Not Invited'){ |
||
| 462 | $invite_count ++; |
||
| 463 | |||
| 464 | //set email links |
||
| 465 | $event->link = "<a href='".$sugar_config['site_url']."/index.php?entryPoint=responseEntryPoint&event=".$event->id."&delegate=".$target->id."&type=t&response=accept'>Accept</a>"; |
||
| 466 | $event->link_declined = "<a href='".$sugar_config['site_url']."/index.php?entryPoint=responseEntryPoint&event=".$event->id."&delegate=".$target->id."&type=t&response=decline'>Decline</a>"; |
||
| 467 | |||
| 468 | //Get the TO name and e-mail address for the message |
||
| 469 | $rcpt_name = $target->first_name . ' ' . $target->last_name; |
||
| 470 | $rcpt_email = $target->email1; |
||
| 471 | |||
| 472 | $emailTemp = new EmailTemplate(); |
||
| 473 | $emailTemp->disable_row_level_security = true; |
||
| 474 | $emailTemp->retrieve($event->invite_templates); //Use the ID value of the email template record |
||
| 475 | |||
| 476 | //parse the lead varibales first |
||
| 477 | $firstpass = $emailTemp->parse_template_bean($emailTemp->body_html, 'Contacts', $target); |
||
| 478 | |||
| 479 | $email_subject = $emailTemp->parse_template_bean($emailTemp->subject, 'FP_events', $event); |
||
| 480 | $email_body = from_html($emailTemp->parse_template_bean($firstpass, 'FP_events', $event)); |
||
| 481 | $alt_emailbody = wordwrap($emailTemp->parse_template_bean($firstpass, 'FP_events', $event), 900); |
||
| 482 | |||
| 483 | //get attachments |
||
| 484 | $attachmentBean = new Note(); |
||
| 485 | $attachment_list = $attachmentBean->get_full_list('',"parent_type = 'Emails' AND parent_id = '".$event->invite_templates."'"); |
||
| 486 | |||
| 487 | $attachments = array(); |
||
| 488 | |||
| 489 | if($attachment_list != null){ |
||
| 490 | |||
| 491 | foreach ($attachment_list as $attachment) { |
||
| 492 | $attachments[] = $attachment; |
||
| 493 | } |
||
| 494 | } |
||
| 495 | |||
| 496 | //send the email |
||
| 497 | $send_invite = $this->sendEmail($rcpt_email, $email_subject, $rcpt_name, $email_body, $alt_emailbody, $target, $attachments); |
||
| 498 | |||
| 499 | |||
| 500 | //Send the message, log if error occurs |
||
| 501 | if (!$send_invite){ |
||
| 502 | $GLOBALS['log']->fatal('ERROR: Invite email failed to send to: '.$rcpt_name.' at '.$rcpt_email); |
||
| 503 | $error_count ++; |
||
| 504 | } |
||
| 505 | else { |
||
| 506 | //update contact to invites |
||
| 507 | $query = 'UPDATE fp_events_prospects_1_c SET invite_status="Invited" WHERE fp_events_prospects_1fp_events_ida="'.$event->id.'" AND fp_events_prospects_1prospects_idb="'.$target->id.'"'; |
||
| 508 | $res = $db->query($query); |
||
| 509 | } |
||
| 510 | } |
||
| 511 | } |
||
| 512 | |||
| 513 | //loop through related leads |
||
| 514 | foreach ($event->fp_events_leads_1->getBeans() as $lead) { |
||
| 515 | |||
| 516 | //Get accept status of contact |
||
| 517 | $query = 'SELECT invite_status FROM fp_events_leads_1_c WHERE fp_events_leads_1fp_events_ida="'.$event->id.'" AND fp_events_leads_1leads_idb="'.$lead->id.'"'; |
||
| 518 | $status = $db->getOne($query); |
||
| 519 | |||
| 520 | if($status == null || $status == '' || $status == 'Not Invited'){ |
||
| 521 | |||
| 522 | $invite_count ++; |
||
| 523 | //set email links |
||
| 524 | $event->link = "<a href='".$sugar_config['site_url']."/index.php?entryPoint=responseEntryPoint&event=".$event->id."&delegate=".$lead->id."&type=l&response=accept'>Accept</a>"; |
||
| 525 | $event->link_declined = "<a href='".$sugar_config['site_url']."/index.php?entryPoint=responseEntryPoint&event=".$event->id."&delegate=".$lead->id."&type=l&response=decline'>Decline</a>"; |
||
| 526 | |||
| 527 | //Get the TO name and e-mail address for the message |
||
| 528 | $rcpt_name = $lead->first_name . ' ' . $lead->last_name; |
||
| 529 | $rcpt_email = $lead->email1; |
||
| 530 | |||
| 531 | $emailTemp = new EmailTemplate(); |
||
| 532 | $emailTemp->disable_row_level_security = true; |
||
| 533 | $emailTemp->retrieve($event->invite_templates); //Use the ID value of the email template record |
||
| 534 | |||
| 535 | //parse the lead varibales first |
||
| 536 | $firstpass = $emailTemp->parse_template_bean($emailTemp->body_html, 'Contacts', $lead); |
||
| 537 | |||
| 538 | $email_subject = $emailTemp->parse_template_bean($emailTemp->subject, 'FP_events', $event); |
||
| 539 | $email_body = from_html($emailTemp->parse_template_bean($firstpass, 'FP_events', $event)); |
||
| 540 | $alt_emailbody = wordwrap($emailTemp->parse_template_bean($firstpass, 'FP_events', $event), 900); |
||
| 541 | |||
| 542 | //get attachments |
||
| 543 | $attachmentBean = new Note(); |
||
| 544 | $attachment_list = $attachmentBean->get_full_list('',"parent_type = 'Emails' AND parent_id = '".$event->invite_templates."'"); |
||
| 545 | |||
| 546 | $attachments = array(); |
||
| 547 | |||
| 548 | if($attachment_list != null){ |
||
| 549 | |||
| 550 | foreach ($attachment_list as $attachment) { |
||
| 551 | $attachments[] = $attachment; |
||
| 552 | } |
||
| 553 | } |
||
| 554 | |||
| 555 | //send the email |
||
| 556 | $send_invite = $this->sendEmail($rcpt_email, $email_subject, $rcpt_name, $email_body, $alt_emailbody, $lead, $attachments); |
||
| 557 | |||
| 558 | |||
| 559 | //Send the message, log if error occurs |
||
| 560 | if (!$send_invite){ |
||
| 561 | $GLOBALS['log']->fatal('ERROR: Invite email failed to send to: '.$rcpt_name.' at '.$rcpt_email); |
||
| 562 | $error_count ++; |
||
| 563 | } |
||
| 564 | else { |
||
| 565 | //update contact to invites |
||
| 566 | $query = 'UPDATE fp_events_leads_1_c SET invite_status="Invited" WHERE fp_events_leads_1fp_events_ida="'.$event->id.'" AND fp_events_leads_1leads_idb="'.$lead->id.'"'; |
||
| 567 | $res = $db->query($query); |
||
| 568 | } |
||
| 569 | } |
||
| 570 | } |
||
| 571 | //Redirect with error message if all linked contacts have already been invited |
||
| 572 | if($invite_count == 0) { |
||
| 573 | SugarApplication::appendErrorMessage($mod_strings['LBL_ERROR_MSG_1']); |
||
| 574 | SugarApplication::redirect("index.php?module=FP_events&return_module=FP_events&action=DetailView&record=".$event->id); |
||
| 575 | } |
||
| 576 | //Redirect if all emails fail to send |
||
| 577 | if($error_count == $delegate_count){ |
||
| 578 | $_SESSION['user_error_message'] = array();//clear the error message array |
||
| 579 | SugarApplication::appendErrorMessage($mod_strings['LBL_ERROR_MSG_2'].$delegate_count); |
||
| 580 | SugarApplication::redirect("index.php?module=FP_events&return_module=FP_events&action=DetailView&record=".$event->id); |
||
| 581 | |||
| 582 | } |
||
| 583 | else if($error_count > 0 && $error_count <= 10) {//redirect with failed email count. |
||
| 584 | $_SESSION['user_error_message'] = array(); |
||
| 585 | SugarApplication::appendErrorMessage($error_count.$mod_strings['LBL_ERROR_MSG_4']); |
||
| 586 | SugarApplication::redirect("index.php?module=FP_events&return_module=FP_events&action=DetailView&record=".$event->id); |
||
| 587 | } |
||
| 588 | // Redirect with error count if failed email attempts are greater than 10 |
||
| 589 | else if($error_count > 10) { |
||
| 590 | $_SESSION['user_error_message'] = array(); |
||
| 591 | SugarApplication::appendErrorMessage($mod_strings['LBL_ERROR_MSG_3']); |
||
| 592 | SugarApplication::redirect("index.php?module=FP_events&return_module=FP_events&action=DetailView&record=".$event->id); |
||
| 593 | } |
||
| 594 | else { |
||
| 595 | SugarApplication::appendErrorMessage($mod_strings['LBL_SUCCESS_MSG']); |
||
| 596 | SugarApplication::redirect("index.php?module=FP_events&return_module=FP_events&action=DetailView&record=".$event->id); |
||
| 597 | } |
||
| 598 | } |
||
| 599 | |||
| 644 |