function removeTag($tagId, $user, $removeFromAllResources = false) { require_once ROOT_DIR . '/services/MyResearch/lib/Tags.php'; require_once ROOT_DIR . '/services/MyResearch/lib/Resource_tags.php'; $rTag = new Resource_tags(); if (!$removeFromAllResources) { $rTag->resource_id = $this->id; } $rTag->tag_id = $tagId; $rTag->user_id = $user->id; $rTag->find(); if ($rTag->N > 0) { while ($rTag->fetch()) { $rTag->delete(); } } else { //the tag was not found. return false; } //Check to see if the tag is still in use by any user for any resource. $rTag = new Resource_tags(); $rTag->tag_id = $tagId; $rTag->find(); if ($rTag->N == 0) { //Tag is still in use, delete it. $tags = new Tags(); $tags->id = $tagId; if ($tags->find(true)) { $tags->delete(); } } return true; }
/** * Add a resource to the user's account. * * @param object $resource The resource to add. * @param object $list The list to store the resource in. * @param array $tagArray An array of tags to associate with the * resource. * @param string $notes User notes about the resource. * @param bool $replaceExisting Whether to replace all existing tags (true) * or append to the existing list (false). * * @return bool * @access public */ public function addResource($resource, $list, $tagArray, $notes, $replaceExisting = true) { $join = new User_resource(); $join->user_id = $this->id; $join->resource_id = $resource->id; $join->list_id = $list->id; if ($join->find(true)) { $join->notes = $notes; $join->update(); // update() will return false if we save without making any changes, // but we always want to report success after this point. $result = true; } else { if ($notes) { $join->notes = $notes; } $result = $join->insert(); } if ($result) { $join = new Resource_tags(); $join->resource_id = $resource->id; $join->user_id = $this->id; $join->list_id = $list->id; if ($replaceExisting) { // Delete old tags -- note that we need to clone $join for this // operation or else it will be broken when we use it for searching // below. $killer = clone $join; $killer->delete(); } // Add new tags, if any: if (is_array($tagArray) && count($tagArray)) { foreach ($tagArray as $value) { $value = str_replace('"', '', $value); $tag = new Tags(); $tag->tag = $value; if (!$tag->find(true)) { $tag->insert(); } $join->tag_id = $tag->id; // Don't save duplicate tags! if (!$join->find(false)) { $join->insert(); } } } // Update list modification date $list->updateModifiedDate(); return true; } else { return false; } }
/** * Add a tag to the current resource. * * @param string $tag The tag to save. * @param object $user The user posting the tag. * * @return bool True on success, false on failure. * @access public */ public function addTag($tag, $user) { $tag = trim($tag); if (!empty($tag)) { include_once 'services/MyResearch/lib/Tags.php'; include_once 'services/MyResearch/lib/Resource_tags.php'; $tags = new Tags(); $tags->tag = $tag; if (!$tags->find(true)) { $tags->insert(); } $rTag = new Resource_tags(); $rTag->resource_id = $this->id; $rTag->tag_id = $tags->id; if (!$rTag->find()) { $rTag->user_id = $user->id; $rTag->insert(); } } return true; }