PageRenderTime 45ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/3.0/modules/latestupdates/controllers/latestupdates.php

https://github.com/TedBrew/gallery3-contrib
PHP | 595 lines | 407 code | 69 blank | 119 comment | 98 complexity | 432d2283bea6e14a197acfd199d74eea MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0, LGPL-2.1
  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-2012 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 latestupdates_Controller extends Controller {
  21. public function user_profiles($str_display_type, $user_id) {
  22. // Make sure user_id is valid, throw a 404 error if its not.
  23. $current_user = ORM::factory("user", $user_id);
  24. if (!$current_user->loaded()) {
  25. throw new Kohana_404_Exception();
  26. }
  27. // Grab the first 10 items for the specified display type.
  28. // Default to "popular" if display type is invalid.
  29. $template = new View("latestupdates_user_profile_carousel.html");
  30. $template->items = latestupdates_Controller::items($str_display_type, $user_id, 10);
  31. // Figure out the text for the "View more" link.
  32. if ($str_display_type == "recent") {
  33. $template->str_view_more_title = t("View all recent uploads");
  34. } elseif ($str_display_type == "albums") {
  35. $template->str_view_more_title = t("View all recent albums");
  36. } else {
  37. $template->str_view_more_title = t("View more popular uploads");
  38. }
  39. // Set up a "View more" url.
  40. $template->str_view_more_url = url::site("latestupdates/users/{$str_display_type}/{$user_id}");
  41. // Display the page.
  42. print $template;
  43. // Make item links in the carousel load as virtual albums for the view type instead of the regular album.
  44. item::set_display_context_callback("latestupdates_Controller::get_display_context",
  45. $str_display_type, $user_id);
  46. return ;
  47. }
  48. public function users($str_display_type, $user_id) {
  49. // Generate a dynamic page with items uploaded by a specific user ($user_id).
  50. // Make sure user_id is valid.
  51. $current_user = ORM::factory("user", $user_id);
  52. if (!$current_user->loaded()) {
  53. throw new Kohana_404_Exception();
  54. }
  55. // Figure out how many items to display on each page.
  56. $page_size = module::get_var("gallery", "page_size", 9);
  57. // Figure out which page # the visitor is on and
  58. // don't allow the visitor to go below page 1.
  59. $page = Input::instance()->get("page", 1);
  60. if ($page < 1) {
  61. url::redirect("latestupdates/users/{$str_display_type}/{$user_id}");
  62. }
  63. // If this page was reached from a breadcrumb, figure out what page to load from the show id.
  64. $show = Input::instance()->get("show");
  65. if ($show) {
  66. $child = ORM::factory("item", $show);
  67. $index = latestupdates_Controller::_get_position($child, $str_display_type, $user_id);
  68. if ($index) {
  69. $page = ceil($index / $page_size);
  70. if ($page == 1) {
  71. url::redirect("latestupdates/users/{$str_display_type}/{$user_id}");
  72. } else {
  73. url::redirect("latestupdates/users/{$str_display_type}/{$user_id}?page=$page");
  74. }
  75. }
  76. }
  77. // First item to display.
  78. $offset = ($page - 1) * $page_size;
  79. // Determine the total number of items,
  80. // for page numbering purposes.
  81. $count = latestupdates_Controller::items_count($str_display_type, $user_id);
  82. // Figure out what the highest page number is.
  83. $max_pages = ceil($count / $page_size);
  84. // Don't let the visitor go past the last page.
  85. if ($max_pages && $page > $max_pages) {
  86. url::redirect("latestupdates/users/{$str_display_type}/{$user_id}?page=$max_pages");
  87. }
  88. // Figure out which items to display on this page.
  89. $children = latestupdates_Controller::items($str_display_type, $user_id, $page_size, $offset);
  90. // Figure out the page title.
  91. $str_page_title = "";
  92. if ($str_display_type == "recent") {
  93. $str_page_title = t("Recent Uploads");
  94. } elseif ($str_display_type == "albums") {
  95. $str_page_title = t("Recent Albums");
  96. } else {
  97. $str_page_title = t("Most Viewed");
  98. }
  99. // Set up the previous and next page buttons.
  100. if ($page > 1) {
  101. $previous_page = $page - 1;
  102. $view->previous_page_link = url::site("latestupdates/users/{$str_display_type}/{$user_id}?page={$previous_page}");
  103. }
  104. if ($page < $max_pages) {
  105. $next_page = $page + 1;
  106. $view->next_page_link = url::site("latestupdates/users/{$str_display_type}/{$user_id}?page={$next_page}");
  107. }
  108. // Set up and display the actual page.
  109. $root = item::root();
  110. $template = new Theme_View("page.html", "collection", "LatestUpdates");
  111. $template->page_title = t("Gallery :: Latest Updates");
  112. $template->set_global(
  113. array("page" => $page,
  114. "max_pages" => $max_pages,
  115. "page_size" => $page_size,
  116. "children" => $children,
  117. "breadcrumbs" => array(
  118. Breadcrumb::instance($root->title, $root->url())->set_first(),
  119. Breadcrumb::instance(t("User profile: %name", array("name" => $current_user->display_name())),
  120. url::site("user_profile/show/{$user_id}")),
  121. Breadcrumb::instance($str_page_title,
  122. url::site("latestupdates/users/{$str_display_type}/{$user_id}"))->set_last()),
  123. "children_count" => $count));
  124. $template->content = new View("dynamic.html");
  125. $template->content->title = $str_page_title;
  126. // Display the page.
  127. print $template;
  128. // Set up the callback so links within the photo page will lead to photos within the virtual album
  129. // instead of the actual album.
  130. item::set_display_context_callback("latestupdates_Controller::get_display_context",
  131. $str_display_type, $user_id);
  132. }
  133. public function albums($id) {
  134. // Figure out how many items to display on each page.
  135. $page_size = module::get_var("gallery", "page_size", 9);
  136. // Load the parent album.
  137. $item = ORM::factory("item", $id);
  138. // Figure out which page # the visitor is on and
  139. // don't allow the visitor to go below page 1.
  140. $page = Input::instance()->get("page", 1);
  141. if ($page < 1) {
  142. url::redirect("latestupdates/albums/{$item->id}");
  143. }
  144. // If this page was reached from a breadcrumb, figure out what page to load from the show id.
  145. $show = Input::instance()->get("show");
  146. if ($show) {
  147. $child = ORM::factory("item", $show);
  148. $index = latestupdates_Controller::_get_position($child, "descendants", $item->id);
  149. if ($index) {
  150. $page = ceil($index / $page_size);
  151. if ($page == 1) {
  152. url::redirect("latestupdates/albums/{$item->id}");
  153. } else {
  154. url::redirect("latestupdates/albums/{$item->id}?page=$page");
  155. }
  156. }
  157. }
  158. // First item to display.
  159. $offset = ($page - 1) * $page_size;
  160. // Determine the total number of items,
  161. // for page numbering purposes.
  162. $count = latestupdates_Controller::items_count("descendants", $item->id);
  163. // Figure out what the highest page number is.
  164. $max_pages = ceil($count / $page_size);
  165. // Don't let the visitor go past the last page.
  166. if ($max_pages && $page > $max_pages) {
  167. url::redirect("latestupdates/albums/{$item->id}?page=$max_pages");
  168. }
  169. // Figure out which items to display on this page.
  170. $children = latestupdates_Controller::items("descendants", $item->id, $page_size, $offset);
  171. // Set up the previous and next page buttons.
  172. if ($page > 1) {
  173. $previous_page = $page - 1;
  174. $view->previous_page_link = url::site("latestupdates/albums/{$item->id}?page={$previous_page}");
  175. }
  176. if ($page < $max_pages) {
  177. $next_page = $page + 1;
  178. $view->next_page_link = url::site("latestupdates/albums/{$item->id}?page={$next_page}");
  179. }
  180. // Set up breadcrumbs.
  181. $breadcrumbs = array();
  182. $counter = 0;
  183. $breadcrumbs[] = Breadcrumb::instance(t("Recent Uploads"), url::site("latestupdates/albums/{$item->id}"))->set_last();
  184. $parent_item = $item;
  185. while ($parent_item->id != 1) {
  186. $breadcrumbs[] = Breadcrumb::instance($parent_item->title, $parent_item->url());
  187. $parent_item = ORM::factory("item", $parent_item->parent_id);
  188. }
  189. $breadcrumbs[] = Breadcrumb::instance($parent_item->title, $parent_item->url())->set_first();
  190. $breadcrumbs = array_reverse($breadcrumbs, true);
  191. // Set up and display the actual page.
  192. $root = item::root();
  193. $template = new Theme_View("page.html", "collection", "LatestUpdates");
  194. $template->page_title = t("Gallery :: Latest Updates");
  195. $template->set_global(
  196. array("page" => $page,
  197. "max_pages" => $max_pages,
  198. "page_size" => $page_size,
  199. "children" => $children,
  200. "breadcrumbs" => $breadcrumbs,
  201. "children_count" => $count));
  202. $template->content = new View("dynamic.html");
  203. $template->content->title = t("Recent Uploads");
  204. // Display the page.
  205. print $template;
  206. // Set up the callback so links within the photo page will lead to photos within the virtual album
  207. // instead of the actual album.
  208. item::set_display_context_callback("latestupdates_Controller::get_display_context",
  209. "descendants", $item->id);
  210. }
  211. public function updates() {
  212. // Figure out how many items to display on each page.
  213. $page_size = module::get_var("gallery", "page_size", 9);
  214. // Figure out which page # the visitor is on and
  215. // don't allow the visitor to go below page 1.
  216. $page = Input::instance()->get("page", 1);
  217. if ($page < 1) {
  218. url::redirect("latestupdates/updates");
  219. }
  220. // If this page was reached from a breadcrumb, figure out what page to load from the show id.
  221. $show = Input::instance()->get("show");
  222. if ($show) {
  223. $child = ORM::factory("item", $show);
  224. $index = latestupdates_Controller::_get_position($child, "recent", 0);
  225. if ($index) {
  226. $page = ceil($index / $page_size);
  227. if ($page == 1) {
  228. url::redirect("latestupdates/updates");
  229. } else {
  230. url::redirect("latestupdates/updates?page=$page");
  231. }
  232. }
  233. }
  234. // First item to display.
  235. $offset = ($page - 1) * $page_size;
  236. // Determine the total number of items,
  237. // for page numbering purposes.
  238. $count = latestupdates_Controller::items_count("recent", 0);
  239. // Figure out what the highest page number is.
  240. $max_pages = ceil($count / $page_size);
  241. // Don't let the visitor go past the last page.
  242. if ($max_pages && $page > $max_pages) {
  243. url::redirect("latestupdates/updates?page=$max_pages");
  244. }
  245. // Figure out which items to display on this page.
  246. $items = latestupdates_Controller::items("recent", 0, $page_size, $offset);
  247. // Set up the previous and next page buttons.
  248. if ($page > 1) {
  249. $previous_page = $page - 1;
  250. $view->previous_page_link = url::site("latestupdates/updates?page={$previous_page}");
  251. }
  252. if ($page < $max_pages) {
  253. $next_page = $page + 1;
  254. $view->next_page_link = url::site("latestupdates/updates?page={$next_page}");
  255. }
  256. // Set up and display the actual page.
  257. $root = item::root();
  258. $template = new Theme_View("page.html", "collection", "LatestUpdates");
  259. $template->page_title = t("Gallery :: Latest Updates");
  260. $template->set_global(
  261. array("page" => $page,
  262. "max_pages" => $max_pages,
  263. "page_size" => $page_size,
  264. "children" => $items,
  265. "breadcrumbs" => array(
  266. Breadcrumb::instance($root->title, $root->url())->set_first(),
  267. Breadcrumb::instance(t("Recent Uploads"),
  268. url::site("latestupdates/updates"))->set_last()),
  269. "children_count" => $count));
  270. $template->content = new View("dynamic.html");
  271. $template->content->title = t("Recent Uploads");
  272. // Display the page.
  273. print $template;
  274. // Set up the callback so links within the photo page will lead to photos within the virtual album
  275. // instead of the actual album.
  276. item::set_display_context_callback("latestupdates_Controller::get_display_context",
  277. "recent", 0);
  278. }
  279. static function get_display_context($item, $str_display_type, $user_id) {
  280. // Set up display elements on the photo page to link to the virtual album.
  281. // Valid $str_display_type values are popular, recent, albums and descendants.
  282. // $user_id can be set to "0" to search site wide.
  283. // For "descendants", $user_id should be the album id #.
  284. // Figure out page title.
  285. $str_page_title = "";
  286. if ($str_display_type == "recent") {
  287. $str_page_title = t("Recent Uploads");
  288. } elseif ($str_display_type == "albums") {
  289. $str_page_title = t("Recent Albums");
  290. } elseif ($str_display_type == "descendants") {
  291. $str_page_title = t("Recent Uploads");
  292. } else {
  293. $str_page_title = t("Most Viewed");
  294. }
  295. // Figure out item position.
  296. $position = latestupdates_Controller::_get_position($item, $str_display_type, $user_id);
  297. // Figure out which items are the previous and next items with the virtual album.
  298. if ($position > 1) {
  299. list ($previous_item, $ignore, $next_item) =
  300. latestupdates_Controller::items($str_display_type, $user_id, 3, $position - 2);
  301. } else {
  302. $previous_item = null;
  303. list ($next_item) = latestupdates_Controller::items($str_display_type, $user_id, 1, $position);
  304. }
  305. // Figure out total number of items (excluding albums).
  306. $count = latestupdates_Controller::items_count($str_display_type, $user_id);
  307. // Set up breadcrumbs.
  308. $root = item::root();
  309. $breadcrumbs = array();
  310. if ($user_id == 0) {
  311. $breadcrumbs[0] = Breadcrumb::instance($root->title, $root->url())->set_first();
  312. $breadcrumbs[1] = Breadcrumb::instance($str_page_title,
  313. url::site("latestupdates/updates?show={$item->id}"));
  314. $breadcrumbs[2] = Breadcrumb::instance($item->title, $item->url())->set_last();
  315. } else {
  316. if ($str_display_type == "descendants") {
  317. $counter = 0;
  318. $breadcrumbs[] = Breadcrumb::instance($item->title, $item->url())->set_last();
  319. $breadcrumbs[] = Breadcrumb::instance(t("Recent Uploads"), url::site("latestupdates/albums/{$user_id}?show={$item->id}"));
  320. $parent_item = ORM::factory("item", $user_id);
  321. while ($parent_item->id != 1) {
  322. $breadcrumbs[] = Breadcrumb::instance($parent_item->title, $parent_item->url());
  323. $parent_item = ORM::factory("item", $parent_item->parent_id);
  324. }
  325. $breadcrumbs[] = Breadcrumb::instance($parent_item->title, $parent_item->url())->set_first();
  326. $breadcrumbs = array_reverse($breadcrumbs, true);
  327. } else {
  328. $current_user = ORM::factory("user", $user_id);
  329. $breadcrumbs[0] = Breadcrumb::instance($root->title, $root->url())->set_first();
  330. $breadcrumbs[1] = Breadcrumb::instance(t("User profile: %name", array("name" => $current_user->display_name())),
  331. url::site("user_profile/show/{$user_id}"));
  332. $breadcrumbs[2] = Breadcrumb::instance($str_page_title,
  333. url::site("latestupdates/users/{$str_display_type}/{$user_id}?show={$item->id}"));
  334. $breadcrumbs[3] = Breadcrumb::instance($item->title, $item->url())->set_last();
  335. }
  336. }
  337. // Return the display elements.
  338. return array("position" => $position,
  339. "previous_item" => $previous_item,
  340. "next_item" => $next_item,
  341. "sibling_count" => $count,
  342. "breadcrumbs" => $breadcrumbs
  343. );
  344. }
  345. static function items_count($str_display_type, $user_id) {
  346. // Figure out the total number of items.
  347. // Valid $str_display_type values are popular, recent, albums and descendants.
  348. // $user_id can be set to "0" to search site wide.
  349. // For "descendants", $user_id should be the album id #.
  350. // If $str_display_type is albums, then we only want albums.
  351. // If it's not, then we want everything except albums.
  352. if ($str_display_type == "albums") {
  353. // This is only used for user profiles, so we always want
  354. // results from a specific user.
  355. $count = ORM::factory("item")
  356. ->viewable()
  357. ->where("type", "=", "album")
  358. ->where("owner_id", "=", $user_id)
  359. ->count_all();
  360. } else {
  361. // If $user_id is not 0 we only want results from a specific user,
  362. // Or else we want results from any user.
  363. if ($user_id == 0) {
  364. $count = ORM::factory("item")
  365. ->viewable()
  366. ->where("type", "!=", "album")
  367. ->count_all();
  368. } else {
  369. // If type is descendants, then user_id is actually an item id#.
  370. if ($str_display_type == "descendants") {
  371. $item = ORM::factory("item", $user_id);
  372. $count = $item
  373. ->viewable()
  374. ->where("type", "!=", "album")
  375. ->order_by("created", "DESC")
  376. ->descendants_count();
  377. } else {
  378. $count = ORM::factory("item")
  379. ->viewable()
  380. ->where("type", "!=", "album")
  381. ->where("owner_id", "=", $user_id)
  382. ->count_all();
  383. }
  384. }
  385. }
  386. return $count;
  387. }
  388. static function items($str_display_type, $user_id, $limit=null, $offset=null) {
  389. // Query the database for a list of items to display in the virtual album.
  390. // Valid $str_display_type values are popular, recent, albums and descendants.
  391. // $user_id can be set to "0" to search site wide.
  392. // For "descendants", $user_id should be the album id #.
  393. // Figure out search parameters based on $str_display_type.
  394. $str_where = array();
  395. $str_orderby_field = "";
  396. if ($str_display_type == "recent") {
  397. $str_where = array(array("type", "!=", "album"));
  398. $str_orderby_field = "created";
  399. } elseif ($str_display_type == "albums") {
  400. $str_where = array(array("type", "=", "album"));
  401. $str_orderby_field = "created";
  402. } else {
  403. $str_where = array(array("type", "!=", "album"));
  404. $str_orderby_field = "view_count";
  405. }
  406. // Search the database for matching items.
  407. // Searching for descendants of a parent album is significantly
  408. // different from the other query types, so we're doing this one
  409. // seperately.
  410. if ($str_display_type == "descendants") {
  411. $item = ORM::factory("item", $user_id);
  412. return $item
  413. ->viewable()
  414. ->where("type", "!=", "album")
  415. ->order_by("created", "DESC")
  416. ->descendants($limit, $offset);
  417. }
  418. // If $user_id is greater then 0, limit results
  419. // to a specific user.
  420. if ($user_id == 0) {
  421. return ORM::factory("item")
  422. ->viewable()
  423. ->merge_where($str_where)
  424. ->order_by($str_orderby_field, "DESC")
  425. ->find_all($limit, $offset);
  426. } else {
  427. return ORM::factory("item")
  428. ->viewable()
  429. ->merge_where($str_where)
  430. ->where("owner_id", "=", $user_id)
  431. ->order_by($str_orderby_field, "DESC")
  432. ->find_all($limit, $offset);
  433. }
  434. }
  435. private function _get_position($item, $str_display_type, $user_id) {
  436. // Figure out the item's position within the virtual album.
  437. // Valid $str_display_type values are popular, recent, albums and descendants.
  438. // $user_id can be set to "0" to search site wide.
  439. // For "descendants", $user_id should be the album id #.
  440. // Figure out search conditions.
  441. $str_where = array();
  442. $str_orderby_field = "";
  443. if ($str_display_type == "recent") {
  444. $str_where = array(array("type", "!=", "album"));
  445. $str_orderby_field = "created";
  446. } elseif ($str_display_type == "albums") {
  447. $str_where = array(array("type", "=", "album"));
  448. $str_orderby_field = "created";
  449. } else {
  450. $str_where = array(array("type", "!=", "album"));
  451. $str_orderby_field = "view_count";
  452. }
  453. // Count the number of records that have a higher orderby_field value then
  454. // the item we're looking for.
  455. $position = 0;
  456. if ($user_id == 0) {
  457. $position = ORM::factory("item")
  458. ->viewable()
  459. ->merge_where($str_where)
  460. ->where($str_orderby_field, ">", $item->$str_orderby_field)
  461. ->order_by($str_orderby_field, "DESC")
  462. ->count_all();
  463. } else {
  464. if ($str_display_type == "descendants") {
  465. $album_item = ORM::factory("item", $user_id);
  466. $position = $album_item
  467. ->viewable()
  468. ->where("type", "!=", "album")
  469. ->where("created", ">", $item->created)
  470. ->order_by("created", "DESC")
  471. ->descendants_count();
  472. } else {
  473. $position = ORM::factory("item")
  474. ->viewable()
  475. ->where("owner_id", "=", $user_id)
  476. ->merge_where($str_where)
  477. ->where($str_orderby_field, ">", $item->$str_orderby_field)
  478. ->order_by($str_orderby_field, "DESC")
  479. ->count_all();
  480. }
  481. }
  482. // Set up a db query for all records with the same orderby field value
  483. // as the item we're looking for.
  484. $items = ORM::factory("item");
  485. if ($user_id == 0) {
  486. $items->viewable()
  487. ->merge_where($str_where)
  488. ->where($str_orderby_field, "=", $item->$str_orderby_field)
  489. ->order_by($str_orderby_field, "DESC");
  490. } else {
  491. if ($str_display_type == "descendants") {
  492. $item_album = ORM::factory("item", $user_id);
  493. $items = $item_album
  494. ->viewable()
  495. ->where("type", "!=", "album")
  496. ->where("created", "=", $item->created)
  497. ->order_by("created", "DESC");
  498. } else {
  499. $items->viewable()
  500. ->where("owner_id", "=", $user_id)
  501. ->merge_where($str_where)
  502. ->where($str_orderby_field, "=", $item->$str_orderby_field)
  503. ->order_by($str_orderby_field, "DESC");
  504. }
  505. }
  506. // Loop through each remaining match, increasing position by 1 each time
  507. // until we find a match.
  508. if ($str_display_type == "descendants") {
  509. foreach ($items->descendants() as $row) {
  510. $position++;
  511. if ($row->id == $item->id) {
  512. break;
  513. }
  514. }
  515. } else {
  516. foreach ($items->find_all() as $row) {
  517. $position++;
  518. if ($row->id == $item->id) {
  519. break;
  520. }
  521. }
  522. }
  523. // Return the result.
  524. return $position;
  525. }
  526. }