|
3 | 3 | * Class to create and send an email outlining upcoming (next 2 weeks) and past-due loads
|
4 | 4 | *
|
5 | 5 | * @author Jared Howland <[email protected]@gmail.com>
|
6 |
| - * @version 2013-04-23 |
| 6 | + * @version 2013-04-25 |
7 | 7 | * @since 2013-04-23
|
8 | 8 | */
|
9 |
| - |
| 9 | + |
10 | 10 | class email {
|
11 |
| - /** |
12 |
| - * Constructor. Displays information from the database |
13 |
| - * |
14 |
| - * @access public |
15 |
| - * @param null |
16 |
| - * @return boolean, string TRUE/FALSE status of emailing, string with message if no resources need to be loaded in the next 2 weeks |
17 |
| - */ |
18 |
| - public function send_email() { |
19 |
| - $resources = $this->get_resources(); |
20 |
| - return $this->get_email($resources); |
| 11 | + /** |
| 12 | + * Constructor. Emails past due and upcoming records to load. |
| 13 | + * |
| 14 | + * @access public |
| 15 | + * @param null |
| 16 | + * @return boolean TRUE/FALSE status of email (sent/not sent) |
| 17 | + */ |
| 18 | + public function send() { |
| 19 | + $message = $this->get_message(); |
| 20 | + if($this->send_message($message)) { |
| 21 | + return TRUE; |
| 22 | + } else { |
| 23 | + return FALSE; |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Emails list of resources that need to be loaded in the next 2 weeks to appropriate parties (as defined in config.php) |
| 29 | + * |
| 30 | + * @access public |
| 31 | + * @param string String containing the message to be emailed. |
| 32 | + * @return boolean TRUE/FALSE status of email (sent/not sent) |
| 33 | + */ |
| 34 | + private function send_message($message) { |
| 35 | + $to = config::NOTIFY_EMAILS; |
| 36 | + $subject = 'MARC Records to Load for ' . date('D, M d, Y'); |
| 37 | + $headers = 'From: ' . config::FROM_EMAIL . "\r\n" . 'Reply-To: ' . config::FROM_EMAIL . "\r\n" . 'X-Mailer: PHP/' . phpversion(); |
| 38 | + ob_start(); |
| 39 | + return mail( $to, $subject, $message, $headers ); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Create email message. |
| 44 | + * |
| 45 | + * @access public |
| 46 | + * @param null |
| 47 | + * @return string Message to be sent via email |
| 48 | + */ |
| 49 | + private function get_message() { |
| 50 | + $past_resources = $this->get_past_resources(); |
| 51 | + $future_resources = $this->get_future_resources(); |
| 52 | + $message = NULL; |
| 53 | + if($past_resources) { |
| 54 | + $message .= 'The following resources are past due:' . "\r\n\r\n" . $this->get_resource_list($past_resources) . "\r\n\r\n"; |
| 55 | + } |
| 56 | + if($future_resources) { |
| 57 | + $message .= 'The following resources need to be loaded in the next two weeks:' . "\r\n\r\n" . $this->get_resource_list($future_resources); |
21 | 58 | }
|
22 |
| - |
23 |
| - /** |
24 |
| - * Gets list of all resources that need to be loaded in the next 2 weeks |
25 |
| - * |
26 |
| - * @access public |
27 |
| - * @param null |
28 |
| - * @return array Array with data about resources |
29 |
| - */ |
30 |
| - private function get_resources() { |
31 |
| - $date = date('Y-m-d', strtotime('+2 weeks')); |
32 |
| - $database = new db; |
33 |
| - $db = $database->connect(); |
34 |
| - $sql = 'SELECT r.resource_name, r.next_load, v.name AS vendor_name FROM records r INNER JOIN vendors v ON r.vendor_id = v.id WHERE r.next_load < :date ORDER BY r.next_load ASC, vendor_name, resource_name'; |
35 |
| - $query = $db->prepare($sql); |
36 |
| - $query->bindParam(':date', $date); |
37 |
| - $query->execute(); |
38 |
| - $resources = $query->fetchAll(PDO::FETCH_ASSOC); |
39 |
| - $db = null; |
40 |
| - return $resources; |
| 59 | + if(!$past_resources && !$future_resources) { |
| 60 | + $message .= 'We are all up to date. There are no resources to load at this time.'; |
41 | 61 | }
|
42 |
| - |
43 |
| - /** |
44 |
| - * Emails list of resources that need to be loaded in the next 2 weeks to appropriate parties (as defined in config.php) |
45 |
| - * |
46 |
| - * @access public |
47 |
| - * @param array Array with resources that need to be loaded in the next 2 weeks |
48 |
| - * @return boolean, string TRUE/FALSE status of emailing, string with message if no resources need to be loaded in the next 2 weeks |
49 |
| - */ |
50 |
| - private function get_email($resources) { |
51 |
| - $count = count($resources); |
52 |
| - // Only send email if MARC records need to be loaded in the next 2 weeks |
53 |
| - if($count > 0) { |
54 |
| - $resource_list = NULL; |
55 |
| - foreach($resources as $resource) { |
56 |
| - $resource_name = $resource['resource_name']; |
57 |
| - $next_load = $resource['next_load']; |
58 |
| - $vendor_name = $resource['vendor_name']; |
59 |
| - $resource_list .= $next_load . ': ' . $vendor_name . '-' . $resource_name . "\r\n"; |
60 |
| - } |
61 |
| - $to = config::NOTIFY_EMAILS; |
62 |
| - $subject = 'MARC Records to Load for ' . date('D, M d, Y'); |
63 |
| - $message = 'The following resources need to be loaded soon:' . "\r\n\r\n" . $resource_list; |
64 |
| - $headers = 'From: ' . config::FROM_EMAIL . "\r\n" . |
65 |
| - 'Reply-To: ' . config::FROM_EMAIL . "\r\n" . |
66 |
| - 'X-Mailer: PHP/' . phpversion(); |
67 |
| - ob_start(); |
68 |
| - $mail = mail( $to, $subject, $message, $headers ); |
69 |
| - return $mail; |
70 |
| - } else { |
71 |
| - return 'Congratulations! No resources need to be loaded in the next 2 weeks.'; |
72 |
| - } |
| 62 | + return $message; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Format array as a list (string) for email message |
| 67 | + * |
| 68 | + * @access public |
| 69 | + * @param array Array containing about resources to email |
| 70 | + * @return string List of resources from array |
| 71 | + */ |
| 72 | + private function get_resource_list($resources) { |
| 73 | + $resource_list = NULL; |
| 74 | + foreach($resources as $resource) { |
| 75 | + $resource_name = $resource['resource_name']; |
| 76 | + $next_load = $resource['next_load']; |
| 77 | + $vendor_name = $resource['vendor_name']; |
| 78 | + $resource_list .= $next_load . ': ' . $vendor_name . '-' . $resource_name . "\r\n"; |
| 79 | + } |
| 80 | + return $resource_list; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Returns array of all resources that are past due |
| 85 | + * |
| 86 | + * @access public |
| 87 | + * @param null |
| 88 | + * @return array, boolean Array with data about resources. Returns FALSE if query returns no results. |
| 89 | + */ |
| 90 | + private function get_past_resources() { |
| 91 | + $date = date('Y-m-d'); |
| 92 | + $database = new db; |
| 93 | + $db = $database->connect(); |
| 94 | + $sql = 'SELECT r.resource_name, r.next_load, v.name AS vendor_name FROM records r INNER JOIN vendors v ON r.vendor_id = v.id WHERE r.next_load < :date ORDER BY r.next_load ASC, vendor_name, resource_name'; |
| 95 | + $query = $db->prepare($sql); |
| 96 | + $query->bindParam(':date', $date); |
| 97 | + $query->execute(); |
| 98 | + $past_resources = $query->fetchAll(PDO::FETCH_ASSOC); |
| 99 | + $db = null; |
| 100 | + if(count($past_resources) > 0) { |
| 101 | + return $past_resources; |
| 102 | + } else { |
| 103 | + return FALSE; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Returns array of all resources that need to be loaded in the next 2 weeks |
| 109 | + * |
| 110 | + * @access public |
| 111 | + * @param null |
| 112 | + * @return array, boolean Array with data about resources. Returns FALSE if query returns no results. |
| 113 | + */ |
| 114 | + private function get_future_resources() { |
| 115 | + $date = date('Y-m-d'); |
| 116 | + $future_date = date('Y-m-d', strtotime('+2 weeks')); |
| 117 | + $database = new db; |
| 118 | + $db = $database->connect(); |
| 119 | + $sql = 'SELECT r.resource_name, r.next_load, v.name AS vendor_name FROM records r INNER JOIN vendors v ON r.vendor_id = v.id WHERE r.next_load between :date AND :future_date ORDER BY r.next_load ASC, vendor_name, resource_name'; |
| 120 | + $query = $db->prepare($sql); |
| 121 | + $query->bindParam(':date', $date); |
| 122 | + $query->bindParam(':future_date', $future_date); |
| 123 | + $query->execute(); |
| 124 | + $future_resources = $query->fetchAll(PDO::FETCH_ASSOC); |
| 125 | + $db = null; |
| 126 | + if(count($future_resources) > 0) { |
| 127 | + return $future_resources; |
| 128 | + } else { |
| 129 | + return FALSE; |
73 | 130 | }
|
| 131 | + } |
74 | 132 | }
|
75 | 133 | ?>
|
0 commit comments