Quantcast
Channel: phpBB.com
Viewing all articles
Browse latest Browse all 2220

phpBB Custom Coding • Custom input box showing up at the top of a post

$
0
0
Hello People. I am not a coder, but, I am knowledgable enough to give this a go.

In my forum f=13 I would like an extra input box for "GKI" number, so that when posting a message into this forum, it looks something like this.
GKI Number
Message text... blah blah blah.
Please can some kind soul take a look and help me get it working. I am trying to save the world from CANCER here!

I asked chat GPT4 to write me the extension, and it gave me the following code.

Code:

ext/└── Vincent/    └── gki/        ├── config/        │   └── services.yml        ├── event/        │   └── main_listener.php        ├── language/        │   └── en/        │       └── common.php        ├── migrations/        │   └── v_0_1_0.php        ├── styles/        │   └── all/        │       └── template/        │           ├── posting_buttons.html        │           └── viewtopic_body_post_row_before.html        ├── composer.json        └── ext.php
Here is the code for all the files I have.

composer.json

Code:

{    "name": "Vincent/gki",    "type": "phpbb-extension",    "description": "GKI Number Extension",    "homepage": "https://www.metcancer.com",    "version": "0.1.0",    "time": "2024-06-28",    "license": "GPL-2.0",    "authors": [        {            "name": "Vincent",            "homepage": "https://www.metcancer.com"        }    ],    "require": {        "php": ">=7.1",        "phpbb/phpbb": ">=3.3.12"    },    "extra": {        "display-name": "GKI Number Extension",        "soft-require": {            "phpbb/phpbb": ">=3.3.12"        }    }}
ext.php

Code:

{    "name": "Vincent/gki",    "type": "phpbb-extension",    "description": "GKI Number Extension",    "homepage": "https://www.metcancer.com",    "version": "0.1.0",    "time": "2024-06-28",    "license": "GPL-2.0",    "authors": [        {            "name": "Vincent",            "homepage": "https://www.metcancer.com"        }    ],    "require": {        "php": ">=7.1",        "phpbb/phpbb": ">=3.3.12"    },    "extra": {        "display-name": "GKI Number Extension",        "soft-require": {            "phpbb/phpbb": ">=3.3.12"        }    }}
services.yml

Code:

services:    Vincent.gki.listener:        class: Vincent\gki\event\main_listener        arguments:            - '@request'            - '@template'            - '@user'            - '@auth'            - '@dbal.conn'            - '@config'        tags:            - { name: event.listener }
main_listener.php

Code:

<?phpnamespace Vincent\gki\event;use Symfony\Component\EventDispatcher\EventSubscriberInterface;class main_listener implements EventSubscriberInterface{    protected $request;    protected $template;    protected $user;    protected $auth;    protected $db;    protected $config;    public function __construct(\phpbb\request\request_interface $request, \phpbb\template\template $template, \phpbb\user $user, \phpbb\auth\auth $auth, \phpbb\db\driver\driver_interface $db, \phpbb\config\config $config)    {        $this->request = $request;        $this->template = $template;        $this->user = $user;        $this->auth = $auth;        $this->db = $db;        $this->config = $config;    }    static public function getSubscribedEvents()    {        return array(            'core.submit_post_end' => 'add_gki_number',            'core.posting_modify_template_vars' => 'add_gki_input_field',            'core.viewtopic_modify_post_row' => 'display_gki_number',        );    }    public function add_gki_number($event)    {        $forum_id = $event['forum_id'];        if ($forum_id != 13) {            return;        }        $gki_number = $this->request->variable('gki_number', '', true);        $data = array(            'post_id' => (int) $event['data']['post_id'],            'user_id' => (int) $this->user->data['user_id'],            'gki_number' => (string) $gki_number,        );        $sql = 'INSERT INTO ' . $this->db->sql_escape('phpbb_gki_numbers') . ' ' . $this->db->sql_build_array('INSERT', $data);        $this->db->sql_query($sql);    }    public function add_gki_input_field($event)    {        $forum_id = $event['forum_id'];        if ($forum_id == 13) {            $this->template->assign_vars(array(                'S_GKI_NUMBER_INPUT' => true,            ));        }    }    public function display_gki_number($event)    {        $forum_id = $event['forum_id'];        if ($forum_id == 13) {            $post_id = $event['post_row']['POST_ID'];            $sql = 'SELECT gki_number FROM ' . $this->db->sql_escape('phpbb_gki_numbers') . ' WHERE post_id = ' . (int) $post_id;            $result = $this->db->sql_query($sql);            $gki_number = $this->db->sql_fetchfield('gki_number');            $this->db->sql_freeresult($result);            $event['post_row']['GKI_NUMBER'] = $gki_number;        }    }}
posting_buttons.html

Code:

<!-- IF S_GKI_NUMBER_INPUT --><div class="panel">    <div class="inner">        <fieldset class="fields1">            <dl>                <dt><label for="gki_number">{L_GKI_NUMBER}:</label></dt>                <dd><input type="text" name="gki_number" id="gki_number" size="25" maxlength="100" value="" class="inputbox autowidth" /></dd>            </dl>        </fieldset>    </div></div><!-- ENDIF -->
viewtopic_body_post_row_before.html

Code:

<!-- IF GKI_NUMBER --><div class="gki_number">GKI Number: {GKI_NUMBER}</div><!-- ENDIF -->
common.php

Code:

<?phpif (!defined('IN_PHPBB')) {    exit;}$lang = array_merge($lang, array(    'GKI_NUMBER' => 'GKI Number',));
v_0_1_0.php

Code:

<?phpnamespace Vincent\gki\migrations;class v_0_1_0 extends \phpbb\db\migration\migration{    public function effectively_installed()    {        return isset($this->config['gki_extension_version']) && version_compare($this->config['gki_extension_version'], '0.1.0', '>=');    }    static public function depends_on()    {        return array('\phpbb\db\migration\data\v31x\v310');    }    public function update_schema()    {        return array(            'add_tables' => array(                $this->table_prefix . 'gki_numbers' => array(                    'COLUMNS' => array(                        'post_id' => array('UINT', 0),                        'user_id' => array('UINT', 0),                        'gki_number' => array('VCHAR:255', ''),                    ),                    'PRIMARY_KEY' => 'post_id',                ),            ),        );    }    public function revert_schema()    {        return array(            'drop_tables' => array(                $this->table_prefix . 'gki_numbers',            ),        );    }    public function update_data()    {        return array(            array('config.add', array('gki_extension_version', '0.1.0')),        );    }}

Statistics: Posted by cancertalk — Fri Jun 28, 2024 10:24 am



Viewing all articles
Browse latest Browse all 2220

Trending Articles