2017-09-24 01:42:39 +08:00
< ? php
class Group {
public $defaults = array (
'Id' => null ,
'Name' => '' ,
2017-10-01 02:19:32 +08:00
'ParentId' => null ,
2017-09-24 01:42:39 +08:00
);
public function __construct ( $IdOrRow = NULL ) {
$row = NULL ;
if ( $IdOrRow ) {
if ( is_integer ( $IdOrRow ) or is_numeric ( $IdOrRow ) ) {
$row = dbFetchOne ( 'SELECT * FROM Groups WHERE Id=?' , NULL , array ( $IdOrRow ) );
if ( ! $row ) {
Error ( 'Unable to load Group record for Id=' . $IdOrRow );
}
} elseif ( is_array ( $IdOrRow ) ) {
$row = $IdOrRow ;
} else {
$backTrace = debug_backtrace ();
$file = $backTrace [ 1 ][ 'file' ];
$line = $backTrace [ 1 ][ 'line' ];
Error ( " Unknown argument passed to Group Constructor from $file : $line ) " );
Error ( " Unknown argument passed to Group Constructor ( $IdOrRow ) " );
return ;
}
} # end if isset($IdOrRow)
if ( $row ) {
foreach ( $row as $k => $v ) {
$this -> { $k } = $v ;
}
}
} // end function __construct
public function __call ( $fn , array $args ) {
if ( count ( $args ) ) {
$this -> { $fn } = $args [ 0 ];
}
if ( array_key_exists ( $fn , $this ) ) {
return $this -> { $fn };
} else if ( array_key_exists ( $fn , $this -> defaults ) ) {
$this -> { $fn } = $this -> defaults { $fn };
return $this -> { $fn };
} else {
$backTrace = debug_backtrace ();
$file = $backTrace [ 1 ][ 'file' ];
$line = $backTrace [ 1 ][ 'line' ];
Warning ( " Unknown function call Group-> $fn from $file : $line " );
}
}
2017-10-01 02:19:32 +08:00
public static function find_all ( $parameters = null ) {
2017-09-24 01:42:39 +08:00
$filters = array ();
2017-10-01 02:19:32 +08:00
$sql = 'SELECT * FROM Groups ' ;
$values = array ();
if ( $parameters ) {
$fields = array ();
$sql .= 'WHERE ' ;
foreach ( $parameters as $field => $value ) {
if ( $value == null ) {
$fields [] = $field . ' IS NULL' ;
2017-10-05 04:40:09 +08:00
} else if ( is_array ( $value ) ) {
$func = function (){ return '?' ;};
$fields [] = $field . ' IN (' . implode ( ',' , array_map ( $func , $value ) ) . ')' ;
$values += $value ;
2017-10-01 02:19:32 +08:00
} else {
$fields [] = $field . '=?' ;
$values [] = $value ;
}
}
$sql .= implode ( ' AND ' , $fields );
}
$sql .= ' ORDER BY Name' ;
$result = dbQuery ( $sql , $values );
$results = $result -> fetchALL ( PDO :: FETCH_CLASS | PDO :: FETCH_PROPS_LATE , 'Group' );
2017-09-24 01:42:39 +08:00
foreach ( $results as $row => $obj ) {
$filters [] = $obj ;
}
return $filters ;
}
public function delete () {
2017-10-10 22:38:13 +08:00
if ( array_key_exists ( 'Id' , $this ) ) {
2017-12-05 04:52:16 +08:00
dbQuery ( 'DELETE FROM Groups WHERE Id=?' , array ( $this -> { 'Id' }) );
dbQuery ( 'DELETE FROM Groups_Monitors WHERE GroupId=?' , array ( $this -> { 'Id' }) );
2017-10-10 22:38:13 +08:00
if ( isset ( $_COOKIE [ 'zmGroup' ]) ) {
if ( $this -> { 'Id' } == $_COOKIE [ 'zmGroup' ] ) {
unset ( $_COOKIE [ 'zmGroup' ] );
setcookie ( 'zmGroup' , '' , time () - 3600 * 24 * 2 );
}
}
}
2017-09-24 01:42:39 +08:00
} # end function delete()
public function set ( $data ) {
foreach ( $data as $k => $v ) {
if ( is_array ( $v ) ) {
$this -> { $k } = $v ;
} else if ( is_string ( $v ) ) {
$this -> { $k } = trim ( $v );
} else if ( is_integer ( $v ) ) {
$this -> { $k } = $v ;
} else if ( is_bool ( $v ) ) {
$this -> { $k } = $v ;
} else {
Error ( " Unknown type $k => $v of var " . gettype ( $v ) );
$this -> { $k } = $v ;
}
}
}
2017-10-05 04:40:09 +08:00
public function depth ( $new = null ) {
if ( isset ( $new ) ) {
$this -> { 'depth' } = $new ;
}
if ( ! array_key_exists ( 'depth' , $this ) or ( $this -> { 'depth' } == null ) ) {
$this -> { 'depth' } = 1 ;
if ( $this -> { 'ParentId' } != null ) {
$Parent = new Group ( $this -> { 'ParentId' } );
$this -> { 'depth' } += $Parent -> depth ();
}
}
return $this -> { 'depth' };
} // end public function depth
2017-09-24 01:42:39 +08:00
2017-12-05 04:52:16 +08:00
public function MonitorIds ( ) {
if ( ! array_key_exists ( 'MonitorIds' , $this ) ) {
$this -> { 'MonitorIds' } = dbFetchAll ( 'SELECT MonitorId FROM Groups_Monitors WHERE GroupId=?' , 'MonitorId' , array ( $this -> { 'Id' }) );
}
return $this -> { 'MonitorIds' };
}
2017-10-05 22:46:04 +08:00
public static function get_group_dropdowns () {
# This will end up with the group_id of the deepest selection
$group_id = 0 ;
$depth = 0 ;
$groups = array ();
$parent_group_ids = null ;
2017-12-14 05:15:03 +08:00
session_start ();
2017-10-05 22:46:04 +08:00
while ( 1 ) {
$Groups = Group :: find_all ( array ( 'ParentId' => $parent_group_ids ) );
if ( ! count ( $Groups ) )
break ;
$parent_group_ids = array ();
$selected_group_id = 0 ;
2017-12-14 05:15:03 +08:00
if ( isset ( $_REQUEST [ 'group' . $depth ]) ) {
$selected_group_id = $group_id = $_SESSION [ 'group' . $depth ] = $_REQUEST [ 'group' . $depth ];
2017-12-14 05:51:56 +08:00
} else if ( isset ( $_SESSION [ 'group' . $depth ] ) ) {
$selected_group_id = $group_id = $_SESSION [ 'group' . $depth ];
2017-12-14 05:15:03 +08:00
} else if ( isset ( $_REQUEST [ 'filtering' ]) ) {
unset ( $_SESSION [ 'group' . $depth ]);
2017-10-05 22:46:04 +08:00
}
foreach ( $Groups as $Group ) {
if ( ! isset ( $groups [ $depth ] ) ) {
$groups [ $depth ] = array ( 0 => 'All' );
}
$groups [ $depth ][ $Group -> Id ()] = $Group -> Name ();
if ( $selected_group_id and ( $selected_group_id == $Group -> Id () ) )
$parent_group_ids [] = $Group -> Id ();
}
2017-12-14 05:15:03 +08:00
echo htmlSelect ( 'group' . $depth , $groups [ $depth ], $selected_group_id , " this.form.submit(); " );
2017-10-05 22:46:04 +08:00
if ( ! count ( $parent_group_ids ) ) break ;
$depth += 1 ;
}
2017-12-14 05:15:03 +08:00
session_write_close ();
2017-10-05 22:46:04 +08:00
return $group_id ;
} # end public static function get_group_dropdowns()
2017-12-05 04:52:16 +08:00
2017-10-05 22:46:04 +08:00
public static function get_group_sql ( $group_id ) {
$groupSql = '' ;
if ( $group_id ) {
2017-12-05 04:52:16 +08:00
$MonitorIds = dbFetchAll ( 'SELECT MonitorId FROM Groups_Monitors WHERE GroupId=?' , 'MonitorId' , array ( $group_id ) );
$MonitorIds = array_merge ( $MonitorIds , dbFetchAll ( 'SELECT MonitorId FROM Groups_Monitors WHERE GroupId IN (SELECT Id FROM Groups WHERE ParentId = ?)' , 'MonitorId' , array ( $group_id ) ) );
$groupSql = " find_in_set( Id, ' " . implode ( ',' , $MonitorIds ) . " ' ) " ;
2017-10-05 22:46:04 +08:00
}
return $groupSql ;
} # end public static function get_group_sql( $group_id )
2017-10-10 22:38:13 +08:00
2017-10-12 22:32:48 +08:00
public static function get_monitors_dropdown ( $options = null ) {
$monitor_id = 0 ;
if ( isset ( $_REQUEST [ 'monitor_id' ] ) ) {
$monitor_id = $_REQUEST [ 'monitor_id' ];
} else if ( isset ( $_COOKIE [ 'zmMonitorId' ]) ) {
$monitor_id = $_COOKIE [ 'zmMonitorId' ];
}
$sql = 'SELECT * FROM Monitors' ;
if ( $options ) {
$sql .= ' WHERE ' . implode ( ' AND ' , array (
( isset ( $options [ 'groupSql' ]) ? $options [ 'groupSql' ] : '' )
) ) . ' ORDER BY Sequence ASC' ;
}
$monitors_dropdown = array ( '' => 'All' );
foreach ( dbFetchAll ( $sql ) as $monitor ) {
if ( ! visibleMonitor ( $monitor [ 'Id' ] ) ) {
continue ;
}
$monitors_dropdown [ $monitor [ 'Id' ]] = $monitor [ 'Name' ];
}
echo htmlSelect ( 'monitor_id' , $monitors_dropdown , $monitor_id , array ( 'onchange' => 'changeMonitor(this);' ) );
return $monitor_id ;
}
2017-10-05 22:46:04 +08:00
} # end class Group
2017-10-12 22:32:48 +08:00
2017-09-24 01:42:39 +08:00
?>