PageRenderTime 47ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/image_block/helpers/image_block_block.php

https://github.com/ShakeNBake/gallery3
PHP | 63 lines | 38 code | 6 blank | 19 comment | 5 complexity | df8879227cbcf641be95293b2e26f65e MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php defined("SYSPATH") or die("No direct script access.");
  2. /**
  3. * Gallery - a web based photo album viewer and editor
  4. * Copyright (C) 2000-2009 Bharat Mediratta
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. class image_block_block_Core {
  21. static function get_site_list() {
  22. return array("random_image" => t("Random Image"));
  23. }
  24. static function get($block_id, $theme) {
  25. $block = "";
  26. switch ($block_id) {
  27. case "random_image":
  28. $block = new Block();
  29. $block->css_id = "gImageBlock";
  30. $block->title = t("Random Image");
  31. $block->content = new View("image_block_block.html");
  32. $random = ((float)mt_rand()) / (float)mt_getrandmax();
  33. $items = ORM::factory("item")
  34. ->viewable()
  35. ->where("type !=", "album")
  36. ->where("rand_key < ", $random)
  37. ->orderby(array("rand_key" => "DESC"))
  38. ->find_all(1);
  39. if ($items->count() == 0) {
  40. // Try once more. If this fails, just ditch the block altogether
  41. $items = ORM::factory("item")
  42. ->viewable()
  43. ->where("type !=", "album")
  44. ->where("rand_key >= ", $random)
  45. ->orderby(array("rand_key" => "DESC"))
  46. ->find_all(1);
  47. }
  48. if ($items->count() > 0) {
  49. $block->content->item = $items->current();
  50. } else {
  51. $block = "";
  52. }
  53. break;
  54. }
  55. return $block;
  56. }
  57. }