]> gitweb.fluxo.info Git - semanticscuttle.git/commitdiff
document attachtags
authorcweiske <cweiske@b3834d28-1941-0410-a4f8-b48e95affb8f>
Sun, 25 Oct 2009 20:07:54 +0000 (20:07 +0000)
committercweiske <cweiske@b3834d28-1941-0410-a4f8-b48e95affb8f>
Sun, 25 Oct 2009 20:07:54 +0000 (20:07 +0000)
git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@425 b3834d28-1941-0410-a4f8-b48e95affb8f

src/SemanticScuttle/Service/Bookmark2Tag.php

index 56c85ae44c7e077c7cc10d65013bfe5808c9a41a..8c61f2f6944ae5b311fb22777c2c18d10541946d 100644 (file)
@@ -30,10 +30,27 @@ class SemanticScuttle_Service_Bookmark2Tag extends SemanticScuttle_DbService
         return true;
     }
 
-    function attachTags($bookmarkid, $tags, $fromApi = false, $extension = NULL, $replace = true, $fromImport = false) {
-        // Make sure that categories is an array of trimmed strings, and that if the categories are
-        // coming in from an API call to add a bookmark, that underscores are converted into strings.
-
+    /**
+     * Attach tags to a bookmark.
+     *
+     * Make sure that categories is an array of trimmed strings.
+     * If the categories are coming in from an API call, be sure
+     * that underscores are converted into strings.
+     *
+     * @param integer $bookmarkid ID of the bookmark
+     * @param array   $tags       Array of tags (strings, trimmed)
+     * @param boolean $fromApi    If this is from an API call
+     * @param string  $extension  File extension (i.e. 'pdf')
+     * @param boolean $replace    If existing tags for this bookmark
+     *                            are to be replaced
+     * @param boolean $fromImport If this is from a file import
+     *
+     * @return boolean True if all went well
+     */
+    public function attachTags(
+        $bookmarkid, $tags, $fromApi = false,
+        $extension = null, $replace = true, $fromImport = false
+    ) {
         if (!is_array($tags)) {
             $tags = trim($tags);
             if ($tags != '') {
@@ -101,56 +118,70 @@ class SemanticScuttle_Service_Bookmark2Tag extends SemanticScuttle_DbService
             }
         }
 
-        $bs =SemanticScuttle_Service_Factory::get('Bookmark');
-        $tts =SemanticScuttle_Service_Factory::get('Tag2Tag');
+        $bs  = SemanticScuttle_Service_Factory::get('Bookmark');
+        $tts = SemanticScuttle_Service_Factory::get('Tag2Tag');
 
         // Create links between tags
-        foreach($tags as $key => $tag) {
-            if(strpos($tag, '=')) {
+        foreach ($tags as $key => $tag) {
+            if (strpos($tag, '=')) {
                 // case "="
                 $pieces = explode('=', $tag);
                 $nbPieces = count($pieces);
-                if($nbPieces > 1) {
-                    for($i = 0; $i < $nbPieces-1; $i++) {
-                        $bookmark = $bs->getBookmark($bookmarkid);
-                        $uId = $bookmark['uId'];
-                        $tts->addLinkedTags($pieces[$i], $pieces[$i+1], '=', $uId);
-                    }
-                    $tags[$key] = $pieces[0]; // Attach just the last tag to the bookmark
+                if ($nbPieces <= 1) {
+                    continue;
                 }
+                for ($i = 0; $i < $nbPieces-1; $i++) {
+                    $bookmark = $bs->getBookmark($bookmarkid);
+                    $uId = $bookmark['uId'];
+                    $tts->addLinkedTags($pieces[$i], $pieces[$i+1], '=', $uId);
+                }
+                // Attach just the last tag to the bookmark
+                $tags[$key] = $pieces[0];
             } else {
                 // case ">"
-                $pieces = explode('>', $tag);
+                $pieces   = explode('>', $tag);
                 $nbPieces = count($pieces);
-                if($nbPieces > 1) {
-                    for($i = 0; $i < $nbPieces-1; $i++) {
-                        $bookmark = $bs->getBookmark($bookmarkid);
-                        $uId = $bookmark['uId'];
-                        $tts->addLinkedTags($pieces[$i], $pieces[$i+1], '>', $uId);
-                    }
-                    $tags[$key] = $pieces[$nbPieces-1]; // Attach just the last tag to the bookmark
+                if ($nbPieces <= 1) {
+                    continue;
+                }
+                for ($i = 0; $i < $nbPieces-1; $i++) {
+                    $bookmark = $bs->getBookmark($bookmarkid);
+                    $uId = $bookmark['uId'];
+                    $tts->addLinkedTags($pieces[$i], $pieces[$i+1], '>', $uId);
                 }
+                // Attach just the last tag to the bookmark
+                $tags[$key] = $pieces[$nbPieces-1];
             }
+        }
 
+        //after exploding, there may be duplicate keys
+        //since we are in a transaction, hasTag() may
+        // not return true for newly added duplicate tags
+        $tags = array_unique($tags);
 
-        }
+        // Add the tags to the DB.
+        foreach ($tags as $tag) {
+            if ($tag == '') {
+                continue;
+            }
+            if ($this->hasTag($bookmarkid, $tag)) {
+                continue;
+            }
 
-        // Add the categories to the DB.
-        for ($i = 0; $i < count($tags); $i++) {
-            if ($tags[$i] != '') {
-                $values = array(
-                    'bId' => intval($bookmarkid),
-                    'tag' => $tags[$i]
-                );
+            $values = array(
+                'bId' => intval($bookmarkid),
+                'tag' => $tag
+            );
 
-                if (!$this->hasTag($bookmarkid, $tags[$i])) {
-                    $sql = 'INSERT INTO '. $this->getTableName() .' '. $this->db->sql_build_array('INSERT', $values);
-                    if (!($dbresult =& $this->db->sql_query($sql))) {
-                        $this->db->sql_transaction('rollback');
-                        message_die(GENERAL_ERROR, 'Could not attach tags', '', __LINE__, __FILE__, $sql, $this->db);
-                        return false;
-                    }
-                }
+            $sql = 'INSERT INTO '. $this->getTableName()
+                . ' ' . $this->db->sql_build_array('INSERT', $values);
+            if (!($dbresult =& $this->db->sql_query($sql))) {
+                $this->db->sql_transaction('rollback');
+                message_die(
+                    GENERAL_ERROR, 'Could not attach tags',
+                    '', __LINE__, __FILE__, $sql, $this->db
+                );
+                return false;
             }
         }
         $this->db->sql_transaction('commit');