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

Got something cool to share with AppGini users? Feel free to post it here!
Post Reply
AhmedBR
AppGini Super Hero
AppGini Super Hero
Posts: 327
Joined: 2013-09-19 10:23

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

Post by AhmedBR » 2022-07-20 11:22

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
AppGini 22.14 - xampp 3.3.0 - PHP 7.4.30 - Summary reports - Calendar - Mass update - Messages - AppGiniHelper

AhmedBR
AppGini Super Hero
AppGini Super Hero
Posts: 327
Joined: 2013-09-19 10:23

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

Post by AhmedBR » 2022-07-20 18:39

Got the name in the message, just use Concatenation (two strings) :D what I was thinking!
AppGini 22.14 - xampp 3.3.0 - PHP 7.4.30 - Summary reports - Calendar - Mass update - Messages - AppGiniHelper

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

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

Post by a.gneady » 2022-07-22 07:49

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.
: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.

AhmedBR
AppGini Super Hero
AppGini Super Hero
Posts: 327
Joined: 2013-09-19 10:23

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

Post by AhmedBR » 2022-07-22 10:13

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.
AppGini 22.14 - xampp 3.3.0 - PHP 7.4.30 - Summary reports - Calendar - Mass update - Messages - AppGiniHelper

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

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

Post by jsetzer » 2022-07-22 11:07

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.
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

AhmedBR
AppGini Super Hero
AppGini Super Hero
Posts: 327
Joined: 2013-09-19 10:23

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

Post by AhmedBR » 2022-07-22 11:32

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!
AppGini 22.14 - xampp 3.3.0 - PHP 7.4.30 - Summary reports - Calendar - Mass update - Messages - AppGiniHelper

Post Reply