Guest User

Untitled

a guest
Jun 18th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.88 KB | None | 0 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-2011 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_Core {
  21. /**
  22. * Associate a tag with an item. Create the tag if it doesn't already exist.
  23. *
  24. * @todo Write test.
  25. *
  26. * @param Item_Model $item an item
  27. * @param string $tag_name a tag name
  28. * @return Tag_Model
  29. * @throws Exception("@todo {$tag_name} WAS_NOT_ADDED_TO {$item->id}")
  30. */
  31. static function add($item, $tag_name) {
  32. if (empty($tag_name)) {
  33. throw new exception("@todo MISSING_TAG_NAME");
  34. }
  35.  
  36. $tag = ORM::factory("tag")->where("name", "=", $tag_name)->find();
  37. if (!$tag->loaded()) {
  38. $tag->name = $tag_name;
  39. }
  40.  
  41. $tag->add($item);
  42. return $tag->save();
  43. }
  44.  
  45. /**
  46. * Return the N most popular tags.
  47. *
  48. * @return ORM_Iterator of Tag_Model in descending tag count order
  49. */
  50. static function popular_tags($count) {
  51. $count = max($count, 1);
  52. return ORM::factory("tag")
  53. ->order_by("count", "DESC")
  54. ->limit($count)
  55. ->find_all();
  56. }
  57.  
  58. /**
  59. * Return the N most popular viewable tags.
  60. *
  61. * @return ORM_Iterator of Tag_Model in descending tag count order
  62. */
  63. static function popular_viewable_tags($count) {
  64. // Using chainable item::viewable helper method.
  65. return item::viewable (
  66. ORM::factory("tag")
  67. ->order_by("count", "DESC")
  68. ->limit($count)
  69. ->join("items_tags", "tags.id", "items_tags.tag_id", "inner")
  70. ->join("items", "items.id", "items_tags.item_id", "inner")
  71. )
  72. ->group_by ("tags.id")
  73. ->find_all();
  74. }
  75.  
  76. /**
  77. * Return a rendering of the cloud for the N most popular tags.
  78. *
  79. * @param integer $count the number of tags
  80. * @return View
  81. */
  82. static function cloud($count) {
  83. $tags = tag::popular_viewable_tags($count)->as_array();
  84. if ($tags) {
  85. $cloud = new View("tag_cloud.html");
  86. $cloud->max_count = $tags[0]->count;
  87. if (!$cloud->max_count) {
  88. return;
  89. }
  90. usort($tags, array("tag", "sort_by_name"));
  91. $cloud->tags = $tags;
  92. return $cloud;
  93. }
  94. }
  95.  
  96. static function sort_by_name($tag1, $tag2) {
  97. return strcasecmp($tag1->name, $tag2->name);
  98. }
  99.  
  100. /**
  101. * Return all the tags for a given item.
  102. * @return array
  103. */
  104. static function item_tags($item) {
  105. return ORM::factory("tag")
  106. ->join("items_tags", "tags.id", "items_tags.tag_id", "left")
  107. ->where("items_tags.item_id", "=", $item->id)
  108. ->find_all();
  109. }
  110.  
  111. static function get_add_form($item) {
  112. $form = new Forge("tags/create/{$item->id}", "", "post", array("id" => "g-add-tag-form", "class" => "g-short-form"));
  113. $label = $item->is_album() ?
  114. t("Add tag to album") :
  115. ($item->is_photo() ? t("Add tag to photo") : t("Add tag to movie"));
  116.  
  117. $group = $form->group("add_tag")->label("Add Tag");
  118. $group->input("name")->label($label)->rules("required")->id("name");
  119. $group->hidden("item_id")->value($item->id);
  120. $group->submit("")->value(t("Add Tag"));
  121. return $form;
  122. }
  123.  
  124. static function get_delete_form($tag) {
  125. $form = new Forge("admin/tags/delete/$tag->id", "", "post", array("id" => "g-delete-tag-form"));
  126. $group = $form->group("delete_tag")
  127. ->label(t("Really delete tag %tag_name?", array("tag_name" => $tag->name)));
  128. $group->submit("")->value(t("Delete Tag"));
  129. return $form;
  130. }
  131.  
  132. /**
  133. * Delete all tags associated with an item
  134. */
  135. static function clear_all($item) {
  136. db::build()
  137. ->update("tags")
  138. ->set("count", db::expr("`count` - 1"))
  139. ->where("count", ">", 0)
  140. ->where("id", "IN", db::build()->select("tag_id")->from("items_tags")->where("item_id", "=", $item->id))
  141. ->execute();
  142. db::build()
  143. ->delete("items_tags")
  144. ->where("item_id", "=", $item->id)
  145. ->execute();
  146. }
  147.  
  148. /**
  149. * Get rid of any tags that have no associated items.
  150. */
  151. static function compact() {
  152. // @todo There's a potential race condition here which we can solve by adding a lock around
  153. // this and all the cases where we create/update tags. I'm loathe to do that since it's an
  154. // extremely rare case.
  155. db::build()->delete("tags")->where("count", "=", 0)->execute();
  156. }
  157. }
Add Comment
Please, Sign In to add comment