Documentation

Like
in package
implements ActionInterface, Routable uses ActionRouter, ActionTrait

Handles liking posts and displaying the list of who liked a post.

Table of Contents

Interfaces

ActionInterface
Interface for all action classes.
Routable
Classes implementing this interface can build routes from URL query parameters and parse routes back into URL query parameters.

Properties

$subaction  : string
$subactions  : array<string|int, mixed>
$already_liked  : bool
$content  : int
$data  : mixed
$error  : string
$extra  : string|bool
$id_topic  : int
$js  : bool
$num_likes  : int
$obj  : static
$set_response  : bool
$type  : string
$valid_likes  : array<string|int, mixed>

Methods

buildRoute()  : array<string|int, mixed>
Builds a routing path based on URL query parameters.
call()  : void
Convenience method to load() and execute() an instance of this class.
canBeLogged()  : bool
Determines whether this action can be logged in the online log.
canShowDebuggingInfo()  : bool
Determines whether debugging info should be shown.
canShowInMaintenanceMode()  : bool
Determines whether this action allows access in maintenance mode.
execute()  : void
The main handler.
get()  : mixed
A simple getter for all protected properties.
getOutputType()  : OutputTypeInterface
Gets the output type for this action.
isAgreementAction()  : bool
Determines whether this action can be accessed without accepting the registration agreement and privacy policy.
isRestrictedGuestAccessAllowed()  : bool
Determines whether this action allows access if guest access is restricted.
isSimpleAction()  : bool
Determines whether this is a simple action.
load()  : static
Static wrapper for constructor.
parseRoute()  : array<string|int, mixed>
Parses a route to get URL query parameters.
__construct()  : mixed
Sets the basic data needed for the rest of the process.
buildActionRoute()  : array<string|int, mixed>
Builds a routing path for an action based on URL query parameters.
check()  : void
Performs basic checks on the data provided, checks for a valid msg like.
count()  : void
Sets $this->num_likes to the actual number of likes that the content has.
delete()  : void
Deletes an entry from user_likes table.
insert()  : void
Inserts a new entry on user_likes table.
like()  : void
Performs a like action, either like or unlike.
parseActionRoute()  : array<string|int, mixed>
Parses a route for an action to get URL query parameters.
respond()  : void
Checks if the user can use JavaScript and acts accordingly.
sendJsonReponse()  : void
Outputs a JSON-encoded response.
view()  : void
This is for viewing the people who liked a thing.

Properties

$subaction

public string $subaction = 'like'

The requested sub-action. This should be set by the constructor.

$subactions

public static array<string|int, mixed> $subactions = ['like' => 'like', 'view' => 'view', 'delete' => 'delete', 'insert' => 'insert', 'count' => 'count']

Available sub-actions.

Tags
todo

Do delete, insert, and count really need to be sub-actions? They are never used as sub-actions in practice. Instead, they are only ever called internally by the like() method. Moreover, the control flow regarding hooks, etc., assumes that they are only called by like().

$already_liked

protected bool $already_liked = false

If the current user has already liked this content.

$content

protected int $content = 0

A valid ID to identify the content being liked.

$data

protected mixed $data

Data for the response.

$error

protected string $error = null

If filled, its value will contain a string matching a key on a language var Lang::$txt[$this->error]

$extra

protected string|bool $extra = false

A generic string used if you need to pass any extra info. It gets set via $_GET['extra'].

$id_topic

protected int $id_topic = 0

The topic ID. Used for liking messages.

$js

protected bool $js = false

Know if a request comes from an ajax call or not. Depends on $_GET['js'] been set.

$num_likes

protected int $num_likes = 0

The number of times the content has been liked.

$obj

protected static static $obj

An instance of this class. This is used by the load() method to prevent multiple instantiations.

$set_response

protected bool $set_response = true

Whether respond() will be executed as normal.

If this is set to false it indicates the method already implemented its own way to send back a response.

$type

protected string $type = ''

The unique type to like, needs to be unique and it needs to be no longer than 6 characters, only numbers and letters are allowed.

$valid_likes

protected array<string|int, mixed> $valid_likes = ['can_like' => false, 'redirect' => '', 'type' => '', 'flush_cache' => '', 'callback' => false, 'json' => false]

Mostly used for external integration. Needs to be filled as an array with the following keys:

'can_like' bool|string True if the current user can actually like this content, or a Lang::$txt key for an error message if not.

'redirect' string URL to redirect to after the like is submitted. If not set, will redirect to the forum index.

'type' string 6 character unique identifier for the content. Must match what was sent in $_GET['ltype']

'flush_cache' bool If true, reset the like content's cache entry after a new entry has been inserted. Optional.

'callback' callable Optional function or method to call immediately after like data has been inserted or deleted. If set, the callback will be called before the integrate_issue_like hook.

'json' bool If true, the class will return a JSON object as a response instead of HTML. Default: false.

Methods

buildRoute()

Builds a routing path based on URL query parameters.

public static buildRoute(array<string|int, mixed> $params) : array<string|int, mixed>
Parameters
$params : array<string|int, mixed>

URL query parameters.

Return values
array<string|int, mixed>

Contains two elements: ['route' => [], 'params' => []]. The 'route' element contains the routing path. The 'params' element contains any $params that weren't incorporated into the route.

call()

Convenience method to load() and execute() an instance of this class.

public static call() : void

canBeLogged()

Determines whether this action can be logged in the online log.

public canBeLogged() : bool
Return values
bool

canShowDebuggingInfo()

Determines whether debugging info should be shown.

public canShowDebuggingInfo() : bool
Return values
bool

canShowInMaintenanceMode()

Determines whether this action allows access in maintenance mode.

public canShowInMaintenanceMode() : bool
Return values
bool

True if access is allowed, false otherwise.

execute()

The main handler.

public execute() : void

Verifies permissions (whether the user can see the content in question), dispatch different method for different sub-actions.

Accessed from index.php?action=likes

get()

A simple getter for all protected properties.

public get([string $property = '' ]) : mixed

This is meant to give read-only access to hooked functions.

Parameters
$property : string = ''

The name of the property to get.

Return values
mixed

Either return the property or false if there isn't a property with that name.

isAgreementAction()

Determines whether this action can be accessed without accepting the registration agreement and privacy policy.

public isAgreementAction() : bool
Return values
bool

isRestrictedGuestAccessAllowed()

Determines whether this action allows access if guest access is restricted.

public isRestrictedGuestAccessAllowed() : bool
Return values
bool

True if access is allowed, false otherwise.

isSimpleAction()

Determines whether this is a simple action.

public isSimpleAction() : bool

Simple actions don't require the index template at all.

Return values
bool

load()

Static wrapper for constructor.

public static load() : static
Return values
static

An instance of this class.

parseRoute()

Parses a route to get URL query parameters.

public static parseRoute(array<string|int, mixed> $route[, array<string|int, mixed> $params = [] ]) : array<string|int, mixed>
Parameters
$route : array<string|int, mixed>

Array of routing path components.

$params : array<string|int, mixed> = []

Any existing URL query parameters.

Return values
array<string|int, mixed>

URL query parameters

__construct()

Sets the basic data needed for the rest of the process.

protected __construct() : mixed

Protected to force instantiation via load().

buildActionRoute()

Builds a routing path for an action based on URL query parameters.

protected static buildActionRoute(array<string|int, mixed> &$params) : array<string|int, mixed>

The 'action', 'area', and 'sa' parameters will be mapped to route path components in that order. The 'action' parameter is required, whereas 'area' and 'sa' are optional and will be silently skipped if not set.

The parameters are passed by reference and parameters are removed from the array when recognized. This lets wrapper methods continue parsing the remainder of the parameters without worrying about duplicate elements.

Parameters
$params : array<string|int, mixed>

URL query parameters.

Return values
array<string|int, mixed>

The route path elements.

check()

Performs basic checks on the data provided, checks for a valid msg like.

protected check() : void

Calls integrate_valid_likes hook for retrieving all the data needed and apply checks based on the data provided.

count()

Sets $this->num_likes to the actual number of likes that the content has.

protected count() : void

delete()

Deletes an entry from user_likes table.

protected delete() : void

insert()

Inserts a new entry on user_likes table.

protected insert() : void

Creates a background task for the inserted entry.

like()

Performs a like action, either like or unlike.

protected like() : void

Counts the total of likes and calls a hook after the event.

parseActionRoute()

Parses a route for an action to get URL query parameters.

protected static parseActionRoute(array<string|int, mixed> &$route) : array<string|int, mixed>

The first element of the route path is mapped to the 'action' parameter.

For the 'admin', 'moderate', and 'profile' actions, the second element of the route is mapped to the 'area' parameter and the third element is mapped to the 'sa' parameter.

For all other actions, the second element of the route path is mapped to the 'sa' parameter.

The route passed by reference and route path elements are removed from the route when recognized. This lets wrapper methods continue parsing the remainder of the route without worrying about duplicate elements.

Parameters
$route : array<string|int, mixed>

Array of routing path components.

Return values
array<string|int, mixed>

URL query parameters

respond()

Checks if the user can use JavaScript and acts accordingly.

protected respond() : void

Calls the appropriate sub-template for each method Handles error messages.

sendJsonReponse()

Outputs a JSON-encoded response.

protected sendJsonReponse() : void

view()

This is for viewing the people who liked a thing.

protected view() : void

Accessed from index.php?action=likes;view and should generally load in a popup.

We use a template for this in case themers want to style it.


        
On this page

Search results