summaryrefslogtreecommitdiffstats
path: root/templates/list_header.inc
blob: 58c7fa613c23ccf2a406d2e1ecfaa1eaac67c75f (plain)
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
<?php
/*

 Copyright (c) 2001 - 2006 Ampache.org
 All rights reserved.

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License v2
 as published by the Free Software Foundation.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/

/**
 * List Header
 * The default pager widget for moving through a list of many items.
 * This relies heavily on the View object to get pieces about how
 * to layout this page.
 */

/* We must have Some Items, if none passed use sesison information */
if (!$total_items) { $total_items = $_SESSION['view_total_items']; }


/* Calculate the Next/Prev Pages */

// Next
$next_offset = $GLOBALS['view']->offset + $GLOBALS['view']->offset_limit;
if ($next_offset > $total_items) { $next_offset = $GLOBALS['view']->offset; }

// Prev
$prev_offset = $GLOBALS['view']->offset - $GLOBALS['view']->offset_limit; 
if ($prev_offset < 0) { $prev_offset = '0'; } 

/* Calculate how many pages total exist */
if (!$GLOBALS['view']->offset_limit) { $GLOBALS['view']->offset_limit = '50'; } 
$pages  = ceil($total_items/$GLOBALS['view']->offset_limit);

/* Calculate current page and how many we have on each side */
$page_data = array('up'=>array(),'down'=>array());

// Can't Divide by 0
if ($GLOBALS['view']->offset > 0) { 
	$current_page = floor($GLOBALS['view']->offset/$GLOBALS['view']->offset_limit);
}
else { 
	$current_page = 0;
}

/* Create 10 pages in either direction */
// Down First
$page = $current_page;
$i = 0;
/* While we have pages left */
while ($page > 0) { 
	if ($i == '15') { $page_data['down'][1] = '...'; $page_data['down'][0] = '0'; break; } 
	$i++;
	$page = $page - 1;
	$page_data['down'][$page] = $page * $GLOBALS['view']->offset_limit;
} // while page > 0

// Up Next
$page = $current_page+1; 
$i = 0;
/* While we have pages left */
while ($page <= $pages) { 
	if ($page * $GLOBALS['view']->offset_limit > $total_items) { break; }
	if ($i == '15') { 
		$key = $pages - 1;
		if (!$page_data['up'][$key]) { $page_data['up'][$key] = '...'; }
		$page_data['up'][$pages] = ($pages-1) * $GLOBALS['view']->offset_limit;
		break;
	}
	$i++;
	$page = $page + 1;
	$page_data['up'][$page] = ($page-1) * $GLOBALS['view']->offset_limit;
} // end while

// Sort These Arrays of Hotness
ksort($page_data['up']);
ksort($page_data['down']);

/* Take the script name and passed action and setup the next action */
preg_match("/.*\/(.+\.php)$/", $_SERVER['SCRIPT_NAME'], $matches);
$action = "action=" . scrub_in($_REQUEST['action']);
$script = conf('web_path') . "/" . $admin_menu . $matches[1];

// are there enough items to even need this view?
if (($pages > 1) && ($_SESSION['view_script'])) {
?>
<table class="list-header" cellpadding="1" cellspacing="0" width="100%">
<tr>
	<td valign="top">
	<a class="list-header" href="<?php echo $script; ?>?<?php echo $action; ?>&amp;offset=<?php echo $prev_offset; ?>&amp;keep_view=true">[<?php echo _('Prev'); ?>]</a>&nbsp;
	</td>
	<td align="center">
	<?php 
		/* Echo Everything below us */
		foreach ($page_data['down'] as $page => $offset) { 
			if ($offset === '...') { echo '...&nbsp;'; } 
			else { 
			// Hack Alert
			$page++;
	?>
	<a class="list-header" href="<?php echo $script; ?>?<?php echo $action; ?>&amp;sort_type=<?php echo $GLOBALS['view']->sort_type;  ?>&amp;offset=<?php echo $offset; ?>&amp;keep_view=true"><?php echo $page; ?></a>&nbsp;
	<?php	
			}
		} // end foreach down

		/* Echo Out current Page */
		$current_page = $current_page +1;
	?>
	<span class="list-header"><strong><?php echo $current_page; ?></strong></span>&nbsp;
	<?php
		
		/* Echo Out Everything Above Us */
		foreach ($page_data['up'] as $page=>$offset) { 
			if ($offset === '...') { echo '...&nbsp;'; } 
			else { 
	?>
	<a class="list-header" href="<?php echo $script; ?>?<?php echo $action; ?>&amp;sort_type=<?php echo $GLOBALS['view']->sort_type;  ?>&amp;offset=<?php echo $offset; ?>&amp;keep_view=true"><?php echo $page; ?></a>&nbsp;
	<?php
			} // end else
		} // end foreach up
		/*
		$counter = 1;
		$offset_pages = 0;
		while ($counter <= $pages) {
		if ($GLOBALS['view']->offset == $offset_pages) { ?>
		<?php } else { ?>
		<?php 	
		} // end if ($GLOBALS['view']->offset == $offset_pages) and else
			$offset_pages += $GLOBALS['view']->offset_limit;
			$counter++;
		} // end while ($counter <= $pages) 
		*/
	?>
	</td>
	<td valign="top">
		<a class="list-header" href="<?php echo $script; ?>?<?php echo $action; ?>&amp;offset=<?php echo $next_offset; ?>&amp;keep_view=true">[<?php echo _('Next'); ?>]</a>&nbsp;
	</td>
</tr>
</table>
<?php
} // if (($pages > 1) && ($_SESSION['view_script']))
?>