Learning to use hooks

Discussions related to customizing hooks. Hooks are documented at http://bigprof.com/appgini/help/advanced-topics/hooks/
Post Reply
dlee
Veteran Member
Posts: 137
Joined: 2020-04-14 00:21
Location: South Carolina, USA
Contact:

Learning to use hooks

Post by dlee » 2021-08-26 15:15

I am new to using hooks in Appgini and I have some questions. I have this code in the file /hooks/duck-season-2015.php:

Code: Select all

	function duck_season_2015_before_insert(&$data, $memberInfo, &$args) {
	
		echo "<script>alert('folder2');</script>";
	
		return TRUE;
	}
I added the "echo "<script>alert('folder2');</script>";" to this event just to see when this event fires. It is not working here but I have tried this code in other parts of my app and it works ok there.

Questions:

1- Should this code work here or am I doing something wrong?

2 - Do I need to supply the parameters, i.e. &$data, $memberInfo, &$args, in order to make this work? If so, what would their values look like?

3 - In a related way, how can I past the value of a variable in one page to code in another page?

Thanks,
TD

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

Re: Learning to use hooks

Post by jsetzer » 2021-08-26 18:33

hooks/duck-season-2015.php
Shouldn't it be duck_season_2015.php with underscores instead of hyphens?
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: Learning to use hooks

Post by jsetzer » 2021-08-26 18:37

I think echoing a string will not have any effect here, because after insert process appgini will redirect to another page and the script command will not be passed over into the follow-up page.

Try

Code: Select all

var_dump("Test"); 
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

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

Re: Learning to use hooks

Post by jsetzer » 2021-08-26 18:40

(3) there are many different ways starting from $_GET or $_POST parameters via $_SESSION variables up to database storage. It really depends on your specific scenario and requirements. Maybe you should specify what exactly you want.
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

dlee
Veteran Member
Posts: 137
Joined: 2020-04-14 00:21
Location: South Carolina, USA
Contact:

Re: Learning to use hooks

Post by dlee » 2021-08-27 16:35

Hi jsetzer, thank you for trying to help! Sorry for the delayed response. As for your comments:
Shouldn't it be duck_season_2015.php with underscores instead of hyphens?
To create this Appgini project I imported an existing database which had tables names with both underscores and hyphens. To change the existing database table names would have broken the entire website. Luckily for me, Appgini allowed me to change the table names in the project only, using names with all underscores, without having to modify the existing database.
I think echoing a string will not have any effect here, because after insert process appgini will redirect to another page and the script command will not be passed over into the follow-up page.
My bad, I was just using an 'echo' command to test if and when the code worked. It was just for testing and I should have indicated that.
(3) there are many different ways starting from $_GET or $_POST parameters via $_SESSION variables up to database storage. It really depends on your specific scenario and requirements. Maybe you should specify what exactly you want.
Strangest thing is right after I post a topic the answer just seems to come to me...$_SESSION variables is the only way to go here.

NOW FOR THE BIG NEWS:
After an embarrassing amount of trial and error I was able to arrive at a working solution that appears to be rock solid though I will be doing multiple user testing to verify it is safe to use.

Here is my solution to uploading images to differnet folders on the server:

/hooks/duck-season-2015.php

Code: Select all

<?php
	// For help on using hooks, please refer to https://bigprof.com/appgini/help/working-with-generated-web-database-application/hooks
	
	session_start();
		
	function duck_season_2015_init(&$options, $memberInfo, &$args) {
		
	$_SESSION['fdr'] = './images/gallery/duck_season_2015/';
		
		return TRUE;
	}
/config.php:

Code: Select all

<?php
	session_start();
	
	$adminConfig = [
	
		/* section left out for clarity */
	
	];
	
	$fldr = $_SESSION['fdr'];
	$adminConfig['baseUploadPath'] = $fldr;
That's it ,really simple, just add the code to each of the /hooks/pages that will be used to upload images to specify where the images get uploaded to.

I will be sending this solution to Appgini support to see if they see any problems with this.

TD

dlee
Veteran Member
Posts: 137
Joined: 2020-04-14 00:21
Location: South Carolina, USA
Contact:

Re: Learning to use hooks

Post by dlee » 2021-08-27 16:50

Need to add this disclaimer - Use at your own risk, no warranty given !!!
TD

dlee
Veteran Member
Posts: 137
Joined: 2020-04-14 00:21
Location: South Carolina, USA
Contact:

Re: Learning to use hooks

Post by dlee » 2021-08-29 03:19

I have made these changes to the code to handle multiple users since $_SESSION variables are not unigue to any one user. The new code uses "variable variables" to create a $_SESSION variable that is unigue to the user. Again this is not fully tested but appears to work correctly so if you use it please test, test, and test again!
TD

/duck-season-2015.php

Code: Select all

	session_start();
		
	function duck_season_2015_init(&$options, $memberInfo, &$args) {
		
		$user = $memberInfo['username'];
		$user_fdr = $user.'_fdr';
		$_SESSION['$$user_fdr'] = './images/gallery/duck_season_2015/';
		
		return TRUE;
	}
/config.php

Code: Select all

<?php
	session_start();
	
	$adminConfig = [
	
		/* section left out for clarity */
	
	];
	
	$user = $memberInfo['username'];
	$user_fdr = $user.'_fdr';
	$fldr = $_SESSION['$$user_fdr'];
	$adminConfig['baseUploadPath'] = $fldr;

Post Reply