Page 1 of 1
How to redirect to Detail View if condition matched
Posted: 2021-01-22 10:52
by ayussuf
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.
Re: How to redirect to Detail View if condition matched
Posted: 2021-01-23 11:45
by pbottcher
Hi,
do you get the same if you use the redirect function (which you commented)?
Re: How to redirect to Detail View if condition matched
Posted: 2021-01-23 16:03
by ayussuf
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.
Re: How to redirect to Detail View if condition matched
Posted: 2021-01-23 16:56
by jsetzer
Did you try exit(); after your redirect instead of returning to AppGini's default procedure?
Re: How to redirect to Detail View if condition matched
Posted: 2021-01-23 16:58
by pbottcher
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.
Re: How to redirect to Detail View if condition matched
Posted: 2021-01-23 18:40
by ayussuf
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
Re: How to redirect to Detail View if condition matched
Posted: 2021-01-23 18:41
by ayussuf
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
Re: How to redirect to Detail View if condition matched
Posted: 2021-01-23 18:47
by ayussuf
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.
Re: How to redirect to Detail View if condition matched
Posted: 2021-01-23 18:51
by ayussuf
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?
Re: How to redirect to Detail View if condition matched
Posted: 2021-01-23 21:31
by pbottcher
Can you clear your browser cache (incl. cookies) and try again
Re: How to redirect to Detail View if condition matched
Posted: 2021-01-23 21:56
by ayussuf
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
Re: How to redirect to Detail View if condition matched
Posted: 2021-01-24 09:10
by pbottcher
which browser(s) did you test?
Re: How to redirect to Detail View if condition matched
Posted: 2021-01-24 10:24
by jsetzer
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.
Re: How to redirect to Detail View if condition matched
Posted: 2021-01-24 10:41
by jsetzer
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...
Re: How to redirect to Detail View if condition matched
Posted: 2021-01-24 10:56
by jsetzer
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.
Re: How to redirect to Detail View if condition matched
Posted: 2021-01-24 11:07
by ayussuf
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)
Re: How to redirect to Detail View if condition matched
Posted: 2021-01-24 11:33
by ayussuf
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 ...
Re: How to redirect to Detail View if condition matched
Posted: 2021-01-24 11:48
by jsetzer
Great, happy that, finally, we got your very specific requirement solved!
Re: How to redirect to Detail View if condition matched
Posted: 2021-01-24 11:56
by ayussuf
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
Re: How to redirect to Detail View if condition matched
Posted: 2021-01-24 12:07
by jsetzer
$mi = getMemberInfo();
var_dump($mi);
exit;
Re: How to redirect to Detail View if condition matched
Posted: 2021-01-24 12:43
by ayussuf
jsetzer wrote: ↑2021-01-24 12:07
$mi = getMemberInfo();
var_dump($mi);
exit;
I can get the var_dump result
But if I want to echo $memberInfo['group']; nothing appear.
What is the problem?
Re: How to redirect to Detail View if condition matched
Posted: 2021-01-24 13:13
by ayussuf
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
But if I want to echo $memberInfo['group']; nothing appear.
What is the problem?
I got it... I used
getMemberInfo()["group"]. It works
Re: How to redirect to Detail View if condition matched
Posted: 2021-01-24 13:17
by jsetzer
$mi["group"]
Get used to working with associative arrays in PHP. You will need it very often when customizing AppGini generated web apps.
Re: How to redirect to Detail View if condition matched
Posted: 2021-01-24 13:20
by ayussuf
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