/modules/tag/models/tag.php

https://github.com/zcopley/gallery3 · PHP · 141 lines · 82 code · 14 blank · 45 comment · 8 complexity · ce149ff354ac1cee5c213cd644e49a33 MD5 · raw file

  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-2010 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_Model_Core extends ORM {
  21. protected $has_and_belongs_to_many = array("items");
  22. public function __construct($id=null) {
  23. parent::__construct($id);
  24. if (!$this->loaded()) {
  25. // Set reasonable defaults
  26. $this->count = 0;
  27. }
  28. }
  29. /**
  30. * Return all viewable items associated with this tag.
  31. * @param integer $limit number of rows to limit result to
  32. * @param integer $offset offset in result to start returning rows from
  33. * @param string $type the type of item (album, photo)
  34. * @return ORM_Iterator
  35. */
  36. public function items($limit=null, $offset=null, $type=null) {
  37. $model = ORM::factory("item")
  38. ->viewable()
  39. ->join("items_tags", "items.id", "items_tags.item_id")
  40. ->where("items_tags.tag_id", "=", $this->id);
  41. if ($type) {
  42. $model->where("items.type", "=", $type);
  43. }
  44. return $model->find_all($limit, $offset);
  45. }
  46. /**
  47. * Return the count of all viewable items associated with this tag.
  48. * @param string $type the type of item (album, photo)
  49. * @return integer
  50. */
  51. public function items_count($type=null) {
  52. $model = ORM::factory("item")
  53. ->viewable()
  54. ->join("items_tags", "items.id", "items_tags.item_id")
  55. ->where("items_tags.tag_id", "=", $this->id);
  56. if ($type) {
  57. $model->where("items.type", "=", $type);
  58. }
  59. return $model->count_all();
  60. }
  61. /**
  62. * Overload ORM::save() to trigger an item_related_update event for all items that are related
  63. * to this tag.
  64. */
  65. public function save() {
  66. $related_item_ids = array();
  67. foreach (db::build()
  68. ->select("item_id")
  69. ->from("items_tags")
  70. ->where("tag_id", "=", $this->id)
  71. ->execute() as $row) {
  72. $related_item_ids[$row->item_id] = 1;
  73. }
  74. if (isset($this->object_relations["items"])) {
  75. $added = array_diff($this->changed_relations["items"], $this->object_relations["items"]);
  76. $removed = array_diff($this->object_relations["items"], $this->changed_relations["items"]);
  77. if (isset($this->changed_relations["items"])) {
  78. $changed = array_merge($added, $removed);
  79. }
  80. $this->count = count($this->object_relations["items"]) + count($added) - count($removed);
  81. }
  82. $result = parent::save();
  83. if (!empty($changed)) {
  84. foreach (ORM::factory("item")->where("id", "IN", $changed)->find_all() as $item) {
  85. module::event("item_related_update", $item);
  86. }
  87. }
  88. return $result;
  89. }
  90. /**
  91. * Overload ORM::delete() to trigger an item_related_update event for all items that are
  92. * related to this tag, and delete all items_tags relationships.
  93. */
  94. public function delete($ignored_id=null) {
  95. $related_item_ids = array();
  96. foreach (db::build()
  97. ->select("item_id")
  98. ->from("items_tags")
  99. ->where("tag_id", "=", $this->id)
  100. ->execute() as $row) {
  101. $related_item_ids[$row->item_id] = 1;
  102. }
  103. db::build()->delete("items_tags")->where("tag_id", "=", $this->id)->execute();
  104. $result = parent::delete();
  105. if ($related_item_ids) {
  106. foreach (ORM::factory("item")
  107. ->where("id", "IN", array_keys($related_item_ids))
  108. ->find_all() as $item) {
  109. module::event("item_related_update", $item);
  110. }
  111. }
  112. return $result;
  113. }
  114. /**
  115. * Return the server-relative url to this item, eg:
  116. * /gallery3/index.php/tags/35
  117. *
  118. * @param string $query the query string (eg "page=3")
  119. */
  120. public function url($query=null) {
  121. $url = url::site("tag/{$this->name}");
  122. if ($query) {
  123. $url .= "?$query";
  124. }
  125. return $url;
  126. }
  127. }