Jump to content

Developer Documentation

Reactions

Implementing the new Reactions feature on your content items is relatively straightforward. In your content item class, add the \IPS\Content\Reactable trait to your class. And then defined a reactionType() method within your content class - note that this is required so as to differentiate your content from the rest of the applications installed - this is equivalent to the $reputationType property in IPS 4.1 and, if your application implemented reputation previously, should be the same as the previous property. Not defining this method will result in a BadMethodCallException being thrown.

namespace IPS\myapp;

class _MyContentClass extends \IPS\Content\Item
{
	use \IPS\Content\Reactable;

	/** 
	 * Reaction Type
	 * @return	string
	 */
	public static function reactionType()
	{
		return 'my_content_id';
	}
}

Once both the trait and the reactionType() method are added, the following methods from the \IPS\Content\Reactable trait will be available to your class automatically.

	/**
	 * React
	 *
	 * @param	\IPS\core\Reaction		THe reaction
	 * @param	\IPS\Member				The member reacting, or NULL 
	 * @return	void
	 * @throws	\DomainException
	 */
	public function react( \IPS\Content\Reaction $reaction, \IPS\Member $member = NULL )
	
	/**
	 * Remove Reaction
	 *
	 * @param	\IPS\Member|NULL		The member, or NULL
	 * @return	void
	 */
	public function removeReaction( \IPS\Member $member = NULL )
	
	/**
	 * Can React
	 *
	 * @param	\IPS\Member|NULL		The member, or NULL for currently logged in
	 * @return	bool
	 */
	public function canReact( \IPS\Member $member = NULL )
	
	/**
	 * Reactions
	 *
	 * @param	array|NULL	$mixInData	If the data is already know, it can be passed here to be manually set
	 * @return	array
	 */
	public function reactions()
	
	/**
	 * Reaction Count
	 *
	 * @return int
	 */
	public function reactionCount()
	
	/**
	 * Reaction Where Clause
	 *
	 * @param	\IPS\Content\Reaction|array|int|NULL 	This can be any one of the following: An \IPS\Content\Reaction object, an array of \IPS\Content\Reaction objects, and integer, or an array of integers, or NULL
	 * @return	array
	 */
	public function getReactionWhereClause( $reactions = NULL )
	
	/**
	 * Reaction Table
	 *
	 * @return	\IPS\Helpers\Table\Db
	 */
	public function reactionTable( $reaction=NULL )
	
	/**
	 * Has reacted?
	 *
	 * @param	\IPS\Member|NULL	The member, or NULL for currently logged in
	 * @return	\IPS\Content\Reaction|FALSE
	 */
	public function reacted( \IPS\Member $member = NULL )
	
	/**
	 * React Blurb
	 *
	 * @return	string
	 */
	public function reactBlurb()
	
	/**
	 * Who Reacted
	 *
	 * @param	bool|NULL	Use like text instead? NULL to automatically determine
	 * @return	string
	 */
	public function whoReacted( $isLike = NULL )

And then, in your templates, simply add the following where you would like reactions to be shown.

			{{if $item->hidden() !== 1 && \IPS\IPS::classUsesTrait( $item, 'IPS\Content\Reactable' ) and settings.reputation_enabled}}
				{template="reputation" group="global" app="core" params="$item"}
			{{endif}}

And your content item will automatically implement reactions.

Reactions themselves are Nodes and as such, can be expanded on by hooks themselves.

namespace IPS\Content;

/**
 * Reaction Model
 */
class _Reaction extends \IPS\Node\Model
{
	/**
	 * @brief	Database Table
	 */
	public static $databaseTable = 'core_reactions';
	
	/**
	 * @brief	Database Prefix
	 */
	public static $databasePrefix = 'reaction_';
		
	/**
	 * @brief	[ActiveRecord] ID Database Column
	 */
	public static $databaseColumnId = 'id';
	
	/**
	 * @brief	[Node] Node Title
	 */
	public static $nodeTitle = 'reactions';
	
	/**
	 * @brief	[Node] Sortable
	 */
	public static $nodeSortable = TRUE;
	
	/**
	 * @brief	[Node] Positon Column
	 */
	public static $databaseColumnOrder = 'position';
	
	/**
	 * @brief	[Node] Title prefix.  If specified, will look for a language key with "{$key}_title" as the key
	 */
	public static $titleLangPrefix = 'reaction_title_';
	
	/**
	 * @brief	[Node] Enabled/Disabled Column
	 */
	public static $databaseColumnEnabledDisabled = 'enabled';
	
	/**
	 * Form
	 *
	 * @param	\IPS\Helpers\Form	The form
	 * @return	void
	 */
	public function form( &$form )
	
	/**
	 * [Node] Format form values from add/edit form for save
	 *
	 * @param	array	$values	Values from the form
	 * @return	array
	 */
	public function formatFormValues( $values )
	
	/**
	 * [Node] Does the currently logged in user have permission to delete this node?
	 *
	 * @return	bool
	 */
	public function canDelete()
	
	/**
	 * Get Icon
	 *
	 * @return	\IPS\File
	 */
	public function get__icon()

	/**
	 * Get Description
	 *
	 * @return	strong
	 */
	public function get__description()
	
	/**
	 * Fetch All Root Nodes
	 *
	 * @param	string|NULL			$permissionCheck	The permission key to check for or NULl to not check permissions
	 * @param	\IPS\Member|NULL	$member				The member to check permissions for or NULL for the currently logged in member
	 * @param	mixed				$where				Additional WHERE clause
	 * @return	array
	 */
	public static function roots( $permissionCheck='view', $member=NULL, $where=array() )
	
	/**
	 * [Node] Get whether or not this node is enabled
	 *
	 * @note	Return value NULL indicates the node cannot be enabled/disabled
	 * @return	bool|null
	 */
	protected function get__enabled()
	
	/**
	 * Set Enabled
	 *
	 * @return	void
	 */
	public function set__enabled( $enabled )
	
	/**
	 * Reaction Store
	 *
	 * @return	array
	 */
	public static function reactionStore()
	
	/**
	 * Is Like Mode
	 *
	 * @return	bool
	 */
	public static function isLikeMode()
	
	/**
	 * Load Record
	 *
	 * @see		\IPS\Db::build
	 * @param	int|string	$id					ID
	 * @param	string		$idField			The database column that the $id parameter pertains to (NULL will use static::$databaseColumnId)
	 * @param	mixed		$extraWhereClause	Additional where clause(s) (see \IPS\Db::build for details)
	 * @return	static
	 * @throws	\InvalidArgumentException
	 * @throws	\OutOfRangeException
	 */
	public static function load( $id, $idField=NULL, $extraWhereClause=NULL )
	
	/**
	 * [ActiveRecord] Delete
	 *
	 * @return	void
	 */
	public function delete()
	
	/**
	 * [ActiveRecord] Save Changed Columns
	 *
	 * @return	void
	 */
	public function save()
	
	/**
	 * [Node] Get buttons to display in tree
	 * Example code explains return value
	 *
	 * @code
	array(
	array(
	'icon'	=>	'plus-circle', // Name of FontAwesome icon to use
	'title'	=> 'foo',		// Language key to use for button's title parameter
	'link'	=> \IPS\Http\Url::internal( 'app=foo...' )	// URI to link to
	'class'	=> 'modalLink'	// CSS Class to use on link (Optional)
	),
	...							// Additional buttons
	);
	 * @endcode
	 * @param	string	$url		Base URL
	 * @param	bool	$subnode	Is this a subnode?
	 * @return	array
	 */
	public function getButtons( $url, $subnode=FALSE )
}

Important Notes

Reactions can also be implemented on Comments and Reviews in the same way as Content Items - applications that use the default comment / review templates in core/front/global will automatically add the template code if the class uses \IPS\Content\Reactable.

Also note that this replaces the reputation feature. As such all methods relating to Reputation in \IPS\Content, \IPS\Content\Item, \IPS\Content\Comment, and \IPS\Content\Review have been removed along with the \IPS\Content\Reputation interface. Previous reputation data will be upgraded automatically, and converted to an appropriate reaction, by the System application depending on the reputation system used (Like Mode, Upvote only, Downvote only, or Upvote / Downvote), so it is not necessary for you to add any upgrade routines for Reactions in your own applications.

Edited by Ryan Ashbrook

  Report Document


×
×
  • Create New...