<?php

class NetworkAddressController extends mfBaseController {
  
  protected function init() {
    $this->needlogin=true;
    $me = new User();
    $me->loadMe();
    $this->me = $me;
    $this->layout()->set("me",$me);
    
    if(!$me->isAdmin()) {
      $this->redirect("Dashboard");
    }
  }
  
  protected function saveAction() {
    $r = $this->request;
    
    if(!is_array($r->roles)) {
      $this->redirect("Network");
    }
    if(!$r->network_id) {
      $this->redirect("Network");
    }
    $network_id = $r->network_id;
    
    $network = new Network($network_id);
    if(!$network->id) {
      $this->layout()->setFlash("Netzgebiet nicht gefunden.", "error");
      $this->redirect("Network");
    }
    
    foreach($r->roles as $address_id => $new_roles) {
      
      $address = new Address($address_id);
      if(!$address->id) {
        $this->layout()->setFlash("Person/Firma nicht gefunden.", "error");
        $this->redirect("Network");  
      }
      
      foreach(TT_NETWORK_ROLES as $rolestring) {
        if(in_array($rolestring, $new_roles)) {
          // check if role exists
          if(NetworkAddressModel::search(['network_id' => $network, 'address_id' => $address_id, 'addresstype' => [$rolestring]])) {
            continue; // role exists
          } else {
            $na = NetworkAddressModel::create(['network_id' => $network_id, 'address_id' => $address_id, 'type' => $rolestring]);
            $na->save();
          }
          // role does not exist, create it
        } else {
          // not in new_roles, delete (if exists)
          $na = NetworkAddressModel::getFirst(['network_id' => $network, 'address_id' => $address_id, 'addresstype' => [$rolestring]]);
          if($na) {
            $na->delete();
          }
        }
        
      }
    }
    $this->redirect("Network", "Index", [],"view=roles&net=$network_id");
  }
  
  protected function createAction() {
    //var_dump($this->request);
    //var_dump($r->roles);exit;
    
    $r = $this->request;
    //var_dump($r->roles);exit;
    if(!is_array($r->roles)) {
      $this->redirect("Network");
    }
    if(!$r->network_id) {
      $this->redirect("Network");
    }
    $network_id = $r->network_id;
    $network = new Network($network_id);
    if(!$network->id) {
      $this->log->info("Netzgebiet nicht gefunden (".$network_id.")");
      $this->layout()->setFlash("Netzgebiet nicht gefunden.", "error");
      $this->redirect("Network");
    }
    
    if(!$r->address_id) {
      $this->redirect("Network");
    }
    $address_id = $r->address_id;
    $address = new Address($address_id);
    if(!$address->id) {
      $this->log->info("Person/Firma nicht gefunden (".$address_id.")");
      $this->layout()->setFlash("Person/Firma nicht gefunden.", "error");
      $this->redirect("Network");
    }    
    
    foreach($r->roles as $type) {
      $na = NetworkAddressModel::create(['network_id' => $network_id, 'address_id' => $address_id, 'type' => $type]);
      $na->save();
    }
    
    $this->redirect("Network", "Index", [],"view=roles&net=$network_id");
  }
  
  protected function deleteAction() {
    $r = $this->request;
    //var_dump($r->roles);exit;
    if(!is_numeric($r->network_id) && $r->network_id <= 0) {
      $this->layout()->setFlash("Netzgebiet nicht gefunden.", "error");
      $this->redirect("Network");
    }
    if(!is_numeric($r->address_id) && $r->address_id <= 0) {
      $this->layout()->setFlash("Person/Firma nicht gefunden.", "error");
      $this->redirect("Network");
    }
    
    $network_id = $r->network_id;
    $address_id = $r->address_id;
    
    $network = new Network($network_id);
    if(!$network->id) {
      $this->layout()->setFlash("Netzgebiet nicht gefunden.", "error");
      $this->redirect("Network");
    }
    
    $address = new Address($address_id);
    if(!$address->id) {
      $this->layout()->setFlash("Person/Firma nicht gefunden.", "error");
      $this->redirect("Network", "Index", [], "view=roles&net=$network_id");
    }
    
    $roles = NetworkAddressModel::search(["network_id" => $network_id, "address_id" => $address_id]);
    //var_dump($roles);exit;
    
    foreach($roles as $role) {
      $role->delete();
    }
    
    $this->layout()->setFlash("Berechtigungen erfolgreich gelöscht.", "success");
    $this->redirect("Network", "Index", [], "view=roles&net=$network_id");
  }
}