-
Notifications
You must be signed in to change notification settings - Fork 3
/
app_model.php
438 lines (410 loc) · 12.1 KB
/
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
<?php
/**
* App model class.
*
* @copyright Copyright 2010, *ROCK*HARBOR
* @link http://rockharbor.org *ROCK*HARBOR
* @package core
* @subpackage core.app
*/
/**
* App model
*
* All models should extend this class
*
* @package core
* @subpackage core.app
*/
class AppModel extends Model {
/**
* Default recursive property
*
* @var integer
*/
public $recursive = -1;
/**
* Behaviors to attach
*
* @var array
*/
public $actsAs = array(
'Sanitizer.Sanitize',
'Cacher.Cache'
);
/**
* Extends model construction
*
* ### Extended functionality:
* - allows use of :ALIAS: in virtual field definitions to be replaced with the
* model's alias
*
* @param mixed $id Sets the model's id on startup
* @param string $table The name of the database table to use
* @param string $ds The datasource connection name
* @see Model::__construct()
*/
public function __construct($id = false, $table = null, $ds = null) {
parent::__construct($id, $table, $ds);
foreach ($this->virtualFields as &$virtualField) {
$virtualField = String::insert(
$virtualField,
array(
'ALIAS' => $this->alias,
),
array(
'after' => ':'
)
);
}
$this->order = String::insert(
$this->order,
array(
'ALIAS' => $this->alias,
),
array(
'after' => ':'
)
);
}
/**
* Takes a set of generic conditions and scopes them according to this model. If
* a model is defined in the condition, it will keep that model's scope. Only
* creates conditions if the model has the field.
*
* {{{
* // assuming we're on the user model
* array('active' => false, 'Ministry.active' => true, 'nonexistentfield' => 'val');
* // becomes
* array('User.active' => false, 'Ministry.active' => true);
* }}}
*
* @param array $conditions Generic conditions
*/
public function scopeConditions($conditions = array()) {
$scoped = array();
foreach ($conditions as $field => $value) {
$exp = explode('.', $field);
$model = $exp[0];
if (!isset($exp[1])) {
$field = $model;
$model = $this->alias;
} else {
$field = $exp[1];
}
if ($model == $this->alias && !$this->hasField($field, true)) {
continue;
}
$scoped[$model.'.'.$field] = $value;
}
return $scoped;
}
/**
* Sets the default image based on the results of a find. Only sets the default
* image if:
* - An image was included in the find conditions (or contained)
* - A default image app setting key exists
* - The default image app setting is actually set
*
* Sets the 'ImageIcon' key if
* - One is not set
* - If the model has an image, that will be used as the icon. If not and a
* default one exists, the default will be used.
*
* @param array $results The results of a find
* @return array (Perhaps) modified results
*/
public function defaultImage($results) {
$offset = false;
if (isset($results['Image'])) {
$offset =& $results;
} elseif (isset($results[$this->alias]['Image'])) {
$offset =& $results[$this->alias];
}
if ($offset) {
if (empty($offset['Image']) || isset($offset['Image']['id']) && empty($offset['Image']['id'])) {
$default = Core::read(strtolower(Inflector::pluralize($this->alias)).'.default_image');
$icon = Core::read(strtolower(Inflector::pluralize($this->alias)).'.default_icon');
if (!$icon) {
$icon = $default;
}
if ($default && isset($default['id'])) {
if (isset($this->hasOne['Image'])) {
$offset['Image'] = $default;
$offset['ImageIcon'] = $icon;
} elseif (isset($this->hasMany['Image'])) {
$offset['Image'] = array($default);
$offset['ImageIcon'] = $icon;
}
}
} else {
if (isset($this->hasOne['Image'])) {
$offset['ImageIcon'] = $offset['Image'];
} elseif (isset($this->hasMany['Image'])) {
$offset['ImageIcon'] = $offset['Image'][0];
}
}
}
return $results;
}
/**
* Creates a LIKE '%foo%' AND LIKE '%bar%' statement as defined in filterArgs
*
* ### Options:
* - including a `operator` key in the filterArg will change the operator (AND
* or OR)
*
* @param array $data The key value pair for the filterArg's name to the query
* @return string
*/
public function makeLikeConditions($data = array()) {
$filterName = key($data);
$filter = Set::extract('/.[name='.$filterName.']', $this->filterArgs);
if (!isset($filter[0]['field'])) {
$filter[0]['field'] = $this->alias.'.'.$this->displayField;
}
if (!isset($filter[0]['operator'])) {
$filter[0]['operator'] = 'AND';
}
$field = $filter[0]['field'];
$operator = $filter[0]['operator'];
$query = $data[$filterName];
if (!is_array($field)) {
$field = array($field);
}
$conditions = array();
foreach ($field as $val) {
$conditions[$val.' LIKE'] = $query.'%';
}
if (strtoupper($operator) === 'AND') {
return $conditions;
} else {
return array(
$operator => $conditions
);
}
}
/**
* Creates a find options array from post data. If the POSTed data has fields
* from this model, they will be added to this model's `fields` list. If it has
* other models, it will create a contain array with those models and their fields.
*
* Use in conjunction with Controller::postConditions() to make search forms super-quick!
*
* @param array $data The Cake post data
* @param Model $Model The model
* @param string $foreignKey A foreign key for the association to include
* @return array The options array
*/
public function postOptions($data, $Model = null, $foreignKey = null) {
// get associated models
$first = false;
if (!$Model) {
$first = true;
$Model = $this;
$data = array_filter_recursive($data);
}
$associated = $Model->getAssociated();
$options = $data;
if ($foreignKey) {
$options['fields'][] = $foreignKey;
}
foreach ($data as $model => $field) {
if ($Model->hasField($model, true)) {
// add to fields array if it's a field
if ($Model->isVirtualField($model)) {
$options['fields'][] = $Model->getVirtualField($model).' AS '.$Model->alias.'__'.$model;
} else {
$options['fields'][] = $model;
}
unset($options[$model]);
} elseif ($Model->alias === $model) {
foreach ($field as $f => $v) {
$options['fields'][] = $f;
}
unset($options[$model]);
} elseif (array_key_exists($model, $associated)) {
// check for habtm [Publication][Publication][0] = 1
if ($model == array_shift(array_keys($field))) {
$field = array();
}
// recusively check for more models to contain
$foreignKey = null;
if (in_array($associated[$model], array('hasOne', 'hasMany'))) {
$foreignKey = $Model->{$associated[$model]}[$model]['foreignKey'];
} elseif ($associated[$model] == 'belongsTo') {
$options['fields'][] = $Model->primaryKey;
$options['fields'][] = $Model->{$associated[$model]}[$model]['foreignKey'];
}
if ($first) {
$options['contain'][$model] = $this->postOptions($field, $Model->{$model}, $foreignKey);
unset($options[$model]);
} else {
$options[$model] = $this->postOptions($field, $Model->{$model}, $foreignKey);
}
} else {
// completely unrelated
unset($options[$model]);
}
}
return $options;
}
/**
* Checks if a user owns a record
*
* @param integer $userId The user's id
* @param integer $modelId The model id. By default it uses Model::id.
* @return boolean
*/
public function ownedBy($userId = null, $modelId = null) {
if (!$this->id || $modelId) {
$this->id = $modelId;
}
if (!$userId || !$this->id) {
return false;
}
$class = $field = null;
// get the field the user id might be in
$fields = array('user_id', 'model_id', 'foreign_key', 'id', 'payment_placed_by');
$field = 'user_id';
$f = 0;
while (!$this->hasField($field) && $f < count($fields)) {
$field = $fields[$f];
$f++;
}
// check for class/model field if it's a polymorphic model
if (!in_array($field, array('id', 'user_id'))) {
$fields = array('model', 'class');
foreach ($fields as $classField) {
if ($this->hasField($classField)) {
$class = $classField;
break;
}
}
return $this->hasAny(array(
$field => $userId,
$class => 'User'
));
}
return $this->field($field) == $userId;
}
/**
* Toggles the `active` field
*
* @param integer $id Id of model
* @param boolean $active Whether to make the model inactive or active
* @param boolean $recursive Whether to iterate through the model's relationships and mark them as $active
* @return boolean Success
*/
public function toggleActivity($id = null, $active = false, $recursive = false) {
if (!$id) {
return false;
}
$this->id = $id;
$this->recursive = 1;
$data = $this->read(null, $id);
if ($recursive) {
foreach ($this->hasOne as $hasOne => $config) {
// get id
$hasOneId = $data[$hasOne][$this->{$hasOne}->primaryKey];
// only go one level deep because most everything leads back to user
if ($id) {
$this->{$hasOne}->toggleActivity($hasOneId, $active, false);
}
}
foreach ($this->hasMany as $hasMany => $config) {
// get ids
$hasManyIds = Set::extract('/'.$hasMany.'/'.$this->{$hasMany}->primaryKey, $data);
// only go one level deep because most everything leads back to user
foreach ($hasManyIds as $disableId) {
$this->{$hasMany}->toggleActivity($disableId, $active, false);
}
}
}
if ($this->hasField('active')) {
return $this->saveField('active', $active) ? true : false;
} else {
return false;
}
}
/**
* Validation rule that checks to see if the passed
* field matches another field
* (useful for password / password confirmation)
*
* @param array $data Passed by validator
* @param string $compareField Field to compare it to
* @return boolean True if it passes validation
*/
public function identicalFieldValues(&$data, $compareField) {
// $data array is passed using the form field name as the key
// have to extract the value to make the function generic
$value = array_values($data);
$comparewithvalue = $value[0];
return ($this->data[$this->name][$compareField] == $comparewithvalue);
}
/**
* Validation rule that invalidates the passed field _if_ other field(s) have a
* correct value. This validation rule should __always__ be placed last.
*
* #### Usage
*
* {{{
* 'field_name' => array(
* 'myRule' => array(
* 'rule' => 'email'
* ),
* 'validationRule' => array(
* 'rule' => array('eitherOr', array('some_field' => 'someValue'))
* )
* )
* }}}
*
* The above rule would validate if `field_name` was an email address *or*
* if `some_field` is equal to 'someValue'.
*
* @param array $data Data array
* @param array $orFields Array of field=>values to allow this field to pass
* @return boolean Always true, because this function only revalidates fields
*/
public function eitherOr(&$data, $orFields = array()) {
foreach ($orFields as $orField => $orValue) {
if ($this->data[$this->alias][$orField] == $orValue) {
unset($this->validationErrors[key($data)]);
}
}
return true;
}
/**
* Deconstructs complex data (specifically here, date) and creates a partial
* date from a Cake date array
*
* The Form helper in Cake splits dates into 3 pieces: month, day and year.
* If the column in the database is set to a string, we'll allow a "partial
* date" so users can, say, estimate the time they were baptized.
*
* @param string $field The name of the column
* @param array $value The complex value being saved
* @return mixed A string if it should be a date string, or deconstructed data
* as determined by Model::deconstruct()
* @see Model::deconstruct()
* @see FormHelper::dateTime()
*/
public function deconstruct($field, $value) {
if ($this->getColumnType($field) == 'string' && is_array($value)) {
if (isset($value['month']) && empty($value['month'])) {
$value['month'] = '00';
}
if (isset($value['day']) && empty($value['day'])) {
$value['day'] = '00';
}
if (isset($value['year']) && empty($value['year'])) {
$value['year'] = '0000';
}
// convert to proper type
if (array_key_exists('month', $value) && array_key_exists('day', $value) && array_key_exists('year', $value)) {
$value = $value['year'].'-'.$value['month'].'-'.$value['day'];
}
}
return parent::deconstruct($field, $value);
}
}