forked from Zuluru/Zuluru
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_model.php
executable file
·549 lines (484 loc) · 16.6 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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
<?php
/**
* Application model for Cake.
*
* This file is application-wide model file. You can put all
* application-wide model-related methods here.
*
* PHP versions 4 and 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package cake
* @subpackage cake.cake.libs.model
* @since CakePHP(tm) v 0.2.9
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
/**
* Application model for Cake.
*
* Add your application-wide methods to the class, your models will inherit them.
*
* @package cake
* @subpackage cake.cake.libs.model
*/
class AppModel extends Model {
// Make all models containable; we make a lot of use of this feature for limiting
// which related data is loaded in any given find.
// TODO: Add trim, ProperCase behaviours where appropriate
var $actsAs = array('Containable');
// Some common-but-non-standard regexes we need in multiple models
const NAME_REGEX = '/^[ \p{Ll}\p{Lm}\p{Lo}\p{Lt}\p{Lu}\p{Nd}\-\.\',]+$/mu';
const EXTENDED_NAME_REGEX = '/^[ \p{Ll}\p{Lm}\p{Lo}\p{Lt}\p{Lu}\p{Nd}\-\.\'",\!\?@&()]*$/i';
const ADDRESS_REGEX = '/^[ \p{Ll}\p{Lm}\p{Lo}\p{Lt}\p{Lu}\p{Nd}\-\.\',#&]*$/i';
//
// Generic afterFind function, which handles data in the many different
// layouts it might appear in, and calls the model-specific _afterFind
// method (if it exists) on the individual records for any adjustments
// that are required.
//
// The records passed into _afterFind will *always* have the alias as an
// index, and may have other indices as well for related models, or related
// models may be "under" the main record, depending on the query. Trying
// to generically handle all of those situations is just too much!
//
function afterFind ($results) {
if (method_exists ($this, '_afterFind') && !empty ($results)) {
// The data can come in many forms
if (Set::numeric(array_keys($results))) {
foreach ($results as $key => $result) {
$results[$key] = $this->afterFind ($result);
}
} else if (array_key_exists($this->alias, $results)) {
if (empty ($results[$this->alias])) {
// Don't do anything with empty records
} else if (Set::numeric(array_keys($results[$this->alias]))) {
$results = $this->afterFind ($results[$this->alias]);
} else {
$results = $this->_afterFind ($results);
}
} else if (count($results) == 1 && array_key_exists ('count', $results)) {
// Don't do anything with records that are just pagination counts
} else {
$results = $this->_afterFind (array($this->alias => $results));
$results = $results[$this->alias];
}
}
return $results;
}
//
// Use model associations to determine whether a record can be deleted.
//
function dependencies($id, $ignore = array()) {
$dependencies = array();
foreach ($this->hasMany as $class => $association) {
if (!in_array($class, $ignore)) {
$conditions = array("$class.{$association['foreignKey']}" => $id);
if (!empty($association['conditions'])) {
$conditions += $association['conditions'];
}
$dependent = $this->$class->find('count', array(
'conditions' => $conditions,
'contain' => false,
));
if ($dependent > 0) {
$dependencies[] = __(Inflector::pluralize($class), true) . ': ' . $dependent;
}
}
}
foreach ($this->hasAndBelongsToMany as $class => $association) {
if (!in_array($class, $ignore)) {
$class = $association['with'];
$conditions = array("$class.{$association['foreignKey']}" => $id);
if (!empty($association['conditions'])) {
$conditions += $association['conditions'];
}
$dependent = $this->$class->find('count', array(
'conditions' => $conditions,
'contain' => false,
));
if ($dependent > 0) {
$dependencies[] = __(Inflector::pluralize($association['className']), true) . ': ' . $dependent;
}
}
}
if (!empty($dependencies)) {
return implode(', ', $dependencies);
}
return false;
}
/**
* Adjust the indices of the provided array, so that the
* array is indexed by a specified id instead of from zero.
*
* This version is for data like
* array(
* 0 => array(
* 'ParentModel' => array(...),
* 'Model' => array(
* 0 => array(fields),
* 1 => array(fields),
* 2 => array(fields),
* ),
* ),
* 1 => array(
* ...
* ),
* );
*/
static function _reindexInner(&$data, $model, $field) {
if (empty ($data)) {
return;
}
if (Set::numeric (array_keys ($data))) {
foreach (array_keys ($data) as $i) {
self::_reindexInner($data[$i], $model, $field);
}
return;
}
if (array_key_exists ($model, $data)) {
$new = array();
foreach (array_keys ($data[$model]) as $key) {
$id = $data[$model][$key][$field];
$new[$id] = $data[$model][$key];
}
$data[$model] = $new;
}
}
/**
* Adjust the indices of the provided array, so that the
* array is indexed by a specified id instead of from zero.
*
* This version is for data like
* array(
* 0 => array('Model' => array(fields)),
* 1 => array('Model' => array(fields)),
* 2 => array('Model' => array(fields)),
* );
*
* or
*
* array(
* 0 => array(array(fields)),
* 1 => array(array(fields)),
* 2 => array(array(fields)),
* );
*/
static function _reindexOuter(&$data, $model, $field) {
if (empty ($data)) {
return;
}
if (!Set::numeric (array_keys ($data))) {
return;
}
$new = array();
foreach (array_keys ($data) as $key) {
if (array_key_exists($model, $data[$key])) {
$id = $data[$key][$model][$field];
} else {
$id = $data[$key][$field];
}
$new[$id] = $data[$key];
}
$data = $new;
}
//
// Validation helpers
//
function mustmatch($check, $field1, $field2) {
$data = current($this->data);
return $data[$field1] === $data[$field2];
}
function comparepassword($password, $hash) {
$compare = $this->hashPassword($password);
return ($compare == $hash);
}
function matchpassword($check) {
$value = current(array_values($check));
return $this->comparepassword($value, $this->data[Configure::read('security.auth_model')]['password']);
}
function mustnotmatch($check, $field1, $field2) {
$data = current($this->data);
if (!array_key_exists ($field1, $data) || !array_key_exists ($field2, $data)) {
return true;
}
return $data[$field1] !== $data[$field2];
}
function inconfig($check, $config) {
// $check array is passed using the form field name as the key
// have to extract the value to make the function generic
$value = current(array_values($check));
// Handle questionnaire values
if (is_array($value) && array_key_exists('question_id', $value) && array_key_exists('answer', $value)) {
$value = $value['answer'];
}
return array_key_exists($value, Configure::read($config));
}
function inconfig_ifenabled($check, $config) {
// If the record is not enabled, don't do any validation on it
if (empty($this->data[$this->alias]['enabled'])) {
return true;
}
// $check array is passed using the form field name as the key
// have to extract the value to make the function generic
$value = current(array_values($check));
return array_key_exists($value, Configure::read($config));
}
function inconfig_ifset($check, $config) {
// $check array is passed using the form field name as the key
// have to extract the value to make the function generic
$value = current(array_values($check));
return empty($value) || array_key_exists($value, Configure::read($config));
}
function indateconfig($check, $config) {
// $check array is passed using the form field name as the key
// have to extract the value to make the function generic
$value = current(array_values($check));
if (!is_array($value)) {
$year = date ('Y', strtotime ($value));
} else {
if (!array_key_exists ('year', $value)) {
return false;
}
$year = $value['year'];
}
$min = Configure::read("options.year.$config.min");
$max = Configure::read("options.year.$config.max");
if ($min === null || $max === null) {
return false;
}
return ($min <= $year && $year <= $max);
}
function indateconfig_ifenabled($check, $config) {
// If the record is not enabled, don't do any validation on it
if (empty($this->data[$this->alias]['enabled'])) {
return true;
}
// $check array is passed using the form field name as the key
// have to extract the value to make the function generic
$value = current(array_values($check));
if (!is_array($value)) {
$year = date ('Y', strtotime ($value));
} else {
if (!array_key_exists ('year', $value)) {
return false;
}
$year = $value['year'];
}
$min = Configure::read("options.year.$config.min");
$max = Configure::read("options.year.$config.max");
if ($min === null || $max === null) {
return false;
}
return ($min <= $year && $year <= $max);
}
function greaterdate($check, $field) {
// $check array is passed using the form field name as the key
// have to extract the value to make the function generic
$value = current(array_values($check));
if (array_key_exists($this->alias, $this->data)) {
$data = $this->data[$this->alias];
} else {
$data = current($this->data);
}
return ($value >= $data[$field]);
}
// Check a combined date and time, using standard separate date and time validators
function datetime($check) {
// $check array is passed using the form field name as the key
// have to extract the value to make the function generic
$value = current(array_values($check));
list ($date, $time) = explode (' ', $value, 2);
$Validation =& Validation::getInstance();
return ($Validation->date ($date) && $Validation->time ($time));
}
function positive($check) {
// $check array is passed using the form field name as the key
// have to extract the value to make the function generic
$value = current(array_values($check));
return (is_numeric($value) && $value >= 0);
}
function inquery($check, $model, $field, $conditions = array()) {
// $check array is passed using the form field name as the key
// have to extract the value to make the function generic
$value = current(array_values($check));
// This needs to work for questionnaire submissions
if (is_array($value)) {
if (array_key_exists('answer_id', $value)) {
$value = $value['answer_id'];
} else if (array_key_exists('answer', $value)) {
$value = $value['answer'];
}
}
// The validation array is always passed at the end of the arguments;
// if no conditions were passed, that will be in this array, so we
// need to get rid of that.
if (array_key_exists ('allowEmpty', $conditions)) {
$conditions = array();
}
$model_obj = ClassRegistry::init($model);
$values = $model_obj->find('list', array('fields' => $field, 'conditions' => $conditions));
return in_array ($value, $values);
}
function notinquery($check, $model, $field, $conditions = array()) {
return (!$this->inquery($check, $model, $field, $conditions));
}
/**
* Validate that a number is in specified range.
* if $lower and $upper are not set, will return true if
* $check is a legal finite on this platform.
* Copied from the main "range" validation function, but
* altered to be an inclusive range instead of exclusive.
*
* @param string $check Value to check
* @param integer $lower Lower limit
* @param integer $upper Upper limit
* @return boolean Success
* @access public
*/
function inclusive_range($check, $lower = null, $upper = null) {
// $check array is passed using the form field name as the key
// have to extract the value to make the function generic
$value = current(array_values($check));
if (!is_numeric($value)) {
return false;
}
if (isset($lower) && isset($upper)) {
return ($value >= $lower && $value <= $upper);
}
return is_finite($value);
}
/**
* Handle validation of a questionnaire response
*
* @param mixed $check The data to check for validity
* @param mixed $rule The rule to check with
* @return mixed true if the data is valid, false otherwise
*
*/
function response($check, $rule) {
// $check array is passed using the form field name as the key
// have to extract the value to make the function generic
$value = current(array_values($check));
if (array_key_exists('answer', $value)) {
$value = $value['answer'];
} else {
$value = $value['answer_id'];
}
$Validation =& Validation::getInstance();
if (method_exists($Validation, $rule)) {
return $Validation->dispatchMethod($rule, array($value));
} elseif (!is_array($rule)) {
return preg_match($rule, $value);
} elseif (Configure::read('debug') > 0) {
trigger_error(sprintf(__('Could not find validation handler %s for %s', true), $rule, 'response'), E_USER_WARNING);
}
return false;
}
function response_select($check, $opts, $required) {
// $check array is passed using the form field name as the key
// have to extract the value to make the function generic
$value = current(array_values($check));
$value = $value['answer_id'];
// A value from the provided list of options is okay
if (in_array ($value, $opts))
return true;
// If the question is not required, a blank value is okay
if ($value === '' && !$required)
return true;
// Nothing else is okay
return false;
}
/**
* Enforce unique team names within leagues instead of divisions,
* but not in a way that messes with playoff divisions.
*/
function team_unique($check, $team_id, $division_id) {
$value = current(array_values($check));
if ($division_id) {
// Find the list of divisions in the same league
$division_obj = ClassRegistry::init('Division');
$division_obj->contain();
$division = $division_obj->read(null, $division_id);
$division_obj->addPlayoffs($division);
$division_conditions = $division['Division']['sister_divisions'];
} else {
$division_conditions = $division_id;
}
$team_obj = ClassRegistry::init('Team');
$duplicate = $team_obj->find('count', array(
'conditions' => array(
'division_id' => $division_conditions,
'id !=' => $team_id,
'name' => $value,
),
'contain' => false,
));
return ($duplicate == 0);
}
function franchise_owner($check, $owner, $is_admin) {
if ($is_admin) {
return true;
}
// $check array is passed using the form field name as the key
// have to extract the value to make the function generic
$value = current(array_values($check));
$value = $value['answer_id'];
// -1 means make a new one with the same name as the team
if ($value != -1) {
$franchise_obj = ClassRegistry::init('Franchise');
$franchise_obj->contain('Person');
$franchise = $franchise_obj->read(null, $value);
$owners = Set::extract('/Person/id', $franchise);
return in_array($owner, $owners);
}
return true;
}
function franchise_unique($check) {
// $check array is passed using the form field name as the key
// have to extract the value to make the function generic
$value = current(array_values($check));
$value = $value['answer_id'];
// -1 means make a new one with the same name as the team
if ($value == -1) {
$name = EventTypeComponent::_extractAnswer($this->data, TEAM_NAME);
return $this->notinquery (array(array('answer' => $name)), 'Franchise', 'name');
}
return true;
}
function rule($check) {
// $check array is passed using the form field name as the key
// have to extract the value to make the function generic
$value = current(array_values($check));
$key = current(array_keys($check));
$rule_obj = AppController::_getComponent ('Rule');
if (!$rule_obj->init ($value)) {
return $rule_obj->parse_error;
}
return true;
}
function valid_score($check, $lower, $upper) {
// $check array is passed using the form field name as the key
// have to extract the value to make the function generic
$value = current(array_values($check));
$data = current($this->data);
if (in_array($data['status'], Configure::read('unplayed_status'))) {
return ($value === null);
}
return $this->inclusive_range($check, $lower, $upper);
}
function valid_play($check) {
// $check array is passed using the form field name as the key
// have to extract the value to make the function generic
$value = current(array_values($check));
$options = array_merge(make_options(array_merge(array_keys(Configure::read('sport.score_options')), array('Start', 'Timeout'))), Configure::read('sport.other_options'));
return array_key_exists($value, $options);
}
}
?>