We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 26482
    • 138 Posts
    Hi,

    I was thinking about showing the latest 10 documents (pdfs) uploaded by my users to 25+ sub directories under /assets/files (also showing the 10 latest documents that have been edited/created and the 10 latest pictures uploaded to the site). My initial thought was that FileDownload (that I use extensively to show these files on their respective pages) should be able to list these files, but I haven’t found a way that works. Also, Ditto would be perfect, but only works on documents, not files.

    Have I missed something in the forums/resources that would make this possible?

    Thanks.
      • 20400
      • 37 Posts
      It depends on how much work you want to do. A PHP programmer should be able to write you a custom snippet in an hour or two - three if she has a coffee break.

      Or, you should be able to do it yourself using Ditto.

      I haven’t tried this (I am not a big Ditto user.) but I think it should work. First create a container in Modx for Ditto to search. When you upload a file - go into Modx an create a link to the file in the container you just created. Ditto should be able to sort them by date. You can even add something into the summary to be displayed on the page.
        • 26482
        • 138 Posts
        Hi,

        I solved this using the following snippet:

        <?php
        include $modx->config['base_path']."assets/snippets/preg_find/preg_find.php";
        ( isset($rss) ) ? $rss : $rss = 0;
        
        if ($rss==0) {
        
        $files = preg_find('/./', 'assets/files',
          PREG_FIND_RECURSIVE|PREG_FIND_RETURNASSOC|PREG_FIND_SORTMODIFIED|PREG_FIND_SORTDESC);
        
        $i=1;
        echo "<table>";
        echo"<tr><td><b>Datum</b></td><td><b>Plaats</b></td><td><b>Bestandsnaam</b></td></tr>";
        foreach($files as $file => $stats) {
        $path_parts = pathinfo($file);
        $var  = explode('/', $file); 
          
        $groep ="/leerlingen/".$var[2];
        
         printf('<tr><td>%d) %s</td><td>%s</td><td><a href="%s">%s.%s</a></td></tr>', $i,
            date('d-m-Y', $stats['stat']['mtime']),$var[2],  $file, $path_parts['filename'],$path_parts['extension']);
          $i++;
          if ($i >10) break;
        }
        echo "</table>";
        }
        else {
        echo '<?xml version="1.0" encoding="'.$modx->config['modx_charset'].'" ?>';
        echo '<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">';
        echo '</rss>';
        
        }
        ?>



        This uses code from http://www.pgregg.com/forums/viewtopic.php?tid=73 (the include ...preg_find.php). The line
        $files = preg_find('/./', 'assets/files',
          PREG_FIND_RECURSIVE|PREG_FIND_RETURNASSOC|PREG_FIND_SORTMODIFIED|PREG_FIND_SORTDESC);
        
        returns an array with all files, sorted on modified date, exactly what I needed!

        As you can see, I’m in the midst of adding an xml feed to this snippet, so the my visitors can subscribe to this list. Not finished yet, as I have zero time to dedicate to this...
        • The feed part of this is what makes it slightly complex... other than that, you could do a simple @DIRECTORY binding to get pretty close to your goal (of listing items in a directory): http://wiki.modxcms.com/index.php/%40DIRECTORY_Binding, but an @EVAL statement or a Snippet triggering a simple bash command would do the trick, e.g.
          Snippet
          <?php
          $a = shell_exec('ls --sort=time');
          echo $a;
          ?>


          @EVAL
          @EVAL $a = shell_exec('ls --sort=time'); echo $a;


          See http://unix-simple.blogspot.com/2008/05/selecting-most-recent-file-in-unix.html