Jump to content

Community

Developer Documentation

Supporting comments in content items

In order to use comments, you'll need to make a few adjustments to your content item model too.

 

Class property changes

You will need to add new properties to your content item model.

 

public static $commentClass = 'string';

This should be the full path to your comment model class. For example:

public static $commentClass = 'IPS\yourApp\YourCommentClass';

 

protected static $firstCommentRequiredboolean;

If your content item requires a first comment (e.g. the Forums app requires the first comment, which appears as the first post in a topic), you can set this property to TRUE. By default, it is FALSE.

 

You also need to add new values to your content item model's $databaseColumnMap, as follows:

  • num_comments
    Required. Value should be the column name that holds the total comment count for the content item.
  • last_comment
    Optional. Value should be the column name that holds a timestamp of when the last comment was made.
  • last_comment_by
    Optional. Value should be the column name that holds the ID of the member who made the last comment.
  • last_comment_by
    Optional. Value should be the column name that holds the username of the member who made the last comment.

Although optional, the last three properties above can make your application more efficient if they are specified and used.

 

Additional content item model methods available

integer static getCommentsPerPage()

Returns the number of comments that should be shown per page. The default is 25. You can override this in your model to change the number shown per page, for example to base it on a setting within your application.

 

integer commentPageCount( [ boolean $recache ] )

Returns the number of comment pages the item has.

  • $recache (boolean, optional, default FALSE)
    Forces a recalculation and recache of the result of this method.

 

string commentPagination( [ array $queryString [, string $template [, integer $pageCount [, \IPS\Http\Url $baseUrl ] ] ] ] )

Returns the HTML for the pagination of the content item comments.

  • $queryString (array, optional)
    An array of additional query string parameters to be added to pagination URLs (for example, current sorting options).
  • $template (string, optional, default 'pagination')
    The name of the template to use to generate the pagination. Either 'pagination' or 'miniPagination'.
  • $pageCount (integer, optional)
    The number of pages of comments on this item, if known. If NULL, will be calculated on demand.
  • $baseUrl (\IPS\Url\Http, optional)
    Allows you to specify an alternative base URL to use for the pagination links. By default, the item's normal base URL will be used.

 

mixed comments( [ integer $limit [, integer $offset [, string $order [, string $orderDirection [, \IPS\Member $member [, boolean $includeHidden [, \IPS\DateTime $cutoff [, mixed $extraWhere [, boolean $bypassCache ] ] ] ] ] ] ] ] ] )

Returns the comments on the content item. If $limit is 1, will return an \IPS\Content\Comment object. For all other params, will return an array of comments if any exist, or NULL.

  • $limit (integer, optional)
    The number of comments to fetch. By default, the number returned by static::getCommentsPerPage is used.
  • $offset (integer, optional)
    The offset from which to return comments. By default, whatever value exists in \IPS\Request::i()->page is used.
  • $order (string, optional, default 'date')
    The field on which comments should be sorted.
  • $orderDirection (string, optional, default 'asc')
    The direction in which comments should be sorted, either 'asc' or 'desc'.
  • $member (\IPS\Member, optional)
    If a member object is provided, will fetch comments only by that member.
  • $includeHidden (boolean, optional)
    Determines whether hidden comments should also be returned. By default, the currently-logged in member's permissions will be used to determine this value.
  • $cutoff (\IPS\DateTime, optional)
    If provided, only comments posted after this time will be returned.
  • $extraWhere (string or array, optional)
    Extra parameters to pass into the WHERE clause of the underlying query.
  • $bypassCache (boolean, optional, default FALSE)
    Determines whether comments should be forcefully reloaded rather than fetched from the cache.

 

string commentForm()

Returns the HTML for the comment form

 

\IPS\Http\Url lastCommentPageUrl()

Returns the URL for the last page of comments, as an \IPS\Http\Url object.

 

boolean canComment( [ \IPS\Member $member ] )

Indicates whether the member is able to comment on the item.

  • $member (\IPS\Member, optional)
    If provided, will use this member's permissions when checking. By default, the currently-logged in member will be used.

 

Other behavioral changes

  • The stats() method will now return an additional key containing the number of comments on the item.

  Report Document


×
×
  • Create New...