XOOPS  2.6.0
XoopsPersistableObjectHandler.php
Go to the documentation of this file.
1 <?php
11 namespace Xoops\Core\Kernel;
12 
17 
31 {
42  protected $handler;
43 
54  private $handlers = array('read' => null, 'stats' => null, 'joint' => null, 'write' => null, 'sync' => null);
55 
62  public $table;
63 
67  public $keyName;
68 
72  public $className;
73 
77  public $table_link;
78 
83 
87  public $field_link;
88 
92  public $field_object;
93 
97  public $keyName_link;
98 
109  protected function __construct(
110  Connection $db = null,
111  $table = '',
112  $className = '',
113  $keyName = '',
114  $identifierName = ''
115  ) {
116  parent::__construct($db);
117  $this->table = $this->db2->prefix($table);
118  $this->keyName = $keyName;
119  $this->className = $className;
120  if ($identifierName) {
121  $this->identifierName = $identifierName;
122  }
123  }
124 
134  public function setHandler($handler = null, $args = null, $path = null)
135  {
136  $this->handler = null;
137  if (is_object($handler)) {
138  $this->handler = $handler;
139  } else {
140  if (is_string($handler)) {
142  $this->handler = $xmf->loadHandler($this, $handler, $args, $path);
143  }
144  }
145  return $this->handler;
146  }
147 
156  public function loadHandler($name, $args = null)
157  {
158  static $handlers;
159  if (!isset($handlers[$name])) {
161  $handlers[$name] = $xmf->loadHandler($this, $name, $args);
162  }
163  /* @var $handler XoopsModelAbstract */
165  $handler->setHandler($this);
166  $handler->setVars($args);
167 
168  return $handler;
169 
181  }
182 
191  public function __call($name, $args)
192  {
193  if (is_object($this->handler) && is_callable(array($this->handler, $name))) {
194  return call_user_func_array(array($this->handler, $name), $args);
195  }
196  foreach (array_keys($this->handlers) as $_handler) {
197  $handler = $this->loadHandler($_handler);
198  if (is_callable(array($handler, $name))) {
199  return call_user_func_array(array($handler, $name), $args);
200  }
201  }
202 
203  return null;
204  }
205 
218  public function create($isNew = true)
219  {
220  if (empty($this->className)) return false;
221 
222  /* @var $obj XoopsObject */
223  $obj = new $this->className();
224  if ($isNew === true) {
225  $obj->setNew();
226  }
227  return $obj;
228  }
229 
238  public function get($id = null, $fields = null)
239  {
240  $object = null;
241  if (empty($id)) {
242  $object = $this->create();
243  return $object;
244  }
245  $qb = $this->db2->createXoopsQueryBuilder();
246  $eb = $qb->expr();
247  if (is_array($fields) && count($fields) > 0) {
248  if (!in_array($this->keyName, $fields)) {
249  $fields[] = $this->keyName;
250  }
251  $first=true;
252  foreach ($fields as $field) {
253  if ($first) {
254  $first=false;
255  $qb->select($field);
256  } else {
257  $qb->addSelect($field);
258  }
259  }
260  } else {
261  $qb->select('*');
262  }
263  $qb->from($this->table, null)
264  ->where($eb->eq($this->keyName, ':id'))
265  ->setParameter(':id', $id, \PDO::PARAM_INT);
266  if (!$result = $qb->execute()) {
267  return $object;
268  }
269  $row = $result->fetch(\PDO::FETCH_ASSOC);
270  if (!$row) {
271  return $object;
272  }
273  $object = $this->create(false);
274  $object->assignVars($row);
275 
276  return $object;
277  }
278 
296  public function insert(XoopsObject $object, $force = true)
297  {
298  /* @var $handler XoopsModelWrite */
299  $handler = $this->loadHandler('write');
300  return $handler->insert($object, $force);
301  }
302 
311  public function delete(XoopsObject $object, $force = false)
312  {
313  /* @var $handler XoopsModelWrite */
314  $handler = $this->loadHandler('write');
315  return $handler->delete($object, $force);
316  }
317 
328  public function deleteAll(CriteriaElement $criteria, $force = true, $asObject = false)
329  {
330  if (empty($criteria)) return false;
331 
332  /* @var $handler XoopsModelWrite */
333  $handler = $this->loadHandler('write');
334  return $handler->deleteAll($criteria, $force, $asObject);
335  }
336 
347  public function updateAll($fieldname, $fieldvalue, CriteriaElement $criteria, $force = false)
348  {
349  if (empty($criteria)) return false;
350 
351  /* @var $handler XoopsModelWrite */
352  $handler = $this->loadHandler('write');
353  return $handler->updateAll($fieldname, $fieldvalue, $criteria, $force);
354  }
355 
374  public function getObjects(CriteriaElement $criteria = null, $id_as_key = false, $as_object = true)
375  {
376  /* @var $handler XoopsModelRead */
377  $handler = $this->loadHandler('read');
378  $ret = $handler->getObjects($criteria, $id_as_key, $as_object);
379  return $ret;
380  }
381 
392  public function getAll(CriteriaElement $criteria = null, $fields = null, $asObject = true, $id_as_key = true)
393  {
394  /* @var $handler XoopsModelRead */
395  $handler = $this->loadHandler('read');
396  $ret = $handler->getAll($criteria, $fields, $asObject, $id_as_key);
397  return $ret;
398  }
399 
409  public function getList(CriteriaElement $criteria = null, $limit = 0, $start = 0)
410  {
411  /* @var $handler XoopsModelRead */
412  $handler = $this->loadHandler('read');
413  $ret = $handler->getList($criteria, $limit, $start);
414  return $ret;
415  }
416 
424  public function getIds(CriteriaElement $criteria = null)
425  {
426  /* @var $handler XoopsModelRead */
427  $handler = $this->loadHandler('read');
428  $ret = $handler->getIds($criteria);
429  return $ret;
430  }
431 
447  public function getCount(CriteriaElement $criteria = null)
448  {
449  /* @var $handler XoopsModelStats */
450  $handler = $this->loadHandler('stats');
451  return $handler->getCount($criteria);
452  }
453 
461  public function getCounts(CriteriaElement $criteria = null)
462  {
463  /* @var $handler XoopsModelStats*/
464  $handler = $this->loadHandler('stats');
465  return $handler->getCounts($criteria);
466  }
467 
487  public function getByLink(
488  CriteriaElement $criteria = null,
489  $fields = null,
490  $asObject = true,
491  $field_link = null,
492  $field_object = null
493  ) {
494  /* @var $handler XoopsModelJoint */
495  $handler = $this->loadHandler('joint');
496  $ret = $handler->getByLink($criteria, $fields, $asObject, $field_link, $field_object);
497  return $ret;
498  }
499 
507  public function getCountByLink(CriteriaElement $criteria = null)
508  {
509  /* @var $handler XoopsModelJoint */
510  $handler = $this->loadHandler('joint');
511  $ret = $handler->getCountByLink($criteria);
512  return $ret;
513  }
514 
522  public function getCountsByLink(CriteriaElement $criteria = null)
523  {
524  /* @var $handler XoopsModelJoint */
525  $handler = $this->loadHandler('joint');
526  $ret = $handler->getCountsByLink($criteria);
527  return $ret;
528  }
529 
538  public function updateByLink($data, CriteriaElement $criteria = null)
539  {
540  /* @var $handler XoopsModelJoint */
541  $handler = $this->loadHandler('joint');
542  $ret = $handler->updateByLink($data, $criteria);
543  return $ret;
544  }
545 
553  public function deleteByLink(CriteriaElement $criteria = null)
554  {
555  /* @var $handler XoopsModelJoint */
556  $handler = $this->loadHandler('joint');
557  $ret = $handler->deleteByLink($criteria);
558  return $ret;
559  }
560 
578  public function cleanOrphan($table_link = '', $field_link = '', $field_object = '')
579  {
580  /* @var $handler XoopsModelSync */
581  $handler = $this->loadHandler('sync');
582  $ret = $handler->cleanOrphan($table_link, $field_link, $field_object);
583  return $ret;
584  }
585 
591  public function synchronization($table_link = '', $field_link = '', $field_object = '')
592  {
593  $retval = $this->cleanOrphan($table_link, $field_link, $field_object);
594  return $retval;
595  }
599 }
getByLink(CriteriaElement $criteria=null, $fields=null, $asObject=true, $field_link=null, $field_object=null)
$path
Definition: execute.php:31
__construct(Connection $db=null, $table= '', $className= '', $keyName= '', $identifierName= '')
$result
Definition: pda.php:33
$id
Definition: admin_menu.php:36
getObjects(CriteriaElement $criteria=null, $id_as_key=false, $as_object=true)
getList(CriteriaElement $criteria=null, $limit=0, $start=0)
cleanOrphan($table_link= '', $field_link= '', $field_object= '')
synchronization($table_link= '', $field_link= '', $field_object= '')
deleteAll(CriteriaElement $criteria, $force=true, $asObject=false)
$limit
Definition: findusers.php:202
$criteria
$start
updateAll($fieldname, $fieldvalue, CriteriaElement $criteria, $force=false)
getAll(CriteriaElement $criteria=null, $fields=null, $asObject=true, $id_as_key=true)