db = Registry::get('db'); $this->direction = $direction; $this->res_assets = $res_assets; $this->res_meta = $res_meta; $this->res_thumbs = $res_thumbs; $this->return_format = $return_format; } public function next() { $row = $this->db->fetchAssoc($this->res_assets); // No more rows? if (!$row) return false; // Looks up metadata. $row['meta'] = []; while ($meta = $this->db->fetchAssoc($this->res_meta)) { if ($meta['id_asset'] != $row['id_asset']) continue; $row['meta'][$meta['variable']] = $meta['value']; } // Reset internal pointer for next asset. $this->db->data_seek($this->res_meta, 0); // Looks up thumbnails. $row['thumbnails'] = []; while ($thumbs = $this->db->fetchAssoc($this->res_thumbs)) { if ($thumbs['id_asset'] != $row['id_asset']) continue; $row['thumbnails'][$thumbs['selector']] = $thumbs['filename']; } // Reset internal pointer for next asset. $this->db->data_seek($this->res_thumbs, 0); if ($this->return_format === 'object') return new Asset($row); else return $row; } public function reset() { $this->db->data_seek($this->res_assets, 0); $this->db->data_seek($this->res_meta, 0); $this->db->data_seek($this->res_thumbs, 0); } public function clean() { if (!$this->res_assets) return; $this->db->free($this->res_assets); $this->res_assets = null; } public function num() { return $this->db->rowCount($this->res_assets); } public static function all() { return self::getByOptions(); } public static function getByOptions(array $options = [], $return_count = false, $return_format = 'object') { $params = [ 'limit' => isset($options['limit']) ? $options['limit'] : 0, 'order' => isset($options['order']) && in_array($options['order'], ['id_asset', 'subdir', 'filename', 'title', 'mime_type', 'image_width', 'image_height', 'date_captured']) ? $options['order'] : 'id_asset', 'direction' => isset($options['direction']) ? $options['direction'] : 'asc', ]; if (isset($options['offset'])) $params['offset'] = $options['offset']; elseif (isset($options['page']) && $options['page'] >= 1) $params['offset'] = $params['limit'] * ($options['page'] - 1); else $params['offset'] = 0; $where = []; if (isset($options['mime_type'])) { $params['mime_type'] = $options['mime_type']; if (is_array($options['mime_type'])) $where[] = 'a.mimetype IN(@mime_type)'; else $where[] = 'a.mimetype = :mime_type'; } if (isset($options['id_user_uploaded'])) { $params['id_user_uploaded'] = $options['id_user_uploaded']; $where[] = 'id_user_uploaded = {int:id_user_uploaded}'; } if (isset($options['id_tag'])) { $params['id_tag'] = $options['id_tag']; $where[] = 'id_asset IN( SELECT l.id_asset FROM assets_tags AS l WHERE l.id_tag = :id_tag)'; } elseif (isset($options['tag'])) { $params['tag'] = $options['tag']; $where[] = 'id_asset IN( SELECT l.id_asset FROM assets_tags AS l INNER JOIN tags AS t ON l.id_tag = t.id_tag WHERE t.slug = :tag)'; } // Make it valid SQL. $order = 'a.' . $params['order'] . ' ' . ($params['direction'] == 'desc' ? 'DESC' : 'ASC'); $where = empty($where) ? '1' : implode(' AND ', $where); // And ... go! $db = Registry::get('db'); // Get a resource object for the assets. $res_assets = $db->query(' SELECT a.* FROM assets AS a WHERE ' . $where . ' ORDER BY ' . $order . (!empty($params['limit']) ? ' LIMIT :offset, :limit' : ''), $params); // Get a resource object for the asset meta. $res_meta = $db->query(' SELECT id_asset, variable, value FROM assets_meta WHERE id_asset IN( SELECT id_asset FROM assets AS a WHERE ' . $where . ' ) ORDER BY id_asset', $params); // Get a resource object for the asset thumbs. $res_thumbs = $db->query(' SELECT id_asset, filename, CONCAT( width, :x, height, IF(mode != :empty1, CONCAT(:_, mode), :empty2) ) AS selector FROM assets_thumbs WHERE id_asset IN( SELECT id_asset FROM assets AS a WHERE ' . $where . ' ) ORDER BY id_asset', $params + [ 'empty1' => '', 'empty2' => '', 'x' => 'x', '_' => '_', ]); $iterator = new self($res_assets, $res_meta, $res_thumbs, $return_format, $params['direction']); // Returning total count, too? if ($return_count) { $count = $db->queryValue(' SELECT COUNT(*) FROM assets AS a WHERE ' . $where, $params); return [$iterator, $count]; } else return $iterator; } public function isAscending() { return $this->direction === 'asc'; } public function isDescending() { return $this->direction === 'desc'; } }