UI: templates and core processor
authorMike Rylander <mrylander@gmail.com>
Tue, 2 Apr 2013 23:14:36 +0000 (19:14 -0400)
committerMike Rylander <mrylander@gmail.com>
Tue, 2 Apr 2013 23:14:36 +0000 (19:14 -0400)
Signed-off-by: Mike Rylander <mrylander@gmail.com>

src/perl/lib/ShareStuff/UI.pm [new file with mode: 0644]
src/perl/lib/templates/home [new file with mode: 0644]
src/perl/lib/templates/package [new file with mode: 0644]
src/perl/lib/templates/package_detail [new file with mode: 0644]
src/perl/lib/templates/package_summary [new file with mode: 0644]

diff --git a/src/perl/lib/ShareStuff/UI.pm b/src/perl/lib/ShareStuff/UI.pm
new file mode 100644 (file)
index 0000000..ce7c6bf
--- /dev/null
@@ -0,0 +1,91 @@
+package ShareStuff::UI;
+use strict;
+use warnings;
+
+use Apache::Constants qw( :common );
+use Template qw( :template );
+use CGI;
+use OpenSRF::EX qw(:try);
+use OpenSRF::System;
+use OpenILS::Utils::CStoreEditor;
+
+
+# set the bootstrap config and template include directory when
+# this module is loaded
+my $bootstrap;
+my $ssl_off;
+
+sub import {
+    my $self = shift;
+    $bootstrap = shift;
+    $ssl_off = shift;
+}
+
+
+sub child_init {
+    OpenSRF::System->bootstrap_client( config_file => $bootstrap );
+    OpenILS::Utils::CStoreEditor::init();
+    return OK;
+}
+
+# we epxect a PerlSetVar of ss_templates pointing to the location of the TT files
+sub handler {
+    my %env = ();
+
+    $env{r} = shift;
+    $env{cgi} = new CGI;
+
+    $env{authtoken} = $cgi->cookie('ses') || $cgi->param('ses');
+    $env{user} = verify_login($env{authtoken});
+    $env{cstore} = new OpenILS::Utils::CStoreEditor;
+
+
+    $env{file} = $env{r}->path_info;
+    $env{file} =~ s[^/][];
+
+    $env{api} = 0;
+    if ($env{file} =~ /^api/) {
+        $env{file} =~ s[^api/][];
+        $env{api} = 1;
+    }
+
+    my $template = Template->new({ 
+        VARIABLES     => \%env,
+        INCLUDE_PATH  => $ss_templates,
+        OUTPUT        => $env{r},     # direct output to Apache request
+    });
+
+    $evn{r}->content_type('text/html') if (!$env{api});
+    $evn{r}->content_type('text/json') if ($env{api});
+    $evn{r}->send_http_header;
+
+    $template->process($env{file}, $env) || return fail($env{r}, SERVER_ERROR, $template->error());
+
+    return OK;
+}
+
+sub fail {
+    my ($r, $status, $message) = @_;
+    $r->log_reason($message, $r->filename);
+    return $status;
+}
+
+# returns the user object if the session is valid, 0 otherwise
+sub verify_login {
+    my $auth_token = shift;
+    return undef unless $auth_token;
+
+    my $user = OpenSRF::AppSession
+        ->create("sharestuff.auth")
+        ->request( "sharestuff.auth.session.retrieve", $auth_token )
+        ->gather(1);
+
+    if (ref($user) eq 'HASH' && $user->{ilsevent} == 1001) {
+        return undef;
+    }
+
+    return $user if ref($user);
+    return undef;
+}
+
+1;
diff --git a/src/perl/lib/templates/home b/src/perl/lib/templates/home
new file mode 100644 (file)
index 0000000..8b13789
--- /dev/null
@@ -0,0 +1 @@
+
diff --git a/src/perl/lib/templates/package b/src/perl/lib/templates/package
new file mode 100644 (file)
index 0000000..993d3cf
--- /dev/null
@@ -0,0 +1,20 @@
+[%
+
+WRAPPER base;
+
+pkg_id = cgi.param( 'pkg' );
+
+
+IF pkg_id
+    INCLUDE package_details
+        pkg = cstore.retrieve_depository_package( [ cgi.param( 'pkg' ), { flesh = 1, flesh_fields = { dp = ['systems'] } } ] )
+        systems = cstore.retrieve_all_depository_source_system();
+ELSE
+    FOREACH p IN cstore.search_depository_package( [ { author = user.id() }, { flesh = 1, flesh_fields = { dp = ['systems'] } } ] );
+        INCLUDE package_summary pkg = p;
+        %] <hr/> [%
+    END;
+END;
+
+END %]
+
diff --git a/src/perl/lib/templates/package_detail b/src/perl/lib/templates/package_detail
new file mode 100644 (file)
index 0000000..e4fbb12
--- /dev/null
@@ -0,0 +1,51 @@
+<!-- expects "pkg", "systems" -->
+
+[%
+
+sysnames = [];
+FOREACH s IN systems;
+    sysnames.push(s.name());
+END;
+
+
+%]
+
+<form id="p[% pkg.id() %]_form" method="POST">
+<table style="border-collapse:collaps; border-width:1px">
+    <tr>
+        <th>Title</th>
+        <td><input name="title" type="text" value="[% pkg.title() | html %]"/></td>
+    </tr>
+    <tr>
+        <th>Description</th>
+        <td><textarea name="description">[% pkg.description() | html %]</textarea></td>
+    </tr>
+    <tr>
+        <th>Version</th>
+        <td><input name="pkg_version" type="text" value="[% pkg.pkg_version() | html %]"/></td>
+    </tr>
+    <tr>
+        <th>Created</th>
+        <td><input name="created" type="text" value="[% pkg.created() | html %]"/></td>
+    </tr>
+    <tr>
+        <th>Systems</th>
+        <td>
+            [% FOREACH s IN sysnames.sort %]
+                <input
+                    name="systems"
+                    type="checkbox"
+                    value="[% s.name() | html %]"
+                    [% 'checked="checked"' IF pgk.systems().grep(s) %]
+                />[% s.name() | html %]>
+                <br/>
+            [% END %]
+        </td>
+    </tr>
+</table>
+<a href="javascript:document.forms['p[% pkg.id() %]_form'].submit()">Save</a><br/>
+<a href="javascript:history.back()">Back</a>
+</form>
+
+[% END %]
+
diff --git a/src/perl/lib/templates/package_summary b/src/perl/lib/templates/package_summary
new file mode 100644 (file)
index 0000000..3cbfb6c
--- /dev/null
@@ -0,0 +1,35 @@
+<!-- expects "pkg" -->
+
+[%
+
+sysnames = [];
+FOREACH s IN pkg.systems();
+    sysnames.push(s.name());
+END;
+
+
+%]
+
+<table style="border-collapse:collaps; border-width:1px; width: 90%">
+    <tr>
+        <th>Title</th>
+        <td><a href="?pkg=[% pkg.id() %]">[% pkg.title() | html %]</a></td>
+        <th>Version</th>
+        <td>[% pkg.pkg_version() | html %]</td>
+        <th>Created</th>
+        <td>[% pkg.created() | html %]"/></td>
+        <th>Systems</th>
+    </tr>
+    <tr>
+        <th>Description</th>
+        <td colspan="5">[% pkg.description() | html %]</textarea></td>
+        <td>
+            [% FOREACH s IN sysnames.sort %]
+                    [% s.name() | html %]<br/>
+            [% END %]
+        </td>
+    </tr>
+</table>
+
+[% END %]
+