class/ModelReadIterator.php
<?php
namespace XoopsModules\Extgallery;
use CriteriaCompo;
use CriteriaElement;
use MyTextSanitizer;
use XoopsModelRead;
/**
* Extended object handlers
*
* You may not change or alter any portion of this comment or credits
* of supporting developers from this source code or any supporting source code
* which is considered copyrighted (c) material of the original comment or credit authors.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* @copyright XOOPS Project (https://xoops.org)
* @license http://www.fsf.org/copyleft/gpl.html GNU public license
* @package kernel
* @subpackage model
* @since 2.3.0
* @author Taiwen Jiang <phppp@users.sourceforge.net>
*/
/**
* Object render handler class.
*
* @author Taiwen Jiang <phppp@users.sourceforge.net>
* @copyright XOOPS Project (https://xoops.org)
*
* {@link XoopsObjectAbstract}
*/
class ModelReadIterator extends XoopsModelRead
{
/**
* get all objects matching a condition
*
* @param \CriteriaElement $criteria {@link CriteriaElement}
* to match
* @param array $fields variables to fetch
* @param bool $asObject flag indicating as object, otherwise as array
* @param bool $id_as_key use the ID as key for the array
* @return array of objects/array {@link XoopsObject}
*/
public function &getAll(CriteriaElement $criteria = null, $fields = null, $asObject = true, $id_as_key = true)
{
if ($fields && \is_array($fields)) {
if (!\in_array($this->handler->keyName, $fields)) {
$fields[] = $this->handler->keyName;
}
$select = '`' . \implode('`, `', $fields) . '`';
} else {
$select = '*';
}
$limit = null;
$start = null;
$sql = "SELECT {$select} FROM `{$this->handler->table}`";
if (\is_object($criteria) && \is_subclass_of($criteria, \CriteriaElement::class)) {
$sql .= ' ' . $criteria->renderWhere();
$sort = $criteria->getSort();
if ($sort) {
$sql .= " ORDER BY {$sort} " . $criteria->getOrder();
$orderSet = true;
}
$limit = $criteria->getLimit();
$start = $criteria->getStart();
}
if (empty($orderSet)) {
$sql .= " ORDER BY `{$this->handler->keyName}` DESC";
}
$result = $this->handler->db->query($sql, $limit, $start);
$ret = [];
if ($asObject) {
while (false !== ($myrow = $this->handler->db->fetchArray($result))) {
$object = $this->handler->create(false);
$object->assignVars($myrow);
if ($id_as_key) {
$ret[$myrow[$this->handler->keyName]] = $object;
} else {
$ret[] = $object;
}
unset($object);
}
} else {
$object = $this->handler->create(false);
while (false !== ($myrow = $this->handler->db->fetchArray($result))) {
$object->assignVars($myrow);
if ($id_as_key) {
$ret[$myrow[$this->handler->keyName]] = $object->getValues(\array_keys($myrow));
} else {
$ret[] = $object->getValues(\array_keys($myrow));
}
}
unset($object);
}
return $ret;
}
/**
* retrieve objects from the database
*
* For performance consideration, getAll() is recommended
*
* @param \CriteriaElement $criteria {@link CriteriaElement}
* conditions to be met
* @param bool $id_as_key use the ID as key for the array
* @param bool $as_object return an array of objects?
*
* @return array
*/
public function &getObjects(CriteriaElement $criteria = null, $id_as_key = false, $as_object = true)
{
$objects = &$this->getAll($criteria, null, $as_object, $id_as_key);
return $objects;
}
/**
* Retrieve a list of objects data
*
* @param \CriteriaElement $criteria {@link CriteriaElement} conditions to be met
* @param int $limit Max number of objects to fetch
* @param int $start Which record to start at
*
* @return array
*/
public function getList(CriteriaElement $criteria = null, $limit = 0, $start = 0)
{
$ret = [];
if (null === $criteria) {
$criteria = new CriteriaCompo();
}
$sql = "SELECT `{$this->handler->keyName}`";
if (!empty($this->handler->identifierName)) {
$sql .= ", `{$this->handler->identifierName}`";
}
$sql .= " FROM `{$this->handler->table}`";
if (\is_object($criteria) && \is_subclass_of($criteria, \CriteriaElement::class)) {
$sql .= ' ' . $criteria->renderWhere();
$sort = $criteria->getSort();
if ($sort) {
$sql .= ' ORDER BY ' . $sort . ' ' . $criteria->getOrder();
}
$limit = $criteria->getLimit();
$start = $criteria->getStart();
}
$result = $this->handler->db->query($sql, $limit, $start);
if (!$result) {
return $ret;
}
$myts = MyTextSanitizer::getInstance();
while (false !== ($myrow = $this->handler->db->fetchArray($result))) {
//identifiers should be textboxes, so sanitize them like that
$ret[$myrow[$this->handler->keyName]] = empty($this->handler->identifierName) ? 1 : \htmlspecialchars($myrow[$this->handler->identifierName]);
}
return $ret;
}
/**
* get IDs of objects matching a condition
*
* @param \CriteriaElement $criteria {@link CriteriaElement} to match
* @return array of object IDs
*/
public function &getIds(CriteriaElement $criteria = null)
{
$ret = [];
$sql = "SELECT `{$this->handler->keyName}` FROM `{$this->handler->table}`";
$limit = $start = null;
if (\is_object($criteria) && \is_subclass_of($criteria, \CriteriaElement::class)) {
$sql .= ' ' . $criteria->renderWhere();
$limit = $criteria->getLimit();
$start = $criteria->getStart();
}
if (!$result = $this->handler->db->query($sql, $limit, $start)) {
return $ret;
}
while (false !== ($myrow = $this->handler->db->fetchArray($result))) {
$ret[] = $myrow[$this->handler->keyName];
}
return $ret;
}
/**
* get a limited list of objects matching a condition
*
* {@link CriteriaCompo}
*
* @param int $limit Max number of objects to fetch
* @param int $start Which record to start at
* @param \CriteriaElement $criteria {@link CriteriaElement} to match
* @param array $fields variables to fetch
* @param bool $asObject flag indicating as object, otherwise as array
* @return array of objects {@link XoopsObject}
*/
public function &getByLimit(
$limit = 0,
$start = 0,
CriteriaElement $criteria = null,
$fields = null,
$asObject = true
) {
$trace = \debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS, 1);
\trigger_error(__CLASS__ . '::' . __FUNCTION__ . '() is deprecated, please use getAll instead.' . ". Called from {$trace[0]['file']}line {$trace[0]['line']}", \E_USER_WARNING);
if (\is_object($criteria) && \is_subclass_of($criteria, \CriteriaElement::class)) {
$criteria->setLimit($limit);
$criteria->setStart($start);
} elseif (!empty($limit)) {
$criteria = new CriteriaCompo();
$criteria->setLimit($limit);
$criteria->setStart($start);
}
$ret = $this->handler->getAll($criteria, $fields, $asObject);
return $ret;
}
/**
* Convert a database resultset to a returnable array
*
* @param database $result resultset
* @param bool $id_as_key - should NOT be used with joint keys
* @param bool $as_object
*
* @return array
*/
public function convertResultSet($result, $id_as_key = false, $as_object = true)
{
$trace = \debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS, 1);
\trigger_error(__CLASS__ . '::' . __FUNCTION__ . '() is deprecated.' . ". Called from {$trace[0]['file']}line {$trace[0]['line']}", \E_USER_WARNING);
$ret = [];
while (false !== ($myrow = $this->handler->db->fetchArray($result))) {
$obj = $this->handler->create(false);
$obj->assignVars($myrow);
if (!$id_as_key) {
if ($as_object) {
$ret[] = &$obj;
} else {
$row = [];
$vars = $obj->getVars();
foreach (\array_keys($vars) as $i) {
$row[$i] = $obj->getVar($i);
}
$ret[] = $row;
}
} else {
if ($as_object) {
$ret[$myrow[$this->handler->keyName]] = &$obj;
} else {
$row = [];
$vars = $obj->getVars();
foreach (\array_keys($vars) as $i) {
$row[$i] = $obj->getVar($i);
}
$ret[$myrow[$this->handler->keyName]] = $row;
}
}
unset($obj);
}
return $ret;
}
}