| 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_Template_Versionable
|
|
|
24 |
*
|
|
|
25 |
* Add revisioning/audit log to your models
|
|
|
26 |
*
|
|
|
27 |
* @package Doctrine
|
|
|
28 |
* @subpackage Template
|
|
|
29 |
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
30 |
* @link www.doctrine-project.org
|
|
|
31 |
* @since 1.0
|
|
|
32 |
* @version $Revision$
|
|
|
33 |
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
|
|
34 |
*/
|
|
|
35 |
class Doctrine_Template_Versionable extends Doctrine_Template
|
|
|
36 |
{
|
|
|
37 |
/**
|
|
|
38 |
* Array of AuditLog Options
|
|
|
39 |
*
|
|
|
40 |
* @var array
|
|
|
41 |
*/
|
|
|
42 |
protected $_options = array('version' => array('name' => 'version',
|
|
|
43 |
'alias' => null,
|
|
|
44 |
'type' => 'integer',
|
|
|
45 |
'length' => 8,
|
|
|
46 |
'options' => array()),
|
|
|
47 |
'generateRelations' => true,
|
|
|
48 |
'tableName' => false,
|
|
|
49 |
'generateFiles' => false,
|
|
|
50 |
'auditLog' => true,
|
|
|
51 |
'deleteVersions' => true,
|
|
|
52 |
'listener' => 'Doctrine_AuditLog_Listener');
|
|
|
53 |
|
|
|
54 |
/**
|
|
|
55 |
* __construct
|
|
|
56 |
*
|
|
|
57 |
* @param array $options
|
|
|
58 |
* @return void
|
|
|
59 |
*/
|
|
|
60 |
public function __construct(array $options = array())
|
|
|
61 |
{
|
|
|
62 |
parent::__construct($options);
|
|
|
63 |
$this->_plugin = new Doctrine_AuditLog($this->_options);
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
/**
|
|
|
67 |
* Setup the Versionable behavior for the template
|
|
|
68 |
*
|
|
|
69 |
* @return void
|
|
|
70 |
*/
|
|
|
71 |
public function setUp()
|
|
|
72 |
{
|
|
|
73 |
if ($this->_plugin->getOption('auditLog')) {
|
|
|
74 |
$this->_plugin->initialize($this->_table);
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
$version = $this->_options['version'];
|
|
|
78 |
$name = $version['name'] . (isset($version['alias']) ? ' as ' . $version['alias'] : '');
|
|
|
79 |
$this->hasColumn($name, $version['type'], $version['length'], $version['options']);
|
|
|
80 |
|
|
|
81 |
$listener = $this->_options['listener'];
|
|
|
82 |
$this->addListener(new $listener($this->_plugin));
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
/**
|
|
|
86 |
* Get plugin for Versionable template
|
|
|
87 |
*
|
|
|
88 |
* @return void
|
|
|
89 |
*/
|
|
|
90 |
public function getAuditLog()
|
|
|
91 |
{
|
|
|
92 |
return $this->_plugin;
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
/**
|
|
|
96 |
* revert
|
|
|
97 |
* reverts this record to given version, this method only works if versioning plugin
|
|
|
98 |
* is enabled
|
|
|
99 |
*
|
|
|
100 |
* @throws Doctrine_Record_Exception if given version does not exist
|
|
|
101 |
* @param integer $version an integer > 1
|
|
|
102 |
* @return Doctrine_Record this object
|
|
|
103 |
*/
|
|
|
104 |
public function revert($version)
|
|
|
105 |
{
|
|
|
106 |
$auditLog = $this->_plugin;
|
|
|
107 |
|
|
|
108 |
if ( ! $auditLog->getOption('auditLog')) {
|
|
|
109 |
throw new Doctrine_Record_Exception('Audit log is turned off, no version history is recorded.');
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
$data = $auditLog->getVersion($this->getInvoker(), $version);
|
|
|
113 |
|
|
|
114 |
if ( ! isset($data[0])) {
|
|
|
115 |
throw new Doctrine_Record_Exception('Version ' . $version . ' does not exist!');
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
$this->getInvoker()->merge($data[0]);
|
|
|
119 |
|
|
|
120 |
|
|
|
121 |
return $this->getInvoker();
|
|
|
122 |
}
|
|
|
123 |
}
|