How to Add a custom button to grab data from database

English! place to talk about development, programming and coding
Post Reply
sunylkumar
Znuny newbie
Posts: 16
Joined: 16 Jul 2011, 02:19
Znuny Version: 3.0
Real Name: Sunyl
Company: DDC

How to Add a custom button to grab data from database

Post by sunylkumar »

Hi, I am new to OTRS

I want to create a custom button on a page that calls perl module which in-turn will connect to the database grabbing required data formatting it into a table and displaying on the fronted.

So far I managed to connect to the database and collect the data in a perl module but not able find a way to trigger that function on click of a button.

Can somebody help me with this?
Alchemy - Alchemy - OTRS - Google Chrome_2011-07-15_17-50-07.png
I have these files to play with:

otrs\Kernel\System\Hello.pm
otrs\Kernel\Modules\AgentHello.pm
otrs\Kernel\Config\Files\Hello.xml
otrs\bin\cgi-bin\Hello.pl
otrs\Kernel\Output\HTML\Standard\AgentHello.dtl
You do not have the required permissions to view the files attached to this post.
jojo
Znuny guru
Posts: 15019
Joined: 26 Jan 2007, 14:50
Znuny Version: Git Master
Contact:

Re: How to Add a custom button to grab data from database

Post by jojo »

I'll move this thread to Development Forum
"Production": OTRS™ 8, OTRS™ 7, STORM powered by OTRS
"Testing": ((OTRS Community Edition)) and git Master

Never change Defaults.pm! :: Blog
Professional Services:: http://www.otrs.com :: enjoy@otrs.com
MichaelR
Znuny expert
Posts: 250
Joined: 12 Oct 2010, 01:35
Znuny Version: 3.0.9
Company: LRS Health

Re: How to Add a custom button to grab data from database

Post by MichaelR »

Without diving too deeply into it myself, look at how the process is handled in say AgentTicketPhoneNew.pm/.dtl?
OTRS: 3.0.9 & ITSM 3.0.4 - OS: Windows 7 - DB: MySQL - Heaps of random/useful hacks :)
[Major Code Changes]
ArticleFreeTime1-3
Ability to search ArticleFreeText
sunylkumar
Znuny newbie
Posts: 16
Joined: 16 Jul 2011, 02:19
Znuny Version: 3.0
Real Name: Sunyl
Company: DDC

Re: How to Add a custom button to grab data from database

Post by sunylkumar »

Thanks MichaelR ,

The only piece I am missing here is how do I make the click/submit button action to be able to listened by the perl module where I have the database connection ( Kernel/System/Hello.pm ). I trying hard to understand the framework but no clue what specifically I should be looking into.

My requirement is when I click the button I should be able send the string in the text box to back end module which in turn will fire a query to the database sending result back to the webpage. I covered the displaying result part (happens on page refresh right now) but I don't know how to make it work with button click.

I would appreciate if you could be more specific on this.
MichaelR
Znuny expert
Posts: 250
Joined: 12 Oct 2010, 01:35
Znuny Version: 3.0.9
Company: LRS Health

Re: How to Add a custom button to grab data from database

Post by MichaelR »

Well in the AgentTicketPhoneNew.dtl screen all the entered parameters are passed to AgentTicketPhoneNew.pm, where you will create an 'Object' of 'Kernel/System/Hello.pm' and call methods from that object.

Follow the process in AgentTicketPhoneNew.pm and you should be able to replicate it into your own module.

Good Luck!
OTRS: 3.0.9 & ITSM 3.0.4 - OS: Windows 7 - DB: MySQL - Heaps of random/useful hacks :)
[Major Code Changes]
ArticleFreeTime1-3
Ability to search ArticleFreeText
sunylkumar
Znuny newbie
Posts: 16
Joined: 16 Jul 2011, 02:19
Znuny Version: 3.0
Real Name: Sunyl
Company: DDC

Re: How to Add a custom button to grab data from database

Post by sunylkumar »

Thanks MichaelR ,

Got it working :)

Cheers!
Andre Bauer
Znuny guru
Posts: 2189
Joined: 08 Dec 2005, 17:01
Znuny Version: 5.0.x
Real Name: André Bauer
Company: Magix Software GmbH
Location: Dresden
Contact:

Re: How to Add a custom button to grab data from database

Post by Andre Bauer »

Would be nice you could share you solution ;-)
Prod: Ubuntu Server 16.04 / Zammad 1.2

DO NOT PM ME WITH OTRS RELATED QUESTIONS! ASK IN THE FORUMS!

OtterHub.org
sunylkumar
Znuny newbie
Posts: 16
Joined: 16 Jul 2011, 02:19
Znuny Version: 3.0
Real Name: Sunyl
Company: DDC

Re: How to Add a custom button to grab data from database

Post by sunylkumar »

Here is what I did,

The basic structure to construct these files can be found in OTRS manual at http://doc.otrs.org/developer/2.2/en/html/ch13.html

There are mainly 4 files in play to create your own customized page/module.
otrs/Kernel/output/HTML/standard/AgentHello.dtl - This is basically your html page which you see on front end

otrs/Kernel/Config/Files/Hello.xml - Here you will register you AgentHello.pm module which gets called when you do form submit CGI stuff on your front end

otr/Kernel/Systems/Hello.pm - This is the module that has methods/functions which you can call from you AgentHello.pm module

otr/Kernel/Systems/AgentHello.pm - This is the registered module that is associated with your frontend , kind of action listener and can call methods from any module you want in this case Hello.pm by using use Kernel::System::Hello

I was required to create a custom submit button when a perticular id is entered which would grab data from database (other than otrs) and display it on frontend.

Now, all I needed was this in AgentHello.dtl create

<form action="$Env{"CGIHandle"}" method="post" enctype="multipart/form-data" id="SearchVenueId">
<input type="hidden" name="Action" value="$Env{"Action"}"/>
<input type="text" id="venueid" name="venueid" class="W50pc" />
<button type="button" onclick="$('#SearchVenueId').submit()" value="$Text{"Search"}">$Text{"Search"}</button>
</form>

On click of submit button the form fields are passed onto AgentHello.pm where you can collect each field values by using:
$ID= $Self->{ParamObject}->GetParam( Param => 'venueid' ); Note : venueid here is element name and not id

the collected value can then be passed to Hello.pm by using:

$Data{HelloText} = $Self->{HelloObject}->GetHelloText($ID); Note GetHelloText is the method name in Hello.pm

(I setup database configuration to point to a different database in Hello.pm )

Once this is done, you can then simple query the database by using:

my ($Id) = @_; Note : $Id gets the parameters passed from AgentHello.pm you can also collect multiple parameters in array by using my (@Id) = @_;

if ($DBObject->Connect()){

if ($DBObject->Prepare(
SQL => "SELECT * FROM table_name WHERE id =?",
Bind => [ \$Id ],
)

#get the result in

while ( my @Row = $DBObject->FetchrowArray() ) {

$resultStr .=$Row[1];

}
return $resultStr;

receive this $resultStr data in AgentHello.pm at $Data{HelloText} = $Self->{HelloObject}->GetHelloText($ID);

output that to AgentHello.dtl by using:

$Output .= $Self->{LayoutObject}->Output(
Data => \%Data,
TemplateFile => 'AgentHello',
);

In AgentHello.dtl receive the data in $Text{"$QData{"HelloText"}"}

Here on you can play/display the data in whatever way you want ...




Hope that saves some time for those who are trying to achieve similar things.

-
Sunyl
Post Reply