PageRenderTime 58ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/3.0/modules/tag_albums/controllers/tag_albums.php

https://github.com/TedBrew/gallery3-contrib
PHP | 888 lines | 678 code | 89 blank | 121 comment | 175 complexity | f183bc92e5490d5916830135c6dd7b37 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 tag_albums_Controller extends Controller {
  21. public function filter($id, $filter) {
  22. // Display the index page, but only show albums for
  23. // tags whose name begins with $filter.
  24. $this->index($id, $filter);
  25. }
  26. public function index($id, $filter) {
  27. // Load a page containing sub-albums for each tag in the gallery.
  28. // Check to see if the user has overridden default behavior, and act accordingly.
  29. if ((module::get_var("tag_albums", "tag_index_scope", "false")) || ($id == "")) {
  30. $tag_album_index_type = module::get_var("tag_albums", "tag_index", "default");
  31. if (($tag_album_index_type == "tagcloudpage") && (module::is_active("tag_cloud_page"))) {
  32. $redirect_url = "tag_cloud_page/";
  33. if ($id) {
  34. $redirect_url .= "?album={$id}";
  35. }
  36. url::redirect($redirect_url);
  37. return;
  38. } elseif (($tag_album_index_type == "alltags") && (module::is_active("all_tags"))) {
  39. $redirect_url = "all_tags/";
  40. if ($id) {
  41. $redirect_url .= "?album={$id}";
  42. }
  43. url::redirect($redirect_url);
  44. return;
  45. }
  46. }
  47. // If an ID was specified, make sure it's valid.
  48. $album_tags = ORM::factory("tags_album_id")
  49. ->where("id", "=", $id)
  50. ->find_all();
  51. if (count($album_tags) == 0) {
  52. $id = 0;
  53. }
  54. // Inherit permissions, title and description from the album that linked to this page,
  55. // if available, if not use the root album and some default values.
  56. $album = "";
  57. $page_title = module::get_var("tag_albums", "tag_page_title", "All Tags");
  58. $page_description = "";
  59. $str_page_url = "";
  60. if ($id == 0) {
  61. $album = ORM::factory("item", 1);
  62. access::required("view", $album);
  63. $str_page_url = "tag_albums/";
  64. } else {
  65. $album = ORM::factory("item", $album_tags[0]->album_id);
  66. access::required("view", $album);
  67. $page_title = $album->title;
  68. $page_description = $album->description;
  69. $str_page_url = "tag_albums/album/" . $id . "/" . urlencode($album->title);
  70. }
  71. // Figure out sort order from module preferences.
  72. $sort_page_field = module::get_var("tag_albums", "tag_sort_by", "name");
  73. $sort_page_direction = module::get_var("tag_albums", "tag_sort_direction", "ASC");
  74. // Figure out how many items to display on each page.
  75. $page_size = module::get_var("gallery", "page_size", 9);
  76. // If this page was reached from a breadcrumb, figure out what page to load from the show id.
  77. $show = Input::instance()->get("show");
  78. if ($show) {
  79. $child = ORM::factory("tag", $show);
  80. $comp = "";
  81. if (!strcasecmp($sort_page_direction, "DESC")) {
  82. $comp = ">";
  83. } else {
  84. $comp = "<";
  85. }
  86. $index = ORM::factory("tag")
  87. ->where($sort_page_field, $comp, $child->$sort_page_field)
  88. ->order_by("tags." . $sort_page_field, $sort_page_direction)
  89. ->count_all();
  90. $tag_model = ORM::factory("tag")
  91. ->where($sort_page_field, "=", $child->$sort_page_field)
  92. ->order_by("tags." . $sort_page_field, $sort_page_direction)
  93. ->find_all();
  94. foreach ($tag_model as $one_tag) {
  95. $index++;
  96. if ($one_tag->id == $show) {
  97. break;
  98. }
  99. }
  100. if ($index) {
  101. $page = ceil($index / $page_size);
  102. if ($page == 1) {
  103. url::redirect("$str_page_url");
  104. } else {
  105. url::redirect("$str_page_url?page=$page");
  106. }
  107. }
  108. }
  109. // Figure out which page # the visitor is on and
  110. // don't allow the visitor to go below page 1.
  111. $page = Input::instance()->get("page", 1);
  112. if ($page < 1) {
  113. url::redirect($str_page_url);
  114. }
  115. // First item to display.
  116. $offset = ($page - 1) * $page_size;
  117. // Determine the total number of items,
  118. // for page numbering purposes.
  119. $all_tags_count_model = ORM::factory("tag");
  120. if ($filter != "") {
  121. if ($filter == "NUM") {
  122. $all_tags_count_model->open();
  123. $all_tags_count_model->where("tags.name", "LIKE", "0%");
  124. $counter = 1;
  125. while ($counter < 10) {
  126. $all_tags_count_model->or_where("tags.name", "LIKE", ($counter++) . "%");
  127. }
  128. $all_tags_count_model->close();
  129. } else {
  130. $all_tags_count_model->where("tags.name", "LIKE", $filter . "%");
  131. }
  132. }
  133. $all_tags_count = $all_tags_count_model->count_all();
  134. // Figure out what the highest page number is.
  135. $max_pages = ceil($all_tags_count / $page_size);
  136. // Don't let the visitor go past the last page.
  137. if ($max_pages && $page > $max_pages) {
  138. url::redirect("$str_page_url?page=$max_pages");
  139. }
  140. // Figure out which items to display on this page.
  141. $display_tags_model = ORM::factory("tag");
  142. if ($filter != "") {
  143. if ($filter == "NUM") {
  144. $display_tags_model->open();
  145. $display_tags_model->where("tags.name", "LIKE", "0%");
  146. $counter = 1;
  147. while ($counter < 10) {
  148. $display_tags_model->or_where("tags.name", "LIKE", ($counter++) . "%");
  149. }
  150. $display_tags_model->close();
  151. } else {
  152. $display_tags_model->where("tags.name", "LIKE", $filter . "%");
  153. }
  154. }
  155. $display_tags_model->order_by("tags." . $sort_page_field, $sort_page_direction);
  156. $display_tags = $display_tags_model->find_all($page_size, $offset);
  157. // Set up the previous and next page buttons.
  158. if ($page > 1) {
  159. $previous_page = $page - 1;
  160. $view->previous_page_link = url::site($str_page_url . "?page={$previous_page}");
  161. }
  162. if ($page < $max_pages) {
  163. $next_page = $page + 1;
  164. $view->next_page_link = url::site($str_page_url . "?page={$next_page}");
  165. }
  166. // Generate an arry of "fake" items, one for each tag on the page.
  167. // Grab thumbnails from a admin-specified photo, or the most recently
  168. // uploaded item for each tag, if available.
  169. $children_array = Array();
  170. foreach ($display_tags as $one_tag) {
  171. $tag_thumb_url = "";
  172. $tag_thumb_width = "";
  173. $tag_thumb_height = "";
  174. // Check and see if the admin specified a photo to use for this tags thumbnail.
  175. $record = ORM::factory("tags_album_tag_cover")->where("tag_id", "=", $one_tag->id)->find();
  176. if ($record->loaded()) {
  177. $tag_thumb_item = ORM::factory("item", $record->photo_id);
  178. if ($tag_thumb_item->loaded()) {
  179. $tag_thumb_url = $tag_thumb_item->thumb_url();
  180. $tag_thumb_width = $tag_thumb_item->thumb_width;
  181. $tag_thumb_height = $tag_thumb_item->thumb_height;
  182. }
  183. }
  184. // If no pre-specified thumbnail was found, use the most recently uploaded photo (if available).
  185. if ($tag_thumb_url == "") {
  186. $tag_item = ORM::factory("item")
  187. ->viewable()
  188. ->join("items_tags", "items.id", "items_tags.item_id")
  189. ->where("items_tags.tag_id", "=", $one_tag->id)
  190. ->order_by("items.id", "DESC")
  191. ->find_all(1, 0);
  192. if (count($tag_item) > 0) {
  193. if ($tag_item[0]->has_thumb()) {
  194. $tag_thumb_url = $tag_item[0]->thumb_url();
  195. $tag_thumb_width = $tag_item[0]->thumb_width;
  196. $tag_thumb_height = $tag_item[0]->thumb_height;
  197. }
  198. }
  199. }
  200. // Create a new object to represent this virtual album, and add it to the array of objects for
  201. // this page.
  202. $child_tag = new Tag_Albums_Item($one_tag->name, url::site("tag_albums/tag/" . $one_tag->id . "/" . $id . "/" . urlencode($one_tag->name)), "album", 0);
  203. if ($tag_thumb_url != "") {
  204. $child_tag->set_thumb($tag_thumb_url, $tag_thumb_width, $tag_thumb_height);
  205. }
  206. $children_array[] = $child_tag;
  207. }
  208. $children = new Tag_Albums_Children($children_array);
  209. // Set up breadcrumbs.
  210. $tag_album_breadcrumbs = Array();
  211. if ($id > 0) {
  212. $counter = 0;
  213. $tag_album_breadcrumbs[] = Breadcrumb::instance($album->title, $album->url())->set_last();
  214. $parent_item = ORM::factory("item", $album->parent_id);
  215. while ($parent_item->id != 1) {
  216. $tag_album_breadcrumbs[] = Breadcrumb::instance($parent_item->title, $parent_item->url());
  217. $parent_item = ORM::factory("item", $parent_item->parent_id);
  218. }
  219. $tag_album_breadcrumbs[] = Breadcrumb::instance($parent_item->title, $parent_item->url())->set_first();
  220. $tag_album_breadcrumbs[1]->url .= "?show=" . $album->id;
  221. $tag_album_breadcrumbs = array_reverse($tag_album_breadcrumbs, true);
  222. } else {
  223. $tag_album_breadcrumbs[] = Breadcrumb::instance(item::root()->title, item::root()->url())->set_first();
  224. $tag_album_breadcrumbs[] = Breadcrumb::instance($page_title, $str_page_url)->set_last();
  225. }
  226. // Set up and display the actual page.
  227. $template = new Theme_View("page.html", "collection", "Tag Albums");
  228. $template->set_global(
  229. array("page" => $page,
  230. "max_pages" => $max_pages,
  231. "page_size" => $page_size,
  232. "children" => $children,
  233. "breadcrumbs" => $tag_album_breadcrumbs,
  234. "children_count" => $all_tags_count));
  235. $template->page_title = $page_title;
  236. $template->content = new View("dynamic.html");
  237. $template->content->title = $page_title;
  238. $template->content->description = $page_description;
  239. $template->content->filter_text = $this->_get_filter_html($id, $filter);
  240. print $template;
  241. }
  242. public function tag($id, $album_id) {
  243. // Display a dynamic album containing everything tagged with a specific tag where,
  244. // TAG is $id.
  245. // Optionally, set the breadcrumbs to make this page look like an album where the
  246. // album is $album_id.
  247. // Make sure $album_id is valid, clear it out if it isn't.
  248. $album_tags = ORM::factory("tags_album_id")
  249. ->where("id", "=", $album_id)
  250. ->find_all();
  251. if (count($album_tags) == 0) {
  252. $album_id = 0;
  253. }
  254. // Load the current tag.
  255. $display_tag = ORM::factory("tag", $id);
  256. // Figure out sort order from module preferences.
  257. $sort_page_field = module::get_var("tag_albums", "subalbum_sort_by", "title");
  258. $sort_page_direction = module::get_var("tag_albums", "subalbum_sort_direction", "ASC");
  259. // Figure out the URL to this page.
  260. $str_page_url = "tag_albums/tag/{$id}/{$album_id}/" . urlencode($display_tag->name);
  261. // Figure out how many items to display on each page.
  262. $page_size = module::get_var("gallery", "page_size", 9);
  263. // If this page was reached from a breadcrumb, figure out what page to load from the show id.
  264. $show = Input::instance()->get("show");
  265. if ($show) {
  266. $child = ORM::factory("item", $show);
  267. $index = $this->_get_position($child->$sort_page_field, $child->id, Array($id), "items." . $sort_page_field, $sort_page_direction, "OR", true);
  268. if ($index) {
  269. $page = ceil($index / $page_size);
  270. if ($page == 1) {
  271. url::redirect($str_page_url);
  272. } else {
  273. url::redirect($str_page_url . "?page=$page");
  274. }
  275. }
  276. }
  277. // Figure out which page # the visitor is on and
  278. // don't allow the visitor to go below page 1.
  279. $page = Input::instance()->get("page", 1);
  280. if ($page < 1) {
  281. url::redirect($str_page_url);
  282. }
  283. // First item to display.
  284. $offset = ($page - 1) * $page_size;
  285. // Determine the total number of items,
  286. // for page numbering purposes.
  287. $count = $this->_count_records(Array($id), "OR", true);
  288. // Figure out what the highest page number is.
  289. $max_pages = ceil($count / $page_size);
  290. // Don't let the visitor go past the last page.
  291. if ($max_pages && $page > $max_pages) {
  292. url::redirect($str_page_url . "/?page=$max_pages");
  293. }
  294. // Figure out which items to display on this page.
  295. $tag_children = $this->_get_records(Array($id), $page_size, $offset, "items." . $sort_page_field, $sort_page_direction, "OR", true);
  296. // Set up the previous and next page buttons.
  297. if ($page > 1) {
  298. $previous_page = $page - 1;
  299. $view->previous_page_link = url::site($str_page_url . "/?page={$previous_page}");
  300. }
  301. if ($page < $max_pages) {
  302. $next_page = $page + 1;
  303. $view->next_page_link = url::site($str_page_url . "/?page={$next_page}");
  304. }
  305. // Set up breadcrumbs for the page.
  306. $tag_album_breadcrumbs = Array();
  307. if ($album_id > 0) {
  308. $counter = 0;
  309. $tag_album_breadcrumbs[] = Breadcrumb::instance($display_tag->name, $str_page_url)->set_last();
  310. $parent_item = ORM::factory("item", $album_tags[0]->album_id);
  311. if ($album_tags[0]->tags != "*") {
  312. $parent_item = ORM::factory("item", $parent_item->parent_id);
  313. } else {
  314. $tag_album_breadcrumbs[] = Breadcrumb::instance($parent_item->name, url::site("tag_albums/album/" . $album_tags[0]->id . "/" . urlencode($parent_item->name)));
  315. $parent_item = ORM::factory("item", $parent_item->parent_id);
  316. }
  317. while ($parent_item->id != 1) {
  318. $tag_album_breadcrumbs[] = Breadcrumb::instance($parent_item->title, $parent_item->url());
  319. $parent_item = ORM::factory("item", $parent_item->parent_id);
  320. }
  321. $tag_album_breadcrumbs[] = Breadcrumb::instance($parent_item->title, $parent_item->url())->set_first();
  322. $parent_item = ORM::factory("item", $album_tags[0]->album_id);
  323. $tag_album_breadcrumbs[1]->url .= "?show=" . $id;
  324. $tag_album_breadcrumbs = array_reverse($tag_album_breadcrumbs, true);
  325. } else {
  326. $tag_album_breadcrumbs[] = Breadcrumb::instance(item::root()->title, item::root()->url())->set_first();
  327. if (module::get_var("tag_albums", "tag_index", "default") == "default") {
  328. $tag_album_breadcrumbs[] = Breadcrumb::instance(module::get_var("tag_albums", "tag_page_title", "All Tags"), url::site("tag_albums/") . "?show=" . $id);
  329. } else {
  330. $tag_album_breadcrumbs[] = Breadcrumb::instance(module::get_var("tag_albums", "tag_page_title", "All Tags"), url::site("tag_albums/"));
  331. }
  332. $tag_album_breadcrumbs[] = Breadcrumb::instance($display_tag->name, $str_page_url)->set_last();
  333. }
  334. // Set up and display the actual page.
  335. $template = new Theme_View("page.html", "collection", "Tag Albums");
  336. $template->set_global(
  337. array("page" => $page,
  338. "max_pages" => $max_pages,
  339. "page_size" => $page_size,
  340. "children" => $tag_children,
  341. "breadcrumbs" => $tag_album_breadcrumbs,
  342. "children_count" => $count));
  343. $template->page_title = $display_tag->name;
  344. $template->content = new View("dynamic.html");
  345. $template->content->title = $display_tag->name;
  346. $template->content->description = $page_description;
  347. $template->set_global("all_siblings", $this->_get_records(Array($id), $count, 0, "items." . $sort_page_field, $sort_page_direction, "OR", false));
  348. print $template;
  349. // Set breadcrumbs on the photo pages to point back to the calendar day view.
  350. item::set_display_context_callback("tag_albums_Controller::get_display_context", $id, $album_id);
  351. }
  352. public function album($id) {
  353. // Displays a dynamic page containing items that have been
  354. // tagged with one or more tags.
  355. // Load the specified ID to make sure it exists.
  356. $album_tags = ORM::factory("tags_album_id")
  357. ->where("id", "=", $id)
  358. ->find_all();
  359. // If it doesn't exist, redirect to the modules root page.
  360. if (count($album_tags) == 0) {
  361. url::redirect("tag_albums/");
  362. }
  363. // If it does exist, and is set to *, load a list of all tags.
  364. if ($album_tags[0]->tags == "*") {
  365. $this->index($id, "");
  366. } else {
  367. // Otherwise, populate this page with the specified items.
  368. // Inherit permissions, title and description from the album that linked to this page.
  369. $album = ORM::factory("item", $album_tags[0]->album_id);
  370. access::required("view", $album);
  371. $page_title = $album->title;
  372. $page_description = $album->description;
  373. // URL to this page
  374. $str_page_url = "tag_albums/album/" . $id . "/" . urlencode($album->name);
  375. // Determine page sort order.
  376. $sort_page_field = $album->sort_column;
  377. $sort_page_direction = $album->sort_order;
  378. // Determine search type (AND/OR) and generate an array of the tag ids.
  379. $tag_ids = Array();
  380. foreach (explode(",", $album_tags[0]->tags) as $tag_name) {
  381. $tag = ORM::factory("tag")->where("name", "=", trim($tag_name))->find();
  382. if ($tag->loaded()) {
  383. $tag_ids[] = $tag->id;
  384. }
  385. }
  386. $album_tags_search_type = $album_tags[0]->search_type;
  387. // Figure out how many items to display on each page.
  388. $page_size = module::get_var("gallery", "page_size", 9);
  389. // If this page was reached from a breadcrumb, figure out what page to load from the show id.
  390. $show = Input::instance()->get("show");
  391. if ($show) {
  392. $child = ORM::factory("item", $show);
  393. $index = $this->_get_position($child->$sort_page_field, $child->id, $tag_ids, "items." . $sort_page_field, $sort_page_direction, $album_tags_search_type, true);
  394. if ($index) {
  395. $page = ceil($index / $page_size);
  396. if ($page == 1) {
  397. url::redirect($str_page_url);
  398. } else {
  399. url::redirect($str_page_url . "?page=$page");
  400. }
  401. }
  402. }
  403. // Figure out how many items are in this "virtual album"
  404. $count = $this->_count_records($tag_ids, $album_tags_search_type, true);
  405. // Figure out which page # the visitor is on and
  406. // don't allow the visitor to go below page 1.
  407. $page = Input::instance()->get("page", 1);
  408. if ($page < 1) {
  409. url::redirect($str_page_url);
  410. }
  411. // First item to display.
  412. $offset = ($page - 1) * $page_size;
  413. // Figure out what the highest page number is.
  414. $max_pages = ceil($count / $page_size);
  415. // Don't let the visitor go past the last page.
  416. if ($max_pages && $page > $max_pages) {
  417. url::redirect($str_page_url . "/?page=$max_pages");
  418. }
  419. // Figure out which items to display on this page and store their details in $children.
  420. $tag_children = $this->_get_records($tag_ids, $page_size, $offset, "items." . $sort_page_field, $sort_page_direction, $album_tags_search_type, true);
  421. // Set up the previous and next page buttons.
  422. if ($page > 1) {
  423. $previous_page = $page - 1;
  424. $view->previous_page_link = url::site($str_page_url . "/?page={$previous_page}");
  425. }
  426. if ($page < $max_pages) {
  427. $next_page = $page + 1;
  428. $view->next_page_link = url::site($str_page_url . "/?page={$next_page}");
  429. }
  430. // Set up breadcrumbs.
  431. $tag_album_breadcrumbs = array();
  432. $counter = 0;
  433. $tag_album_breadcrumbs[] = Breadcrumb::instance($album->title, $album->url())->set_last();
  434. $parent_item = ORM::factory("item", $album->parent_id);
  435. while ($parent_item->id != 1) {
  436. $tag_album_breadcrumbs[] = Breadcrumb::instance($parent_item->title, $parent_item->url());
  437. $parent_item = ORM::factory("item", $parent_item->parent_id);
  438. }
  439. $tag_album_breadcrumbs[] = Breadcrumb::instance($parent_item->title, $parent_item->url())->set_first();
  440. $tag_album_breadcrumbs[1]->url .= "?show=" . $album->id;
  441. $tag_album_breadcrumbs = array_reverse($tag_album_breadcrumbs, true);
  442. // Set up and display the actual page.
  443. $template = new Theme_View("page.html", "collection", "Tag Albums");
  444. $template->set_global(
  445. array("page" => $page,
  446. "max_pages" => $max_pages,
  447. "page_size" => $page_size,
  448. "children" => $tag_children,
  449. "breadcrumbs" => $tag_album_breadcrumbs,
  450. "children_count" => $count));
  451. $template->page_title = $page_title;
  452. $template->content = new View("dynamic.html");
  453. $template->content->title = $page_title;
  454. $template->content->description = $page_description;
  455. $template->set_global("all_siblings", $this->_get_records($tag_ids, $count, 0, "items." . $sort_page_field, $sort_page_direction, $album_tags_search_type, false));
  456. print $template;
  457. // Set breadcrumbs on the photo pages to point back to the calendar day view.
  458. item::set_display_context_callback("tag_albums_Controller::get_display_context", 0, $id);
  459. }
  460. }
  461. public function show($item_id, $tag_id, $album_id) {
  462. // This function used to be responsible for displaying photos.
  463. // As of Gallery 3.0.3, it is no longer needed, now it just
  464. // redirects to the photo's primary URL to avoid breaking older links.
  465. item::set_display_context_callback("tag_albums_Controller::get_display_context", $tag_id, $album_id);
  466. $item = ORM::factory("item", $item_id);
  467. url::redirect(url::abs_site("{$item->type}s/{$item->id}"));
  468. }
  469. public function make_tag_album_cover($id, $tag_id, $album_id) {
  470. if (!identity::active_user()->admin) {
  471. message::error(t("You do not have sufficient privileges to do this"));
  472. url::redirect("tag_albums/show/" . $id . "/" . $tag_id . "/" . $album_id . "/" . urlencode($item->name));
  473. }
  474. $item = ORM::factory("item", $id);
  475. if (($album_id > 0) && ($tag_id == 0)) {
  476. // If we are dealing with a dynamic album, set it's thumbnail to this pics.
  477. // Based on modules/gallery/helpers/item.php
  478. $album_tags = ORM::factory("tags_album_id")
  479. ->where("id", "=", $album_id)
  480. ->find_all();
  481. if (count($album_tags) > 0) {
  482. $parent = ORM::factory("item", $album_tags[0]->album_id);
  483. $parent->album_cover_item_id = $item->id;
  484. $parent->thumb_dirty = 1;
  485. graphics::generate($parent);
  486. $parent->save();
  487. $grand_parent = $parent->parent();
  488. if ($grand_parent && access::can("edit", $grand_parent) &&
  489. $grand_parent->album_cover_item_id == null) {
  490. item::make_album_cover($parent);
  491. }
  492. }
  493. message::success(t("Made " . $item->title . " this album's cover"));
  494. url::redirect("tag_albums/show/" . $id . "/" . $tag_id . "/" . $album_id . "/" . urlencode($item->name));
  495. } else {
  496. // If setting a thumbnail for an auto-generated all tags->tag album.
  497. $record = ORM::factory("tags_album_tag_cover")->where("tag_id", "=", $tag_id)->find();
  498. if (!$record->loaded()) {
  499. $record->tag_id = $tag_id;
  500. }
  501. $record->photo_id = $id;
  502. $record->save();
  503. message::success(t("Made " . $item->title . " this album's cover"));
  504. url::redirect("tag_albums/show/" . $id . "/" . $tag_id . "/" . $album_id . "/" . urlencode($item->name));
  505. }
  506. }
  507. static function get_display_context($item, $tag_id, $album_id) {
  508. // Make sure #album_id is valid, clear it out if it isn't.
  509. // Note: $dynamic_siblings is used exclusively for Grey Dragon.
  510. $album_tags = ORM::factory("tags_album_id")
  511. ->where("id", "=", $album_id)
  512. ->find_all();
  513. if (count($album_tags) == 0) {
  514. $album_id = 0;
  515. }
  516. // Load the tag and item, make sure the user has access to the item.
  517. $display_tag = ORM::factory("tag", $tag_id);
  518. // Figure out sort order from module preferences.
  519. $sort_page_field = "";
  520. $sort_page_direction = "";
  521. if (($tag_id > 0) || (count($album_tags) == 0)) {
  522. $sort_page_field = module::get_var("tag_albums", "subalbum_sort_by", "title");
  523. $sort_page_direction = module::get_var("tag_albums", "subalbum_sort_direction", "ASC");
  524. } else {
  525. $parent_album = ORM::factory("item", $album_tags[0]->album_id);
  526. $sort_page_field = $parent_album->sort_column;
  527. $sort_page_direction = $parent_album->sort_order;
  528. }
  529. // Load the number of items in the parent album, and determine previous and next items.
  530. $sibling_count = "";
  531. $tag_children = "";
  532. $previous_item = "";
  533. $next_item = "";
  534. $position = 0;
  535. $dynamic_siblings = "";
  536. if ($tag_id > 0) {
  537. $sibling_count = tag_albums_Controller::_count_records(Array($tag_id), "OR", false);
  538. $position = tag_albums_Controller::_get_position($item->$sort_page_field, $item->id, Array($tag_id), "items." . $sort_page_field, $sort_page_direction, $album_tags_search_type, false);
  539. if ($position > 1) {
  540. $previous_item_object = tag_albums_Controller::_get_records(Array($tag_id), 1, $position-2, "items." . $sort_page_field, $sort_page_direction, $album_tags_search_type, false);
  541. if (count($previous_item_object) > 0) {
  542. $previous_item = $previous_item_object[0];
  543. }
  544. }
  545. $next_item_object = tag_albums_Controller::_get_records(Array($tag_id), 1, $position, "items." . $sort_page_field, $sort_page_direction, $album_tags_search_type, false);
  546. if (count($next_item_object) > 0) {
  547. $next_item = $next_item_object[0];
  548. }
  549. $dynamic_siblings = tag_albums_Controller::_get_records(Array($tag_id), $sibling_count, 0, "items." . $sort_page_field, $sort_page_direction, $album_tags_search_type, false);
  550. } else {
  551. $tag_ids = Array();
  552. foreach (explode(",", $album_tags[0]->tags) as $tag_name) {
  553. $tag = ORM::factory("tag")->where("name", "=", trim($tag_name))->find();
  554. if ($tag->loaded()) {
  555. $tag_ids[] = $tag->id;
  556. }
  557. }
  558. $album_tags_search_type = $album_tags[0]->search_type;
  559. $sibling_count = tag_albums_Controller::_count_records($tag_ids, $album_tags_search_type, false);
  560. $position = tag_albums_Controller::_get_position($item->$sort_page_field, $item->id, $tag_ids, "items." . $sort_page_field, $sort_page_direction, $album_tags_search_type, false);
  561. if ($position > 1) {
  562. $previous_item_object = tag_albums_Controller::_get_records($tag_ids, 1, $position-2, "items." . $sort_page_field, $sort_page_direction, $album_tags_search_type, false);
  563. if (count($previous_item_object) > 0) {
  564. $previous_item = $previous_item_object[0];
  565. }
  566. }
  567. $next_item_object = tag_albums_Controller::_get_records($tag_ids, 1, $position, "items." . $sort_page_field, $sort_page_direction, $album_tags_search_type, false);
  568. if (count($next_item_object) > 0) {
  569. $next_item = $next_item_object[0];
  570. }
  571. $dynamic_siblings = tag_albums_Controller::_get_records($tag_ids, $sibling_count, 0, "items." . $sort_page_field, $sort_page_direction, $album_tags_search_type, false);
  572. }
  573. // Set up breadcrumbs
  574. $tag_album_breadcrumbs = Array();
  575. if ($album_id > 0) {
  576. $counter = 0;
  577. $tag_album_breadcrumbs[] = Breadcrumb::instance($item->title, $item->url())->set_last();
  578. if ($album_tags[0]->tags == "*") {
  579. $tag_album_breadcrumbs[] = Breadcrumb::instance($display_tag->name, url::site("tag_albums/tag/" . $display_tag->id . "/" . $album_id . "/" . urlencode($display_tag->name)));
  580. }
  581. $parent_item = ORM::factory("item", $album_tags[0]->album_id);
  582. $tag_album_breadcrumbs[] = Breadcrumb::instance($parent_item->title, url::site("tag_albums/album/" . $album_id . "/" . urlencode($parent_item->name)));
  583. $parent_item = ORM::factory("item", $parent_item->parent_id);
  584. while ($parent_item->id != 1) {
  585. $tag_album_breadcrumbs[] = Breadcrumb::instance($parent_item->title, $parent_item->url());
  586. $parent_item = ORM::factory("item", $parent_item->parent_id);
  587. }
  588. $tag_album_breadcrumbs[] = Breadcrumb::instance($parent_item->title, $parent_item->url())->set_first();
  589. $tag_album_breadcrumbs[1]->url .= "?show=" . $item->id;
  590. $tag_album_breadcrumbs = array_reverse($tag_album_breadcrumbs, true);
  591. } else {
  592. $tag_album_breadcrumbs[] = Breadcrumb::instance(item::root()->title, item::root()->url())->set_first();
  593. $tag_album_breadcrumbs[] = Breadcrumb::instance(module::get_var("tag_albums", "tag_page_title", "All Tags"), url::site("tag_albums/"));
  594. $tag_album_breadcrumbs[] = Breadcrumb::instance($display_tag->name, url::site("tag_albums/tag/" . $display_tag->id . "/" . urlencode($display_tag->name)) . "?show=" . $item->id);
  595. $tag_album_breadcrumbs[] = Breadcrumb::instance($item->title, $item->url())->set_last();
  596. }
  597. return array("position" => $position,
  598. "previous_item" => $previous_item,
  599. "next_item" => $next_item,
  600. "tag_id" => $tag_id,
  601. "album_id" => $album_id,
  602. "is_tagalbum_page" => true,
  603. "dynamic_siblings" => $dynamic_siblings,
  604. "sibling_count" => $sibling_count,
  605. "breadcrumbs" => $tag_album_breadcrumbs);
  606. }
  607. private function _get_position($item_title, $item_id, $tag_ids, $sort_field, $sort_direction, $search_type, $include_albums) {
  608. // Determine an item's position within a virtual album.
  609. // Convert ASC/DESC to < or > characters.
  610. if (!strcasecmp($sort_direction, "DESC")) {
  611. $comp = ">";
  612. } else {
  613. $comp = "<";
  614. }
  615. // Figure out how many items are _before the current item.
  616. $items_model = ORM::factory("item");
  617. if ($search_type == "AND") {
  618. $items_model->select('COUNT("*") AS result_count');
  619. } else {
  620. $items_model->select("items.id");
  621. }
  622. $items_model->viewable();
  623. $items_model->join("items_tags", "items.id", "items_tags.item_id");
  624. $items_model->open();
  625. $items_model->where("items_tags.tag_id", "=", $tag_ids[0]);
  626. $counter = 1;
  627. while ($counter < count($tag_ids)) {
  628. $items_model->or_where("items_tags.tag_id", "=", $tag_ids[$counter]);
  629. $counter++;
  630. }
  631. $items_model->close();
  632. if ($include_albums == false) {
  633. $items_model->and_where("items.type", "!=", "album");
  634. }
  635. $items_model->and_where($sort_field, $comp, $item_title);
  636. $items_model->order_by($sort_field, $sort_direction);
  637. $items_model->group_by("items.id");
  638. if ($search_type == "AND") {
  639. $items_model->having("result_count", "=", count($tag_ids));
  640. }
  641. $position = count($items_model->find_all());
  642. // In case multiple items have identical sort criteria, query for
  643. // everything with the same criteria, and increment the position
  644. // one at a time until we find the right item.
  645. $items_model = ORM::factory("item");
  646. if ($search_type == "AND") {
  647. $items_model->select("items.id");
  648. $items_model->select('COUNT("*") AS result_count');
  649. } else {
  650. $items_model->select("items.id");
  651. }
  652. $items_model->viewable();
  653. $items_model->join("items_tags", "items.id", "items_tags.item_id");
  654. $items_model->open();
  655. $items_model->where("items_tags.tag_id", "=", $tag_ids[0]);
  656. $counter = 1;
  657. while ($counter < count($tag_ids)) {
  658. $items_model->or_where("items_tags.tag_id", "=", $tag_ids[$counter]);
  659. $counter++;
  660. }
  661. $items_model->close();
  662. if ($include_albums == false) {
  663. $items_model->and_where("items.type", "!=", "album");
  664. }
  665. $items_model->and_where($sort_field, "=", $item_title);
  666. $items_model->order_by($sort_field, $sort_direction);
  667. $items_model->group_by("items.id");
  668. if ($search_type == "AND") {
  669. $items_model->having("result_count", "=", count($tag_ids));
  670. }
  671. $match_items = $items_model->find_all();
  672. foreach ($match_items as $one_item) {
  673. $position++;
  674. if ($one_item->id == $item_id) {
  675. break;
  676. }
  677. }
  678. return ($position);
  679. }
  680. private function _get_records($tag_ids, $page_size, $offset, $sort_field, $sort_direction, $search_type, $include_albums) {
  681. // Returns an array of items to be displayed on the current page.
  682. $items_model = ORM::factory("item");
  683. if ($search_type == "AND") {
  684. // For some reason, if I do 'select("*")' the item ids all have values that are 1000+
  685. // higher then they should be. So instead, I'm manually selecting each column that I need.
  686. $items_model->select("items.id");
  687. $items_model->select("items.name");
  688. $items_model->select("items.title");
  689. $items_model->select("items.view_count");
  690. $items_model->select("items.owner_id");
  691. $items_model->select("items.rand_key");
  692. $items_model->select("items.type");
  693. $items_model->select("items.thumb_width");
  694. $items_model->select("items.thumb_height");
  695. $items_model->select("items.left_ptr");
  696. $items_model->select("items.right_ptr");
  697. $items_model->select("items.relative_path_cache");
  698. $items_model->select("items.relative_url_cache");
  699. $items_model->select('COUNT("*") AS result_count');
  700. }
  701. $items_model->viewable();
  702. $items_model->join("items_tags", "items.id", "items_tags.item_id");
  703. $items_model->open();
  704. $items_model->where("items_tags.tag_id", "=", $tag_ids[0]);
  705. $counter = 1;
  706. while ($counter < count($tag_ids)) {
  707. $items_model->or_where("items_tags.tag_id", "=", $tag_ids[$counter]);
  708. $counter++;
  709. }
  710. $items_model->close();
  711. if ($include_albums == false) {
  712. $items_model->and_where("items.type", "!=", "album");
  713. }
  714. $items_model->order_by($sort_field, $sort_direction);
  715. $items_model->group_by("items.id");
  716. if ($search_type == "AND") {
  717. $items_model->having("result_count", "=", count($tag_ids));
  718. }
  719. return $items_model->find_all($page_size, $offset);
  720. }
  721. private function _count_records($tag_ids, $search_type, $include_albums) {
  722. // Count the number of viewable items for the designated tag(s)
  723. // and return that number.
  724. if (count($tag_ids) == 0) {
  725. // If no tags were specified, return 0.
  726. return 0;
  727. } elseif (count($tag_ids) == 1) {
  728. // if one tag was specified, we can use count_all to get the number.
  729. $count = ORM::factory("item")
  730. ->viewable()
  731. ->join("items_tags", "items.id", "items_tags.item_id")
  732. ->where("items_tags.tag_id", "=", $tag_ids[0]);
  733. if ($include_albums == false) {
  734. $count->and_where("items.type", "!=", "album");
  735. }
  736. return $count->count_all();
  737. } else {
  738. // If multiple tags were specified, count_all won't work,
  739. // so we'll have to do count(find_all) instead.
  740. $items_model = ORM::factory("item");
  741. if ($search_type == "AND") {
  742. $items_model->select('COUNT("*") AS result_count');
  743. } else {
  744. $items_model->select('items.id');
  745. }
  746. $items_model->viewable();
  747. $items_model->join("items_tags", "items.id", "items_tags.item_id");
  748. $items_model->where("items_tags.tag_id", "=", $tag_ids[0]);
  749. $counter = 1;
  750. while ($counter < count($tag_ids)) {
  751. $items_model->or_where("items_tags.tag_id", "=", $tag_ids[$counter]);
  752. $counter++;
  753. }
  754. if ($include_albums == false) {
  755. $items_model->and_where("items.type", "!=", "album");
  756. }
  757. $items_model->group_by("items.id");
  758. if ($search_type == "AND") {
  759. $items_model->having("result_count", "=", count($tag_ids));
  760. }
  761. return count($items_model->find_all());
  762. }
  763. }
  764. private function _get_filter_html($album_id, $str_filter) {
  765. // Generate HTML to display filter links on the index page.
  766. // Make sure $album_id is set.
  767. if ($album_id == "") {
  768. $album_id = 0;
  769. }
  770. // Generate the links.
  771. $str_html = "Filter: ";
  772. if ($str_filter != "") {
  773. if ($album_id > 0) {
  774. $album_tags = ORM::factory("tags_album_id")
  775. ->where("id", "=", $album_id)
  776. ->find_all();
  777. $album = ORM::factory("item", $album_tags[0]->album_id);
  778. $str_html .= "<a href=\"" . url::site("tag_albums/album/" . $album_id . "/" . urlencode($album->name)) . "\">(All)</a> ";
  779. } else {
  780. $str_html .= "<a href=\"" . url::site("tag_albums/") . "\">(All)</a> ";
  781. }
  782. }
  783. if ($str_filter == "NUM") {
  784. $str_html .= "# ";
  785. } else {
  786. $str_html .= "<a href=\"" . url::site("tag_albums/filter/" . $album_id . "/NUM") . "\">#</a> ";
  787. }
  788. foreach(range('A','Z') as $letter) {
  789. if ($letter == $str_filter) {
  790. $str_html .= $letter . " ";
  791. } else {
  792. $str_html .= "<a href=\"" . url::site("tag_albums/filter/" . $album_id . "/" . $letter) . "\">";
  793. $str_html .= $letter . "</a> ";
  794. }
  795. }
  796. // Return the HTML.
  797. return $str_html;
  798. }
  799. }