Thanks for programming PHPBB!!
I've been using phpBB for few years, making some mod sometimes, and I'm looking to improve my PHP skills over time.
I have a 3.3.10 installation now and would like to make some changes by approaching the creation of extensions.
I would like not to directly modify phpBB's PHP files but rather make integrations so that they can be maintained over time, even with future updates.
I have a forum that interacts with a website. On the site, I've recreated and assigned the same login as the forum (IN PHPBB TRUE), so the navigation is closely linked to the forum. but it uses another db.
So far, I've modified some functions in the template to integrate the forum and the site.
like...
SQL phpbb-DB
SQL website-db
same host, different name, user and psw
However, I'd like to go further. I have some text lines in a secondary database (simply containing links) that I would like to display in the forum, particularly on the posting.php page (or specifically when someone wants to reply to a topic). I would like that, beneath the text area, there is a list of the last 15 lines from a secondary database. All of this to avoid affecting the main phpBB database.
Creating it statically in HTML is simple. You just need to modify and insert an "event" in the posting_editor_message_after.html position. The problem arises when I want to insert these 15 links as the latest ones with an SQL query:
Code: Select all
SELECT * FROM `foto` ORDER BY `foto`.`id` DESC LIMIT 15;
Nevertheless, I tried to create this PHP event (thank www and gpt for helping ), but the result is only causing the forum to crash with errors on the posting page.
Code: Select all
page listener.php
<?php
/**
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*/
namespace sebo\galleria_posting\event;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class listener implements EventSubscriberInterface
{
/** @var \phpbb\db\driver\driver_interface */
protected $db;
/** @var \phpbb\template\twig\twig */
protected $template;
/**
* Constructor for listener
*
* @param \phpbb\db\driver\driver_interface $db phpBB database connection
* @param \phpbb\template\twig\twig $template phpBB template
* @access public
*/
public function __construct(\phpbb\db\driver\mysqli\mysqli $db, \phpbb\template\twig\twig $template)
{
$this->db = $db;
$this->template = $template;
}
/**
* Assign functions defined in this class to event listeners in the core
*
* @return array
* @static
* @access public
*/
static public function getSubscribedEvents()
{
return array(
'core.posting_modify_message_text' => 'galleria_posting',
);
}
/**
* Update the template variables with data from the external database
*
* @param object $event The event object
* @return null
* @access public
*/
public function galleria_posting($event)
{
// Establish a connection to the external database (replace with your connection details)
$externalDb = new \phpbb\db\driver\mysqli\mysqli();
$externalDb->sql_connect('db_host', 'db_user', 'db_passw', 'db_name');
// Query the "foto" table in the external database
$sql = 'SELECT * FROM ' . $externalDb->sql_table('foto') . ' ORDER BY ' . $externalDb->sql_escape('foto.id') . ' DESC LIMIT 15';
$result = $externalDb->sql_query($sql);
// Fetch the data and assign it to the template
$links = array();
while ($row = $externalDb->sql_fetchrow($result))
{
// Assumi che la colonna nel risultato della query si chiami 'link'
// Se la colonna ha un nome diverso, sostituisci 'link' con il nome corretto
$links[] = $row['link'];
}
// Chiudi il risultato della query
$externalDb->sql_freeresult($result);
// Assegna i dati al template
$this->template->assign_vars(array(
'EXTERNAL_DB_LINKS' => $links,
));
}
}
Code: Select all
page services.yml
services:
sebo.galleria_posting.listener:
class: sebo\galleria_posting\event\listener
arguments:
- '@template'
tags:
- { name: event.listener }
Code: Select all
page posting_editor_message_after.html
{EXTERNAL_DB_LINKS}
Thank you very much!