# Source for file AuditLog.php

Documentation is available at AuditLog.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. /**
22. * Doctrine_AuditLog
23. *
24. * @package Doctrine
25. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
26. * @category Object Relational Mapping
27. * @link www.phpdoctrine.com
28. * @since 1.0
29. * @version $Revision$
30. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
31. */
32. class Doctrine_AuditLog
33. {
34. protected $_options = array(
35. 'className' => '%CLASS%Version',
36. 'versionColumn' => 'version',
37. 'generateFiles' => false,
38. 'table' => false,
39. );
41. protected $_auditTable;
43. public function __construct($options)
44. {
45. $this->_options = array_merge($this->_options, $options);
46. }
47. /**
48. * __get
49. * an alias for getOption
50. *
51. * @param string $option
52. */
53. public function __get($option)
54. {
55. if (isset($this->options[$option])) {
56. return $this->_options[$option];
57. }
58. return null;
59. }
60. /**
61. * __isset
62. *
63. * @param string $option
64. */
65. public function __isset($option)
66. {
67. return isset($this->_options[$option]);
68. }
69. /**
70. * getOptions
71. * returns all options of this table and the associated values
72. *
73. * @return array all options and their values
74. */
75. public function getOptions()
76. {
77. return $this->_options;
78. }
79. /**
80. * setOption
81. * sets an option and returns this object in order to
82. * allow flexible method chaining
83. *
84. * @see slef::$_options for available options
85. * @param string $name the name of the option to set
86. * @param mixed $value the value of the option
87. * @return Doctrine_AuditLog this object
88. */
89. public function setOption($name, $value)
90. {
91. if ( ! isset($this->_options[$name])) {
92. throw new Doctrine_Exception('Unknown option ' . $name);
93. }
94. $this->_options[$name] = $value;
95. }
96. /**
97. * getOption
98. * returns the value of given option
99. *
100. * @param string $name the name of the option
101. * @return mixed the value of given option
102. */
103. public function getOption($name)
104. {
105. if (isset($this->_options[$name])) {
106. return $this->_options[$name];
107. }
108. return null;
109. }
111. public function getVersion(Doctrine_Record $record, $version)
112. {
113. $className = $this->_options['className'];
115. $q = new Doctrine_Query();
117. $values = array();
118. foreach ((array) $this->_options['table']->getIdentifier() as $id) {
119. $conditions[] = $className . '.' . $id . ' = ?';
120. $values[] = $record->get($id);
121. }
122. $where = implode(' AND ', $conditions) . ' AND ' . $className . '.' . $this->_options['versionColumn'] . ' = ?';
124. $values[] = $version;
126. $q->from($className)
127. ->where($where);
129. return $q->execute($values, Doctrine_HYDRATE::HYDRATE_ARRAY);
130. }
131. public function buildDefinition(Doctrine_Table $table)
132. {
133. $this->_options['className'] = str_replace('%CLASS%',
134. $this->_options['table']->getComponentName(),
135. $this->_options['className']);
137. $name = $table->getComponentName();
139. $className = $name . 'Version';
141. if (class_exists($className)) {
142. return false;
143. }
145. $columns = $table->getColumns();
147. // the version column should be part of the primary key definition
148. $columns[$this->_options['versionColumn']]['primary'] = true;
150. $id = $table->getIdentifier();
152. $options = array('className' => $className);
154. $builder = new Doctrine_Import_Builder();
156. $def = $builder->buildDefinition($options, $columns);
158. if ( ! $this->_options['generateFiles']) {
159. eval($def);
160. }
161. return true;
162. }
163. }