soo_page_numbers Page 2 of 2 «
soo_page_numbers source code
1 : require_plugin('soo_txp_obj');
2 :
3 : if ( @txpinterface == 'admin' )
4 : {
5 : add_privs('plugin_lifecycle.soo_page_numbers','1,2');
6 : register_callback('soo_page_numbers_lifecycle', 'plugin_lifecycle.soo_page_numbers');
7 : }
8 :
9 : // delete the Textpack
10 : // Note: callback_event() does not display message from lifecycle callbacks
11 : function soo_page_numbers_lifecycle ( $event, $step )
12 : {
13 : if ( substr($event, strlen('plugin_lifecycle.')) === 'soo_page_numbers' && $step === 'deleted' )
14 : {
15 : safe_delete('txp_lang', "event = 'soo_page_numbers'");
16 : }
17 : }
18 :
19 : // load the Textpack
20 : if ( @txpinterface == 'public' )
21 : {
22 : if ( $soo_page_numbers_textpack = load_lang_event('soo_page_numbers') )
23 : {
24 : global $textarray;
25 : $textarray += $soo_page_numbers_textpack;
26 : unset($soo_page_numbers_textpack);
27 : }
28 : }
29 :
30 : function soo_page_links ( $atts )
31 : {
32 : extract(lAtts(array(
33 : 'placeholder' => '…',
34 : 'window_size' => 5,
35 : 'active_class' => 'here',
36 : 'wraptag' => '',
37 : 'class' => '',
38 : 'html_id' => '',
39 : 'break' => '',
40 : 'showalways' => false,
41 : ), $atts));
42 :
43 : global $thispage;
44 :
45 : if ( is_array($thispage) ) extract($thispage);
46 : else return _soo_page_numbers_secondpass(__FUNCTION__, $atts);
47 :
48 : if ( ! $showalways and $numPages <= 1 ) return;
49 :
50 : $w_start = max(1,
51 : min($pg - floor($window_size / 2), $numPages - $window_size + 1));
52 : $w_end = min($w_start + $window_size - 1, $numPages);
53 :
54 : $pgs = array_unique(array_merge(
55 : array(1), range($w_start, $w_end), array($numPages)
56 : ));
57 :
58 : $break_text = $wraptag ? '' : $break;
59 : $text_tag = ( $break and $wraptag ) ? '' : 'span';
60 : $active_class = $active_class ? " class=\"$active_class\"" : '';
61 :
62 : $uri = new soo_uri;
63 : while ( $pgs )
64 : {
65 : $n = array_shift($pgs);
66 : $uri->set_query_param('pg', ( $n > 1 ? $n : null ));
67 : $fill = $pgs ? ( $pgs[0] > $n + 1 ? $placeholder : $break_text ) : '';
68 : if ( $n == $pg )
69 : $items[] = $text_tag ?
70 : tag($n, $text_tag, $active_class) : $n;
71 : else
72 : $items[] = href($n, $uri->full, ' title="' . gTxt('page') . sp . $n . '"');
73 : if ( $n < $numPages and $fill )
74 : $items[] = $text_tag ?
75 : tag($fill, $text_tag) : $fill;
76 : }
77 : $uri->set_query_param('pg', ( $pg > 1 ? $pg : null ));
78 : if ( isset($items) )
79 : return $wraptag ? str_replace("<$break>$pg<", "<$break$active_class>$pg<",
80 : doWrap($items, $wraptag, $break, $class, '', '', '', $html_id))
81 : : implode($break_text ? '' : n, $items);
82 : }
83 :
84 : function soo_page_count ( $atts )
85 : {
86 : extract(lAtts(array(
87 : 'format' => gTxt('soo_page_count'),
88 : 'prev' => '«',
89 : 'next' => '»',
90 : 'first' => '|«',
91 : 'last' => '»|',
92 : 'showalways' => false,
93 : 'wraptag' => '',
94 : ), $atts));
95 :
96 : global $thispage;
97 :
98 : if ( is_array($thispage) ) extract($thispage);
99 : else return _soo_page_numbers_secondpass(__FUNCTION__, $atts);
100 :
101 : if ( ! $showalways and $numPages <= 1 ) return;
102 :
103 : $uri = new soo_uri;
104 : if ( $pg > 1 )
105 : {
106 : $uri->set_query_param('pg', null);
107 : $first = href($first, $uri->full, ' title="' . gTxt('page') . ' 1"');
108 : $prev_pg_param = $pg > 2 ? $pg - 1 : null;
109 : $uri->set_query_param('pg', $prev_pg_param);
110 : $prev = href($prev, $uri->full, ' title="' . gTxt('prev') . '"' );
111 : }
112 : elseif ( ! $showalways )
113 : $first = $prev = '';
114 :
115 : if ( $pg < $numPages ) {
116 : $uri->set_query_param('pg', $numPages);
117 : $last = href($last, $uri->full, ' title="' . gTxt('page') . sp . $numPages . '"');
118 : $uri->set_query_param('pg', $pg + 1);
119 : $next = href($next, $uri->full, ' title="' . gTxt('next') . '"' );
120 : }
121 : elseif ( ! $showalways )
122 : $last = $next = '';
123 :
124 : $out = str_replace(
125 : array('{prev}', '{next}', '{first}', '{last}', '{current}', '{total}'),
126 : array($prev, $next, $first, $last, $pg, $numPages), $format);
127 : return $wraptag ? tag($out, $wraptag) : $out;
128 : }
129 :
130 : function soo_prev_page ( $atts )
131 : {
132 : if ( isset($atts['text']) )
133 : {
134 : $atts['prev'] = $atts['text'];
135 : unset($atts['text']);
136 : }
137 : $atts['format'] = '{prev}';
138 : return soo_page_count($atts);
139 : }
140 :
141 : function soo_next_page ( $atts )
142 : {
143 : if ( isset($atts['text']) )
144 : {
145 : $atts['next'] = $atts['text'];
146 : unset($atts['text']);
147 : }
148 : $atts['format'] = '{next}';
149 : return soo_page_count($atts);
150 : }
151 :
152 : function _soo_page_numbers_secondpass ( $func, $atts )
153 : {
154 : // in case $func's associated tag comes before an article tag,
155 : // this runs the tag again during textpattern()'s second parse() pass
156 : global $pretext;
157 : if ( $pretext['secondpass'] ) return; // you only live twice
158 : foreach ( $atts as $k => $v )
159 : $a[] = $k . '="' . $v . '" ';
160 : return "<txp:$func " . ( isset($a) ? implode('', $a) : '' ) . '/>';
161 : }

