Table of Contents
This extension provides a bare minimum of “vote” functionality without the frills. It consists of a HTML form with just a submit button.
Usage
Add this snippet at any point in the .\lot\y\*\page.php
file (for example, in the page footer) in your active layout folder to display the vote button:
<?= self::form('vote'); ?>
Options
Key | Description |
---|---|
active | If the value is set to false , then the vote button will be disabled. |
description | The description text or HTML that will be used as the value of the title attribute on the submit button. If this option is not set, the value will be 'Vote up' if it has not been voted on, or 'Cancel' if it has been voted on. |
key | See Voting Context. |
kick | Custom redirection link. If this option is not set, the user will be redirected to the associated page route after the vote. |
page | The page object to associate with the vote data. If this option is not set, it will take the current page object as the value. |
title | The submit button text or HTML. The %d format in it will be converted to the current vote value. If this option is not set, the value will be '%d Vote' if current vote value is 1 , otherwise the value will be '%d Votes' . |
Voting Target
The page
option can be used to specify a page object where the vote data can be stored. By default, it takes the current page object as its value:
<?php
$page = new Page(LOT . D . 'page' . D . 'about.page');
$vote = $page->votes[""] ?? 0;
echo self::form('vote', [
'page' => $page,
'title' => '%d Like' . (1 === $vote ? "" : 's'),
]);
?>
It is especially useful if you want to display vote buttons in .\lot\y\*\pages.php
file:
<?php foreach ($pages as $page): ?>
<article>
<h3>
<?= $page->title; ?>
</h3>
<p>
<?= $page->description; ?>
</p>
<?= self::form('vote', [
'kick' => $url->path,
'page' => $page
]); ?>
</article>
<?php endforeach; ?>
Voting Context
By default, the context is an empty string, so your vote data will be stored as a ""
item in the $page->votes
array:
test($page->votes); // Returns `["" => 1]`
However, you can use the same form to create your own voting context to store your vote data on a specific item in the $page->votes
array, that is, by determining your voting context on the key
option:
<div class="votes">
<?= self::form('vote', [
'key' => 'y',
'title' => 'Yes <span role="status">%d</span>'
]); ?>
<?= self::form('vote', [
'key' => 'n',
'title' => 'No <span role="status">%d</span>'
]); ?>
</div>
.votes {
display: flex;
flex-wrap: wrap;
gap: 0.5em;
margin: 2em 0;
}
.votes > * {
margin: 0;
padding: 0;
}
.votes button {
font-weight: bold;
}
.votes [role='status'] {
color: inherit;
font-weight: normal;
opacity: 0.75;
}
.votes [role='status']::before {
content: '(';
}
.votes [role='status']::after {
content: ')';
}
Voting Limit
In some cases, you may want the user to be able to select only one vote option after being given multiple vote options. For example, two vote options in the form of “Yes” and “No” would usually be better if they could only be selected once, so a user can select either “Yes” or “No”, but not both.
This can be achieved by inspecting the $page->voted
array, which stores the voting state of each voting context. If a voting context exists and has its value set to true
, then it means that a vote on that context has been made by the current user:
<div class="votes">
<?= self::form('vote', [
'active' => empty($page->voted['n']),
'key' => 'y',
'title' => 'Yes <span role="status">%d</span>'
]); ?>
<?= self::form('vote', [
'active' => empty($page->voted['y']),
'key' => 'n',
'title' => 'No <span role="status">%d</span>'
]); ?>
</div>
Voting Group
Dot character has a special purpose in the key
value, so you should not include it as part of the key
value. It is used to determine a deep context which allows you to store the vote data in a specific group. The following is an example of a vote feature with scale options:
<h3>
<?= i('On a scale of 0 to 5, how severe is your pain?'); ?>
</h3>
<div class="votes">
<?php foreach ([0, 1, 2, 3, 4, 5] as $v): ?>
<?= self::form('vote', [
'active' => empty($page->voted['level']) || !empty($page->voted['level'][$v]),
'description' => empty($page->voted['level'][$v]) ? i('Select this level') : i('Cancel'),
'key' => 'level.' . $v,
'title' => $v . ' <span role="status">%d</span>',
]); ?>
<?php endforeach; ?>
</div>
0 Comments
No comments yet.