function engagements_send_email_or_txt_form_submit

6.x engagements.module engagements_send_email_or_txt_form_submit($form, &$form_state)
1 string reference to 'engagements_send_email_or_txt_form_submit'
engagements_form_alter in modules/engagements/engagements.module
Implements hook_form_alter

File

modules/engagements/engagements.module, line 3117
This is the primary module file for the engagements module.

Code

function engagements_send_email_or_txt_form_submit($form, &$form_state) 
 {

  global $user;

  $values = $form_state ['values'];
  $cid = $form_state ['content_object']->cid; // At this point, the content has already been saved, so we know its cid.
  $content = $form_state ['content_object'];
  $engagement_type = $values ['engagement_type'];

  $manual_entry = @trim(strtoupper($values ['manual_entry']));

  if ($engagement_type != 'email' && $engagement_type != 'txt_msg') {
    return; // we are not interested, harmlessly return.
  }

  $db = get_global_database_handler();
  $student_id = $values ['student_id'];
  $faculty_id = $values ['faculty_id'];
  $faculty_name = $db->get_faculty_name($faculty_id);

  $initials = variable_get("school_initials", "DEMO");

  $initials = variable_get_for_school("school_initials", "DEMO", db_get_school_id_for_student_id($student_id));

  // get the "from_number"
  $from_number = $values ['from_sms_phone'];

  // this needs to be a setting with replacement values!
  // Add header to msg...
  $header = engagements_sms_replace_patterns(variable_get('sms_header', '(@initials) from @name:'), $from_number, $user);

  $msg = $values ['engagement_msg'];

  $db = get_global_database_handler();

  if ($engagement_type == 'email' && $manual_entry != 'Y') {
    $student_email = fp_get_student_email($student_id);

    $subject = $values ['title'];

    // Add our tracking pixel to the end of the email msg (but not what is saved to our database).
    // We will use that to tell how many times the message gets opened.
    $tracking_img_url = engagements_create_new_tracking_img_url($cid);


    $msg .= "<br><br>----<br>You may respond directly to this email.  It will be stored with FlightPath for $faculty_name to then review.";

    $msg .= "\n\n<img src='$tracking_img_url'>";


    // Were there any attachments with this content?

    $attachments = array();
    $attachment_csv = trim(@$content->field__attachment ['value']);
    if ($attachment_csv) {
      $temp = explode(",", $attachment_csv);
      foreach ($temp as $fid) {
        if ($fid == "") {
          continue;
        }
        $file = content_get_uploaded_file($fid);
        if ($file) {
          // Handle encrypted files  
          $fcont = file_get_contents($file ['full_filename']); // the full_filename already contains the path to the file
          if (intval($file ['is_encrypted']) === 1) {
            $fcont = encryption_decrypt($fcont);
          }
          $attachments [$file ['original_filename']] = $fcont;
        }
      }
    }



    smtp_mail($student_email, $subject, $header . "<br><br>" . $msg, TRUE, $attachments, TRUE); // the TRUE sends as HTML.
    fp_add_message(t("Email has been successfully sent."));
  } // type == 'email'


  $mobile_phone = '';

  if ($engagement_type == 'txt_msg' && $manual_entry != 'Y') {

    $student_user_id = db_get_user_id_from_cwid($student_id, 'student');

    $temp = user_get_attribute($student_user_id, 'mobile_phone');
    $mobile_phone = engagements_convert_to_valid_phone_number(trim($temp));
    // Since we have passed validation, we will assume we have a valid phone number for this student.

    // Send the message!
    $external_msg_id = engagements_send_sms_to_number($mobile_phone, $header . "\n" . $msg, $student_id, $from_number);

    if ($external_msg_id) {
      fp_add_message(t("Text Message has been successfully sent."));
      // save the external_msg_id with the content.
      $content->field__external_msg_id ['value'] = $external_msg_id;

      $content->field__to_sms_phone ['value'] = $mobile_phone;
      $content->field__from_sms_phone ['value'] = $from_number;

      $content->log = "[AUTO] Updating external_msg_id";
      content_save($content);
    }
  } // type == 'txt_msg'

  if ($manual_entry != 'Y') {
    watchdog("engagements", "sent $engagement_type to $student_id ($mobile_phone)");
  }
  else {
    watchdog("engagements", "logged $engagement_type to $student_id");
  }

  watchdog("engagements", "$engagement_type message to $student_id: $header <br> $msg", array(), WATCHDOG_DEBUG);
}