Skip to content

Instantly share code, notes, and snippets.

@aa21
Created March 23, 2017 07:54
Show Gist options
  • Save aa21/1b8846b32ec535c3359d7dc1e306e8b6 to your computer and use it in GitHub Desktop.
Save aa21/1b8846b32ec535c3359d7dc1e306e8b6 to your computer and use it in GitHub Desktop.
<?php
class MM_Notifications {
private $usr_mentor;
private $usr_mentee;
private $mentor_action;
private $email_codes;
private $notice_type;
private $debug_email;
private $booking;
public function __construct ($usr_mentor, $usr_mentee, $mentor_action, $booking, $debug_email = false){
$this -> usr_mentor = $usr_mentor;
$this -> usr_mentee = $usr_mentee;
$this -> mentor_action = $mentor_action;
$this -> debug_email = $debug_email ? $debug_email : false;
$this -> booking = $booking;
}
public function send($notice_type){
if(!in_array($notice_type, ['requested', 'confirmed', 'updated', 'deleted'])) die( 'Unknown notice type: ' . $notice_type );
$this -> notice_type = $notice_type;
$notice_template = $this -> notice_template();
$tpl_subject = $notice_template['subject'];
$tpl_body = $notice_template['body'];
preg_match_all('/![a-z_]*?!/', $tpl_subject.$tpl_body, $matches);
$this -> email_codes = array_shift($matches);
$email_to = 'mentor';
while($email_to){
$subject = str_ireplace($this -> email_codes, $this -> replacer($email_to == 'mentor'), $tpl_subject);
$body = str_ireplace($this -> email_codes, $this -> replacer($email_to == 'mentor'), $tpl_body);
$to_email_address = $this -> debug_email ? $this -> debug_email : ($email_to == 'mentor' ? $this -> usr_mentor -> email : $this -> usr_mentee -> email);
Email::send_email($to_email_address, SITE_EMAIL, $subject, $body, SYNDICATE_NAME);
$email_to = $email_to == 'mentor' ? 'mentee' : false;
}
}
private function replacer($mentor_email=true){
$v = +boolval($mentor_email) ."". +boolval($this -> mentor_action);
$usr_mentor = $this -> usr_mentor;
$usr_mentee = $this -> usr_mentee;
$code_values = [
'!sub_person!' => in_array($v, ['11', '00']) ? 'you' : ($v == '10' ? $usr_mentee -> name : $usr_mentor -> name ),
'!bod_hi!' => in_array($v, ['11', '10']) ? $usr_mentor -> first_name : $usr_mentee -> first_name ,
'!bod_who_did!' => in_array($v, ['11', '00']) ? 'You have' : ( $v == '10' ? 'Mentee '.$usr_mentee -> name . ' has' : 'Mentor '.$usr_mentor -> name . ' has' ) ,
'!bod_to_whom!' => in_array($v, ['01', '10']) ? 'you' : ($v == '11' ? 'mentee '.$usr_mentee -> name : 'mentor '.$usr_mentor -> name ),
'!user_type!' => in_array($v, ['11', '01']) ? 'mentor': 'mentee',
'!bod_appt_text!' => in_array($this -> notice_type, ['requested', 'updated']) ? 'the appointment' : 'appointments',
'!sub_appt_text!' => $this -> notice_type == 'requested' ? 'New mentor appointment' : 'Mentor appointment',
];
return array_map(function($c) use($code_values) { if(isset($code_values[$c]) ) return $code_values[$c]; }, $this -> email_codes);
}
private function notice_template(){
$usr_mentor = $this -> usr_mentor;
$usr_mentee = $this -> usr_mentee;
$type = $this -> notice_type;
$booking_data = $this -> booking;
$url_params = $type != 'cancelled' ? '?!user_type!_booking='.$booking_data['id'].'&mentor='.$booking_data['mid'].'&jump-to-date='.$booking_data['from_date']: '';
$subject = "!sub_appt_text! $type by !sub_person!: (".SYNDICATE_NAME .")";
$body = "<p>Hi !bod_hi!,</p>
<p>!bod_who_did! $type an appointment with !bod_to_whom! on the ".SYNDICATE_NAME." Mentor Manager.</p>
<p><b>Agenda</b>: ".$booking_data['purpose']."<br>
<b>At</b>: ".$this -> time_string($booking_data['from_date'], $booking_data['from_time'], $booking_data['to_date'], $booking_data['to_time'] )."</p>
<p>Please <a href=".'https://'.THEME_NAME .'.hatcher.com/app/admin-mentors.php'. $url_params .">click here</a> to view and manage !bod_appt_text! on the Mentor Manager.</p>
<p><br>Have a great day,
<br>".SYNDICATE_NAME."</p>";
return ['subject' => $subject, 'body' => $body];
}
private static function time_string($fDate, $fTime, $tDate, $tTime, $glue = 'to'){
$fTime = preg_replace('/:00$/', '', $fTime);
$tTime = preg_replace('/:00$/', '', $tTime);
if($fDate == $tDate ) return $fDate . ', ' . $fTime ." $glue ". $tTime;
else return $fDate . " " . $fTime . " $glue " . $tDate . " " . $tTime;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment