-1

I need to prevent users from simultaneously editing the same text or article on tinymce.

How can I block other users from editing while one user is editing text in the admin panel?

Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151
Haroldas
  • 58
  • 9

2 Answers2

2

Well many ways actually.. but just of the top of my head while I sit in the John is to

  1. When someone within your application begins the edit you then send a update query to the database top update the row "editing"

  2. Now if the next user tries to edit the same post redirect then to a error message.

  3. If the first user is done then send another query to update the row "editing". Now the second ist can edit what ever they want...

Something like this will work :

If($_POST['startedit'] ) { 
     ............... // start your editing and then also send a query to a database

} else { 
   If($_POST['finished']){ 
       .............// send the update query and send them some where like homepage
   }
}

Any questions please ask

Edit 1

Alright so you asked how would you get the editing time? Well there are plethora ways to get it simple you could just use the database in fact to do the time stamps for you and I'll show you have that is possible.

Step 1) create a table in a database consisting of all the fields you need, and then at the end of the table a time stamp field that is set to update when the row is updated. (this can be done easily use PHPmyadmin if you do not know any sql)

Step 2) when a user clicks on the "startedit" button

you would send a query like so :

$query = $pdo->prepare("UPDATE `yourtable` SET `editing` = '1' WHERE `text_id` = ':id'"); //Using PDO you prepare a update query to dynamically update any ID based on what form a user is using.

$query->bindParam(':ID', $form_id); // Bind your value of the form to the parameter being passed to the PDO query

$query->execute() // Run the PDO query.

now you can do a conditional if on $query to check if it executed, if it did it will return true... so we do it like this :

if($query) {
    Redirect(to the form that he / she wants to work on) // Redirect (obviously lol)
} else { 
............ What ever? ...........
}

Now your asking yourself well crap how do I check if some one is currently editing this form?

Well then we need to run a query when along side of to see if the form is being edited.

so we do :

$query = $pdo->prepare("SELECT * from `yourtable` WHERE `editing` = `1` and `form_id` = :ID"); // Retrieve all forms that are being edited

$query->bindParam(':ID', $form_id); // Bind the parameter 

 $query->execute();

 if ($query->rowCount() > 0 ) {
     die("OOOPS! Some one is editing this form!! - Sorry to late!")
 } else {
   echo "Have fun editing punk!"; 
 }

There you go, that is pretty much the gist of it. I will not code all of it for you. But this is a rudimentary way for you to get this done. Now of course you want to add if a time out has occurred kick the user from editing and what not. And if the user finishes then update editing to = 0. and so on.

This will help you build up your work.

I am also using PDO so if your not accustomed to it read this and this.

  • Enjoy
Community
  • 1
  • 1
Rixhers Ajazi
  • 1,303
  • 11
  • 18
  • The complications are too many. Mainly if a user locks the row and then doesnt complete due to whatever reason. Then you need a timeout period to "open" the lock. Which will allow the second user to start editing (but how will he know?) Also what if the inactive period is because the first user is still editing? Its a difficult task (not impossible, wordly/google docs et al) but can easily become over engineered and complicated - remember google wave? – raidenace Apr 17 '13 at 19:47
  • Oh it is absolutely going to be complicated again please excuse the thought process above, I just answered the OP's primary concern. Of course there will be more issues with this. I would implement a time out and if time out occurs redirect the first user to a error screen. There is a lot of work that could possibly be going into this. But where is the cut off line between programming a solution and administrator responsibility? – Rixhers Ajazi Apr 17 '13 at 19:52
  • 1
    You would also want to lock it to a specific user. Preferably in another table. Something like `TABLE article_lock(user_id, article_id, expires_date)`. – Bart Apr 17 '13 at 19:59
  • @Haroldas if you can wait until I get to a desktop computer I will help you. I'm currently on the road driving on my cell phone. Expect a more in detail answer in a bit – Rixhers Ajazi Apr 18 '13 at 15:48
  • oh thanks :) can you give me your contacts or smthg, i want to send for you code source – Haroldas Apr 18 '13 at 16:01
  • Answer updated. Now you are free to sink or to rise to the surface. - Enjoy :) – Rixhers Ajazi Apr 18 '13 at 20:39
  • @Richers Ajazi - there's a flaw in your sample code. If for some reason you didn't finish the edit you cannot edit it again because it's locked for everybody. That is why you need to tie it to a user as well. So only that user can edit even if he comes back to finish a job undone. – Bart Apr 18 '13 at 20:54
  • I'm confused then from the OP's standpoint. I was under the impression that if the user starts the edit they end the edit? Thats why I coded it the way I did. If there should be the need to have a "Finish Later Option" then yes you are correct. However I was on the impression that once the user starts they end. – Rixhers Ajazi Apr 18 '13 at 20:58
  • What exactly are you using to program in / what platform? Are you in a CMS? (I know your using PHP) – Rixhers Ajazi Apr 18 '13 at 22:22
  • Which CMS? I have never used a CMS so supporting you might be a bit challenging - lol – Rixhers Ajazi Apr 19 '13 at 11:42
0

Probably the most friendly solution is to let users edit articles simultaneously. In case of a conflict - say the article gets saved by different users in a given timespan - one could choose wich version of the article to use. This would require versioning though and is probably harder to implement then @Rixhers Ajazi solution.

Bart
  • 17,070
  • 5
  • 61
  • 80
  • That versioning system could crazy really fast depending on how many people submit their version. On personal experience locking the record is the smartest way to do it. But this answer is also a solution but a hell off lot harder and mentally more costly if you ask me. – Rixhers Ajazi Apr 18 '13 at 15:51
  • I really wonder how many simultaneous edits will happen in real life scenarios. It's highly unlikely that more then 2 edits will accure within a given timespan. – Bart Apr 18 '13 at 16:42
  • Depends on the situation. I am student and as work study I work at a financial aid office, the last scholarship database we had there were at least 10 edits a day on the same student. (I would know.. I helped package students with aid) So yes your right, but it all depends on the system. - This is something I've already taken into account since I am redoing their legacy system :P – Rixhers Ajazi Apr 18 '13 at 20:38