<?php

class BuildingFileController 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 editAction() {
    // internal redirect to File::editAction
  }
  
  protected function deleteAction() {
    $id = $this->request->id;
    
    $buildingfile = new BuildingFile($id);
    if(!$buildingfile->id || $buildingfile->id != $id) {
      $this->layout()->setFlash("Datei nicht gefunden.", "error");
      $this->redirect("Building");
    }
    
    $building_id = $buildingfile->building_id;
    
    $buildingfile->file->delete();
    $buildingfile->delete();
    
    if($this->request->from == "pipework") {
      $this->redirect("Pipework", "Index", false, "object=".$building_id);
    }
    
    $this->redirect("Building", "edit", ["id" => $building_id]);
  }
}