1
0
mirror of synced 2025-01-18 22:41:43 +03:00
doctrine2/lib/Doctrine/AuditLog.php

133 lines
4.7 KiB
PHP
Raw Normal View History

2007-05-31 22:00:59 +00:00
<?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>.
*/
2007-05-31 22:00:59 +00:00
/**
* Doctrine_AuditLog
*
* @package Doctrine
* @subpackage AuditLog
2007-05-31 22:00:59 +00:00
* @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>
*/
2007-09-02 11:29:14 +00:00
class Doctrine_AuditLog extends Doctrine_Plugin
2007-05-31 22:00:59 +00:00
{
2007-06-05 23:58:11 +00:00
protected $_options = array(
'className' => '%CLASS%Version',
2007-07-17 20:45:10 +00:00
'versionColumn' => 'version',
'generateFiles' => false,
2007-07-17 21:10:18 +00:00
'table' => false,
2007-10-13 09:01:03 +00:00
'pluginTable' => false,
2007-06-05 23:58:11 +00:00
);
2007-07-17 20:45:10 +00:00
/**
* Create a new auditlog_
*
* @param array $options An array of options
* @return void
*/
2007-07-17 20:45:10 +00:00
public function __construct($options)
2007-06-05 23:58:11 +00:00
{
2007-07-17 20:45:10 +00:00
$this->_options = array_merge($this->_options, $options);
2007-06-05 23:58:11 +00:00
}
/**
* Get the version
*
* @param Doctrine_Record $record
* @param mixed $version
* @return array An array with version information
*/
2007-06-07 20:46:08 +00:00
public function getVersion(Doctrine_Record $record, $version)
2007-07-17 21:10:18 +00:00
{
$className = $this->_options['className'];
2007-06-07 20:46:08 +00:00
$q = new Doctrine_Query();
2007-07-17 21:10:18 +00:00
2007-06-07 20:46:08 +00:00
$values = array();
2007-07-17 21:10:18 +00:00
foreach ((array) $this->_options['table']->getIdentifier() as $id) {
2007-06-07 20:46:08 +00:00
$conditions[] = $className . '.' . $id . ' = ?';
$values[] = $record->get($id);
}
$where = implode(' AND ', $conditions) . ' AND ' . $className . '.' . $this->_options['versionColumn'] . ' = ?';
$values[] = $version;
2007-06-05 23:58:11 +00:00
2007-07-17 21:10:18 +00:00
$q->from($className)
->where($where);
2007-10-08 22:45:34 +00:00
return $q->execute($values, Doctrine::HYDRATE_ARRAY);
2007-06-07 20:46:08 +00:00
}
/**
* buildDefinition for a table
*
* @param Doctrine_Table $table
* @return boolean true on success otherwise false.
*/
2007-07-17 20:45:10 +00:00
public function buildDefinition(Doctrine_Table $table)
2007-07-18 19:31:43 +00:00
{
2007-07-18 19:18:30 +00:00
$this->_options['className'] = str_replace('%CLASS%',
$this->_options['table']->getComponentName(),
$this->_options['className']);
2007-07-17 20:45:10 +00:00
$name = $table->getComponentName();
2007-06-05 23:58:11 +00:00
2007-07-17 20:45:10 +00:00
$className = $name . 'Version';
// check that class doesn't exist (otherwise we cannot create it)
2007-07-17 20:45:10 +00:00
if (class_exists($className)) {
return false;
2007-06-05 23:58:11 +00:00
}
2007-06-06 00:12:44 +00:00
2007-07-17 20:45:10 +00:00
$columns = $table->getColumns();
// remove all sequence, autoincrement and unique constraint definitions
foreach ($columns as $column => $definition) {
unset($columns[$column]['autoincrement']);
unset($columns[$column]['sequence']);
unset($columns[$column]['unique']);
}
2007-07-18 19:31:43 +00:00
// the version column should be part of the primary key definition
2007-10-18 19:39:19 +00:00
$columns[$this->_options['versionColumn']] = array('type' => 'integer',
'length' => 8,
'primary' => true);
2007-06-06 00:12:44 +00:00
2007-07-17 20:45:10 +00:00
$id = $table->getIdentifier();
2007-06-07 18:21:07 +00:00
2007-07-17 20:45:10 +00:00
$options = array('className' => $className);
2007-10-13 21:40:43 +00:00
$relations = array($name => array('local' => $id,
'foreign' => $id,
'onDelete' => 'CASCADE',
2007-10-13 21:40:43 +00:00
'onUpdate' => 'CASCADE'));
2007-06-05 23:58:11 +00:00
2007-10-13 09:01:42 +00:00
$this->generateClass($options, $columns, array());
2007-10-13 09:01:03 +00:00
$this->_options['pluginTable'] = $table->getConnection()->getTable($this->_options['className']);
2007-06-05 18:38:00 +00:00
2007-07-17 20:45:10 +00:00
return true;
2007-05-31 22:00:59 +00:00
}
}