document attachtags

git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@425 b3834d28-1941-0410-a4f8-b48e95affb8f
This commit is contained in:
cweiske 2009-10-25 20:07:54 +00:00
parent f327c6a0f1
commit 184099743b

View file

@ -30,10 +30,27 @@ class SemanticScuttle_Service_Bookmark2Tag extends SemanticScuttle_DbService
return true; 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 * Attach tags to a bookmark.
// coming in from an API call to add a bookmark, that underscores are converted into strings. *
* 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)) { if (!is_array($tags)) {
$tags = trim($tags); $tags = trim($tags);
if ($tags != '') { if ($tags != '') {
@ -110,49 +127,63 @@ class SemanticScuttle_Service_Bookmark2Tag extends SemanticScuttle_DbService
// case "=" // case "="
$pieces = explode('=', $tag); $pieces = explode('=', $tag);
$nbPieces = count($pieces); $nbPieces = count($pieces);
if($nbPieces > 1) { if ($nbPieces <= 1) {
continue;
}
for ($i = 0; $i < $nbPieces-1; $i++) { for ($i = 0; $i < $nbPieces-1; $i++) {
$bookmark = $bs->getBookmark($bookmarkid); $bookmark = $bs->getBookmark($bookmarkid);
$uId = $bookmark['uId']; $uId = $bookmark['uId'];
$tts->addLinkedTags($pieces[$i], $pieces[$i+1], '=', $uId); $tts->addLinkedTags($pieces[$i], $pieces[$i+1], '=', $uId);
} }
$tags[$key] = $pieces[0]; // Attach just the last tag to the bookmark // Attach just the last tag to the bookmark
} $tags[$key] = $pieces[0];
} else { } else {
// case ">" // case ">"
$pieces = explode('>', $tag); $pieces = explode('>', $tag);
$nbPieces = count($pieces); $nbPieces = count($pieces);
if($nbPieces > 1) { if ($nbPieces <= 1) {
continue;
}
for ($i = 0; $i < $nbPieces-1; $i++) { for ($i = 0; $i < $nbPieces-1; $i++) {
$bookmark = $bs->getBookmark($bookmarkid); $bookmark = $bs->getBookmark($bookmarkid);
$uId = $bookmark['uId']; $uId = $bookmark['uId'];
$tts->addLinkedTags($pieces[$i], $pieces[$i+1], '>', $uId); $tts->addLinkedTags($pieces[$i], $pieces[$i+1], '>', $uId);
} }
$tags[$key] = $pieces[$nbPieces-1]; // Attach just the last tag to the bookmark // 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( $values = array(
'bId' => intval($bookmarkid), 'bId' => intval($bookmarkid),
'tag' => $tags[$i] 'tag' => $tag
); );
if (!$this->hasTag($bookmarkid, $tags[$i])) { $sql = 'INSERT INTO '. $this->getTableName()
$sql = 'INSERT INTO '. $this->getTableName() .' '. $this->db->sql_build_array('INSERT', $values); . ' ' . $this->db->sql_build_array('INSERT', $values);
if (!($dbresult =& $this->db->sql_query($sql))) { if (!($dbresult =& $this->db->sql_query($sql))) {
$this->db->sql_transaction('rollback'); $this->db->sql_transaction('rollback');
message_die(GENERAL_ERROR, 'Could not attach tags', '', __LINE__, __FILE__, $sql, $this->db); message_die(
GENERAL_ERROR, 'Could not attach tags',
'', __LINE__, __FILE__, $sql, $this->db
);
return false; return false;
} }
} }
}
}
$this->db->sql_transaction('commit'); $this->db->sql_transaction('commit');
return true; return true;
} }