<?php

include_once(dirname(__FILE__).'/Base.php');

class TypePointerOneToOne extends TypeBase
{
  function OnView($Item)
  {
    $Type = $this->System->Type->TypeDefinitionList[$Item['Type']];
    $DbResult = $this->System->Database->query('SELECT '.$Type['Parameters']['Name'].' AS Name FROM `'.$Type['Parameters']['Table'].'` WHERE `'.$Type['Parameters']['Id'].'`='.$Item['Value']);
    $DbRow = $DbResult->fetch_assoc();
    if($this->System->Modules['Permission']->Check('Corporation', 'Show'))
      $Output = $this->System->HTML->MakeLink($DbRow['Name'], $this->System->MakeLink($Type['Parameters']['Table'], 'Show', array('Id' => $Item['Value'])));
      else $Output = $DbRow['Name'];
    return($Output);
  }

  function OnEdit($Item)
  {
    $Output = '<select name="'.$Item['Name'].'">';
    $Type = $this->System->Type->TypeDefinitionList[$Item['Type']];
    if(array_key_exists('Condition', $Type['Parameters'])) $Where = ' WHERE '.$Type['Parameters']['Condition'];
      else $Where = '';
    $DbResult = $this->System->Database->query('SELECT '.$Type['Parameters']['Name'].' AS Name,'.$Type['Parameters']['Id'].' AS Id FROM `'.$Type['Parameters']['Table'].'`'.$Where.' ORDER BY Name DESC');
    while($DbRow = $DbResult->fetch_assoc())
    {
      if($Item['Value'] == $DbRow['Id']) $Selected = ' selected="1"'; else $Selected = '';
      $Output .= '<option value="'.$DbRow['Id'].'"'.$Selected.'>'.$DbRow['Name'].'</option>';
    }
    $Output .= '</select>';
    return($Output);
  }

  function OnLoad($Item)
  {
    return($_POST[$Item['Name']]);
  }
}

?>
