PSR-2. I'm not a huge fan, but ugly consistency beats no consistency...

This commit is contained in:
Shish
2019-05-28 17:59:38 +01:00
parent 5ec3e89884
commit 34b05cca7c
295 changed files with 27094 additions and 24632 deletions

View File

@ -20,60 +20,59 @@
* <code>/random_image/download/size=1024x768+cute</code>
*/
class RandomImage extends Extension {
public function onPageRequest(PageRequestEvent $event) {
global $page;
class RandomImage extends Extension
{
public function onPageRequest(PageRequestEvent $event)
{
global $page;
if($event->page_matches("random_image")) {
if($event->count_args() == 1) {
$action = $event->get_arg(0);
$search_terms = array();
}
else if($event->count_args() == 2) {
$action = $event->get_arg(0);
$search_terms = explode(' ', $event->get_arg(1));
}
else {
throw new SCoreException("Error: too many arguments.");
}
$image = Image::by_random($search_terms);
if ($event->page_matches("random_image")) {
if ($event->count_args() == 1) {
$action = $event->get_arg(0);
$search_terms = [];
} elseif ($event->count_args() == 2) {
$action = $event->get_arg(0);
$search_terms = explode(' ', $event->get_arg(1));
} else {
throw new SCoreException("Error: too many arguments.");
}
$image = Image::by_random($search_terms);
if($action === "download") {
if(!is_null($image)) {
$page->set_mode("data");
$page->set_type($image->get_mime_type());
$page->set_data(file_get_contents($image->get_image_filename()));
}
}
else if($action === "view") {
if(!is_null($image)) {
send_event(new DisplayingImageEvent($image));
}
}
else if($action === "widget") {
if(!is_null($image)) {
$page->set_mode("data");
$page->set_type("text/html");
$page->set_data($this->theme->build_thumb_html($image));
}
}
}
}
if ($action === "download") {
if (!is_null($image)) {
$page->set_mode("data");
$page->set_type($image->get_mime_type());
$page->set_data(file_get_contents($image->get_image_filename()));
}
} elseif ($action === "view") {
if (!is_null($image)) {
send_event(new DisplayingImageEvent($image));
}
} elseif ($action === "widget") {
if (!is_null($image)) {
$page->set_mode("data");
$page->set_type("text/html");
$page->set_data($this->theme->build_thumb_html($image));
}
}
}
}
public function onSetupBuilding(SetupBuildingEvent $event) {
$sb = new SetupBlock("Random Image");
$sb->add_bool_option("show_random_block", "Show Random Block: ");
$event->panel->add_block($sb);
}
public function onSetupBuilding(SetupBuildingEvent $event)
{
$sb = new SetupBlock("Random Image");
$sb->add_bool_option("show_random_block", "Show Random Block: ");
$event->panel->add_block($sb);
}
public function onPostListBuilding(PostListBuildingEvent $event) {
global $config, $page;
if($config->get_bool("show_random_block")) {
$image = Image::by_random($event->search_terms);
if(!is_null($image)) {
$this->theme->display_random($page, $image);
}
}
}
public function onPostListBuilding(PostListBuildingEvent $event)
{
global $config, $page;
if ($config->get_bool("show_random_block")) {
$image = Image::by_random($event->search_terms);
if (!is_null($image)) {
$this->theme->display_random($page, $image);
}
}
}
}

View File

@ -1,57 +1,59 @@
<?php
class RandomTest extends ShimmiePHPUnitTestCase {
public function testRandom() {
$this->log_in_as_user();
$image_id = $this->post_image("tests/pbx_screenshot.jpg", "test");
$this->log_out();
class RandomTest extends ShimmiePHPUnitTestCase
{
public function testRandom()
{
$this->log_in_as_user();
$image_id = $this->post_image("tests/pbx_screenshot.jpg", "test");
$this->log_out();
$this->get_page("random_image/view");
$this->assert_title("Image $image_id: test");
$this->get_page("random_image/view");
$this->assert_title("Image $image_id: test");
$this->get_page("random_image/view/test");
$this->assert_title("Image $image_id: test");
$this->get_page("random_image/view/test");
$this->assert_title("Image $image_id: test");
$this->get_page("random_image/download");
# FIXME: assert($raw == file(blah.jpg))
}
$this->get_page("random_image/download");
# FIXME: assert($raw == file(blah.jpg))
}
public function testPostListBlock() {
$this->log_in_as_admin();
$this->get_page("setup");
public function testPostListBlock()
{
$this->log_in_as_admin();
$this->get_page("setup");
$this->markTestIncomplete();
$this->markTestIncomplete();
$this->set_field("_config_show_random_block", true);
$this->click("Save Settings");
$this->log_out();
$this->set_field("_config_show_random_block", true);
$this->click("Save Settings");
$this->log_out();
# enabled, no image = no text
$this->get_page("post/list");
$this->assert_no_text("Random Image");
# enabled, no image = no text
$this->get_page("post/list");
$this->assert_no_text("Random Image");
$this->log_in_as_user();
$image_id = $this->post_image("tests/pbx_screenshot.jpg", "test");
$this->log_out();
$this->log_in_as_user();
$image_id = $this->post_image("tests/pbx_screenshot.jpg", "test");
$this->log_out();
# enabled, image = text
$this->get_page("post/list");
$this->assert_text("Random Image");
# enabled, image = text
$this->get_page("post/list");
$this->assert_text("Random Image");
$this->log_in_as_admin();
$this->get_page("setup");
$this->set_field("_config_show_random_block", true);
$this->click("Save Settings");
$this->log_in_as_admin();
$this->get_page("setup");
$this->set_field("_config_show_random_block", true);
$this->click("Save Settings");
# disabled, image = no text
$this->get_page("post/list");
$this->assert_text("Random Image");
# disabled, image = no text
$this->get_page("post/list");
$this->assert_text("Random Image");
$this->delete_image($image_id);
$this->log_out();
$this->delete_image($image_id);
$this->log_out();
# disabled, no image = no image
$this->get_page("post/list");
$this->assert_no_text("Random Image");
}
# disabled, no image = no image
$this->get_page("post/list");
$this->assert_no_text("Random Image");
}
}

View File

@ -1,19 +1,21 @@
<?php
class RandomImageTheme extends Themelet {
public function display_random(Page $page, Image $image) {
$page->add_block(new Block("Random Image", $this->build_random_html($image), "left", 8));
}
class RandomImageTheme extends Themelet
{
public function display_random(Page $page, Image $image)
{
$page->add_block(new Block("Random Image", $this->build_random_html($image), "left", 8));
}
public function build_random_html(Image $image, ?string $query = null): string {
public function build_random_html(Image $image, ?string $query = null): string
{
$i_id = int_escape($image->id);
$h_view_link = make_link("post/view/$i_id", $query);
$h_thumb_link = $image->get_thumb_link();
$h_tip = html_escape($image->get_tooltip());
$tsize = get_thumbnail_size($image->width, $image->height);
$i_id = int_escape($image->id);
$h_view_link = make_link("post/view/$i_id", $query);
$h_thumb_link = $image->get_thumb_link();
$h_tip = html_escape($image->get_tooltip());
$tsize = get_thumbnail_size($image->width, $image->height);
return "
return "
<center><div>
<a href='$h_view_link' style='position: relative; height: {$tsize[1]}px; width: {$tsize[0]}px;'>
@ -22,6 +24,5 @@ class RandomImageTheme extends Themelet {
</div></center>
";
}
}
}