Skip to content
This repository was archived by the owner on Aug 23, 2023. It is now read-only.

Commit c1ff45e

Browse files
committed
Made more object oriented.
1 parent a2ccf9f commit c1ff45e

13 files changed

+308
-104
lines changed

add.php

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
require_once 'config.php';
33

4+
// TODO: Convert to OOP
45
$vendor_id = $_REQUEST['vendor_id'];
56

67
if(is_null($vendor_id)) {

all.php

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
<?php
2+
/**
3+
* Displays all resources in the database
4+
*
5+
* @author Jared Howland <[email protected]>
6+
* @version 2013-04-23
7+
* @since 2013-04-23
8+
*
9+
*/
210
require_once 'config.php';
311

4-
$database = new db;
5-
$db = $database->connect();
6-
$sql = 'SELECT r.id, r.resource_name, r.url, r.username, r.password, r.frequency, r.last_load, r.next_load, r.num_records, r.notes, r.last_updated, r.file_exists, v.name AS vendor_name FROM records r INNER JOIN vendors v ON r.vendor_id = v.id ORDER BY r.next_load ASC, vendor_name, resource_name';
7-
$query = $db->prepare($sql);
8-
$query->execute();
9-
$marc_record_loads = $query->fetchAll(PDO::FETCH_ASSOC);
10-
$db = null;
11-
12-
$html = array('title' => 'All', 'results' => $marc_record_loads);
12+
$display = new display;
13+
$html = $display->html('all');
1314
template::display('html.tmpl', $html);
1415
?>

classes/class.display.php

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
/**
3+
* Class to display information from database
4+
*
5+
* @author Jared Howland <[email protected]@gmail.com>
6+
* @version 2013-04-23
7+
* @since 2013-04-23
8+
*/
9+
10+
class display {
11+
/**
12+
* Constructor. Displays information from the database
13+
*
14+
* @access public
15+
* @param string Type of query to display (Valid values: “all”, “active”, “inactive”)
16+
* @return string HTML to put into template
17+
*/
18+
public function html($type) {
19+
$results = $this->query_database($type);
20+
return $this->get_html($type, $results);
21+
}
22+
23+
/**
24+
* Gets list of all requested data from the database
25+
*
26+
* @access public
27+
* @param string Type of query to display (Valid values: “all”, “active”, “inactive”)
28+
* @return array Array with data about resources
29+
*/
30+
private function query_database($type) {
31+
$database = new db;
32+
$db = $database->connect();
33+
switch ($type) {
34+
case 'all':
35+
$sql = 'SELECT r.id, r.resource_name, r.url, r.username, r.password, r.frequency, r.last_load, r.next_load, r.num_records, r.notes, r.last_updated, r.file_exists, v.name AS vendor_name FROM records r INNER JOIN vendors v ON r.vendor_id = v.id ORDER BY r.next_load ASC, vendor_name, resource_name';
36+
break;
37+
case 'active':
38+
$sql = 'SELECT r.id, r.resource_name, r.url, r.username, r.password, r.frequency, r.last_load, r.next_load, r.num_records, r.notes, r.last_updated, r.file_exists, v.name AS vendor_name FROM records r INNER JOIN vendors v ON r.vendor_id = v.id WHERE load_records = "Y" ORDER BY r.next_load ASC, vendor_name, resource_name';
39+
break;
40+
case 'inactive':
41+
$sql = 'SELECT r.id, r.resource_name, r.url, r.username, r.password, r.frequency, r.last_load, r.next_load, r.num_records, r.notes, r.last_updated, r.file_exists, v.name AS vendor_name FROM records r INNER JOIN vendors v ON r.vendor_id = v.id WHERE load_records = "N" ORDER BY r.next_load ASC, vendor_name, resource_name';
42+
break;
43+
default:
44+
$sql = 'SELECT r.id, r.resource_name, r.url, r.username, r.password, r.frequency, r.last_load, r.next_load, r.num_records, r.notes, r.last_updated, r.file_exists, v.name AS vendor_name FROM records r INNER JOIN vendors v ON r.vendor_id = v.id WHERE load_records = "Y" ORDER BY r.next_load ASC, vendor_name, resource_name';
45+
break;
46+
}
47+
$query = $db->prepare($sql);
48+
$query->execute();
49+
$results = $query->fetchAll(PDO::FETCH_ASSOC);
50+
$db = null;
51+
return $results;
52+
}
53+
54+
/**
55+
* Creates array of data to be put into HTML template
56+
*
57+
* @access public
58+
* @param string Type of query to display (Valid values: “all”, “active”, “inactive”)
59+
* @param array Array with database query results
60+
* @return array Array with data about resources
61+
*/
62+
private function get_html($type, $results) {
63+
switch ($type) {
64+
case 'all':
65+
$html = array('title' => 'All', 'results' => $results);
66+
break;
67+
case 'active':
68+
$stats = $this->get_stats($results);
69+
$html = array('title' => 'Home', 'stats' => $stats, 'results' => $results);
70+
break;
71+
case 'inactive':
72+
$html = array('title' => 'Inactive', 'results' => $results);
73+
break;
74+
default:
75+
$stats = $this->get_stats($results);
76+
$html = array('title' => 'Home', 'stats' => $stats, 'results' => $results);
77+
break;
78+
}
79+
return $html;
80+
}
81+
82+
/**
83+
* Calculates and returns vital statistics to be presented on the home page
84+
*
85+
* @access public
86+
* @param array Array with data about resources
87+
* @return array Array with stats about the resources
88+
*/
89+
private function get_stats($results) {
90+
$num_resources = count($results);
91+
$num_up_to_date_resources = 0;
92+
$num_records = 0;
93+
94+
foreach($results as $resource) {
95+
$num_records += $resource['num_records'];
96+
// To count as being up to date, must have a file associated with it, and have the next load date in the future
97+
if($resource['file_exists'] == 'Y' && (strtotime($resource['next_load']) > strtotime(date('Y-m-d')) || is_null($resource['next_load']))) {
98+
$num_up_to_date_resources++;
99+
}
100+
}
101+
$percent_complete = round(($num_up_to_date_resources / $num_resources) * 100) . '%';
102+
103+
return array('num_records' => $num_records, 'num_resources' => $num_resources, 'num_up_to_date_resources' => $num_up_to_date_resources, 'percent_complete' => $percent_complete);
104+
}
105+
}
106+
?>

classes/class.download.php

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
/**
3+
* Class to download all MARC record files
4+
*
5+
* @author Jared Howland <[email protected]@gmail.com>
6+
* @version 2013-04-23
7+
* @since 2013-04-23
8+
*/
9+
10+
require_once 'lib/Archive_Tar/Archive/Tar.php';
11+
12+
class download {
13+
/**
14+
* Constructor. Compresses all MARC records files and presents them to user for download
15+
*
16+
* @access public
17+
* @param null
18+
* @return boolean TRUE if files compressed successfully
19+
*/
20+
public function download_file() {
21+
set_time_limit(300);
22+
$marc_files = $this->get_all();
23+
$compressed_files = $this->compress($marc_files);
24+
$this->download_all();
25+
}
26+
27+
/**
28+
* Gets list of all files to compress and download
29+
*
30+
* @access public
31+
* @param null
32+
* @return array Array with id, load_records (Y or N), file_exists (Y or N)
33+
*/
34+
private function get_all() {
35+
$database = new db;
36+
$db = $database->connect();
37+
$sql = 'SELECT id, load_records, file_exists FROM records WHERE load_records = "Y" AND file_exists = "Y"';
38+
$query = $db->prepare($sql);
39+
$query->execute();
40+
$marc_record_loads = $query->fetchAll(PDO::FETCH_ASSOC);
41+
$db = null;
42+
return $marc_record_loads;
43+
}
44+
45+
/**
46+
* Compresses all the active MARC files
47+
*
48+
* @access public
49+
* @param array Array with id, load_records (Y or N), file_exists (Y or N) for all active MARC files
50+
* @return boolean TRUE if compressed file created successfully
51+
*/
52+
private function compress($marc_files) {
53+
$tar = new Archive_Tar(config::UPLOAD_DIRECTORY . '/All.tar.gz', 'gz');
54+
$files = array();
55+
foreach($marc_files as $resource) {
56+
$resource_id = $resource['id'];
57+
$filename = config::UPLOAD_DIRECTORY . '/' . $resource_id . '.xml';
58+
$files[] = $filename;
59+
}
60+
$tar->create($files) or die('Could not create archive. Please go back and try again.');
61+
return TRUE;
62+
}
63+
64+
private function download_all() {
65+
if(rename(config::UPLOAD_DIRECTORY . '/All.tar.gz', 'download_all/All.tar.gz')){
66+
$this->present_download('download_all/All.tar.gz', 'All.tar.gz');
67+
}
68+
}
69+
70+
private function present_download($f_location, $f_name){
71+
header('Content-Description: File Transfer');
72+
header('Content-Type: application/x-tar');
73+
header('Content-Disposition: attachment; filename=' . basename($f_name));
74+
readfile($f_location);
75+
}
76+
}
77+
?>

classes/class.email.php

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
/**
3+
* Class to create and send an email outlining upcoming (next 2 weeks) and past-due loads
4+
*
5+
* @author Jared Howland <[email protected]@gmail.com>
6+
* @version 2013-04-23
7+
* @since 2013-04-23
8+
*/
9+
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);
21+
}
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;
41+
}
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+
}
73+
}
74+
}
75+
?>

config.example.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class config {
2020
const URL = '';
2121
const UPLOAD_DIRECTORY = 'records';
2222
const TIME_ZONE = ''; // Needed for date calculations in PHP
23-
const NOTIFY_EMAILS = '';
23+
const NOTIFY_EMAILS = ''; // Who should receive the emails; comma-delimited if more than one recipient (http://php.net/manual/en/function.mail.php)
2424
const FROM_EMAIL = '';
2525
const FREQUENCY = '["Once","Weekly","Monthly","Quarterly","Semiannually","Annually","When notified"]'; // JSON of allowed values for frequency records are loaded
2626

download.php

+3-30
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,13 @@
33
* Compress and download all files
44
*
55
* @author Jared Howland <[email protected]>
6-
* @version 2013-03-05
6+
* @version 2013-04-23
77
* @since 2013-03-04
88
*
99
*/
1010
require_once 'config.php';
11-
require_once 'lib/Archive_Tar/Archive/Tar.php';
1211

13-
$database = new db;
14-
$db = $database->connect();
15-
$sql = 'SELECT id, load_records, file_exists FROM records WHERE load_records = "Y" AND file_exists = "Y"';
16-
$query = $db->prepare($sql);
17-
$query->execute();
18-
$marc_record_loads = $query->fetchAll(PDO::FETCH_ASSOC);
19-
$db = null;
20-
21-
$tar = new Archive_Tar(config::UPLOAD_DIRECTORY . '/All.tar.gz', 'gz');
22-
$files = array();
23-
foreach($marc_record_loads as $resource) {
24-
$resource_id = $resource['id'];
25-
$filename = config::UPLOAD_DIRECTORY . '/' . $resource_id . '.xml';
26-
$files[] = $filename;
27-
}
28-
29-
$compressed_tar = $tar->create($files) or die('Could not create archive. Please go back and try again.');
30-
31-
if(rename(config::UPLOAD_DIRECTORY . '/All.tar.gz', 'download_all/All.tar.gz')){
32-
download('download_all/All.tar.gz', 'All.tar.gz');
33-
}
34-
35-
function download($f_location, $f_name){
36-
header('Content-Description: File Transfer');
37-
header('Content-Type: application/x-tar');
38-
header('Content-Disposition: attachment; filename=' . basename($f_name));
39-
readfile($f_location);
40-
}
12+
$download = new download;
13+
$download_all = $download->download_file();
4114

4215
?>

edit.php

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
require_once 'config.php';
33

4+
// TODO: Convert to OOP
45
$resource_id = $_REQUEST['id'];
56
$submit = $_REQUEST['submit'];
67

0 commit comments

Comments
 (0)