File: SmartyPaginate.class.php

package info (click to toggle)
collabtive 2.0%2Bdfsg-5
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 17,708 kB
  • ctags: 27,425
  • sloc: php: 100,872; sh: 72; makefile: 50
file content (437 lines) | stat: -rw-r--r-- 13,077 bytes parent folder | download | duplicates (2)
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
<?php

/**
 * Project:     SmartyPaginate: Pagination for the Smarty Template Engine
 * File:        SmartyPaginate.class.php
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * @link http://www.phpinsider.com/php/code/SmartyPaginate/
 * @copyright 2001-2005 New Digital Group, Inc.
 * @author Monte Ohrt <monte at newdigitalgroup dot com>
 * @package SmartyPaginate
 * @version 1.6
 */

class SmartyPaginate
{
    /**
     * Class Constructor
     */
    function SmartyPaginate()
    {
    }

    /**
     * initialize the session data
     *
     * @param string $id the pagination id
     * @param string $formvar the variable containing submitted pagination information
     */
    static function connect($id = 'default', $formvar = null)
    {
        if (!isset($_SESSION['SmartyPaginate'][$id]))
        {
            SmartyPaginate::reset($id);
        }
        // use $_GET by default unless otherwise specified
        $_formvar = isset($formvar) ? $formvar : $_GET;
        // set the current page
        $_total = SmartyPaginate::getTotal($id);

        if (isset($_formvar[SmartyPaginate::getUrlVar($id)]) && $_formvar[SmartyPaginate::getUrlVar($id)] > 0 && (!isset($_total) || $_formvar[SmartyPaginate::getUrlVar($id)] <= $_total))
            $_SESSION['SmartyPaginate'][$id]['current_item'] = $_formvar[$_SESSION['SmartyPaginate'][$id]['urlvar']];
    }

    /**
     * see if session has been initialized
     *
     * @param string $id the pagination id
     */
    function isConnected($id = 'default')
    {
        return isset($_SESSION['SmartyPaginate'][$id]);
    }

    /**
     * reset/init the session data
     *
     * @param string $id the pagination id
     */
    static function reset($id = 'default')
    {
        $_SESSION['SmartyPaginate'][$id] = array('item_limit' => 10,
            'item_total' => null,
            'current_item' => 1,
            'urlvar' => 'next',
            'url' => full_url(),
            'prev_text' => 'prev',
            'next_text' => 'next',
            'first_text' => 'first',
            'last_text' => 'last'
            );
    }

    /**
     * clear the SmartyPaginate session data
     *
     * @param string $id the pagination id
     */
    static function disconnect($id = null)
    {
        if (isset($id))
            unset($_SESSION['SmartyPaginate'][$id]);
        else
            unset($_SESSION['SmartyPaginate']);
    }

    /**
     * set maximum number of items per page
     *
     * @param string $id the pagination id
     */
    static function setLimit($limit, $id = 'default')
    {
        if (!preg_match('!^\d+$!', $limit))
        {
            trigger_error('SmartyPaginate setLimit: limit must be an integer.');
            return false;
        }
        settype($limit, 'integer');
        if ($limit < 1)
        {
            trigger_error('SmartyPaginate setLimit: limit must be greater than zero.');
            return false;
        }
        $_SESSION['SmartyPaginate'][$id]['item_limit'] = $limit;
    }

    /**
     * get maximum number of items per page
     *
     * @param string $id the pagination id
     */
    static function getLimit($id = 'default')
    {
        return $_SESSION['SmartyPaginate'][$id]['item_limit'];
    }

    /**
     * set the total number of items
     *
     * @param int $total the total number of items
     * @param string $id the pagination id
     */
    static function setTotal($total, $id = 'default')
    {
        if (!preg_match('!^\d+$!', $total))
        {
            trigger_error('SmartyPaginate setTotal: total must be an integer.');
            return false;
        }
        settype($total, 'integer');
        if ($total < 0)
        {
            trigger_error('SmartyPaginate setTotal: total must be positive.');
            return false;
        }
        $_SESSION['SmartyPaginate'][$id]['item_total'] = $total;
    }

    /**
     * get the total number of items
     *
     * @param string $id the pagination id
     */
    static function getTotal($id = 'default')
    {
        return $_SESSION['SmartyPaginate'][$id]['item_total'];
    }

    /**
     * set the url used in the links, default is $PHP_SELF
     *
     * @param string $url the pagination url
     * @param string $id the pagination id
     */
    static function setUrl($url, $id = 'default')
    {
        $_SESSION['SmartyPaginate'][$id]['url'] = $url;
    }

    /**
     * get the url variable
     *
     * @param string $id the pagination id
     */
    static function getUrl($id = 'default')
    {
        return $_SESSION['SmartyPaginate'][$id]['url'];
    }

    /**
     * set the url variable ie. ?next=10
     *                            ^^^^
     *
     * @param string $url url pagination varname
     * @param string $id the pagination id
     */
    static function setUrlVar($urlvar, $id = 'default')
    {
        $_SESSION['SmartyPaginate'][$id]['urlvar'] = $urlvar;
    }

    /**
     * get the url variable
     *
     * @param string $id the pagination id
     */
    static function getUrlVar($id = 'default')
    {
        return $_SESSION['SmartyPaginate'][$id]['urlvar'];
    }

    /**
     * set the current item (usually done automatically by next/prev links)
     *
     * @param int $item index of the current item
     * @param string $id the pagination id
     */
    static function setCurrentItem($item, $id = 'default')
    {
        $_SESSION['SmartyPaginate'][$id]['current_item'] = $item;
    }

    /**
     * get the current item
     *
     * @param string $id the pagination id
     */
    static function getCurrentItem($id = 'default')
    {
        return $_SESSION['SmartyPaginate'][$id]['current_item'];
    }

    /**
     * get the current item index
     *
     * @param string $id the pagination id
     */
    static function getCurrentIndex($id = 'default')
    {
        return $_SESSION['SmartyPaginate'][$id]['current_item'] - 1;
    }

    /**
     * get the last displayed item
     *
     * @param string $id the pagination id
     */
    static function getLastItem($id = 'default')
    {
        $_total = SmartyPaginate::getTotal($id);
        $_limit = SmartyPaginate::getLimit($id);
        $_last = SmartyPaginate::getCurrentItem($id) + $_limit - 1;
        return ($_last <= $_total) ? $_last : $_total;
    }

    /**
     * assign $paginate var values
     *
     * @param obj $ &$smarty the smarty object reference
     * @param string $var the name of the assigned var
     * @param string $id the pagination id
     */
    static function assign(&$smarty, $var = 'paginate', $id = 'default')
    {
        if (is_object($smarty) && (strtolower(get_class($smarty)) == 'smarty' || is_subclass_of($smarty, 'smarty')))
        {
            $_paginate['total'] = SmartyPaginate::getTotal($id);
            $_paginate['first'] = SmartyPaginate::getCurrentItem($id);
            $_paginate['last'] = SmartyPaginate::getLastItem($id);
            $_paginate['page_current'] = ceil(SmartyPaginate::getLastItem($id) / SmartyPaginate::getLimit($id));
            $_paginate['page_total'] = ceil(SmartyPaginate::getTotal($id) / SmartyPaginate::getLimit($id));
            $_paginate['size'] = $_paginate['last'] - $_paginate['first'];
            $_paginate['url'] = SmartyPaginate::getUrl($id);
            $_paginate['urlvar'] = SmartyPaginate::getUrlVar($id);
            $_paginate['current_item'] = SmartyPaginate::getCurrentItem($id);
           // $_paginate['prev_text'] = SmartyPaginate::getPrevText($id);
           // $_paginate['next_text'] = SmartyPaginate::getNextText($id);
        	 $_paginate['prev_text'] = "<<";
        	 $_paginate['next_text'] = ">>";

            $_paginate['limit'] = SmartyPaginate::getLimit($id);

            $_item = 1;
            $_page = 1;
            while ($_item <= $_paginate['total'])
            {
                $_paginate['page'][$_page]['number'] = $_page;
                $_paginate['page'][$_page]['item_start'] = $_item;
                $_paginate['page'][$_page]['item_end'] = ($_item + $_paginate['limit'] - 1 <= $_paginate['total']) ? $_item + $_paginate['limit'] - 1 : $_paginate['total'];
                $_paginate['page'][$_page]['is_current'] = ($_item == $_paginate['current_item']);
                $_item += $_paginate['limit'];
                $_page++;
            }
            $smarty->assign($var, $_paginate);
        }
        else
        {
            trigger_error("SmartyPaginate: [assign] I need a valid Smarty object.");
            return false;
        }
    }

    /**
     * set the default text for the "previous" page link
     *
     * @param string $text index of the current item
     * @param string $id the pagination id
     */
    static function setPrevText($text, $id = 'default')
    {
        $_SESSION['SmartyPaginate'][$id]['prev_text'] = $text;
    }

    /**
     * get the default text for the "previous" page link
     *
     * @param string $id the pagination id
     */
    static function getPrevText($id = 'default')
    {
        return $_SESSION['SmartyPaginate'][$id]['prev_text'];
    }

    /**
     * set the text for the "next" page link
     *
     * @param string $text index of the current item
     * @param string $id the pagination id
     */
    static function setNextText($text, $id = 'default')
    {
        $_SESSION['SmartyPaginate'][$id]['next_text'] = $text;
    }

    /**
     * get the default text for the "next" page link
     *
     * @param string $id the pagination id
     */
    static function getNextText($id = 'default')
    {
        return $_SESSION['SmartyPaginate'][$id]['next_text'];
    }

    /**
     * set the text for the "first" page link
     *
     * @param string $text index of the current item
     * @param string $id the pagination id
     */
    static function setFirstText($text, $id = 'default')
    {
        $_SESSION['SmartyPaginate'][$id]['first_text'] = $text;
    }

    /**
     * get the default text for the "first" page link
     *
     * @param string $id the pagination id
     */
    static function getFirstText($id = 'default')
    {
        return $_SESSION['SmartyPaginate'][$id]['first_text'];
    }

    /**
     * set the text for the "last" page link
     *
     * @param string $text index of the current item
     * @param string $id the pagination id
     */
    static function setLastText($text, $id = 'default')
    {
        $_SESSION['SmartyPaginate'][$id]['last_text'] = $text;
    }

    /**
     * get the default text for the "last" page link
     *
     * @param string $id the pagination id
     */
    static function getLastText($id = 'default')
    {
        return $_SESSION['SmartyPaginate'][$id]['last_text'];
    }

    /**
     * set default number of page groupings in {paginate_middle}
     *
     * @param string $id the pagination id
     */
    static function setPageLimit($limit, $id = 'default')
    {
        if (!preg_match('!^\d+$!', $limit))
        {
            trigger_error('SmartyPaginate setPageLimit: limit must be an integer.');
            return false;
        }
        settype($limit, 'integer');
        if ($limit < 1)
        {
            trigger_error('SmartyPaginate setPageLimit: limit must be greater than zero.');
            return false;
        }
        $_SESSION['SmartyPaginate'][$id]['page_limit'] = $limit;
    }

    /**
     * get default number of page groupings in {paginate_middle}
     *
     * @param string $id the pagination id
     */
    static function getPageLimit($id = 'default')
    {
        return $_SESSION['SmartyPaginate'][$id]['page_limit'];
    }

    /**
     * get the previous page of items
     *
     * @param string $id the pagination id
     */
    static function _getPrevPageItem($id = 'default')
    {
        $_prev_item = $_SESSION['SmartyPaginate'][$id]['current_item'] - $_SESSION['SmartyPaginate'][$id]['item_limit'];

        return ($_prev_item > 0) ? $_prev_item : false;
    }

    /**
     * get the previous page of items
     *
     * @param string $id the pagination id
     */
    static function _getNextPageItem($id = 'default')
    {
        $_next_item = $_SESSION['SmartyPaginate'][$id]['current_item'] + $_SESSION['SmartyPaginate'][$id]['item_limit'];

        return ($_next_item <= $_SESSION['SmartyPaginate'][$id]['item_total']) ? $_next_item : false;
    }
}

?>