Skip to content

Instantly share code, notes, and snippets.

@zelon88
Created July 4, 2016 00:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zelon88/baec16f5ca2e60cfa34153ceff5623e2 to your computer and use it in GitHub Desktop.
Save zelon88/baec16f5ca2e60cfa34153ceff5623e2 to your computer and use it in GitHub Desktop.
Kirby File Sorting by Date
<?php
//Save to site/plugins/file-sorting-by-date.php
kirby()->hook('panel.file.upload', 'uploaddatesort');
kirby()->hook('panel.file.replace', 'sortfiles');
function setdate($file) {
//Reference Date Options:
//"today": Current date.
//"modified": Time of last file modification. If uploaded via Panel, this will be the upload date.
//"taken": If a photo, then when it was taken. Falls back on "modified" behavior otherwise.
$referencedate = "taken";
switch ($referencedate) {
case "today":
$filedate = time('Y-m-d');
break;
case "modified":
$filedate = $file->modified('Y-m-d');
break;
case "taken":
if ($file->type() == "image"){
$filedate = date('Y-m-d',$file->exif()->timestamp());
} else {
$filedate = $file->modified('Y-m-d');
}
break;
}
$file->update(array(
'date' => $filedate
));
}
function uploaddatesort ($file) {
setdate($file);
sortfiles($file);
}
function sortfiles($file) {
//Order Options:
//'asc': ascending, oldest to newest
//'desc': descending, newest to oldest
$order = 'desc';
foreach ($file->files() as $f) {
if ($f->date() == "") {
setdate($f);
}
}
$i = 1;
foreach ($file->files()->sortBy('date', $order) as $f) {
$f->update(array(
'sort' => $i
));
$i++;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment