/** * Gets the list of current votes on this object by the current user * * @param Member $user * * @return ArrayList */ public function currentVotesByUser($user = null) { if (!$user) { $user = $this->securityContext->getMember(); } $votes = MicroPostVote::get()->filter(array('UserID' => $user->ID, 'PostID' => $this->ID)); return $votes->toArray(); }
/** * Create a list of posts depending on a filter and time range * * @param array $filter * * @param int $since * The ID after which to get posts * @param int $before * The ID or pagination offset from which to get posts before. * @param type $topLevelOnly * Only retrieve the top level of posts. * @param array $tags * A set of tags to filter posts by * @param int $offset * Offset to start returning results by * @param int $number * How many results to return * * @return DataList */ public function microPostList($filter, $sortBy = 'ID', $since = 0, $before = false, $topLevelOnly = true, $tags = array(), $offset = 0, $number = 10) { if ($topLevelOnly) { $filter['ParentID'] = '0'; } $filter['Deleted'] = 0; if ($since) { $since = Convert::raw2sql($since); $filter['ID:GreaterThan'] = $since; } if ($before !== false) { $before = (int) $before; $filter['ID:LessThan'] = $before; } $sort = array(); if (is_string($sortBy)) { if (in_array($sortBy, $this->canSort)) { $sort[$sortBy] = 'DESC'; } // final sort as a tie breaker $sort['ID'] = 'DESC'; } else { if (is_array($sortBy)) { // $sort = $sortBy; foreach ($sortBy as $sortKey => $sortDir) { if (in_array($sortKey, $this->canSort)) { $sort[$sortKey] = $sortDir; } } } else { $sort = array('ID' => 'DESC'); } } $offset = (int) $offset; $limit = $number ? $offset . ', ' . (int) $number : ''; if (count($tags)) { $filter['Tags.Title'] = $tags; } $this->recordUserAction(); $list = MicroPost::get()->filter($filter)->sort($sort)->limit($limit); $list = $this->updatePostList($list); // if we're only allowing singe votes, we need to get _all_ the current user's votes and // mark the individual posts that have been voted on; this allows the toggling // of the vote options if ($this->singleVotes && $this->securityContext->getMember()) { $ids = $list->column('ID'); $votes = MicroPostVote::get()->filter(array('UserID' => $this->securityContext->getMember()->ID, 'PostID' => $ids)); $map = $votes->map('PostID', 'Direction')->toArray(); foreach ($list as $post) { if (isset($map[$post->ID])) { $post->UserVote = $map[$post->ID] > 0 ? 'upvote' : 'downvote'; } } } return $list->restrict(); }