AuditLog.php 4.11 KB
Newer Older
zYne's avatar
zYne committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
<?php
/*
 *  $Id$
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information, see
 * <http://www.phpdoctrine.com>.
 */
/**
 * Doctrine_AuditLog
 *
 * @package     Doctrine
25
 * @subpackage  AuditLog
zYne's avatar
zYne committed
26 27 28 29 30 31
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @link        www.phpdoctrine.com
 * @since       1.0
 * @version     $Revision$
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
 */
zYne's avatar
zYne committed
32
class Doctrine_AuditLog extends Doctrine_Plugin
zYne's avatar
zYne committed
33
{
zYne's avatar
zYne committed
34 35
    protected $_options = array(
                            'className'     => '%CLASS%Version',
zYne's avatar
zYne committed
36 37
                            'versionColumn' => 'version',
                            'generateFiles' => false,
zYne's avatar
zYne committed
38
                            'table'         => false,
zYne's avatar
zYne committed
39
                            'pluginTable'   => false,
zYne's avatar
zYne committed
40
                            );
zYne's avatar
zYne committed
41

zYne's avatar
zYne committed
42
    protected $_auditTable;
zYne's avatar
zYne committed
43

zYne's avatar
zYne committed
44
    public function __construct($options)
zYne's avatar
zYne committed
45
    {
zYne's avatar
zYne committed
46
        $this->_options = array_merge($this->_options, $options);
zYne's avatar
zYne committed
47 48
    }

zYne's avatar
zYne committed
49
    public function getVersion(Doctrine_Record $record, $version)
zYne's avatar
zYne committed
50 51 52
    {           
        $className = $this->_options['className'];

zYne's avatar
zYne committed
53
        $q = new Doctrine_Query();
zYne's avatar
zYne committed
54

zYne's avatar
zYne committed
55
        $values = array();
zYne's avatar
zYne committed
56
        foreach ((array) $this->_options['table']->getIdentifier() as $id) {
zYne's avatar
zYne committed
57 58 59 60 61 62
            $conditions[] = $className . '.' . $id . ' = ?';
            $values[] = $record->get($id);
        }
        $where = implode(' AND ', $conditions) . ' AND ' . $className . '.' . $this->_options['versionColumn'] . ' = ?';
        
        $values[] = $version;
zYne's avatar
zYne committed
63

zYne's avatar
zYne committed
64 65 66
        $q->from($className)
          ->where($where);

zYne's avatar
zYne committed
67
        return $q->execute($values, Doctrine::HYDRATE_ARRAY);
zYne's avatar
zYne committed
68
    }
zYne's avatar
zYne committed
69
    public function buildDefinition(Doctrine_Table $table)
zYne's avatar
zYne committed
70
    {
zYne's avatar
zYne committed
71 72 73 74
        $this->_options['className'] = str_replace('%CLASS%', 
                                                   $this->_options['table']->getComponentName(),
                                                   $this->_options['className']);

zYne's avatar
zYne committed
75
        $name = $table->getComponentName();
zYne's avatar
zYne committed
76

zYne's avatar
zYne committed
77
        $className = $name . 'Version';
78 79

        // check that class doesn't exist (otherwise we cannot create it)
zYne's avatar
zYne committed
80 81
        if (class_exists($className)) {
            return false;
zYne's avatar
zYne committed
82
        }
zYne's avatar
zYne committed
83

zYne's avatar
zYne committed
84
        $columns = $table->getColumns();
85

86
        // remove all sequence, autoincrement and unique constraint definitions
87 88 89
        foreach ($columns as $column => $definition) {
            unset($columns[$column]['autoincrement']);
            unset($columns[$column]['sequence']);
90
            unset($columns[$column]['unique']);
91 92
        }

zYne's avatar
zYne committed
93 94
        // the version column should be part of the primary key definition
        $columns[$this->_options['versionColumn']]['primary'] = true;
zYne's avatar
zYne committed
95

zYne's avatar
zYne committed
96
        $id = $table->getIdentifier();
zYne's avatar
zYne committed
97

zYne's avatar
zYne committed
98
        $options = array('className' => $className);
99
        
zYne's avatar
zYne committed
100
        $relations = array($name => array('local' => $id,
101 102
                                          'foreign' => $id, 
                                          'onDelete' => 'CASCADE',
zYne's avatar
zYne committed
103
                                          'onUpdate' => 'CASCADE'));
zYne's avatar
zYne committed
104

zYne's avatar
zYne committed
105
        $this->generateClass($options, $columns, array());
zYne's avatar
zYne committed
106 107
        
        $this->_options['pluginTable'] = $table->getConnection()->getTable($this->_options['className']);
zYne's avatar
zYne committed
108

zYne's avatar
zYne committed
109
        return true;
zYne's avatar
zYne committed
110 111
    }
}