diff --git a/htdocs/relationships.php b/htdocs/relationships.php index 2ca0d699..bad414af 100644 --- a/htdocs/relationships.php +++ b/htdocs/relationships.php @@ -31,8 +31,7 @@ require_once("interactive-page.php"); $browser->DoQuery(); -// if ( $session->AllowedTo("Support") ) - $relationship_menu->AddOption("New Relationship","/relationship.php?create","Add a new relationship", false, 10); + $user_menu->AddOption("Relationships","/relationship.php?user_no=$user_no","Relationships for this user", false, 10); $active_menu_pattern = "#^/relationship#"; diff --git a/htdocs/user.php b/htdocs/user.php index 8fd3281b..2290576f 100644 --- a/htdocs/user.php +++ b/htdocs/user.php @@ -18,20 +18,31 @@ } $show = 0; - if ( !$session->just_logged_in && isset($_POST['submit']) ) { + if ( !$session->just_logged_in && (isset($_POST['submit']) || isset($_GET['action'])) ) { if ( $session->AllowedTo("Admin") || $session->AllowedTo("Support") || ($user->user_no > 0 && $user->user_no == $session->user_no) ) { $session->Log( "DBG: Record %s write type is %s.", $user->Table, $user->WriteType ); - $user->PostToValues(); - if ( $user->Validate() ) { - $user->Write(); - $user = new User($user->user_no); - $user->EditMode = true; - if ( $user->user_no == 0 ) { - $c->page_title = ( $user_no != "" ? "User Unavailable" : "New User" ); + if ( isset($_POST['submit']) ) { + $user->PostToValues(); + if ( $user->Validate() ) { + $user->Write(); + $user = new RSCDSUser($user->user_no); + $user->EditMode = true; + if ( $user->user_no == 0 ) { + $c->page_title = ( $user_no != "" ? "User Unavailable" : "New User" ); + } + else { + $c->page_title = $user->Get("user_no"). " - " . $user->Get("fullname"); + } } else { - $c->page_title = $user->Get("user_no"). " - " . $user->Get("fullname"); + /** + * Handle any actions, such as 'delete_relation' + */ + if ( $user->HandleAction($_GET['action']) ) { + $user = new RSCDSUser($user->user_no); + $user->EditMode = true; + } } } } diff --git a/inc/RSCDSUser.php b/inc/RSCDSUser.php index d3d38aca..79e175cc 100644 --- a/inc/RSCDSUser.php +++ b/inc/RSCDSUser.php @@ -46,7 +46,7 @@ class RSCDSUser extends User $html .= "

$title

\n"; } - if ( $ef->editmode ) { + if ( $ef->EditMode ) { $html .= $ef->StartForm( array("autocomplete" => "off" ) ); if ( $this->user_no > 0 ) $html .= $ef->HiddenField( "user_no", $this->user_no ); } @@ -57,12 +57,13 @@ class RSCDSUser extends User $html .= $this->RenderRoles($ef); - $html .= $this->RenderRelationships($ef); + $html .= $this->RenderRelationshipsFrom($ef); + $html .= $this->RenderRelationshipsTo($ef); $html .= "\n"; $html .= ""; - if ( $ef->editmode ) { + if ( $ef->EditMode ) { $html .= ''; @@ -78,7 +79,7 @@ class RSCDSUser extends User * * @return string The string of html to be output */ - function RenderRelationships( $ef, $title = "User Relationships" ) { + function RenderRelationshipsFrom( $ef, $title = "Relationships from this user" ) { global $session, $c; $browser = new Browser(""); @@ -86,14 +87,87 @@ class RSCDSUser extends User $browser->AddHidden( 'user_link', "'' || fullname || ''" ); $browser->AddColumn( 'rt_name', 'Relationship' ); $browser->AddColumn( 'fullname', 'Linked To', 'left', '##user_link##' ); - $browser->AddColumn( 'rt_isgroup', 'Group?' ); +// $browser->AddColumn( 'is_group', 'Group?', 'centre', '', "CASE WHEN rt_isgroup THEN 'Yes' ELSE 'No' END" ); $browser->AddHidden( 'confers', 'Confers' ); $browser->AddColumn( 'email', 'EMail' ); + if ( $ef->EditMode ) { // && $session->AllowedTo("MaintainRelationships") ) { + $browser->AddColumn( 'delete', 'Delete', 'centre', '', "'user_no&to_user=' || user_no || '\">Delete'" ); + } $browser->SetJoins( 'relationship NATURAL JOIN relationship_type rt LEFT JOIN usr ON (to_user = user_no)' ); $browser->SetWhere( "from_user = $this->user_no" ); - $browser->SetUnion("SELECT rt.rt_name, fullname, rt.rt_isgroup, email, '' || fullname || '' AS user_link, rt.confers AS confers FROM relationship NATURAL JOIN relationship_type rt1 LEFT JOIN relationship_type rt ON (rt.rt_id = rt1.rt_inverse) LEFT JOIN usr ON (from_user = user_no) WHERE to_user = $this->user_no "); + if ( isset( $_GET['o']) && isset($_GET['d']) ) { + $browser->AddOrder( $_GET['o'], $_GET['d'] ); + } + else + $browser->AddOrder( 'rt_name', 'A' ); + + $browser->RowFormat( "\n", "\n", '#even' ); + $browser->DoQuery(); + + /** + * Present an extra editable row at the bottom of the browse. + */ + if ( $ef->EditMode ) { // && $session->AllowedTo("MaintainRelationships") ) { + if ( isset($this->roles['Group Target']) ) { + /** + * We only allow individuals to link to group targets at this stage. + */ + $group_target = 'AND NOT EXISTS (SELECT 1 FROM role_member WHERE role_no = 2 AND user_no=usr.user_no)'; + } + $sql = <<user_no) + OR (from_user = usr.user_no AND to_user = $this->user_no)) + $group_target +EOSQL; + $person_selection = $ef->DataEntryField( "", "lookup", "relate_to", + array("title" => "Select the user, resource or group to relate this user to", + "_null" => "--- select a user ".( isset($this->roles['Group Target']) ? '' : ', group ' ).'or resource ---', + "_sql" => $sql ) ); + + $group_target = ( isset($this->roles['Group Target']) ? 'WHERE NOT rt_isgroup' : '' ); + $relationship_type_selection = $ef->DataEntryField( "", "lookup", "relate_as", + array("title" => "Select the type of relationship from this user", + "_null" => "--- select a relationship type ---", + "_sql" => "SELECT rt_id, rt_name FROM relationship_type $group_target " ) ); + + $browser->AddRow( array( + 'rt_name' => $relationship_type_selection, /* Since 'fullname' is formatted to display this value */ + 'user_link' => $person_selection, + 'delete' => '' + ) ); + } + + $html = ( $title == "" ? "" : $ef->BreakLine($title) ); + $html .= " \n"; + $html .= $browser->Render(); + $html .= "\n"; + + return $html; + } + + /** + * Render the user's relationships to other users & resources + * + * @return string The string of html to be output + */ + function RenderRelationshipsTo( $ef, $title = "Relationships to this user" ) { + global $session, $c; + + $browser = new Browser(""); + + $browser->AddHidden( 'user_link', "'' || fullname || ''" ); + $browser->AddColumn( 'fullname', 'Linked From', 'left', '##user_link##' ); + $browser->AddColumn( 'rt_name', 'Relationship' ); + $browser->AddColumn( 'is_group', 'Group?', 'centre', '', "CASE WHEN rt_isgroup THEN 'Yes' ELSE 'No' END" ); + $browser->AddHidden( 'confers', 'Confers' ); + $browser->AddColumn( 'email', 'EMail' ); + + $browser->SetJoins( 'relationship NATURAL JOIN relationship_type rt LEFT JOIN usr ON (from_user = user_no)' ); + $browser->SetWhere( "to_user = $this->user_no" ); if ( isset( $_GET['o']) && isset($_GET['d']) ) { $browser->AddOrder( $_GET['o'], $_GET['d'] ); @@ -112,6 +186,64 @@ class RSCDSUser extends User return $html; } + /** + * Validate the information the user submitted + * @return boolean Whether the form data validated OK. + */ + function Validate( ) { + return parent::Validate( ); + } + + + /** + * Handle any unusual actions we might invent + */ + function HandleAction( $action ) { + global $session; + + switch( $action ) { + case 'delete_relation': + if ( $this->AllowedTo("Admin") ) { + dbg_error_log("User",":HandleAction: Deleting relationship from %d to %d", $this->user_no, $_POST['to_user'] ); + $qry = new PgQuery("DELETE FROM relationship WHERE from_user=? AND to_user=?;", $this->user_no, $_POST['to_user'] ); + if ( $qry->Exec() ) { + $c->messages[] = "Relationship deleted"; + } + else { + $c->messages[] = "There was an error writing to the database."; + return false; + } + } + return true; + + default: + return false; + } + } + + + /** + * Write the record to the file + */ + function Write( ) { + global $session; + + if ( parent::Write() ) { + if ( $this->AllowedTo("Admin") && isset($_POST['relate_to']) && isset($_POST['relate_as']) && isset($_POST['submit']) && $_POST['submit'] == 'Add Relationship' ) { + dbg_error_log("User",":Write: Adding relationship as %d to %d", $_POST['relate_as'], isset($_POST['relate_to'] ) ); + $qry = new PgQuery("INSERT INTO relationship (from_user, to_user, rt_id ) VALUES( $this->user_no, ?, ? )", $_POST['relate_to'], $_POST['relate_as'] ); + if ( $qry->Exec() ) { + $c->messages[] = "Relationship added."; + } + else { + $c->messages[] = "There was an error writing to the database."; + return false; + } + } + return true; + } + return false; + } } ?> \ No newline at end of file