Lineup extension

Discussion forum for Extension Writers regarding Extension Development.
Bizkaitarra
Registered User
Posts: 2
Joined: Tue Sep 29, 2009 8:47 am

Lineup extension

Post by Bizkaitarra »

Hi,

I'm learning how to work with PHPBB extensions.

I'm developing an extension that recives players of a team and a tactic and should display de lineup of that team in the correct format.

Example of usage:

Code: Select all

[lineup tactica=443 alineacion=Unai Simón, Ander Capa, Yeray Álvarez, Iñigo Martínez, Mikel Balenziaga, Dani García, Unai López, Alex Berenguer, Iker Muniain, Iñaki Williams, Raúl García][/lineup]
I want that when I use that code the extension displays these html

Code: Select all

<div class="lineup"><div class="defenders">Unai Simón, Ander Capa, Yeray Álvarez, Iñigo Martínez</div><div class="midfielders"> Mikel Balenziaga, Dani García, Unai López, Alex Berenguer</div><div class="forwards"> Iker Muniain, Iñaki Williams, Raúl García</div></div>
This is done by these listener that is already working:

Code: Select all

<?php

namespace bizkaitarra\lineup\event;

use s9e\TextFormatter\Configurator;
use s9e\TextFormatter\Configurator\Items\Template;
use s9e\TextFormatter\Parser\Tag;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class listener implements EventSubscriberInterface
{
    public static function getSubscribedEvents()
    {
        return [
            'core.text_formatter_s9e_configure_after' => 'configure_lineup'
        ];
    }

    public function configure_lineup($event)
    {
        /** @var Configurator $configurator */
        $configurator = $event['configurator'];

        $template = new Template('<div class="lineup1" data-tactica="{@tactica}">{@alineacion}</div>');
        $template->isNormalized(true);
        $configurator->BBCodes->addCustom(
            '[lineup tactica={NUMBER} alineacion={TEXT}][/lineup]',
            $template
        );

        /** @var Tag $lineupTag */
        $lineupTag = $configurator->tags['lineup'];
        $lineupTag->filterChain->append(__CLASS__ . '::filter_lineup');
    }

    public static function filter_lineup(Tag $tag)
    {
        $tactica = $tag->getAttribute('tactica');
        $alineacion = $tag->getAttribute('alineacion');

        $jugadores = explode(",", trim($alineacion));
        
        $output = self::generate_lineup($tactica, $jugadores);

        // Para almacenar el contenido generado
        $tag->setAttribute('alineacion', $output);

        return true;
    }

    private static function generate_lineup($tactica, $jugadores)
    {
        // Procesa los jugadores y distribúyelos según la táctica
        $html = '<div class="lineup">';
        // Ejemplo simple de procesamiento basado en la táctica
        // Necesitarás implementar el procesamiento adecuado según la táctica
        $html .= '<div class="defenders">' . implode(', ', array_slice($jugadores, 0, 4)) . '</div>';
        $html .= '<div class="midfielders">' . implode(', ', array_slice($jugadores, 4, 4)) . '</div>';
        $html .= '<div class="forwards">' . implode(', ', array_slice($jugadores, 8, 3)) . '</div>';
        $html .= '</div>';

        return $html;
    }
}

The problem is that the HTML is not interpreted, is writen as if it was an string in the forum message.

How could I make these to be interpreted as HTML?
Bizkaitarra
Registered User
Posts: 2
Joined: Tue Sep 29, 2009 8:47 am

Re: Lineup extension

Post by Bizkaitarra »

I have done a workaround.

I have put muy players into tags like these:

Code: Select all

<?php
public static function filter_lineup(Tag $tag)
    {
        $tactica = $tag->getAttribute('tactica');
        $alineacion = $tag->getAttribute('alineacion');

        $jugadores = explode(",", trim($alineacion));
        if (count($jugadores) !== 11) {
            return false;
        }
        switch ($tactica) {
            case 541:
                $tag->setAttribute('goalkeeper', implode(',',array_slice($jugadores, 0, 1)));
                $tag->setAttribute('defenders', implode(',',array_slice($jugadores, 1, 5)));
                $tag->setAttribute('midfielders', implode(',',array_slice($jugadores, 6, 4)));
                $tag->setAttribute('forwards', implode(',',array_slice($jugadores, 10, 1)));
                break;
            case 442:
                $tag->setAttribute('goalkeeper', implode(',',array_slice($jugadores, 0, 1)));
                $tag->setAttribute('defenders', implode(',',array_slice($jugadores, 1, 4)));
                $tag->setAttribute('midfielders', implode(',',array_slice($jugadores, 5, 4)));
                $tag->setAttribute('forwards', implode(',',array_slice($jugadores, 9, 2)));
                break;
            case 433:
                $tag->setAttribute('goalkeeper', implode(',',array_slice($jugadores, 0, 1)));
                $tag->setAttribute('defenders', implode(',',array_slice($jugadores, 1, 4)));
                $tag->setAttribute('midfielders', implode(',',array_slice($jugadores, 5, 3)));
                $tag->setAttribute('forwards', implode(',',array_slice($jugadores, 8, 3)));
                break;
        }

        return true;
    }
And them in the tag template:

Code: Select all

$configurator->BBCodes->addCustom(
            '[lineup tactica={NUMBER} alineacion={TEXT}][/lineup]',
            '
                    <div class="field" style="'.$fieldClass.'">
                        <div class="background" style="'.$backgroundClass.'"></div> <!-- Capa de fondo con opacidad -->

                        <div class="formation" style="'.$formationClass.'">
                            <div class="line goalkeeper" style="'.$goalkeeperClass.'">{@goalkeeper}</div>
                            <div class="line defenders" style="'.$defendersClass.'">{@defenders}</div>
                            <div class="line midfielders" style="'.$midfieldersClass.'">{@midfielders}</div>
                            <div class="line forwards">{@forwards}</div>
                        </div>
                    </div>
                    
                    '
        );
deninho32
Registered User
Posts: 227
Joined: Tue May 21, 2019 8:57 am

Re: Lineup extension

Post by deninho32 »

Looking forward to this extension!
phpBB 3.3.7 | PHP Version 7.4.33 | Milk Theme

Return to “Extension Writers Discussion”