Code

Added exporters
authorcajus <cajus@594d385d-05f5-0310-b6e9-bd551577e9d8>
Tue, 25 Aug 2009 11:51:16 +0000 (11:51 +0000)
committercajus <cajus@594d385d-05f5-0310-b6e9-bd551577e9d8>
Tue, 25 Aug 2009 11:51:16 +0000 (11:51 +0000)
git-svn-id: https://oss.gonicus.de/repositories/gosa/trunk@14126 594d385d-05f5-0310-b6e9-bd551577e9d8

gosa-core/include/class_listing.inc
gosa-core/include/exporter/class_cvsExporter.inc
gosa-core/include/exporter/class_pdfExporter.inc [new file with mode: 0644]
gosa-core/include/php_setup.inc

index 89f383f668555e5be929cc5901d9f9962ac0f4c7..9d9835b2b38bb74e34e181017466108b345115e3 100644 (file)
@@ -49,7 +49,6 @@ class listing {
   var $snapshotHandler= null;
   var $exporter= array();
   var $exportColumns= array();
-  var $showExporter= false;
 
 
   function listing($filename)
@@ -84,7 +83,9 @@ class listing {
     foreach($class_mapping as $class => $dummy) {
       if (preg_match('/Exporter$/', $class)) {
         $info= call_user_func(array($class, "getInfo"));
-        $this->exporter= array_merge($this->exporter, $info);
+        if ($info != null) {
+          $this->exporter= array_merge($this->exporter, $info);
+        }
       }
     }
   }
@@ -386,12 +387,6 @@ class listing {
 
     $result.= "</table></div>";
 
-    // Open export window?
-    if ($this->showExporter) {
-      $result.= "<SCRIPT TYPE='text/javascript'>window.open('getbin.php', '"._("GOsa - export list")."');</SCRIPT>";
-      $this->showExporter= false;
-    }
-
     $smarty= get_smarty();
     $smarty->assign("FILTER", $this->filter->render());
     $smarty->assign("SIZELIMIT", print_sizelimit_warning());
@@ -448,16 +443,14 @@ class listing {
     }
 
     // Filter POST with "act" attributes -> posted from action menu
-    if (isset($_POST['act']) && $_POST['act'] != '') {
+    if (isset($_POST['exec_act']) && $_POST['act'] != '') {
       if (preg_match('/^export.*$/', $_POST['act']) && isset($this->exporter[$_POST['act']])) {
         $exporter= $this->exporter[$_POST['act']];
-        $instance= new $exporter['class']($this->plainHeader, $this->entries, $this->exportColumns);
+        $userinfo= ", "._("created by")." ".$ui->cn." - ".strftime('%A, %d. %B %Y, %H:%M:%S');
+        $instance= new $exporter['class']($this->headline.$userinfo, $this->plainHeader, $this->entries, $this->exportColumns);
         $type= call_user_func(array($exporter['class'], "getInfo"));
         $type= $type[$_POST['act']];
-        session::set('binarytype', $type['mime']);
-        session::set('binaryfile', $type['filename']);
-        session::set('binary', $instance->query());
-        $this->showExporter= true;
+        send_binary_content($instance->query(), $type['filename'], $type= $type['mime']);
       }
     }
 
@@ -976,7 +969,7 @@ class listing {
 
     // Load shortcut
     $actions= &$this->xmlData['actionmenu']['action'];
-    $result= "<input type='hidden' name='act' id='actionmenu' value=''>".
+    $result= "<input type='hidden' name='act' id='actionmenu' value=''><div style='display:none'><input type='submit' name='exec_act' id='exec_act' value=''></div>".
              "<ul class='level1' id='root'><li><a href='#'>Aktionen&nbsp;<img ".
              "border=0 class='center' src='images/lists/sort-down.png'></a>";
 
@@ -1217,13 +1210,13 @@ class listing {
 
       // Copy entry
       if($copy){
-        $result.= "<li$separator><a href='#' onClick='document.getElementById(\"actionmenu\").value= \"copy\";mainform.submit();'><img src='images/lists/copy.png' alt='' border='0' class='center'>&nbsp;"._("Copy")."</a></li>";
+        $result.= "<li$separator><a href='#' onClick='document.getElementById(\"actionmenu\").value= \"copy\";document.getElementById(\"exec_act\").click();'><img src='images/lists/copy.png' alt='' border='0' class='center'>&nbsp;"._("Copy")."</a></li>";
         $separator= "";
       }
 
       // Cut entry
       if($cut){
-        $result.= "<li$separator><a href='#' onClick='document.getElementById(\"actionmenu\").value= \"cut\";mainform.submit();'><img src='images/lists/cut.png' alt='' border='0' class='center'>&nbsp;"._("Cut")."</a></li>";
+        $result.= "<li$separator><a href='#' onClick='document.getElementById(\"actionmenu\").value= \"cut\";document.getElementById(\"exec_act\").click();'><img src='images/lists/cut.png' alt='' border='0' class='center'>&nbsp;"._("Cut")."</a></li>";
         $separator= "";
       }
     }
@@ -1231,7 +1224,7 @@ class listing {
     // Draw entries that allow pasting entries
     if($paste){
       if($this->copyPasteHandler->entries_queued()){
-        $result.= "<li$separator><a href='#' onClick='document.getElementById(\"actionmenu\").value= \"paste\";mainform.submit();'><img src='images/lists/paste.png' alt='' border='0' class='center'>&nbsp;"._("Paste")."</a></li>";
+        $result.= "<li$separator><a href='#' onClick='document.getElementById(\"actionmenu\").value= \"paste\";document.getElementById(\"exec_act\").click();'><img src='images/lists/paste.png' alt='' border='0' class='center'>&nbsp;"._("Paste")."</a></li>";
       }else{
         $result.= "<li$separator><a href='#'><img src='images/lists/paste-grey.png' alt='' border='0' class='center'>&nbsp;"._("Paste")."</a></li>";
       }
@@ -1299,7 +1292,7 @@ class listing {
 
       // Draw icons according to the restore flag
       if($restore){
-        $result.= "<li$separator><a href='#' onClick='document.getElementById(\"actionmenu\").value= \"restore\";mainform.submit();'><img src='images/lists/restore.png' alt='' border='0' class='center'>&nbsp;"._("Restore snapshots")."</a></li>";
+        $result.= "<li$separator><a href='#' onClick='document.getElementById(\"actionmenu\").value= \"restore\";document.getElementById(\"exec_act\").click();'><img src='images/lists/restore.png' alt='' border='0' class='center'>&nbsp;"._("Restore snapshots")."</a></li>";
       }else{
         $result.= "<li$separator><a href='#'><img src='images/lists/restore_grey.png' alt='' border='0' class='center'>&nbsp;"._("Restore snapshots")."</a></li>";
       }
@@ -1319,7 +1312,7 @@ class listing {
 
     // Export CVS as build in exporter
     foreach ($this->exporter as $action => $exporter) {
-      $result.= "<li><a href='#' onClick='document.getElementById(\"actionmenu\").value= \"$action\";mainform.submit();'><img border='0' class='center' src='".$exporter['image']."'>&nbsp;".$exporter['label']."</a></li>";
+      $result.= "<li><a href='#' onClick='document.getElementById(\"actionmenu\").value= \"$action\";document.getElementById(\"exec_act\").click();'><img border='0' class='center' src='".$exporter['image']."'>&nbsp;".$exporter['label']."</a></li>";
     }
 
     // Finalize list
@@ -1380,7 +1373,7 @@ class listing {
       $events= DaemonEvent::get_event_types_by_category($this->categories);
       if(isset($events['BY_CLASS']) && count($events['BY_CLASS'])){
         foreach($events['BY_CLASS'] as $name => $event){
-          $result.= "<li$separator><a href='#' onClick='document.getElementById(\"actionmenu\").value=\"$name\";mainform.submit();'>".$event['MenuImage']."&nbsp;".$event['s_Menu_Name']."</a></li>";
+          $result.= "<li$separator><a href='#' onClick='document.getElementById(\"actionmenu\").value=\"$name\";document.getElementById(\"exec_act\").click();'>".$event['MenuImage']."&nbsp;".$event['s_Menu_Name']."</a></li>";
           $separator= "";
         }
       }
index 110014f8baeb689d4ea2c576c3f43ca235605c5f..5c7b07657cea62f4959b93fb6e07d22cea50ef7f 100644 (file)
@@ -4,7 +4,7 @@ class csvExporter
 {
   var $result;
 
-  function csvExporter($header, $entries, $columns= array()) {
+  function csvExporter($headline, $header, $entries, $columns= array()) {
     // If no preset, render all columns
     if (!count($columns)) {
       foreach ($header as $index => $dummy) {
@@ -16,7 +16,7 @@ class csvExporter
     $this->result= "#";
     foreach ($columns as $index) {
       if (isset($header[$index])){
-        $this->result.= $header[$index].";";
+        $this->result.= trim($header[$index]).";";
       } else {
         $this->result.= ";";
       }
@@ -27,7 +27,7 @@ class csvExporter
     foreach ($entries as $row) {
       foreach ($columns as $index) {
         if (isset($row["_sort$index"])){
-          $this->result.= $row["_sort$index"].";";
+          $this->result.= trim($row["_sort$index"]).";";
         } else {
           $this->result.= ";";
         }
@@ -45,7 +45,7 @@ class csvExporter
 
   static function getInfo()
   {
-    return array("exportCVS" => array( "label" => _("CSV"), "image" => "plugins/lists/exportCSV.png", "class"=> "csvExporter", "mime" => "text/x-csv", "filename" => "export.csv" ));
+    return array("exportCVS" => array( "label" => _("CSV"), "image" => "images/lists/csv.png", "class"=> "csvExporter", "mime" => "text/x-csv", "filename" => "export.csv" ));
   }
 
 }
diff --git a/gosa-core/include/exporter/class_pdfExporter.inc b/gosa-core/include/exporter/class_pdfExporter.inc
new file mode 100644 (file)
index 0000000..28012a0
--- /dev/null
@@ -0,0 +1,129 @@
+<?php
+
+// Try to load PDF library
+define('FPDF_FONTPATH', '/usr/share/php/fpdf/font/');
+include('fpdf/fpdf.php');
+
+class pdfExporter
+{
+  var $result;
+
+  function pdfExporter($headline, $header, $entries, $columns= array()) {
+    // If no preset, render all columns
+    if (!count($columns)) {
+      foreach ($header as $index => $dummy) {
+        $columns[]= $index;
+      }
+    }
+
+    // Create new PDF
+    $this->result=new FPDF('L', 'mm', 'A4');
+    $this->result->SetFont('Helvetica', '', 10);
+    $this->result->AddPage();
+
+    // Analyze for width
+    $width= $this->calcWidth($header, $entries, $columns);
+    
+    // Render head
+    $this->result->SetFont('','B');
+    $this->result->Cell(0,0,utf8_decode($headline),0,0,'L');
+    $this->result->Ln(5);
+
+    // Generate header
+    $this->result->SetFillColor(240,240,240);
+    $this->result->SetTextColor(0);
+    $this->result->SetDrawColor(0,0,0);
+    $this->result->SetLineWidth(.3);
+    $this->result->SetFont('','B');
+
+    foreach ($columns as $order => $index) {
+      if (isset($header[$index])){
+        $this->result->Cell($width[$order], 7, utf8_decode($header[$index]), 1, 0, 'C', 1);
+      } else {
+        $this->result->Cell($width[$order], 7, '', 1, 0, 'C', 1);
+      }
+    }
+    $this->result->Ln();
+
+    // Append entries
+    $this->result->SetFillColor(224,235,255);
+    $this->result->SetTextColor(0);
+    $this->result->SetFont('');
+
+    $fill= false;
+    foreach ($entries as $row) {
+      foreach ($columns as $order => $index) {
+
+        if (isset($row["_sort$index"])){
+          $this->result->Cell($width[$order], 6, utf8_decode($row["_sort$index"]), 'LR', 0, 'L', $fill);
+        } else {
+          $this->result->Cell($width[$order], 6, '', 'LR', 0, 'L', $fill);
+        }
+      }
+
+      $this->result->Ln();
+      $fill= !$fill;
+    }
+    $this->result->Cell(array_sum($width), 0, '', 'T');
+  }
+
+
+  function calcWidth($header, $entries, $columns)
+  {
+    $width= array();
+
+    // Locate longest value for each column
+    foreach ($columns as $order => $index) {
+      $max= 0;
+
+      if (isset($header[$index])){
+        $len= $this->result->GetStringWidth($header[$index]);
+        if ($len > $max) {
+          $max= $len;
+        }
+      }
+
+      foreach ($entries as $row) {
+        if (isset($row["_sort$index"])){
+          $len= $this->result->GetStringWidth($row["_sort$index"]);
+          if ($len > $max) {
+            $max= $len;
+          }
+        }
+      }
+
+      $width[]= $max;
+    }
+
+    // Scale to page width
+    $printWidth= 280;
+    $scale= $printWidth / array_sum($width);
+    foreach ($width as $index => $w) {
+      $width[$index]= (int)($width[$index] * $scale);
+    }
+
+    return $width;
+  }
+
+
+  function query()
+  {
+     return $this->result->Output("", "S");
+  }
+
+
+  static function getInfo()
+  {
+    // Check if class defined
+    $classes= get_declared_classes();
+    if(in_array('FPDF', $classes)) {
+      return array("exportPDF" => array( "label" => _("PDF"), "image" => "images/lists/pdf.png", "class"=> "pdfExporter", "mime" => "application/pdf", "filename" => "export.pdf" ));
+    } else {
+      return null;
+    }
+
+  }
+
+}
+
+?>
index 6566da07f23bf6ca10880b25945d1c2a9a827fdd..2732905613aba9c873865be5fe039bda5cee9543 100644 (file)
@@ -274,7 +274,7 @@ ini_set("register_globals",0);
 ini_set("track_vars",1);
 ini_set("display_errors",1);
 ini_set("report_memleaks",1);
-ini_set("include_path",".:$BASE_DIR/include:$BASE_DIR/include/utils/excel");
+ini_set("include_path",".:$BASE_DIR/include:$BASE_DIR/include/utils/excel:/usr/share/php");
 
 /* Do smarty setup */
 require("smarty/Smarty.class.php");