Provide a downloadable calendar for this year
authorJoey Schulze <joey@infodrom.org>
Thu, 7 Aug 2008 14:24:18 +0000 (14:24 +0000)
committerJoey Schulze <joey@infodrom.org>
Thu, 7 Aug 2008 14:24:18 +0000 (14:24 +0000)
Styles/calendar_pdf.style [new file with mode: 0644]
src/Linux/calendar.html.wml

diff --git a/Styles/calendar_pdf.style b/Styles/calendar_pdf.style
new file mode 100644 (file)
index 0000000..fab7c1f
--- /dev/null
@@ -0,0 +1,182 @@
+<?
+  # PDF generation
+
+  class CalPDF extends FPDF
+  {
+    var $year;
+    var $colors;
+
+    function CalPDF($year,$colors=true,$orientation='P',$unit='mm',$format='A4')
+    {
+      //Call parent constructor
+      $this->FPDF($orientation,$unit,$format);
+      //Initialization
+      $this->year = $year;
+      $this->colors = $colors;
+    }
+
+
+    function Header()
+    {
+      $this->Image(LOGO, 8.5, 4, 17, 0, LOGO_TYPE, LOGO_LINK);
+      $this->SetFont('Arial','',10);
+      $this->SetY(5.5);
+      $this->Cell(0, 6, 'Infodrom Oldenburg', 0, 0, 'R');
+
+      $this->SetFont('Arial','B',14);
+      $this->SetY(20);
+      $this->Cell(0,0,'Free Software Events Calendar ' . $this->year, 0, 2, 'C');
+    }
+
+    function Footer()
+    {
+      //Position at 1.5 cm from bottom
+      $this->SetY(-15);
+      //Arial italic 8
+      $this->SetFont('Arial','',8);
+      //Page number
+      $this->Cell(0,10,$this->PageNo().'/{nb}',0,'T','C');
+    }
+
+    function RGBtoInt($rgb)
+    {
+      if (substr($rgb, 0, 1) == '#')
+       $rgb = substr($rgb, 1, 6);
+
+      return array(hexdec(substr($rgb, 0, 2)),
+                  hexdec(substr($rgb, 2, 2)),
+                  hexdec(substr($rgb, 4, 2)));
+    }
+
+    function SetFillCol($rgb)
+    {
+      if ($this->colors === false)
+       return;
+
+      error_log('SetFillColor('.$rgb.')');
+      $val = $this->RGBtoInt($rgb);
+      $this->SetFillColor($val[0], $val[1], $val[2]);
+    }
+
+    function SetTextCol($rgb)
+    {
+      if ($this->colors === false)
+       return;
+
+      error_log('SetTextColor('.$rgb.')');
+      $val = $this->RGBtoInt($rgb);
+      $this->SetTextColor($val[0], $val[1], $val[2]);
+    }
+
+    function Ending()
+    {
+      $this->Ln();
+      $this->SetFont('Arial','',10);
+      $this->SetTextCol('000000');
+      $this->Write(4.5, 'Infodrom Oldenburg');
+      $this->Ln();
+      $this->SetTextCol('0000ff');
+      $this->Write(4.5, 'http://www.infodrom.org/', 'http://www.infodrom.org/');
+      $this->Ln();
+      $this->SetTextCol('000000');
+      $this->Write(4.5, 'Free Software Calendar');
+      $this->Ln();
+      $this->SetTextCol('0000ff');
+      $this->Write(4.5, 'http://www.infodrom.org/Linux/calendar.html', 'http://www.infodrom.org/Linux/calendar.html');
+    }
+  }
+
+  function gather_events($year)
+  {
+    global $dbh;
+
+    $result = array();
+    $dbh = pg_pconnect ("", "", "web")
+      or die("Unable to connect to SQL server");
+    pg_exec ($dbh, "SET DateStyle = 'ISO'") or die("Datenbank-Abfrage!");
+
+    $event_cols = "name,start,start+delta AS end,city,country,conference,exhibition,url";
+    $query = sprintf("SELECT %s FROM events WHERE cancelled=0 " .
+                    "AND start >= '%04d-01-01' and start < '%04d-01-01' " .
+                    "ORDER BY start,name",
+                    $event_cols, $year, $year+1);
+
+    $sth = pg_exec ($dbh, $query) or die("Datenbank-Abfrage!");
+
+    for ($i=0; $i < pg_NumRows ($sth); $i++) {
+      $row = pg_fetch_array ($sth, $i);
+      $date = explode (" ", $row['start']);
+      $date = explode ("-", $date[0]);
+      $end = explode (" ", $row[2]);
+      $end = explode ("-", $end[0]);
+      $date = str_replace('&nbsp;', ' ', format_date ($date, $end));
+
+      $event = array($date, $row['name'], $row['city'], $row['country'],
+                    $row['conference']==1?"x":"",
+                    $row['exhibition']==1?"x":"");
+      $result[] = $event;
+    }
+
+    return $result;
+  }
+
+  function caltable_head($pdf, $columns)
+  {
+    $pdf->SetY(30);
+    $pdf->SetX(10);
+    $pdf->SetFont('Arial','B',10);
+    $pdf->SetFillCol('1AA2FE');
+    $pdf->SetTextCol('ffffff');
+    foreach ($columns as $col) {
+      $pdf->Cell($col['width'], 6, $col['name'], 1, 0, 'C', true);
+    }
+    $pdf->Ln();
+  }
+
+  function caltable_data($pdf, $columns, $rows, $chunk)
+  {
+    $pdf->SetFont('Arial','',8);
+    $pdf->SetFillCol('d7d7d7');
+    $pdf->SetTextCol('000000');
+
+    $fill = false;
+    $nr = 0;
+    $offset = $chunk * EVENTS_PER_PAGE;
+    $maxnr = min(count($rows), $offset + EVENTS_PER_PAGE);
+    for ($nr=$offset; $nr < $maxnr; $nr++) {
+      $row = $rows[$nr];
+      for ($i=0; $i < count($columns); $i++) {
+       $frame = 'LR';
+       if ($nr == $maxnr-1) $frame .= 'B';
+       $pdf->Cell($columns[$i]['width'], 4.5, $row[$i], $frame, 0, $columns[$i]['justify'], $fill);
+      }
+      $pdf->Ln();
+      $fill=!$fill;
+    }
+  }
+
+  function calendar_pdf($year)
+  {
+    require('fpdf/fpdf.php');
+
+    $pdf = new CalPDF($year, true);
+    $pdf->AliasNbPages();
+    $pdf->SetTitle('Free Software Events Calendar');
+    $pdf->SetAuthor('Joey Schulze <joey@infodrom.org>');
+    $pdf->SetCreator('Infodrom Oldenburg <http://www.infodrom.org/>');
+    $pdf->SetFillColor(255,255,255);
+    # $pdf->SetAutoPageBreak(true, 10);
+
+    $rows = gather_events($year);
+
+    for ($chunk=0; $chunk < ceil(count($rows)/EVENTS_PER_PAGE); $chunk++) {
+      $pdf->AddPage();
+
+      caltable_head($pdf, $columns);
+      caltable_data($pdf, $columns, $rows, $chunk);
+    }
+
+    $pdf->Ending();
+    $pdf->Output();
+  }
+?>
index 4ac42ff..a14c0d2 100644 (file)
@@ -1,7 +1,16 @@
 #include <infodrom.style>
 #include <calendar.style>
+#include <calendar_pdf.style>
 #include <linux.style>
 #include <addresses.inc>
+<?
+  $dbh = event_connect ();
+
+  if (isset($_GET['download']) && $_GET['download'] == 'pdf') {
+    calendar_pdf(date("Y"));
+    exit;
+  }
+?>
 
 <page title="GNU/Linux Events -- Upcoming Events Calendar" alternates="calendar.ics calendar.rdf|Free~Software~Events">
 
@@ -19,8 +28,6 @@ mail to <joey mailto>.
 <p>[ Conf = Conference &ndash; Exhib = Exhibition ]</p>
 
 <?
-  $dbh = event_connect ();
-
   $query = "SELECT $event_cols FROM events WHERE cancelled = 0 "
          ."AND (start <= 'now') AND (start + delta + '23:59' >= 'now') ORDER BY start,name";