diff --git a/assets/js/multi_user.js b/assets/js/multi_user.js index 1d04d24..8fc40a1 100644 --- a/assets/js/multi_user.js +++ b/assets/js/multi_user.js @@ -1,28 +1,52 @@ jQuery(document).ready(function($) { - //get User info + function saveUser(id, entry, ajaxURL){ + $.ajax({ + method: 'GET', + url: ajaxURL, + data: { + id: id, + entry: parseInt(entry) + } + }) + } + + //get info id = parseInt($('#session li').eq(0).find('a').attr('href').match(/\/([^\/]+)\/?$/)[1]); - name = $('#session li').eq(0).find('a').text(); + // name = $('#session li').eq(0).find('a').text(); entry = document.URL.match(/\/([^\/]+)\/?$/)[1]; + ajaxURL = Symphony.Context.get('symphony') + '/extension/multi_user_edit/multi_user/'; - - //send User info - console.log('test'); - - var ajaxURL = Symphony.Context.get('symphony') + '/extension/multi_user_edit/multi_user/'; - + //Check if the entry is being used by another author $.ajax({ method: 'GET', url: ajaxURL, data: { - id: id, - name: name, entry: parseInt(entry) + }, + success: function(response){ + response = $(response); + if(response.hasClass('locked')){ + //if locked, get difference in minutes + $diff = response.find('#diff').text(); + if($diff > 5){ + //Gain access to the article as author is taking too long. + console.log('Exceeded 5 minutes; Updated log.') + saveUser(id, entry, ajaxURL); + } + else{ + console.log(response.find('.show').text()); + console.log((5 - $diff).toFixed(2) + ' minute/s to go.'); + } + } + else{ + //Save User info + saveUser(id, entry, ajaxURL); + } } - }).done(function(response){ // after the request has been loaded... - $('#results').html(response); // put the response in the div + }).done(function(response){ + //code.. }); - }); \ No newline at end of file diff --git a/content/content.multi_user.php b/content/content.multi_user.php index 511c08c..776c313 100644 --- a/content/content.multi_user.php +++ b/content/content.multi_user.php @@ -1,10 +1,13 @@ update($update, 'sym_multi_user', "`entry_id` = ".$entryId)){ echo('Success'); } else{ echo('error'); } - die; } + //Check if entry is being used + if(isset($_GET['entry']) && !isset($_GET['id'])){ + + $entryId = MySQL::cleanValue($_GET['entry']); + + $query = "SELECT session_start, user_id + FROM sym_multi_user + WHERE `entry_id` = '".$entryId."' + LIMIT 1"; + + $userId = Symphony::Database()->fetchVar('user_id',0,$query); + + if( $userId == 0){ + echo('
Unlocked
'); + } + else{ + $time = new DateTime(Symphony::Database()->fetchVar('session_start',0,$query)); + + $now = new DateTime(); + + //Check for how long the article is locked + $diff = $now->diff($time); + + $diffString = $diff->format('%i.%S'); + + + echo('
User ID
'.$userId.'
has this article locked for
'.$diffString.'
(minutes.seconds).
Please wait for other editors to finish editing this article.
'); + } + die; + } } + +} diff --git a/extension.driver.php b/extension.driver.php index 418157f..8adf646 100644 --- a/extension.driver.php +++ b/extension.driver.php @@ -48,8 +48,6 @@ public function enable(){ public function preRender($context) { - // var_dump($context);die; - $entry_id = $context['entry']->get('id'); //Check if the entry id is already created in the table. @@ -58,12 +56,10 @@ public function preRender($context) { FROM tbl_multi_user as multi WHERE multi.entry_id = '". $entry_id ."'"; - - $count = Symphony::Database()->fetchVar('count',0,$query); if ($count == 0){ - //Add the user and entry id to the database. Adding 0 as a temporary value for author. + //Add the entry id to the database. Adding 0 as a temporary value for author. $query = "INSERT INTO tbl_multi_user (user_id, entry_id) VALUES ('0', $entry_id)"; @@ -86,16 +82,12 @@ public function preRender($context) { public function postEdit($context) { $entry_id = $context['entry']->get('id'); - //Delete all the entries related to this entry. - - $query = "DELETE FROM tbl_multi_user - WHERE multi.entry_id = '". $entry_id ."'"; - - if(Symphony::Database()->query($query) == TRUE){ - return true; - } + //Release lock. + $update = array(); + $update['user_id'] = intval('0'); + $update['session_start'] = date('0'); - return false; + Symphony::Database()->update($update, 'sym_multi_user', "`entry_id` = ".$entry_id); } }