Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 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
/**
23
 * Doctrine_AuditLog
24
 *
25
 * @package     Doctrine
26
 * @subpackage  AuditLog
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_AuditLog extends Doctrine_Record_Generator
34
{
35
    /**
36
     * Array of AuditLog Options
37
     *
38
     * @var array
39
     */
40
    protected $_options = array('className'         => '%CLASS%Version',
41
                                'version'           => array('name'   => 'version',
42
                                                             'alias'  => null,
43
                                                             'type'   => 'integer',
44
                                                             'length' => 8,
45
                                                             'options' => array('primary' => true)),
46
                                'tableName'         => false,
47
                                'generateFiles'     => false,
48
                                'table'             => false,
49
                                'pluginTable'       => false,
50
                                'children'          => array(),
51
                                'auditLog'          => true,
52
                                'deleteVersions'    => true,
53
                                'cascadeDelete'     => true,
54
                                'excludeFields'     => array(),
55
                                'appLevelDelete'    => false);
56
 
57
    /**
58
     * Accepts array of options to configure the AuditLog
59
     *
60
     * @param   array $options An array of options
61
     * @return  void
62
     */
63
    public function __construct(array $options = array())
64
    {
65
        $this->_options = Doctrine_Lib::arrayDeepMerge($this->_options, $options);
66
    }
67
 
68
    public function buildRelation()
69
    {
70
        $this->buildForeignRelation('Version');
71
        $this->buildLocalRelation();
72
    }
73
 
74
    /**
75
     * Set the table definition for the audit log table
76
     *
77
     * @return  void
78
     */
79
    public function setTableDefinition()
80
    {
81
        $name = $this->_options['table']->getComponentName();
82
 
83
        // Building columns
84
        $columns = $this->_options['table']->getColumns();
85
 
86
        // remove all sequence, autoincrement and unique constraint definitions and add to the behavior model
87
        foreach ($columns as $column => $definition) {
88
            if (in_array($column, $this->_options['excludeFields'])) {
89
                continue;
90
            }
91
            unset($definition['autoincrement']);
92
            unset($definition['sequence']);
93
            unset($definition['unique']);
94
 
95
            $fieldName = $this->_options['table']->getFieldName($column);
96
            if ($fieldName != $column) {
97
                $name = $column . ' as ' . $fieldName;
98
            } else {
99
                $name = $fieldName;
100
            }
101
 
102
            $this->hasColumn($name, $definition['type'], $definition['length'], $definition);
103
        }
104
 
105
        // the version column should be part of the primary key definition
106
        $this->hasColumn(
107
	        $this->_options['version']['name'],
108
            $this->_options['version']['type'],
109
            $this->_options['version']['length'],
110
            $this->_options['version']['options']);
111
    }
112
 
113
    /**
114
     * Get array of information for the passed record and the specified version
115
     *
116
     * @param   Doctrine_Record $record
117
     * @param   integer         $version
118
     * @param   integer         $hydrationMode
119
     * @param	boolean			$asCollection
120
     * @return  array           An array or Doctrine_Collection or a Doctrine_Record
121
     */
122
    public function getVersion(Doctrine_Record $record, $version, $hydrationMode = Doctrine_Core::HYDRATE_ARRAY, $asCollection = true)
123
    {
124
        $className = $this->_options['className'];
125
        $method    = ($asCollection) ? 'execute' : 'fetchOne';
126
 
127
        $q = Doctrine_Core::getTable($className)
128
            ->createQuery();
129
 
130
        $values = array();
131
        foreach ((array) $this->_options['table']->getIdentifier() as $id) {
132
            $conditions[] = $className . '.' . $id . ' = ?';
133
            $values[] = $record->get($id);
134
        }
135
 
136
        $where = implode(' AND ', $conditions) . ' AND ' . $className . '.' . $this->_options['version']['name'] . ' = ?';
137
 
138
        $values[] = $version;
139
 
140
        $q->where($where);
141
 
142
        return $q->$method($values, $hydrationMode);
143
    }
144
 
145
    /**
146
     * Get the max version number for a given Doctrine_Record
147
     *
148
     * @param Doctrine_Record $record
149
     * @return Integer $versionnumber
150
     */
151
    public function getMaxVersion(Doctrine_Record $record)
152
    {
153
        $className = $this->_options['className'];
154
        $select = 'MAX(' . $className . '.' . $this->_options['version']['name'] . ') max_version';
155
 
156
        foreach ((array) $this->_options['table']->getIdentifier() as $id) {
157
            $conditions[] = $className . '.' . $id . ' = ?';
158
            $values[] = $record->get($id);
159
        }
160
 
161
        $q = Doctrine_Core::getTable($className)
162
            ->createQuery()
163
            ->select($select)
164
            ->where(implode(' AND ',$conditions));
165
 
166
        $result = $q->execute($values, Doctrine_Core::HYDRATE_ARRAY);
167
 
168
        return isset($result[0]['max_version']) ? $result[0]['max_version']:0;
169
    }
170
}