<?php

class VoicenumberblockController 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 indexAction() {
    $this->layout()->setTemplate("Voicenumberblock/Index");
    
    $filter = [];
    if(is_array($this->request->filter)) {
      $filter = $this->request->filter;
    }
    
    $this->layout->set("filter", $filter);
    
    if($filter) {
      $filter = $this->getPreparedFilter($filter);
    }
    
    // pagination defaults
    $pagination = [];
    $pagination['start'] = 0;
    $pagination['count'] = 20;
    $pagination['maxItems'] = 0;
    
    if(is_numeric($this->request->s)) {
      $pagination['start'] = intval($this->request->s);
    }
    
    $pagination['maxItems'] = VoicenumberblockModel::count($filter);
    $blocks = VoicenumberblockModel::search($filter, $pagination);
    
    
    $this->layout()->set("blocks", $blocks);
    $this->layout()->set("pagination", $pagination);
    
  }
  
  private function getPreparedFilter($filter) {
    return $filter;
  }
  
  protected function addAction() {
    $this->layout()->setTemplate("Voicenumberblock/Form");
  }
  
  protected function editAction() {
    $id = $this->request->id;
    if(!is_numeric($id) || !$id) {
      $this->layout()->setFlash("Rufnummernblock nicht gefunden", "error");
      $this->redirect("Voicenumberblock");
    }
    
    $block = new Voicenumberblock($id);
    if($block->id != $id) {
      $this->layout()->setFlash("Rufnummernblock nicht gefunden", "error");
      $this->redirect("Voicenumberblock");
    }
    
    $this->layout()->set("block", $block);
    return $this->addAction();
  }
  
  protected function saveAction() {
    $r = $this->request;
    $id = $r->id;
    //var_dump($r);exit;
    if(is_numeric($id) && $id > 0) {
      $mode = "edit";
      $block = new Voicenumberblock($id);
      if(!$block->id) {
        $this->layout()->setFlash("Rufnummernblock nicht gefunden", "error");
        $this->redirect("Voicenumberblock");
      }
    } else {
      $mode = "add";
    }
    
    if(!$r->countrycode ||!$r->areacode || !$r->first || !$r->last) {
      $this->layout()->setFlash("Bitte alle benötigten Felder ausfüllen!", "error");
      $this->layout()->set("block", $r);
      return $this->add();
    }
    
    $data = [];
    $data['name'] = $r->name;
    $data['countrycode'] = $r->countrycode;
    $data['areacode'] = $r->areacode;
    $data['first'] = $r->first;
    $data['last'] = $r->last;
    $data['comment'] = $r->comment;
    $data['edit_by'] = $this->me->id;
    
    if($mode == "add") {
      $data['create_by'] = 1;
      $block = VoicenumberblockModel::create($data);
    } else {
      $block->update($data);
    }
    
    $new_id = $block->save();
    if(!$new_id) {
      $this->layout()->setFlash("Fehler beim Speichern", "error");
      $this->layout()->set("block", $block);
      return $this->addAction();
    }
    
    $this->layout()->setFlash("Rufnummernblock erfolgreich gespeichert.", "success");
    $this->redirect("Voicenumberblock");
    
  }
}