/** * Get all resources associated with the current tag. * * @param string $source Record source (optional limiter) * @param string $sort Resource field to sort on (optional) * @param int $offset Offset for results * @param int $limit Limit for results (null for none) * * @return array * @access public */ public function getResources($source = null, $sort = null, $offset = 0, $limit = null) { // Set up base query: $tag = $this; $callback = function ($select) use($tag, $source, $sort, $offset, $limit) { $select->columns(array(new Expression('DISTINCT(?)', array('resource.id'), array(Expression::TYPE_IDENTIFIER)), '*')); $select->join(array('rt' => 'resource_tags'), 'resource.id = rt.resource_id', array()); $select->where->equalTo('rt.tag_id', $tag->id); if (!empty($source)) { $select->where->equalTo('source', $source); } if (!empty($sort)) { ResourceTable::applySort($select, $sort); } if ($offset > 0) { $select->offset($offset); } if (!is_null($limit)) { $select->limit($limit); } }; $table = $this->getDbTable('Resource'); return $table->select($callback); }
/** * Get all resources associated with the provided tag query. * * @param string $query Search query * @param string $source Record source (optional limiter) * @param string $sort Resource field to sort on (optional) * @param int $offset Offset for results * @param int $limit Limit for results (null for none) * * @return array */ public function fuzzyResourceSearch($query, $source = null, $sort = null, $offset = 0, $limit = null) { $cb = function ($select) use($query, $source, $sort, $offset, $limit) { $select->columns([new Expression('DISTINCT(?)', ['resource.id'], [Expression::TYPE_IDENTIFIER])]); $select->join(['rt' => 'resource_tags'], 'tags.id = rt.tag_id', []); $select->join(['resource' => 'resource'], 'rt.resource_id = resource.id', '*'); $select->where->literal('lower(tags.tag) like lower(?)', [$query]); if (!empty($source)) { $select->where->equalTo('source', $source); } if (!empty($sort)) { Resource::applySort($select, $sort); } if ($offset > 0) { $select->offset($offset); } if (null !== $limit) { $select->limit($limit); } }; return $this->select($cb); }
/** * Get a set of records from the requested favorite list. * * @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). * @param int $offset Offset for results * @param int $limit Limit for results (null for none) * * @return \Zend\Db\ResultSet\AbstractResultSet */ public function getFavorites($user, $list = null, $tags = [], $sort = null, $offset = 0, $limit = null) { // Set up base query: $obj =& $this; return $this->select(function ($s) use($user, $list, $tags, $sort, $offset, $limit, $obj) { $s->columns([new Expression('DISTINCT(?)', ['resource.id'], [Expression::TYPE_IDENTIFIER]), '*']); $s->join(['ur' => 'user_resource'], 'resource.id = ur.resource_id', []); $s->where->equalTo('ur.user_id', $user); // Adjust for list if necessary: if (!is_null($list)) { $s->where->equalTo('ur.list_id', $list); } if ($offset > 0) { $s->offset($offset); } if (!is_null($limit)) { $s->limit($limit); } // Adjust for tags if necessary: if (!empty($tags)) { $linkingTable = $obj->getDbTable('ResourceTags'); foreach ($tags as $tag) { $matches = $linkingTable->getResourcesForTag($tag, $user, $list)->toArray(); $getId = function ($i) { return $i['resource_id']; }; $s->where->in('resource.id', array_map($getId, $matches)); } } // Apply sorting, if necessary: if (!empty($sort)) { Resource::applySort($s, $sort); } }); }