How to Capturing the topic_id variable inside a form action (controller with route)? [resolved]

Discussion forum for Extension Writers regarding Extension Development.
User avatar
caiocald
Registered User
Posts: 60
Joined: Mon Feb 26, 2018 9:32 pm
Location: Brasil
Name: B!
Contact:

How to Capturing the topic_id variable inside a form action (controller with route)? [resolved]

Post by caiocald » Mon Dec 17, 2018 10:04 am

After these last 10 months studying php I think I finally got the logic behind in programming.

Unfortunately I am not a programmer and my college is from a completely different area. So all my knowledge on the subject is still very limited.

So I decided to redo the topic on my extension project. Now I think I already know what to ask to be able to actually remedy my doubt.



Come on,
As some already know, I'm on the journey to create my first phpbb extension. It's not being easy, so I ask for your help.

My extension will basically be a punctuation tool for essays. Each topic created by the user (previously determined), will represent a text to be evaluated.

Currently, I've only been able to configure migration and template events.

This is my extension project in partial operation:
https://goo.gl/i9qtVT

link to the project repository:
https://goo.gl/jtQbnm

I think that's all I could put of information. Thank you guys :D !!!
Last edited by caiocald on Sat Dec 22, 2018 2:49 pm, edited 4 times in total.

User avatar
caiocald
Registered User
Posts: 60
Joined: Mon Feb 26, 2018 9:32 pm
Location: Brasil
Name: B!
Contact:

Re: Help with the adaptation of a tool in php for phpbb extension

Post by caiocald » Mon Dec 17, 2018 10:08 am

The question that I want to do at the moment and that does not let me advance in the studies is: where do i start within the core events functions of phpbb?

User avatar
mrgoldy
Jr. Extension Validator
Posts: 1083
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs
Contact:

Re: Help with the adaptation of a tool in php for phpbb extension

Post by mrgoldy » Mon Dec 17, 2018 12:04 pm

You would have to use an "event listener" and listen to phpBB core events.
See Tutorial: Events
And Event list
phpBB Studio / Proud member of the Studio

User avatar
caiocald
Registered User
Posts: 60
Joined: Mon Feb 26, 2018 9:32 pm
Location: Brasil
Name: B!
Contact:

Re: Help with the adaptation of a tool in php for phpbb extension

Post by caiocald » Mon Dec 17, 2018 12:25 pm

mrgoldy wrote:
Mon Dec 17, 2018 12:04 pm
You would have to use an "event listener" and listen to phpBB core events.
See Tutorial: Events
And Event list
Hi, Gijs. Thanks for the answer.

At the moment, I find myself "stuck" in this part of the tutorial. I do not know which core event to begin with.
At first, I'm trying to pull the information (manually injected) from the data pool to fill in the fields of my tool.

I need the class to get topic_id and user_id on the viewtopic page. For this, I found the most appropriate "core.viewtopic_modify_post_data" event.
But I still can not make progress.

In my normal php code, I can pull the data like this:

Code: Select all

        $consulta_nota_geral = "SELECT COUNT(*) as number_of_rows , avg(comp1) as competencia1 , avg(comp2) as competencia2 , avg(comp3) as competencia3 , avg(comp4) as competencia4 , avg(comp5) as competencia5  FROM phpbb_pontuacao_enem WHERE topic_id = '$topic_id'";
        $consulta_nota_geral_conect = mysqli_query($conn,$consulta_nota_geral) or die (mysqli_error($conn));
        $nota_geral = mysqli_fetch_assoc($consulta_nota_geral_conect);
        $numero_linhas = $nota_geral["number_of_rows"];
        round('2.57',1);
        if ($nota_geral['number_of_rows'] > 0) {
            $nota_geral_total = $nota_geral['competencia1'] + $nota_geral['competencia2'] + $nota_geral['competencia3'] + $nota_geral['competencia4'] + $nota_geral['competencia5'];
        }*/
However, I do not know how to convert this to a phpbb class.
Do you have any study tips for that? What is the name of the concept that I have to study? Any name already helps me search exactly what I need in google.

Thank you one more time

User avatar
mrgoldy
Jr. Extension Validator
Posts: 1083
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs
Contact:

Re: Help with the adaptation of a tool in php for phpbb extension

Post by mrgoldy » Mon Dec 17, 2018 12:34 pm

well, you asked on how to achieve something through events, that's why I provided those links.
However, events are when phpBB "does" something and you want to do something on that event aswell.
For example if someone makes a post, phpBB triggers multiple events which you can use to also do something with/to the post.

What you're trying to do, looks more like a separate action, not something that phpBB is already doing and you want to do something with/to.
That means you will probably have to create a route and a controller.
So you add a link to your route with the necessary information and do your stuff in the controller.
Which you can read more about here: Tutorial: Controllers and Routes

So in your case it would probably be something like a <form> with the action="" attribute set to your controller.
You request all the information from the form into your controller, do your calculations and output a page or redirect the user to where he was.

Side note, from the small piece of code you provided.
Coding in your native language might be easier for you, but if you seek help it's rather disadvantageous as nobody will know what it means.
Aswell as you're using mysqli_fetch_assoc() and such, while you should use the phpbb\db\driver\driver_interface to make sure your extension works for all database types.

Basically, before trying to make your own extension work, go through the example extension provided in the linked tutorials. That should give you a better understanding on how things work and what is possible (and what not). Or have a look at the phpBB Skeleton extension.
phpBB Studio / Proud member of the Studio

User avatar
caiocald
Registered User
Posts: 60
Joined: Mon Feb 26, 2018 9:32 pm
Location: Brasil
Name: B!
Contact:

Re: Help with the adaptation of a tool in php for phpbb extension

Post by caiocald » Mon Dec 17, 2018 12:40 pm

mrgoldy wrote:
Mon Dec 17, 2018 12:34 pm
well, you asked on how to achieve something through events, that's why I provided those links.
However, events are when phpBB "does" something and you want to do something on that event aswell.
For example if someone makes a post, phpBB triggers multiple events which you can use to also do something with/to the post.

What you're trying to do, looks more like a separate action, not something that phpBB is already doing and you want to do something with/to.
That means you will probably have to create a route and a controller.
So you add a link to your route with the necessary information and do your stuff in the controller.
Which you can read more about here: https://area51.phpbb.com/docs/dev/3.2.x ... llers.html

So in your case it would probably be something like a <form> with the action="" attribute set to your controller.
You request all the information from the form into your controller, do your calculations and output a page or redirect the user to where he was.

Side note, from the small piece of code you provided.
Coding in your native language might be easier for you, but if you seek help it's rather disadvantageous as nobody will know what it means.
Aswell as you're using mysqli_fetch_assoc() and such, while you should use the phpbb\db\driver\driver_interface to make sure your extension works for all database types.

Basically, before trying to make your own extension work, go through the example extension provided in the linked tutorials. That should give you a better understanding on how things work and what is possible (and what not). Or have a look at the phpBB Skeleton extension.
Great answer. I really was not sure whether to use the core event or create a controller.

At the moment I'm using a pre-generated file in skeleton extension. It has helped me a lot to understand.

Do you have any extension that has a function similar to the one I want so I can study it?

Will not my project need a core event?

I'm sorry, I'll put the tags of my project in English

User avatar
mrgoldy
Jr. Extension Validator
Posts: 1083
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs
Contact:

Re: Help with the adaptation of a tool in php for phpbb extension

Post by mrgoldy » Mon Dec 17, 2018 12:45 pm

Once again, I do not know your project, nor do I (or anyone else probably) have the time to dig into it for you.
And there are a lot of extensions out there, some of which are likely to do something similar to what you're after.
You will have to look for yourself through the Customisation Database or the Extensions in Development and see if something is close to what you are after.

We are here to help you with specific issues, not to write the extension for you ;).
phpBB Studio / Proud member of the Studio

User avatar
caiocald
Registered User
Posts: 60
Joined: Mon Feb 26, 2018 9:32 pm
Location: Brasil
Name: B!
Contact:

Re: Help with the adaptation of a tool in php for phpbb extension

Post by caiocald » Tue Dec 18, 2018 11:47 pm

Continuing ..

I created a controller and a php core to capture data from the database.

Code: Select all

 public function getuserandtopicid()
    {
        $sql = 'SELECT * 
                FROM ' . PONTUACAO_ENEM_TABLE . '
                WHERE user_id = ' . $this->user->data['user_id'];
        $result = $this->db->sql_query($sql);
        $rowset = $this->db->sql_fetchrowset($result);
        $user_id = $this->db->sql_fetchfield('user_id');
        $this->db->sql_freeresult($result);
        $rows = sizeof($rowset);

        return true;
    }
How can I capture the topic_id url that my form is located in?
Last edited by caiocald on Wed Dec 19, 2018 9:27 am, edited 1 time in total.

User avatar
Toxyy
Registered User
Posts: 698
Joined: Mon Oct 24, 2016 3:22 pm
Location: Namek
Contact:

Re: Help with the adaptation of a tool in php for phpbb extension

Post by Toxyy » Wed Dec 19, 2018 12:41 am

caiocald wrote:
Tue Dec 18, 2018 11:47 pm
How can I capture the topic_id to use in the WHERE function if I need to?
My own example with username:

Code: Select all

public function get_username($user_id)
{
        $sql = 'SELECT username
                FROM ' . USERS_TABLE . '
                WHERE user_id = ' . $user_id;
        $result = $this->db->sql_query($sql);
        $username = $this->db->sql_fetchfield('username');
        $this->db->sql_freeresult($result);
        unset($result);
        return $username;
}
I am a web developer/administrator, specializing in forums. If you have work you need done or are too lazy to do, pm me!

My extensions:
[3.2][BETA] Anonymous Posts || [3.2][BETA] Sticky Ad || [3.2][RC] Show User Activity ||
[3.2][DEV] User Delete Topics

User avatar
caiocald
Registered User
Posts: 60
Joined: Mon Feb 26, 2018 9:32 pm
Location: Brasil
Name: B!
Contact:

Re: Help with the adaptation of a tool in php for phpbb extension

Post by caiocald » Wed Dec 19, 2018 1:05 am

Toxyy wrote:
Wed Dec 19, 2018 12:41 am
caiocald wrote:
Tue Dec 18, 2018 11:47 pm
How can I capture the topic_id to use in the WHERE function if I need to?
My own example with username:

Code: Select all

public function get_username($user_id)
{
        $sql = 'SELECT username
                FROM ' . USERS_TABLE . '
                WHERE user_id = ' . $user_id;
        $result = $this->db->sql_query($sql);
        $username = $this->db->sql_fetchfield('username');
        $this->db->sql_freeresult($result);
        unset($result);
        return $username;
}
for the reply. I tried capturing the topic_id but I could not.

Ex.:
i can capture "User_id" with

Code: Select all

user_id = ' . $this->user->data['user_id'];
But "topic_id" does not work ...
thanks

User avatar
GanstaZ
Registered User
Posts: 804
Joined: Wed Oct 11, 2017 10:29 pm
Location: Zverse

Re: Help with the adaptation of a tool in php for phpbb extension

Post by GanstaZ » Wed Dec 19, 2018 4:12 pm

Here you can find some basic examples Database Abstraction Layer and wiki:Database. You should read or re-read everything in phpBB Development Documents.
"When answer lies in the question,.. question becomes redundant!"

User avatar
caiocald
Registered User
Posts: 60
Joined: Mon Feb 26, 2018 9:32 pm
Location: Brasil
Name: B!
Contact:

Re: Help with the adaptation of a tool in php for phpbb extension

Post by caiocald » Wed Dec 19, 2018 6:21 pm

GanstaZ wrote:
Wed Dec 19, 2018 4:12 pm
Here you can find some basic examples Database Abstraction Layer and wiki:Database. You should read or re-read everything in phpBB Development Documents.

Gantaz, thanks for the tip!
I was a bit confused by that.

To capture the topic_id of the url in which my form is located, would it be something related to $ request or $ db?

Sorry, I'm learning about phpbb.

User avatar
GanstaZ
Registered User
Posts: 804
Joined: Wed Oct 11, 2017 10:29 pm
Location: Zverse

Re: Help with the adaptation of a tool in php for phpbb extension

Post by GanstaZ » Wed Dec 19, 2018 6:53 pm

What do you mean by form? Is it forum?
Depends on what do you mean by get topic. Is it required for something or what is the purpose of getting it. The word request is already telling us, that one is requesting something and it can be combined with database or arrays or conditions and what not.
Best thing to do before starting a complex project is to write down a blueprint/sketch and use it as a base for development.
"When answer lies in the question,.. question becomes redundant!"

User avatar
caiocald
Registered User
Posts: 60
Joined: Mon Feb 26, 2018 9:32 pm
Location: Brasil
Name: B!
Contact:

Re: Help with the adaptation of a tool in php for phpbb extension

Post by caiocald » Wed Dec 19, 2018 7:00 pm

GanstaZ wrote:
Wed Dec 19, 2018 6:53 pm
What do you mean by form? Is it forum?
Depends on what do you mean by get topic. Is it required for something or what is the purpose of getting it. The word request is already telling us, that one is requesting something and it can be combined with database or arrays or conditions and what not.
Best thing to do before starting a complex project is to write down a blueprint/sketch and use it as a base for development.
Form is the form of my extension that is located in the footer of the viewtopic page
Attachments
Capturar.PNG

User avatar
caiocald
Registered User
Posts: 60
Joined: Mon Feb 26, 2018 9:32 pm
Location: Brasil
Name: B!
Contact:

Re: Help with the adaptation of a tool in php for phpbb extension

Post by caiocald » Thu Dec 20, 2018 1:12 am

mrgoldy wrote:
Mon Dec 17, 2018 12:45 pm
Once again, I do not know your project, nor do I (or anyone else probably) have the time to dig into it for you.
And there are a lot of extensions out there, some of which are likely to do something similar to what you're after.
You will have to look for yourself through the Customisation Database or the Extensions in Development and see if something is close to what you are after.

We are here to help you with specific issues, not to write the extension for you ;).
Mr. Goldy.


Please help me at least with that.
I can not capture the topic_id of the url in any way.
I've read all the tutorials I could find, but none works.(https://wiki.phpbb.com/PhpBB3.1/RFC/Request_class)

Code: Select all

 $topic_id = $this->request->variable('t',0);
or

Code: Select all

$topic_id = $this->request->is_set('t');
I'm still lay in phpbb. I am stagnant in my studies because I need to be able to capture this variable to continue building my extension.

In normal PHP I would get with a GET, but in phpbb I do not know

Code: Select all

topic_id = $_GET['t'];
There is almost no content on phpbb in my native language. This forum is my last hope.

PLEASE
Last edited by caiocald on Fri Dec 21, 2018 12:55 pm, edited 4 times in total.

Post Reply

Return to “Extension Writers Discussion”