forked from mariano/email
-
Notifications
You must be signed in to change notification settings - Fork 0
/
email_app_model.php
77 lines (70 loc) · 2.15 KB
/
email_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
<?php
class EmailAppModel extends AppModel {
/**
* Behaviors
*
* @var array
*/
public $actsAs = array('Containable');
/**
* Validates that at least one of the given fields is not empty
*
* @param array $value Array in the form of $field => $value
* @param array $fields List of fields where to check
* @return bool Success
*/
public function validateOneNotEmpty($value, $fields = array()) {
reset($value);
$field = key($value);
$value = array_shift($value);
if (empty($fields)) {
$fields = array($field);
}
$valid = false;
foreach((array) $fields as $field) {
if (!empty($this->data[$this->alias][$field]) && Validation::notEmpty($this->data[$this->alias][$field])) {
$valid = true;
break;
}
}
return $valid;
}
/**
* Checks if the value defined is unique for the given data model.
* The check for uniqueness is case-insensitive. If
* {@link $params}['conditions'] is given, this is used as a constraint.
* If {@link $params}['scope'] is given, the value is only checked against
* records that match the value of the column/field defined by
* {@link $params}['scope'].
*
* @param array $value Array in the form of $field => $value.
* @return bool True if value is unique; false otherwise.
* @access public
*/
public function validateUnique($value, $params) {
$value = array_shift($value);
$column = $this->alias . '.' . $params['field'];
$id = $this->alias . '.' . $this->primaryKey;
$conditions = array();
if (isset($params['conditions'])) {
$conditions = $params['conditions'];
}
if (isset($params['scope'])) {
if (is_array($params['scope'])) {
foreach ($params['scope'] as $scope) {
$conditions[$scope] = $this->data[$this->alias][$scope];
}
} else if (is_string($params['scope'])) {
$conditions[$params['scope']] = $this->data[$this->alias][$params['scope']];
}
}
$conditions[$column] = $value;
if (isset($this->data[$this->alias][$this->primaryKey])) {
$conditions[$id . ' !='] = $this->data[$this->alias][$this->primaryKey];
} else if (!empty($this->id)) {
$conditions[$id . ' !='] = $this->id;
}
return !$this->hasAny($conditions);
}
}
?>