Problem with before_insert hook

Discussions related to customizing hooks. Hooks are documented at http://bigprof.com/appgini/help/advanced-topics/hooks/
Post Reply
iwilliams
Posts: 27
Joined: 2020-10-24 15:10

Problem with before_insert hook

Post by iwilliams » 2021-01-06 15:08

Hi,

I am trying to prevent more than three records being written to a particular table i.e. TimeSheetMissed. I figured out using the AppGini documentation that the best place was to modify the hooks/TimeSheetMissed.php file _before_insert hook.

The behaviour is that it ignores my code, drops straight through and creates a record. I guess I might have an error in my code but despite many iterations iterations I cannot figure it out. Could someone help please?

The table (TimeSheetMissed) contains the following fields (non calculated fields are not shown):-

TSMSID (int 11, PK)
TSHDRID (int 11). This field relates it to a TimeSheet Header record
Reason (varchar 40)
ApptDate (Date)
StartTime (Time)
EndTime (Time)
DateInformed (Date)
TimeInformed (Time)

Code: Select all

	function TimeSheetMissed_before_insert(&$data, $memberInfo, &$args) {
		
		$tshdrid = $data['TSHDRID'];
		$numrecords = sqlValue("SELECT COUNT('TSHDRID') FROM 'TimeSheetMissed' WHERE 'TSHDRID' = $tshdrid");
		/* $numrecords = sqlValue("SELECT COUNT('TSHDRID') FROM 'TimeSheetMissed' WHERE 'TSHDRID' = '{$data['TSHDRID']}'"); */
		if($numrecords > 3){
			$args['error_message'] = 'Error: Only three missed appointments per timesheet are allowed';
			return FALSE;
		}
		
		return TRUE;
	}
I have commented out an attempt to use TSHDRID directly from the $data array which didn't work. Within the SQL I have tried field names and tablename with and without dog ears (''). Many thanks in anticipation.

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

Re: Problem with before_insert hook

Post by jsetzer » 2021-01-06 15:12

If I was you, I would first check if the funtion gets called at all, and if, do some more debugging to inspect the values and conditions.

Code: Select all

var_dump($data);
exit;

// ...
Last edited by jsetzer on 2021-01-06 15:15, edited 1 time in total.
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: Problem with before_insert hook

Post by jsetzer » 2021-01-06 15:15

There may be problems with your single quotes surrounding field name and table name, but it depends on your database settings.

Try this instead:

Code: Select all

$numrecords = sqlValue("SELECT COUNT(*) FROM `TimeSheetMissed` WHERE `TSHDRID` = '{$tshdrid}'");
var_dump($numrecords);
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: Problem with before_insert hook

Post by jsetzer » 2021-01-06 15:23

If sqlValue returns false (and then $numrecords equals false), there may probably be an error in your SQL query.

If so, try the following for narrowing down:

Code: Select all

// ...
$eo = null;
$numrecords = sqlValue("...your query here...", $eo);
var_dump($numrecords);
if ($eo) {
  echo "<pre>" . $eo . "</pre>";
}

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

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

Re: Problem with before_insert hook

Post by pbottcher » 2021-01-06 15:46

Hi,

you could try

Code: Select all

sqlValue("SELECT COUNT(TSHDRID) FROM TimeSheetMissed WHERE TSHDRID = {$tshdrid}");
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.

iwilliams
Posts: 27
Joined: 2020-10-24 15:10

Re: Problem with before_insert hook

Post by iwilliams » 2021-01-06 16:40

Thanks very much. I will them all and let you know how I got on.

iwilliams
Posts: 27
Joined: 2020-10-24 15:10

Re: Problem with before_insert hook

Post by iwilliams » 2021-01-06 17:08

When I ran the var_dump($data) it appears that the field (TSHDRID) I want to test for using SQL is not present. Please see below.

Code: Select all


array(6) { ["Reason"]=> string(16) "Docs appointment" ["ApptDate"]=> string(8) "2021-1-3" ["StartTime"]=> string(7) "1:00:00" ["EndTime"]=> string(7) "2:00:00" ["DateInformed"]=> string(8) "2021-1-1" ["TimeInformed"]=> string(7) "1:00:00" }
This would account for the null response from my SQL query but how do I get round this please? It appears that $data holds the datafields but not my primary key and my foreign key that I use to link to my timesheet header.

Do I need to regenerate my hooks file just for this table to see if they now appear?

Many thanks

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

Re: Problem with before_insert hook

Post by pbottcher » 2021-01-06 17:40

How is the defintion in AppGini for the TSHDRID field?
Did you make it read-only somehow?
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.

iwilliams
Posts: 27
Joined: 2020-10-24 15:10

Re: Problem with before_insert hook

Post by iwilliams » 2021-01-06 18:35

Yes you are correct TSHDRID is defined as read only. However, I did that on purpose so that the user couldn't change it and the record therefore didn't lose its integrity. If I disable read only but hide it in detail view would it then appear on $data? Would I have to regenerate the hooks file again?

Thanks

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

Re: Problem with before_insert hook

Post by pbottcher » 2021-01-06 18:41

Hi,

correct, but you cannot use the AppGini default option (through the GUI) to hide it in the detailview, as it would no apear at all and hence would again not be available in the $data variable.
You can hide the field e.g. through the hooks/TABLENAME-dv.js. Or you could also just disable the field, so it would be visible, but the user could not modify it.
(If you want to make sure that even if the user hacks the page and changes the value, you should still verify the data through the before_insert function.)

You do not need to regenerate your hooks file. It should work right away.
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.

iwilliams
Posts: 27
Joined: 2020-10-24 15:10

Re: Problem with before_insert hook

Post by iwilliams » 2021-01-06 18:51

Thanks. I think I prefer the disable option but I’m not sure how to code that in JQuery/JS. Would it be possible for you let me know please?

Thank you

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

Re: Problem with before_insert hook

Post by pbottcher » 2021-01-06 19:58

Hi,

try to add to the hooks/TimeSheetMissed-dv.js

Code: Select all

function wait_for(data,callback,time=100) {
	if($j(data).length != 0) {
		callback();
		return;
	}
	else {
		setTimeout(function() {
			wait_for(data,callback, time);
		}, time);
	}
}
$j(function() {		
	wait_for("#TSHDRID-container", function() {
		$j("#TSHDRID-container").select2("disable", true)}
	);
})
Verify that TSHDRID is the correct field.
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.

iwilliams
Posts: 27
Joined: 2020-10-24 15:10

Re: Problem with before_insert hook

Post by iwilliams » 2021-01-06 20:14

Thanks very much. I will give that a try.

iwilliams
Posts: 27
Joined: 2020-10-24 15:10

Re: Problem with before_insert hook

Post by iwilliams » 2021-01-07 14:43

Everything now works fine. Many thanks for your help pbottcher and jsetzer.

Post Reply