From 184099743bb508297fb1cd7c2680b89ed68e2813 Mon Sep 17 00:00:00 2001 From: cweiske Date: Sun, 25 Oct 2009 20:07:54 +0000 Subject: document attachtags git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@425 b3834d28-1941-0410-a4f8-b48e95affb8f --- src/SemanticScuttle/Service/Bookmark2Tag.php | 109 +++++++++++++++++---------- 1 file changed, 70 insertions(+), 39 deletions(-) (limited to 'src') diff --git a/src/SemanticScuttle/Service/Bookmark2Tag.php b/src/SemanticScuttle/Service/Bookmark2Tag.php index 56c85ae..8c61f2f 100644 --- a/src/SemanticScuttle/Service/Bookmark2Tag.php +++ b/src/SemanticScuttle/Service/Bookmark2Tag.php @@ -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'); -- cgit v1.2.3