1<?php
2/**
3 * Plugin nspages : Displays nicely a list of the pages of a namespace
4 *
5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 */
7
8if(!defined('DOKU_INC')) die();
9
10abstract class nspages_sorter {
11    protected $reverse;
12
13    function __construct($reverse){
14        $this->reverse = $reverse;
15    }
16
17    function sort(&$array){
18        $this->actualSort($array);
19        if ($this->reverse) {
20          $array = array_reverse($array);
21        }
22    }
23
24    protected function actualSort(&$array){
25        usort($array, array($this, 'comparator'));
26    }
27
28    abstract function comparator($item1, $item2);
29}
30
31class nspages_default_sorter extends nspages_sorter {
32    function __construct($reverse){
33        parent::__construct($reverse);
34    }
35
36    function comparator($item1, $item2){
37        return strcasecmp($item1['sort'], $item2['sort']);
38    }
39}
40
41class nspages_naturalOrder_sorter extends nspages_sorter {
42    function __construct($reverse){
43        parent::__construct($reverse);
44    }
45
46    function comparator($item1, $item2){
47        return strnatcasecmp($item1['sort'], $item2['sort']);
48    }
49}
50
51class nspages_dictOrder_sorter extends nspages_sorter {
52    private $dictOrder;
53
54    function __construct($reverse, $dictOrder){
55        parent::__construct($reverse);
56        $this->dictOrder = $dictOrder;
57    }
58
59    function actualSort(&$array){
60        $oldLocale=setlocale(LC_ALL, 0);
61        setlocale(LC_COLLATE, $this->dictOrder);
62        usort($array, array($this, "comparator"));
63        setlocale(LC_COLLATE, $oldLocale);
64    }
65
66    function comparator($item1, $item2){
67        return strcoll($item1['sort'], $item2['sort']);
68    }
69}
70
71