From 4e0809ab86e4d809c2522782ca94a7fe7539896b Mon Sep 17 00:00:00 2001
From: Leif Andersson <leif.andersson@control.lth.se>
Date: Sun, 20 Sep 2015 18:45:38 +0200
Subject: [PATCH] Lade till utskrift av grupper

---
 action.add_edit_Group.php | 11 ++++++++++
 action.printGroup.php     | 42 +++++++++++++++++++++++++++++++++++++++
 lib/class.Group.php       | 11 ----------
 lib/class.Person.php      | 22 ++++++++++++++++++++
 4 files changed, 75 insertions(+), 11 deletions(-)
 create mode 100644 action.printGroup.php

diff --git a/action.add_edit_Group.php b/action.add_edit_Group.php
index 0821c6b..eb42f0c 100644
--- a/action.add_edit_Group.php
+++ b/action.add_edit_Group.php
@@ -2,6 +2,17 @@
 if (!isset($gCms)) exit;
 //echo '<pre>'; print_r($params); echo '</pre>';
 
+$ID='';
+$paramKeys = array_keys($params);
+$print = 'print_';
+foreach ($paramKeys as $key) {
+  $pos = strpos($key,$print);
+  if ($pos === false) continue;
+  $ID = substr($key,strlen($print));
+  $this->Redirect($id, 'printGroup', $returnid, array('ID'=>$ID));
+  return;
+}
+
 if (! $this->CheckPermission('Use ReglerAddress')) {
   return $this->DisplayErrorPage($id, $params, $returnid,
 				 'ReglerAddress access denied.');
diff --git a/action.printGroup.php b/action.printGroup.php
new file mode 100644
index 0000000..ac2faa3
--- /dev/null
+++ b/action.printGroup.php
@@ -0,0 +1,42 @@
+<?php
+if (!isset($gCms)) exit;
+//echo '<pre>'; print_r($params); echo '</pre>';
+
+$ID = $params['ID'];
+
+$HEAD = <<<EOS
+<!doctype html>
+<html lang="en" dir="ltr">
+<head>
+<meta charset="utf-8" />
+<title>Grupp $ID</title>
+<meta http-equiv="Content-Type" content="text/plain"/>
+</head>
+EOS;
+
+
+$members = Person::getGroupMembers($ID);
+$addresses = array();
+foreach ($members as $member) {
+  $adr = '';
+  if (trim($member->Namn)) $adr .= $member->Namn.PHP_EOL;
+  
+  if ($member->SkolaForetag) $adr .= $member->SkolaForetag.PHP_EOL;
+  if ($member->Avdelning) $adr .= $member->Avdelning.PHP_EOL;
+  if ($member->Adress) $adr .= $member->Adress.PHP_EOL;
+  $addresses[] = $adr;
+}
+  
+  
+
+
+
+echo $HEAD;
+echo '<body>'.PHP_EOL;
+echo '<pre>';
+foreach ($addresses as $adr) echo $adr.PHP_EOL;
+ echo '</pre>';
+echo '</body></html>';
+
+exit;
+?>
diff --git a/lib/class.Group.php b/lib/class.Group.php
index 9aec894..874b04b 100644
--- a/lib/class.Group.php
+++ b/lib/class.Group.php
@@ -2,9 +2,6 @@
 
 class Group {
 
-  private static $maildir = '/home/leif/slask/maildir';
-
-
   private static $queryList = <<<EOS
     select ID, Gruppnamn, GruppInfo from Grupper order by Gruppnamn
 EOS;
@@ -165,14 +162,6 @@ EOS;
     if ($result === false) {die("Database error!<br>" . $db->ErrorMsg()); }
   }  
 
-  /* public static function getGroup() { */
-  /*   if (!isset(self::$instance)) { */
-  /*     $c = __CLASS__; */
-  /*     self::$instance = new $c; */
-  /*   } */
-  /*   return self::$instance->list; */
-  /* } */
-
   public static function setMailLists() {
     global $gCms; if (!isset($gCms)) exit;
     $config =& $gCms->GetConfig();
diff --git a/lib/class.Person.php b/lib/class.Person.php
index bed50b0..e72b908 100644
--- a/lib/class.Person.php
+++ b/lib/class.Person.php
@@ -18,6 +18,11 @@ EOT;
     Grupper.ID=GruppID and AdressID = ?
 EOT;
 
+  private static $queryGroupMembers = <<<EOS
+    select distinct *, concat(Fornamn, ' ', Efternamn) as Namn
+    from Adresser,GruppAdress where Adresser.ID=AdressID and GruppID=?
+EOS;
+  
   public static $firstFields = array(
     'ID','fornamn', 'efternamn', 'foretag','avdelning', 'adress', 'Grupp');
 
@@ -167,5 +172,22 @@ EOT;
     }
     return $records;
   }
+  public static function getGroupMembers($groupID) {
+    global $gCms; if (!isset($gCms)) exit;
+    $db = AddressDB::getDB();
+    $class = __CLASS__;
+    $records = array();
+    $values = array($groupID);
+    $result = $db->Execute(self::$queryGroupMembers,$values);
+    if (!$result) { die("Database error!<br>" . $db->ErrorMsg()); }
+    while ($row = $result->FetchRow()) {
+      $person = new $class();
+      foreach ($row as $key => $value) {
+	$person->$key = $value;
+      }
+      $records[] = $person;
+    }
+    return $records;
+  }
 }
 ?>
\ No newline at end of file
-- 
GitLab