From d1d5e3dec4f55b3a7a8782919efe6c6156f1b928 Mon Sep 17 00:00:00 2001
From: Leif Andersson <leif.andersson@control.lth.se>
Date: Tue, 15 Sep 2015 16:48:13 +0200
Subject: [PATCH] Lade till hantering av grupper.

---
 action.add_edit_Group.php       | 78 +++++++++++++++++++++++++++++++++
 action.editGroups.php           | 10 +++--
 lib/class.Group.php             | 61 +++++++++++++++++++++++++-
 templates/editpanel.tpl         |  2 +-
 templates/groupEditOnePanel.tpl | 14 ++++++
 templates/groupEditPanel.tpl    |  7 +--
 6 files changed, 162 insertions(+), 10 deletions(-)
 create mode 100644 action.add_edit_Group.php
 create mode 100644 templates/groupEditOnePanel.tpl

diff --git a/action.add_edit_Group.php b/action.add_edit_Group.php
new file mode 100644
index 0000000..0821c6b
--- /dev/null
+++ b/action.add_edit_Group.php
@@ -0,0 +1,78 @@
+<?php
+if (!isset($gCms)) exit;
+//echo '<pre>'; print_r($params); echo '</pre>';
+
+if (! $this->CheckPermission('Use ReglerAddress')) {
+  return $this->DisplayErrorPage($id, $params, $returnid,
+				 'ReglerAddress access denied.');
+}
+require_once 'lib/class.Group.php';
+
+if (isset($params['cancelUpdate'])) {
+  $paramsB = array(); 
+  $this->Redirect($id, 'editGroups', $returnid, $paramsB);
+  return;
+}
+
+if (isset($params['submit'])) {
+  $record = new Group($params);
+  echo '<pre>'; print_r($record); echo '</pre>';
+  $record->updateRecord();
+  $paramsB['module_message'] = 'Gruppen ändrad';
+  $this->Redirect($id, 'editGroups', $returnid, $paramsB);
+  return;
+}
+
+
+
+$smarty = $this->smarty;
+$smarty->assign('headline','Hantera grupp ');
+
+$paramKeys = array_keys($params);
+$ch = 'change_';
+foreach ($paramKeys as $key) {
+  $pos = strpos($key,$ch);
+  if ($pos === false) continue;
+  $ID = substr($key,strlen($ch));
+  break;
+}
+if (isset($params['new'])){
+  $group = new Group();
+  $ID = '';
+} else {
+  $group = new Group($ID);
+}
+
+$place = $this->CreateFormStart($id, 'add_edit_Group', $returnid);
+$smarty->assign('start_form', $place);
+$smarty->assign('end_form', $this->CreateFormEnd());
+$fields = Group::$groupFields;
+
+$smarty->assign('title_ID','ID');
+$smarty->assign('value_ID',$ID);
+$smarty->assign('title_Gruppnamn','Gruppnamn');
+$place = $this->CreateInputText($id,'Gruppnamn',$group->Gruppnamn,30);
+$smarty->assign('input_Gruppnamn',$place);
+$smarty->assign('title_GruppInfo', 'Grupp info');
+$place = $this->CreateTextArea(false,$id,$group->GruppInfo,'GruppInfo',
+				 'width:auto; height:auto;','','','',30,3);
+$place = str_replace('class','style', $place);
+$smarty->assign('text_'.'GruppInfo',$place);
+
+$place = $this->CreateInputSubmit($id, 'submit', 'Uppdatera');
+$smarty->assign('submitButton',$place);
+
+$place = $this->CreateInputSubmit($id, 'cancelUpdate', 'Avbryt');
+$smarty->assign('cancelButton',$place);
+
+if (isset($ID)) {
+   $place = $this->CreateInputHidden($id, 'ID',$ID);
+   $smarty->assign('hidden',$place);
+}
+$smarty->assign('Group',$group);
+
+//echo '<pre>'; print_r($group); echo '</pre>';
+echo $this->ProcessTemplate('groupEditOnePanel.tpl');  
+  
+
+?>
diff --git a/action.editGroups.php b/action.editGroups.php
index 03dc047..8a18dd6 100644
--- a/action.editGroups.php
+++ b/action.editGroups.php
@@ -11,18 +11,20 @@ require_once 'lib/class.Group.php';
 $smarty = $this->smarty;
 $smarty->assign('headline','Grupphantering');
 
-$place = $this->CreateFormStart($id, 'editGroups', $returnid);
+$place = $this->CreateFormStart($id, 'add_edit_Group', $returnid);
 $smarty->assign('start_form', $place);
 $smarty->assign('end_form', $this->CreateFormEnd());
 
 $groups = Group::GetGroupList();
 foreach ($groups as $group) {
-  $group->change = 'Change';
-  $group->print = 'Print';
+  $group->change =  $this->CreateInputSubmit($id, 'change_'.$group->ID, 'Ändra');
+  $group->print = $this->CreateInputSubmit($id, 'print_'.$group->ID, 'Skriv ut');
 }
+$place = $this->CreateInputSubmit($id, 'new', 'Lägg till grupp');
+$smarty->assign('newButton',$place);
 
 $smarty->assign('Groups',$groups);
-
+//echo '<pre>'; print_r($groups); echo '</pre>';
 echo $this->ProcessTemplate('groupEditPanel.tpl');  
   
 
diff --git a/lib/class.Group.php b/lib/class.Group.php
index 8db34fd..e322566 100644
--- a/lib/class.Group.php
+++ b/lib/class.Group.php
@@ -9,6 +9,10 @@ class Group {
     select ID, Gruppnamn, GruppInfo from Grupper order by Gruppnamn
 EOS;
 
+  private static $queryOne = <<<EOS
+    select * from Grupper where ID = ?
+EOS;
+
   private static $queryMembership = <<<EOS
     select Grupper.ID, Gruppnamn from Grupper,GruppAdress where 
     GruppID = Grupper.ID and Adressid = ? order by Gruppnamn
@@ -27,14 +31,19 @@ EOS;
     Adresser.ID = AdressID and GruppID = ?
 EOS;
 
-  private static $groupFields = array('ID', 'Gruppnamn', 'GruppInfo');
+  private static $queryUpdate = <<<EOS
+    update Grupper set where ID = ?
+EOS;
+
+
+  public static $groupFields = array('ID', 'Gruppnamn', 'GruppInfo');
   private static $buttonFields = array('change','print');
 
   private static $instance;
 
   private static $list = array();
 
-  private function __construct($param = NULL) {
+  public function __construct($param = NULL) {
     foreach (self::$groupFields as $field) {$this->$field = '';}
     foreach (self::$buttonFields as $field) {$this->$field = '';}
     if (is_null($param)) return;
@@ -42,9 +51,57 @@ EOS;
       foreach (self::$groupFields as $field) {
 	$this->$field = $param[$field];
       }
+      return;
+    }
+    $db = AddressDB::getDB();
+    $result = $db->Execute(self::$queryOne,array($param));
+    if ($result === false) { die("Database error!<br>" . $db->ErrorMsg());}
+    $count = $result->RecordCount();
+    switch ($count) {
+    case 0:
+      die(sprintf('Bad group ID %s'.PHP_EOL,$adressID));
+      break;
+    case 1:
+      $row = $result->FetchRow();
+      foreach (self::$groupFields as $field) {
+	if (isset($row[$field])) $this->$field = $row[$field];
+      }
+      break;
+    default:
+      die(sprintf(
+         'Strange error. Exactly one group record expected for key %s',
+         $ID));
     }
   }
 
+  private function createRecord() {
+    $db = AddressDB::getDB();
+    $query = 'insert into Grupper set Gruppnamn = ""';
+    $result = $db->Execute($query,array());
+    if ($result === false) {die("Database error!<br>" . $publDB->ErrorMsg()); }
+    $this->ID = $db->Insert_ID();
+  }
+ 
+  public function updateRecord() {
+    global $gCms; if (!isset($gCms)) exit;
+    $db = AddressDB::getDB();
+    if ($this->ID == '') { $this->createRecord(); }
+
+    $query = 'update Grupper set ';
+    $qfields = array(); $values = array();
+    foreach (self::$groupFields as $field) {
+      if ($field == 'ID') continue;
+      $val = trim($this->$field);
+      if (strlen($val) == 0) $val = null;
+      $qfields[] = sprintf('%s = ?',$field);
+      $values[] = $val;
+    }
+    $values[] = $this->ID;
+    $query .= join(', ',$qfields) . ' where ID=?';
+    $result = $db->Execute($query,$values);
+    if ($result === false) {die("Database error!<br>" . $db->ErrorMsg());}
+  }
+ 
   public static function getGroupList() {
     if (count(self::$list) > 0) return $list;
     global $gCms; if (!isset($gCms)) exit;
diff --git a/templates/editpanel.tpl b/templates/editpanel.tpl
index 01f270d..dff7bb7 100644
--- a/templates/editpanel.tpl
+++ b/templates/editpanel.tpl
@@ -47,5 +47,5 @@
 
 
 </tbody></table>
-{$hidden}
+{if isset($hidden)}{$hidden}{/if}
 {$end_form}
diff --git a/templates/groupEditOnePanel.tpl b/templates/groupEditOnePanel.tpl
new file mode 100644
index 0000000..82546f4
--- /dev/null
+++ b/templates/groupEditOnePanel.tpl
@@ -0,0 +1,14 @@
+{$start_form}
+<table CELLPADDING="3" align="center"  bgcolor="#f0f0ff">
+<thead><tr><td bgcolor="#000080" colspan="2" style="text-align: center;
+   font-size: 200%; font-weight:bold; color: white;">{$headline}</td></tr>
+   <tr><td colspan="2"><td>&nbsp;</td></tr>
+   </thead>
+<tbody>
+<tr><td>{$title_ID}</td><td>{$value_ID}</td></tr>
+<tr><td>{$title_Gruppnamn}</td><td>{$input_Gruppnamn}</td></tr>
+<tr><td>{$title_GruppInfo}</td><td>{$text_GruppInfo}</td></tr>
+<tr><td colspan="2" style="text-align: center;">{$submitButton}&nbsp;{$cancelButton}</td></tr>
+</tbody></table>
+{if isset($hidden)}{$hidden}{/if}
+{$end_form}
diff --git a/templates/groupEditPanel.tpl b/templates/groupEditPanel.tpl
index 8ca44f3..fbe1f68 100644
--- a/templates/groupEditPanel.tpl
+++ b/templates/groupEditPanel.tpl
@@ -5,13 +5,14 @@
    <tr><td colspan="3"><td>&nbsp;</td></tr>
    </thead>
 <tbody>
-{if !empty($groups)}
-{foreach $groups as $group}
+{if !empty($Groups)}
+{foreach $Groups as $group}
 <tr><td>{$group->ID}</td><td>{$group->Gruppnamn}</td>
     <td>{$group->GruppInfo}</td><td>{$group->change}</td>
     <td>{$group->print}</td></tr>
 {/foreach}
 {/if}
+<tr><td colspan="5" style="text-align: center;">{$newButton}</td></tr>
 </tbody></table>
-{$hidden}
+{if isset($hidden)}{$hidden}{/if}
 {$end_form}
-- 
GitLab