There’s probably an easier way but here’s what I did:
I wanted users to be able to upload newsletters and to have a page of links to those newsletters.
Here’s the snippet on the upload page (you have set up the document groups so that only members of the group can upload. If you want to allow anyone to upload, just leave out the &uploadgroups parameter).
[[!FileUpload? &tpl=`MyNewsletterChunk`&path=`/assets/newsletters/`&uploadgroups=`wugNewsAdmins`&extensions=`doc,xls,pdf`!]
Here’s the MyNewsletter Chunk:
</p>
<form enctype="multipart/form-data" action="[~[*id*]~]" method="post">
<p>
<input type="hidden" name="formid" value="[+fileupload.formid+]" />
<!-- MAX_FILE_SIZE must precede the file input field -->
<input type="hidden" name="MAX_FILE_SIZE" value="[+fileupload.maxsize+]" />
Select file 1:<input name="userfile[]" type="file" /><br />
Select file 2:<input name="userfile[]" type="file" /><br />
<input type="submit" value="Upload file" />
</p>
</form>
<p>
To automaticall display the links, I wrote a snippet that reads the directory with the files, and outputs them as links. This code is probably more complicated than you need because it sorts them by filename. The filename has a particular format and contains the year and issue number so I had to sort them by issue number within each year.
The Show Newsletter page just contains this snippet which includes the shownewsletters code:
// Path where ShowNewsletters is installed
define(SHOW_NEWSLETTERS_PATH, "assets/snippets/shownewsletters/");
//
//include snippet file
include($modx->config['base_path'].SHOW_NEWSLETTERS_PATH.'shownewsletters.php');
Note that the code below could simply replace the code in the snippet above. Having the php code in a separate file just makes it easier to edit and debug. Looking it over, I see that it could definitely use some informative else statements that report errors.
This is what’s in shownewsletters.php:
<?php
// Shows all files in the newsletter directory,
// descending by year,
// assending by issue number within each year.
// Filenames must be in format: yyyy-nn-MonthName
// e.g. 2007-02-February
$newline = "<br/>";
$dir = $modx->config['base_path'].'assets/newsletters'; // set path to newsletter files
$dir_array = array(); // main array - contains all file names in directory
$temp_array = array(); // temp array used to hold one year's file names
// open directory and parse file list
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
// iterate over file list to create full directory array
while (($filename = readdir($dh)) !== false) {
if (($filename != ".") && ($filename != "..") && ($filename != "WS_FTP.LOG")) { // skip self, parent, and ftp log
$dir_array[] = $filename; // add the filename to the array
}
}
closedir($dh); // close directory
}
natsort($dir_array); // sort in ascending order by year and issue number
$dir_array = array_reverse($dir_array, false); // reverse array (descending by year)
$year = ""; // used for comparison to find new year
$i = 0;
$n = count($dir_array); // total number of files
for($i=0;$i<$n;$i++) { // can't use foreach() here -- need to manipulate index
$file_year = substr($dir_array[$i],0,4); // year is first four characters of file name
if ($file_year != $year) { // new year
echo "<h3> ",$file_year,"</h3>";
$j = $i; // set j (temporary index) to beginning of the new year
$temp_array=array(); // empty $temp_array
$year = $file_year; // set $year to current year
while($year == substr($dir_array[$j],0,4)) { // build temp array for one year
if ($j > $n) { // don't go beyond end of full array
break;
}
//$value = $dir_array[$j];
//$temp_array[] = $value;
$temp_array[] = $dir_array[$j]; // add one issue to temp_array
$j++;
}
$temp_array = array_reverse($temp_array, false); // reverse array to show ascending months
foreach($temp_array as $temp_value) { // create one year's links on screen
$link = $modx->config['base_url'].'assets/newsletters/'.$temp_value;
echo ' <a href ="';
echo $link;
echo '">';
echo $temp_value;
echo '</a>';
echo $newline;
}
$j--; // decrement j because we went one too far
$i = $j; // set main index to beginning of next year
$year = $file_year; // set $file year for start of new year
}
}
}
?>
Hope this helps,
Bob