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.org>.
|
20 |
*/
|
21 |
|
22 |
/**
|
23 |
* Doctrine_AuditLog
|
24 |
*
|
25 |
* @package Doctrine
|
26 |
* @subpackage AuditLog
|
27 |
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
28 |
* @link www.phpdoctrine.org
|
29 |
* @since 1.0
|
30 |
* @version $Revision$
|
31 |
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
32 |
*/
|
33 |
class Doctrine_AuditLog extends Doctrine_Plugin
|
34 |
{
|
35 |
protected $_options = array(
|
36 |
'className' => '%CLASS%Version',
|
37 |
'versionColumn' => 'version',
|
38 |
'generateFiles' => false,
|
39 |
'table' => false,
|
40 |
'pluginTable' => false,
|
41 |
);
|
42 |
|
43 |
/**
|
44 |
* Create a new auditlog_
|
45 |
*
|
46 |
* @param array $options An array of options
|
47 |
* @return void
|
48 |
*/
|
49 |
public function __construct(array $options = array())
|
50 |
{
|
51 |
$this->_options = array_merge($this->_options, $options);
|
52 |
}
|
53 |
|
54 |
/**
|
55 |
* Get the version
|
56 |
*
|
57 |
* @param Doctrine_Record $record
|
58 |
* @param mixed $version
|
59 |
* @return array An array with version information
|
60 |
*/
|
61 |
public function getVersion(Doctrine_Record $record, $version)
|
62 |
{
|
63 |
$className = $this->_options['className'];
|
64 |
|
65 |
$q = new Doctrine_Query();
|
66 |
|
67 |
$values = array();
|
68 |
foreach ((array) $this->_options['table']->getIdentifier() as $id) {
|
69 |
$conditions[] = $className . '.' . $id . ' = ?';
|
70 |
$values[] = $record->get($id);
|
71 |
}
|
72 |
$where = implode(' AND ', $conditions) . ' AND ' . $className . '.' . $this->_options['versionColumn'] . ' = ?';
|
73 |
|
74 |
$values[] = $version;
|
75 |
|
76 |
$q->from($className)
|
77 |
->where($where);
|
78 |
|
79 |
return $q->execute($values, Doctrine::HYDRATE_ARRAY);
|
80 |
}
|
81 |
|
82 |
/**
|
83 |
* buildDefinition for a table
|
84 |
*
|
85 |
* @param Doctrine_Table $table
|
86 |
* @return boolean true on success otherwise false.
|
87 |
*/
|
88 |
public function buildDefinition()
|
89 |
{
|
90 |
$name = $this->_options['table']->getComponentName();
|
91 |
|
92 |
$columns = $this->_options['table']->getColumns();
|
93 |
|
94 |
// remove all sequence, autoincrement and unique constraint definitions
|
95 |
foreach ($columns as $column => $definition) {
|
96 |
unset($columns[$column]['autoincrement']);
|
97 |
unset($columns[$column]['sequence']);
|
98 |
unset($columns[$column]['unique']);
|
99 |
}
|
100 |
|
101 |
// the version column should be part of the primary key definition
|
102 |
$columns[$this->_options['versionColumn']] = array('type' => 'integer',
|
103 |
'length' => 8,
|
104 |
'primary' => true);
|
105 |
|
106 |
$id = $this->_options['table']->getIdentifier();
|
107 |
|
108 |
$options = array('className' => $this->_options['className']);
|
109 |
|
110 |
$relations = array($name => array('local' => $id,
|
111 |
'foreign' => $id,
|
112 |
'onDelete' => 'CASCADE',
|
113 |
'onUpdate' => 'CASCADE'));
|
114 |
|
115 |
$this->generateClass($options, $columns, array());
|
116 |
|
117 |
$this->_options['pluginTable'] = $this->_options['table']->getConnection()->getTable($this->_options['className']);
|
118 |
|
119 |
return true;
|
120 |
}
|
121 |
}
|