2017-09-24 01:42:39 +08:00
< ? php
2019-02-22 22:19:07 +08:00
namespace ZM ;
2017-09-24 01:42:39 +08:00
2019-08-09 01:51:56 +08:00
class Group extends ZM_Object {
protected static $table = 'Groups' ;
protected $defaults = array (
2018-04-04 01:36:14 +08:00
'Id' => null ,
'Name' => '' ,
'ParentId' => null ,
);
2017-09-24 01:42:39 +08:00
2019-08-09 01:51:56 +08:00
public static function find ( $parameters = array (), $options = array () ) {
return ZM_Object :: _find ( get_class (), $parameters , $options );
2017-09-24 01:42:39 +08:00
}
2019-08-09 01:51:56 +08:00
public static function find_one ( $parameters = array (), $options = array () ) {
return ZM_Object :: _find_one ( get_class (), $parameters , $options );
}
2017-09-24 01:42:39 +08:00
public function delete () {
2019-12-08 00:45:32 +08:00
if ( property_exists ( $this , 'Id' ) ) {
2020-06-04 23:44:59 +08:00
dbQuery ( 'DELETE FROM `Groups_Monitors` WHERE `GroupId`=?' , array ( $this -> { 'Id' }));
dbQuery ( 'UPDATE `Groups` SET `ParentId`=NULL WHERE `ParentId`=?' , array ( $this -> { 'Id' }));
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()
2017-10-05 04:40:09 +08:00
public function depth ( $new = null ) {
if ( isset ( $new ) ) {
$this -> { 'depth' } = $new ;
}
2019-12-08 00:45:32 +08:00
if ( ! property_exists ( $this , 'depth' ) or ( $this -> { 'depth' } === null ) ) {
2018-12-24 22:38:55 +08:00
$this -> { 'depth' } = 0 ;
2017-10-05 04:40:09 +08:00
if ( $this -> { 'ParentId' } != null ) {
2018-04-18 00:36:35 +08:00
$Parent = Group :: find_one ( array ( 'Id' => $this -> { 'ParentId' }));
2018-12-24 22:38:55 +08:00
$this -> { 'depth' } += $Parent -> depth () + 1 ;
2017-10-05 04:40:09 +08:00
}
}
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 ( ) {
2019-12-08 00:45:32 +08:00
if ( ! property_exists ( $this , 'MonitorIds' ) ) {
2020-06-04 23:44:59 +08:00
$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
$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' ]) ) {
2019-08-20 21:46:53 +08:00
zm_session_start ();
2018-01-13 03:25:15 +08:00
unset ( $_SESSION [ 'groups' ]);
2019-08-20 21:46:53 +08:00
session_write_close ();
2018-01-13 03:25:15 +08:00
}
2020-06-04 23:44:59 +08:00
return htmlSelect ( 'GroupId[]' , Group :: get_dropdown_options (), isset ( $_SESSION [ 'GroupId' ]) ? $_SESSION [ 'GroupId' ] : null , array (
2019-01-16 22:59:58 +08:00
'data-on-change' => 'submitThisForm' ,
2018-02-27 08:10:10 +08:00
'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 ();
2019-10-01 03:02:05 +08:00
foreach ( Group :: find ( array (), array ( 'order' => 'lower(Name)' )) as $Group ) {
2021-03-30 23:51:52 +08:00
if ( $Group -> canView ()) $Groups [ $Group -> Id ()] = $Group ;
2018-01-13 03:25:15 +08:00
}
# 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-12-24 22:38:55 +08:00
} # end foreach
2018-01-13 03:25:15 +08:00
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 ;
2018-12-24 22:38:55 +08:00
} # end function get_options
2018-01-13 03:25:15 +08:00
$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-12-24 22:38:55 +08:00
} # end function get_dropdown_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 ) ) . ')' ;
2019-08-16 04:04:37 +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
2019-08-16 04:04:37 +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 {
2019-08-16 04:04:37 +08:00
$MonitorIds = dbFetchAll ( 'SELECT `MonitorId` FROM `Groups_Monitors` WHERE `GroupId`=?' , 'MonitorId' , array ( $group_id ));
2018-01-13 03:25:15 +08:00
2019-08-16 04:04:37 +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 ) {
2019-08-16 04:04:37 +08:00
$monitor_id = 0 ;
if ( isset ( $_REQUEST [ 'monitor_id' ]) ) {
$monitor_id = $_REQUEST [ 'monitor_id' ];
} else if ( isset ( $_COOKIE [ 'zmMonitorId' ]) ) {
$monitor_id = $_COOKIE [ 'zmMonitorId' ];
}
$sql = 'SELECT `Id`,`Name` FROM `Monitors`' ;
2017-10-12 22:32:48 +08:00
if ( $options ) {
$sql .= ' WHERE ' . implode ( ' AND ' , array (
( isset ( $options [ 'groupSql' ]) ? $options [ 'groupSql' ] : '' )
2019-08-16 04:04:37 +08:00
) ) . ' ORDER BY `Sequence` ASC' ;
2017-10-12 22:32:48 +08:00
}
$monitors_dropdown = array ( '' => 'All' );
2021-03-30 23:51:52 +08:00
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 ( 'data-on-change-this' => 'changeMonitor' ));
return $monitor_id ;
}
public function Parent ( ) {
if ( $this -> { 'ParentId' } ) {
return Group :: find_one ( array ( 'Id' => $this -> { 'ParentId' }));
}
return null ;
}
public function Parents () {
$Parents = array ();
$Parent = $this -> Parent ();
2021-12-04 01:03:23 +08:00
$seen_parents = array ();
while ( $Parent ) {
$seen_parents [ $Parent -> Id ()] = $Parent ;
2021-03-30 23:51:52 +08:00
array_unshift ( $Parents , $Parent );
$Parent = $Parent -> Parent ();
2021-12-04 01:03:23 +08:00
if ( $Parent and isset ( $seen_parents [ $Parent -> Id ()])) {
Warning ( " Detected hierarchy loop in group { $Parent -> Name () } " );
break ;
}
2021-03-30 23:51:52 +08:00
}
return $Parents ;
}
public function Children () {
if ( ! property_exists ( $this , 'Children' )) {
$this -> { 'Children' } = Group :: find ( array ( 'ParentId' => $this -> Id ()));
}
return $this -> { 'Children' };
}
public function Monitors () {
if ( ! property_exists ( $this , 'Monitors' ) ) {
$this -> { 'Monitors' } = Monitor :: find ( array ( 'Id' => $this -> MonitorIds ()));
}
return $this -> { 'Monitors' };
}
public function canView ( $u = null ) {
global $user ;
if ( ! $u ) $u = $user ;
2021-12-04 01:03:23 +08:00
if ( ! count ( $this -> Monitors ()) and ! count ( $this -> Children ())) {
return true ;
}
2021-03-30 23:51:52 +08:00
# Can view if we can view any of the monitors in it.
foreach ( $this -> Monitors () as $monitor ) {
if ( $monitor -> canView ( $u )) return true ;
}
foreach ( $this -> Children () as $child ) {
if ( $child -> canView ( $u )) return true ;
}
return false ;
}
2017-10-05 22:46:04 +08:00
} # end class Group
2017-09-24 01:42:39 +08:00
?>