AuditLog.php 3.73 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
<?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>.
 */
21

zYne's avatar
zYne committed
22 23 24 25
/**
 * Doctrine_AuditLog
 *
 * @package     Doctrine
26
 * @subpackage  AuditLog
zYne's avatar
zYne committed
27 28 29 30 31 32
 * @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
33
class Doctrine_AuditLog extends Doctrine_Record_Generator
zYne's avatar
zYne committed
34
{
zYne's avatar
zYne committed
35 36
    protected $_options = array(
                            'className'     => '%CLASS%Version',
zYne's avatar
zYne committed
37 38
                            'versionColumn' => 'version',
                            'generateFiles' => false,
zYne's avatar
zYne committed
39
                            'table'         => false,
zYne's avatar
zYne committed
40
                            'pluginTable'   => false,
41
                            'children'      => array(),
zYne's avatar
zYne committed
42
                            );
zYne's avatar
zYne committed
43

44 45 46 47 48 49
    /**
     * Create a new auditlog_ 
     * 
     * @param array $options An array of options
     * @return void
     */
50
    public function __construct(array $options = array())
zYne's avatar
zYne committed
51
    {
52
        $this->_options = Doctrine_Lib::arrayDeepMerge($this->_options, $options);
zYne's avatar
zYne committed
53 54
    }

55 56 57 58 59 60 61
    /**
     * Get the version 
     * 
     * @param Doctrine_Record $record 
     * @param mixed $version 
     * @return array An array with version information
     */
zYne's avatar
zYne committed
62
    public function getVersion(Doctrine_Record $record, $version)
zYne's avatar
zYne committed
63 64 65
    {           
        $className = $this->_options['className'];

zYne's avatar
zYne committed
66
        $q = new Doctrine_Query();
zYne's avatar
zYne committed
67

zYne's avatar
zYne committed
68
        $values = array();
zYne's avatar
zYne committed
69
        foreach ((array) $this->_options['table']->getIdentifier() as $id) {
zYne's avatar
zYne committed
70 71 72 73 74 75
            $conditions[] = $className . '.' . $id . ' = ?';
            $values[] = $record->get($id);
        }
        $where = implode(' AND ', $conditions) . ' AND ' . $className . '.' . $this->_options['versionColumn'] . ' = ?';
        
        $values[] = $version;
zYne's avatar
zYne committed
76

zYne's avatar
zYne committed
77 78 79
        $q->from($className)
          ->where($where);

zYne's avatar
zYne committed
80
        return $q->execute($values, Doctrine::HYDRATE_ARRAY);
zYne's avatar
zYne committed
81
    }
82 83 84 85 86 87 88

    /**
     * buildDefinition for a table 
     * 
     * @param Doctrine_Table $table 
     * @return boolean true on success otherwise false.
     */
89
    public function setTableDefinition()
zYne's avatar
zYne committed
90
    {
91
        $name = $this->_options['table']->getComponentName();
zYne's avatar
zYne committed
92

93
        $columns = $this->_options['table']->getColumns();
94

95
        // remove all sequence, autoincrement and unique constraint definitions
96 97 98
        foreach ($columns as $column => $definition) {
            unset($columns[$column]['autoincrement']);
            unset($columns[$column]['sequence']);
99
            unset($columns[$column]['unique']);
100 101
        }

102
        $this->hasColumns($columns);
zYne's avatar
zYne committed
103

104 105
        // the version column should be part of the primary key definition
        $this->hasColumn($this->_options['versionColumn'], 'integer', 8, array('primary' => true));
zYne's avatar
zYne committed
106
    }
zYne's avatar
zYne committed
107
}