OSDN Git Service

Controller::Index: add sidebar support
authorhylom <hylom@users.sourceforge.jp>
Mon, 21 Nov 2016 12:39:28 +0000 (21:39 +0900)
committerhylom <hylom@users.sourceforge.jp>
Mon, 21 Nov 2016 12:39:50 +0000 (21:39 +0900)
src/newslash_web/lib/Newslash/Web/Controller/Index.pm
src/newslash_web/templates/common/sidebar.html.tt2

index 1450fcc..349680c 100644 (file)
@@ -1,12 +1,39 @@
 package Newslash::Web::Controller::Index;
 use Mojo::Base 'Mojolicious::Controller';
 use Mojo::Util qw(dumper);
+use Mojo::JSON qw(decode_json encode_json);
+use Template;
 
 sub root {
     my $c = shift;
     my $stories = $c->model('stories')->latest;
+    my $boxes = $c->model('boxes');
+
+    my $user = $c->stash("user");
+    my $users = $c->model('users');
+    my $items = $users->sidebar_items(uid => $user->{uid});
+
+    my $sidebar_items = [];
+    my $tt = Template->new();
+
+    for my $box (@$items) {
+        if (!$box->{query_params}) {
+            push @$sidebar_items, $box->{template};
+            next;
+        }
+        my $q_params = decode_json($box->{query_params});
+        my $model = $c->model($box->{model});
+        next if !$model;
+        my $rs = $model->select(%$q_params);
+        my $result = '';
+        my $template = $box->{template};
+        if ($tt->process(\$template, {items => $rs}, \$result)) {
+            push @$sidebar_items, $result;
+        }
+    }
+    $c->app->log->debug(dumper($sidebar_items));
     #$c->app->log->debug(dumper($stories));
-    $c->render(stories => $stories);
+    $c->render(stories => $stories, boxes => $sidebar_items);
 }
 
 sub journals {
index c97d1c6..3ab7542 100644 (file)
@@ -2,4 +2,9 @@
   <div class="banner-sample" style="width: 300px; height: 250px; border: 1px solid;">
     Sample Rectangle
   </div>
+  [%- FOREACH box IN boxes -%]
+  <div class="sidebar-item contents-text">
+    [%- box -%]
+  </div>
+  [%- END -%]
 </div>