mirror of
https://gitlab.com/davical-project/davical.git
synced 2026-08-30 18:37:12 +00:00
Add add relationship/delete relationship to the user maintenance.
This commit is contained in:
parent
6fcde6ec08
commit
0f511c2841
@ -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#";
|
||||
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -46,7 +46,7 @@ class RSCDSUser extends User
|
||||
$html .= "<h1>$title</h1>\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 .= "</table>\n";
|
||||
$html .= "</div>";
|
||||
|
||||
if ( $ef->editmode ) {
|
||||
if ( $ef->EditMode ) {
|
||||
$html .= '<div id="footer">';
|
||||
$html .= $ef->SubmitButton( "submit", (("insert" == $this->WriteType) ? "Create" : "Update") );
|
||||
$html .= '</div>';
|
||||
@ -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', "'<a href=\"/user.php?user_no=' || user_no || '\">' || fullname || '</a>'" );
|
||||
$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', '', "'<a class=\"\" href=\"/user.php?action=delete_relationsip&from_user=$this->user_no&to_user=' || user_no || '\">Delete</a>'" );
|
||||
}
|
||||
|
||||
$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, '<a href=\"/user.php?user_no=' || user_no || '\">' || fullname || '</a>' 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( "<tr onMouseover=\"LinkHref(this,1);\" title=\"Click to display that relationship\" class=\"r%d\">\n", "</tr>\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 = <<<EOSQL
|
||||
SELECT user_no, fullname FROM usr
|
||||
WHERE NOT EXISTS ( SELECT 0 FROM relationship
|
||||
WHERE (to_user = usr.user_no AND from_user = $this->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' => '<input type="submit" name="submit" value="Add Relationship" class="fsubmit">'
|
||||
) );
|
||||
}
|
||||
|
||||
$html = ( $title == "" ? "" : $ef->BreakLine($title) );
|
||||
$html .= "<tr><td> </td><td>\n";
|
||||
$html .= $browser->Render();
|
||||
$html .= "</td></tr>\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', "'<a href=\"/user.php?user_no=' || user_no || '\">' || fullname || '</a>'" );
|
||||
$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;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
Loading…
x
Reference in New Issue
Block a user