-
Notifications
You must be signed in to change notification settings - Fork 8
Custom triggers
Steve Guidetti edited this page Sep 26, 2017
·
2 revisions
The Profile Flair allows the addition of custom auto-assignment triggers. All it takes is a listener to the stevotvr.flair.load_triggers
to add the name of your event and one or two language strings for the ACP form field.
To use a trigger, inject the @stevotvr.flair.operator.trigger
service and make a call to the dispatch($user_id, $trigger_name, $trigger_value)
method at an appropriate place.
/**
* Dispatch a trigger for a user.
*
* @param int $user_id The user ID
* @param string $trigger_name The trigger name
* @param int $trigger_value The trigger value
*/
public function dispatch($user_id, $trigger_name, $trigger_value);
The sample below demonstrates how to add a custom trigger named test_trigger
.
services:
test.trigger.listener:
class: test\trigger\event\main_listener
arguments:
- '@dbal.conn'
- '@language'
- '@stevotvr.flair.operator.trigger'
tags:
- { name: event.listener }
<?php
/**
*
* Trigger Sample. An extension for the phpBB Forum Software package.
*
* @copyright (c) 2017, Steve Guidetti, https://github.com/stevotvr
* @license GNU General Public License, version 2 (GPL-2.0)
*
*/
namespace test\trigger\event;
use phpbb\db\driver\driver_interface;
use phpbb\event\data;
use phpbb\language\language;
use stevotvr\flair\operator\trigger_interface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Trigger Sample event listener.
*/
class main_listener implements EventSubscriberInterface
{
/**
* @var \phpbb\db\driver\driver_interface
*/
protected $db;
/**
* @var \phpbb\language\language
*/
protected $language;
/**
* @var \stevotvr\flair\operator\trigger_interface
*/
protected $trigger_operator;
/**
* @param \phpbb\db\driver\driver_interface $db
* @param \phpbb\language\language $language
* @param \stevotvr\flair\operator\trigger_interface $trigger_operator
*/
public function __construct(driver_interface $db, language $language, trigger_interface $trigger_operator)
{
$this->db = $db;
$this->language = $language;
$this->trigger_operator = $trigger_operator;
}
static public function getSubscribedEvents()
{
return array(
'stevotvr.flair.load_triggers' => 'load_triggers',
'core.submit_post_end' => 'submit_post_end',
);
}
/**
* Add the custom trigger and load language strings.
*
* @param \phpbb\event\data $event The event data
*/
public function load_triggers(data $event)
{
// Add test_trigger to the list of trigger names
$trigger_names = $event['trigger_names'];
$trigger_names[] = 'test_trigger';
$event['trigger_names'] = $trigger_names;
// Add our language file containing out trigger title and description
$this->language->add_lang('trigger', 'test/trigger');
}
/**
* Dispatch the test_trigger trigger with the users post count. The test_trigger has the same
* functionality as the built-in post_count trigger, which gives an item to users with a
* minimum number of posts.
*
* @param \phpbb\event\data $event The event data
*/
public function submit_post_end(data $event)
{
// Query the user post count from the users table
$user_id = $event['data']['poster_id'];
$sql = 'SELECT user_posts
FROM ' . USERS_TABLE . '
WHERE user_id = ' . (int) $user_id;
$result = $this->db->sql_query($sql);
$post_count = (int) $this->db->sql_fetchfield('user_posts');
/**
* Dispatch a trigger for a user.
*
* @param int $user_id The user ID
* @param string $trigger_name The trigger name
* @param int $trigger_value The trigger value
*/
$this->trigger_operator->dispatch($user_id, 'test_trigger', $post_count);
$this->db->sql_freeresult($result);
}
}
<?php
/**
*
* Trigger Sample. An extension for the phpBB Forum Software package.
*
* @copyright (c) 2017, Steve Guidetti, https://github.com/stevotvr
* @license GNU General Public License, version 2 (GPL-2.0)
*
*/
/**
* DO NOT CHANGE
*/
if (!defined('IN_PHPBB'))
{
exit;
}
if (empty($lang) || !is_array($lang))
{
$lang = array();
}
$lang = array_merge($lang, array(
// The label for the ACP field (ACP_FLAIR_TRIGGER_{TRIGGER_NAME})
'ACP_FLAIR_TRIGGER_TEST_TRIGGER' => 'Sample trigger',
// Optional description for the field (ACP_FLAIR_TRIGGER_{TRIGGER_NAME}_EXPLAIN)
'ACP_FLAIR_TRIGGER_TEST_TRIGGER_EXPLAIN' => 'This is the description of the sample trigger.',
));