PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Template/Listener/Timestampable.php

https://bitbucket.org/pycmam/symfony
PHP | 138 lines | 63 code | 9 blank | 66 comment | 18 complexity | d8c344094d0991c2fcff38698d293447 MD5 | raw file
Possible License(s): AGPL-3.0, LGPL-2.1, BSD-3-Clause, LGPL-3.0, ISC
  1. <?php
  2. /*
  3. * $Id$
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information, see
  19. * <http://www.doctrine-project.org>.
  20. */
  21. /**
  22. * Listener for the Timestampable behavior which automatically sets the created
  23. * and updated columns when a record is inserted and updated.
  24. *
  25. * @package Doctrine
  26. * @subpackage Template
  27. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  28. * @link www.doctrine-project.org
  29. * @since 1.0
  30. * @version $Revision$
  31. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
  32. */
  33. class Doctrine_Template_Listener_Timestampable extends Doctrine_Record_Listener
  34. {
  35. /**
  36. * Array of timestampable options
  37. *
  38. * @var string
  39. */
  40. protected $_options = array();
  41. /**
  42. * __construct
  43. *
  44. * @param string $options
  45. * @return void
  46. */
  47. public function __construct(array $options)
  48. {
  49. $this->_options = $options;
  50. }
  51. /**
  52. * Set the created and updated Timestampable columns when a record is inserted
  53. *
  54. * @param Doctrine_Event $event
  55. * @return void
  56. */
  57. public function preInsert(Doctrine_Event $event)
  58. {
  59. if ( ! $this->_options['created']['disabled']) {
  60. $createdName = $event->getInvoker()->getTable()->getFieldName($this->_options['created']['name']);
  61. $modified = $event->getInvoker()->getModified();
  62. if ( ! isset($modified[$createdName])) {
  63. $event->getInvoker()->$createdName = $this->getTimestamp('created', $event->getInvoker()->getTable()->getConnection());
  64. }
  65. }
  66. if ( ! $this->_options['updated']['disabled'] && $this->_options['updated']['onInsert']) {
  67. $updatedName = $event->getInvoker()->getTable()->getFieldName($this->_options['updated']['name']);
  68. $modified = $event->getInvoker()->getModified();
  69. if ( ! isset($modified[$updatedName])) {
  70. $event->getInvoker()->$updatedName = $this->getTimestamp('updated', $event->getInvoker()->getTable()->getConnection());
  71. }
  72. }
  73. }
  74. /**
  75. * Set updated Timestampable column when a record is updated
  76. *
  77. * @param Doctrine_Event $evet
  78. * @return void
  79. */
  80. public function preUpdate(Doctrine_Event $event)
  81. {
  82. if ( ! $this->_options['updated']['disabled']) {
  83. $updatedName = $event->getInvoker()->getTable()->getFieldName($this->_options['updated']['name']);
  84. $modified = $event->getInvoker()->getModified();
  85. if ( ! isset($modified[$updatedName])) {
  86. $event->getInvoker()->$updatedName = $this->getTimestamp('updated', $event->getInvoker()->getTable()->getConnection());
  87. }
  88. }
  89. }
  90. /**
  91. * Set the updated field for dql update queries
  92. *
  93. * @param Doctrine_Event $evet
  94. * @return void
  95. */
  96. public function preDqlUpdate(Doctrine_Event $event)
  97. {
  98. if ( ! $this->_options['updated']['disabled']) {
  99. $params = $event->getParams();
  100. $updatedName = $event->getInvoker()->getTable()->getFieldName($this->_options['updated']['name']);
  101. $field = $params['alias'] . '.' . $updatedName;
  102. $query = $event->getQuery();
  103. if ( ! $query->contains($field)) {
  104. $query->set($field, '?', $this->getTimestamp('updated', $event->getInvoker()->getTable()->getConnection()));
  105. }
  106. }
  107. }
  108. /**
  109. * Gets the timestamp in the correct format based on the way the behavior is configured
  110. *
  111. * @param string $type
  112. * @return void
  113. */
  114. public function getTimestamp($type, $conn = null)
  115. {
  116. $options = $this->_options[$type];
  117. if ($options['expression'] !== false && is_string($options['expression'])) {
  118. return new Doctrine_Expression($options['expression'], $conn);
  119. } else {
  120. if ($options['type'] == 'date') {
  121. return date($options['format'], time());
  122. } else if ($options['type'] == 'timestamp') {
  123. return date($options['format'], time());
  124. } else {
  125. return time();
  126. }
  127. }
  128. }
  129. }