/libraries/koowa/database/behavior/lockable.php

https://github.com/raeldc/nooku-server · PHP · 187 lines · 82 code · 22 blank · 83 comment · 18 complexity · 8620828350b5601db1203e6cd176a7f0 MD5 · raw file

  1. <?php
  2. /**
  3. * @version $Id$
  4. * @category Koowa
  5. * @package Koowa_Database
  6. * @subpackage Behavior
  7. * @copyright Copyright (C) 2007 - 2010 Johan Janssens. All rights reserved.
  8. * @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
  9. */
  10. /**
  11. * Database Lockable Behavior
  12. *
  13. * @author Johan Janssens <johan@nooku.org>
  14. * @category Koowa
  15. * @package Koowa_Database
  16. * @subpackage Behavior
  17. */
  18. class KDatabaseBehaviorLockable extends KDatabaseBehaviorAbstract
  19. {
  20. /**
  21. * The lock lifetime
  22. *
  23. * @var integer
  24. */
  25. protected $_lifetime;
  26. /**
  27. * Initializes the options for the object
  28. *
  29. * Called from {@link __construct()} as a first step of object instantiation.
  30. *
  31. * @param object An optional KConfig object with configuration options
  32. * @return void
  33. */
  34. protected function _initialize(KConfig $config)
  35. {
  36. $config->append(array(
  37. 'priority' => KCommand::PRIORITY_HIGH,
  38. 'lifetime' => '900' //in seconds
  39. ));
  40. $this->_lifetime = $config->lifetime;
  41. parent::_initialize($config);
  42. }
  43. /**
  44. * Get the methods that are available for mixin based
  45. *
  46. * This function conditionaly mixies the behavior. Only if the mixer
  47. * has a 'locked_by' property the behavior will be mixed in.
  48. *
  49. * @param object The mixer requesting the mixable methods.
  50. * @return array An array of methods
  51. */
  52. public function getMixableMethods(KObject $mixer = null)
  53. {
  54. $methods = array();
  55. if(isset($mixer->locked_by) && isset($mixer->locked_on)) {
  56. $methods = parent::getMixableMethods($mixer);
  57. }
  58. return $methods;
  59. }
  60. /**
  61. * Lock a row
  62. *
  63. * Requires an 'locked_on' and 'locked_by' column
  64. *
  65. * @return boolean If successfull return TRUE, otherwise FALSE
  66. */
  67. public function lock()
  68. {
  69. //Prevent lock take over, only an saved and unlocked row and be locked
  70. if(!$this->isNew() && !$this->locked())
  71. {
  72. $this->locked_by = (int) JFactory::getUser()->get('id');
  73. $this->locked_on = gmdate('Y-m-d H:i:s');
  74. $this->save();
  75. }
  76. return true;
  77. }
  78. /**
  79. * Unlock a row
  80. *
  81. * Requires an locked_on and locked_by column to be present in the table
  82. *
  83. * @return boolean If successfull return TRUE, otherwise FALSE
  84. */
  85. public function unlock()
  86. {
  87. $userid = JFactory::getUser()->get('id');
  88. //Only an saved row can be unlocked by the user who locked it
  89. if(!$this->isNew() && $this->locked_by != 0 && $this->locked_by == $userid)
  90. {
  91. $this->locked_by = 0;
  92. $this->locked_on = 0;
  93. $this->save();
  94. }
  95. return true;
  96. }
  97. /**
  98. * Checks if a row is locked
  99. *
  100. * @return boolean If the row is locked TRUE, otherwise FALSE
  101. */
  102. public function locked()
  103. {
  104. $result = false;
  105. if(!$this->isNew())
  106. {
  107. if(isset($this->locked_on) && isset($this->locked_by))
  108. {
  109. $locked = strtotime($this->locked_on);
  110. $current = strtotime(gmdate('Y-m-d H:i:s'));
  111. //Check if the lock has gone stale
  112. if($current - $locked < $this->_lifetime)
  113. {
  114. $userid = JFactory::getUser()->get('id');
  115. if($this->locked_by != 0 && $this->locked_by != $userid) {
  116. $result= true;
  117. }
  118. }
  119. }
  120. }
  121. return $result;
  122. }
  123. /**
  124. * Get the locked information
  125. *
  126. * @return string The locked information as an internationalised string
  127. */
  128. public function lockMessage()
  129. {
  130. $message = '';
  131. if($this->locked())
  132. {
  133. $user = JFactory::getUser($this->locked_by);
  134. $date = $this->getService('com:default.template.helper.date')->humanize(array('date' => $this->locked_on));
  135. $message = JText::sprintf('Locked by %s %s', $user->get('name'), $date);
  136. }
  137. return $message;
  138. }
  139. /**
  140. * Checks if a row can be updated
  141. *
  142. * This function determines if a row can be updated based on it's locked_by information.
  143. * If a row is locked, and not by the logged in user, the function will return false,
  144. * otherwise it will return true
  145. *
  146. * @return boolean True if row can be updated, false otherwise
  147. */
  148. protected function _beforeTableUpdate(KCommandContext $context)
  149. {
  150. return (bool) !$this->locked();
  151. }
  152. /**
  153. * Checks if a row can be deleted
  154. *
  155. * This function determines if a row can be deleted based on it's locked_by information.
  156. * If a row is locked, and not by the logged in user, the function will return false,
  157. * otherwise it will return true
  158. *
  159. * @return boolean True if row can be deleted, false otherwise
  160. */
  161. protected function _beforeTableDelete(KCommandContext $context)
  162. {
  163. return (bool) !$this->locked();
  164. }
  165. }