Code
<?php
require("Cogent.php");
/**
* PubsTable
* A class for generating HTML tables of publication PDFs by scanning directories
* containing folders of PDF files. For each directory found, PubsTable
* will add an album (top-level div) with a heading of that name.
*
* Outline:
* PicsPane("<directory name>")
* PicsPane->run(); // generates HTML output for all images found, organized by album
*
* Author: Jonathan Scholz
* 2/3/2010
*/
class PubsTable extends Cogent
{
/**
* Like the list_content() function in the base class, except fancier. Instead
* of just returning the filtered list of files found, this function creates a
* dictionary of fields for information from the filename, and returns an array
* containing both the raw filename and the dictionary of parsed information as a
* tuple (array)
* Both arrays within this tuple are sorted by the date field of the parsed filenames
*/
public function list_and_parse($dir) {
// The delimiters for parsing filenames
$tokens = "()";
$hndl = opendir($dir);
if ($hndl == false) {
print "Could not find directory $dir";
exit;
} else {
while (false !== ($item = readdir($hndl))) {
// Get file extension
$ext = substr(strrchr($item, '.'), 1);
// Match against filter
if (Cogent::in_arrayi($ext, $this->filters)) {
// Extract tokens (hard coded tokens for now)
$items_toks[] = array("author" => strtok($item, $tokens),
"date" => strtok($tokens),
"title" => strtok($tokens),
"journal" => strtok($tokens));
$items[] = $item;
}
}
closedir($hndl); // close directory
// Now sort both arrays by the "date" key
if (isset($items) && count($items)!=0) {
foreach ($items_toks as $key => $row) {
// Create an array of dates, in the order they appear originally
$dates[$key] = $row['date'];
}
// Sort the full names (items) and the tokenized array by ascending $dates elements
array_multisort($dates,SORT_DESC,$items_toks,$items);
return array($items, $items_toks);
}
}
return;
}
/**
* Generates HTML for a table of publications organized by category
*/
protected function generate($dir, $files){
print '<table class="pubs">'."\n\n";
// Fetch PDFs & parse
$all_PDF_info = $this->list_and_parse($this->rootdir."/".$dir);
// Write out the section heading
print '<tr><td class="pubs_section">'.$dir.'</td>';
print '<td style="text-align: center;">download</td></tr>';
if (count($all_PDF_info[0])==0) {
print '<tr><td class="pubs_entry_disc">Working on it...</td>';
print '<td class="pubs_entry_link"></td></tr>'."\n\n"; // print second cell
}
else {
for ($paper=0; $paper < count($all_PDF_info[0]); $paper++) {
// write the table row and start the text column:
print '<tr><td class="pubs_entry_disc">';
// write the authors:
print $all_PDF_info[1][$paper]['author'];
// write the date:
print "(".$all_PDF_info[1][$paper]['date'].")";
//write the title (in bold):
print "<strong>".$all_PDF_info[1][$paper]['title']."</strong>";
//write the journal (in parens):
print "<i>".$all_PDF_info[1][$paper]['journal']."</i>";
//close td:
print '</td>'."\n";
// Write the link to the actual file, which is the 1st element of the main info array
print '<td class="pubs_entry_link"><a href="'.
$this->rootdir."/".$dir."/".$all_PDF_info[0][$paper].'" '. // set target
'onClick="_gaq.push([\'_trackEvent\', \'Publication\', \'Downloaded\', \''.$all_PDF_info[0][$paper].'\']);">'. // install tracker
'<img src="resources/icons/pdf-icon.jpg" /></a></td></tr>'."\n\n";
}
}
print '</table>'."\n";
}
} // End: class PubsTable
?> About me