-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.php
More file actions
616 lines (558 loc) · 11.6 KB
/
Model.php
File metadata and controls
616 lines (558 loc) · 11.6 KB
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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
<?php
/**
* @package framework
* @copyright Copyright (c) 2005-2020 The Regents of the University of California.
* @license http://opensource.org/licenses/MIT MIT
*/
namespace Qubeshub\Base;
/**
* Abstract model class
*/
abstract class Model extends Obj
{
/**
* Unpublished state
*
* @var integer
*/
const APP_STATE_UNPUBLISHED = 0;
/**
* Published state
*
* @var integer
*/
const APP_STATE_PUBLISHED = 1;
/**
* Deleted state
*
* @var integer
*/
const APP_STATE_DELETED = 2;
/**
* Flagged state
*
* @var integer
*/
const APP_STATE_FLAGGED = 3;
/**
* Table class name
*
* @var string
*/
protected $_tbl_name = null;
/**
* Table
*
* @var object
*/
protected $_tbl = null;
/**
* Database
*
* @var object
*/
protected $_db = null;
/**
* Model context.
* option.model(.content)
*
* @var string
*/
protected $_context = null;
/**
* Constructor
*
* @param mixed $oid Integer (ID), string (alias), object or array
* @return void
*/
public function __construct($oid=null)
{
$this->_db = $this->initDbo();
if ($this->_tbl_name)
{
$cls = $this->_tbl_name;
$this->_tbl = new $cls($this->_db);
if (!($this->_tbl instanceof \Hubzero\Database\Table))
{
$this->_logError(
__CLASS__ . '::' . __FUNCTION__ . '(); ' . \Lang::txt('Table class must be an instance of Table.')
);
throw new \LogicException(\Lang::txt('Table class must be an instance of Table.'));
}
if (is_numeric($oid) || is_string($oid))
{
// Make sure $oid isn't empty
// This saves a database call
if ($oid)
{
$this->_tbl->load($oid);
}
}
else if (is_object($oid) || is_array($oid))
{
$this->bind($oid);
}
}
}
/**
* Returns a property of the object or the default value if the property is not set.
*
* @param string $property The name of the property
* @param mixed $default The default value
* @return mixed The value of the property
*/
public function get($property, $default=null)
{
if (isset($this->_tbl->$property))
{
return $this->_tbl->$property;
}
else if (isset($this->_tbl->{'__' . $property}))
{
return $this->_tbl->{'__' . $property};
}
return $default;
}
/**
* Modifies a property of the object, creating it if it does not already exist.
*
* @param string $property The name of the property
* @param mixed $value The value of the property to set
* @return object This current model
*/
public function set($property, $value = null)
{
if (!array_key_exists($property, $this->_tbl->getProperties()))
{
$property = '__' . $property;
}
$this->_tbl->$property = $value;
return $this;
}
/**
* Method to get the database connection.
*
* If detected that the code is being run in a super group
* component, it will return the super group DB connection
* instead of the site connection.
*
* @return object Database
*/
public function initDbo()
{
if (defined('JPATH_GROUPCOMPONENT'))
{
$r = new \ReflectionClass($this);
if (substr($r->getFileName(), 0, strlen(JPATH_GROUPCOMPONENT)) == JPATH_GROUPCOMPONENT)
{
return \Hubzero\User\Group\Helper::getDbo();
}
}
return \App::get('db');
}
/**
* Method to get the Database connector object.
*
* @return object The internal database connector object.
*/
public function getDbo()
{
return $this->_db;
}
/**
* Method to set the database connector object.
*
* @param object &$db A database connector object to be used by the table object.
* @return boolean True on success.
*/
public function setDbo(&$db)
{
if (!($db instanceof \Hubzero\Database\Driver))
{
return false;
}
$this->_db = $db;
$this->_tbl->setDBO($this->_db);
return true;
}
/**
* Check if the entry exists (i.e., has a database record)
*
* @return boolean True if record exists, False if not
*/
public function exists()
{
if (!array_key_exists('id', $this->_tbl->getFields()))
{
return true;
}
if ($this->get('id') && (int) $this->get('id') > 0)
{
return true;
}
return false;
}
/**
* Has the offering started?
*
* @return boolean
*/
public function isPublished()
{
if (!array_key_exists('state', $this->_tbl->getFields()))
{
return true;
}
if ($this->get('state') == self::APP_STATE_PUBLISHED)
{
return true;
}
return false;
}
/**
* Has the offering started?
*
* @return boolean
*/
public function isUnpublished()
{
if (!array_key_exists('state', $this->_tbl->getFields()))
{
return false;
}
if ($this->get('state') == self::APP_STATE_UNPUBLISHED)
{
return true;
}
return false;
}
/**
* Has the offering started?
*
* @return boolean
*/
public function isDeleted()
{
if (!array_key_exists('state', $this->_tbl->getFields()))
{
return false;
}
if ($this->get('state') == self::APP_STATE_DELETED)
{
return true;
}
return false;
}
/**
* Bind data to the model
*
* @param mixed $data Object or array
* @return boolean True on success, False on error
*/
public function bind($data=null)
{
if (is_object($data))
{
$res = $this->_tbl->bind($data);
if ($res)
{
$properties = $this->_tbl->getProperties();
foreach (get_object_vars($data) as $key => $property)
{
if (!array_key_exists($key, $properties))
{
$this->_tbl->set('__' . $key, $property);
}
}
}
}
else if (is_array($data))
{
$res = $this->_tbl->bind($data);
if ($res)
{
$properties = $this->_tbl->getProperties();
foreach (array_keys($data) as $key)
{
if (!array_key_exists($key, $properties))
{
$this->_tbl->set('__' . $key, $data[$key]);
}
}
}
}
else
{
$this->_logError(
__CLASS__ . '::' . __FUNCTION__ . '(); ' . \Lang::txt('Data must be of type object or array. Type given was %s', gettype($data))
);
throw new \InvalidArgumentException(\Lang::txt('Data must be of type object or array. Type given was %s', gettype($data)));
}
return $res;
}
/**
* Log an error message
*
* @param string $message Message to log
* @return void
*/
protected function _logError($message)
{
return $this->_log('error', $message);
}
/**
* Log an error message
*
* @param string $message Message to log
* @return void
*/
protected function _logDebug($message)
{
return $this->_log('debug', $message);
}
/**
* Log an error message
*
* @param string $message Message type to log
* @param string $message Message to log
* @return void
*/
protected function _log($type, $message)
{
if (!$message)
{
return;
}
if (\App::get('config')->get('debug'))
{
$message = '[' . Request::getVar('REQUEST_URI', '', 'server') . '] [' . $message . ']';
}
$type = strtolower($type);
if (!in_array($type, array('error', 'debug', 'critical', 'warning', 'notice', 'alert', 'emergency', 'info')))
{
return;
}
$logger = \Log::getRoot();
$logger->$type($message);
}
/**
* Perform data validation
*
* @return boolean False if error, True on success
*/
public function check()
{
// Is data valid?
if (!$this->_tbl->check())
{
$this->_errors = $this->_tbl->getErrors();
return false;
}
return true;
}
/**
* Store changes to this database entry
*
* @param boolean $check Perform data validation check?
* @return boolean False if error, True on success
*/
public function store($check=true)
{
// Validate data?
if ($check)
{
// Is data valid?
if (!$this->check())
{
return false;
}
if ($this->_context)
{
$results = \Event::trigger('content.onContentBeforeSave', array(
$this->_context,
&$this,
$this->exists()
));
foreach ($results as $result)
{
if ($result === false)
{
$this->setError(\App::get('language')->txt('Content failed validation.'));
return false;
}
}
}
}
// Attempt to store data
if (!$this->_tbl->store())
{
$this->setError($this->_tbl->getError());
return false;
}
return true;
}
/**
* Delete a record
*
* @return boolean True on success, false on error
*/
public function delete()
{
// Can't delete what doesn't exist
if (!$this->exists())
{
return true;
}
// Remove record from the database
if (!$this->_tbl->delete())
{
$this->setError($this->_tbl->getError());
return false;
}
// Hey, no errors!
return true;
}
/**
* Import a set of plugins
*
* @return object
*/
public function importPlugin($type='')
{
\Plugin::import($type);
return $this;
}
/**
* Import a set of plugins
*
* @return object
*/
public function trigger($event='', $params=array())
{
return \Event::trigger($event, $params);
}
/**
* Turn the object into a string
*
* @return string
*/
public function __toString()
{
return $this->toString();
}
/**
* Turn the object into a string
*
* @return string
*/
public function toString($ignore=array('_db'))
{
return $this->_print_r($this, $ignore);
}
/**
* Special print_r to strip out any vars passed in $ignore
*
* @param object $subject Object to print_r
* @param array $ignore Property names to ignore
* @param integer $depth Recursion depth
* @param array $refChain Reference chain
* @return string
*/
private function _print_r($subject, $ignore = array(), $depth = 1, $refChain = array())
{
$str = '';
if ($depth > 20)
{
return $str;
}
if (is_object($subject))
{
foreach ($refChain as $refVal)
{
if ($refVal === $subject)
{
$str .= "*RECURSION*\n";
return $str;
}
}
array_push($refChain, $subject);
$str .= get_class($subject) . " Object ( \n";
$subject = (array) $subject;
foreach ($subject as $key => $val)
{
if (is_array($ignore) && !in_array($key, $ignore, 1))
{
if ($key[0] == "\0")
{
$keyParts = explode("\0", $key);
if (is_array($ignore) && in_array($keyParts[2], $ignore, 1))
{
continue;
}
$str .= str_repeat(" ", $depth * 4) . '[';
$str .= $keyParts[2] . (($keyParts[1] == '*') ? ':protected' : ':private');
}
else
{
$str .= str_repeat(" ", $depth * 4) . '[';
$str .= $key;
}
$str .= '] => ';
$str .= $this->_print_r($val, $ignore, $depth + 1, $refChain);
}
}
$str .= str_repeat(" ", ($depth - 1) * 4) . ")\n";
array_pop($refChain);
}
elseif (is_array($subject))
{
$str .= "Array ( \n";
foreach ($subject as $key => $val)
{
if (is_array($ignore) && !in_array($key, $ignore, 1))
{
$str .= str_repeat(" ", $depth * 4) . '[' . $key . '] => ';
$str .= $this->_print_r($val, $ignore, $depth + 1, $refChain);
}
}
$str .= str_repeat(" ", ($depth - 1) * 4) . ")\n";
}
else
{
$str .= $subject . "\n";
}
return $str;
}
/**
* Method to return model values in array format
*
* @param boolean $verbose Include prefixed "__" vars in output
* @return array Array of model values.
*/
public function toArray($verbose = false)
{
$output = $this->_tbl->getProperties();
if ($verbose)
{
foreach ($this->_tbl as $key => $value)
{
if (substr($key, 0, 2) == '__')
{
$output[substr($key, 2)] = $value;
}
}
}
return $output;
}
/**
* Dynamically handle error additions.
*
* @param string $method
* @param array $parameters
* @return mixed
*/
public function __call($method, $parameters)
{
throw new \BadMethodCallException(sprintf(__CLASS__ . '; Method [%s] does not exist.', $method));
}
}