Attach List Sort
Summary: Sort an attachlist
Version: 1.1
Prerequisites: none
Status: Working
Maintainer: JB
Categories: Uploads
Downloads: Attach:AttachListSort.php
Questions answered by this recipe
- Is there a way to sort the attachlist?
Description
This recipe allows you to sort the attachlist via name and/or date/time and/or size and/or file type.
Syntax is easy:
(:attachlist sort=name:) (:attachlist sort=type:) (:attachlist sort=date:) (:attachlist sort=size:) (:attachlist sort=time:) - sorting by time same as sorting by date (:attachlist sort=ext:) - same as "type" (:attachlist sort=ascendingname:) - ascending is default direction (:attachlist sort=ascendingtype:) (:attachlist sort=ascendingdate:) (:attachlist sort=ascendingsize:) (:attachlist sort=ascendingtime:) (:attachlist sort=ascendingext:) (:attachlist sort=descendingname:) (:attachlist sort=descendingtype:) (:attachlist sort=descendingdate:) (:attachlist sort=descendingsize:) (:attachlist sort=descendingtime:) (:attachlist sort=descendingext:) (:attachlist sort=date ext=jpg:) sort just the jpg files
Multi-Sort
The left most parameter value has precedence when using sort.
(:attachlist sort="date size":) - Sort list by dates which are sorted by size (:attachlist sort="type,size":) - Sort list by types which are sorted by size (:attachlist sort="name type date size":) - default sort
Case-insensitive name-sorting
By default, the sort method by name IS SENSITIVE to case. That means all filenames where the first letter is capitalized will come before all filenames with a lowercase first letter. To have the files be sorted alphabetically irrelevant of case, go into the given AttachListSort.php and replace all instances of $namelist with array_map(‘strtolower’, $namelist) .
The filename’s capitalization will still be shown in the attachment list - it just won’t be used for sorting purposes if you make this change.
Installing
Copy Attach:AttachListSort.php
to your cookbook directory
In your config.php enter:
or in your farmconfig.php enter:
$FarmD/cookbook/AttachListSort.php”);
Notes
Release Notes
- Version: 10 06 2006 7:02 PM CST - Initial release
- Version: 10 07 2006 7:00 AM CST - fix bugs, improve sort, add ascending & descending
- Version: 10 09 2006 4:53 PM CST - Add ability to sort based on file type
- Version: 10 10 2006 2:34 AM CST - Add Multi-Sort, Include seconds in file information
- Version: 10 20 2006 2:57 AM CST - fix “$D = SORT_DESC;” bug & fix tabs in code
- Version: 1.1 - Add recipeinfo
Comments
- Does this work? I installed as stated above and get “Argument #2 is expected to be an array or a sort flag in c:\program files\apache group\Apache\htdocs\PMwiki\cookbook\AttachListSort.php on line 170
Warning: Cannot modify header information - headers already sent by (output started at c:\program files\apache group\Apache\htdocs\PMwiki\cookbook\AttachListSort.php:170) in c:\program files\apache group\Apache\htdocs\PMwiki\pmwiki.php on line 884″ error message.
- Change line 102 in “AttachListSort.php” from “$D = SORT_DEC;” to “$D = SORT_DESC;” and it works.
– Subhash Nov. 30, 2006Fantastic catch there subhash! It is fixed in the newest version JB
There seems to be another problem there. No matter what parameters I give to “sort” I always get sort by name. I think that the problem is that the sorting result is not used at all. When using the array_multisort:
array_multisort($sortlist[$sortkey[0]], $sorthow[$sortkey[0]],
$sortlist[$sortkey[1]], $sorthow[$sortkey[1]],
$sortlist[$sortkey[2]], $sorthow[$sortkey[2]],
$sortlist[$sortkey[3]], $sorthow[$sortkey[3]]);
$filelist = $namelist;
As you can see the result can not be in $namelist, since it is not sent to array_multisort. So the script just does nothing, you will always get sort by name (since $sortlist[‘name’]=$namelist ), unless you send to $filelist some part of $sortlist…
I am not a PHP expert (I am not sure I can even be considered as a beginner…) so I might be wrong. but whet I try the following change I get the right result
$filelist = $sortlist[$sortkey[0]];
please check…
- Using “$filelist = $namelist;” has always worked for me. I originally programmed it in PHP version 5 which makes array assignments by reference. We moved to a new server and now I use PHP version 4.39 and it still works great with “$filelist = $namelist;”. Can you please tell me what version of PHP you are using? JB
Please correct the code as indicated above because it did not work at all with PHP 5.2.0 (3 may 2007).
- I also have PHP 5.2.0+ and had to go into AttachListSort.php and change that line to ‘$filelist = $sortlist[$sortkey[0]]’ to get it to work. I’ve uploaded my changed version to be the current one so others don’t have to go to the same confusion & trouble. That said, JB, this is some great code, and thanks for it. —ez
BTW I wonder no one has ever thought of that. This cookbook can be easily tweaked to produce a table of attachments, which can be easily sorted on-the-fly using sorttable.js which can be embedded in pmwiki.(or the great firefox extention TableTools which can be used to sort/filter on HTML tables)
And another idea - this could be combined with - adding an icon to attachments based on file types, as was written recently in the “AttachIcon” idea in the mailing list.
I have tried it on my private pmwiki installation. took 5 minutes to update attachlistsort for both…
- Avi Dec. 05, 2006 (add some more on 19-Dec-2006)
- Please do make these recipes for everyone using PMWiki to use!!! JB
When I listed files with non-ASCII characters in their names I had strange effects. To get proper encoding I’ve made some changes to your recipe as follows:
1. add $Charset to globals in attachlistsort function,
2. change $namelist[$file] = $file; to $namelist[$file] = iconv(‘CP1250′, $Charset, $file);
I’m not a PHP guru so feel free to correct me if I’m wrong.
—errorsys, 2007–10–24
I’m using version 1.1 ; when I use AttachListSort on an empty list of attachment, I get an error (implode function illegal argument).
I simply suggest to return an empty string when the file list is empty, e.g. by adding the following after line 101 :
if (count($namelist) == 0) { return “”; }
I’m not a PHP guru so feel free to correct me if I’m wrong :).
—jce, 2008–05–27
See Also
Contributors