record locking

119 views
Skip to first unread message

D Gleba

unread,
Aug 2, 2013, 10:01:52 PM8/2/13
to xata...@googlegroups.com
Is there a way to lock records in Xataface?

sudiph...@gmail.com

unread,
Aug 5, 2013, 3:31:04 AM8/5/13
to xata...@googlegroups.com
what does Lock means
that no body else could not able to edit it when it is in use ?

confirm then I will give you a solution.

-samhans

D Gleba

unread,
Aug 5, 2013, 5:05:15 PM8/5/13
to xata...@googlegroups.com
I would like to be able to view the record on the details page, but when edit button is pressed, it shows a message saying:

This record currently being edited by user = joesmith on ip=1.1.2.3. The edit was started on 2013-08-05_Mon_17.00-PM EDT. 
You cannot edit this record until the current lock by user=joesmith is released. 
Contact your system administrator if you think this lock is in error.

sudiph...@gmail.com

unread,
Aug 5, 2013, 10:59:46 PM8/5/13
to xata...@googlegroups.com
Good to go,

A way to do it pro-grammatically using xataface API. So follow this: 
===> create a field in the your table for eg "lock_ck" having value 0 or 1.
==> Now the trick is every user who want to edit a record has to check the value of "lock_ck" , if the value is "0" he can enter to edit the record.
      While entering he will set the value to "1" so that every body understand that the record is under lock condition  and going out set the value to "0" again
===> There are functions in xataface which can handle this situation :
               1) First is function beforeHandleRequest(){  }===> use this function to call your code which check the value of the lock_ck and do the necessary changes before editing a particular record.

               2) Second is "after_action_edit" action which can be called to set the parameters when the edit part is complete.

Give a trial . It will work.

--samhans

Steve Hannah

unread,
Aug 6, 2013, 12:45:51 AM8/6/13
to sudiphansraj1, xata...@googlegroups.com
Just to add to samhans' suggestion.  This can be a tricky thing to do well.  E.g. if the user cancels their editing, it may not  unlock the record.

I have used ajax polling before to continually lock the record while in the edit form.  You may find that storing the name of the user who is editing the record adds some flexibility to this strategy (rather than just a 1 for locked and 0 for unlocked).

But in general, samhans' suggestion is the right direction to be looking.

Steve
--
Steve Hannah
Web Lite Solutions Corp.

sudiph...@gmail.com

unread,
Aug 6, 2013, 3:05:16 PM8/6/13
to xata...@googlegroups.com, sudiphansraj1
Steve ,

The point that you have raised is a valid point to consider.

So now  I think of a solution :

==> The solution is in the form of jquery. The problem as we know is that what happen when the user has not edited the form and is going away so the trigger after_action_edit will
not fire and thus the record will remain in locked condition.

==> so to handle this situation I am going away from the traditional method of "after_action_edit" trigger and coding my own trigger for this in jquery.
       As for example if the user entered a record in edit mode, a jquery function will trap the address bar URL. and check for it changes. The time the user moves out from
      the edit form we will execute a ajax request to change the record lock condition .
  
for example:

function block__before_edit_record_form(){
    echo "
    <script type='text/javascript'>
    $(window).bind('beforeunload',function(){   // this function will fire when page unloads
   
    //Here you can place your ajax call to amend lock variable
   
    });
    </script>";
   
    }


The suggestion of storing '0' or '1' is experimental and as you have said we can very well add name of the user, timestamp, ip address etc in the field.
So it is for the developer to decide.

--  Hope it helps.


-- samhans

D Gleba

unread,
Aug 7, 2013, 4:07:45 PM8/7/13
to xata...@googlegroups.com, sudiphansraj1
Is there a trigger available before a record is edited? For example when the edit button is pressed and/or before the "default_browse_action=edit" happens.

Steve Hannah

unread,
Aug 7, 2013, 4:10:48 PM8/7/13
to D Gleba, xata...@googlegroups.com, sudiphansraj1
beforeSave(), beforeUpdate(), and beforeInsert() are all called before a record is saved (beforeInsert called for inserts, beforeUpdate called for updates).  However records may be saved outside of the context of an "Edit" form.  E.g. you could write an action the loops through a bunch of records, makes some changes and saves those changes, and these triggers would be called for each one.  Keep that in mind.

Steve

D Gleba

unread,
Aug 7, 2013, 4:23:29 PM8/7/13
to xata...@googlegroups.com, D Gleba, sudiphansraj1
Maybe I am not understanding, or maybe I am missing the point.

I want to call my function to create a lock for a record when the edit button is pressed. I want to stop a user from beginning to edit a record if someone else is editing it.

I seems that beforeUpdate() would be called when the save button is pressed after editing the record. The user may have spent considerable time and effort changing the record, only to find out that someone else has it "locked".

Steve Hannah

unread,
Aug 7, 2013, 4:28:10 PM8/7/13
to D Gleba, xata...@googlegroups.com, sudiphansraj1
Probably the most reliable way to do this would be to add some javascript in the edit form (you can do this by implementing a block that appears on the edit form) that polls the server via AJAX to let it know the record is being edited.  The server would receive the user name of the user who has the form opened, and you could poll the server every 10 seconds or so.  Set the lock to timeout if it hasn't received a ping from the client in over 20 seconds, and also clear the lock when the record is, in fact, saved.

Steve

D Gleba

unread,
Aug 13, 2013, 12:47:49 PM8/13/13
to xata...@googlegroups.com, D Gleba, sudiphansraj1
I will have to find some time and start wrapping my head around this. I haven't used AJAX myself yet. It will be a learning experience.

I typically start with w3schools.com and go from there.

I have successfully put some javascript, jquery, and css in place in my xataface apps, using this site, w3schools, and a few google searches. Much of what I have done is borrowed from one post or another.

If someone can jump start me with some off the cuff untested code it would help me know what to look into further. I love those real function and syntax examples to get me started researching how to do it.

Richard Mücke

unread,
Aug 7, 2025, 8:59:05 AMAug 7
to Xataface
Long time ago... 
Clicking on cancel calls the record with the action VIEW.
So inside beforeHandleRequest you can check if the locking user (the user editing the record) views the record. And then delete the lock.

Example:
      function beforeHandleRequest() {
        $app =& Dataface_Application::getInstance();
        $auth =& Dataface_AuthenticationTool::getInstance();
        $user =& $auth->getLoggedInUser();

// LOCKING
$dataedit_locking_time = 30;  // mins
$query =& $app->getQuery();
$action = $query['-action'];
$table = $query['-table'];
if ($query['-recordid']) {
$recordid = $query['-recordid'];
$rectable = null;
$recid = null;
list($rectable, $recid) = explode('?id=', $recordid, 2);
$username = $user->val('name');

if ($action == 'edit') {
$record = df_get_record($table, array('id' =>'='.$recid));
$locked_by = $record->val('locked_by');
$locked_until = strtotime($record->strval('locked_until'));
$now = time();

if ($locked_by && $locked_until > $now && $locked_by != $user->val('name')) {
$uhrbis = date("Y-m-d H:i:s", $locked_until);
die("Datensatz ist gesperrt von $locked_by bis $uhrbis Uhr.");
} else {
// Lock setzen
$record->setValue('locked_by', $user->val('name'));
$record->setValue('locked_until', date('Y-m-d H:i:s', $now + $dataedit_locking_time * 60));
$record->save();
}
}
if ($action == 'view') {
$record = df_get_record($table, array('id' =>'='.$recid));
$locked_by = $record->val('locked_by');
$locked_until = strtotime($record->strval('locked_until'));
$now = time();

if ($locked_by && $locked_until > $now && $locked_by == $user->val('name')) {
$record->setValue('locked_by', null);
$record->setValue('locked_until', null);
$record->save();
}
}


}
}

Works for me without java or ajax. 
Cancel will be detected. But viewing the record in another browser or tab while editing it unlocks it also. 
This is a risk I would take :)

Greets, Conri
Reply all
Reply to author
Forward
0 new messages