/modules/tag/models/tag.php

https://github.com/ffchung/gallery3 · PHP · 155 lines · 91 code · 14 blank · 50 comment · 9 complexity · 2f1acdbefbf769117da88eede1b16110 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-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_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 $where an array of arrays, each compatible with ORM::where()
  34. * @return ORM_Iterator
  35. */
  36. public function items($limit=null, $offset=null, $where=array()) {
  37. if (is_scalar($where)) {
  38. // backwards compatibility
  39. $where = array(array("items.type", "=", $where));
  40. }
  41. return ORM::factory("item")
  42. ->viewable()
  43. ->join("items_tags", "items.id", "items_tags.item_id")
  44. ->where("items_tags.tag_id", "=", $this->id)
  45. ->merge_where($where)
  46. ->order_by("items.id")
  47. ->find_all($limit, $offset);
  48. }
  49. /**
  50. * Return the count of all viewable items associated with this tag.
  51. * @param string $where an array of arrays, each compatible with ORM::where()
  52. * @return integer
  53. */
  54. public function items_count($where=array()) {
  55. if (is_scalar($where)) {
  56. // backwards compatibility
  57. $where = array(array("items.type", "=", $where));
  58. }
  59. return $model = ORM::factory("item")
  60. ->viewable()
  61. ->join("items_tags", "items.id", "items_tags.item_id")
  62. ->where("items_tags.tag_id", "=", $this->id)
  63. ->merge_where($where)
  64. ->count_all();
  65. }
  66. /**
  67. * Overload ORM::save() to trigger an item_related_update event for all items that are related
  68. * to this tag.
  69. */
  70. public function save() {
  71. // Check to see if another tag exists with the same name
  72. $duplicate_tag = ORM::factory("tag")
  73. ->where("name", "=", $this->name)
  74. ->where("id", "!=", $this->id)
  75. ->find();
  76. if ($duplicate_tag->loaded()) {
  77. // If so, tag its items with this tag so as to merge it
  78. $duplicate_tag_items = ORM::factory("item")
  79. ->join("items_tags", "items.id", "items_tags.item_id")
  80. ->where("items_tags.tag_id", "=", $duplicate_tag->id)
  81. ->find_all();
  82. foreach ($duplicate_tag_items as $item) {
  83. $this->add($item);
  84. }
  85. // ... and remove the duplicate tag
  86. $duplicate_tag->delete();
  87. }
  88. if (isset($this->object_relations["items"])) {
  89. $added = array_diff($this->changed_relations["items"], $this->object_relations["items"]);
  90. $removed = array_diff($this->object_relations["items"], $this->changed_relations["items"]);
  91. if (isset($this->changed_relations["items"])) {
  92. $changed = array_merge($added, $removed);
  93. }
  94. $this->count = count($this->object_relations["items"]) + count($added) - count($removed);
  95. }
  96. $result = parent::save();
  97. if (!empty($changed)) {
  98. foreach (ORM::factory("item")->where("id", "IN", $changed)->find_all() as $item) {
  99. module::event("item_related_update", $item);
  100. }
  101. }
  102. return $result;
  103. }
  104. /**
  105. * Overload ORM::delete() to trigger an item_related_update event for all items that are
  106. * related to this tag, and delete all items_tags relationships.
  107. */
  108. public function delete($ignored_id=null) {
  109. $related_item_ids = array();
  110. foreach (db::build()
  111. ->select("item_id")
  112. ->from("items_tags")
  113. ->where("tag_id", "=", $this->id)
  114. ->execute() as $row) {
  115. $related_item_ids[$row->item_id] = 1;
  116. }
  117. db::build()->delete("items_tags")->where("tag_id", "=", $this->id)->execute();
  118. $result = parent::delete();
  119. if ($related_item_ids) {
  120. foreach (ORM::factory("item")
  121. ->where("id", "IN", array_keys($related_item_ids))
  122. ->find_all() as $item) {
  123. module::event("item_related_update", $item);
  124. }
  125. }
  126. return $result;
  127. }
  128. /**
  129. * Return the server-relative url to this item, eg:
  130. * /gallery3/index.php/tags/35
  131. *
  132. * @param string $query the query string (eg "page=3")
  133. */
  134. public function url($query=null) {
  135. $url = url::site("tag/{$this->id}/" . urlencode($this->name));
  136. if ($query) {
  137. $url .= "?$query";
  138. }
  139. return $url;
  140. }
  141. }