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 != '') {
@ -101,56 +118,70 @@ class SemanticScuttle_Service_Bookmark2Tag extends SemanticScuttle_DbService
} }
} }
$bs =SemanticScuttle_Service_Factory::get('Bookmark'); $bs = SemanticScuttle_Service_Factory::get('Bookmark');
$tts =SemanticScuttle_Service_Factory::get('Tag2Tag'); $tts = SemanticScuttle_Service_Factory::get('Tag2Tag');
// Create links between tags // Create links between tags
foreach($tags as $key => $tag) { foreach ($tags as $key => $tag) {
if(strpos($tag, '=')) { if (strpos($tag, '=')) {
// case "=" // case "="
$pieces = explode('=', $tag); $pieces = explode('=', $tag);
$nbPieces = count($pieces); $nbPieces = count($pieces);
if($nbPieces > 1) { if ($nbPieces <= 1) {
for($i = 0; $i < $nbPieces-1; $i++) { continue;
$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
} }
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 { } else {
// case ">" // case ">"
$pieces = explode('>', $tag); $pieces = explode('>', $tag);
$nbPieces = count($pieces); $nbPieces = count($pieces);
if($nbPieces > 1) { if ($nbPieces <= 1) {
for($i = 0; $i < $nbPieces-1; $i++) { continue;
$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
} }
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];
} }
} }
// Add the categories to the DB. //after exploding, there may be duplicate keys
for ($i = 0; $i < count($tags); $i++) { //since we are in a transaction, hasTag() may
if ($tags[$i] != '') { // not return true for newly added duplicate tags
$values = array( $tags = array_unique($tags);
'bId' => intval($bookmarkid),
'tag' => $tags[$i]
);
if (!$this->hasTag($bookmarkid, $tags[$i])) { // Add the tags to the DB.
$sql = 'INSERT INTO '. $this->getTableName() .' '. $this->db->sql_build_array('INSERT', $values); foreach ($tags as $tag) {
if (!($dbresult =& $this->db->sql_query($sql))) { if ($tag == '') {
$this->db->sql_transaction('rollback'); continue;
message_die(GENERAL_ERROR, 'Could not attach tags', '', __LINE__, __FILE__, $sql, $this->db); }
return false; if ($this->hasTag($bookmarkid, $tag)) {
} continue;
} }
$values = array(
'bId' => intval($bookmarkid),
'tag' => $tag
);
$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'); $this->db->sql_transaction('commit');