1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Implementation of the paranoid behavior, so when you delete soemthing it does not dissapear but is set with a flag is_deleted |
5
|
|
|
* |
6
|
|
|
* @package Jam |
7
|
|
|
* @category Behavior |
8
|
|
|
* @author Ivan Kerin |
9
|
|
|
* @copyright (c) 2011-2012 Despark Ltd. |
10
|
|
|
* @license http://www.opensource.org/licenses/isc-license.txt |
11
|
|
|
*/ |
12
|
|
|
class Kohana_Jam_Behavior_Paranoid extends Jam_Behavior |
13
|
|
|
{ |
14
|
|
|
protected static $_default_filter = 'normal'; |
15
|
|
|
|
16
|
29 |
|
public static function filter($filter = NULL) |
17
|
|
|
{ |
18
|
29 |
|
if ($filter !== NULL) |
19
|
|
|
{ |
20
|
4 |
|
Jam_Behavior_Paranoid::$_default_filter = $filter; |
|
|
|
|
21
|
|
|
} |
22
|
|
|
|
23
|
29 |
|
return Jam_Behavior_Paranoid::$_default_filter; |
|
|
|
|
24
|
|
|
} |
25
|
|
|
|
26
|
1 |
|
public static function with_filter($filter, $closure) |
27
|
|
|
{ |
28
|
1 |
|
$current_filter = static::filter(); |
29
|
1 |
|
static::filter($filter); |
30
|
|
|
|
31
|
1 |
|
$result = call_user_func($closure); |
32
|
|
|
|
33
|
1 |
|
static::filter($current_filter); |
34
|
1 |
|
return $result; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
const ALL = 'all'; |
38
|
|
|
const DELETED = 'deleted'; |
39
|
|
|
const NORMAL = 'normal'; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The field used for checking if an item is deleated |
43
|
|
|
* |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
protected $_field = 'is_deleted'; |
47
|
|
|
|
48
|
|
|
public function initialize(Jam_Meta $meta, $name) |
49
|
|
|
{ |
50
|
|
|
parent::initialize($meta, $name); |
51
|
|
|
|
52
|
|
|
$meta |
53
|
|
|
->field($this->_field, Jam::field('boolean', array('default' => FALSE))); |
54
|
|
|
|
55
|
|
|
$meta->events() |
56
|
|
|
->bind('builder.before_select', array($this, 'builder_paranoid_filter')) |
57
|
|
|
->bind('builder.before_update', array($this, 'builder_paranoid_filter')); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Perform the actual where modification when it is needed |
62
|
|
|
* |
63
|
|
|
* @param Jam_Query_Builder_Select $builder |
64
|
|
|
*/ |
65
|
29 |
|
public function builder_paranoid_filter(Database_Query $builder) |
66
|
|
|
{ |
67
|
29 |
|
$filter_type = $builder->params('paranoid_filter_type') ?: Jam_Behavior_Paranoid::filter(); |
|
|
|
|
68
|
|
|
|
69
|
|
|
switch ($filter_type) |
70
|
|
|
{ |
71
|
29 |
|
case Jam_Behavior_Paranoid::ALL: |
72
|
4 |
|
break; |
73
|
|
|
|
74
|
29 |
|
case Jam_Behavior_Paranoid::DELETED: |
75
|
4 |
|
$builder->where($this->_field, '=', TRUE); |
|
|
|
|
76
|
4 |
|
break; |
77
|
|
|
|
78
|
29 |
|
case Jam_Behavior_Paranoid::NORMAL: |
79
|
|
|
default: |
80
|
29 |
|
$builder->where($this->_field, '=', FALSE); |
|
|
|
|
81
|
29 |
|
break; |
82
|
|
|
} |
83
|
29 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* $builder->deleted(Jam_Behavior_Paranoid::ALL), |
87
|
|
|
* $builder->deleted(Jam_Behavior_Paranoid::DELETED), |
88
|
|
|
* $builder->deleted(Jam_Behavior_Paranoid::NORMAL) |
89
|
|
|
* |
90
|
|
|
* @param Jam_Builder $builder |
91
|
|
|
* @param Jam_Event_Data $data |
92
|
|
|
* @param string $paranoid_filter_type |
93
|
|
|
*/ |
94
|
4 |
|
public function builder_call_deleted(Database_Query $builder, Jam_Event_Data $data, $paranoid_filter_type = Jam_Behavior_Paranoid::NORMAL) |
|
|
|
|
95
|
|
|
{ |
96
|
4 |
|
if ( ! in_array($paranoid_filter_type, array(Jam_Behavior_Paranoid::DELETED, Jam_Behavior_Paranoid::NORMAL, Jam_Behavior_Paranoid::ALL))) |
97
|
|
|
throw new Kohana_Exception("Deleted type should be Jam_Behavior_Paranoid::DELETED, Jam_Behavior_Paranoid::NORMAL or Jam_Behavior_Paranoid::ALL"); |
98
|
|
|
|
99
|
4 |
|
$builder->params('paranoid_filter_type', $paranoid_filter_type); |
|
|
|
|
100
|
4 |
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* $model->delete() Delete the item only if the "real delete" flag has been set to TRUE, otherwise set the 'is_deleted' column to TRUE |
104
|
|
|
* |
105
|
|
|
* @param Jam_Model $model |
106
|
|
|
* @param Jam_Event_Data $data |
107
|
|
|
*/ |
108
|
3 |
|
public function model_before_delete(Jam_Model $model, Jam_Event_Data $data) |
109
|
|
|
{ |
110
|
3 |
|
if ( ! $model->_real_delete) |
|
|
|
|
111
|
|
|
{ |
112
|
3 |
|
Jam::update($this->_model) |
113
|
3 |
|
->where_key($model->id()) |
114
|
3 |
|
->value($this->_field, TRUE) |
115
|
3 |
|
->execute(); |
116
|
|
|
|
117
|
3 |
|
$data->return = FALSE; |
118
|
|
|
} |
119
|
3 |
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* $model->real_delte() Set the flag 'real_delete' to true and perform the deletion |
123
|
|
|
* |
124
|
|
|
* @param Jam_Model $model |
125
|
|
|
* @param Jam_Event_Data $data |
126
|
|
|
*/ |
127
|
3 |
|
public function model_call_real_delete(Jam_Model $model, Jam_Event_Data $data) |
128
|
|
|
{ |
129
|
3 |
|
$model->_real_delete = TRUE; |
|
|
|
|
130
|
3 |
|
$data->stop = TRUE; |
131
|
3 |
|
$data->return = $model->delete(); |
132
|
3 |
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* $model->restore_delete() Perform this to "undelete" a model |
136
|
|
|
* |
137
|
|
|
* @param Jam_Model $model |
138
|
|
|
* @param Jam_Event_Data $data |
|
|
|
|
139
|
|
|
*/ |
140
|
3 |
|
public function model_call_restore_delete(Jam_Model $model) |
141
|
|
|
{ |
142
|
3 |
|
Jam::update($this->_model) |
|
|
|
|
143
|
3 |
|
->where_key($model->id()) |
144
|
3 |
|
->deleted(Jam_Behavior_Paranoid::DELETED) |
145
|
3 |
|
->value($this->_field, FALSE) |
146
|
3 |
|
->execute(); |
147
|
3 |
|
} |
148
|
|
|
} |
149
|
|
|
|
This check looks for access to properties that are not accessible from the current context.
If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.