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

/application/plugins/tags/models/Tags.class.php

https://github.com/cj/Project-Pier
PHP | 212 lines | 114 code | 26 blank | 72 comment | 21 complexity | ec4f36071005bf11ee36039506357b6a MD5 | raw file
  1. <?php
  2. /**
  3. * Tags
  4. *
  5. * @http://www.projectpier.org/
  6. */
  7. class Tags extends BaseTags {
  8. /**
  9. * Return tags for specific object
  10. *
  11. * @access public
  12. * @param ApplicationDataObject $object
  13. * @param string $manager_class
  14. * @return array
  15. */
  16. function getTagsByObject(ApplicationDataObject $object, $manager_class) {
  17. return self::findAll(array(
  18. 'conditions' => array('`rel_object_id` = ? AND `rel_object_manager` = ?', $object->getObjectId(), get_class($object->manager())),
  19. 'order' => '`tag`'
  20. )); // findAll
  21. } // getTagsByObject
  22. /**
  23. * Return tag names as array for specific object
  24. *
  25. * @access public
  26. * @param ApplicationDataObject $object
  27. * @param string $manager_class
  28. * @return array
  29. */
  30. function getTagNamesByObject(ApplicationDataObject $object, $manager_class) {
  31. $rows = DB::executeAll('SELECT `tag` FROM ' . self::instance()->getTableName(true) . ' WHERE `rel_object_id` = ? AND `rel_object_manager` = ? ORDER BY `tag`', $object->getId(), $manager_class);
  32. if (!is_array($rows)) {
  33. return null;
  34. }
  35. $tags = array();
  36. foreach ($rows as $row) {
  37. $tags[] = $row['tag'];
  38. }
  39. return $tags;
  40. } // getTagNamesByObject
  41. /**
  42. * Clear tags of specific object
  43. *
  44. * @access public
  45. * @param ApplicationDataObject $object
  46. * @param string $manager_class
  47. * @return boolean
  48. */
  49. function clearObjectTags(ApplicationDataObject $object, $manager_class) {
  50. $tags = $object->getTags(); // save the tags list
  51. if (is_array($tags)) {
  52. foreach ($tags as $tag) {
  53. $tag->delete();
  54. }
  55. } // if
  56. } // clearObjectTags
  57. /**
  58. * Set tags for specific object
  59. *
  60. * @access public
  61. * @param array $tags Array of tags... Can be NULL or empty
  62. * @param ApplicationDataObject $object
  63. * @param string $manager_class
  64. * @param Project $project
  65. * @return null
  66. */
  67. function setObjectTags($tags, ApplicationDataObject $object, $manager_class, $project = null) {
  68. self::clearObjectTags($object, $manager_class);
  69. if (is_array($tags) && count($tags)) {
  70. $tags = array_unique($tags);
  71. foreach ($tags as $tag_name) {
  72. if (trim($tag_name) <> '') {
  73. $tag = new Tag();
  74. if ($project instanceof Project) {
  75. $tag->setProjectId($project->getId());
  76. }
  77. $tag->setTag($tag_name);
  78. $tag->setRelObjectId($object->getId());
  79. $tag->setRelObjectManager($manager_class);
  80. $tag->setIsPrivate($object->isPrivate());
  81. $tag->save();
  82. } // if
  83. } // foreach
  84. } // if
  85. return true;
  86. } // setObjectTags
  87. /**
  88. * Return unique tag names used on project objects
  89. *
  90. * @access public
  91. * @param Project $project
  92. * @return array
  93. */
  94. function getProjectTagNames(Project $project, $exclude_private = false) {
  95. if ($exclude_private) {
  96. $rows = DB::executeAll("SELECT DISTINCT `tag` FROM " . self::instance()->getTableName(true) . ' WHERE `project_id` = ? AND `is_private` = ? ORDER BY `tag`', $project->getId(), 0);
  97. } else {
  98. $rows = DB::executeAll("SELECT DISTINCT `tag` FROM " . self::instance()->getTableName(true) . ' WHERE `project_id` = ? ORDER BY `tag`', $project->getId());
  99. } // if
  100. if (!is_array($rows) || !count($rows)) {
  101. return null;
  102. }
  103. $tags = array();
  104. foreach ($rows as $row) {
  105. $tags[] = $row['tag'];
  106. } // foreach
  107. return $tags;
  108. } // getProjectTagNames
  109. /**
  110. * Return unique tag names used for certain classes of objects
  111. *
  112. * @access public
  113. * @param array $class_name
  114. * @return array
  115. */
  116. function getClassTagNames($class_names, $exclude_private = false) {
  117. if ($exclude_private) {
  118. $rows = DB::executeAll("SELECT DISTINCT `tag` FROM " . self::instance()->getTableName(true) . ' WHERE `rel_object_manager` IN (?) AND `is_private` = ? ORDER BY `tag`', $class_names, 0);
  119. } else {
  120. $rows = DB::executeAll("SELECT DISTINCT `tag` FROM " . self::instance()->getTableName(true) . ' WHERE `rel_object_manager` IN (?) ORDER BY `tag`', $class_names);
  121. } // if
  122. if (!is_array($rows) || !count($rows)) {
  123. return null;
  124. } // if
  125. $tags = array();
  126. foreach ($rows as $row) {
  127. $tags[] = $row['tag'];
  128. } // foreach
  129. return $tags;
  130. } // getClassTagNames
  131. /**
  132. * Return array of project objects. Optional filters are by tag and / or by object class
  133. *
  134. * @access public
  135. * @param Project $project
  136. * @param string $tag Return objects that are tagged with specific tag
  137. * @param string $class Return only object that match specific class (manager class name)
  138. * @param boolean $exclude_private Exclude private objects from listing
  139. * @return array
  140. */
  141. function getProjectObjects(Project $project, $tag = null, $class = null, $exclude_private = false) {
  142. $conditions = '`project_id` = ' . DB::escape($project->getId());
  143. if (trim($tag) <> '') {
  144. $conditions .= ' AND `tag` = ' . DB::escape($tag);
  145. }
  146. if (trim($class) <> '') {
  147. $conditions .= ' AND `rel_object_manager` = ' . DB::escape($class);
  148. }
  149. if ($exclude_private) {
  150. $conditions .= ' AND `is_private` = ' . DB::escape(0);
  151. }
  152. $tags = self::findAll(array(
  153. 'conditions' => $conditions,
  154. 'order_by' => '`created_on`'
  155. )); // findById
  156. if (!is_array($tags)) {
  157. return null;
  158. }
  159. $objects = array();
  160. foreach ($tags as $tag_object) {
  161. $object = $tag_object->getObject();
  162. if ($object instanceof ApplicationDataObject ) {
  163. $objects[] = $object;
  164. }
  165. } // foreach
  166. return count($objects) ? $objects : null;
  167. } // getProjectObjects
  168. /**
  169. * Returns number of objects tagged with specific tag
  170. *
  171. * @access public
  172. * @param string $tag Tag name
  173. * @param Project $project Only objects that belong to this project
  174. * @param boolean $exclude_private Exclude private objects from listing
  175. * @return integer
  176. */
  177. function countProjectObjectsByTag($tag, Project $project, $exclude_private = false) {
  178. if ($exclude_private) {
  179. $row = DB::executeOne("SELECT COUNT(`id`) AS 'row_count' FROM " . self::instance()->getTableName(true) . " WHERE `tag` = ? AND `project_id` = ? AND `is_private` = ?", $tag, $project->getId(), 0);
  180. } else {
  181. $row = DB::executeOne("SELECT COUNT(`id`) AS 'row_count' FROM " . self::instance()->getTableName(true) . " WHERE `tag` = ? AND `project_id` = ?", $tag, $project->getId());
  182. } // if
  183. return array_var($row, 'row_count', 0);
  184. } // countProjectObjectsByTag
  185. } // Tags
  186. ?>