Plugins

Per sviluppare dei plugins che aggiungano funzionalità ad Adon, leggere questa sezione.

Questo è un esempio di plugin che aggiunge ad Adon funzionalità di generazione di stampa, pdf e invia-ad-un-amico.

package Kernel::Adon::Plugins::Pdf;
use strict;
use warnings;
use HTTP::Request::Common qw(GET);
use LWP::UserAgent;

sub Content {
  my $Self = shift;
  my %Param = @_;
  my $Values = $Param{Values};
  my $dbh = $Self->{dbh};
  my $cgi = $Self->{cgi};
  my $doc_url = $cgi->param('loc') || $cgi->param('url') || $ENV{HTTP_REFERER};
  my $content = '';
  my $pdf_header = '';
  my $pdf_footer = '';
  my $prn_css = $Self->{CommonObject}->{ConfigObject}->Get('PrnCss') || '';
  my $main_act = $Self->{act};
  my $mode = $cgi->param('Mode');
  my $content = '';
  if ($doc_url =~ /adon\.pl\?(.*)/) {
    my $old_env = Kernel::System::Template::CreateCgiEnv($Self, $1 || '');
    my $act = $cgi->param('act');
    if ($act) {
      my $plugins = $Self->{CommonObject}->{ConfigObject}->Get('Plugins');
      my $plugin;
      my $_act = $act;
      if ($plugins) {
         my $definitions = $plugins->{Definitions};
         if ($definitions->{$act}) {
           my $mode = $cgi->param('Mode');
           $plugin = $act;
           $_act .= "::$mode";
           $act = $mode;
        }
      }
      my $Adon = Kernel::System::Adon->new(%{$Self->{CommonObject}}, Plugin => $plugin, Included => 1);
      if (!$Adon) {
        print "ERRORE CREAZIONE OGGETTO ADON\n";
      }
      my $Values;
      $Values->{return} = 1;
      $Values->{UserData} = $Adon->{UserData};
      $content = $Adon->$act(Values => $Values);
    }
    Kernel::System::Template::RestoreCgiEnv($Self, $old_env);
  } else {
    my $req=HTTP::Request::Common::GET ($doc_url) || die "$!";
    my $ua = LWP::UserAgent->new;
    my $res = $ua->request($req);
    my $response = $ua->request($req);
    $content = $response->content || '';
  }
  my $url = $ENV{SERVER_NAME};
  $content =~ s/\r/ /g;
  $content =~ s/\n/###LINE_END###/g;
  $content =~ s/.*?//gi;
  $content =~ s//\n/i if $prn_css;
  $content =~ s/###LINE_END###/\n/g;
  $content =~ s/src=" +/src="/g;
  $content =~ s/background=" +/background="/g;
  $content =~ s/src="(.*)" mce_src="(.*)"/src="http:\/\/$url\/$1" mce_src="http:\/\/$url\/$1"/g;
  $content =~ s/background="(.*)"/background="http:\/$url\/$1"/g;
  return $content;
}

sub Prn {
  my $content = &Content(@_);
  print $content;
}

sub Pdf {
  my $content = &Content(@_);
  my $content_disposition = '';
  if ( $ENV{'HTTP_USER_AGENT'}=~/MSIE 5.5/ ) {
    $content_disposition = "\nContent-Disposition: filename=\"documento$$.pdf\""
  } else {
    $content_disposition = "\nContent-Disposition: attachment; filename=\"documento$$.pdf\""
  }
  print "Content-Type: application/pdf$content_disposition\n\n";
  use HTML::HTMLDoc;
  my $htmldoc = new HTML::HTMLDoc();
  $htmldoc->set_html_content($content);
  my $pdfh = $htmldoc->generate_pdf();
  my $pdf = $pdfh->to_string();
  if ($pdf) {
    print $pdf;
  } else {
    print "HTML NON CORRETTO ALL'INDIRIZZO";
  }
}

1;


Il plugin comincia con la dichiarazione del pacchetto nella classe Kernel::Adon::Plugins
All'interno del pacchetto del plugin è possibile accedere a tutte le classi e tutti i metodi nel pacchetto di Adon ed è possibile utilizzare muduli e programmi perl addizionali.
Tutti i metodi ricevono dall'ambiente Adon un parametro (il primo) che eredita tutte le variabili e tutti i metodi nei pacchetti Adon.


my $Self = shift;
The names space is inherited from the hash pointer with the label Values
  my %Param = @_;
  my $Values = $Param{Values};
Here is an example how to retrieve from Adon environment the handler to the db connections and to cgi environment received from the web server.
  my $dbh = $Self->{dbh};
  my $cgi = $Self->{cgi};

A plugin must be saved in configured in Config.pm in the section Plugins
    $Self->{Plugins} = {
       PreActionsPlugins => {
         'Login' => 'ExtendedLogin::Post',
         'doc' => 'CheckDocPerm::Verify',
       },
        Definitions => {
          'Pdf' => {
            File => 'Kernel::Adon::Plugins::Pdf',
            Background => 0,
        }
    };


Per accedere ai metodi in un plugin è necessario utilizzare il nome del pacchetto come parametro "act" e il nome del metodo come parametro "Mode".

Ecco un esempio (riferito al plugin Pdf nell'esempio):
http://mydomain.com/adon.pl?act=Pdf&Mode=Prn&url=%2Findex.html