function transition_user_to_state($user_id, $newState, $force = false) { $user = get_user($user_id); if (!$user) { throw new RuntimeException('Unknown user'); } $status = $user->status; if (!$force) { if ($newState == $status) { return 0; } // The list of state transitions that are valid. $transitions = array(STATUS_CREATED => array(STATUS_DRAFT), STATUS_DRAFT => array(STATUS_SUBMITTED), STATUS_SUBMITTED => array(STATUS_ACCEPTED, STATUS_REJECTED), STATUS_ACCEPTED => array(STATUS_CONFIRMED)); if (!array_key_exists($status, $transitions)) { throw new RuntimeException('Unknown starting state ' . $status); } if (array_search($newState, $transitions[$status]) === false) { throw new RuntimeException('Invalid state transition: ' . $status . '->' . $newState); } } $db = new DbConn(); $rows = $db->exec('update users set status = ?, laststatuschange = ? where id = ?', (int) $newState, date_create(), (int) $user_id); log_event(LOG_USER_STATE_CHANGE, (int) $user_id, (int) $newState); $db->exec('delete from mails_scheduled where userid = ?', $user_id); switch ($newState) { case STATUS_DRAFT: break; case STATUS_SUBMITTED: schedule_mail($user_id, MAIL_CONFIRM_APP); schedule_mail($user_id, ADMINMAIL_SUBMITTED_NOTIFICATION); schedule_mail($user_id, ADMINMAIL_DECISION_DUE, new DateTime('+2 weeks -2 days')); schedule_mail($user_id, ADMINMAIL_DECISION_OVERDUE, new DateTime('+2 weeks +1 day')); break; case STATUS_ACCEPTED: schedule_mail($user_id, MAIL_ACCEPTED); schedule_mail($user_id, ADMINMAIL_CONFIRMATION_REMINDER, new DateTime("+1 month")); break; case STATUS_CONFIRMED: schedule_mail($user_id, MAIL_ITINERARY_CONFIRMED); $arrivaldate = new DateTime($user->arrivaldate); schedule_mail($user_id, MAIL_TWO_MONTHS, new DateTime("{$user->arrivaldate} -2 months")); schedule_mail($user_id, MAIL_ONE_MONTH, new DateTime("{$user->arrivaldate} -1 month")); schedule_mail($user_id, MAIL_ONE_WEEK, new DateTime("{$user->arrivaldate} -1 week")); break; case STATUS_REJECTED: schedule_mail($user_id, MAIL_DENIED); break; case STATUS_INACTIVE: break; } return $rows; }
/** * $template_id - mail_templates.id * $user - User id or assoc array */ function send_user_mail($template, $user, $to = NULL) { $CI =& get_instance(); $CI->load->library('email'); $CI->load->library('admin'); $volunteer_coordinator = $CI->admin->get_volunteer_coordinator(); $mail_sender_name = $CI->config->item('mail_sender_name'); $mail_sender_email = $CI->config->item('mail_sender_email'); if (!is_array($user)) { $user = get_user_assoc($user); } if ($template->recipient == MAILRECIPIENT_ADMIN || $to) { $user['application_url'] = site_url('admin/volunteers/show/' . $user['id']); } $user['homepage_url'] = base_url(); $user['admin_email'] = $volunteer_coordinator->email; $user['admin_name'] = $volunteer_coordinator->name; $mail = render_mail($template, $user); $CI->email->initialize(array('mailtype' => 'html')); $CI->email->clear(TRUE); $CI->email->from($mail_sender_email, $mail_sender_name); if ($volunteer_coordinator) { $CI->email->reply_to($volunteer_coordinator->email, $volunteer_coordinator->name); } if ($to) { $CI->email->to($to); } else { if ($template->recipient == MAILRECIPIENT_APPLICANT) { $CI->email->to($user['email']); } else { if ($template->recipient == MAILRECIPIENT_ADMIN) { $CI->load->library('admin'); // get all admin e-mails $emails = $CI->admin->get_admin_emails(); $CI->email->to(implode(', ', $emails)); } } } $CI->email->subject($mail->subject); $CI->email->message($mail->html); $CI->email->set_alt_message($mail->plaintext); /* The filenames that the attachments use on disk are not human-readable--they have IDs, not the original filenames. To restore the original filenames we must create a temp directory structure and then copy the files to the temp dir, attach from there, send the file, then clean up the temp dir. */ $tmp = tempnam(sys_get_temp_dir(), 'vteer_mail_'); if (!unlink($tmp) || !mkdir($tmp)) { throw new RuntimeException("Failed to create temp dir"); } while ($attachment = $template->attachments->next()) { $filedata = make_attachment_path($attachment->id); $subtmp = $tmp . DIRECTORY_SEPARATOR . $attachment->id; $filetmp = $subtmp . DIRECTORY_SEPARATOR . $attachment->filename; if (!mkdir($subtmp) || !copy($filedata, $filetmp)) { throw new RuntimeException("Failed to attach file"); } $CI->email->attach($filetmp); } $CI->email->send(); if ($template->recurrence) { schedule_mail($user['id'], $template->templateid, new DateTime($template->recurrence)); } $db = new DbConn(); $db->exec('insert into mails_sent (userid, templateverid, sent) values (?, ?, ?)', $user['id'], $template->id, date_create()); deltree($tmp); }
function sendpassword() { $error = FALSE; $email = $this->input->post('email'); if (!$email) { $error = 'Please enter a valid e-mail address'; } else { $user = get_user_by_email($email); if (!$user) { $error = 'That e-mail address does not exist in our records.'; } else { schedule_mail($user->id, MAIL_PASSWORD); } } if ($error) { $this->load->view('header'); $this->load->view('passwordhelp', array('error' => $error)); $this->load->view('footer'); } else { $this->load->view('header'); $this->load->view('loginform', array('login_error' => "Your password has been e-mailed to you.")); $this->load->view('footer'); } }