zoneminder/web/includes/Server.php

85 lines
2.2 KiB
PHP
Raw Normal View History

2015-09-17 03:16:07 +08:00
<?php
require_once( 'database.php' );
class Server {
public function __construct( $IdOrRow = NULL ) {
$row = NULL;
if ( $IdOrRow ) {
if ( is_integer( $IdOrRow ) or is_numeric( $IdOrRow ) ) {
$row = dbFetchOne( 'SELECT * FROM Servers WHERE Id=?', NULL, array( $IdOrRow ) );
if ( ! $row ) {
Error("Unable to load Server record for Id=" . $IdOrRow );
}
} elseif ( is_array( $IdOrRow ) ) {
$row = $IdOrRow;
}
} # end if isset($IdOrRow)
if ( $row ) {
foreach ($row as $k => $v) {
$this->{$k} = $v;
}
} else {
$this->{'Name'} = '';
$this->{'Hostname'} = '';
2015-09-18 03:34:26 +08:00
}
}
public static function find_all() {
$servers = array();
$result = dbQuery( 'SELECT * FROM Servers ORDER BY Name');
$results = $result->fetchALL(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, 'Server' );
foreach ( $results as $row => $server_obj ) {
$servers[] = $server_obj;
}
return $servers;
}
2015-09-18 03:34:26 +08:00
2015-12-02 04:16:07 +08:00
public function Url() {
if ( $this->Id() ) {
return ZM_BASE_PROTOCOL . '://'. $this->Hostname();
} else {
return '';
}
2015-09-18 03:34:26 +08:00
}
public function Hostname() {
2015-10-03 02:22:19 +08:00
if ( isset( $this->{'Hostname'} ) and ( $this->{'Hostname'} != '' ) ) {
return $this->{'Hostname'};
}
2015-10-03 02:22:19 +08:00
return $this->{'Name'};
2015-09-17 03:16:07 +08:00
}
2015-12-02 04:16:07 +08:00
public function __call( $fn, array $args= NULL){
if( array_key_exists( $fn, $this) ) {
return $this->{$fn};
#array_unshift($args, $this);
#call_user_func_array( $this->{$fn}, $args);
2015-12-02 04:16:07 +08:00
}
}
public static function find( $parameters = array(), $limit = NULL ) {
$sql = 'SELECT * FROM Servers';
$values = array();
if ( sizeof($parameters) ) {
$sql .= ' WHERE ' . implode( ' AND ', array_map(
function($v){ return $v.'=?'; },
array_keys( $parameters )
) );
$values = array_values( $parameters );
}
if ( $limit ) {
$sql .= ' LIMIT ' . $limit;
}
$results = dbFetchAll( $sql, NULL, $values );
if ( $results ) {
return array_map( function($id){ return new Server($id); }, $results );
}
}
public static function find_one( $parameters = array() ) {
$results = Server::find( $parameters, 1 );
if ( ! sizeof( $results ) ) {
return;
}
return $results[0];
}
2015-09-17 03:16:07 +08:00
}
?>