How to redirect to Detail View if condition matched

Discussions related to customizing hooks. Hooks are documented at http://bigprof.com/appgini/help/advanced-topics/hooks/
Post Reply
ayussuf
Veteran Member
Posts: 39
Joined: 2021-01-20 17:15

How to redirect to Detail View if condition matched

Post by ayussuf » 2021-01-22 10:52

Hello,

I've restricted one data to one user. So, if a user clicks the table's icon in homepage, he will redirected automatically to Detail view for his own data.

Code: Select all

         function Pelajar_init(&$options, $memberInfo, &$args) {

		if($memberInfo['group'] == 'Pelajar') {
			
			//return false;
			
			$memberid1 = ($memberInfo['username']);
			$getrecordcount = sqlValue("select COUNT(tableName) FROM membership_userrecords WHERE tableName = 'Pelajar' AND memberID = '$memberid1' ");
			
			if ($getrecordcount > 0) {
				
				$pkValue = sqlValue("select pkValue FROM membership_userrecords WHERE tableName = 'Pelajar' AND memberID = '$memberid1' ");
				
				//redirect("Pelajar_view.php?SelectedID=".$pkValue);
				header("Location: Pelajar_view.php?SelectedID=".$pkValue);
				//exit;
				return false;
			}
		}
		
		return TRUE;
But the Detail page didn't appear. The error message was:
The page isn’t redirecting properly

An error occurred during a connection to keahlian.woag-malaysia.org.my.

This problem can sometimes be caused by disabling or refusing to accept cookies.
Please help

Thanks.
AppGini 5.92 - Upgraded to 5.94

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: How to redirect to Detail View if condition matched

Post by pbottcher » 2021-01-23 11:45

Hi,

do you get the same if you use the redirect function (which you commented)?
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

ayussuf
Veteran Member
Posts: 39
Joined: 2021-01-20 17:15

Re: How to redirect to Detail View if condition matched

Post by ayussuf » 2021-01-23 16:03

pböttcher wrote:
2021-01-23 11:45
Hi,

do you get the same if you use the redirect function (which you commented)?
Yes pböttcher,

I've tried also redirecting using Javascript... but It couldn't redirect... the difference is the page keep looping.

Then I tried this code....

Code: Select all

function Pelajar_init(&$options, $memberInfo, &$args) {

	if($memberInfo['group'] == 'Pelajar') {
		
		$table = "Pelajar";
		$getrecordcount = sqlValue("select COUNT(tableName) FROM membership_userrecords WHERE tableName = '".$table."' AND memberID = '".$memberInfo['username']."'");			
			
		if ($getrecordcount > 0) {
			
			$recid = sqlValue("select id FROM ".$table." WHERE milik = '".$memberInfo['username']."'");
			
			//redirect($table."_view.php?SelectedID=".$recid);

			return TRUE;
			
		} else {
			
			$tarikh_buat = date("j/m/Y h:i:s a");
			$masuk = "insert into ".$table." (tarikh_buat, milik, nama, nokp, notel, emel) VALUES ('".$tarikh_buat."', '".$memberInfo['username']."', '".$memberInfo['custom'][0]."', '".$memberInfo['custom'][1]."', '".$memberInfo['custom'][2]."', '".$memberInfo['email']."')";
			sql($masuk, $eo);
			
			$rid = sqlValue("select id FROM ".$table." WHERE milik = '".$memberInfo['username']."'");
			set_record_owner($table, $rid, $memberInfo['username']);

			$izin = "INSERT INTO `membership_userpermissions`(`memberID`, `tableName`, `allowInsert`, `allowView`, `allowEdit`, `allowDelete`) VALUES ('" . $memberInfo['username'] . "', '".$table."', '0', '1', '1', '0')";
			sql($izin, $eo);
			
			//this redirection works properly if the above redirection is commented
			redirect($table."_view.php?SelectedID=".$rid);
			
			return TRUE;
		}
		
	}
	
	return TRUE;
}
The second redirection works properly if the first redirection is commented.

I don't know what is the problem of the first redirection... I've tried almost all methods for redirecting using PHP and Javascript, but nothing done.
AppGini 5.92 - Upgraded to 5.94

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: How to redirect to Detail View if condition matched

Post by jsetzer » 2021-01-23 16:56

Did you try exit(); after your redirect instead of returning to AppGini's default procedure?
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: How to redirect to Detail View if condition matched

Post by pbottcher » 2021-01-23 16:58

Hi,

did you check you table permissions, as you check for the group. Has the group permissions to read the table?
In your second part you add an explicit permission to the user.
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

ayussuf
Veteran Member
Posts: 39
Joined: 2021-01-20 17:15

Re: How to redirect to Detail View if condition matched

Post by ayussuf » 2021-01-23 18:40

jsetzer wrote:
2021-01-23 16:56
Did you try exit(); after your redirect instead of returning to AppGini's default procedure?
Still not work

I've change the code to:

Code: Select all

function Pelajar_init(&$options, $memberInfo, &$args) {

	if($memberInfo['group'] == 'Pelajar') {
			
		redirect("Pelajar_view.php?SelectedID=".$recid);
		exit();
	}
	
	return TRUE;
}
Also didn't work
AppGini 5.92 - Upgraded to 5.94

ayussuf
Veteran Member
Posts: 39
Joined: 2021-01-20 17:15

Re: How to redirect to Detail View if condition matched

Post by ayussuf » 2021-01-23 18:41

pböttcher wrote:
2021-01-23 16:58
Hi,

did you check you table permissions, as you check for the group. Has the group permissions to read the table?
In your second part you add an explicit permission to the user.
You mean view permission isn't it? Yes.

But still didn't work
AppGini 5.92 - Upgraded to 5.94

ayussuf
Veteran Member
Posts: 39
Joined: 2021-01-20 17:15

Re: How to redirect to Detail View if condition matched

Post by ayussuf » 2021-01-23 18:47

Code: Select all

function Pelajar_init(&$options, $memberInfo, &$args) {

	if($memberInfo['group'] == 'Pelajar') {
		
		$RecID = sqlValue("select id FROM Pelajar WHERE milik = '".$memberInfo['username']."'");			
		redirect("Pelajar_view.php?SelectedID=".$RecID);
		exit();
	}
	
	return TRUE;
}
This is the new code... nothing change.
Attachments
masalah.png
problem result in firefox
masalah.png (24.93 KiB) Viewed 6476 times
AppGini 5.92 - Upgraded to 5.94

ayussuf
Veteran Member
Posts: 39
Joined: 2021-01-20 17:15

Re: How to redirect to Detail View if condition matched

Post by ayussuf » 2021-01-23 18:51

Actually, using redirect is not important to me... The important thing is I don't want the users to view the table view if they click the table icon in homepage, because they only have one record...so no reason to view table view.

Is there any method to do this?
AppGini 5.92 - Upgraded to 5.94

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: How to redirect to Detail View if condition matched

Post by pbottcher » 2021-01-23 21:31

Can you clear your browser cache (incl. cookies) and try again
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

ayussuf
Veteran Member
Posts: 39
Joined: 2021-01-20 17:15

Re: How to redirect to Detail View if condition matched

Post by ayussuf » 2021-01-23 21:56

pböttcher wrote:
2021-01-23 21:31
Can you clear your browser cache (incl. cookies) and try again
I've clear all my cache including cookies.. and use another browser to test it... but the problem still happen
AppGini 5.92 - Upgraded to 5.94

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: How to redirect to Detail View if condition matched

Post by pbottcher » 2021-01-24 09:10

which browser(s) did you test?
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: How to redirect to Detail View if condition matched

Post by jsetzer » 2021-01-24 10:24

As a starting point:

Alternatives using jquery

(A) replace the link to TV in dashboard by a link to DV

or

(B) after TV has loaded, get the number of rows having attribute data-id. If number equals 1, get the id if that single tr and use window.location.href=... and redirect user to the DV. Backdraw: This will load the tv first, then load the dv.

Both alternatives require additional javascript/jquery coding and it will be tricky.
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: How to redirect to Detail View if condition matched

Post by jsetzer » 2021-01-24 10:41

Alternative (A).

Just replace items by your specific table name and replace 123 by the pk (primary key) of your currently logged in user's record in that table.

Code: Select all

<!-- file: hooks/header-extras.php -->
<?php

if (HOMEPAGE) {

    $tn = "items"; // tablename
    $pk = 123; // get pk of current user's record

    /* dont' touch this unless you know what you are doing here ;-)  */
    echo '<script>$j(document).ready(function(){ let a = $j(\'.table_links > div > .panel > .panel-body > a[href="' . $tn . '_view.php"]\'); if (a.length) a.attr("href", "' . $tn . '_view.php?SelectedID=' . $pk . '");  })</script>';
}

?>
I'm going to post an update in a few minutes...
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: How to redirect to Detail View if condition matched

Post by jsetzer » 2021-01-24 10:56

Alternative (A) Updated

Now with automatic detection of the primary key of the currently logged in user's record* in your tablename ($tn).

Code: Select all

<!-- file: hooks/header-extras.php -->
<?php

if (HOMEPAGE) {
    
    /* Change homepage link:
     * redirect user to specific record rather than to TV
     * with automatic detection of pk.
     * this will fetch the pk of the latest record created by current user
     */

     // change your tablename
    $tn = "items"; 

    /* don't touch the code below unless you know what you are doing ;-) */
    $pk = sqlValue("SELECT pkValue FROM membership_userrecords WHERE tableName='" . makeSafe($tn) . "' AND memberID='" . getLoggedMemberID() . "' ORDER BY pkValue DESC LIMIT 1");
    if ($pk !== false) echo '<script>$j(document).ready(function(){ let a = $j(\'.table_links > div > .panel > .panel-body > a[href="' . $tn . '_view.php"]\'); if (a.length) a.attr("href", "' . $tn . '_view.php?SelectedID=' . $pk . '");  })</script>';
}

?>
* Note: If you allow anonymous users, you may have to disable this functionality for guest-users.
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

ayussuf
Veteran Member
Posts: 39
Joined: 2021-01-20 17:15

Re: How to redirect to Detail View if condition matched

Post by ayussuf » 2021-01-24 11:07

pböttcher wrote:
2021-01-24 09:10
which browser(s) did you test?
After clearing the cache, I've used the browser (firefox).... and also used another browser (edge)
AppGini 5.92 - Upgraded to 5.94

ayussuf
Veteran Member
Posts: 39
Joined: 2021-01-20 17:15

Re: How to redirect to Detail View if condition matched

Post by ayussuf » 2021-01-24 11:33

jsetzer wrote:
2021-01-24 10:56
Alternative (A) Updated

Now with automatic detection of the primary key of the currently logged in user's record* in your tablename ($tn).

Code: Select all

<!-- file: hooks/header-extras.php -->
<?php

if (HOMEPAGE) {
    
    /* Change homepage link:
     * redirect user to specific record rather than to TV
     * with automatic detection of pk.
     * this will fetch the pk of the latest record created by current user
     */

     // change your tablename
    $tn = "items"; 

    /* don't touch the code below unless you know what you are doing ;-) */
    $pk = sqlValue("SELECT pkValue FROM membership_userrecords WHERE tableName='" . makeSafe($tn) . "' AND memberID='" . getLoggedMemberID() . "' ORDER BY pkValue DESC LIMIT 1");
    if ($pk !== false) echo '<script>$j(document).ready(function(){ let a = $j(\'.table_links > div > .panel > .panel-body > a[href="' . $tn . '_view.php"]\'); if (a.length) a.attr("href", "' . $tn . '_view.php?SelectedID=' . $pk . '");  })</script>';
}

?>
* Note: If you allow anonymous users, you may have to disable this functionality for guest-users.
It works perfect... thank you very much jsetzer ...
AppGini 5.92 - Upgraded to 5.94

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: How to redirect to Detail View if condition matched

Post by jsetzer » 2021-01-24 11:48

Great, happy that, finally, we got your very specific requirement solved!
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

ayussuf
Veteran Member
Posts: 39
Joined: 2021-01-20 17:15

Re: How to redirect to Detail View if condition matched

Post by ayussuf » 2021-01-24 11:56

Is $memberInfo['group'] can be called in header-extras.php ?

Actually, my tables name that I'll use for this purpose is same as group name.

So, rather than using $tn = "items", I want to use $tn= $memberInfo['group'].

But it didn't work
AppGini 5.92 - Upgraded to 5.94

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: How to redirect to Detail View if condition matched

Post by jsetzer » 2021-01-24 12:07

$mi = getMemberInfo();

var_dump($mi);
exit;
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

ayussuf
Veteran Member
Posts: 39
Joined: 2021-01-20 17:15

Re: How to redirect to Detail View if condition matched

Post by ayussuf » 2021-01-24 12:43

jsetzer wrote:
2021-01-24 12:07
$mi = getMemberInfo();

var_dump($mi);
exit;
I can get the var_dump result

Code: Select all

["group"]=> string(8) "Penyelia"
But if I want to echo $memberInfo['group']; nothing appear.

What is the problem?
AppGini 5.92 - Upgraded to 5.94

ayussuf
Veteran Member
Posts: 39
Joined: 2021-01-20 17:15

Re: How to redirect to Detail View if condition matched

Post by ayussuf » 2021-01-24 13:13

ayussuf wrote:
2021-01-24 12:43
jsetzer wrote:
2021-01-24 12:07
$mi = getMemberInfo();

var_dump($mi);
exit;
I can get the var_dump result

Code: Select all

["group"]=> string(8) "Penyelia"
But if I want to echo $memberInfo['group']; nothing appear.

What is the problem?
I got it... I used getMemberInfo()["group"]. It works
AppGini 5.92 - Upgraded to 5.94

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: How to redirect to Detail View if condition matched

Post by jsetzer » 2021-01-24 13:17

$mi["group"]

Get used to working with associative arrays in PHP. You will need it very often when customizing AppGini generated web apps.
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

ayussuf
Veteran Member
Posts: 39
Joined: 2021-01-20 17:15

Re: How to redirect to Detail View if condition matched

Post by ayussuf » 2021-01-24 13:20

jsetzer wrote:
2021-01-24 13:17
$mi["group"]

Get used to working with associative arrays in PHP. You will need it very often when customizing AppGini generated web apps.
Thank you very much jsetzer
AppGini 5.92 - Upgraded to 5.94

Post Reply