/*
auth: tneirync
date: 04/15/07
name: FormatClass
purp: format all kinds of data-structures into valid html code (such as publications, create tables, etc..)
(a little like Lib class, but only with formatting functions, and only to create html output)
*/
class FormatClass{
//constructorc
function FormatClass() {
//nothing much to construct here..
$this->Lib = new LibClass();
}
//builds hyperlink (you can pass $data and $link as two strings, or as two arrays.
//if you pass as arrays, elements with same index correspond to $data - $link pair)
function lnk($data, $link){
if (is_array($data)){
for ($i = 0; $i < count($data); $i++){
if ($link[$i]) {
$hprlnk[] = ''.$data[$i].'';
}else{
$hprlnk[] = $data[$i];
}
}
}else{
if ($link){
$hprlnk = ''.$data.'';
}else{
$hprlnk = ''.$data.'';
#$hprlnk = $data;
}
}
return $hprlnk;
}
############################################################################################
function subtitle($title){
//var_dump($title);exit;
$content = '
'.$title.'
';
return $content;
}
############################################################################################
function anchor($title){
$content = ''."\n";
return $content;
}
############################################################################################
function box_in($html){
$content = '
';
$content .= $html;
$content .= " |
\n";
return $content;
}
############################################################################################
//create list//$each element in data is row//you can send valid html list tags. however, you can just specify with breaks
function lst($data,$listtype){
if ($listtype != "br"){
$list = '<'.$listtype.">\n";
if(!empty($data)){
foreach ($data as $element){
$list .= "".$element."\n";
}
}
$list .= ''.$listtype.">\n";
}else {
@$list = implode("
",$data);
}
return $list;
}
############################################################################################
//create a thumbnail with specified width and links
function thmb($img, $subdir,$h, $w){
$file = $_SESSION[config][serv_media_img].$subdir."/".$img;
if (isset($img) && file_exists($file)){
$thumbFile = $_SESSION[config][media_loc]."images/".$subdir."/".$img;
}else{
$thumbFile = $_SESSION[config][noimage];
}
$thumb .= '';
return $thumb;
}
############################################################################################
function thmb_small($img, $subdir,$h, $w){
$file = $_SESSION[config][serv_media_img].$subdir."/".$img;
if (isset($img) && file_exists($file)){
$thumbFile = $_SESSION[config][media_loc]."images/".$subdir."/".$img;
}else{
$thumbFile = $_SESSION[config][noimage];
}
$thumb = '';
#$thumb = '';
$thumb .= '';
$thumb .= '';
#$thumb .= '';
$thumb .= '';
return $thumb;
}
############################################################################################
//create table (send columns-array (each array is column), each element of column is rowelement) length of each column should be the same)
//width array specifies widths
function tbl($columns,$widths){
$content .= '';
for($i=0;$i'.$columns[$j][$i]."";
}
$content .="\n";
}
$content .= "
";
return $content;
}
############################################################################################
// Format a publication citation
function cttn(
$id, $type, $authorsRaw, $editorsRaw, $date, $titleRaw, $publicationVenue,
$venueVolume, $venueNumber, $venuePages, $publisher, $venueChapter, $fileNameURL) {
// Authors
$authorsRaw = $this->removePeriodAfterFinalName($authorsRaw);
$authors = $this->authorseditors($this->splitlink($authorsRaw));
// Editors
$editorsRaw = $this->removePeriodAfterFinalName($editorsRaw);
// Remove any existing commas after each name in $editorsRaw for consistency with $authors.
$editorsRaw = str_replace(",###", "###", $editorsRaw);
$editors = $this->authorseditors($this->splitlink($editorsRaw));
// Editors with "ed" marker
if ($editorsRaw) {
if (strpos($editors, " and ") === FALSE) {
$edMarker = ", ed";
}
else {
$edMarker = ", eds";
}
}
$editorsWithMarker = $editors . $edMarker;
// Title
$title = $this->formatTitle($titleRaw, $fileNameURL);
// Venue and associated numbers
$venueNumbers = $this->formatVenueNumbers($venueVolume, $venueNumber, $venuePages);
$venueAndNumbers = join(" ", array_filter(array(
$this->italicized($publicationVenue),
$venueNumbers)));
// Put together citation
// $citationParts = array("citation", "Parts");
switch($type) {
case "Journal Articles":
$citationParts = array(
$authors,
$date,
$this->quoted($title),
$venueAndNumbers);
break;
case "Edited Journals":
$citationParts = array(
$editorsWithMarker,
$date,
$this->quoted($title),
$venueAndNumbers);
break;
case "Books":
$citationParts = array(
$authors,
$date,
$this->italicized($title),
$publicationVenue,
$publisher);
break;
case "Book Chapters":
$italicizedPublicationVenue = $this->italicized($publicationVenue);
$citationParts = array(
$authors,
$date,
$this->quoted($title),
$this->smartJoin(", ", array(
$publicationVenue ? "In $italicizedPublicationVenue" : "",
$this->nonEmpty($editors) ? "edited by $editors" : "",
$venueChapter ? "Ch. $venueChapter" : "",
$venuePages
)),
$publisher);
break;
case "Edited Books":
$citationParts = array(
$editorsWithMarker,
$date,
$this->italicized($title),
$publicationVenue,
$publisher);
break;
case "Ph.D. Theses":
$citationParts = array(
$authors,
$date,
$this->quoted($title),
$publicationVenue,
$publisher);
break;
default: // "Conference Proceedings", "Unrefereed", "Non-reviewed", "Video - Audio", "Technical Reports", "Patents", other
$citationParts = array(
$authors,
$date,
$this->quoted($title),
$venueAndNumbers);
}
return $this->smartJoin(". ", $citationParts) . "." . "\n";
}
function formatVenueNumbers($volume, $number, $pages) {
// return "formatVenueNumbers";
$venueNumbers = "";
$venueNumbers .= $volume ? $volume : "";
$venueNumbers .= ($volume and $number) ? " " : "";
$venueNumbers .= $number ? "($number)" : "";
$venueNumbers .= (($volume or $number) and $pages) ? ": " : "";
$venueNumbers .= $pages ? $pages : "";
return $venueNumbers;
}
function nonEmpty($s) {
// return TRUE;
return trim(strip_tags($s)) !== "";
}
function formatTitle($titleRaw, $fileNameURL) {
// return "formatTitle";
if ($fileNameURL) {
$url = parse_url($fileNameURL);
$file = $_SESSION[config][serv_media].$_SESSION[config][pub_loc].$fileNameURL;
if (file_exists($file)) {
$link = $_SESSION[config][media_loc].$_SESSION[config][pub_loc].$fileNameURL;
} elseif ($url['scheme'] == 'http') {
$link = $fileNameURL;
}
}
return $this->lnk($titleRaw, $link);
}
// Does "." occur before the final "###"? If so, remove it.
function removePeriodAfterFinalName($authors) {
// return "removePeriodAfterFinalName";
$SEP = "###";
$PERIOD = ".";
$finalSepPos = strrpos($authors, $SEP);
if ($finalSepPos !== FALSE
and $finalSepPos >= 1
and $authors[$finalSepPos - 1] === $PERIOD) {
return $this->replaceLastOccurrence($PERIOD . $SEP, $SEP, $authors);
} else {
return $authors;
}
}
function endsWith($haystack, $needle) {
// return FALSE;
$length = strlen($needle);
$start = $length * -1;
return (substr($haystack, $start) === $needle);
}
function replaceLastOccurrence($search, $replace, $subject) {
// return "replaceLastOccurrence";
$pos = strrpos($subject, $search);
if($pos === FALSE) {
return $subject;
} else {
return substr_replace($subject, $replace, $pos, strlen($search));
}
}
function smartJoin($glue, $pieces) {
$glueTrimmed = trim($glue);
// $piecesFixed = $pieces;
$piecesFixed = array();
foreach ($pieces as $piece) {
if ($this->endsWith($piece, $glueTrimmed)) {
$piecesFixed[] = $this->replaceLastOccurrence($glueTrimmed, "", $piece);
} else {
$piecesFixed[] = $piece;
}
}
return join($glue, array_filter($piecesFixed,
create_function('$s', 'return trim(strip_tags($s)) !== "";'))); //, "nonEmpty"));
}
function quoted($s) {
// return "quoted";
return $s ? "\"$s\"" : "";
}
function italicized($s) {
// return "italicized";
return $s ? "$s" : "";
}
############################################################################################
// Format an array of authors or editors as a comma-separated string.
function authorseditors($people) {
if (count($people) > 1) {
$finalIndex = count($people) - 1;
$people[$finalIndex] = 'and ' . $people[$finalIndex];
}
return join(", ", $people);
// for ($i = 0; $i < count($ae); $i++){
// if ($i && $i != (count($ae) - 1)) {
// $string .= ", ";
// }
// if (($i == (count($ae) - 1)) && $i != 0) {
// $string .= " & ";
// }
// $string .= $ae[$i];
// }
// return $string;
}
############################################################################################
//split by @@@, then split by ### and link together;(could actually by in library class, ... maybe..)
function splitlink($itemslinks){
$items = explode("@@@",$itemslinks);
foreach ($items as $item){
list($thing, $link) = explode("###", $item);
$things[] = $this->lnk(trim($thing), $link);
}
return $things;
}
############################################################################################
//same as above, but for research projects
function splitlinkresearch($itemslinks){
$items = explode("@@@",$itemslinks);
foreach ($items as $item){
list($thing, $link) = explode("###", $item);
$things[] = $this->lnk($thing, "../research/#" .$link);
}
return $things;
}
############################################################################################
//formats a grant
function fmtFundingGrant($grant){
$row = "".$grant["grantTitle"].". " . $grant["grantName"] ." ";
$row .= "(".$this->authorseditors($this->splitlink($grant['copisGrants'])).", ";
#print "=".$grant["amount"]."
";
#remove blanks
$amount = preg_replace('/^\s+|\s+$|,/','',$grant["amount"]);
$amount = number_format($amount);
# $format = "%s%s";
# $amount = sprintf($format,'$ ',htmlspecialchars($amount));
if (!empty($grant["amount"])) {
$row .= "^".$amount .") ";
}
$row .= $grant["startDate"] ." - " . $grant["endDate"].".";
$row = preg_replace('/(\d{4})-(\d{1,2})-(\d{1,2})/','\1.\2.\3',$row);
// if (!empty($grant['researchGrants']))
// $row .= " Research(es): ". $this->authorseditors($this->splitlinkresearch($grant['researchGrants']));
return $row;
}
############################################################################################
//formats a presentation
function ppt($pres){
$t = "".$pres['presentationtitle']."";
$ai = $pres['additionalInfo'];
$d = $pres['exactdate'];
$e = $pres['eventstitle'];
$eu = $pres['eventurl'];
$p = $this->authorseditors($this->splitlink($pres['presenters']));
$c = $pres['city'];
if ($pres['StateProvince'] && !($pres['Country']))
{
$s = ", ". $pres['StateProvince'];
}
elseif($pres['Country'])
{
$s = ", ". $pres['Country'];
}
$f = $pres['fileNameURL'];
$mt = $pres['mediatype'];
//format date
$sd = $this->splitPostgresDate($d);
$d = $this->toDayMonth($sd[d],$sd[m]);
//create link
if ($f) {
switch ($mt) {
case '10': //local path
$file = $_SESSION[config][serv_media].$_SESSION[config][pres_loc].$f;
if (file_exists($file)) {
$link = $_SESSION[config][media_loc].$_SESSION[config][pres_loc].$f;
}
break;
case '01'://full URL
//check if url exists
#if ($this->Lib->url_exists($f)) {
$link = $f;
#}
break;
}
}
$t = $this->lnk($t,$link);
if (preg_match('/@@@/',$pres['presenters']) && isset($pres['presenters'])){
$pres = " present ";
}elseif(isset($pres['presenters'])){
$pres = " presents ";
}else{
$pres = "";
}
//format presentation
$row = $d .": ".$p . $pres . $t;
if ($ai) $row .= ", " . $ai ;
if ($e) $row .= " at " ;
if ($eu){
$ev = $this->lnk($e,$eu);
}else{
$ev = $e;
}
$row .= $ev;
if ($c) $row .= ", ". $c . $s;
$row .= ".";
return $row;
}
############################################################################################
function dateToSemester($date){
list($day,$month,$year) = $date;
if ($month >= 1 && $month <= 5){
$semester = "Spring";
}elseif($month >= 6 && $month <= 7){
$semester = "Summer";
}elseif($month >= 8 && $month <= 12 ){
$semester = "Fall";
}
$semester .= " ".$year;
return $semester;
}
############################################################################################
function splitPostgresDate($timestamp){
list( $date['y'], $date['m'], $date['d'])= explode("-",$timestamp);
return $date;
}
############################################################################################
function toDayMonth($day,$month){
$months['01'] = 'Jan';
$months['02'] = 'Feb';
$months['03'] = 'Mar';
$months['04'] = 'Apr';
$months['05'] = 'May';
$months['06'] = 'Jun';
$months['07']= 'Jul';
$months['08'] = 'Aug';
$months['09'] = 'Sep';
$months['10'] = 'Oct';
$months['11'] = 'Nov';
$months['12'] = 'Dec';
$date = "".$months[$month]." " .$day."";
return $date;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
?>