|
[ Index ]
|
Source Code Reference for V1.00
|
[Summary view]
[Print]
[Text view]
1 <?php
2
3
4
5
6
7
8
9 if (!defined('W2P_BASE_DIR')) {
10 die('You should not access this file directly.');
11 }
12
13 require_once $AppUI->getSystemClass('query');
14
15
16
17
18
19
20
21
22 class CW2pObject {
23
24
25
26 var $_tbl_prefix = '';
27
28
29
30 var $_tbl = '';
31
32
33
34 var $_tbl_key = '';
35
36
37
38 var $_error = '';
39
40
41
42
43 var $_query;
44
45
46
47
48
49
50
51
52 function CW2pObject($table, $key) {
53 $this->_tbl = $table;
54 $this->_tbl_key = $key;
55 $this->_tbl_prefix = w2PgetConfig('dbprefix', '');
56 $this->_query = &new DBQuery;
57 }
58
59
60
61 function getError() {
62 return $this->_error;
63 }
64
65
66
67
68
69
70
71 function bind($hash) {
72 if (!is_array($hash)) {
73 $this->_error = get_class($this) . '::bind failed.';
74 return false;
75 } else {
76
77
78
79
80
81
82 foreach ($hash as $k => $v) {
83 if (!(is_object($hash[$k]))) {
84 $filtered_hash[$k] = $v;
85 }
86 }
87 $this->_query->bindHashToObject($filtered_hash, $this);
88 $this->_query->clear();
89 return true;
90 }
91 }
92
93
94
95
96
97
98 function load($oid = null, $strip = true) {
99 $k = $this->_tbl_key;
100 if ($oid) {
101 $this->$k = intval($oid);
102 }
103 $oid = $this->$k;
104 if ($oid === null) {
105 return false;
106 }
107 $this->_query->clear();
108 $this->_query->addTable($this->_tbl);
109 $this->_query->addWhere($this->_tbl_key . ' = ' . $oid);
110 $hash = $this->_query->loadHash();
111
112 if (!$hash) {
113 return false;
114 }
115 $this->_query->bindHashToObject($hash, $this, null, $strip);
116 $this->_query->clear();
117 return $this;
118 }
119
120
121
122
123
124 function loadAll($order = null, $where = null) {
125 $this->_query->clear();
126 $this->_query->addTable($this->_tbl);
127 if ($order) {
128 $this->_query->addOrder($order);
129 }
130 if ($where) {
131 $this->_query->addWhere($where);
132 }
133 $result = $this->_query->loadHashList($this->_tbl_key);
134 $this->_query->clear();
135 return $result;
136 }
137
138
139
140
141
142
143 function &getQuery($alias = null) {
144 $this->_query->clear();
145 $this->_query->addTable($this->_tbl, $alias);
146 return $this->_query;
147 }
148
149
150
151
152
153
154
155 function check() {
156 return null;
157 }
158
159
160
161
162
163
164
165 function duplicate() {
166 $_key = $this->_tbl_key;
167
168
169
170 if (version_compare(phpversion(), '5') >= 0) {
171 $newObj = clone($this);
172 } else {
173 $newObj = $this;
174 }
175
176 $newObj->$_key = '';
177
178 return $newObj;
179 }
180
181
182
183
184
185
186
187
188 function w2PTrimAll() {
189 $trim_arr = get_object_vars($this);
190 foreach ($trim_arr as $trim_key => $trim_val) {
191 if (!(strcasecmp(gettype($trim_val), 'string'))) {
192 $this->{$trim_key} = trim($trim_val);
193 }
194 }
195 }
196
197
198
199
200
201
202
203 function store($updateNulls = false) {
204 global $AppUI;
205
206 $this->w2PTrimAll();
207
208 $msg = $this->check();
209 if ($msg) {
210 return get_class($this) . '::store-check failed ' . $msg;
211 }
212 $k = $this->_tbl_key;
213 if ($this->$k) {
214 $store_type = 'update';
215 $q = new DBQuery;
216 $ret = $q->updateObject($this->_tbl, $this, $this->_tbl_key, $updateNulls);
217 $q->clear();
218 } else {
219 $store_type = 'add';
220 $q = new DBQuery;
221 $ret = $q->insertObject($this->_tbl, $this, $this->_tbl_key);
222 $q->clear();
223 }
224
225 if ($ret) {
226
227 addHistory($this->_tbl, $this->$k, $store_type, $AppUI->_('ACTION') . ': ' . $store_type . ' ' . $AppUI->_('TABLE') . ': ' . $this->_tbl . ' ' . $AppUI->_('ID') . ': ' . $this->$k);
228 }
229 return ((!$ret) ? (get_class($this) . '::store failed ' . db_error()) : null);
230 }
231
232
233
234
235
236
237
238
239
240
241 function canDelete(&$msg, $oid = null, $joins = null) {
242 global $AppUI;
243
244
245 $acl = &$AppUI->acl();
246 if (!$acl->checkModuleItem($this->_tbl, 'delete', $oid)) {
247 $msg = $AppUI->_('noDeletePermission');
248 return false;
249 }
250
251 $k = $this->_tbl_key;
252 if ($oid) {
253 $this->$k = intval($oid);
254 }
255 if (is_array($joins)) {
256 $select = $k;
257 $join = '';
258
259 $q = new DBQuery;
260 $q->addTable($this->_tbl);
261 $q->addWhere($k . ' = \'' . $this->$k . '\'');
262 $q->addGroup($k);
263 foreach ($joins as $table) {
264 $q->addQuery('COUNT(DISTINCT ' . $table['idfield'] . ') AS ' . $table['idfield']);
265 $q->addJoin($table['name'], $table['name'], $table['joinfield'] . ' = ' . $k);
266 }
267 $obj = null;
268 $q->loadObject($obj);
269 $q->clear();
270
271 if (!$obj) {
272 $msg = db_error();
273 return false;
274 }
275 $msg = array();
276 foreach ($joins as $table) {
277 $k = $table['idfield'];
278 if ($obj->$k) {
279 $msg[] = $AppUI->_($table['label']);
280 }
281 }
282
283 if (count($msg)) {
284 $msg = $AppUI->_('noDeleteRecord') . ': ' . implode(', ', $msg);
285 return false;
286 } else {
287 return true;
288 }
289 }
290
291 return true;
292 }
293
294
295
296
297
298
299
300 function delete($oid = null) {
301 $k = $this->_tbl_key;
302 if ($oid) {
303 $this->$k = intval($oid);
304 }
305 if (!$this->canDelete($msg)) {
306 return $msg;
307 }
308
309 $q = new DBQuery;
310 $q->setDelete($this->_tbl);
311 $q->addWhere($this->_tbl_key . ' = \'' . $this->$k . '\'');
312 $result = ((!$q->exec()) ? db_error() : null);
313 if (!$result) {
314
315 addHistory($this->_tbl, $this->$k, 'delete');
316 }
317 $q->clear();
318 return $result;
319 }
320
321
322
323
324
325
326 function getDeniedRecords($uid) {
327 $uid = intval($uid);
328 $uid || exit('FATAL ERROR ' . get_class($this) . '::getDeniedRecords failed, user id = 0');
329
330 $perms = &$GLOBALS['AppUI']->acl();
331 return $perms->getDeniedItems($this->_tbl, $uid);
332 }
333
334
335
336
337
338
339
340
341
342
343
344 function getAllowedRecords($uid, $fields = '*', $orderby = '', $index = null, $extra = null, $table_alias = '') {
345 $perms = &$GLOBALS['AppUI']->acl();
346 $uid = intval($uid);
347 $uid || exit('FATAL ERROR ' . get_class($this) . '::getAllowedRecords failed');
348 $deny = &$perms->getDeniedItems($this->_tbl, $uid);
349 $allow = &$perms->getAllowedItems($this->_tbl, $uid);
350
351
352
353
354
355
356
357
358
359
360 $this->_query->clear();
361 $this->_query->addQuery($fields);
362 $this->_query->addTable($this->_tbl);
363
364 if ($extra['from']) {
365 $this->_query->addTable($extra['from']);
366 }
367
368 if ($extra['join'] && $extra['on']) {
369 $this->_query->addJoin($extra['join'], $extra['join'], $extra['on']);
370 }
371
372 if (count($allow)) {
373 if ((array_search('0', $allow)) === false) {
374
375 $this->_query->addWhere(($table_alias ? $table_alias . '.' : '') . $this->_tbl_key . ' IN (' . implode(',', $allow) . ')');
376 } else {
377
378 }
379
380 if (count($deny)) {
381 if ((array_search('0', $deny)) === false) {
382
383 $this->_query->addWhere(($table_alias ? $table_alias . '.' : '') . $this->_tbl_key . ' NOT IN (' . implode(',', $deny) . ')');
384 } elseif ((array_search('0', $allow)) === false) {
385
386 } else {
387
388 $this->_query->addWhere('0=1');
389 }
390 }
391 } else {
392
393 $this->_query->addWhere('0=1');
394 }
395
396 if (isset($extra['where'])) {
397 $this->_query->addWhere($extra['where']);
398 }
399
400 if ($orderby) {
401 $this->_query->addOrder($orderby);
402 }
403
404 return $this->_query->loadHashList($index);
405 }
406
407 function getAllowedSQL($uid, $index = null) {
408 $perms = &$GLOBALS['AppUI']->acl();
409 $uid = intval($uid);
410 $uid || exit('FATAL ERROR ' . get_class($this) . '::getAllowedSQL failed');
411 $deny = &$perms->getDeniedItems($this->_tbl, $uid);
412 $allow = &$perms->getAllowedItems($this->_tbl, $uid);
413
414
415
416
417
418
419
420
421 // No access, and no allow overrides, so nothing to show.
422
423
424
425
426 if (!isset($index)) {
427 $index = $this->_tbl_key;
428 }
429 $where = array();
430 if (count($allow)) {
431 if ((array_search('0', $allow)) === false) {
432
433 $where[] = $index . ' IN (' . implode(',', $allow) . ')';
434 } else {
435
436 }
437
438 if (count($deny)) {
439 if ((array_search('0', $deny)) === false) {
440
441 $where[] = $index . ' NOT IN (' . implode(',', $deny) . ')';
442 } elseif ((array_search('0', $allow)) === false) {
443
444 } else {
445
446 $where[] = '0=1';
447 }
448 }
449 } else {
450
451 $where[] = '0=1';
452 }
453 return $where;
454 }
455
456 function setAllowedSQL($uid, &$query, $index = null, $key = null) {
457 $perms = &$GLOBALS['AppUI']->acl();
458 $uid = intval($uid);
459 $uid || exit('FATAL ERROR ' . get_class($this) . '::getAllowedSQL failed');
460 $deny = &$perms->getDeniedItems($this->_tbl, $uid);
461 $allow = &$perms->getAllowedItems($this->_tbl, $uid);
462
463 if (isset($index)) {
464 if (!