Made the random list searchable

This commit is contained in:
im-mi 2016-08-14 18:56:24 -04:00
parent d9485bbb40
commit 3bebe77add
2 changed files with 76 additions and 13 deletions

View file

@ -15,24 +15,40 @@ class RandomList extends Extension {
global $config, $page;
if($event->page_matches("random")) {
if(isset($_GET['search'])) {
// implode(explode()) to resolve aliases and sanitise
$search = url_escape(Tag::implode(Tag::explode($_GET['search'], false)));
if(empty($search)) {
$page->set_mode("redirect");
$page->set_redirect(make_link("random"));
}
else {
$page->set_mode("redirect");
$page->set_redirect(make_link('random/'.$search));
}
return;
}
if($event->count_args() == 0) {
$search_terms = array();
}
else if($event->count_args() == 1) {
$search_terms = explode(' ', $event->get_arg(0));
}
else {
throw new SCoreException("Error: too many arguments.");
}
// set vars
$page->title = "Random Images";
$images_per_page = $config->get_int("random_images_list_count", 12);
$random_images = array();
$random_html = "<b>Refresh the page to view more images</b>
<div class='shm-image-list'>";
// generate random images
for ($i = 0; $i < $images_per_page; $i++)
array_push($random_images, Image::by_random());
array_push($random_images, Image::by_random($search_terms));
// create html to display images
foreach ($random_images as $image)
$random_html .= $this->theme->build_thumb_html($image);
// display it
$random_html .= "</div>";
$page->add_block(new Block("Random Images", $random_html));
$this->theme->set_page($search_terms);
$this->theme->display_page($page, $random_images);
}
}

View file

@ -1,4 +1,51 @@
<?php
/* needed for access to build_thumb_html */
class RandomListTheme extends Themelet {}
class RandomListTheme extends Themelet {
protected $search_terms;
/**
* @param string[] $search_terms
*/
public function set_page($search_terms) {
$this->search_terms = $search_terms;
}
/**
* @param Page $page
* @param Image[] $images
*/
public function display_page(Page $page, $images) {
$page->title = "Random Images";
$html = "<b>Refresh the page to view more images</b>
<div class='shm-image-list'>";
foreach ($images as $image)
$html .= $this->build_thumb_html($image);
$html .= "</div>";
$page->add_block(new Block("Random Images", $html));
$nav = $this->build_navigation($this->search_terms);
$page->add_block(new Block("Navigation", $nav, "left", 0));
}
/**
* @param string[] $search_terms
* @return string
*/
protected function build_navigation($search_terms) {
$h_search_string = html_escape(implode(" ", $search_terms));
$h_search_link = make_link("random");
$h_search = "
<p><form action='$h_search_link' method='GET'>
<input type='search' name='search' value='$h_search_string' placeholder='Search random list' class='autocomplete_tags' autocomplete='off' />
<input type='hidden' name='q' value='/random'>
<input type='submit' value='Find' style='display: none;' />
</form>
";
return $h_search;
}
}