require_once 'session.ppp' ;
require_once 'local.ppp' ;
require_once 'conf.ppp' ;
require_once 'baselib.ppp' ;
require_once 'wwwsec.ppp' ;
require_once 'librepo.ppp' ;
# $DEBUG = 2 ;
get_config () ;
$conn = db_connect_or_die () ;
assert_login () ;
if ( $ADM = GETVAR ( 'ADM' ) ) { switch_actv ( $ADM ) ; }
if ( ! $USER ) { html_exit ( "groups : not authorised" ) ; }
if ( ! Conf ( 'has_groups' ) ) { html_exit ( "groups : not configured" ) ; }
$ACT = GETVAR ( 'ACT' ) ;
$view = 'form' ;
$view_err = '' ;
$view_upd = '' ;
$view_ack = '' ;
function gen_groups_tab ( $one )
{ global $USER, $TBL ;
$qwe = '1 = 0' ;
if ( $one and ( $GID = GETVAR ( 'GID' ) ) )
{ $qwe = "gid = $GID" ; }
elseif ( is_adm () )
{ $qwe = '' ; }
elseif ( $USER )
{ $sub = "SELECT gid FROM {$TBL(repo_group_members)} "
. "WHERE pid = '$USER'" ;
$qwe = "gid IN ( $sub )" ;
}
# echo "qwe ($qwe)
\n" ;
$groups = get_repo_groups ( '', $qwe, 'name' ) ;
if ( ! count ( $groups ) )
{ return UL ( LI ( 'no groups available' ) ) ; }
$res = repo_groups_info_head () ;
foreach ( $groups as $group )
{ $res .= $group -> info_row () ; }
return TAB ( $res ) ;
}
if ( $_POST [ 'CANCEL' ] )
{ $view = 'home' ; }
elseif ( $ACT == 'SHW_GRP' )
{ $view = 'home' ;
}
elseif ( $ACT == 'NEW_GRP_FORM' )
{ if ( ! may_create_repo_group () )
{ html_exit ( "you can't create groups ($USER)" ) ; }
$group = new Repo_group ;
$group -> set_defaults () ;
}
elseif ( $ACT == 'UPD_GRP_FORM' )
{ $GID = GETVAR ( 'GID' ) ;
if ( ! $GID )
{ $view_err .= UL ( LI ( "no GID" ) ) ; $view = 'home' ; }
else
{ $group = get_repo_group ( $GID ) ;
$group -> assert_is_manager () ;
}
}
elseif ( $ACT == 'GET_FORM' )
{ if ( $gid = GETVAR ( 'GID' ) )
{ $group = get_repo_group ( $gid ) ;
$group -> assert_is_manager () ;
}
else
{ $group = new Repo_group ;
$group -> set_defaults () ;
}
if ( $err = $group -> get_form () )
{ $view_err .= $err ; }
elseif ( $group -> gid )
{ $view_upd = $group -> update () ;
$group = $group -> reget () ;
# maybe I removed myself as manager ...
$view = $group -> is_manager () ? 'form' : 'home' ;
}
else
{ $res = $group -> insert () ;
$view_upd .= $res ;
$group = $group -> reget () ;
}
}
elseif ( $ACT == 'DEL' )
{ $GID = GETVAR ( 'GID' ) ;
if ( ! $GID )
{ $view_err .= UL ( LI ( "no GID" ) ) ; $view = 'home' ; }
elseif ( ! $group = try_repo_group ( $GID ) )
{ $view_err .= UL ( LI ( "can't find group $GID" ) ) ; $view = 'home' ; }
else
{ $group -> assert_is_manager () ; $view = 'del_ack' ; }
}
elseif ( $ACT == 'DEL_ACK' )
{ $GID = GETVAR ( 'GID' ) ;
if ( ! $GID )
{ $view_err .= UL ( LI ( "no GID" ) ) ; $view = 'home' ; }
elseif ( ! $group = try_repo_group ( $GID ) )
{ $view_err .= UL ( LI ( "can't find group $GID" ) ) ; $view = 'home' ; }
else
{ $group -> assert_is_manager () ;
if ( $_POST [ 'YES' ] )
{ $view_upd .= $group -> delete () ; $view = 'home' ; }
else
{ $view_upd .= UL ( LI ( RED ( "canceled delete" ) ) ) ; }
}
}
elseif ( $ACT )
{ $view_err .= BLD ( "unknown ACT ($ACT)" ) ; $view = 'home' ; }
else
{ $view = 'home' ; }
html_start ( REPO_TIT ( 'groups' ) ) ;
if ( $view_err )
{ echo H2 ( RED ( 'error' ) )
, BQ ( RED ( "Nothing changed because :\n" . $view_err ) ) ;
}
if ( $view_upd )
{ echo H3 ( 'updates' ) ; echo RED ( $view_upd ) ; }
if ( $view == 'home' )
{ $msg1 = "as a guest, you can't create groups" ;
echo UL ( LI2 ( is_guest (), $msg1, new_repo_group_url () ) ) ;
echo BQ ( gen_groups_tab ( $ACT == 'SHW_GRP' ) ) ;
}
elseif ( $view == 'form' )
{ $is_used = $group -> is_used () ;
$times = ( $is_used == 1 ? 'once' : "$is_used times" ) ;
$name = $group -> name ;
echo H3 ( $group -> gid ? "update $name" : 'create a new group' ) ;
echo UL
( $group -> gid
? LI
( $is_used
? "this group is used $times."
: "this group is not used ; " . $group -> del_url ()
)
: LI
( "name must be lowercase alpha-numeric, prefixed with '@'" )
) ;
echo BQ ( $group -> gen_form ( 'GET_FORM' ) ) ;
}
elseif ( $view == 'del_ack' )
{ echo H4 ( 'confirm' ) ;
echo $group -> gen_form_del_ack ( 'DEL_ACK' ) ;
}
else
{ echo H4 ( 'error' ) . RED ( "unknown view ($view)" ) ; }
html_end () ;
# phpinfo() ;
?>