CommentModel::getOffset PHP Method

getOffset() public method

Events: BeforeGetOffset
Since: 2.0.0
public getOffset ( mixed $Comment ) : object
$Comment mixed Unique ID or or a comment object for which the offset is being defined.
return object SQL result.
    public function getOffset($Comment)
    {
        $this->fireEvent('BeforeGetOffset');
        if (is_numeric($Comment)) {
            $Comment = $this->getID($Comment);
        }
        $this->SQL->select('c.CommentID', 'count', 'CountComments')->from('Comment c')->where('c.DiscussionID', val('DiscussionID', $Comment));
        $this->SQL->beginWhereGroup();
        // Figure out the where clause based on the sort.
        foreach ($this->_OrderBy as $Part) {
            //$Op = count($this->_OrderBy) == 1 || isset($PrevWhere) ? '=' : '';
            list($Expr, $Value) = $this->_WhereFromOrderBy($Part, $Comment, '');
            if (!isset($PrevWhere)) {
                $this->SQL->where($Expr, $Value);
            } else {
                $this->SQL->orOp();
                $this->SQL->beginWhereGroup();
                $this->SQL->orWhere($PrevWhere[0], $PrevWhere[1]);
                $this->SQL->where($Expr, $Value);
                $this->SQL->endWhereGroup();
            }
            $PrevWhere = $this->_WhereFromOrderBy($Part, $Comment, '==');
        }
        $this->SQL->endWhereGroup();
        return $this->SQL->get()->firstRow()->CountComments;
    }

Usage Example

コード例 #1
0
 /**
  * Display discussion page starting with a particular comment.
  *
  * @since 2.0.0
  * @access public
  *
  * @param int $CommentID Unique comment ID
  */
 public function comment($CommentID)
 {
     // Get the discussionID
     $Comment = $this->CommentModel->getID($CommentID);
     if (!$Comment) {
         throw notFoundException('Comment');
     }
     $DiscussionID = $Comment->DiscussionID;
     // Figure out how many comments are before this one
     $Offset = $this->CommentModel->getOffset($Comment);
     $Limit = Gdn::config('Vanilla.Comments.PerPage', 30);
     $PageNumber = pageNumber($Offset, $Limit, true);
     $this->setData('Page', $PageNumber);
     $this->View = 'index';
     $this->index($DiscussionID, 'x', $PageNumber);
 }