<?php

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

class ModuleSystem extends Module
{
  public bool $InstalledChecked;

  function __construct(System $System)
  {
    parent::__construct($System);
    $this->Name = 'System';
    $this->Version = '1.0';
    $this->Creator = 'Chronos';
    $this->License = 'GNU/GPLv3';
    $this->Description = 'Base system module';
    $this->Type = ModuleType::System;
    $this->Dependencies = array(ModuleModuleManager::GetName());
    $this->Models = array(UnitOfMeasure::GetClassName(), ActionIcon::GetClassName(), ActionGroup::GetClassName(),
      ActionType::GetClassName(), Action::GetClassName(), Language::GetClassName(), Country::GetClassName());
  }

  function DoStart(): void
  {
    $this->System->FormManager->RegisterClass('Action', array(
      'Title' => 'Akce',
      'Table' => 'Action',
      'Items' => array(
        //'Name' => array('Type' => 'String', 'Caption' => 'Název', 'Default' => ''),
        'Title' => array('Type' => 'String', 'Caption' => 'Název', 'Default' => ''),
        'URL' => array('Type' => 'Hyperlink', 'Caption' => 'Odkaz', 'Default' => ''),
        'Icon' => array('Type' => 'TActionIcon', 'Caption' => 'Ikony', 'Default' => '', 'Null' => true),
        'Type' => array('Type' => 'TActionType', 'Caption' => 'Typ', 'Default' => ''),
        'Group' => array('Type' => 'TActionGroup', 'Caption' => 'Skupina', 'Default' => '', 'Null' => true),
        'PermissionOperation' => array('Type' => 'TPermissionOperation', 'Caption' => 'Operace oprávnění', 'Default' => ''),
        'Enable' => array('Type' => 'Boolean', 'Caption' => 'Povolení', 'Default' => ''),
      ),
    ));
    $this->System->FormManager->RegisterClass('ActionIcon', array(
      'Title' => 'Ikony akcí',
      'Table' => 'ActionIcon',
      'Items' => array(
        'Name' => array('Type' => 'Image', 'Caption' => 'Název souboru', 'Default' => ''),
        'Items' => array('Type' => 'TActionListIcon', 'Caption' => 'Položky', 'Default' => ''),
      ),
    ));
    $this->System->FormManager->RegisterClass('ActionGroup', array(
      'Title' => 'Skupiny akcí',
      'Table' => 'ActionGroup',
      'Items' => array(
        'Name' => array('Type' => 'String', 'Caption' => 'Název', 'Default' => ''),
        'Items' => array('Type' => 'TActionListGroup', 'Caption' => 'Položky', 'Default' => ''),
      ),
    ));
    $this->System->FormManager->RegisterClass('ActionType', array(
      'Title' => 'Typy akcí',
      'Table' => 'ActionType',
      'Items' => array(
        'Name' => array('Type' => 'String', 'Caption' => 'Název', 'Default' => ''),
        'Items' => array('Type' => 'TActionListType', 'Caption' => 'Položky', 'Default' => ''),
      ),
    ));
    $this->System->FormManager->RegisterClass('Language', array(
      'Title' => 'Jazyky',
      'Table' => 'Language',
      'DefaultSortColumn' => 'Name',
      'Items' => array(
        'Name' => array('Type' => 'String', 'Caption' => 'Jméno', 'Default' => ''),
      ),
    ));
    $this->System->FormManager->RegisterClass('UnitOfMeasure', array(
      'Title' => 'Měrné jednotky',
      'Table' => 'UnitOfMeasure',
      'DefaultSortColumn' => 'Name',
      'Items' => array(
        'Name' => array('Type' => 'String', 'Caption' => 'Jméno', 'Default' => ''),
        'Unit' => array('Type' => 'String', 'Caption' => 'Jednotka', 'Default' => ''),
      ),
    ));
    $this->System->FormManager->RegisterClass('Country', array(
      'Title' => 'Země',
      'Table' => 'Country',
      'DefaultSortColumn' => 'Name',
      'Items' => array(
        'Name' => array('Type' => 'String', 'Caption' => 'Jméno', 'Default' => ''),
      ),
    ));
    $this->System->FormManager->RegisterFormType('TCountry', array(
      'Type' => 'Reference',
      'Table' => 'Country',
      'Id' => 'Id',
      'Name' => 'Name',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterFormType('TUnitOfMeasure', array(
      'Type' => 'Reference',
      'Table' => 'UnitOfMeasure',
      'Id' => 'Id',
      'Name' => 'Name',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterFormType('TLanguage', array(
      'Type' => 'Reference',
      'Table' => 'Language',
      'Id' => 'Id',
      'Name' => 'Name',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterFormType('TAction', array(
      'Type' => 'Reference',
      'Table' => 'Action',
      'Id' => 'Id',
      'Name' => 'Title',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterFormType('TActionIcon', array(
      'Type' => 'Reference',
      'Table' => 'ActionIcon',
      'Id' => 'Id',
      'Name' => 'Name',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterFormType('TActionType', array(
      'Type' => 'Reference',
      'Table' => 'ActionType',
      'Id' => 'Id',
      'Name' => 'Name',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterFormType('TActionGroup', array(
      'Type' => 'Reference',
      'Table' => 'ActionGroup',
      'Id' => 'Id',
      'Name' => 'Name',
      'Filter' => '1',
    ));
  }

  static function Cast(Module $Module): ModuleSystem
  {
    if ($Module instanceof ModuleSystem)
    {
      return $Module;
    }
    throw new Exception('Expected ModuleSystem type but got '.gettype($Module));
  }

  function ShowAction(string $Id): string
  {
    $Output = '';
    $DbResult = $this->Database->query('SELECT *, `ActionIcon`.`Name` AS `Icon` FROM `Action` '.
      'LEFT JOIN `ActionIcon` ON `ActionIcon`.`Id` = `Action`.`Icon` '.
      'WHERE (`Action`.`Id`='.$Id.')');
    if ($DbResult->num_rows > 0)
    {
      $Action = $DbResult->fetch_assoc();
      if ($Action['Icon'] == '') $Action['Icon'] = 'clear.png';
      if (substr($Action['URL'], 0, 4) != 'http') $Action['URL'] = Core::Cast($this->System)->Link($Action['URL']);
      if (!defined('NEW_PERMISSION') or ModuleUser::Cast(Core::Cast($this->System)->GetModule('User'))->User->CheckPermission('System', 'Read', 'Item', $Id))
        $Output .= '<img alt="'.$Action['Title'].'" src="'.Core::Cast($this->System)->Link('/images/favicons/'.$Action['Icon']).
        '" width="16" height="16" /> <a href="'.$Action['URL'].'">'.$Action['Title'].'</a>';
    }
    return $Output;
  }
}
