~patrix-sbs/oraculum/git

« back to all changes in this revision

Viewing changes to models/doctrine/lib/Doctrine/Template/Listener/SoftDelete.php

  • Committer: Patrick Kaminski
  • Date: 2009-09-02 02:33:07 UTC
  • Revision ID: git-v1:943803254fca67bfb4c0374422b1b836b14dc518
Tags: v0.1a
Sending Oraculum Framework v0.1 alpha

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.phpdoctrine.org>.
 
20
 */
 
21
 
 
22
/**
 
23
 * Listener for SoftDelete behavior which will allow you to turn on the behavior which
 
24
 * sets a delete flag instead of actually deleting the record and all queries automatically
 
25
 * include a check for the deleted flag to exclude deleted records.
 
26
 *
 
27
 * @package     Doctrine
 
28
 * @subpackage  Template
 
29
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 
30
 * @link        www.phpdoctrine.org
 
31
 * @since       1.0
 
32
 * @version     $Revision$
 
33
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
 
34
 * @author      Jonathan H. Wage <jonwage@gmail.com>
 
35
 */
 
36
class Doctrine_Template_Listener_SoftDelete extends Doctrine_Record_Listener
 
37
{
 
38
    /**
 
39
     * Array of SoftDelete options
 
40
     *
 
41
     * @var string
 
42
     */
 
43
    protected $_options = array();
 
44
 
 
45
    /**
 
46
     * __construct
 
47
     *
 
48
     * @param string $options 
 
49
     * @return void
 
50
     */
 
51
    public function __construct(array $options)
 
52
    {
 
53
        $this->_options = $options;
 
54
    }
 
55
 
 
56
    /**
 
57
     * Skip the normal delete options so we can override it with our own
 
58
     *
 
59
     * @param Doctrine_Event $event
 
60
     * @return void
 
61
     */
 
62
    public function preDelete(Doctrine_Event $event)
 
63
    {
 
64
        $event->skipOperation();
 
65
    }
 
66
 
 
67
    /**
 
68
     * Implement postDelete() hook and set the deleted flag to true
 
69
     *
 
70
     * @param Doctrine_Event $event
 
71
     * @return void
 
72
     */
 
73
    public function postDelete(Doctrine_Event $event)
 
74
    {
 
75
        $name = $this->_options['name'];
 
76
        $event->getInvoker()->$name = true;
 
77
        $event->getInvoker()->save();
 
78
    }
 
79
 
 
80
    /**
 
81
     * Implement preDqlDelete() hook and modify a dql delete query so it updates the deleted flag
 
82
     * instead of deleting the record
 
83
     *
 
84
     * @param Doctrine_Event $event
 
85
     * @return void
 
86
     */
 
87
    public function preDqlDelete(Doctrine_Event $event)
 
88
    {
 
89
        $params = $event->getParams();
 
90
        $field = $params['alias'] . '.' . $this->_options['name'];
 
91
        $query = $event->getQuery();
 
92
        if ( ! $query->contains($field)) {
 
93
            $query->from('')->update($params['component']['table']->getOption('name') . ' ' . $params['alias']);
 
94
            $query->set($field, '?', array('true'));
 
95
            $query->addWhere(
 
96
                $field . ' = ' . $query->getConnection()->convertBooleans(false) . ' OR ' . $field . ' IS NULL'
 
97
            );
 
98
        }
 
99
    }
 
100
 
 
101
    /**
 
102
     * Implement preDqlSelect() hook and add the deleted flag to all queries for which this model 
 
103
     * is being used in.
 
104
     *
 
105
     * @param Doctrine_Event $event 
 
106
     * @return void
 
107
     */
 
108
    public function preDqlSelect(Doctrine_Event $event)
 
109
    {
 
110
        $params = $event->getParams();
 
111
        $field = $params['alias'] . '.' . $this->_options['name'];
 
112
        $query = $event->getQuery();
 
113
        if ( ! $query->contains($field)) {
 
114
            $query->addWhere(
 
115
                $field . ' = ' . $query->getConnection()->convertBooleans(false) . ' OR ' . $field . ' IS NULL'
 
116
            );
 
117
        }
 
118
    }
 
119
}
 
 
b'\\ No newline at end of file'