"Fossies" - the Fresh Open Source Software Archive

Member "otrs-5.0.40/Kernel/System/ProcessManagement/DB/ActivityDialog.pm" (12 Dec 2019, 18234 Bytes) of package /linux/www/otrs-5.0.40.tar.bz2:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Perl source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "ActivityDialog.pm" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: rel-6_0_25_vs_rel-6_0_26.

    1 # --
    2 # Copyright (C) 2001-2019 OTRS AG, https://otrs.com/
    3 # --
    4 # This software comes with ABSOLUTELY NO WARRANTY. For details, see
    5 # the enclosed file COPYING for license information (GPL). If you
    6 # did not receive this file, see https://www.gnu.org/licenses/gpl-3.0.txt.
    7 # --
    8 
    9 package Kernel::System::ProcessManagement::DB::ActivityDialog;
   10 
   11 use strict;
   12 use warnings;
   13 
   14 use Kernel::System::VariableCheck qw(:all);
   15 
   16 our @ObjectDependencies = (
   17     'Kernel::Config',
   18     'Kernel::System::Cache',
   19     'Kernel::System::DB',
   20     'Kernel::System::Log',
   21     'Kernel::System::YAML',
   22 );
   23 
   24 =head1 NAME
   25 
   26 Kernel::System::ProcessManagement::DB::ActivityDialog
   27 
   28 =head1 SYNOPSIS
   29 
   30 Process Management DB ActivityDialog backend
   31 
   32 =head1 PUBLIC INTERFACE
   33 
   34 =over 4
   35 
   36 =cut
   37 
   38 =item new()
   39 
   40 create an object. Do not use it directly, instead use:
   41 
   42     use Kernel::System::ObjectManager;
   43     local $Kernel::OM = Kernel::System::ObjectManager->new();
   44     my $ActivityDialogObject = $Kernel::OM->Get('Kernel::System::ProcessManagement::DB::ActivityDialog');
   45 
   46 =cut
   47 
   48 sub new {
   49     my ( $Type, %Param ) = @_;
   50 
   51     # allocate new hash for object
   52     my $Self = {};
   53     bless( $Self, $Type );
   54 
   55     # get the cache TTL (in seconds)
   56     $Self->{CacheTTL} = int( $Kernel::OM->Get('Kernel::Config')->Get('Process::CacheTTL') || 3600 );
   57 
   58     # set lower if database is case sensitive
   59     $Self->{Lower} = '';
   60     if ( $Kernel::OM->Get('Kernel::System::DB')->GetDatabaseFunction('CaseSensitive') ) {
   61         $Self->{Lower} = 'LOWER';
   62     }
   63 
   64     return $Self;
   65 }
   66 
   67 =item ActivityDialogAdd()
   68 
   69 add new ActivityDialog
   70 
   71 returns the id of the created activity dialog if success or undef otherwise
   72 
   73     my $ID = $ActivityDialogObject->ActivityDialogAdd(
   74         EntityID    => 'AD1'                   # mandatory, exportable unique identifier
   75         Name        => 'NameOfActivityDialog', # mandatory
   76         Config      => $ConfigHashRef,         # mandatory, activity dialog configuration to be
   77                                                #    stored in YAML format
   78         UserID      => 123,                    # mandatory
   79     );
   80 
   81 Returns:
   82 
   83     $ID = 567;
   84 
   85 =cut
   86 
   87 sub ActivityDialogAdd {
   88     my ( $Self, %Param ) = @_;
   89 
   90     # check needed stuff
   91     for my $Key (qw(EntityID Name Config UserID)) {
   92         if ( !$Param{$Key} ) {
   93             $Kernel::OM->Get('Kernel::System::Log')->Log(
   94                 Priority => 'error',
   95                 Message  => "Need $Key!",
   96             );
   97             return;
   98         }
   99     }
  100 
  101     # get database object
  102     my $DBObject = $Kernel::OM->Get('Kernel::System::DB');
  103 
  104     # check if EntityID already exists
  105     return if !$DBObject->Prepare(
  106         SQL => "
  107             SELECT id
  108             FROM pm_activity_dialog
  109             WHERE $Self->{Lower}(entity_id) = $Self->{Lower}(?)",
  110         Bind  => [ \$Param{EntityID} ],
  111         Limit => 1,
  112     );
  113 
  114     my $EntityExists;
  115     while ( my @Data = $DBObject->FetchrowArray() ) {
  116         $EntityExists = 1;
  117     }
  118 
  119     if ($EntityExists) {
  120         $Kernel::OM->Get('Kernel::System::Log')->Log(
  121             Priority => 'error',
  122             Message  => "The EntityID:$Param{EntityID} already exists for an activity dialog!"
  123         );
  124         return;
  125     }
  126 
  127     # check config valid format (at least it must contain the description short, fields and field
  128     # order)
  129     if ( !IsHashRefWithData( $Param{Config} ) ) {
  130         $Kernel::OM->Get('Kernel::System::Log')->Log(
  131             Priority => 'error',
  132             Message  => "Config needs to be a valid Hash reference!",
  133         );
  134         return;
  135     }
  136     for my $Needed (qw(DescriptionShort Fields FieldOrder)) {
  137         if ( !$Param{Config}->{$Needed} ) {
  138             $Kernel::OM->Get('Kernel::System::Log')->Log(
  139                 Priority => 'error',
  140                 Message  => "Need $Needed in Config!",
  141             );
  142             return;
  143         }
  144     }
  145 
  146     # check config formats
  147     if ( ref $Param{Config}->{Fields} ne 'HASH' ) {
  148         $Kernel::OM->Get('Kernel::System::Log')->Log(
  149             Priority => 'error',
  150             Message  => "Config Fields must be a Hash!",
  151         );
  152         return;
  153     }
  154     if ( ref $Param{Config}->{FieldOrder} ne 'ARRAY' ) {
  155         $Kernel::OM->Get('Kernel::System::Log')->Log(
  156             Priority => 'error',
  157             Message  => "Config Fields must be an Array!",
  158         );
  159         return;
  160     }
  161 
  162     # dump layout and config as string
  163     my $Config = $Kernel::OM->Get('Kernel::System::YAML')->Dump( Data => $Param{Config} );
  164 
  165     # Make sure the resulting string has the UTF-8 flag. YAML only sets it if
  166     #   part of the data already had it.
  167     utf8::upgrade($Config);
  168 
  169     # sql
  170     return if !$DBObject->Do(
  171         SQL => '
  172             INSERT INTO pm_activity_dialog ( entity_id, name, config, create_time,
  173                 create_by, change_time, change_by )
  174             VALUES (?, ?, ?, current_timestamp, ?, current_timestamp, ?)',
  175         Bind => [
  176             \$Param{EntityID}, \$Param{Name}, \$Config, \$Param{UserID}, \$Param{UserID},
  177         ],
  178     );
  179 
  180     return if !$DBObject->Prepare(
  181         SQL  => 'SELECT id FROM pm_activity_dialog WHERE entity_id = ?',
  182         Bind => [ \$Param{EntityID} ],
  183     );
  184 
  185     my $ID;
  186     while ( my @Row = $DBObject->FetchrowArray() ) {
  187         $ID = $Row[0];
  188     }
  189 
  190     # delete cache
  191     $Kernel::OM->Get('Kernel::System::Cache')->CleanUp(
  192         Type => 'ProcessManagement_ActivityDialog',
  193     );
  194 
  195     return if !$ID;
  196 
  197     return $ID;
  198 }
  199 
  200 =item ActivityDialogDelete()
  201 
  202 delete an ActivityDialog
  203 
  204 returns 1 if success or undef otherwise
  205 
  206     my $Success = $ActivityDialogObject->ActivityDialogDelete(
  207         ID      => 123,
  208         UserID  => 123,
  209     );
  210 
  211 =cut
  212 
  213 sub ActivityDialogDelete {
  214     my ( $Self, %Param ) = @_;
  215 
  216     # check needed stuff
  217     for my $Key (qw(ID UserID)) {
  218         if ( !$Param{$Key} ) {
  219             $Kernel::OM->Get('Kernel::System::Log')->Log(
  220                 Priority => 'error',
  221                 Message  => "Need $Key!"
  222             );
  223             return;
  224         }
  225     }
  226 
  227     # check if exists
  228     my $ActivityDialog = $Self->ActivityDialogGet(
  229         ID     => $Param{ID},
  230         UserID => 1,
  231     );
  232     return if !IsHashRefWithData($ActivityDialog);
  233 
  234     # delete activity dialog
  235     return if !$Kernel::OM->Get('Kernel::System::DB')->Do(
  236         SQL  => 'DELETE FROM pm_activity_dialog WHERE id = ?',
  237         Bind => [ \$Param{ID} ],
  238     );
  239 
  240     # delete cache
  241     $Kernel::OM->Get('Kernel::System::Cache')->CleanUp(
  242         Type => 'ProcessManagement_ActivityDialog',
  243     );
  244 
  245     return 1;
  246 }
  247 
  248 =item ActivityDialogGet()
  249 
  250 get Activity Dialog attributes
  251 
  252     my $ActivityDialog = $ActivityDialogObject->ActivityDialogGet(
  253         ID            => 123,            # ID or EntityID is needed
  254         EntityID      => 'P1',
  255         UserID        => 123,            # mandatory
  256     );
  257 
  258 Returns:
  259 
  260     $ActivityDialog = {
  261         ID           => 123,
  262         EntityID     => 'AD1',
  263         Name         => 'some name',
  264         Config       => $ConfigHashRef,
  265         CreateTime   => '2012-07-04 15:08:00',
  266         ChangeTime   => '2012-07-04 15:08:00',
  267     };
  268 
  269 =cut
  270 
  271 sub ActivityDialogGet {
  272     my ( $Self, %Param ) = @_;
  273 
  274     # check needed stuff
  275     if ( !$Param{ID} && !$Param{EntityID} ) {
  276         $Kernel::OM->Get('Kernel::System::Log')->Log(
  277             Priority => 'error',
  278             Message  => 'Need ID or EntityID!'
  279         );
  280         return;
  281     }
  282 
  283     if ( !$Param{UserID} ) {
  284         $Kernel::OM->Get('Kernel::System::Log')->Log(
  285             Priority => 'error',
  286             Message  => 'Need UserID!',
  287         );
  288         return;
  289     }
  290 
  291     # get cache object
  292     my $CacheObject = $Kernel::OM->Get('Kernel::System::Cache');
  293 
  294     # check cache
  295     my $CacheKey;
  296     if ( $Param{ID} ) {
  297         $CacheKey = 'ActivityDialogGet::ID::' . $Param{ID};
  298     }
  299     else {
  300         $CacheKey = 'ActivityDialogGet::EntityID::' . $Param{EntityID};
  301     }
  302 
  303     my $Cache = $CacheObject->Get(
  304         Type => 'ProcessManagement_ActivityDialog',
  305         Key  => $CacheKey,
  306     );
  307     return $Cache if $Cache;
  308 
  309     # get database object
  310     my $DBObject = $Kernel::OM->Get('Kernel::System::DB');
  311 
  312     # sql
  313     if ( $Param{ID} ) {
  314         return if !$DBObject->Prepare(
  315             SQL => '
  316                 SELECT id, entity_id, name, config, create_time, change_time
  317                 FROM pm_activity_dialog
  318                 WHERE id = ?',
  319             Bind  => [ \$Param{ID} ],
  320             Limit => 1,
  321         );
  322     }
  323     else {
  324         return if !$DBObject->Prepare(
  325             SQL => '
  326                 SELECT id, entity_id, name, config, create_time, change_time
  327                 FROM pm_activity_dialog
  328                 WHERE entity_id = ?',
  329             Bind  => [ \$Param{EntityID} ],
  330             Limit => 1,
  331         );
  332     }
  333 
  334     # get yaml object
  335     my $YAMLObject = $Kernel::OM->Get('Kernel::System::YAML');
  336 
  337     my %Data;
  338     while ( my @Data = $DBObject->FetchrowArray() ) {
  339 
  340         my $Config = $YAMLObject->Load( Data => $Data[3] );
  341 
  342         %Data = (
  343             ID         => $Data[0],
  344             EntityID   => $Data[1],
  345             Name       => $Data[2],
  346             Config     => $Config,
  347             CreateTime => $Data[4],
  348             ChangeTime => $Data[5],
  349 
  350         );
  351     }
  352 
  353     return if !$Data{ID};
  354 
  355     # set cache
  356     $CacheObject->Set(
  357         Type  => 'ProcessManagement_ActivityDialog',
  358         Key   => $CacheKey,
  359         Value => \%Data,
  360         TTL   => $Self->{CacheTTL},
  361     );
  362 
  363     return \%Data;
  364 }
  365 
  366 =item ActivityDialogUpdate()
  367 
  368 update ActivityDialog attributes
  369 
  370 returns 1 if success or undef otherwise
  371 
  372     my $Success = $ActivityDialogObject->ActivityDialogUpdate(
  373         ID          => 123,                    # mandatory
  374         EntityID    => 'AD1'                   # mandatory, exportable unique identifier
  375         Name        => 'NameOfActivityDialog', # mandatory
  376         Config      => $ConfigHashRef,         # mandatory, actvity dialog configuration to be
  377                                                #   stored in YAML format
  378         UserID      => 123,                    # mandatory
  379     );
  380 
  381 =cut
  382 
  383 sub ActivityDialogUpdate {
  384     my ( $Self, %Param ) = @_;
  385 
  386     # check needed stuff
  387     for my $Key (qw(ID EntityID Name Config UserID)) {
  388         if ( !$Param{$Key} ) {
  389             $Kernel::OM->Get('Kernel::System::Log')->Log(
  390                 Priority => 'error',
  391                 Message  => "Need $Key!"
  392             );
  393             return;
  394         }
  395     }
  396 
  397     # get database object
  398     my $DBObject = $Kernel::OM->Get('Kernel::System::DB');
  399 
  400     # check if EntityID already exists
  401     return if !$DBObject->Prepare(
  402         SQL => "
  403             SELECT id FROM pm_activity_dialog
  404             WHERE $Self->{Lower}(entity_id) = $Self->{Lower}(?)
  405             AND id != ?",
  406         Bind  => [ \$Param{EntityID}, \$Param{ID} ],
  407         LIMIT => 1,
  408     );
  409 
  410     my $EntityExists;
  411     while ( my @Data = $DBObject->FetchrowArray() ) {
  412         $EntityExists = 1;
  413     }
  414 
  415     if ($EntityExists) {
  416         $Kernel::OM->Get('Kernel::System::Log')->Log(
  417             Priority => 'error',
  418             Message  => "The EntityID:$Param{Name} already exists for an ActivityDialog!",
  419         );
  420         return;
  421     }
  422 
  423     # check config valid format (at least it must contain the description)
  424     if ( !IsHashRefWithData( $Param{Config} ) ) {
  425         $Kernel::OM->Get('Kernel::System::Log')->Log(
  426             Priority => 'error',
  427             Message  => "Config needs to be a valid Hash reference!",
  428         );
  429         return;
  430     }
  431     for my $Needed (qw(DescriptionShort Fields FieldOrder)) {
  432         if ( !$Param{Config}->{$Needed} ) {
  433             $Kernel::OM->Get('Kernel::System::Log')->Log(
  434                 Priority => 'error',
  435                 Message  => "Need $Needed in Config!",
  436             );
  437             return;
  438         }
  439     }
  440 
  441     # check config formats
  442     if ( ref $Param{Config}->{Fields} ne 'HASH' ) {
  443         $Kernel::OM->Get('Kernel::System::Log')->Log(
  444             Priority => 'error',
  445             Message  => "Config Fields must be a Hash!",
  446         );
  447         return;
  448     }
  449     if ( ref $Param{Config}->{FieldOrder} ne 'ARRAY' ) {
  450         $Kernel::OM->Get('Kernel::System::Log')->Log(
  451             Priority => 'error',
  452             Message  => "Config Fields must be an Array!",
  453         );
  454         return;
  455     }
  456 
  457     # dump layout and config as string
  458     my $Config = $Kernel::OM->Get('Kernel::System::YAML')->Dump( Data => $Param{Config} );
  459 
  460     # Make sure the resulting string has the UTF-8 flag. YAML only sets it if
  461     #   part of the data already had it.
  462     utf8::upgrade($Config);
  463 
  464     # check if need to update db
  465     return if !$DBObject->Prepare(
  466         SQL => '
  467             SELECT entity_id, name, config
  468             FROM pm_activity_dialog
  469             WHERE id = ?',
  470         Bind  => [ \$Param{ID} ],
  471         Limit => 1,
  472     );
  473 
  474     my $CurrentEntityID;
  475     my $CurrentName;
  476     my $CurrentConfig;
  477     while ( my @Data = $DBObject->FetchrowArray() ) {
  478         $CurrentEntityID = $Data[0];
  479         $CurrentName     = $Data[1];
  480         $CurrentConfig   = $Data[2];
  481     }
  482 
  483     if ($CurrentEntityID) {
  484 
  485         return 1 if $CurrentEntityID eq $Param{EntityID}
  486             && $CurrentName eq $Param{Name}
  487             && $CurrentConfig eq $Config;
  488     }
  489 
  490     # sql
  491     return if !$DBObject->Do(
  492         SQL => '
  493             UPDATE pm_activity_dialog
  494             SET entity_id = ?, name = ?,  config = ?, change_time = current_timestamp,
  495                 change_by = ?
  496             WHERE id = ?',
  497         Bind => [
  498             \$Param{EntityID}, \$Param{Name}, \$Config, \$Param{UserID}, \$Param{ID},
  499         ],
  500     );
  501 
  502     # delete cache
  503     $Kernel::OM->Get('Kernel::System::Cache')->CleanUp(
  504         Type => 'ProcessManagement_ActivityDialog',
  505     );
  506 
  507     return 1;
  508 }
  509 
  510 =item ActivityDialogList()
  511 
  512 get an ActivityDialog list
  513 
  514     my $List = $ActivityDialogObject->ActivityDialogList(
  515         UseEntities => 0,                       # default 0, 1 || 0. if 0 the return hash keys are
  516                                                 #    the activity dialog IDs otherwise keys are the
  517                                                 #    activity dialog entity IDs
  518         UserID      => 1,
  519     );
  520 
  521     Returns:
  522 
  523     $List = {
  524         1 => 'NameOfActivityDialog',
  525     }
  526 
  527     or
  528 
  529     $List = {
  530         'AD1' => 'NameOfActivityDialog',
  531     }
  532 
  533 =cut
  534 
  535 sub ActivityDialogList {
  536     my ( $Self, %Param ) = @_;
  537 
  538     # check needed
  539     if ( !$Param{UserID} ) {
  540         $Kernel::OM->Get('Kernel::System::Log')->Log(
  541             Priority => 'error',
  542             Message  => "Need UserID!"
  543         );
  544         return;
  545     }
  546 
  547     # check cache
  548     my $UseEntities = 0;
  549     if ( defined $Param{UseEntities} && $Param{UseEntities} ) {
  550         $UseEntities = 1;
  551     }
  552 
  553     # get cache object
  554     my $CacheObject = $Kernel::OM->Get('Kernel::System::Cache');
  555 
  556     my $CacheKey = 'ActivityDialogList::UseEntities::' . $UseEntities;
  557     my $Cache    = $CacheObject->Get(
  558         Type => 'ProcessManagement_ActivityDialog',
  559         Key  => $CacheKey,
  560     );
  561     return $Cache if ref $Cache;
  562 
  563     # get database object
  564     my $DBObject = $Kernel::OM->Get('Kernel::System::DB');
  565 
  566     my $SQL = '
  567             SELECT id, entity_id, name
  568             FROM pm_activity_dialog';
  569 
  570     return if !$DBObject->Prepare( SQL => $SQL );
  571 
  572     my %Data;
  573     while ( my @Row = $DBObject->FetchrowArray() ) {
  574         if ( !$UseEntities ) {
  575             $Data{ $Row[0] } = $Row[2];
  576         }
  577         else {
  578             $Data{ $Row[1] } = $Row[2];
  579         }
  580     }
  581 
  582     # set cache
  583     $CacheObject->Set(
  584         Type  => 'ProcessManagement_ActivityDialog',
  585         Key   => $CacheKey,
  586         Value => \%Data,
  587         TTL   => $Self->{CacheTTL},
  588     );
  589 
  590     return \%Data;
  591 }
  592 
  593 =item ActivityDialogListGet()
  594 
  595 get an Activity Dialog list with all activity dialog details
  596 
  597     my $List = $ActivityDialogObject->ActivityDialogListGet(
  598         UserID      => 1,
  599     );
  600 
  601 Returns:
  602 
  603     $List = [
  604         {
  605             ID             => 123,
  606             EntityID       => 'AD1',
  607             Name           => 'some name',
  608             Config         => $ConfigHashRef,
  609             CreateTime     => '2012-07-04 15:08:00',
  610             ChangeTime     => '2012-07-04 15:08:00',
  611         }
  612         {
  613             ID             => 456,
  614             EntityID       => 'AD2',
  615             Name           => 'some name',
  616             Config         => $ConfigHashRef,
  617             CreateTime     => '2012-07-04 15:09:00',
  618             ChangeTime     => '2012-07-04 15:09:00',
  619         }
  620     ];
  621 
  622 =cut
  623 
  624 sub ActivityDialogListGet {
  625     my ( $Self, %Param ) = @_;
  626 
  627     # check needed stuff
  628     if ( !$Param{UserID} ) {
  629         $Kernel::OM->Get('Kernel::System::Log')->Log(
  630             Priority => 'error',
  631             Message  => 'Need UserID!',
  632         );
  633         return;
  634     }
  635 
  636     # get cache object
  637     my $CacheObject = $Kernel::OM->Get('Kernel::System::Cache');
  638 
  639     # check cache
  640     my $CacheKey = 'ActivityDialogListGet';
  641 
  642     my $Cache = $CacheObject->Get(
  643         Type => 'ProcessManagement_ActivityDialog',
  644         Key  => $CacheKey,
  645     );
  646     return $Cache if $Cache;
  647 
  648     # get database object
  649     my $DBObject = $Kernel::OM->Get('Kernel::System::DB');
  650 
  651     # sql
  652     return if !$DBObject->Prepare(
  653         SQL => '
  654             SELECT id, entity_id
  655             FROM pm_activity_dialog
  656             ORDER BY id',
  657     );
  658 
  659     my @ActivityDialogIDs;
  660     while ( my @Row = $DBObject->FetchrowArray() ) {
  661         push @ActivityDialogIDs, $Row[0];
  662     }
  663 
  664     my @Data;
  665     for my $ItemID (@ActivityDialogIDs) {
  666 
  667         my $ActivityDialogData = $Self->ActivityDialogGet(
  668             ID     => $ItemID,
  669             UserID => 1,
  670         );
  671         push @Data, $ActivityDialogData;
  672     }
  673 
  674     # set cache
  675     $CacheObject->Set(
  676         Type  => 'ProcessManagement_ActivityDialog',
  677         Key   => $CacheKey,
  678         Value => \@Data,
  679         TTL   => $Self->{CacheTTL},
  680     );
  681 
  682     return \@Data;
  683 }
  684 
  685 1;
  686 
  687 =back
  688 
  689 =head1 TERMS AND CONDITIONS
  690 
  691 This software is part of the OTRS project (L<https://otrs.org/>).
  692 
  693 This software comes with ABSOLUTELY NO WARRANTY. For details, see
  694 the enclosed file COPYING for license information (GPL). If you
  695 did not receive this file, see L<https://www.gnu.org/licenses/gpl-3.0.txt>.
  696 
  697 =cut