Created
July 4, 2016 00:41
-
-
Save zelon88/baec16f5ca2e60cfa34153ceff5623e2 to your computer and use it in GitHub Desktop.
Kirby File Sorting by Date
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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