forked from ibuildingsnl/ATK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.atknodevalidator.inc
274 lines (244 loc) · 8.2 KB
/
class.atknodevalidator.inc
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
/**
* This file is part of the Achievo ATK distribution.
* Detailed copyright and licensing information can be found
* in the doc/COPYRIGHT and doc/LICENSE files which should be
* included in the distribution.
*
* @package atk
*
* @copyright (c)2000-2004 Ibuildings.nl BV
* @license http://www.achievo.org/atk/licensing ATK Open Source License
*
* @version $Revision$
* $Id$
*/
/**
* Validator for records, based on node definition.
*
* The class takes a node, and based on the attribute definitions,
* validation can be performed on records.
*
* @author Kees van Dieren <kees@ibuildings.nl>
* @package atk
*
*/
class atkNodeValidator
{
/**
* @var atkNode The node which needs to get validated
* @access private
*/
var $m_nodeObj = null;
/**
* @var array the record of the node which will get validated
* @access private
*/
var $m_record = array();
/**
* @var String the mode in which the validate will get runned
* @access private
*/
var $m_mode = "";
/**
* constructor
*
*/
function atkNodeValidator()
{
}
/**
* set the list of fields which will get ignored
* @param array $fieldArr List of fields to ignore during validation
*/
function setIgnoreList($fieldArr)
{
$this->m_ignoreList = $fieldArr;
}
/**
* set the mode in which to validate
* @param String $mode The mode ("edit"/"add")
*/
function setMode($mode)
{
$this->m_mode = $mode;
}
/**
* Set the Node which should be validated
*
* @param atkNode $nodeObj The node for validation
*/
function setNode(&$nodeObj)
{
$this->m_nodeObj = &$nodeObj;
}
/**
* set the record which should get validated.
* @param array $record The record to validate. The record is passed by
* reference, because errors that are found are
* stored in the record.
*/
function setRecord(&$record)
{
$this->m_record = &$record;
}
/**
* Validate a record
*
* @param string $mode Override the mode
* @param array $ignoreList Override the ignoreList
*/
function validate($mode="", $ignoreList=array())
{
// check overrides
if(count($ignoreList))
$this->setIgnoreList($ignoreList);
if($mode != "")
$this->setMode($mode);
atkdebug("validate() with mode ".$this->m_mode." for node ".$this->m_nodeObj->atkNodeType());
// set the record
$record = &$this->m_record;
// Check flags and values
$db = &$this->m_nodeObj->getDb();
foreach ($this->m_nodeObj->m_attribIndexList as $attribdata)
{
$attribname = $attribdata['name'];
if (!atk_in_array($attribname, $this->m_ignoreList))
{
$p_attrib = &$this->m_nodeObj->m_attribList[$attribname];
$this->validateAttributeValue($p_attrib, $record);
if ($p_attrib->hasFlag(AF_PRIMARY) && !$p_attrib->hasFlag(AF_AUTO_INCREMENT))
{
$atkorgkey = $record["atkprimkey"];
if(($atkorgkey == '' // no orgkey, so adding this record
|| $atkorgkey != $this->m_nodeObj->primaryKey($record)) // key has changed, so check is necessary
&& $this->m_nodeObj->countDb($this->m_nodeObj->primaryKey($record))>0
)
{
atkTriggerError($record, $p_attrib, 'error_primarykey_exists');
}
}
// if no root elements may be added to the tree, then every record needs to have a parent!
if ($p_attrib->hasFlag(AF_PARENT) && $this->m_nodeObj->hasFlag(NF_TREE_NO_ROOT_ADD) && $this->m_nodeObj->m_action == "save")
$p_attrib->m_flags |= AF_OBLIGATORY;
// validate obligatory fields (but not the auto_increment ones, because they don't have a value yet)
if ($p_attrib->hasFlag(AF_OBLIGATORY) && !$p_attrib->hasFlag(AF_AUTO_INCREMENT) && $p_attrib->isEmpty($record))
{
atkTriggerError($record, $p_attrib, 'error_obligatoryfield');
}
// if flag is primary
else if ($p_attrib->hasFlag(AF_UNIQUE) && !$p_attrib->hasFlag(AF_PRIMARY) && !$p_attrib->isEmpty($record)
&& $this->m_nodeObj->countDb($this->m_nodeObj->getTable().".{$attribname}='".$db->escapeSQL($p_attrib->value2db($record))."'".($this->m_mode != 'add' ? " AND NOT (".$this->m_nodeObj->primaryKey($record).")" : ""))>0
)
{
atkTriggerError($record, $p_attrib, 'error_uniquefield');
}
}
}
if(isset($record['atkerror'])&&count($record['atkerror']) > 0)
{
for ($i = 0, $_i = count($record["atkerror"]); $i < $_i; $i++)
$record["atkerror"][$i]["node"] = $this->m_nodeObj->m_type;
}
$this->validateUniqueFieldSets($record);
if (isset($record['atkerror']))
{
for ($i = 0, $_i = count($record["atkerror"]); $i < $_i; $i++)
$record["atkerror"][$i]["node"] = $this->m_nodeObj->m_type;
return false;
}
return true;
}
/**
* Validate attribute value.
*
* @param atkAttribute $p_attrib pointer to the attribute
* @param array $record record
*/
function validateAttributeValue(&$p_attrib, &$record)
{
if (!$p_attrib->isEmpty($record))
{
$funcname = $p_attrib->m_name."_validate";
if (method_exists($this->m_nodeObj, $funcname))
{
$this->m_nodeObj->$funcname($record, $this->m_mode);
}
else
{
$p_attrib->validate($record, $this->m_mode);
}
}
}
/**
* @deprecated
*
* @param array $record
*/
function validateAttributes(&$record)
{
foreach (array_keys($this->m_nodeObj->m_attribList) as $attribname)
{
if (!atk_in_array($attribname, $this->m_ignoreList))
{
$p_attrib = &$this->m_nodeObj->m_attribList[$attribname];
$this->validateAttributeValue($p_attrib, $record);
}
}
}
/**
* Check unique field combinations.
* The function is called by the validate() method automatically. It is
* not necessary to call this manually in a validation process.
* Errors that are found are stored in the $record parameter
*
* @param array $record The record to validate
*/
function validateUniqueFieldSets(&$record)
{
$db = &$this->m_nodeObj->getDb();
atkimport('atk.db.atkquery');
foreach($this->m_nodeObj->m_uniqueFieldSets as $uniqueFieldSet)
{
$query = &$db->createQuery();
$query->addField('*');
$query->addTable($this->m_nodeObj->m_table);
$attribs = array();
foreach($uniqueFieldSet as $field)
{
$attrib = &$this->m_nodeObj->m_attribList[$field];
if ($attrib)
{
$attribs[] = &$attrib;
if (is_a($attrib, 'atkmanytoonerelation') && count($attrib->m_refKey) > 1)
{
$attrib->createDestination();
foreach ($attrib->m_refKey as $refkey)
{
$query->addCondition($query->quoteField($refkey)." = '".$db->escapeSQL($record[$attrib->fieldName()][$refkey])."'");
}
}
else if (!$attrib->isNotNullInDb() && $attrib->isEmpty($record))
{
$query->addCondition($query->quoteField($field)." IS NULL");
}
else
{
$query->addCondition($query->quoteField($field)." = '".$attrib->value2db($record)."'");
}
}
else
atkerror("Field $field is mentioned in uniquefieldset but does not exist in ".$this->m_nodeObj->atknodetype());
}
if ($this->m_mode != 'add')
{
$query->addCondition("NOT (".$this->m_nodeObj->primaryKey($record).")");
}
if (count($db->getRows($query->buildSelect()))> 0)
{
atkTriggerError($record, $attribs, 'error_uniquefieldset');
}
}
}
}
?>