When child record updated as Completed, then corresponding parent records should not be Editable

The recommended method of customizing your AppGini-generated application is through hooks. But sometimes you might need to add functionality not accessible through hooks. You can discuss this here.
Post Reply
ndoddi
Posts: 13
Joined: 2020-03-26 18:08

When child record updated as Completed, then corresponding parent records should not be Editable

Post by ndoddi » 2020-04-26 01:14

I have below 3 tables created with data as shown in the image.
"Lanlords" table is parent to "Properties" table.
"Properties" table is parent to "Applicant" table.
img parent.png
img parent.png (84.52 KiB) Viewed 1918 times
When "Applicant1" status is updated as "Completed", then the user should not be able to update its parents records.
It means user should not be able to edit "Property1" and "Landlord1" records in the parent tables.
How can I achieve this?

User avatar
onoehring
AppGini Super Hero
AppGini Super Hero
Posts: 1156
Joined: 2019-05-21 22:42
Location: Germany
Contact:

Re: When child record updated as Completed, then corresponding parent records should not be Editable

Post by onoehring » 2020-04-26 07:03

Hi,

in hooks/child-tablename.php you can check in the _before_update function if field X from some table contains Y and the react on that.
In your case: If status=completed then return false -> False will cancel the update process, thus making it impossible to change anything in the child record.
You might want to inform the user about this (see my footer: Custom (error) message

You can also check out the $options->AllowUpdate and set this in the _init function of hooks/child-tablename.php. This will make it possible to disallow any changes. Same procedure: check (in the init) if the user can (still) edit the record. If no, set the value to 0 which should disable editing that record.

AllowUpdate is (unfortunately as some others) not listed on https://bigprof.com/appgini/help/advanc ... ist-object
@ahmed: Please update the options on that page as every single /hooks/tablename file references this URL.

Olaf

User avatar
a.gneady
Site Admin
Posts: 1281
Joined: 2012-09-27 14:46
Contact:

Re: When child record updated as Completed, then corresponding parent records should not be Editable

Post by a.gneady » 2020-04-28 14:16

AllowUpdate is (unfortunately as some others) not listed on https://bigprof.com/appgini/help/advanc ... ist-object
@ahmed: Please update the options on that page as every single /hooks/tablename file references this URL.
Well, the reason AllowUpdate is not listed is that it won't work :)
To actually prevent editing a record based on some value in another table, one way is to set up the users permissions so that users can view all but edit only their group records. And in the childtable_after_update/childtable_after_insert hooks, change the owner of the related parent record to another group 'locked' for example or so. This way, the parent record will no longer be editable for users.
:idea: AppGini plugins to add more power to your apps:
  • DataTalk is an innovative AppGini plugin based on ChatGPT that allows you to interact with your AppGini database using natural language questions, without writing any SQL. Check the demo video
  • Mass Update plugin: Update multiple records at once and improve your workflow efficiency.
  • Check our other plugins and get a generous discount of up to 30% when buying 2 or more plugins.

ndoddi
Posts: 13
Joined: 2020-03-26 18:08

Re: When child record updated as Completed, then corresponding parent records should not be Editable

Post by ndoddi » 2020-04-28 16:35

@onoehring, @a.gneady - Thank you for your response and guidance. I have followed your suggestions and updated code to meet my requirements and got the solution working. :D

I am sharing the solution below for other friends, who might be looking for actual code.

Code: Select all


// ====== Show Error for Parent Edit, if child status is 'Completed' 

/* ====== STEP-1:
==================
create a view with below query in mySQL (phpMyAdmin) in your DB.
This query assuming I have 5 tables as below.

DB schema (Tables): Parent > Child relation: 
landlords > properties > applicants > schedule > status
*/	
//view_name: "parentlock_forchildcompleted"
SELECT a.id as Tstatus_id, a.status as Tstatus, 
b.id as Tschedule_id, b.date as Tschedule_date, 
c.id as Tapplicant_id, c.first_name as Tapplicant_first_name,
d.id as Tproperties_id, d.property_name as Tproperties_property_name,
e.id as Tlandlords_id, e.first_name as Tlandlords_first_name
FROM `status` a, `schedule` b, `applicants` c, `properties` d, `landlords` e 
WHERE a.status = 'completed'
AND a.Schedule_id = b.id
AND b.applicant_name = c.id 
AND c.property = d.id
AND d.landlord_name = e.id 



/* ====== STEP-2:
==================
Put below code in hooks/<ParentTable>.php file under Landlords_before_update(). 
In this example my parent table name as "Landlords". 
So I need to add below code in "hooks/Landlords.php" file.  
*/	
	function Landlords_before_update(&$data, $memberInfo, &$args) {
		$sqlQuery = "
		SELECT DISTINCT `Tlandlords_id`
		FROM `parentlock_forchildcompleted` va 
		WHERE va.Tlandlords_id = '{$data['selectedID']}' ";
		$id = sqlValue($sqlQuery);
		$msg = "This parent record can not be updated, as its child record(s) status is 'Completed'";
		
		if($id==null){
			return TRUE;
		}
		else{
			$_SESSION['custom_err_msg']= $msg; //json_encode($data);
			return FALSE;
		}
	}

/* ====== STEP-3:
==================
Put below code in hooks/header-extras.php file 
*/
<?php 
if (isset($_SESSION['custom_err_msg'])) {
	$customError ='<div style="margin-top:2em !important; padding:0.5em !important;" class="custmErrMsg alert alert-dismissable alert-danger">'.$_SESSION['custom_err_msg'].'</div>';
	echo $customError;	 
	if ($_SESSION['custom_err_shown'] == 1){
		unset($_SESSION['custom_err_msg']);
		$_SESSION['custom_err_shown'] = 0;
	}
	else {
		$_SESSION['custom_err_shown'] = 1;
	}
}




Post Reply