<?php

class VoicenumberblockModel {
  public $name;
  public $countrycode;
  public $areacode;
  public $first;
  public $last;
  public $comment;
  
  public $create_by;
  public $edit_by;
  public $create;
  public $edit;
  
  
  public static function create(Array $data) {
    $model = new Voicenumberblock();
    
    foreach($data as $field => $value) {
      if(property_exists(get_called_class(), $field)) {
        $model->$field = $value;
      }
    }
    
    $me = new User();
    $me->loadMe();

    if($model->create_by === null) {
      $model->create_by = $me->id;
    }
    if($model->edit_by === null) {
      $model->edit_by = $me->id;
    }
    
    return $model;
  }
  
  public static function getAll() {
    $items = [];
    
    $db = FronkDB::singleton();
    
    $res = $db->select("Voicenumberblock", "*");
    if($db->num_rows($res)) {
      while($data = $db->fetch_object($res)) {
        $items[] = new Voicenumberblock($data);
      }
    }
    return $items;
    
  }
  
  public static function getFirst() {
    $db = FronkDB::singleton();
    
    $where = self::getSqlFilter($filter);
    $res = $db->select("Voicenumberblock", "*", "$where ORDER BY countrycode, areacode, first, last");
    if($db->num_rows($res)) {
      $data = $db->fetch_object($res);
      $item = new Voicenumberblock($data);
      if($item->id) {
        return $item;
      } else {
        return null;
      }
    }
    return null;
  }
  
  public static function count($filter) {
    $db = FronkDB::singleton();
    
    $where = self::getSqlFilter($filter);
    $sql = "SELECT COUNT(*) as cnt FROM Voicenumberblock
            WHERE $where
           ";
    
    $res = $db->query($sql);
    if($db->num_rows($res)) {
      $data = $db->fetch_object($res);
      return $data->cnt;
    }
    return 0;
  }
  
  public static function search($filter) {
    $items = [];
    $db = FronkDB::singleton();
    
    $where = self::getSqlFilter($filter);
    //mfLoghandler::singleton()->log->debug($where);
    $res = $db->select("Voicenumberblock", "*", "$where ORDER BY countrycode, areacode, first, last");
    if($db->num_rows($res)) {
      while($data = $db->fetch_object($res)) {
        $items[] = new Voicenumberblock($data);
      }
    }
    return $items;
  }
  
  private function getSqlFilter($filter) {
    $db = FronkDB::singleton();
    $where = "1=1 ";
    
    if(!is_array($filter)) {
      return $where;
    }
    
    //var_dump($filter);exit;
    if(array_key_exists("countrycode", $filter)) {
      $countrycode = $filter['countrycode'];
      if(is_numeric($countrycode)) {
        $where .= " AND countrycode like '%$countrycode%'";
      }
    }
    
    if(array_key_exists("areacode", $filter)) {
      $areacode = $filter['areacode'];
      if(is_numeric($areacode)) {
        $where .= " AND areacode like '%$areacode%'";
      }
    }
    
    if(array_key_exists("first", $filter)) {
      $first = $filter['first'];
      if(is_numeric($first)) {
        $where .= " AND first like '%$first%'";
      }
    }
    
    if(array_key_exists("last", $filter)) {
      $last = $filter['last'];
      if(is_numeric($last)) {
        $where .= " AND last= like '%$last%'";
      }
    }
    
    if(array_key_exists("number", $filter)) {
      $number = $filter['number'];
      if(is_numeric($number)) {
        $where .= " AND first <= $number AND last >= $number";
      }
    }
    
    if(array_key_exists("name", $filter)) {
      $name = $db->escape($filter['name']);
      if($name) {
        $where .= " AND name like '%$name%'";
      }
    }
    
    if(array_key_exists("comment", $filter)) {
      $comment = $db->escape($filter['comment']);
      if($comment) {
        $where .= " AND comment like '%$comment%'";
      }
    }
    
    //var_dump($filter, $where);exit;
    return $where;
  }
  
}