# Source for file Listener.php

Documentation is available at Listener.php

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.phpdoctrine.com>.
20. */
21. Doctrine::autoload('Doctrine_Record_Listener');
22. /**
23. * Doctrine_AuditLog_Listener
24. *
25. * @package Doctrine
26. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
27. * @category Object Relational Mapping
28. * @link www.phpdoctrine.com
29. * @since 1.0
30. * @version $Revision$
31. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
32. */
33. class Doctrine_AuditLog_Listener extends Doctrine_Record_Listener
34. {
36. protected $_auditLog;
38. public function __construct(Doctrine_AuditLog $auditLog) {
39. $this->_auditLog = $auditLog;
40. }
41. public function preInsert(Doctrine_Event $event)
42. {
43. $versionColumn = $this->_auditLog->getOption('versionColumn');
45. $event->getInvoker()->set($versionColumn, 1);
46. }
47. public function preDelete(Doctrine_Event $event)
48. {
49. $class = $this->_auditLog->getOption('className');
51. $record = $event->getInvoker();
53. $version = new $class();
54. $version->merge($record->toArray());
55. $version->save();
57. $versionColumn = $this->_auditLog->getOption('versionColumn');
58. $version = $record->get($versionColumn);
60. $record->set($versionColumn, ++$version);
61. }
62. public function preUpdate(Doctrine_Event $event)
63. {
64. $class = $this->_auditLog->getOption('className');
65. $record = $event->getInvoker();
67. $version = new $class();
68. $version->merge($record->toArray());
69. $version->save();
71. $versionColumn = $this->_auditLog->getOption('versionColumn');
73. $version = $record->get($versionColumn);
75. $record->set($versionColumn, ++$version);
76. }
77. }