home pageabout moiagroupour servicesour workhow we workcontact usstuff we like

Sorting in TinyMCE ImageManager

A client was already using TinyMCE throughout his custom application and asked if it would be possible to allow him to arbitrarily insert images throughout his site in any rich text box. The last thing I wanted to do was write my own media handler, so using the TinyMCE ImageManager component seemed perfect — you just create the authentication code, and away you go. However, within a day or so we realized that the plugin displays the thumbnails in the default sort provided by the system. So you could upload a file called “zzzhello.jpg” and have to flip through multiple screens of thumbanils — after just uploading it! — before you could find it to place it into your text box. Kind of dumb. So I wrote a quick and dirty fix to make the system show files most recent first instead:

in tiny_mce/plugins/imagemanager/classes/FileSystems/LocalFileImpl.php

look for

function &listFilesFiltered(&$filter) {

then, right after this line:

sort($fileArray);

add this:

$sortedFiles = array();
foreach($fileArray as $file) {
     $key = filemtime($dir . "/" . $file);
     while(isset($sortedFiles[$key])) $key++;
     $sortedFiles[$key] = $file;
}
ksort($sortedFiles);
$fileArray = array_reverse(array_values($sortedFiles));

I know it’s not the most elegant solution, but it works! Or did I miss something? Is there any easier way to do this?

Leave a Comment