The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
%# RT::Extension::ReferenceIDoitObjects
%#
%# Copyright 2011-12 synetics GmbH, http://i-doit.org/
%#
%# This program is free software; you can redistribute it and/or modify it under
%# the same terms as Perl itself.
%#
%# Request Tracker (RT) is Copyright Best Practical Solutions, LLC.

<div id="i-doit-objectbrowser" style="position: relative; min-width: 400px;">
    <div id="data-store" style="display:none;"></div>
    <div id="i-doit-browser-notice" class="ui-corner-all" style="display: none;"></div>
    <div id="i-doit-content" style="display:none;">
        <table class="object-table" style="width:100%;">
            <thead>
                <tr>
                    <th width="5%"><% loc('ID') %></th>
                    <th width="35%"><% loc('Name') %></th>
                    <th width="35%"><% loc('Type') %></th>
                    <th width="15%"><% loc('Location') %></th>
                    <th width="10%"><% loc('Link') %></th>
                </tr>
            </thead>
            <tbody>
            </tbody>
        </table>
    </div>
</div>

<script type="text/javascript">
    // Global variables (without "var").
    idoit_url = '<% $IDoitURL %>';

    api_url = '<% $IDoitAPI %>';
    api_lang = '<% $Language %>';
    api_user = '<% $IDoitUser %>';
    api_password = '<% $IDoitPassword %>';
    api_mandator = '<% $IDoitMandator %>';

    show_custom_fields = <% $IDoitShowCustomFields %>;

    browser_preselection_field = 'tr#<% $IDoitObjectsField %>';
    browser_mandator_field = 'tr#<% $IDoitMandatorField %>';

    preselection = [<% $IDoitObjects %>];
</script>

<script type="text/javascript" src="<% RT->Config->Get('WebPath') %>/NoAuth/js/i-doit_object_browser.js"></script>

<script type="text/javascript">
    (function($) {
        $(document).ready(function() {
            browser_preselection_field = $(browser_preselection_field);
            browser_mandator_field = $(browser_mandator_field);

            list = $('#i-doit-content table.object-table').dataTable({
                "bJQueryUI": true,
                "bAutoWidth": false,
                "bPaginate": false,
                "bLengthChange": false,
                "bSort": false,
                "oLanguage": datatable_lang
            });

            if (isNaN(parseInt(api_mandator)) || api_mandator <= 0) {
                window.error_notice('<% loc("Please select an i-doit mandator.") %>');
            } else if (preselection.length == 0) {
                window.error_notice('<% loc("No objects has been selected yet.") %>');
            } else {
                window.error_notice('<% loc("Loading...") %>');

                data = {
                    "method":"cmdb.objects",
                    "params":{
                        "session":{
                            "username":api_user,
                            "password":api_password,
                            "language":api_lang,
                            "mandator":api_mandator},
                        "filter":{
                            "ids":preselection,
                            "location": true
                        }},
                    "id":"1",
                    "jsonrpc":"2.0"};

                idoit_ajax(data, function(response) {
                        if (response.error == null) {
                            $('#i-doit-browser-notice').css({display: 'none'});
                            $('#i-doit-content').css({display: 'block'});

                            var entities = [];

                            $.each(response.result, function(i, e) {
                                var link = '<a href="' + idoit_url + '?objID=' + e.id + '" title="<% loc('Go to i-doit') %>" target="_blank">&raquo; i-doit</a>';
                                entities.push([e.id, e.title, e.type_title, window.render_location_tree(e.location), link]);
                            });

                            list.fnAddData(entities);
                        } else {
                            window.error_notice('<% loc("Error while loading pre-selecting objects") %>');
                        }
                    });
            }

            // Hide custom fields:
            if (show_custom_fields == 0) {
                browser_preselection_field.hide();
                browser_mandator_field.hide();
            }
        });
    })(jQuery);
</script>

<%INIT>
$IDoitURL = RT->Config->Get('IDoitURL');
unless($IDoitURL) {
    my $msg = loc('URL for i-doit is not configured.');
    RT::Logger->error($msg);
    $$skip_create = 1;
    push @{$results}, $msg;
}

$IDoitAPI = RT->Config->Get('IDoitAPI');
unless($IDoitAPI) {
    my $msg = loc("URL for i-doit's API is not configured.");
    RT::Logger->error($msg);
    $$skip_create = 1;
    push @{$results}, $msg;
}

$IDoitUser = RT->Config->Get('IDoitUser');
unless($IDoitUser) {
    my $msg = loc("User for i-doit's API is not configured.");
    RT::Logger->error($msg);
    $$skip_create = 1;
    push @{$results}, $msg;
}

$IDoitPassword = RT->Config->Get('IDoitPassword');
unless($IDoitPassword) {
    my $msg = loc("Password for i-doit's API is not configured.");
    RT::Logger->error($msg);
    $$skip_create = 1;
    push @{$results}, $msg;
}

my $cfMandator = 'i-doit mandator';

$IDoitMandator = $ticket->FirstCustomFieldValue($cfMandator);
unless($IDoitMandator) {
    $IDoitMandator = RT->Config->Get('IDoitDefaultMandator');

    unless ($IDoitMandator) {
        my $msg = loc('Default mandator is not configured.');
        RT::Logger->error($msg);
        $$skip_create = 1;
        push @{$results}, $msg;
    }
}

my $cf = RT::CustomField->new($RT::SystemUser);
$cf->LoadByNameAndQueue(Name => $cfMandator, Queue => '0');
unless($cf->id) {
    my $msg = sprintf(loc("Custom field %s does not exist."), $cfMandator);
    RT::Logger->error($msg);
    $$skip_create = 1;
    push @{$results}, $msg;
}
$IDoitMandatorField = 'CF-' . $cf->id . '-ShowRow';

my $cfObjects = 'i-doit objects';

my $IDoitObjectValues = $ticket->CustomFieldValues($cfObjects);
my @IDoitObjects;
while (my $IDoitObjectValue = $IDoitObjectValues->Next) {
  push(@IDoitObjects, $IDoitObjectValue->Content);
}
 
$IDoitObjects = join(', ', @IDoitObjects);

$cf = RT::CustomField->new($RT::SystemUser);
$cf->LoadByNameAndQueue(Name => $cfObjects, Queue => '0');
unless($cf->id) {
    my $msg = sprintf(loc("Custom field %s does not exist."), $cfObjects);
    RT::Logger->error($msg);
    $$skip_create = 1;
    push @{$results}, $msg;
}
$IDoitObjectsField = 'CF-' . $cf->id . '-ShowRow';

$IDoitShowCustomFields = RT->Config->Get('IDoitShowCustomFields');
if ($IDoitShowCustomFields < 0 || $IDoitShowCustomFields > 1) {
    my $msg = loc('Configuration option to show/hide i-doit related custom fields is not set.');
    RT::Logger->error($msg);
    $$skip_create = 1;
    push @{$results}, $msg;
}

$Language = substr($session{CurrentUser}->UserObj->Lang, 0, 2);
</%INIT>

<%ARGS>
$skip_create => undef
$results => undef

$ticket => undef

$Language => undef

$IDoitURL => undef
$IDoitAPI => undef
$IDoitUser => undef
$IDoitPassword => undef
$IDoitMandator => undef
$IDoitMandatorField => undef
$IDoitObjects => undef
$IDoitObjectsField => undef
$IDoitView => undef
$IDoitShowCustomFields => undef
</%ARGS>