-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
executable file
·65 lines (51 loc) · 1.93 KB
/
index.php
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
<?php
// This plugin will alphabetise a given page array or tag array
Kirby::plugin('splorp/alphabetise', []);
function alphabetise($items, $options = array())
{
// Default key and orderby values
// To sort letters listed first, set 'orderby' to SORT_NUMERIC
// To sort numbers listed first, set 'orderby' to SORT_REGULAR or SORT_STRING
$defaults = array('key' => 'title', 'orderby' => SORT_REGULAR);
// Merge default and options arrays
$options = array_merge($defaults, $options);
// Set the initial case check variable
$case = '';
// Put the input into a two dimensional array, using '~' as separator
foreach ($items as $item) {
$temp = explode('~', $item->{$options['key']}());
// Check whether two sequential items share the same spelling
// for the value located in the 'key' field (eg: Bob vs BOB)
// Prefix the array index so the two values are not merged
if (strtolower($temp[0]) == $case) {
$temp = substr($temp[0], 0, 1) . $temp[0];
} else {
$temp = $temp[0];
}
$temp = strtolower($temp);
$case = $temp;
$array[$temp][] = $item;
}
// Check to see array is not empty
if ((!empty($array))) {
//Make an array of key and data
foreach ($array as $temp => $item) {
if (strlen($temp) < 2) {
$temp = $temp . '_';
$array[substr($temp, 0, 2)][] = $item[0];
} else {
$array[substr($temp, 0, 1)][] = $item[0];
}
unset($array[$temp]);
}
// Sort the $array using 'orderby' flag
ksort($array, $options['orderby']);
} else {
// If there’s a problem, set $array to an error message
$array = array(
"Alphabetise Plugin Error: Problem with array or invalid key!
Make sure your array is valid, not empty & that the key is valid for this type of array. (You can probably ignore the errors after this point, until this error has been resolved.)" => "Error"
);
}
return $array;
}