/** * Get a random image as an array * @since Version 3.10.0 * @param string $namespace An optional linked namespace to filter by * @param int $namespaceKey An optional linked namespace key to filter by * @return array */ public static function randomImage($namespace, $namespaceKey) { $Database = (new AppCore())->getDatabaseConnection(); if (is_null($namespace) && !is_null($namespaceKey)) { throw new InvalidArgumentException("A namespace key was specified but an associated namespace value was not."); } if (is_null($namespace) && is_null($namespaceKey)) { $query = "SELECT * FROM image AS r1 JOIN (SELECT CEIL(RAND() * (SELECT MAX(id) FROM image)) AS randomid) AS r2 WHERE r1.id >= r2.randomid ORDER BY r1.id ASC LIMIT 1"; $row = $Database->fetchRow($query); $row['meta'] = json_decode($row['meta'], true); $row['sizes'] = Images::normaliseSizes($row['meta']['sizes']); $row['url'] = Url::CreateFromImageID($row['id']); $row['url'] = $row['url']->getURLs(); return $row; } if (!is_null($namespace)) { $query = "SELECT il.image_id FROM image_link AS il LEFT JOIN image AS i ON i.id = il.image_id WHERE il.namespace = ? AND i.provider IS NOT NULL"; $params = [$namespace]; if (!is_null($namespaceKey)) { $query .= " AND namespace_key = ?"; $params[] = $namespaceKey; } $ids = []; foreach ($Database->fetchAll($query, $params) as $row) { $ids[] = $row['image_id']; } $image_id = $ids[array_rand($ids)]; $query = "SELECT * FROM image WHERE id = ?"; $row = $Database->fetchRow($query, $image_id); $row['meta'] = json_decode($row['meta'], true); $row['sizes'] = Images::normaliseSizes($row['meta']['sizes']); $row['url'] = Url::CreateFromImageID($row['id']); $row['url'] = $row['url']->getURLs(); return $row; } return; }
/** * Commit changes to this image * * @since Version 3.8.7 * @return boolean */ public function commit() { $this->validate(); $user_id = isset($this->author->User) && $this->author->User instanceof User ? $this->author->User->id : 0; $author = $this->author; unset($author->User); $data = array("title" => $this->title, "description" => $this->description, "captured" => $this->DateCaptured instanceof DateTime ? $this->DateCaptured->format("Y-m-d H:i:s") : null, "provider" => $this->provider, "photo_id" => $this->photo_id, "user_id" => $user_id, "meta" => json_encode(array("title" => $this->title, "description" => $this->description, "sizes" => $this->sizes, "links" => $this->links, "data" => $this->meta, "author" => $author))); if ($this->Place instanceof Place) { $data['lat'] = $this->Place->lat; $data['lon'] = $this->Place->lon; } // Update if (filter_var($this->id, FILTER_VALIDATE_INT)) { $this->Memcached->delete($this->mckey); $this->Redis->delete($this->mckey); $where = array("id = ?" => $this->id); $Date = new DateTime(); $data['modified'] = $Date->format("Y-m-d g:i:s"); $this->db->update("image", $data, $where); $this->getJSON(); return $this; } // Insert $this->db->insert("image", $data); $this->id = $this->db->lastInsertId(); $this->url = Utility\Url::CreateFromImageID($this->id); $this->getJSON(); return $this; }
/** * Get previous photos * @since Version 3.10.0 * @return array * @param int $page * @param int $itemsPerPage */ public function getPreviousPhotos($page = 1, $itemsPerPage = 25) { $Date = self::getStartOfWeek(new DateTime()); $query = "SELECT SQL_CALC_FOUND_ROWS i.*, u.username, iw.added_by AS user_id FROM image_weekly AS iw\r\n LEFT JOIN image AS i ON iw.image_id = i.id\r\n LEFT JOIN nuke_users AS u ON u.user_id = iw.added_by\r\n WHERE iw.datefrom < ? LIMIT ?, ?"; $params = [$Date->format("Y-m-d"), ($page - 1) * $itemsPerPage, $itemsPerPage]; $result = $this->db->fetchAll($query, $params); $return = ["total" => 0, "page" => $page, "items_per_page" => $itemsPerPage, "photos" => []]; $return['total'] = $this->db->fetchOne("SELECT FOUND_ROWS() AS total"); foreach ($result as $row) { $row['meta'] = json_decode($row['meta'], true); $row['meta']['sizes'] = Images::normaliseSizes($row['meta']['sizes']); $row['url'] = Utility\Url::CreateFromImageID($row['id'])->getURLs(); $return['photos'][] = $row; } return $return; }