-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.php
124 lines (112 loc) · 3.99 KB
/
common.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
###############################################################################
# $Id$
# Common functions to all of the 3m scripts.
###############################################################################
require_once dirname(__FILE__).'/config.php';
### Defined Functions
function fatal($message) {
@header("HTTP/1.1 500 Internal Server Error");
@header("Content-type: text/plain");
error_log($message);
die($message." \nE-mail [email protected] if this problem persists.");
}
# Takes a year and returns the appropriately formated data
# $quote type should either be "UTF-8" or "html"
function formatYear($year, $quote = 'UTF-8') {
global $min_class_year, $max_class_year, $class_year_map;
$utf8_quote = "\xe2\x80\x99";
$html_quote = "&rsquote;";
$quoteSymbol = ($quote == 'html' ? $html_quote : $utf8_quote);
$year = intval($year);
if ($year > $min_class_year && $year < $max_class_year) {
return($quoteSymbol . substr($year, 2));
} else {
# NULL if $year not in $class_year_map!
return $class_year_map[$year];
}
}
# Formats a 10 digit phone number. We better be using 10 digit phone numbers
# Fuck internationalzation
function formatPhone($phone) {
return preg_replace("/([0-9]{3})([0-9]{3})([0-9]{4})/", "$1-$2-$3", $phone);
}
# Queries the databases for all members of the specified department
# Returns an array of position to array of formatted members
# i.e. "Position" => array("Tech Staffer ’81", "Been There Done That Colen");
function getDepartmentMembers($department) {
global $mdb2;
$dquote = $mdb2->quote($department);
$sql = <<<EOF
SELECT display_name, year, position FROM staff
JOIN departments ON (staff.dept=departments.name)
JOIN titles ON (staff.position=titles.name)
WHERE
staff.dept=$dquote AND
staff.active = "yes"
ORDER BY titles.order, staff.year, staff.last, staff.first, staff.middle ASC
EOF;
$res =& $mdb2->query($sql);
if(PEAR::isError($res)) {
error_log($res->getDebugInfo());
fatal("Could not get department members: " . $res->getMessage());
}
$res->bindColumn("display_name", $name);
$res->bindColumn("year", $year);
$res->bindColumn("position", $position);
$ret = array();
while ($row = $res->fetchRow()) {
$formattedYear = formatYear($year);
if ($formattedYear) {
$person = $name .
mb_convert_encoding(' ', 'UTF-8', 'HTML-ENTITIES') .
formatYear($year);
} else {
//trim off space if no year
$person = $name;
}
if(isset($ret[$position])) {
array_push($ret[$position], $person);
} else {
$ret[$position] = array($person);
}
}
return $ret;
}
# Returns an array of Titles valid for the given department
function getDepartmentTitles($department) {
global $mdb2;
$sql = 'SELECT name FROM titles WHERE FIND_IN_SET((SELECT shortname FROM ';
$sql .= 'departments WHERE name = ' . $mdb2->quote($department) . '), ';
$sql .= 'inDepartment) > 0 AND isActive = "yes" ORDER BY titles.order ASC';
$res =& $mdb2->query($sql);
if(PEAR::isError($res)) {
error_log($res->getDebugInfo());
fatal("Could not get title names: " . $res->getMessage());
}
return $res->fetchCol();
}
# Returns an array of Department Names
function getDepartments() {
global $mdb2;
$sql = 'SELECT name FROM departments WHERE isActive = "yes" ORDER BY ';
$sql .= 'departments.order ASC';
$res =& $mdb2->query($sql);
if(PEAR::isError($res)) {
error_log($res->getDebugInfo());
fatal("Could not get department names: " . $res->getMessage());
}
return $res->fetchCol();
}
$mdb2 =& MDB2::connect("mysql://$db_user:$db_pass@$db_host/$db_name");
if(PEAR::isError($mdb2)) {
error_log($mdb2->getDebugInfo());
fatal("Could not connect to database: ".$mdb2->getMessage());
}
# We care about empty strings. Supposedly Oracle thinks "" and NULL are the same
$mdb2->setOption('portability',
MDB2_PORTABILITY_ALL ^ MDB2_PORTABILITY_EMPTY_TO_NULL);
$res =& $mdb2->query('SET NAMES utf8'); # Use UTF-8 for communication
if(PEAR::isError($res)) {
error_log($mdb2->getDebugInfo());
}