<?php
/**
 * DisplayAdminPanel($id, $params, $returnid, $message)
 * NOT PART OF THE MODULE API
 * 
 */

/**
 * For separated methods, you'll always want to start with the following
 * line which check to make sure that method was called from the module
 * API, and that everything's safe to continue:
 */ 
// require_once 'lib/class.Group.php';
if (!isset($gCms)) exit;

//echo '<pre>'; print_r($params); echo '</pre>';

/** 
 * For separated methods, you won't be able to do permission checks in
 * the DoAction method, so you'll need to do them as needed in your
 * method:
*/ 
/* if (! $this->CheckPermission('Use ReglerAddress')) { */
/*   return $this->DisplayErrorPage($id, $params, $returnid, */
/* 				 'ReglerAddress access denied.'); */
/* } */
$canChangeRecord = $this->CheckPermission('Use ReglerAddress');
/**
 * After this, the code is identical to the code that would otherwise be
 * wrapped in the DisplayAdminPanel() method in the module body.
 */
 
$groups = Groups::getGroupList(); 
//echo '<pre>'; print_r($groups); echo '</pre>'; exit;

$addrDB = AddressDB::getDB();
$smarty = $this->smarty;
// echo '<pre>'; print_r($params); echo '</pre>'; 

$place = $this->CreateFormStart($id, 'defaultadmin', $returnid);
$smarty->assign('start_form', $place);
$smarty->assign('end_form', $this->CreateFormEnd());

$fields = array(
  'fornamn' => 'Förnamn',
  'efternamn' => 'Efternamn',
  'foretag' => 'Skola/Företag',
  'avdelning' => 'Avdelning',
  'adress' => 'Adress',
  );
$smarty->assign('fields',$fields);

if (isset($params['new'])) {
  $this->Redirect($id, 'add_edit', $returnid, array());
}

if (isset($params['groupChange'])) {
  $this->Redirect($id,'editGroups',$returnid,array());
}
	
foreach ($fields as $key => $res) {
  $value = '';
  if (isset($params[$key])) {$value = trim($params[$key]); }
  $smarty->assign('title_'.$key,$res);
  $place = $this->CreateInputText($id,$key,$value,40);
  $smarty->assign('input_'.$key,$place);
}  
  

$place = $this->CreateInputSubmit($id, 'search', 'Sök');
$smarty->assign('searchButton',$place);

if ($canChangeRecord) {
  $place = $this->CreateInputSubmit($id, 'new', 'Ny post');
} else {
  $place = '';
}
$smarty->assign('newButton',$place);

$place =  $this->CreateInputSubmit($id, 'groupChange', 'Hantera grupper');
$smarty->assign('groupButton',$place);

$name = 'Grupp';
$groupNames = array(' ' => 'None');
foreach ($groups as $group) {
  $groupNames[$group->Gruppnamn] = $group->ID;
}
$selected = array();
if (isset($params[$name])) $selected[] = $params[$name];
$place = $this->CreateInputSelectList($id, $name, $groupNames, 
//   $selecteditems = array(), $size = 10, $addttext = '', $multiple = false);
     $selected, 10, '', false);
$smarty->assign('selectgroup',$place);

$formValues = array(); $hiddenParameters = '';
foreach ($params as $key => $value) {
  if ($key == 'search' || $key == 'action') continue;
  if ($key == 'Grupp' && $value == 'None') continue;
  if ($key == 'module_message') continue;
  if ($value != '') {
    $formValues[$key] = $value;
    $hiddenParameters[$key] = $value;
  }
}
$Persons = array();
if (count($formValues) > 0) $Persons = Person::searchPersons($formValues);
foreach ($Persons as $person) {
  $params = array('ID' => $person->ID) + $hiddenParameters;
  $person->link = $this->CreateLink($id, 'add_edit', '', 'Visa', $params);
}

if (isset($params['module_message'])) {
   $smarty->assign('module_message',$params['module_message']);
}

$smarty->assign('Persons',$Persons);

echo $this->ProcessTemplate('adminpanel.tpl');

//echo '<pre>'; print_r($Persons); echo '</pre>';

?>