/
app_model.php
188 lines (174 loc) · 6.01 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
<?php
class AppModel extends Model {
var $recursive = -1; // Default setting for all Model::find() calls, better use containable
// TODO: Is this used anyway?
// This is bad design
function purge($id) {
if (!isset($this->skipOnPurge)) {
$this->skipOnPurge = array('id', 'created', 'modified');
}
if ($id != null) {
$purgeData = array_diff(array_keys($this->_schema), $this->skipOnPurge);
$purgeData = Set::normalize($purgeData);
$purgeData[$this->alias] = array_fill_keys(array_keys($purgeData), null);
$purgeData[$this->alias]['is_deleted'] = '1';
$purgeData[$this->alias][$this->primaryKey] = $this->id;
if ($this->save($purgeData, null, false) !== false) {
return true;
}
}
return false;
}
/**
* TODO: should be moved into a behavior named DynamicDisplayFieldable
* Usage: var $displayField = array("%s %s", "{n}.User.name", "{n}.User.secondname");
* Based on: http://bakery.cakephp.org/articles/view/multiple-display-field-3#3875
* by Arialdo Martini
*/
function find($conditions = null, $fields = array(), $order = null, $recursive = null) {
$translationFile = 'additions'; // TODO: Should default to cakephp's default
if ($conditions == 'list' && is_array($this->displayField)) {
$data = $this->find('all', $fields, $order, $recursive);
$list = Set::combine($data, '{n}.' . $this->name . '.' . $this->primaryKey,
$this->displayField);
if (isset($this->displayFieldDoTranslate) and $this->displayFieldDoTranslate === true) {
foreach ($list as &$element) {
$element = __d($translationFile, $element, true); // Translation
}
unset($element); // Fixes PHP 5.2.x issue with foreach on & references
// see: http://php.net/manual/en/control-structures.foreach.php#92116
}
return $list;
} else if ($conditions == 'pagination') {
// TODO ...
} else {
return parent::find($conditions, $fields, $order, $recursive);
}
}
/**
* TODO all validations should be moved into a behavior like "extendedValidationableBehavior"
* including pause and unpause validation
*/
function validateEqualData($data, $comparisonField, $message = null) {
if (is_array($data)) {
foreach ($data as $value) {
if (isset($this->data[$this->alias][$comparisonField])) {
if ($value !== $this->data[$this->alias][$comparisonField]) {
$this->invalidate($comparisonField, $message);
return false;
}
}
}
}
return true;
}
function validateDatetime($data = null, $fieldname) {
if (is_array($data)) {
$re = "/^[12][0-9]{3}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01]) ([01][0-9]|[2][0-4]):"
. "([0-5][0-9]|[0-9]):([0-5][0-9]|[0-9])$/";
if (preg_match($re, $data[$fieldname])) {
return true;
}
}
return false;
}
function validateAuthKey($data = null, $fieldname) {
if (is_array($data)) {
// Matches AB42-KD24-D2JS-24M6 and alike
if (preg_match("/^[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{4}$/",
$data[$fieldname])) {
return true;
}
}
return false;
}
function validateUuid($data = null, $fieldname) {
if (is_array($data)) {
$re = "/[A-Fa-f0-9]{8}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{12}/";
if (preg_match($re, $data[$fieldname])) {
return true;
}
}
return false;
}
function pauseValidation($fieldname, $rulename, $switch = true) {
if ($switch) {
$this->pausedValidate[$fieldname][$rulename] = $this->validate[$fieldname][$rulename];
unset($this->validate[$fieldname][$rulename]);
} else {
$this->validate[$fieldname][$rulename] = $this->pausedValidate[$fieldname][$rulename];
unset($this->pausedValidate[$fieldname]);
}
}
function unpauseValidation($fieldname, $rulename) {
$this->pauseValidation($fieldname, $rulename, false);
}
// TODO: Should be moved into a MailableBehavior
// Wrapper around EMailComponent (and possibly SMSGatewayComponent in future)
function _sendMessage($viewData, $template, $gateway) {
if (isset($gateway['email'])) {
App::import('Core', 'Controller');
App::import('Component', 'Email');
// We need this fake controller
$Controller = new Controller();
$Email = new EmailComponent(null);
$Email->initialize($Controller);
$Email->to = $gateway['email']['to'];
$Email->subject = $gateway['email']['subject'];
$Email->template = $template;
$EMail->sendAs = 'both';
$domainName = env('SERVER_NAME');
if (strpos($domainName, 'www.') === 0) {
$domainName = substr($domainName, 4);
}
$Email->from = 'noreply@' . $domainName;
$Controller->set($viewData);
$isSend = $Email->send();
if ($isSend == false) {
$this->log('Sending mail not successful.', 'error');
$this->invalidate('could_not_send');
}
unset($Email);
unset($Controller);
return $isSend;
}
// ENH: SMS-Gateway
}
// TODO: move this into extendedDebuggableBehavior
function validates($options = array()) {
// Debug validation
$errors = $this->invalidFields($options);
if (is_array($errors) && !empty($errors)) {
$this->log($errors, 'debug');
}
// Validate
return parent::validates($options);
}
// TODO: Move to extendedOperationableBehavior
// Saves a field, if the object exists
function saveFieldIfExists($id = null, $fieldname, $value, $validate = false) {
if ($this->find('count', array('conditions' => array($this->primaryKey => $id))) < 1) {
return false;
}
$backupThisId = $this->id;
$this->id = $id;
$return = $this->saveField($fieldname, $value , $validate);
$this->id = $backupThisId;
return $return;
}
// TODO: Move to extendedOperationable
// Gets a single field value, if the object exists
// Todo add cache and cache clean, on save, saveFieldIfExists and saveField
// if the row (or in case of saveField the row+field) is touched, unvalidate cache
function getFieldIfExists($id = null, $fieldname) { // Switch params, $id = $this->id by default
$data = $this->find('first', array(
'conditions' => array($this->primaryKey => $id),
'fields' => array($fieldname)));
if ($data == false) {
return false;
} else {
return $data[$this->alias][$fieldname];
}
}
}
?>