diff --git a/action.add_edit_Group.php b/action.add_edit_Group.php new file mode 100644 index 0000000000000000000000000000000000000000..0821c6b9851695f4e2905745c87b906d8b996e14 --- /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 03dc04739d4c710e7768fa2ea5fa604fe7838943..8a18dd62e357ec98426ece8a54424baee11e8801 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 8db34fdfc9f35783f3537cfec095a58b22e7fd93..e322566b7cae40a559dcf84fb83ab5c3c87d74e3 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 01f270db5abf2656c450268d208670f53ac4d20e..dff7bb7d7cf5528ba09ee638f6a5930e31d11f61 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 0000000000000000000000000000000000000000..82546f4da972ee5d9ba4538e56322e240ca0a237 --- /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> </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} {$cancelButton}</td></tr> +</tbody></table> +{if isset($hidden)}{$hidden}{/if} +{$end_form} diff --git a/templates/groupEditPanel.tpl b/templates/groupEditPanel.tpl index 8ca44f35c40ea724e189d6ed2632f157af4d87f5..fbe1f684bb87bcaa54118fc1893231c41d3d20d2 100644 --- a/templates/groupEditPanel.tpl +++ b/templates/groupEditPanel.tpl @@ -5,13 +5,14 @@ <tr><td colspan="3"><td> </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}