<?php #resources/Personal/Personal.php

namespace Resources\Personal;

require_once "resources/Personal/Kategorie.php";
require_once "resources/Personal/Funktion.php";

require_once "links/Personal_Abteilungen.php";
require_once "links/Personal_Gruppen.php";
require_once "links/Personal_Lehrgaenge.php";
require_once "links/Personal_Verwalter.php";
require_once "links/Fahrzeuge_Einweisungen.php";
require_once "links/Spinde_Zuordnung.php";

class Personal extends \Resources\Handler
{
	protected $names = ["table" => "Personal", "ident" => "Personal", "short" => "p"];
	protected $has = ["uuid" => false, "sha256" => false];
	protected $definitions = [
		"fields" => [
			"OFnr" => ["default" => 0, "type" => "i"],
			"Pnr" => ["default" => 0, "type" => "i"],
			"Login" => ["default" => "", "type" => "s"],
			"Nachnamen" => ["default" => "", "type" => "s"],
			"Vornamen" => ["default" => "", "type" => "s"],
		],
		"keys" => [
			"Kategorie" => ["resourceClass" => "Personal\\Kategorie", "nullable" => false, "type" => "s"],
			"Funktion" => ["resourceClass" => "Personal\\Funktion", "nullable" => true, "type" => "s"],
		],
		"links" => [
			"Abteilungen" => ["linkClass" => "Personal_Abteilungen"],
			"Gruppen" => ["linkClass" => "Personal_Gruppen"],
			"Lehrgänge" => ["linkClass" => "Personal_Lehrgänge"],
			"Verwalter" => ["linkClass" => "Personal_Verwalter"],
			"Einweisungen" => ["linkClass" => "Fahrzeuge_Einweisungen"],
			"Spinde" => ["linkClass" => "Spinde_Zuordnung"],
		],
		"files" => [
			"Bildadresse" => "Nopic.svg",
		]
	];
	
  protected static $rights = null;
  
  public static function Rights() {
  	if (is_null(self::$rights)) {
  		self::$rights = \Rights\MainRights::Make()
  		  ->Add(\Rights\Main::Make("DARF_PERSONAL_VERWALTEN")->Allow("admin"))
  		  // Jeder darf sich selbst sehen
  		  ->Add(\Rights\Main::Make()->Allow("readonly")->Require(\Rights\Condition::Make()->Add("ID", \Login::ID())))
  		  // Jeder darf seine Telefonnummer und E-Mail-Adresse ändern
  		  ->Add(\Rights\Main::Make()
  		  	->Allow(["update"])
  		  	->Require(\Rights\Condition::Make()->Add("ID", \Login::ID()))
  		  	->Limit(["Telefon", "EMail"]))
  		 	->Add(\Rights\Main::Make("DARF_KRAFTFAHRER_SEHEN")
  		 		->Allow("readonly")
  		 		->Require(\Rights\Condition::Make()->Add("Lehrgänge", 5, \Links\Personal_Lehrgänge::Get()->Short())))
  		 	->Add(\Rights\Main::Make("DARF_AGTS_SEHEN")
  		 		->Allow("readonly")
  		 		->Require(\Rights\Condition::Make()->Add("Lehrgänge", 2, \Links\Personal_Lehrgänge::Get()->Short())));
  	}
  	return self::$rights;
  }
}
