Add method to get tagIds without having to call ensureTags()
authorMichael J. Rubinsky <mrubinsk@horde.org>
Mon, 8 Mar 2010 23:09:19 +0000 (18:09 -0500)
committerMichael J. Rubinsky <mrubinsk@horde.org>
Mon, 8 Mar 2010 23:09:19 +0000 (18:09 -0500)
Calling ensureTags() results in the tags being created. This isn't needed
in every case we want a list of tagIds.

content/lib/Tagger.php

index e4c3f07..587b14c 100644 (file)
@@ -607,14 +607,14 @@ class Content_Tagger
     }
 
     /**
-     * Ensure that an array of tags exist, create any that don't, and
-     * return ids for all of them.
-     *
-     * @param array $tags  Array of tag names or ids.
-     *
-     * @return array  Array of tag ids.
+     * Check if tags exists, optionally create then if they don't and return
+     * ids for all that exist (including those that are optionally created).
+     * 
+     * @param <type> $tags
+     * @param <type> $create
+     * @return <type> \
      */
-    public function ensureTags($tags)
+    protected function _checkTags($tags, $create = true)
     {
         if (!is_array($tags)) {
             $tags = array($tags);
@@ -646,15 +646,38 @@ class Content_Tagger
             }
         }
 
-        // Create any tags that didn't already exist
-        foreach ($tagText as $tag => $tagIndex) {
-            $tagIds[$tagIndex] = $this->_db->insert('INSERT INTO ' . $this->_t('tags') . ' (tag_name) VALUES (' . $this->_db->quote($tag) . ')');
+        if ($create) {
+            // Create any tags that didn't already exist
+            foreach ($tagText as $tag => $tagIndex) {
+                $tagIds[$tagIndex] = $this->_db->insert('INSERT INTO ' . $this->_t('tags') . ' (tag_name) VALUES (' . $this->_db->quote($tag) . ')');
+            }
         }
 
         return $tagIds;
     }
 
     /**
+     * Ensure that an array of tags exist, create any that don't, and
+     * return ids for all of them.
+     *
+     * @param array $tags  Array of tag names or ids.
+     *
+     * @return array  Array of tag ids.
+     */
+    public function ensureTags($tags)
+    {
+        return $this->_checkTags($tags);
+    }
+
+    /**
+     *
+     */
+    public function getTagIds($tags)
+    {
+        return $this->_checkTags($tags, false);
+    }
+
+    /**
      * Split a string into an array of tag names, respecting tags with spaces
      * and ones that are quoted in some way. For example:
      *   this, "somecompany, llc", "and ""this"" w,o.rks", foo bar