XOOPS  2.6.0
XoopsObject.php
Go to the documentation of this file.
1 <?php
13 namespace Xoops\Core\Kernel;
14 
16 
20 define('XOBJ_DTYPE_TXTBOX', 1);
21 define('XOBJ_DTYPE_TXTAREA', 2);
22 define('XOBJ_DTYPE_INT', 3);
23 define('XOBJ_DTYPE_URL', 4);
24 define('XOBJ_DTYPE_EMAIL', 5);
25 define('XOBJ_DTYPE_ARRAY', 6);
26 define('XOBJ_DTYPE_OTHER', 7);
27 define('XOBJ_DTYPE_SOURCE', 8);
28 define('XOBJ_DTYPE_STIME', 9);
29 define('XOBJ_DTYPE_MTIME', 10);
30 define('XOBJ_DTYPE_LTIME', 11);
31 define('XOBJ_DTYPE_FLOAT', 13);
32 define('XOBJ_DTYPE_DECIMAL', 14);
33 define('XOBJ_DTYPE_ENUM', 15);
34 
47 abstract class XoopsObject
48 {
54  public $vars = array();
55 
61  public $cleanVars = array();
62 
68  private $_isNew = false;
69 
75  private $_isDirty = false;
76 
82  private $_errors = array();
83 
89  private $_filters = array();
90 
94  public $plugin_path;
95 
101  public function setNew()
102  {
103  $this->_isNew = true;
104  }
105 
111  public function unsetNew()
112  {
113  $this->_isNew = false;
114  }
115 
121  public function isNew()
122  {
123  return $this->_isNew;
124  }
125 
134  public function setDirty()
135  {
136  $this->_isDirty = true;
137  }
138 
144  public function unsetDirty()
145  {
146  $this->_isDirty = false;
147  }
148 
154  public function isDirty()
155  {
156  return $this->_isDirty;
157  }
158 
172  public function initVar($key, $data_type, $value = null, $required = false, $maxlength = null, $options = '')
173  {
174  $this->vars[$key] = array(
175  'value' => $value,
176  'required' => $required,
177  'data_type' => $data_type,
178  'maxlength' => $maxlength,
179  'changed' => false,
180  'options' => $options
181  );
182  }
183 
192  public function assignVar($key, $value)
193  {
194  if (isset($key) && isset($this->vars[$key])) {
195  $this->vars[$key]['value'] = $value;
196  }
197  }
198 
206  public function assignVars($var_arr)
207  {
208  if(is_array($var_arr)) foreach ($var_arr as $key => $value) {
209  $this->assignVar($key, $value);
210  }
211  }
212 
222  public function setVar($key, $value, $not_gpc = false)
223  {
224  if (!empty($key) && isset($value) && isset($this->vars[$key])) {
225  $this->vars[$key]['value'] = $value;
226  $this->vars[$key]['not_gpc'] = $not_gpc;
227  $this->vars[$key]['changed'] = true;
228  $this->setDirty();
229  }
230  }
231 
240  public function setVars($var_arr, $not_gpc = false)
241  {
242  if(is_array($var_arr)) foreach ($var_arr as $key => $value) {
243  $this->setVar($key, $value, $not_gpc);
244  }
245  }
246 
254  public function destroyVars($var)
255  {
256  if (empty($var)) {
257  return true;
258  }
259  $var = !is_array($var) ? array($var) : $var;
260  foreach ($var as $key) {
261  if (!isset($this->vars[$key])) {
262  continue;
263  }
264  $this->vars[$key]['changed'] = null;
265  }
266  return true;
267  }
268 
282  public function setFormVars($var_arr = null, $pref = 'xo_', $not_gpc = false)
283  {
284  $len = strlen($pref);
285  if(is_array($var_arr)) foreach ($var_arr as $key => $value) {
286  if ($pref == substr($key, 0, $len)) {
287  $this->setVar(substr($key, $len), $value, $not_gpc);
288  }
289  }
290  }
291 
298  public function getVars()
299  {
300  return $this->vars;
301  }
302 
312  public function getValues($keys = null, $format = 's', $maxDepth = 1)
313  {
314  if (!isset($keys)) {
315  $keys = array_keys($this->vars);
316  }
317  $vars = array();
318  if(is_array($keys)) foreach ($keys as $key) {
319  if (isset($this->vars[$key])) {
320  if (is_object($this->vars[$key]) && is_a($this->vars[$key], 'Xoops\Core\Kernel\XoopsObject')) {
321  if ($maxDepth) {
322  /* @var $obj XoopsObject */
323  $obj = $this->vars[$key];
324  $vars[$key] = $obj->getValues(null, $format, $maxDepth - 1);
325  }
326  } else {
327  $vars[$key] = $this->getVar($key, $format);
328  }
329  }
330  }
331  return $vars;
332  }
333 
342  public function getVar($key, $format = 's')
343  {
344  $ret = null;
345  if (!isset($this->vars[$key])) {
346  return $ret;
347  }
348  $ret = Dtype::getVar($this, $key, $format);
349  return $ret;
350  }
351 
361  public function cleanVars($quote = true)
362  {
364  $existing_errors = $this->getErrors();
365  $this->_errors = array();
366  foreach ($this->vars as $k => $v) {
367  if (!$v['changed']) {
368  } else {
369  $this->cleanVars[$k] = Dtype::cleanVar($this, $k, $quote);
370  }
371  }
372  if (count($this->_errors) > 0) {
373  $this->_errors = array_merge($existing_errors, $this->_errors);
374  return false;
375  }
376  // $this->_errors = array_merge($existing_errors, $this->_errors);
377  $this->unsetDirty();
378  return true;
379  }
380 
388  public function registerFilter($filtername)
389  {
390  $this->_filters[] = $filtername;
391  }
392 
398  private function _loadFilters()
399  {
400  static $loaded;
401  if (isset($loaded)) {
402  return;
403  }
404  $loaded = 1;
405 
406  $path = empty($this->plugin_path) ? __DIR__ . '/filters' : $this->plugin_path;
407  if (\XoopsLoad::fileExists($file = $path . '/filter.php')) {
408  include_once $file;
409  if(is_array($this->_filters)) foreach ($this->_filters as $f) {
410  if (\XoopsLoad::fileExists($file = $path . '/' . strtolower($f) . 'php')) {
411  include_once $file;
412  }
413  }
414  }
415  }
416 
430  public function loadFilters($method)
431  {
432  $this->_loadFilters();
433 
434  $class = get_class($this);
435  $modules_active = \Xoops::getInstance()->getActiveModules();
436  if (is_array($modules_active)) foreach ($modules_active as $dirname) {
437  $file = \XoopsBaseConfig::get('root-path') . '/modules/' . $dirname . '/filter/' . $class . '.' . $method . '.php';
439  include_once $file;
440  $function = $dirname . '_' . $class . '_' . $method;
441  if (function_exists($function)) {
442  call_user_func_array($function, array(&$this));
443  }
444  }
445  }
446  }
447 
454  public function xoopsClone()
455  {
456  /* @var $clone XoopsObject */
457  $class = get_class($this);
458  $clone = null;
459  $clone = new $class();
460  foreach ($this->vars as $k => $v) {
461  $clone->assignVar($k, $v['value']); // Only for vars initialized within clone::__construct
462  }
463  // need this to notify the handler class that this is a newly created object
464  $clone->setNew();
465  return $clone;
466  }
467 
475  public function setErrors($err_str)
476  {
477  if (is_array($err_str)) {
478  $this->_errors = array_merge($this->_errors, $err_str);
479  } else {
480  $this->_errors[] = trim($err_str);
481  }
482  }
483 
490  public function getErrors()
491  {
492  return $this->_errors;
493  }
494 
502  public function getHtmlErrors()
503  {
504  $ret = '<h4>Errors</h4>';
505  if (!empty($this->_errors)) {
506  foreach ($this->_errors as $error) {
507  $ret .= $error . '<br />';
508  }
509  } else {
510  $ret .= 'None<br />';
511  }
512  return $ret;
513  }
514 
521  public function toArray()
522  {
523  return $this->getValues();
524  }
525 }
$path
Definition: execute.php:31
if(empty($settings['ROOT_PATH'])) elseif(empty($settings['DB_PARAMETERS'])) $error
static getVar(XoopsObject $obj, $key, $format)
Definition: Dtype.php:52
static getInstance()
Definition: Xoops.php:160
static cleanVar(XoopsObject $obj, $key, $quote=true)
Definition: Dtype.php:38
if(!isset($xoops->paths[$path_type])) if($path_type== 'var') $file
Definition: browse.php:55
$options['editor']
getVar($key, $format= 's')
setFormVars($var_arr=null, $pref= 'xo_', $not_gpc=false)
getValues($keys=null, $format= 's', $maxDepth=1)
static fileExists($file)
Definition: xoopsload.php:506
static get($name)
setVars($var_arr, $not_gpc=false)
$keys
Definition: install_tpl.php:37
$dirname
Definition: backend.php:38
$var
Definition: userinfo.php:125
$modules_active
setVar($key, $value, $not_gpc=false)
initVar($key, $data_type, $value=null, $required=false, $maxlength=null, $options= '')