2017-09-24 01:42:39 +08:00
< ? php
2018-04-11 04:05:37 +08:00
$group_cache = array ();
2017-09-24 01:42:39 +08:00
class Group {
2018-04-04 01:36:14 +08:00
public $defaults = array (
'Id' => null ,
'Name' => '' ,
'ParentId' => null ,
);
2017-09-24 01:42:39 +08:00
public function __construct ( $IdOrRow = NULL ) {
2018-04-11 04:05:37 +08:00
global $group_cache ;
2017-09-24 01:42:39 +08:00
$row = NULL ;
if ( $IdOrRow ) {
2018-04-04 01:36:14 +08:00
if ( is_integer ( $IdOrRow ) or is_numeric ( $IdOrRow ) ) {
$row = dbFetchOne ( 'SELECT * FROM Groups WHERE Id=?' , NULL , array ( $IdOrRow ));
2017-09-24 01:42:39 +08:00
if ( ! $row ) {
2018-04-04 01:36:14 +08:00
Error ( 'Unable to load Group record for Id=' . $IdOrRow );
2017-09-24 01:42:39 +08:00
}
2018-04-18 00:36:35 +08:00
} elseif ( is_array ( $IdOrRow ) ) {
2017-09-24 01:42:39 +08:00
$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 ;
}
2018-04-18 00:36:35 +08:00
$group_cache [ $row [ 'Id' ]] = $this ;
2017-09-24 01:42:39 +08:00
}
} // end function __construct
2018-04-11 04:05:37 +08:00
public function __call ( $fn , array $args ) {
if ( count ( $args ) ) {
2017-09-24 01:42:39 +08:00
$this -> { $fn } = $args [ 0 ];
}
2018-04-11 04:05:37 +08:00
if ( array_key_exists ( $fn , $this ) ) {
2017-09-24 01:42:39 +08:00
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 " );
}
}
2018-09-08 04:31:11 +08:00
public static function find ( $parameters = null , $options = null ) {
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 '?' ;};
2018-09-08 04:31:11 +08:00
$fields [] = $field . ' IN (' . implode ( ',' , array_map ( $func , $value )) . ')' ;
2017-10-05 04:40:09 +08:00
$values += $value ;
2017-10-01 02:19:32 +08:00
} else {
$fields [] = $field . '=?' ;
$values [] = $value ;
}
}
2018-09-08 04:31:11 +08:00
$sql .= implode ( ' AND ' , $fields );
} # end if parameters
if ( $options ) {
if ( isset ( $options [ 'order' ]) ) {
$sql .= ' ORDER BY ' . $options [ 'order' ];
}
if ( isset ( $options [ 'limit' ]) ) {
if ( is_integer ( $options [ 'limit' ]) or ctype_digit ( $options [ 'limit' ]) ) {
$sql .= ' LIMIT ' . $limit ;
} else {
$backTrace = debug_backtrace ();
$file = $backTrace [ 1 ][ 'file' ];
$line = $backTrace [ 1 ][ 'line' ];
Error ( " Invalid value for limit( $limit ) passed to Group::find from $file : $line " );
2018-09-10 01:59:10 +08:00
return array ();
2018-09-08 04:31:11 +08:00
}
}
} # end if options
$groups = array ();
2017-10-01 02:19:32 +08:00
$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 ) {
2018-09-08 04:31:11 +08:00
$groups [] = $obj ;
}
return $groups ;
} # end find()
public static function find_one ( $parameters = null , $options = null ) {
global $group_cache ;
if (
( count ( $parameters ) == 1 ) and
isset ( $parameters [ 'Id' ]) and
isset ( $group_cache [ $parameters [ 'Id' ]]) ) {
return $group_cache [ $parameters [ 'Id' ]];
}
$results = Group :: find ( $parameters , $options );
if ( count ( $results ) > 1 ) {
Error ( " Group::find_one Returned more than 1 " );
return $results [ 0 ];
} else if ( count ( $results ) ) {
return $results [ 0 ];
} else {
return null ;
2017-09-24 01:42:39 +08:00
}
}
public function delete () {
2018-04-18 00:36:35 +08:00
if ( array_key_exists ( 'Id' , $this ) ) {
2017-12-05 04:52:16 +08:00
dbQuery ( 'DELETE FROM Groups_Monitors WHERE GroupId=?' , array ( $this -> { 'Id' }) );
2018-04-18 00:36:35 +08:00
dbQuery ( 'DELETE FROM Groups WHERE Id=?' , array ( $this -> { 'Id' }) );
2017-10-10 22:38:13 +08:00
if ( isset ( $_COOKIE [ 'zmGroup' ]) ) {
if ( $this -> { 'Id' } == $_COOKIE [ 'zmGroup' ] ) {
2018-04-18 00:36:35 +08:00
unset ( $_COOKIE [ 'zmGroup' ]);
setcookie ( 'zmGroup' , '' , time () - 3600 * 24 * 2 );
2017-10-10 22:38:13 +08:00
}
}
}
2017-09-24 01:42:39 +08:00
} # end function delete()
public function set ( $data ) {
foreach ( $data as $k => $v ) {
2018-04-18 00:36:35 +08:00
if ( is_array ( $v ) ) {
2017-09-24 01:42:39 +08:00
$this -> { $k } = $v ;
2018-04-18 00:36:35 +08:00
} else if ( is_string ( $v ) ) {
2017-09-24 01:42:39 +08:00
$this -> { $k } = trim ( $v );
2018-04-18 00:36:35 +08:00
} else if ( is_integer ( $v ) ) {
2017-09-24 01:42:39 +08:00
$this -> { $k } = $v ;
2018-04-18 00:36:35 +08:00
} else if ( is_bool ( $v ) ) {
2017-09-24 01:42:39 +08:00
$this -> { $k } = $v ;
} else {
2018-04-18 00:36:35 +08:00
Error ( " Unknown type $k => $v of var " . gettype ( $v ));
2017-09-24 01:42:39 +08:00
$this -> { $k } = $v ;
}
}
}
2017-10-05 04:40:09 +08:00
public function depth ( $new = null ) {
if ( isset ( $new ) ) {
$this -> { 'depth' } = $new ;
}
2018-04-18 00:36:35 +08:00
if ( ! array_key_exists ( 'depth' , $this ) or ( $this -> { 'depth' } == null ) ) {
2017-10-05 04:40:09 +08:00
$this -> { 'depth' } = 1 ;
if ( $this -> { 'ParentId' } != null ) {
2018-04-18 00:36:35 +08:00
$Parent = Group :: find_one ( array ( 'Id' => $this -> { 'ParentId' }));
2017-10-05 04:40:09 +08:00
$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 ( ) {
2018-04-18 00:36:35 +08:00
if ( ! array_key_exists ( 'MonitorIds' , $this ) ) {
$this -> { 'MonitorIds' } = dbFetchAll ( 'SELECT MonitorId FROM Groups_Monitors WHERE GroupId=?' , 'MonitorId' , array ( $this -> { 'Id' }));
2017-12-05 04:52:16 +08:00
}
return $this -> { 'MonitorIds' };
}
2018-02-27 08:10:10 +08:00
public static function get_group_dropdown ( ) {
2018-01-13 03:25:15 +08:00
session_start ();
$selected_group_id = 0 ;
if ( isset ( $_REQUEST [ 'groups' ]) ) {
$selected_group_id = $group_id = $_SESSION [ 'groups' ] = $_REQUEST [ 'groups' ];
} else if ( isset ( $_SESSION [ 'groups' ] ) ) {
$selected_group_id = $group_id = $_SESSION [ 'groups' ];
} else if ( isset ( $_REQUEST [ 'filtering' ]) ) {
unset ( $_SESSION [ 'groups' ]);
}
session_write_close ();
2018-02-27 08:10:10 +08:00
return htmlSelect ( 'Group[]' , Group :: get_dropdown_options (), isset ( $_SESSION [ 'Group' ]) ? $_SESSION [ 'Group' ] : null , array (
'onchange' => 'this.form.submit();' ,
'class' => 'chosen' ,
'multiple' => 'multiple' ,
'data-placeholder' => 'All' ,
) );
} # end public static function get_group_dropdown
public static function get_dropdown_options () {
2018-01-13 03:25:15 +08:00
$Groups = array ();
2018-09-08 04:31:11 +08:00
foreach ( Group :: find ( ) as $Group ) {
2018-01-13 03:25:15 +08:00
$Groups [ $Group -> Id ()] = $Group ;
}
# This array is indexed by parent_id
2018-02-27 08:10:10 +08:00
global $children ;
2018-01-13 03:25:15 +08:00
$children = array ();
foreach ( $Groups as $id => $Group ) {
if ( $Group -> ParentId () != null ) {
if ( ! isset ( $children [ $Group -> ParentId ()] ) )
$children [ $Group -> ParentId ()] = array ();
$children [ $Group -> ParentId ()][] = $Group ;
}
}
2018-04-18 00:36:35 +08:00
function get_options ( $Group ) {
2018-01-13 03:25:15 +08:00
global $children ;
2018-04-18 00:36:35 +08:00
$options = array ( $Group -> Id () => str_repeat ( ' ' , $Group -> depth ()) . $Group -> Name ());
2018-01-13 03:25:15 +08:00
if ( isset ( $children [ $Group -> Id ()]) ) {
foreach ( $children [ $Group -> Id ()] as $child ) {
2018-04-18 00:36:35 +08:00
$options += get_options ( $child );
2018-01-13 03:25:15 +08:00
}
}
return $options ;
}
$group_options = array ();
foreach ( $Groups as $id => $Group ) {
if ( ! $Group -> ParentId () ) {
2018-04-19 00:30:32 +08:00
$group_options += get_options ( $Group );
2018-01-13 03:25:15 +08:00
}
}
2018-02-27 08:10:10 +08:00
return $group_options ;
}
2018-01-13 03:25:15 +08:00
2018-04-18 00:36:35 +08:00
public static function get_group_sql ( $group_id ) {
2017-10-05 22:46:04 +08:00
$groupSql = '' ;
if ( $group_id ) {
2018-04-18 00:36:35 +08:00
if ( is_array ( $group_id ) ) {
2018-01-13 03:25:15 +08:00
$group_id_sql_part = ' IN (' . implode ( ',' , array_map ( function (){ return '?' ;}, $group_id ) ) . ')' ;
2018-04-18 00:36:35 +08:00
$MonitorIds = dbFetchAll ( 'SELECT MonitorId FROM Groups_Monitors WHERE GroupId' . $group_id_sql_part , 'MonitorId' , $group_id );
2017-12-05 04:52:16 +08:00
2018-04-18 00:36:35 +08:00
$MonitorIds = array_merge ( $MonitorIds , dbFetchAll ( 'SELECT MonitorId FROM Groups_Monitors WHERE GroupId IN (SELECT Id FROM Groups WHERE ParentId' . $group_id_sql_part . ')' , 'MonitorId' , $group_id ));
2018-01-13 03:25:15 +08:00
} else {
2018-04-18 00:36:35 +08:00
$MonitorIds = dbFetchAll ( 'SELECT MonitorId FROM Groups_Monitors WHERE GroupId=?' , 'MonitorId' , array ( $group_id ));
2018-01-13 03:25:15 +08:00
2018-04-18 00:36:35 +08:00
$MonitorIds = array_merge ( $MonitorIds , dbFetchAll ( 'SELECT MonitorId FROM Groups_Monitors WHERE GroupId IN (SELECT Id FROM Groups WHERE ParentId = ?)' , 'MonitorId' , array ( $group_id )));
2018-01-13 03:25:15 +08:00
}
2018-04-18 00:36:35 +08:00
$groupSql = " find_in_set( M.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
2018-04-18 00:36:35 +08:00
public static function get_monitors_dropdown ( $options = null ) {
2017-10-12 22:32:48 +08:00
$monitor_id = 0 ;
2018-04-18 00:36:35 +08:00
if ( isset ( $_REQUEST [ 'monitor_id' ]) ) {
2017-10-12 22:32:48 +08:00
$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' );
2018-04-18 00:36:35 +08:00
foreach ( dbFetchAll ( $sql ) as $monitor ) {
if ( ! visibleMonitor ( $monitor [ 'Id' ]) ) {
2017-10-12 22:32:48 +08:00
continue ;
}
$monitors_dropdown [ $monitor [ 'Id' ]] = $monitor [ 'Name' ];
}
2018-04-18 00:36:35 +08:00
echo htmlSelect ( 'monitor_id' , $monitors_dropdown , $monitor_id , array ( 'onchange' => 'changeMonitor(this);' ));
2017-10-12 22:32:48 +08:00
return $monitor_id ;
}
2018-03-02 11:23:03 +08:00
public function Parent ( ) {
if ( $this -> { 'ParentId' } ) {
2018-04-11 04:05:37 +08:00
return Group :: find_one ( array ( 'Id' => $this -> { 'ParentId' }));
2018-03-02 11:23:03 +08:00
}
return null ;
}
public function Parents () {
$Parents = array ();
$Parent = $this -> Parent ();
while ( $Parent ) {
array_unshift ( $Parents , $Parent );
$Parent = $Parent -> Parent ();
}
return $Parents ;
}
2017-10-05 22:46:04 +08:00
} # end class Group
2017-09-24 01:42:39 +08:00
?>