AuditLog.php 5.12 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 25 26 27 28 29 30 31
<?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
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @category    Object Relational Mapping
 * @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
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
                            );
zYne's avatar
zYne committed
40

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

zYne's avatar
zYne committed
43
    public function __construct($options)
zYne's avatar
zYne committed
44
    {
zYne's avatar
zYne committed
45
        $this->_options = array_merge($this->_options, $options);
zYne's avatar
zYne committed
46
    }
zYne's avatar
zYne committed
47 48 49 50 51 52 53 54 55
    /**
     * __get
     * an alias for getOption
     *
     * @param string $option
     */
    public function __get($option)
    {
        if (isset($this->options[$option])) {
zYne's avatar
zYne committed
56
            return $this->_options[$option];
zYne's avatar
zYne committed
57 58 59 60 61 62 63 64 65 66
        }
        return null;
    }
    /**
     * __isset
     *
     * @param string $option
     */
    public function __isset($option) 
    {
zYne's avatar
zYne committed
67
        return isset($this->_options[$option]);
zYne's avatar
zYne committed
68 69 70 71 72 73 74 75 76
    }
    /**
     * getOptions
     * returns all options of this table and the associated values
     *
     * @return array    all options and their values
     */
    public function getOptions()
    {
zYne's avatar
zYne committed
77
        return $this->_options;
zYne's avatar
zYne committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
    }
    /**
     * setOption
     * sets an option and returns this object in order to
     * allow flexible method chaining
     *
     * @see slef::$_options             for available options
     * @param string $name              the name of the option to set
     * @param mixed $value              the value of the option
     * @return Doctrine_AuditLog        this object
     */
    public function setOption($name, $value)
    {
        if ( ! isset($this->_options[$name])) {
            throw new Doctrine_Exception('Unknown option ' . $name);
        }
zYne's avatar
zYne committed
94
        $this->_options[$name] = $value;
zYne's avatar
zYne committed
95 96 97 98 99 100 101 102 103 104
    }
    /**
     * getOption
     * returns the value of given option
     *
     * @param string $name  the name of the option
     * @return mixed        the value of given option
     */
    public function getOption($name)
    {
zYne's avatar
zYne committed
105 106
        if (isset($this->_options[$name])) {
            return $this->_options[$name];
zYne's avatar
zYne committed
107 108 109
        }
        return null;
    }
zYne's avatar
zYne committed
110

zYne's avatar
zYne committed
111
    public function getVersion(Doctrine_Record $record, $version)
zYne's avatar
zYne committed
112 113 114
    {           
        $className = $this->_options['className'];

zYne's avatar
zYne committed
115
        $q = new Doctrine_Query();
zYne's avatar
zYne committed
116

zYne's avatar
zYne committed
117
        $values = array();
zYne's avatar
zYne committed
118
        foreach ((array) $this->_options['table']->getIdentifier() as $id) {
zYne's avatar
zYne committed
119 120 121 122 123 124
            $conditions[] = $className . '.' . $id . ' = ?';
            $values[] = $record->get($id);
        }
        $where = implode(' AND ', $conditions) . ' AND ' . $className . '.' . $this->_options['versionColumn'] . ' = ?';
        
        $values[] = $version;
zYne's avatar
zYne committed
125

zYne's avatar
zYne committed
126 127 128 129
        $q->from($className)
          ->where($where);

        return $q->execute($values, Doctrine_HYDRATE::HYDRATE_ARRAY);
zYne's avatar
zYne committed
130
    }
zYne's avatar
zYne committed
131
    public function buildDefinition(Doctrine_Table $table)
zYne's avatar
zYne committed
132
    {
zYne's avatar
zYne committed
133 134 135 136
        $this->_options['className'] = str_replace('%CLASS%', 
                                                   $this->_options['table']->getComponentName(),
                                                   $this->_options['className']);

zYne's avatar
zYne committed
137
        $name = $table->getComponentName();
zYne's avatar
zYne committed
138

zYne's avatar
zYne committed
139 140 141 142
        $className = $name . 'Version';
        
        if (class_exists($className)) {
            return false;
zYne's avatar
zYne committed
143
        }
zYne's avatar
zYne committed
144

zYne's avatar
zYne committed
145
        $columns = $table->getColumns();
zYne's avatar
zYne committed
146 147 148
        
        // the version column should be part of the primary key definition
        $columns[$this->_options['versionColumn']]['primary'] = true;
zYne's avatar
zYne committed
149

zYne's avatar
zYne committed
150
        $id = $table->getIdentifier();
zYne's avatar
zYne committed
151

zYne's avatar
zYne committed
152
        $options = array('className' => $className);
zYne's avatar
zYne committed
153

zYne's avatar
zYne committed
154
        $builder = new Doctrine_Import_Builder();
zYne's avatar
zYne committed
155

zYne's avatar
zYne committed
156
        $def = $builder->buildDefinition($options, $columns);
zYne's avatar
zYne committed
157

zYne's avatar
zYne committed
158 159 160 161
        if ( ! $this->_options['generateFiles']) {
            eval($def);
        }
        return true;
zYne's avatar
zYne committed
162 163
    }
}