/atk/lock/lock.js.php

https://github.com/teamplus/teamplus · PHP · 142 lines · 77 code · 16 blank · 49 comment · 13 complexity · 9a77c5906e006a4621c8cbf6b6ebee52 MD5 · raw file

  1. <?php
  2. /**
  3. * This file is part of the Achievo ATK distribution.
  4. * Detailed copyright and licensing information can be found
  5. * in the doc/COPYRIGHT and doc/LICENSE files which should be
  6. * included in the distribution.
  7. *
  8. * @package atk
  9. * @subpackage lock
  10. *
  11. * The ATK lock javascript. This script allows us to extend the lock lease
  12. * for a certain record / item.
  13. *
  14. * @author Peter C. Verhage <peter@ibuildings.nl>
  15. *
  16. * @copyright (c)2000-2004 Ibuildings.nl BV
  17. * @license http://www.achievo.org/atk/licensing ATK Open Source License
  18. *
  19. * @version $Revision: 4845 $
  20. * $Id: lock.js.php 6354 2009-04-15 02:41:21Z mvdam $
  21. */
  22. /** @internal some defines */
  23. global $ATK_VARS;
  24. $id = (int)$ATK_VARS["id"];
  25. $message = atktext("lock_expired");
  26. $stack = $ATK_VARS["stack"];
  27. ?>
  28. var atkLock = new Object();
  29. /**
  30. * Initialize the lock.
  31. * @param identifier the lock ID
  32. */
  33. function atkLockInit(identifier)
  34. {
  35. /* initialize */
  36. atkLock.theIdentifier = identifier;
  37. atkLock.theSequence = 1;
  38. atkLock.isLocked = true;
  39. atkLock.type = (window.XMLHttpRequest || window.ActiveXObject) ? 'xml' : 'image';
  40. /* start the timer */
  41. atkLockTimer();
  42. }
  43. /**
  44. * The lock timer recursively calls itself until the lock
  45. * lease has expired. Every run it checks if the lock is
  46. * still valid and increments the lock sequence.
  47. */
  48. function atkLockTimer()
  49. {
  50. if (!atkLock.isLocked) return;
  51. atkLockCheck();
  52. atkLock.theSequence++;
  53. setTimeout('atkLockTimer()', 30000);
  54. }
  55. /**
  56. * Check the DOM XML document response.
  57. * @param XMLDocument the DOM XML document
  58. */
  59. function atkLockCheckResponse(XMLdocument)
  60. {
  61. try
  62. {
  63. var root = XMLdocument.getElementsByTagName("response");
  64. if (root.length == 0) throw Error("Invalid lock response document.");
  65. var response = root.item(0);
  66. // success?
  67. if (response.getElementsByTagName("success").length > 0)
  68. {
  69. // lock lease extended, do nothing at this time
  70. }
  71. // failure?
  72. else if (response.getElementsByTagName("failure").length > 0)
  73. {
  74. throw Error("Lock has expired");
  75. }
  76. // invalid response?
  77. else
  78. {
  79. throw Error("Invalid lock response document.");
  80. }
  81. }
  82. // failure
  83. catch (exception)
  84. {
  85. // for now ignore the exception messages
  86. atkLockUnlock();
  87. }
  88. }
  89. /**
  90. * Fetch a new lock image, which extends the lock, or if
  91. * the lock lease has expired triggers an error.
  92. */
  93. function atkLockCheck()
  94. {
  95. var sURI = '<?php echo session_url('include.php?file=atk/lock/lock.php&type=xml&stack='.$stack);?>&id=' + atkLock.theIdentifier + '&sequence=' + atkLock.theSequence;
  96. if (atkLock.type == 'xml')
  97. {
  98. var xmlHttp = XmlHttp.create();
  99. xmlHttp.open("GET", sURI, true);
  100. xmlHttp.onreadystatechange = function ()
  101. {
  102. if (xmlHttp.readyState == 4)
  103. {
  104. atkLockCheckResponse(xmlHttp.responseXML);
  105. }
  106. }
  107. xmlHttp.send(null);
  108. }
  109. else
  110. {
  111. var image = new Image();
  112. image.onerror = atkLockUnlock;
  113. image.src = '<?php echo session_url('include.php?file=atk/lock/lock.php&type=image&stack='.$stack);?>&id=' + atkLock.theIdentifier + '&sequence=' + atkLock.theSequence;
  114. }
  115. }
  116. /**
  117. * When the lock lease has expired we notify the user.
  118. */
  119. function atkLockUnlock()
  120. {
  121. atkLock.isLocked = false;
  122. if (typeof(document.images['_lock_']) != 'undefined')
  123. document.images['_lock_'].src='<?php echo atkconfig("atkroot");?>atk/images/lock_expired.gif';
  124. alert('<?php echo addslashes($message); ?>');
  125. }
  126. atkLockInit('<?php echo $id; ?>');