-
Notifications
You must be signed in to change notification settings - Fork 2
/
flour_app_model.php
274 lines (242 loc) · 5.7 KB
/
flour_app_model.php
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<?php
/**
* Flour App Model
*
* @package flour
* @author Dirk Brünsicke
* @version $Id$
* @copyright bruensicke.com GmbH
**/
class FlourAppModel extends AppModel
{
var $tablePrefix = 'flour_';
/**
* flour beforeSave
*
* @access public
*/
function beforeSave()
{
$this->_addUserdata();
return true;
}
/**
* flour soft delete
*
* @access public
*/
function delete($id = null, $cascade = true) {
if (!empty($id)) {
$this->id = $id;
}
$id = $this->id;
if(!$this->hasField('deleted')) {
return parent::delete($id, $cascade);
}
$user = $this->_getUser();
if($user) {
$this->data[$this->alias]['deleted_by'] = $user;
}
$this->data[$this->alias]['deleted'] = date('Y-m-d H:i:s');
// TODO: handle cascade delete / soft delete
return $this->save($this->data);
}
/**
* flour undelete
*
* @access public
*/
function undelete($id) {
$this->id = $id;
if(!$this->hasField('deleted') || !$this->exists()){
return false;
}
$this->data = $this->read(null, $id);
$this->_addUserData();
$this->data[$this->alias]['deleted'] = null;
$this->data[$this->alias]['deleted_by'] = null;
return $this->save($this->data);
}
/**
* flour find
*
* @var array
* @access private
* @todo add missing params to fulfill the standard find() signature
*/
function find($type, $options = array())
{
if (!isset($options['conditions']) ) {
$options['conditions'] = array();
}
if (!isset($options['contain']) ) {
// This kills all non-explicit contains like hasMany etc
// $options['contain'] = array();
}
if (!isset($options['order']) ) {
$options['order'] = array();
}
if($this->hasField('deleted') && ! isset($options['conditions'][$this->alias.'.'.$this->primaryKey])) {
$options['conditions'][$this->alias.'.deleted'] = null;
}
switch ($type) {
case 'active':
$this->_setActive($options, $type);
break;
case 'current':
$this->_setCurrent($options, $type);
break;
case 'editions':
$this->_setEditions($options, $type);
break;
case 'deleted':
$this->_setDeleted($options, $type);
break;
}
return parent::find($type, $options);
}
/**
* flour find
*
* @var array
* @access private
* @todo add missing params to fulfill the standard find() signature
*/
function _setActive(&$options, &$type)
{
$this->_setValid($options, $type);
$options['conditions'] = array_merge(
$options['conditions'],
array(
$this->alias.'.status >' => 0,
)
);
$type = (isset($options['conditions'][$this->alias.'.'.$this->primaryKey]))
? 'first'
: 'all';
}
/**
* undocumented function
*
* @param string $options
* @param string $type
* @return void
*/
function _setCurrent(&$options, &$type) {
$this->_setValid($options, $type);
$options['conditions'] = array_merge(
$options['conditions'],
array(
$this->alias.'.status >' => 0,
)
);
$options['order'] = array_merge(
$options['order'],
array(
$this->alias.'.valid_from' => 'DESC',
)
);
$type = 'first';
}
/**
* undocumented function
*
* @param string $options
* @param string $type
* @return void
* @access public
*/
function _setEditions(&$options, &$type) {
$slug = null;
// find all editions with same slug by reference id:
if(isset($options['conditions'][$this->alias.'.'.$this->primaryKey])) {
$slug = $this->field('slug', $options['conditions']);
}
// find all editions with given slug
if(isset($options['conditions'][$this->alias.'.slug'])) {
$slug = $options['conditions'][$this->alias.'.slug'];
}
unset($options['conditions'][$this->alias.'.'.$this->primaryKey]);
$options['conditions'] = array_merge(
$options['conditions'],
array(
$this->alias.'.slug' => $slug,
)
);
$type = 'all';
}
function _setDeleted(&$options, &$type) {
unset($options['conditions'][$this->alias.'.deleted']);
$options['conditions'] = array_merge(
$options['conditions'],
array( 'NOT' => array(
$this->alias.'.deleted' => NULL
),
)
);
$type = 'all';
}
function _setValid(&$options, &$type) {
if(!$this->hasField('valid_from') || !$this->hasField('valid_to')) {
return;
}
$options['conditions'] = array_merge(
$options['conditions'],
array(
'AND' => array(
array(
'OR' => array(
$this->alias.'.valid_from <=' => date('Y-m-d H:i:s'),
$this->alias.'.valid_from' => null,
)
),
array(
'OR' => array(
$this->alias.'.valid_to >=' => date('Y-m-d H:i:s'),
$this->alias.'.valid_to' => null,
),
),
),
)
);
}
function _addUserData()
{
$user = $this->_getUser();
if(!$user) return false;
if(!isset($this->data[$this->alias][$this->primaryKey])) {
$this->data[$this->alias]['created_by'] = $user;
}
$this->data[$this->alias]['modified_by'] = $user;
}
function _getUser()
{
$user = Configure::read('App.User.id');
if(!empty($user)) return $user;
$user = Configure::read('Auth.User.id');
if(!empty($user)) return $user;
return false;
}
/**
* Customized paginateCount method (copied from CakeDC Tags.Taggable Plugin)
*
* @param array
* @param integer
* @param array
* @return
* @access public
*/
public function paginateCount($conditions = array(), $recursive = 0, $extra = array()) {
$parameters = compact('conditions');
if ($recursive != $this->recursive) {
$parameters['recursive'] = $recursive;
}
if (isset($extra['type']) && isset($this->_findMethods[$extra['type']])) {
$extra['operation'] = 'count';
return $this->find($extra['type'], array_merge($parameters, $extra));
} else {
return $this->find('count', array_merge($parameters, $extra));
}
}
}
?>