forked from hsrai/GNDEC-SMS-Service
-
Notifications
You must be signed in to change notification settings - Fork 2
/
class-contactlist.php
executable file
·260 lines (226 loc) · 8.93 KB
/
class-contactlist.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<?php
/*************************************************************
* THE ADDRESS BOOK : version 1.04
*
* lib/class-contactlist.php
* Object: Retrieves information relating to the contact list to be displayed.
*
*************************************************************/
// Original code by hannelore
class ContactList {
var $group_id;
var $group_name;
var $current_letter;
var $max_entries;
var $current_page;
var $total_pages;
var $sql;
var $title;
var $nav_menu;
function ContactList() {
global $options;
// DEPENDENT VARIABLES -- Values for these variables are passed to the object after ContactList is created
// If no values are provided, then it uses some defaults
$this->group_id = 0; // defaults to 0 upon creation of object
$this->current_page = 1; // defaults to first page
$this->current_letter = $options->defaultLetter; // defaults to value set in options
$this->max_entries = $options->limitEntries; // defaults to value set in options; 0=no maximum (display all on page 1)
// RESULTANT VARIABLES -- Values for these variables start out blank and will be filled in by this object's methods
$this->group_name = ""; // determined in $this->group_name()
$this->total_pages = 1; // total # of pages, determined in $this->retrieve()
$this->sql = ""; // determined in $this->retrieve(), useful for debugging purposes
$this->title = ""; // determined in $this->title()
$this->nav_menu = ""; // determined in $this->create_nav()
}
function group_name() {
global $db_link;
global $lang;
// OBTAIN NAME OF GROUP IN DISPLAYED LIST
// Force $this->group_id to an integer equal to 0 or greater
$this->group_id = intval($this->group_id);
if ($this->group_id <= 0) $this->group_id = 0;
// group_id = 0 --> "All Entries"
if ($this->group_id == 0) $this->group_name = $lang['GROUP_ALL_LABEL'];
// group_id = 1 --> "Ungrouped Entries"
elseif ($this->group_id == 1) $this->group_name = $lang['GROUP_UNGROUPED_LABEL'];
// group_id = 2 --> "Hidden Entries"
elseif ($this->group_id == 2) {
// Admin check
if ($_SESSION['usertype'] != "admin") {
reportScriptError("URL tampering detected.");
exit();
}
$this->group_name = $lang['GROUP_HIDDEN_LABEL']; // "Hidden Entries"
}
// group_id >= 3 --> Check the database for user-defined group
else {
$tbl_grouplist = mysql_fetch_array(mysql_query("SELECT * FROM " . TABLE_GROUPLIST . " AS grouplist WHERE groupid=$this->group_id", $db_link));
$this->group_name = $tbl_grouplist['groupname'];
// Reassign to "All Entries" if given a groupid that doesn't exist
if ($this->group_name == "") {
$this->group_id = 0;
$this->group_name = "All Entries";
}
}
// Return value
return $this->group_name;
}
function title() {
$this->title = $this->group_name;
if (!empty($this->current_letter)) $this->title .= " - $this->current_letter";
if ($this->total_pages > 1) $this->title .= " (page $this->current_page of $this->total_pages)";
return $this->title;
}
function retrieve($uname) {
global $db_link;
// The following needs to be set to retrieve correctly
// $this->group_id
// $this->current_letter
// $this->max_entries
// $this->current_page
// CREATE INITIAL SQL FRAGMENT
$this->sql = "SELECT contact.id, CONCAT(lastname,', ',firstname) AS fullname, lastname, firstname,
refid, line1, line2, city, state, zip, phone1, phone2, country, whoAdded
FROM ( " . TABLE_CONTACT . " AS contact ";
// CREATE SQL FRAGMENTS TO FILTER BY GROUP
// group_id = 0 --> "All Entries"
// if ($this->group_id == 0) {
// $sql_group = ") LEFT JOIN " . TABLE_ADDRESS . " AS address ON contact.id=address.id AND contact.primaryAddress=address.refid
// WHERE contact.hidden != 1 and whoAdded = '".$uname."'";
if ($this->group_id == 0) {
$sql_group = ") LEFT JOIN " . TABLE_ADDRESS . " AS address ON contact.id=address.id AND contact.primaryAddress=address.refid
WHERE contact.hidden != 1";
}
// group_id = 1 --> "Ungrouped Entries"
elseif ($this->group_id == 1) {
$sql_group = ") LEFT JOIN " . TABLE_ADDRESS . " AS address ON contact.id=address.id AND contact.primaryAddress=address.refid
LEFT JOIN " . TABLE_GROUPS . " AS groups ON groups.id=contact.id
WHERE groups.id IS NULL AND contact.hidden != 1 ";
}
// group_id = 2 --> "Hidden Entries"
elseif ($this->group_id == 2) {
$sql_group = ") LEFT JOIN " . TABLE_ADDRESS . " AS address ON contact.id=address.id AND contact.primaryAddress=address.refid
WHERE contact.hidden = 1 ";
}
// group_id >= 3 --> Specified user-defined group
else {
$sql_group = ", " . TABLE_GROUPS . " AS groups)
LEFT JOIN ". TABLE_ADDRESS ." AS address ON contact.id=address.id AND contact.primaryAddress=address.refid
WHERE contact.id=groups.id AND groups.groupid=$this->group_id AND contact.hidden != 1 ";
}
// CREATE SQL FRAGMENTS TO FILTER BY LETTER
switch ($this->current_letter) {
case "": // No letter filter
$sql_letter = "";
break;
case "1": // If selecting non-alphabetical characters
$sql_letter = " AND lastname REGEXP '^[^[:alpha:]]'";
break;
default: // If a letter is set
$sql_letter = " AND lastname LIKE '$this->current_letter%'";
break;
}
// CREATE SQL FRAGMENTS TO LIMIT NUMBER OF ENTRIES PER PAGE
if ($this->max_entries > 0) { //if this option is set, limit the number of entries shown per page
// Count number of rows (this uses group and letter sql fragments, determined previously)
$count = mysql_fetch_array(mysql_query("SELECT COUNT(*) FROM " . TABLE_CONTACT . " AS contact" . $sql_group . $sql_letter, $db_link));
$this->total_pages = intval(ceil($count[0]/$this->max_entries)); //divide the total entries by the limit per page. Round up to an integer
// Users like to start counting from 1 in stead of 0
$lowerLimit = $this->current_page - 1; //deduct 1 from the result page number in the URL, use this to calculate the lower limit of the range
$lowerLimit = $lowerLimit*$this->max_entries; //lower limit of the range
$sql_limit = " LIMIT $lowerLimit, $this->max_entries";
}
// ASSEMBLE THE SQL QUERY
$this->sql .= $sql_group . $sql_letter . " ORDER BY fullname" . $sql_limit;
// EXECUTE THE SQL QUERY
$r_contact = mysql_query($this->sql, $db_link)
or die(reportSQLError($this->sql));
// RETURN RESULTS OF QUERY
return $r_contact;
}
function nav_abc($link) {
$abc = array('A' => 'A',
'B' => 'B',
'C' => 'C',
'D' => 'D',
'E' => 'E',
'F' => 'F',
'G' => 'G',
'H' => 'H',
'I' => 'I',
'J' => 'J',
'K' => 'K',
'L' => 'L',
'M' => 'M',
'N' => 'N',
'O' => 'O',
'P' => 'P',
'Q' => 'Q',
'R' => 'R',
'S' => 'S',
'T' => 'T',
'U' => 'U',
'V' => 'V',
'W' => 'W',
'X' => 'X',
'Y' => 'Y',
'Z' => 'Z',
'1' => '[0-9]',
'' => '[all]');
foreach ($abc as $key => $letter) {
$this->nav_menu .= "<a href='$link$key'>$letter</a>\n";
}
}
function nav_pages($link) {
if ($this->total_pages > 1) { //check whether there are multiple result pages for the request
$this->nav_menu .= "Pages: ";
for ($i=1; $i <= $this->total_pages; $i++) { //create an array of links to the result pages
if ($this->current_page == $i) { //indicate the current page in the navigation
$this->nav_menu .= "<b>[$i]</b>\n";
}
else { //create links to all other result pages
$this->nav_menu .= "<a href='$link$this->current_letter&page=$i'>$i</a> \n";
}
}
$this->nav_menu .= "<a href='$link$this->current_letter&limit=0'>[all]</a>\n";
}
}
function create_nav() {
/*
Here's the logic behind the navigation links:
If we have... Then the links will have....
group letter page
1. x (always occurs) - use letter links w/ #
2. x x use letter links only (no page links)
3. x x x use page links within current letter, and letter links to page 1
4. x x use page links only (no letter)
*/
// Base link
$link = $_SERVER['PHP_SELF'] . "?groupid=$this->group_id";
// Case 2. Group and letter (no page)
if ((!empty($this->current_letter)) && ($this->total_pages <= 1)) {
$link .= "&letter=";
$this->nav_abc($link);
}
// Case 3. Group, letter, and page.
elseif ((!empty($this->current_letter)) && ($this->total_pages > 1)) {
$link .= "&limit=$this->max_entries&letter=";
$this->nav_abc($link);
$this->nav_pages($link);
}
// Case 4. Group and page (no letter)
elseif ((empty($this->current_letter)) && ($this->total_pages > 1)) {
$link .= "&limit=$this->max_entries";
$this->nav_pages($link);
}
// Case 1. (default) Group only.
else {
//$link .= "#";
$link = "#";
$this->nav_abc($link);
}
return $this->nav_menu;
}
}
// END ContactList
?>