Commit d1d5e3de authored by Leif Andersson's avatar Leif Andersson
Browse files

Lade till hantering av grupper.

parent 09e6dea6
<?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');
?>
......@@ -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');
......
......@@ -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;
......
......@@ -47,5 +47,5 @@
</tbody></table>
{$hidden}
{if isset($hidden)}{$hidden}{/if}
{$end_form}
{$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}
......@@ -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}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment