Thank youmarcovo wrote:@parshavok: I will look into this.
Code: Select all
<?php
/**
*
* @package Hide_BBcode
* @copyright (c) 2015 Marco van Oort
* @license http://opensource.org/licenses/gpl-license.php GNU Public License v2
*
*/
namespace marcovo\hide_bbcode\event;
/**
* @ignore
*/
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Event listener
*/
class listener implements EventSubscriberInterface
{
protected $db;
protected $user;
protected $template;
protected $b_hide = true;
/**
* Constructor
*
* @param \phpbb\db\driver\driver $db Database object
* @param \phpbb\controller\helper $helper Controller helper object
*/
public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\user $user, \phpbb\template\template $template)
{
$this->db = $db;
$this->user = $user;
$this->template = $template;
}
static public function getSubscribedEvents()
{
return array(
'core.user_setup' => 'load_language_on_setup',
// 'core.modify_posting_parameters' => 'check_user_is_trusted',
'core.modify_text_for_display_after' => 'parse_bbcodes_after',
'core.modify_format_display_text_after' => 'parse_bbcodes_topicPreview',
// 'core.parse_attachments_modify_template_data' => 'check_attachment',
'test.topic_review_modify_template_vars' => 'topic_review_modify_template_vars',
'core.posting_modify_template_vars' => 'posting_modify_template_vars',
);
}
/**
* Load common files during user setup
*
* @param object $event The event object
* @return null
* @access public
*/
public function load_language_on_setup($event)
{
$lang_set_ext = $event['lang_set_ext'];
$lang_set_ext[] = array(
'ext_name' => 'marcovo/hide_bbcode',
'lang_set' => 'hide_bbcode',
);
$event['lang_set_ext'] = $lang_set_ext;
$this->check_user_is_trusted();
}
/**
* Alter BBCodes after they are processed by phpBB
*
* @param object $event The event object
*/
public function topic_review_modify_template_vars($event)
{
if ($this->b_hide)
{
$event['decoded_message'] = preg_replace("#\[hide\].*?\[/hide\]#is", '{{'.$this->user->lang('HIDEBB_HIDDEN_MESSAGE')."}}\n", $event['decoded_message']);
}
}
/**
* Alter BBCodes after they are processed by phpBB
*
* @param object $event The event object
*/
public function posting_modify_template_vars($event)
{
if ($this->b_hide)
{
$page_data = $event['page_data'];
$page_data['MESSAGE'] = preg_replace("#\[hide\].*?\[/hide\]#is", '{{'.$this->user->lang('HIDEBB_HIDDEN_MESSAGE')."}}\n", $page_data['MESSAGE']);
$event['page_data'] = $page_data;
}
}
/**
* Alter BBCodes after they are processed by phpBB
*
* @param object $event The event object
*/
private function check_user_is_trusted()
{
// Check if the is in the Trusted group
$sql = "SELECT group_id FROM " . USER_GROUP_TABLE . " WHERE group_id = 8 AND user_id = " . $this->user->data['user_id'];
$result = $this->db->sql_query($sql);
$b_hide = $this->db->sql_affectedrows($result) ? false : true;
$this->db->sql_freeresult($result);
$this->b_hide = $b_hide;
}
/**
* Alter BBCodes after they are processed by phpBB
*
* @param object $event The event object
*/
/*
public function check_attachment($event)
{
if($this->b_hide)
{
$event['attachment'] = array();
$event['block_array'] = array();
$event['download_link'] = '';
}
}
*/
/**
* Alter BBCodes after they are processed by phpBB
*
* @param object $event The event object
*/
public function parse_bbcodes_after($event)
{
$event['text'] = preg_replace_callback('#<!-- HIDE_BBCODE -->(.*?)<!-- /HIDE_BBCODE -->#s', array($this, 'hidden_pass'), $event['text']);
}
/**
* Alter BBCodes after they are processed by phpBB
*
* @param object $event The event object
*/
public function parse_bbcodes_topicPreview($event)
{
$event['text'] = preg_replace_callback('#<!-- HIDE_BBCODE -->(.*?)<!-- /HIDE_BBCODE -->#s', array($this, 'hidden_pass_topicPreview'), $event['text']);
}
/**
* Convert Hidden BBCode into its final appearance
*
* @param array $matches
* @return string HTML render of hidden bbcode
*/
protected function hidden_pass($matches)
{
$this->template->set_style(array('styles', 'ext/marcovo/hide_bbcode/styles'));
$bbcode = new \bbcode();
$bbcode->template_filename = $this->template->get_source_file_for_handle('hide_bbcode.html');
if ($this->b_hide) return $bbcode->bbcode_tpl('hide');
else return $bbcode->bbcode_tpl('unhide_open') . $matches[1] . $bbcode->bbcode_tpl('unhide_close');
}
/**
* Convert Hidden BBCode into its final appearance
*
* @param array $matches
* @return string HTML render of hidden bbcode
*/
protected function hidden_pass_topicPreview($matches)
{
$this->template->set_style(array('styles', 'ext/marcovo/hide_bbcode/styles'));
$bbcode = new \bbcode();
$bbcode->template_filename = $this->template->get_source_file_for_handle('hide_bbcode.html');
return $bbcode->bbcode_tpl('unhide_open') . $matches[1] . $bbcode->bbcode_tpl('unhide_close');
}
}
This actually is an extension.Sepp71 wrote:I am interested in the hide-function, too but I would like to have it implemented the official way for extensions (without having to change the code), because I have sworn to myself keep my source-code clean at 3.1 (I manage several boards on different URLs and want don't want to mix it all up as I did with 3.0.
So for the moment just watching this...
If this is still necessary it means a change to the original files (so not just adding and installing the extension), doesn't it?The event above is not used any more in this extension. Instead, the following two edits will have to be done to make everything work correctly.
a month has gonemarcovo wrote:With phpBB 3.1.3, not all necessary events were implemented in phpBB yet, so this extension needed an extra edit in the code. In phpBB 3.1.4, all necessary events are available, but this extension still has to adapt to it. I expect I will do this within a month from now.