bergie / midcom

MVC framework for PHP and the Midgard content repository

This URL has Read+Write access

bergie (author)
Wed Jun 24 21:57:52 -0700 2009
commit  afd3f46b73d85a8d425b845a593a65afd8afb5e3
tree    681bcb7d31b4da4eaecb1c993c3ca2e01fde0dfd
parent  320e0614676db7a65372a5d321d02c570fde2668
midcom / org_openpsa_qbpager / pager.php
100644 670 lines (580 sloc) 20.324 kb
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
<?php
/**
* @package org_openpsa_qbpager
* @author The Midgard Project, http://www.midgard-project.org
* @copyright The Midgard Project, http://www.midgard-project.org
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
*/
 
/**
* Pages QB resultsets
*
* @package org_openpsa_qbpager
*/
class org_openpsa_qbpager_pager
{
    private $qb = false;
    private $qb_count = false;
    private $pager_id = false;
    private $offset = 0;
    private $limit;
    private $prefix = '';
    private $data = array();
    private $current_page = 1;
    public $results_per_page = 25;
    public $display_pages = 10;
    public $count = false;
    private $_count_mode = false;
    
    /**
* parameter listening enabled
*
* @access private
* @var boolean
*/
    private $listen_parameters = false;
 
    /**
* Registered HTTP GET -parameters for listening
*
* @access private
* @var array
*/
    private $http_get_params = array();
 
    /**
* Cache for parameters to be listened
*
* @access private
* @var string
*/
    private $parameter_cache = false;
    
    function __construct($classname, $pager_id = null)
    {
        $this->limit =& $this->results_per_page;
        
        if (is_null($pager_id))
        {
            $pager_id = $classname;
        }
        
        if (!class_exists($classname))
        {
            throw new Exception("MgdSchema class {$classname} not loaded.");
        }
 
        $this->pager_id = $pager_id;
        $this->qb = new midgard_query_builder($classname);
        // Make another QB for counting, we need to do this to avoid trouble with core internal references system
        $this->qb_count = new midgard_query_builder($classname);
        if (!$this->check_sanity())
        {
            return false;
        }
        $this->prefix = 'org_openpsa_qbpager_' . $this->pager_id . '_';
 
        return true;
    }
    
    public function listen_parameter($name, $value=false)
    {
        if (empty($name))
        {
            return;
        }
 
        if ( isset($this->http_get_parameters[$name])
            && $this->http_get_parameters[$name] == $value)
        {
            return;
        }
        $this->http_get_parameters[$name] = $value;
 
        $this->listen_parameters = true;
    }
    
    private function collect_parameters()
    {
        if (empty($this->http_get_parameters))
        {
            $this->_parameter_cache = '';
            return;
        }
 
        $_prefix = '&';
        $this->_parameter_cache = '';
 
        foreach ($this->http_get_parameters as $key => $value)
        {
            if (isset($_MIDCOM->dispatcher->get[$key]))
            {
                if ($value)
                {
                    if (is_array($value))
                    {
                        foreach ($value as $val)
                        {
                            if ($_MIDCOM->dispatcher->get[$key] == $val)
                            {
                                $this->_parameter_cache .= "{$_prefix}{$key}={$val}";
                            }
                        }
                    }
                    elseif ($_MIDCOM->dispatcher->get[$key] == $value)
                    {
                        $this->_parameter_cache .= "{$_prefix}{$key}={$value}";
                    }
                    elseif ($value == "*")
                    {
                        $this->_parameter_cache .= "{$_prefix}{$key}={$_MIDCOM->dispatcher->get[$key]}";
                    }
                }
                elseif (! $_MIDCOM->dispatcher->get[$key])
                {
                    $this->_parameter_cache .= "{$_prefix}{$key}";
                }
            }
        }
    }
 
    private function get_parameter_string()
    {
        if (! $this->listen_parameters)
        {
            return '';
        }
 
        if (! $this->_parameter_cache)
        {
            $this->collect_parameters();
        }
 
        return $this->_parameter_cache;
    }
 
    /**
* Makes sure we have some absolutely required things properly set
*/
    private function check_sanity()
    {
        if (!is_object($this->qb))
        {
            return false;
        }
        
        if (empty($this->pager_id))
        {
 
            return false;
        }
        return true;
    }
    
    /**
* Check $_REQUEST for variables and sets internal status accordingly
*/
    private function check_request_variables()
    {
        $page_var = $this->prefix . 'page';
        $results_var = $this->prefix . 'results';
        if ( array_key_exists($page_var, $_REQUEST)
            && !empty($_REQUEST[$page_var]))
        {
            $this->current_page = $_REQUEST[$page_var];
        }
        if ( array_key_exists($results_var, $_REQUEST)
            && !empty($_REQUEST[$results_var]))
        {
            $this->results_per_page = $_REQUEST[$results_var];
        }
        $this->offset = ($this->current_page-1) * $this->results_per_page;
        if ($this->offset < 0)
        {
            $this->offset = 0;
        }
        return;
    }
    
    /**
* Get the current page number
*/
    public function get_current_page()
    {
        return $this->current_page;
    }
    
    /**
* Generates a previous/next selector
*
* @return string Previous/Next selector
*/
    public function get_previousnext($acl_checks = false)
    {
        $this->data['prefix'] = $this->prefix;
        $this->data['current_page'] = $this->current_page;
        $this->data['page_count'] = $this->count_pages($acl_checks);
        $this->data['results_per_page'] = $this->limit;
        $this->data['offset'] = $this->offset;
        $this->data['display_pages'] = $this->display_pages;
        
        $previousnext = '';
 
        //Won't work (wrong scope), so the code is copied below.
        //midcom_show_style('show-pages');
        $data =& $this->data;
 
        //Skip the header in case we only have one page
        if ($data['page_count'] <= 1)
        {
            return;
        }
 
        //TODO: "showing results (offset)-(offset+limit)
        //TODO: Localizations
        $page_var = $data['prefix'] . 'page';
        $results_var = $data['prefix'] . 'results';
        $previousnext .= '<div class="org_openpsa_qbpager_previousnext">';
 
        if ($data['current_page'] > 1)
        {
            $previous = $data['current_page'] - 1;
            $previousnext .= "\n<a class=\"previous_page\" href=\"?{$page_var}={$previous}" . $this->get_query_string() . "\">" . 'previous' . "</a>";
        }
 
        if ($data['current_page'] < $data['page_count'])
        {
            $next = $data['current_page'] + 1;
            $previousnext .= "\n<a class=\"next_page\" href=\"?{$page_var}={$next}" . $this->get_query_string() . "\">" . 'next' . "</a>";
        }
 
        $previousnext .= "\n</div>\n";
 
        return $previousnext;
    }
    
    /**
* Displays page selector
*/
    public function get_pages($acl_checks=false)
    {
        $this->data['prefix'] = $this->prefix;
        $this->data['current_page'] = $this->current_page;
        $this->data['page_count'] = $this->count_pages($acl_checks);
        $this->data['results_per_page'] = $this->limit;
        $this->data['offset'] = $this->offset;
        $this->data['display_pages'] = $this->display_pages;
        //Won't work (wrong scope), so the code is copied below.
        //midcom_show_style('show-pages');
        $data =& $this->data;
 
        //Skip the header in case we only have one page
        if ($data['page_count'] <= 1)
        {
            return;
        }
        
        $pages = '';
 
        //TODO: "showing results (offset)-(offset+limit)
        //TODO: Localizations
        $page_var = $data['prefix'] . 'page';
        $results_var = $data['prefix'] . 'results';
        $pages .= '<div class="org_openpsa_qbpager_pages">';
        $page = 0;
        $display_start = $data['current_page'] - ceil($data['display_pages']/2);
        if ($display_start < 0)
        {
            $display_start = 0;
        }
        $display_end = $data['current_page'] + ceil($data['display_pages']/2);
        if ($display_end > $data['page_count'])
        {
            $display_end = $data['page_count'];
        }
 
        if ($data['current_page'] > 1)
        {
            $previous = $data['current_page'] - 1;
            if ($previous != 1)
            {
                $pages .= "\n<a class=\"first_page\" href=\"?{$page_var}=1" . $this->get_query_string() . "\">" . 'first' . "</a>";
            }
            $pages .= "\n<a class=\"previous_page\" href=\"?{$page_var}={$previous}" . $this->get_query_string() . "\">" . 'previous' . "</a>";
        }
 
 
        while ($page++ < $display_end)
        {
            if ($page < $display_start)
            {
                continue;
            }
            if ($page == $data['current_page'])
            {
                $pages .= "\n<span class=\"current_page\">{$page}</span>";
                continue;
            }
            $pages .= "\n<a class=\"select_page\" href=\"?{$page_var}={$page}" . $this->get_query_string() . "\">{$page}</a>";
        }
 
        if ($data['current_page'] < $data['page_count'])
        {
            $next = $data['current_page'] + 1;
            $pages .= "\n<a class=\"next_page\" href=\"?{$page_var}={$next}" . $this->get_query_string() . "\">" . 'next' . "</a>";
 
            if ($next != $data['page_count'])
            {
                $pages .= "\n<a class=\"last_page\" href=\"?{$page_var}={$data['page_count']}" . $this->get_query_string() . "\">" . 'last' . "</a>";
            }
        }
 
        $pages .= "\n</div>\n";
 
        return $pages;
    }
 
    /**
* Displays page selector as XML
*/
    public function get_pages_as_xml($acl_checks = false)
    {
        $pages_xml_str = "<pages ";
        
        $this->data['prefix'] = $this->prefix;
        $this->data['current_page'] = $this->current_page;
        $this->data['page_count'] = $this->count_pages($acl_checks);
        $this->data['results_per_page'] = $this->limit;
        $this->data['offset'] = $this->offset;
        $this->data['display_pages'] = $this->display_pages;
        $data =& $this->data;
        
        $pages_xml_str .= "total=\"{$data['page_count']}\">\n";
        
        //Skip the header in case we only have one page
        if ($data['page_count'] <= 1)
        {
            $pages_xml_str .= "</pages>\n";
            return $pages_xml_str;
        }
 
        //TODO: "showing results (offset)-(offset+limit)
 
        $page_var = $data['prefix'] . 'page';
        $results_var = $data['prefix'] . 'results';
        $page = 0;
        $display_start = $data['current_page'] - ceil($data['display_pages']/2);
        if ($display_start < 0)
        {
            $display_start = 0;
        }
        $display_end = $data['current_page'] + ceil($data['display_pages']/2);
        if ($display_end > $data['page_count'])
        {
            $display_end = $data['page_count'];
        }
 
        if ($data['current_page'] > 1)
        {
            $previous = $data['current_page'] - 1;
            if ($previous != 1)
            {
                $pages_xml_str .= "<page class=\"first_page\" number=\"1\" url=\"?{$page_var}=1" . $this->get_query_string() . "\"><![CDATA[" . 'first' . "]]></page>\n";
            }
            $pages_xml_str .= "<page class=\"previous_page\" number=\"{$previous}\" url=\"?{$page_var}={$previous}" . $this->get_query_string() . "\"><![CDATA[" . 'previous' . "]]></page>\n";
        }
 
 
        while ($page++ < $display_end)
        {
            if ($page < $display_start)
            {
                continue;
            }
            
            if ($page == $data['current_page'])
            {
                $pages_xml_str .= "<page class=\"current_page\" number=\"{$page}\" url=\"\">{$page}</page>\n";
                continue;
            }
            
            $pages_xml_str .= "<page class=\"select_page\" number=\"{$page}\" url=\"?{$page_var}={$page}" . $this->get_query_string() . "\">{$page}</page>\n";
        }
 
        if ($data['current_page'] < $data['page_count'])
        {
            $next = $data['current_page'] + 1;
            $pages_xml_str .= "<page class=\"next_page\" number=\"{$next}\" url=\"?{$page_var}={$next}" . $this->get_query_string() . "\"><![CDATA[" . 'next' . "]]></page>\n";
 
            if ($next != $data['page_count'])
            {
                $pages_xml_str .= "<page class=\"last_page\" number=\"{$data['page_count']}\" url=\"?{$page_var}={$data['page_count']}" . $this->get_query_string() . "\"><![CDATA[" . 'last' . "]]></page>\n";
            }
        }
        
        $pages_xml_str .= "</pages>\n";
        
        return $pages_xml_str;
    }
    
    /**
* Displays page selector as list
*/
    public function get_pages_as_list($acl_checks=false)
    {
        $link_suffix = $this->get_parameter_string();
 
        $this->data['prefix'] = $this->prefix;
        $this->data['current_page'] = $this->current_page;
        $this->data['page_count'] = $this->count_pages($acl_checks);
        $this->data['results_per_page'] = $this->limit;
        $this->data['offset'] = $this->offset;
        $this->data['display_pages'] = $this->display_pages;
        //Won't work (wrong scope), so the code is copied below.
        //midcom_show_style('show-pages');
        $data =& $this->data;
        
        $pages = '';
 
        //Skip the header in case we only have one page
        if ($data['page_count'] <= 1)
        {
            return;
        }
 
        //TODO: "showing results (offset)-(offset+limit)
        //TODO: Localizations
        $page_var = $data['prefix'] . 'page';
        $results_var = $data['prefix'] . 'results';
        $pages .= '<div class="org_openpsa_qbpager_pages">';
        $pages .= "\n <ul>\n";
        $page = 0;
        $display_start = $data['current_page'] - ceil($data['display_pages']/2);
        if ($display_start < 0)
        {
            $display_start = 0;
        }
        $display_end = $data['current_page'] + ceil($data['display_pages']/2);
        if ($display_end > $data['page_count'])
        {
            $display_end = $data['page_count'];
        }
 
        if ($data['current_page'] > 1)
        {
            $previous = $data['current_page'] - 1;
            if ($previous != 1)
            {
                //$pages .= "\n<li class=\"first\"><a class=\"first_page\" href=\"?{$page_var}=1\">&nbsp;</a></li>";
                //$pages .= "\n<li class=\"separator\"></li>";
            }
 
            $pages .= "\n<li class=\"prev\" onclick=\"window.location='?{$page_var}={$previous}{$link_suffix}';\"></li>";
 
            if ($display_start > 1)
            {
                $pages .= "\n<li class=\"separator\"></li>";
                $pages .= "\n<li class=\"page last\" onclick=\"window.location='?{$page_var}=1{$link_suffix}';\">1</li>";
                $pages .= "\n<li class=\"separator\"></li>";
                $pages .= "\n<li class=\"page splitter\">...</li>";
                $pages .= "\n<li class=\"separator\"></li>";
            }
        }
 
 
        while ($page++ < $display_end)
        {
            if ($page < $display_start)
            {
                continue;
            }
            if ($page == $data['current_page'])
            {
                $pages .= "\n<li class=\"page active\">{$page}</li>";
                $pages .= "\n<li class=\"separator\"></li>";
                continue;
            }
            if ($page < $data['page_count'])
            {
                $pages .= "\n<li class=\"page\" onclick=\"window.location='?{$page_var}={$page}{$link_suffix}';\">{$page}</li>";
                $pages .= "\n<li class=\"separator\"></li>";
            }
        }
 
        if ($data['current_page'] < $data['page_count'])
        {
            $next = $data['current_page'] + 1;
 
            if ($next != $data['page_count'])
            {
                //$pages .= "\n<li class=\"separator\"></li>";
                //$pages .= "\n<li class=\"last\"><a class=\"last_page\" href=\"?{$page_var}={$data['page_count']}\">&nbsp;</a></li>";
                $pages .= "\n<li class=\"page splitter\">...</li>";
                $pages .= "\n<li class=\"separator\"></li>";
                $pages .= "\n<li class=\"page last\" onclick=\"window.location='?{$page_var}={$data['page_count']}{$link_suffix}';\">{$data['page_count']}</li>";
            }
 
 
            $pages .= "\n<li class=\"next\" onclick=\"window.location='?{$page_var}={$next}{$link_suffix}';\"></li>";
        }
 
        $pages .= "\n </ul>\n";
        $pages .= "</div>\n";
 
        return $pages;
    }
    
    /**
* Fetch all $_MIDCOM->dispatcher->get variables, but leave out the page number
*
*/
    private function get_query_string()
    {
        $query_string = '';
        foreach (explode('&', $_SERVER['QUERY_STRING']) as $key)
        {
            if( !preg_match('/org_openpsa_qbpager/', $key)
               && $key != '')
            {
                $query_string .= '&amp;'.$key;
            }
        }
        return $query_string;
    }
    
    /**
* sets LIMIT and OFFSET for requested page
*/
    private function set_qb_limits(&$qb)
    {
        $this->check_request_variables();
        $qb->set_limit($this->limit);
        $qb->set_offset($this->offset);
        return;
    }
 
    private function clear_qb_limits(&$qb)
    {
        $limit = abs(pow(2, 31) - 1); //Largest signed integer we can use as limit.
        $offset = 0;
        $qb->set_limit($limit);
        $qb->set_offset($offset);
        return;
    }
 
    public function execute()
    {
        if (!$this->check_sanity())
        {
            return false;
        }
 
        $this->set_qb_limits($this->qb);
        return $this->qb->execute();
    }
 
    /**
* Returns number of total pages for query
*
* By default returns a number of pages without any ACL checks, checked
* count is available but is much slower.
*/
    public function count_pages($acl_checks=false)
    {
        if (!$this->check_sanity())
        {
            return false;
        }
        
        $this->count = $this->count();
        
        if (!$this->results_per_page)
        {
            $this->results_per_page = 4;
        }
        
        return ceil($this->count / $this->results_per_page);
    }
    
    // Rest of supported methods wrapped with extra sanity check
    public function add_constraint($param, $op, $val)
    {
        if (!$this->check_sanity())
        {
            return false;
        }
        $this->qb_count->add_constraint($param, $op, $val);
        return $this->qb->add_constraint($param, $op, $val);
    }
 
    public function add_order($param, $sort='ASC')
    {
        if (!$this->check_sanity())
        {
            return false;
        }
        return $this->qb->add_order($param, $sort);
    }
 
    public function begin_group($type)
    {
        if (!$this->check_sanity())
        {
            return false;
        }
        $this->qb_count->begin_group($type);
        return $this->qb->begin_group($type);
    }
 
    public function end_group()
    {
        if (!$this->check_sanity())
        {
            return false;
        }
        $this->qb_count->end_group();
        return $this->qb->end_group();
    }
 
    public function include_deleted()
    {
        $this->qb_count->include_deleted();
        return $this->qb->include_deleted();
    }
 
    public function set_lang($lang)
    {
        if (!$this->check_sanity())
        {
            return false;
        }
        $this->qb_count->set_lang($lang);
        return $this->qb->set_lang($lang);
    }
 
    public function count()
    {
        if (!$this->check_sanity())
        {
            return false;
        }
        if ( !$this->count
            || $this->count_mode != 'count')
        {
            $this->count = $this->qb_count->count();
        }
        $this->count_mode = 'count';
        return $this->count;
    }
}
?>