Skip to content

Commit a0de444

Browse files
author
Russell Baldwin
committed
Added 'orderby' key for alternative sorting
The default is letter and then number, setting the ‘orderby’ key to SORT_STRING gives you number then letters.
1 parent 5282a87 commit a0de444

File tree

2 files changed

+49
-22
lines changed

2 files changed

+49
-22
lines changed

alphabetise.php

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
<?php
2-
// The Alphabetise plugin for Kirby CMS will Alphabetise a given page array or tag array
2+
// The Alphabetise plugin for Kirby CMS will Alphabetise a given page array or tag array
33
// and return it for further processing/display
44
// The 'key' text field must be longer than a single character.
55
// See readme for further information
6-
// Recent Updates: Fix for single alphabetical index bug #3 initial bug fix problem
6+
// Recent Updates: Added key for ksort sort_flags - set orderby to SORT_STRING for numbers & then letters
7+
// Default it is set to SORT_REGLAUR for letters and then numbers
8+
// 'orderby' key is NOT a string!
79
// -----
8-
// Russ Baldwin
10+
// Russ Baldwin
911
// shoesforindustry.net
10-
// v0.0.8
12+
// v0.0.9
1113
// -----
1214
function alphabetise($parent, $options=array()) {
13-
15+
1416
// default key values
15-
$defaults = array('key'=> 'title');
16-
17+
// As we are using ksort the default 'orderby' is SORT_REGULAR
18+
// To sort with number first you can use 'orderby' set to SORT_STRING
19+
// Other ksort sort_flags may be usuable but not tested!
20+
$defaults = array('key'=> 'title','orderby'=> SORT_REGULAR);
21+
1722
// merge defaults and options
18-
$options = array_merge($defaults, $options);
23+
$options = array_merge($defaults, $options);
1924

2025
//Gets the input into a two dimensional array - uses '~' as separator;
2126
foreach ($parent as $item){
@@ -26,7 +31,7 @@ function alphabetise($parent, $options=array()) {
2631
}
2732

2833
// Check to see array is not empty
29-
if ( (!empty ($array)) ) {
34+
if ( (!empty ($array)) ) {
3035
//Make an array of key and data
3136
foreach ($array as $temp => $item){
3237
if (strlen($temp)<2){
@@ -35,21 +40,21 @@ function alphabetise($parent, $options=array()) {
3540
} else {
3641
$array[substr($temp, 0, 1)][]=$item[0];
3742
}
38-
unset ($array[$temp]);
43+
unset ($array[$temp]);
3944
}
4045

4146
// If all OK $array will be returned and sorted
42-
ksort($array);
43-
47+
ksort($array,$options['orderby']);
48+
4449
} else {
45-
50+
4651
// There has been a problem so set $array with error message and then return $array
4752
$array = array(
4853
"Alphabetise Plugin Error: Problem with array or invalid key!
4954
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"
5055
);
5156
}
52-
57+
5358
return $array;
5459
}
5560
?>

readme.mdown

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
## What is it?
44

5-
The Alphabetise plugin will alphabetise a given [Kirby CMS](http://getkirby.com/) *page* array or *tag* array and return it for further processing/display as an alphabetised array.
5+
The Alphabetise plugin will alphabetise a given [Kirby CMS](http://getkirby.com/) *page* array or *tag* array and return it for further processing/display as an alphabetised array.
66

7-
*The array whose key your are trying to sort on should of course only contain letters of the alphabet, if not problems may occur.*
7+
*The array whose key your are trying to sort on should of course only contain letters of the alphabet, if not problems may occur.*
88

99
*Also the code (explode) uses a '~' tilde, if you use this in your key, especially at the beginning of the string, then you could run into sorting problems. You could of course manually change it if required.*
1010

11-
## Installation
11+
## Installation
1212

1313
Put all the files into your **site/plugins/alphabetise** folderor use the [Kirby CLI](https://github.com/getkirby/cli): In your project folder, from the command line, enter:
1414
```kirby plugin:install shoesforindustry/kirby-plugins-alphabetise```
@@ -34,7 +34,7 @@ In your template call, it like this:
3434
```php
3535
<?php $alphabetise = alphabetise($page->children()->visible()->sortby('title'), array('key' => 'title')); ?>
3636
```
37-
The first argument you pass is the sorted **page** array you want to *alphabetise*. The second array **key** argument is so you can set what you want to *alphabetise* by. It should be a string like a page 'title'. The 'sortby' and the 'key' should usually be the same.
37+
The first argument you pass is the sorted **page** array you want to *alphabetise*. The second array **key** argument is so you can set what you want to *alphabetise* by. It should be a string like a page 'title'. The 'sortby' and the 'key' should usually be the same.
3838

3939
You then want to loop through the returned results and display them for example:
4040
```php
@@ -47,13 +47,13 @@ You then want to loop through the returned results and display them for example:
4747
<?php echo $item->title()?>
4848
</a>
4949
</li>
50-
<?php endforeach ?>
50+
<?php endforeach ?>
5151
</ul>
5252
<hr/>
5353
<?php endforeach ?>
5454
```
5555

56-
####Example2: Alphabetical list of tags using tag name as the key
56+
####Example2: Alphabetical list of tags using tag name as the key
5757

5858
#####A
5959
+ Aa tag
@@ -102,7 +102,29 @@ You can use any valid array element, so for tags you can use also add **$item->r
102102
</li>
103103
```
104104

105-
105+
####Example3: Set 'orderby' key
106+
107+
Version 0.0.9 adds a key to alter how the array appears, the default is with letters and then numbers e.g.
108+
109+
+ A
110+
+ B
111+
+ 1
112+
+ 2
113+
114+
Or you can not set the 'orderby' key to SORT_STRING, to have the numbers listed first e.g.
115+
116+
+ 1
117+
+ 2
118+
+ A
119+
+ B
120+
121+
```php
122+
<?php $alphabetise = alphabetise($page->children()->visible()->sortby('title'), array('key' => 'title', 'orderby'=>SORT_STRING))); ?>
123+
124+
```
125+
126+
**Note**: *We are using 'ksort' so other 'sort_flags' might be possible but not tested! The 'orderby' key is not a string!*
127+
106128
## Author
107129
Russ Baldwin
108-
[shoesforindustry.net](shoesforindustry.net)
130+
[shoesforindustry.net](shoesforindustry.net)

0 commit comments

Comments
 (0)