Prevent update for the record based on filed value. and prevent adding child records for a specified table.

Discussions related to customizing hooks. Hooks are documented at http://bigprof.com/appgini/help/advanced-topics/hooks/
Post Reply
User avatar
afayez
Posts: 12
Joined: 2020-09-06 15:24
Location: EGYPT
Contact:

Prevent update for the record based on filed value. and prevent adding child records for a specified table.

Post by afayez » 2020-09-07 05:46

Dears,
Good day,

I need to prevent update for the record, if a specified filed value is more than zero.
Also i need to prevent adding a child record for a specified child table (not all the child tables) if the specified filed value in the parent table is more than zero.

I tried the below code in the "qtn.php" in the hooks folder, where is "qtn" is my table name. but it works only with an editable field, and it didn't work with the auto calculated field.


function qtn_before_update(&$data, $memberInfo, &$args) {

if( $data['onex'] > 0 ) return FALSE;

return TRUE;
}



"onex" is an editable field, the previous code works well.
but when i put $data['sign_count'] which is a calculated field, it doesn't work.


the auto calculated field contains:

SELECT
COUNT(`digital_signatures`.`id_sign`)
FROM
`qtn` LEFT JOIN
`digital_signatures` ON `qtn`.`id_qtn` = `digital_signatures`.`id_qtn`
WHERE
`qtn`.`id_qtn` = '%ID%'


Please support
Thanks

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

Re: Prevent update for the record based on filed value. and prevent adding child records for a specified table.

Post by jsetzer » 2020-09-07 06:19

Please check if $data contains "sign_count" at all.

Code: Select all

function TABLENAME_before_insert(&$data, $memberInfo, &$args)
{
	echo '<pre>';
	var_dump($data);
	echo '</pre>';
	exit;

	return TRUE;
}
If I remember right, readonly fields are not being passed to *_before_* hooks.
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
afayez
Posts: 12
Joined: 2020-09-06 15:24
Location: EGYPT
Contact:

Re: Prevent update for the record based on filed value. and prevent adding child records for a specified table.

Post by afayez » 2020-09-07 08:38

jsetzer wrote:
2020-09-07 06:19
Please check if $data contains "sign_count" at all.

Code: Select all

function TABLENAME_before_insert(&$data, $memberInfo, &$args)
{
	echo '<pre>';
	var_dump($data);
	echo '</pre>';
	exit;

	return TRUE;
}
If I remember right, readonly fields are not being passed to *_before_* hooks.

Yes Dear jsetzer, The read only field did not appears a below:


array(19) {
["date"]=>
string(8) "2020-9-6"
["subject"]=>
string(0) ""
["customer"]=>
string(1) "1"
["attn"]=>
string(0) ""
["location"]=>
string(0) ""
["status"]=>
string(10) "PO waiting"
["sales_man"]=>
string(0) ""
["sales_tel"]=>
string(0) ""
["sales_email"]=>
string(0) ""
["payment"]=>
string(15) "100% in advance"
["execution"]=>
string(46) "During 1-2 week from receiving the site ready."
["offer_validity"]=>
string(7) "15 Days"
["terms"]=>
string(81) "15 NOT including VAT - All prices in Saudi Riyal - NOT including civil works"
["stamp"]=>
string(1) "2"
["myheader"]=>
string(1) "2"
["myfooter"]=>
string(1) "2"
["onex"]=>
string(1) "0"
["selectedID"]=>
string(1) "1"
["documents"]=>
string(0) ""
}



can i make a SQL inquiry inside the PHP code? like below:
If yes, please help me for the right formula.



function tablename_before_update(&$data, $memberInfo, &$args){

$get_my_value = sqlValue("?????????????????????'");

if($get_my_value > 0 ) return FALSE;

return TRUE;
}

User avatar
afayez
Posts: 12
Joined: 2020-09-06 15:24
Location: EGYPT
Contact:

Re: Prevent update for the record based on filed value. and prevent adding child records for a specified table.

Post by afayez » 2020-09-07 08:52

afayez wrote:
2020-09-07 08:38


can i make a SQL inquiry inside the PHP code? like below:
If yes, please help me for the right formula.



function tablename_before_update(&$data, $memberInfo, &$args){

$get_my_value = sqlValue("?????????????????????'");

if($get_my_value > 0 ) return FALSE;

return TRUE;
}
This is the SQL auto calculated from the field "sign_count" from the table "qtn"

SELECT
COUNT(`digital_signatures`.`id_sign`)
FROM
`qtn` LEFT JOIN
`digital_signatures` ON `qtn`.`id_qtn` = `digital_signatures`.`id_qtn`
WHERE
`qtn`.`id_qtn` = '%ID%'

download/file.php?mode=view&id=1847
Attachments
22.png
22.png (209.05 KiB) Viewed 2892 times

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

Re: Prevent update for the record based on filed value. and prevent adding child records for a specified table.

Post by pbottcher » 2020-09-07 16:11

Hi,

try

Code: Select all

$get_my_value = sqlValue("SELECT COUNT(`digital_signatures`.`id_sign`) FROM `qtn` LEFT JOIN `digital_signatures` ON `qtn`.`id_qtn` =  
 digital_signatures`.`id_qtn` WHERE `qtn`.`id_qtn` = ".$data['selectedID']);
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
afayez
Posts: 12
Joined: 2020-09-06 15:24
Location: EGYPT
Contact:

Re: Prevent update for the record based on filed value. and prevent adding child records for a specified table.

Post by afayez » 2020-09-08 08:41

Thanks a lot dear pböttcher :) :)
Your code helped me, it worked with little modification that i took the SQL value of the read only field "sign_count" instead of recalculating the value.

$get_my_value = sqlValue("SELECT `sign_count` from `qtn` where `id_qtn`='{$data['selectedID']}'");
if( $get_my_value > 0 ) return FALSE;


Can i prevent adding any new child record for the table "qtn_items" based on the same previous rule if( $get_my_value > 0 ) ?

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

Re: Prevent update for the record based on filed value. and prevent adding child records for a specified table.

Post by pbottcher » 2020-09-08 19:52

Hi,

gald it worked out. Yes you can use the same also in the child record handling. Just put it into the correct hooks -> before_insert function.
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.

Post Reply