Completed
Push — xmfsync ( ae7362...f1856c )
by Richard
06:23
created

XoopsObject::isNew()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
ccs 2
cts 2
cp 1
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * XOOPS Kernel Object
4
 *
5
 * You may not change or alter any portion of this comment or credits
6
 * of supporting developers from this source code or any supporting source code
7
 * which is considered copyrighted (c) material of the original comment or credit authors.
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 */
12
13
namespace Xoops\Core\Kernel;
14
15
use Xoops\Core\Kernel\Dtype;
16
17
/**
18
 * Establish Xoops object datatype legacy defines
19
 * New code should use Dtype::TYPE_* constants
20
 *
21
 * These will eventually be removed. See Xoops\Core\Kernel\Dtype for more.
22
 */
23
define('XOBJ_DTYPE_TXTBOX',  Dtype::TYPE_TEXT_BOX);
24
define('XOBJ_DTYPE_TXTAREA', Dtype::TYPE_TEXT_AREA);
25
define('XOBJ_DTYPE_INT',     Dtype::TYPE_INTEGER);
26
define('XOBJ_DTYPE_URL',     Dtype::TYPE_URL);
27
define('XOBJ_DTYPE_EMAIL',   Dtype::TYPE_EMAIL);
28
define('XOBJ_DTYPE_ARRAY',   Dtype::TYPE_ARRAY);
29
define('XOBJ_DTYPE_OTHER',   Dtype::TYPE_OTHER);
30
define('XOBJ_DTYPE_SOURCE',  Dtype::TYPE_SOURCE);
31
define('XOBJ_DTYPE_STIME',   Dtype::TYPE_SHORT_TIME);
32
define('XOBJ_DTYPE_MTIME',   Dtype::TYPE_MEDIUM_TIME);
33
define('XOBJ_DTYPE_LTIME',   Dtype::TYPE_LONG_TIME);
34
define('XOBJ_DTYPE_FLOAT',   Dtype::TYPE_FLOAT);
35
define('XOBJ_DTYPE_DECIMAL', Dtype::TYPE_DECIMAL);
36
define('XOBJ_DTYPE_ENUM',    Dtype::TYPE_ENUM);
37
38
/**
39
 * Base class for all objects in the Xoops kernel (and beyond)
40
 *
41
 * @category  Xoops\Core\Kernel\XoopsObject
42
 * @package   Xoops\Core\Kernel
43
 * @author    Kazumi Ono (AKA onokazu) <http://www.myweb.ne.jp/, http://jp.xoops.org/>
44
 * @author    Taiwen Jiang <[email protected]>
45
 * @copyright 2000-2015 XOOPS Project (http://xoops.org)
46
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
47
 * @link      http://xoops.org
48
 * @since     2.0.0
49
 */
50
abstract class XoopsObject implements \ArrayAccess
51
{
52
    /**
53
     * holds all variables(properties) of an object
54
     *
55
     * @var array
56
     */
57
    public $vars = array();
58
59
    /**
60
     * variables cleaned for store in DB
61
     *
62
     * @var array
63
     */
64
    public $cleanVars = array();
65
66
    /**
67
     * is it a newly created object?
68
     *
69
     * @var bool
70
     */
71
    private $isNew = false;
72
73
    /**
74
     * has any of the values been modified?
75
     *
76
     * @var bool
77
     */
78
    private $isDirty = false;
79
80
    /**
81
     * errors
82
     *
83
     * @var array
84
     */
85
    private $errors = array();
86
87
    /**
88
     * @var string
89
     */
90
    public $plugin_path;
91
92
    /**
93
     * used for new/clone objects
94
     *
95
     * @return void
96
     */
97 13
    public function setNew()
98
    {
99 13
        $this->isNew = true;
100 13
    }
101
102
    /**
103
     * clear new flag
104
     *
105
     * @return void
106
     */
107 8
    public function unsetNew()
108
    {
109 8
        $this->isNew = false;
110 8
    }
111
112
    /**
113
     * check new flag
114
     *
115
     * @return bool
116
     */
117 12
    public function isNew()
118
    {
119 12
        return $this->isNew;
120
    }
121
122
    /**
123
     * mark modified objects as dirty
124
     *
125
     * used for modified objects only
126
     *
127
     * @return void
128
     */
129 29
    public function setDirty()
130
    {
131 29
        $this->isDirty = true;
132 29
    }
133
134
    /**
135
     * cleaar dirty flag
136
     *
137
     * @return void
138
     */
139 14
    public function unsetDirty()
140
    {
141 14
        $this->isDirty = false;
142 14
    }
143
144
    /**
145
     * check dirty flag
146
     *
147
     * @return bool
148
     */
149 14
    public function isDirty()
150
    {
151 14
        return $this->isDirty;
152
    }
153
154
    /**
155
     * initialize variables for the object
156
     *
157
     * @param string $key       key
158
     * @param int    $data_type set to one of Dtype::TYPE_XXX constants (set to Dtype::TYPE_OTHER
159
     *                           if no data type checking nor text sanitizing is required)
160
     * @param mixed  $value     value
161
     * @param bool   $required  require html form input?
162
     * @param mixed  $maxlength for Dtype::TYPE_TEXT_BOX type only
163
     * @param string $options   does this data have any select options?
164
     *
165
     * @return void
166
     */
167 273
    public function initVar($key, $data_type, $value = null, $required = false, $maxlength = null, $options = '')
168
    {
169 273
        $this->vars[$key] = array(
170 273
            'value' => $value,
171 273
            'required' => $required,
172 273
            'data_type' => $data_type,
173 273
            'maxlength' => $maxlength,
174
            'changed' => false,
175 273
            'options' => $options
176
        );
177 273
    }
178
179
    /**
180
     * assign a value to a variable
181
     *
182
     * @param string $key   name of the variable to assign
183
     * @param mixed  $value value to assign
184
     *
185
     * @return void
186
     */
187 49
    public function assignVar($key, $value)
188
    {
189 49 View Code Duplication
        if (isset($key) && isset($this->vars[$key])) {
190 49
            $this->vars[$key]['value'] = $value;
191
        }
192 49
    }
193
194
    /**
195
     * assign values to multiple variables in a batch
196
     *
197
     * @param array $var_arr associative array of values to assign
198
     *
199
     * @return void
200
     */
201 40
    public function assignVars($var_arr)
202
    {
203 40
        if (is_array($var_arr)) {
204 40
            foreach ($var_arr as $key => $value) {
205 40
                $this->assignVar($key, $value);
206
            }
207
        }
208 40
    }
209
210
    /**
211
     * assign a value to a variable
212
     *
213
     * @param string $key     name of the variable to assign
214
     * @param mixed  $value   value to assign
215
     *
216
     * @return void
217
     */
218 27
    public function setVar($key, $value)
219
    {
220 27
        if (!empty($key) && isset($value) && isset($this->vars[$key])) {
221 27
            $this->vars[$key]['value'] = $value;
222 27
            $this->vars[$key]['changed'] = true;
223 27
            $this->setDirty();
224
        }
225 27
    }
226
227
    /**
228
     * assign values to multiple variables in a batch
229
     *
230
     * @param array $var_arr associative array of values to assign
231
     *
232
     * @return void
233
     */
234 1
    public function setVars($var_arr)
235
    {
236 1
        if (is_array($var_arr)) {
237 1
            foreach ($var_arr as $key => $value) {
238 1
                $this->setVar($key, $value);
239
            }
240
        }
241 1
    }
242
243
    /**
244
     * unset variable(s) for the object
245
     *
246
     * @param mixed $var variable(s)
247
     *
248
     * @return bool
249
     */
250 1
    public function destroyVars($var)
251
    {
252 1
        if (empty($var)) {
253 1
            return true;
254
        }
255
        $var = !is_array($var) ? array($var) : $var;
256
        foreach ($var as $key) {
257
            if (!isset($this->vars[$key])) {
258
                continue;
259
            }
260
            $this->vars[$key]['changed'] = null;
261
        }
262
        return true;
263
    }
264
265
    /**
266
     * Assign values to multiple variables in a batch
267
     *
268
     * Meant for a CGI context:
269
     * - prefixed CGI args are considered save
270
     * - avoids polluting of namespace with CGI args
271
     *
272
     * @param mixed  $var_arr associative array of values to assign
273
     * @param string $pref    prefix (only keys starting with the prefix will be set)
274
     *
275
     * @return void
276
     */
277 1
    public function setFormVars($var_arr = null, $pref = 'xo_')
278
    {
279 1
        $len = strlen($pref);
280 1
        if (is_array($var_arr)) {
281 1
            foreach ($var_arr as $key => $value) {
282 1
                if ($pref == substr($key, 0, $len)) {
283 1
                    $this->setVar(substr($key, $len), $value);
284
                }
285
            }
286
        }
287 1
    }
288
289
    /**
290
     * returns all variables for the object
291
     *
292
     * @return array associative array of key->value pairs
293
     */
294 26
    public function getVars()
295
    {
296 26
        return $this->vars;
297
    }
298
299
    /**
300
     * Returns the values of the specified variables
301
     *
302
     * @param mixed  $keys     An array containing the names of the keys to retrieve, or null to get all of them
303
     * @param string $format   Format to use (see getVar)
304
     * @param int    $maxDepth Maximum level of recursion to use if some vars are objects themselves
305
     *
306
     * @return array associative array of key->value pairs
307
     */
308 3
    public function getValues($keys = null, $format = Dtype::FORMAT_SHOW, $maxDepth = 1)
309
    {
310 3
        if (!isset($keys)) {
311 3
            $keys = array_keys($this->vars);
312
        }
313 3
        $vars = array();
314 3
        if (is_array($keys)) {
315 3
            foreach ($keys as $key) {
316 3
                if (isset($this->vars[$key])) {
317 3
                    if (is_object($this->vars[$key]) && is_a($this->vars[$key], 'Xoops\Core\Kernel\XoopsObject')) {
318
                        if ($maxDepth) {
319
                            /* @var $obj XoopsObject */
320
                            $obj = $this->vars[$key];
321
                            $vars[$key] = $obj->getValues(null, $format, $maxDepth - 1);
322
                        }
323
                    } else {
324 3
                        $vars[$key] = $this->getVar($key, $format);
325
                    }
326
                }
327
            }
328
        }
329 3
        return $vars;
330
    }
331
332
    /**
333
     * returns a specific variable for the object in a proper format
334
     *
335
     * @param string $key    key of the object's variable to be returned
336
     * @param string $format format to use for the output
337
     *
338
     * @return mixed formatted value of the variable
339
     */
340 181
    public function getVar($key, $format = Dtype::FORMAT_SHOW)
341
    {
342 181
        $ret = null;
343 181
        if (!isset($this->vars[$key])) {
344 1
            return $ret;
345
        }
346 181
        $ret = Dtype::getVar($this, $key, $format);
347 181
        return $ret;
348
    }
349
350
    /**
351
     * clean values of all variables of the object for storage.
352
     *
353
     * @return bool true if successful
354
     */
355 3
    public function cleanVars()
356
    {
357 3
        $existing_errors = $this->getErrors();
358 3
        $this->errors = array();
359 3
        foreach ($this->vars as $k => $v) {
360 3
            if (!$v['changed']) {
361
            } else {
362 3
                $this->cleanVars[$k] = Dtype::cleanVar($this, $k);
363
            }
364
        }
365 3
        if (count($this->errors) > 0) {
366
            $this->errors = array_merge($existing_errors, $this->errors);
367
            return false;
368
        }
369
        // $this->_errors = array_merge($existing_errors, $this->_errors);
370 3
        $this->unsetDirty();
371 3
        return true;
372
    }
373
374
    /**
375
     * create a clone(copy) of the current object
376
     *
377
     * @return object clone
378
     */
379 1
    public function xoopsClone()
380
    {
381 1
        $clone = clone $this;
382 1
        return $clone;
383
    }
384
385
    /**
386
     * Adjust a newly cloned object
387
     */
388 1
    public function __clone()
389
    {
390
        // need this to notify the handler class that this is a newly created object
391 1
        $this->setNew();
392 1
    }
393
394
    /**
395
     * add an error
396
     *
397
     * @param string $err_str to add
398
     *
399
     * @return void
400
     */
401 2
    public function setErrors($err_str)
402
    {
403 2
        if (is_array($err_str)) {
404 1
            $this->errors = array_merge($this->errors, $err_str);
405
        } else {
406 2
            $this->errors[] = trim($err_str);
407
        }
408 2
    }
409
410
    /**
411
     * return the errors for this object as an array
412
     *
413
     * @return array an array of errors
414
     */
415 14
    public function getErrors()
416
    {
417 14
        return $this->errors;
418
    }
419
420
    /**
421
     * return the errors for this object as html
422
     *
423
     * @return string html listing the errors
424
     * @todo remove hardcoded HTML strings
425
     */
426 1
    public function getHtmlErrors()
427
    {
428 1
        $ret = '<h4>Errors</h4>';
429 1
        if (!empty($this->errors)) {
430 1
            foreach ($this->errors as $error) {
431 1
                $ret .= $error . '<br />';
432
            }
433
        } else {
434
            $ret .= 'None<br />';
435
        }
436 1
        return $ret;
437
    }
438
439
    /**
440
     * toArray
441
     *
442
     * @deprecated
443
     * @return array
444
     */
445 1
    public function toArray()
446
    {
447 1
        return $this->getValues();
448
    }
449
450
    /**
451
     * ArrayAccess methods
452
     */
453
454
    /**
455
     * offsetExists
456
     *
457
     * @param mixed $offset array key
458
     *
459
     * @return bool true if offset exists
460
     */
461 2
    public function offsetExists($offset)
462
    {
463 2
        return isset($this->vars[$offset]);
464
    }
465
466
    /**
467
     * offsetGet
468
     *
469
     * @param mixed $offset array key
470
     *
471
     * @return mixed value
472
     */
473 2
    public function offsetGet($offset)
474
    {
475 2
        return $this->getVar($offset);
476
    }
477
478
    /**
479
     * offsetSet
480
     *
481
     * @param mixed $offset array key
482
     * @param mixed $value
483
     *
484
     * @return void
485
     */
486 6
    public function offsetSet($offset, $value)
487
    {
488 6
        $this->setVar($offset, $value);
489 6
    }
490
491
    /**
492
     * offsetUnset
493
     *
494
     * @param mixed $offset array key
495
     *
496
     * @return void
497
     */
498 1
    public function offsetUnset($offset)
499
    {
500 1
        $this->vars[$offset]['value'] = null;
501 1
        $this->vars[$offset]['changed'] = true;
502 1
        $this->setDirty();
503 1
    }
504
}
505