Skip to content

Commit

Permalink
migrated tag related methods
Browse files Browse the repository at this point in the history
  • Loading branch information
skellamp committed Dec 25, 2023
1 parent 3613dfc commit 1fdf7e3
Show file tree
Hide file tree
Showing 15 changed files with 292 additions and 231 deletions.
7 changes: 4 additions & 3 deletions module/VuFind/src/VuFind/Controller/Plugin/Favorites.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,14 @@ public function saveBulk($params, $user)
[$source, $id] = explode('|', $current, 2);

// Get or create a resource object as needed:
$resourceTable = $this->getController()->getTable('Resource');
$resource = $resourceTable->findResource($id, $source);
$resourceService = $this->getController()->getDbService(\VuFind\Db\Service\ResourceService::class);
$resource = $resourceService->findResource($id, $source);

// Add the information to the user's account:
$tags = isset($params['mytags'])
? $this->tags->parse($params['mytags']) : [];
$user->saveResource($resource, $list->getId(), $tags, '', false);
$userService = $this->getController()->getDbService(\VuFind\Db\Service\UserService::class);
$userService->saveResource($resource, $user->id, $list, $tags, '', false);

// Collect record IDs for caching
if ($this->cache->isCachable($resource->source)) {
Expand Down
93 changes: 93 additions & 0 deletions module/VuFind/src/VuFind/Db/Entity/Ratings.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

namespace VuFind\Db\Entity;

use DateTime;
use Doctrine\ORM\Mapping as ORM;

/**
Expand Down Expand Up @@ -107,6 +108,16 @@ class Ratings implements EntityInterface
*/
protected $created = '2000-01-01 00:00:00';

/**
* Id getter
*
* @return ?int
*/
public function getId(): ?int
{
return $this->id;
}

/**
* Get user.
*
Expand All @@ -116,4 +127,86 @@ public function getUser()
{
return $this->user;
}

/**
* User setter
*
* @param User $user
*
* @return Ratings
*/
public function setUser(User $user): Ratings
{
$this->user = $user;
return $this;
}

/**
* Rating setter
*
* @param int $rating
*
* @return Ratings
*/
public function setRating(int $rating): Ratings
{
$this->rating = $rating;
return $this;
}

/**
* Rating getter
*
* @return int
*/
public function getRating(): int
{
return $this->rating;
}

/**
* Resource setter
*
* @param Resource $resource
*
* @return Ratings
*/
public function setResource(Resource $resource): Ratings
{
$this->resource = $resource;
return $this;
}

/**
* Resource getter
*
* @return Resource
*/
public function getResource(): Resource
{
return $this->resource;
}

/**
* Created setter.
*
* @param Datetime $dateTime Created date
*
* @return UserList
*/
public function setCreated(DateTime $dateTime): Ratings
{
$this->created = $dateTime;
return $this;
}

/**
* Created getter
*
* @return Datetime
*/
public function getCreated(): Datetime
{
return $this->created;
}
}
20 changes: 20 additions & 0 deletions module/VuFind/src/VuFind/Db/Entity/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,16 @@ public function setTitle(string $title): Resource
return $this;
}

/**
* Title getter
*
* @return string
*/
public function getTitle(): string
{
return $this->title;
}

/**
* Author setter
*
Expand Down Expand Up @@ -231,4 +241,14 @@ public function setExtraMetadata(?string $extraMetadata): Resource
$this->extraMetadata = $extraMetadata;
return $this;
}

/**
* Extra Metadata getter
*
* @return ?string
*/
public function getExtraMetadata(): ?string
{
return $this->extraMetadata;
}
}
117 changes: 0 additions & 117 deletions module/VuFind/src/VuFind/Db/Row/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@
namespace VuFind\Db\Row;

use VuFind\Date\DateException;
use VuFind\Exception\LoginRequired as LoginRequiredException;

use function intval;
use function is_object;
use function strlen;

/**
Expand Down Expand Up @@ -70,121 +68,6 @@ public function __construct($adapter)
parent::__construct('id', 'resource', $adapter);
}

/**
* Remove tags from the current resource.
*
* @param \VuFind\Db\Row\User $user The user deleting the tags.
* @param string $list_id The list associated with the tags
* (optional -- omitting this will delete ALL of the user's tags).
*
* @return void
*/
public function deleteTags($user, $list_id = null)
{
$unlinker = $this->getDbService(\VuFind\Db\Service\TagService::class);
$unlinker->destroyResourceLinks($this->id, $user->id, $list_id);
}

/**
* Add a tag to the current resource.
*
* @param string $tagText The tag to save.
* @param \VuFind\Db\Row\User $user The user posting the tag.
* @param string $list_id The list associated with the tag
* (optional).
*
* @return void
*/
public function addTag($tagText, $user, $list_id = null)
{
$tagText = trim($tagText);
if (!empty($tagText)) {
$tagService = $this->getDbService(\VuFind\Db\Service\TagService::class);
$tag = $tagService->getByText($tagText);

$tagService->createLink(
$tag,
$this->id,
is_object($user) ? $user->id : null,
$list_id
);
}
}

/**
* Remove a tag from the current resource.
*
* @param string $tagText The tag to delete.
* @param \VuFind\Db\Row\User $user The user deleting the tag.
* @param string $list_id The list associated with the tag
* (optional).
*
* @return void
*/
public function deleteTag($tagText, $user, $list_id = null)
{
$tagText = trim($tagText);
$tagService = $this->getDbService(\VuFind\Db\Service\TagService::class);
if (!empty($tagText)) {
$tagIds = [];
foreach ($tagService->getByText($tagText, false, false) as $tag) {
$tagIds[] = $tag->getId();
}
if (!empty($tagIds)) {
$tagService->destroyResourceLinks(
$this->id,
$user->id,
$list_id,
$tagIds
);
}
}
}

/**
* Add or update user's rating for the current resource.
*
* @param int $userId User ID
* @param ?int $rating Rating (null to delete)
*
* @throws LoginRequiredException
* @throws \Exception
* @return int ID of rating added, deleted or updated
*/
public function addOrUpdateRating(int $userId, ?int $rating): int
{
if (null !== $rating && ($rating < 0 || $rating > 100)) {
throw new \Exception('Rating value out of range');
}

$ratings = $this->getDbTable('Ratings');
$callback = function ($select) use ($userId) {
$select->where->equalTo('ratings.resource_id', $this->id);
$select->where->equalTo('ratings.user_id', $userId);
};
if ($existing = $ratings->select($callback)->current()) {
if (null === $rating) {
$existing->delete();
} else {
$existing->rating = $rating;
$existing->save();
}
return $existing->id;
}

if (null === $rating) {
return 0;
}

$row = $ratings->createRow();
$row->user_id = $userId;
$row->resource_id = $this->id;
$row->rating = $rating;
$row->created = date('Y-m-d H:i:s');
$row->save();
return $row->id;
}

/**
* Use a record driver to assign metadata to the current row. Return the
* current object to allow fluent interface.
Expand Down
36 changes: 0 additions & 36 deletions module/VuFind/src/VuFind/Db/Row/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,42 +324,6 @@ public function getSavedData(
return $table->getSavedData($resourceId, $source, $listId, $this->id);
}

/**
* Add/update a resource in the user's account.
*
* @param \VuFind\Db\Row\Resource $resource The resource to add/update
* @param int $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 void
*/
public function saveResource(
$resource,
$list,
$tagArray,
$notes,
$replaceExisting = true
) {
// Create the resource link if it doesn't exist and update the notes in any
// case:
$linkTable = $this->getDbTable('UserResource');
$linkTable->createOrUpdateLink($resource->id, $this->id, $list, $notes);

// If we're replacing existing tags, delete the old ones before adding the
// new ones:
if ($replaceExisting) {
$resource->deleteTags($this, $list);
}

// Add the new tags:
foreach ($tagArray as $tag) {
$resource->addTag($tag, $this, $list);
}
}

/**
* Given an array of item ids, remove them from all lists
*
Expand Down
Loading

0 comments on commit 1fdf7e3

Please sign in to comment.