|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Public Model Mail_Model |
|
5
|
|
|
* @package Todo |
|
6
|
|
|
*/ |
|
7
|
|
|
class Mail_model extends CI_Model { |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Constructor |
|
11
|
|
|
*/ |
|
12
|
|
|
public function __construct() |
|
13
|
|
|
{ |
|
14
|
|
|
parent::__construct(); |
|
15
|
|
|
|
|
16
|
|
|
$this->load->library('email'); |
|
17
|
|
|
|
|
18
|
|
|
//Set email config |
|
19
|
|
|
$config = array( |
|
20
|
|
|
'useragent' => "Tim's Todo", |
|
21
|
|
|
'protocol' => 'mail', |
|
22
|
|
|
'mailtype' => 'text', |
|
23
|
|
|
'charset' => 'utf-8', |
|
24
|
|
|
'validate' => 'true', |
|
25
|
|
|
'priority' => '3', |
|
26
|
|
|
); |
|
27
|
|
|
|
|
28
|
|
|
$this->email->initialize($config); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
// -------------------------------------------------------------------------- |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Check what reminders need to be sent out for the current run |
|
35
|
|
|
* |
|
36
|
|
|
* @return mixed |
|
37
|
|
|
*/ |
|
38
|
|
|
public function check_db() |
|
39
|
|
|
{ |
|
40
|
|
|
//Get the current time |
|
41
|
|
|
$now = time(); |
|
42
|
|
|
|
|
43
|
|
|
//Get two minuts from now |
|
44
|
|
|
$interval_new = $now + 900; |
|
45
|
|
|
|
|
46
|
|
|
//Get reminders within two minutes of now, that have not been sent |
|
47
|
|
|
$this->db->select('reminder.id as rem_id, todo_item.id as task_num, |
|
48
|
|
|
reminder_time, due, sent, title, email, username') |
|
49
|
|
|
->from('reminder') |
|
50
|
|
|
->join('item', 'todo_item.id = todo_reminder.task_id', 'inner') |
|
51
|
|
|
->join('user', 'todo_user.id = todo_reminder.user_id', 'inner') |
|
52
|
|
|
->where('reminder_time <', $interval_new) |
|
53
|
|
|
->where('sent', 0); |
|
54
|
|
|
|
|
55
|
|
|
$query = $this->db->get(); |
|
56
|
|
|
|
|
57
|
|
|
//If no results, return |
|
58
|
|
|
if($query->num_rows() == 0) |
|
59
|
|
|
return; |
|
60
|
|
|
|
|
61
|
|
|
//Format, then send the email |
|
62
|
|
|
$this->_format_email($query); |
|
63
|
|
|
|
|
64
|
|
|
//Return debugging info |
|
65
|
|
|
$return = $this->email->print_debugger(); |
|
66
|
|
|
|
|
67
|
|
|
//Log debugging info |
|
68
|
|
|
log_message('debug', $return); |
|
69
|
|
|
|
|
70
|
|
|
//Clear the email object for the next loop |
|
71
|
|
|
$this->email->clear(); |
|
72
|
|
|
|
|
73
|
|
|
return $return; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
// -------------------------------------------------------------------------- |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Format the email to send for a reminder |
|
80
|
|
|
* |
|
81
|
|
|
* @param $query |
|
82
|
|
|
*/ |
|
83
|
|
|
private function _format_email($query) |
|
84
|
|
|
{ |
|
85
|
|
|
foreach($query->result() as $row) |
|
86
|
|
|
{ |
|
87
|
|
|
$due = $row->due; |
|
88
|
|
|
$due_reminder = $row->reminder_time; |
|
89
|
|
|
|
|
90
|
|
|
//Time until task is due, in seconds |
|
91
|
|
|
$until_due = $due - $due_reminder; |
|
92
|
|
|
|
|
93
|
|
|
//In hours |
|
94
|
|
|
$until_hours = ($until_due >= 3600) ? floor((int)$until_due / 3600) : 0; |
|
95
|
|
|
|
|
96
|
|
|
//In additional minutes |
|
97
|
|
|
$um = (int)$until_due - ($until_hours * 3600); |
|
98
|
|
|
$until_minutes = (int)($um / 60); |
|
99
|
|
|
|
|
100
|
|
|
$user = $row->username; |
|
101
|
|
|
$task_num = $row->task_num; |
|
102
|
|
|
$task = $row->title; |
|
103
|
|
|
$to = $row->email; |
|
104
|
|
|
|
|
105
|
|
|
$rem_id = $row->rem_id; |
|
106
|
|
|
|
|
107
|
|
|
$due_time = date('D M d, Y g:iA T', $due); |
|
108
|
|
|
|
|
109
|
|
|
$subject = "Tim's Todo Reminder: '" . $task . "' is due soon"; |
|
110
|
|
|
$message = $user . ",\r\n". |
|
111
|
|
|
"This is a reminder that task #". $task_num .", '".$task."' is due in ". |
|
112
|
|
|
$until_hours." hours and ".$until_minutes." minutes, at " . $due_time; |
|
113
|
|
|
|
|
114
|
|
|
//Set email parameters |
|
115
|
|
|
$this->email->to($to); |
|
116
|
|
|
$this->email->from('[email protected]', "Tim's Todo"); |
|
117
|
|
|
$this->email->message($message); |
|
118
|
|
|
$this->email->subject($subject); |
|
119
|
|
|
|
|
120
|
|
|
$this->_send($rem_id); |
|
121
|
|
|
|
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
// -------------------------------------------------------------------------- |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Send a reminder, and mark the reminder as sent |
|
129
|
|
|
* |
|
130
|
|
|
* @param $rem_id |
|
131
|
|
|
*/ |
|
132
|
|
|
private function _send($rem_id) |
|
133
|
|
|
{ |
|
134
|
|
|
$result = $this->email->send(); |
|
135
|
|
|
|
|
136
|
|
|
echo (int) $result . "\n"; |
|
137
|
|
|
|
|
138
|
|
|
if($result != FALSE) |
|
139
|
|
|
{ |
|
140
|
|
|
//Set as set in the database |
|
141
|
|
|
$this->db->set('sent', 1) |
|
142
|
|
|
->where('id', $rem_id) |
|
143
|
|
|
->update('reminder'); |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
// End of models/mail_model.php |