Page 1 of 1

Single Edit ("Row Lock") - just hook and js nothing else!

Posted: 2022-07-20 11:22
by AhmedBR
Have been looking for an easy way to do this without anything fancy.
We all know the problem of double editing, row locking, deadlocks etc.

Finally got this idea, works perfect for what I need, sure will piss off some users, but I do not have that many users using the app at the same time (happens but very little).
If you have a busy app, you need to look else where for sure!

I had two fields that I wanted for something else, but I decided to use them since I already had them in every table (if you are creating from scratch choose better names for the fields)

Enough talk, let´s do business:

In your table you need two fields (owner, group) (Choose better names, like edited and lock or something)
owner field should have automatic <%%editingTimestamp%%> (read only and HIDDEN)
group just VarChar 40 (normal no read only and not hidden)

open this file, create it if it does not exist:
tableName-dv.js and add the following lines

Code: Select all

document.getElementById("group").style.backgroundColor = "#2E3338";
document.getElementById("group").style.borderColor = "#2E3338";
document.getElementById("group").readOnly = true;
The color should match your theme to make the field vanish, the user should not see this.

Now for the hook file of the same table:
go to function after insert and add the following

Code: Select all

	// group = time() for the lock
	sql("update `tableName` set  `group`= UNIX_TIMESTAMP(), `owner` = `group` where `id` = {$data['id']}",$eo);

go to function before update and add the following

Code: Select all

	// For the lock
	$LockTime=sqlValue("select `owner` from tableName where `id` ='{$data['selectedID']}'");
    if ( $data['group'] !== $LockTime){$args['error_message'] = 'Someone modified your row, check the new data or some other message!';return FALSE;} ;
	$data['group'] = $data['owner'];
	//===============================
Appgini will retrieve the new data automatically, you need to warn your user that the new data is shown now before he hits save again.

if you click save again after the message, your editing will be saved.
the other guy that saved the last editing will get the error message if he tries to save a second time, and so on.
Works perfect for me without locking, time lock etc.

I hope this solves the problem for some of you.

Please advise if there is a better approach while keeping it simple.

What I am trying to do now is: instead of saying Someone in the error message I want to put the user name instead.
No luck so far, so I added a field showing edited by! does the same but would be better if I put it in the message.

I am sending this to Ahmed appgini for his evaluation!
Thank you and happy coding

Re: Single Edit ("Row Lock") - just hook and js nothing else!

Posted: 2022-07-20 18:39
by AhmedBR
Got the name in the message, just use Concatenation (two strings) :D what I was thinking!

Re: Single Edit ("Row Lock") - just hook and js nothing else!

Posted: 2022-07-22 07:49
by a.gneady
Great idea. Thanks for sharing that. The field names might be a bit confusing (storing a timestamp in a field named 'group'), but other than that, it's very good. Will consider adding something like that in future releases.

Re: Single Edit ("Row Lock") - just hook and js nothing else!

Posted: 2022-07-22 10:13
by AhmedBR
a.gneady wrote:
2022-07-22 07:49
The field names might be a bit confusing (storing a timestamp in a field named 'group')
Totally agree, more than a bit, that is why I mentioned to choose better names.
I just had them in every table when I had this idea, was too lazy to rename all of them (about 48 tables in the project), I wanted to see it working right away!

Next project I will choose better names for sure!

Thanks, that would be great if this comes with Appgini.
I was thinking of adding a field to membership_userrecords since it already has dateUpdated, something like MidUpdatedBy, this way I only need to add one field per table.

Any suggestion to improve on this is very welcome.

Re: Single Edit ("Row Lock") - just hook and js nothing else!

Posted: 2022-07-22 11:07
by jsetzer
Thanks for you contribution!

Instead of adding such a feature and specific implementation to standard AppGini generated code and tables, personally I'd prefer having a plugin for such. If it was a plugin, we all could decide by ourselves if we want that feature or not. Just my 5 cents.

Re: Single Edit ("Row Lock") - just hook and js nothing else!

Posted: 2022-07-22 11:32
by AhmedBR
Hi Jan,

I totally agree for the lock you are developing to be as a plugin, very nice work by the way:
here is the link for those with busy apps, this is the way to go:
viewtopic.php?f=8&t=1556#p19749

But for most of us who only has a punch of users in his app, a small "edit" warning is good to be in Appgini, especially most of the competitors have built-in feature of some kind.
I would be happy to make this as a plugin for Appgini, the code is very simple, but have no idea how to make a plugin!