diff --git a/module/VuFind/src/VuFind/Db/Table/Resource.php b/module/VuFind/src/VuFind/Db/Table/Resource.php index 200fdc86bc33778358fca2be9d1b4206264731b2..1c047dcfeeccebb9c434de3be813e4d528f5d1d2 100644 --- a/module/VuFind/src/VuFind/Db/Table/Resource.php +++ b/module/VuFind/src/VuFind/Db/Table/Resource.php @@ -112,8 +112,8 @@ class Resource extends Gateway /** * Get a set of records from the requested favorite list. * - * @param string $userId ID of user owning favorite list - * @param string $listId ID of list to retrieve (null for all favorites) + * @param string $user ID of user owning favorite list + * @param string $list ID of list to retrieve (null for all favorites) * @param array $tags Tags to use for limiting results * @param string $sort Resource table field to use for sorting (null for * no particular sort). @@ -122,59 +122,58 @@ class Resource extends Gateway * * @return \Zend\Db\ResultSet\AbstractResultSet */ - public function getFavorites($userId, $listId = null, $tags = array(), + public function getFavorites($user, $list = null, $tags = array(), $sort = null, $offset = 0, $limit = null ) { // Set up base query: - $callback = function ($select) use ($userId, $listId, $tags, $sort, $offset, - $limit - ) { - $select->columns( - array( - new Expression( - 'DISTINCT(?)', array('resource.id'), - array(Expression::TYPE_IDENTIFIER) - ), '*' - ) - ); - $select->join( - array('ur' => 'user_resource'), 'resource.id = ur.resource_id', - array() - ); - $select->where->equalTo('ur.user_id', $userId); - - // Adjust for list if necessary: - if (!is_null($listId)) { - $select->where->equalTo('ur.list_id', $listId); - } + return $this->select( + function ($select) use ($user, $list, $tags, $sort, $offset, $limit) { + $select->columns( + array( + new Expression( + 'DISTINCT(?)', array('resource.id'), + array(Expression::TYPE_IDENTIFIER) + ), '*' + ) + ); + $select->join( + array('ur' => 'user_resource'), 'resource.id = ur.resource_id', + array() + ); + $select->where->equalTo('ur.user_id', $user); - if ($offset > 0) { - $select->offset($offset); - } - if (!is_null($limit)) { - $select->limit($limit); - } + // Adjust for list if necessary: + if (!is_null($list)) { + $select->where->equalTo('ur.list_id', $list); + } - // Adjust for tags if necessary: - if (!empty($tags)) { - $linkingTable = $this->getDbTable('ResourceTags'); - foreach ($tags as $tag) { - $matches = $linkingTable - ->getResourcesForTag($tag, $userId, $listId)->toArray(); - $getId = function ($i) { - return $i['resource_id']; - }; - $select->where->in('resource.id', array_map($getId, $matches)); + if ($offset > 0) { + $select->offset($offset); + } + if (!is_null($limit)) { + $select->limit($limit); } - } - // Apply sorting, if necessary: - if (!empty($sort)) { - Resource::applySort($select, $sort); - } - }; + // Adjust for tags if necessary: + if (!empty($tags)) { + $linkingTable = $this->getDbTable('ResourceTags'); + foreach ($tags as $tag) { + $matches = $linkingTable + ->getResourcesForTag($tag, $user, $list)->toArray(); + $getId = function ($i) { + return $i['resource_id']; + }; + $select->where + ->in('resource.id', array_map($getId, $matches)); + } + } - return $this->select($callback); + // Apply sorting, if necessary: + if (!empty($sort)) { + Resource::applySort($select, $sort); + } + } + ); } /** diff --git a/module/VuFind/src/VuFind/Db/Table/ResourceTags.php b/module/VuFind/src/VuFind/Db/Table/ResourceTags.php index 1b3c531de5cab59907721ac32e5b83e7d6b42040..b1085ed22fae07323f0e96c773703e4a27eb6f74 100644 --- a/module/VuFind/src/VuFind/Db/Table/ResourceTags.php +++ b/module/VuFind/src/VuFind/Db/Table/ResourceTags.php @@ -50,28 +50,25 @@ class ResourceTags extends Gateway /** * Look up a row for the specified resource. * - * @param string $resource_id ID of resource to link up - * @param string $tag_id ID of tag to link up - * @param string $user_id ID of user creating link (optional but recommended) - * @param string $list_id ID of list to link up (optional) + * @param string $resource ID of resource to link up + * @param string $tag ID of tag to link up + * @param string $user ID of user creating link (optional but recommended) + * @param string $list ID of list to link up (optional) * * @return void */ - public function createLink($resource_id, $tag_id, $user_id = null, - $list_id = null - ) { - $callback = function ($select) use ($resource_id, $tag_id, $user_id, - $list_id - ) { - $select->where->equalTo('resource_id', $resource_id) - ->equalTo('tag_id', $tag_id); - if (!is_null($list_id)) { - $select->where->equalTo('list_id', $list_id); + public function createLink($resource, $tag, $user = null, $list = null) + { + $callback = function ($select) use ($resource, $tag, $user, $list) { + $select->where->equalTo('resource_id', $resource) + ->equalTo('tag_id', $tag); + if (!is_null($list)) { + $select->where->equalTo('list_id', $list); } else { $select->where->isNull('list_id'); } - if (!is_null($user_id)) { - $select->where->equalTo('user_id', $user_id); + if (!is_null($user)) { + $select->where->equalTo('user_id', $user); } else { $select->where->isNull('user_id'); } @@ -81,13 +78,13 @@ class ResourceTags extends Gateway // Only create row if it does not already exist: if (empty($result)) { $result = $this->createRow(); - $result->resource_id = $resource_id; - $result->tag_id = $tag_id; - if (!is_null($list_id)) { - $result->list_id = $list_id; + $result->resource_id = $resource; + $result->tag_id = $tag; + if (!is_null($list)) { + $result->list_id = $list; } - if (!is_null($user_id)) { - $result->user_id = $user_id; + if (!is_null($user)) { + $result->user_id = $user; } $result->save(); } @@ -164,40 +161,37 @@ class ResourceTags extends Gateway /** * Unlink rows for the specified resource. * - * @param string|array $resource_id ID (or array of IDs) of resource(s) to + * @param string|array $resource ID (or array of IDs) of resource(s) to * unlink (null for ALL matching resources) - * @param string $user_id ID of user removing links - * @param string $list_id ID of list to unlink (null for ALL matching + * @param string $user ID of user removing links + * @param string $list ID of list to unlink (null for ALL matching * lists, 'none' for tags not in a list) - * @param string $tag_id ID of tag to unlink (null for ALL matching + * @param string $tag ID of tag to unlink (null for ALL matching * tags) * * @return void */ - public function destroyLinks($resource_id, $user_id, $list_id = null, - $tag_id = null - ) { - $callback = function ($select) use ($resource_id, $user_id, $list_id, - $tag_id - ) { - $select->where->equalTo('user_id', $user_id); - if (!is_null($resource_id)) { - if (!is_array($resource_id)) { - $resource_id = array($resource_id); + public function destroyLinks($resource, $user, $list = null, $tag = null) + { + $callback = function ($select) use ($resource, $user, $list, $tag) { + $select->where->equalTo('user_id', $user); + if (!is_null($resource)) { + if (!is_array($resource)) { + $resource = array($resource); } - $select->where->in('resource_id', $resource_id); + $select->where->in('resource_id', $resource); } - if (!is_null($list_id)) { - if ($list_id != 'none') { - $select->where->equalTo('list_id', $list_id); + if (!is_null($list)) { + if ($list != 'none') { + $select->where->equalTo('list_id', $list); } else { - // special case -- if $list_id is set to the string "none", we + // special case -- if $list is set to the string "none", we // want to delete tags that are not associated with lists. $select->where->isNull('list_id'); } } - if (!is_null($tag_id)) { - $select->where->equalTo('tag_id', $tag_id); + if (!is_null($tag)) { + $select->where->equalTo('tag_id', $tag); } }; diff --git a/module/VuFind/src/VuFind/Db/Table/Tags.php b/module/VuFind/src/VuFind/Db/Table/Tags.php index b586d44ae03abeda40277e8765efe8cc5331ffb8..420487cdeb37492da228ce3d24afac2ac953252c 100644 --- a/module/VuFind/src/VuFind/Db/Table/Tags.php +++ b/module/VuFind/src/VuFind/Db/Table/Tags.php @@ -87,63 +87,61 @@ class Tags extends Gateway /** * Get tags associated with the specified resource. * - * @param string $id Record ID to look up - * @param string $source Source of record to look up - * @param int $limit Max. number of tags to return (0 = no limit) - * @param int $list_id ID of list to load tags from (null for no restriction, + * @param string $id Record ID to look up + * @param string $source Source of record to look up + * @param int $limit Max. number of tags to return (0 = no limit) + * @param int $list ID of list to load tags from (null for no restriction, * true for on ANY list, false for on NO list) - * @param int $user_id ID of user to load tags from (null for all users) - * @param string $sort Sort type ('count' or 'tag') + * @param int $user ID of user to load tags from (null for all users) + * @param string $sort Sort type ('count' or 'tag') * * @return array */ public function getForResource($id, $source = 'VuFind', $limit = 0, - $list_id = null, $user_id = null, $sort = 'count' + $list = null, $user = null, $sort = 'count' ) { - $callback = function ($select) use ($id, $source, $limit, $list_id, $user_id, - $sort - ) { - $select->columns( - array( - 'id', 'tag', - 'cnt' => new Expression( - 'COUNT(?)', array('tags.tag'), - array(Expression::TYPE_IDENTIFIER) + return $this->select( + function ($select) use ($id, $source, $limit, $list, $user, $sort) { + $select->columns( + array( + 'id', 'tag', + 'cnt' => new Expression( + 'COUNT(?)', array('tags.tag'), + array(Expression::TYPE_IDENTIFIER) + ) ) - ) - ); - $select->join( - array('rt' => 'resource_tags'), 'tags.id = rt.tag_id', array() - ); - $select->join( - array('r' => 'resource'), 'rt.resource_id = r.id', array() - ); - $select->where->equalTo('r.record_id', $id) - ->equalTo('r.source', $source); - $select->group(array('id', 'tag')); + ); + $select->join( + array('rt' => 'resource_tags'), 'tags.id = rt.tag_id', array() + ); + $select->join( + array('r' => 'resource'), 'rt.resource_id = r.id', array() + ); + $select->where->equalTo('r.record_id', $id) + ->equalTo('r.source', $source); + $select->group(array('id', 'tag')); - if ($sort == 'count') { - $select->order(array('cnt DESC', 'tags.tag')); - } else if ($sort == 'tag') { - $select->order(array('tags.tag')); - } + if ($sort == 'count') { + $select->order(array('cnt DESC', 'tags.tag')); + } else if ($sort == 'tag') { + $select->order(array('tags.tag')); + } - if ($limit > 0) { - $select->limit($limit); - } - if ($list_id === true) { - $select->where->isNotNull('rt.list_id'); - } else if ($list_id === false) { - $select->where->isNull('rt.list_id'); - } else if (!is_null($list_id)) { - $select->where->equalTo('rt.list_id', $list_id); - } - if (!is_null($user_id)) { - $select->where->equalTo('rt.user_id', $user_id); + if ($limit > 0) { + $select->limit($limit); + } + if ($list === true) { + $select->where->isNotNull('rt.list_id'); + } else if ($list === false) { + $select->where->isNull('rt.list_id'); + } else if (!is_null($list)) { + $select->where->equalTo('rt.list_id', $list); + } + if (!is_null($user)) { + $select->where->equalTo('rt.user_id', $user); + } } - }; - - return $this->select($callback); + ); } /**