wisest owl (wisest_owl) wrote in changelog,
wisest owl
wisest_owl
changelog

[livejournal] r17626: LJSUP-7052. Add Featured Communities Wid...

Committer: gprochaev
LJSUP-7052. Add Featured Communities Widget

U   trunk/bin/upgrading/update-db-general.pl
U   trunk/cgi-bin/LJ/Vertical.pm
U   trunk/cgi-bin/LJ/Widget/Browse.pm
A   trunk/htdocs/admin/browse/featured_communities.bml
U   trunk/htdocs/admin/browse/index.bml
A   trunk/templates/Browse/featured_communities.tmpl
U   trunk/templates/Browse/sidebar.tmpl
Modified: trunk/bin/upgrading/update-db-general.pl
===================================================================
--- trunk/bin/upgrading/update-db-general.pl	2010-11-01 09:35:38 UTC (rev 17625)
+++ trunk/bin/upgrading/update-db-general.pl	2010-11-01 10:24:55 UTC (rev 17626)
@@ -3128,6 +3128,19 @@
 )
 EOC
 
+register_tablecreate("vertical_comms", <<'EOC');
+CREATE TABLE vertical_comms (
+  vert_id int(11) NOT NULL,
+  journalid int(11) NOT NULL,
+  timecreate datetime NOT NULL,
+  timeadded datetime NOT NULL,
+  is_deleted tinyint(1) default '0',
+  PRIMARY KEY  (vert_id),
+  KEY journalid (journalid),
+  KEY timecreate (timecreate)
+)
+EOC
+
 ## --
 ## -- embedconten previews
 ## --

Modified: trunk/cgi-bin/LJ/Vertical.pm
===================================================================
--- trunk/cgi-bin/LJ/Vertical.pm	2010-11-01 09:35:38 UTC (rev 17625)
+++ trunk/cgi-bin/LJ/Vertical.pm	2010-11-01 10:24:55 UTC (rev 17626)
@@ -586,6 +586,27 @@
 # Loaders
 #
 
+sub load_communities {
+    my $self = shift;
+    my %args = @_;
+
+    my $is_random = $args{'is_random'};
+
+    my $dbh = LJ::get_db_writer();
+    my $comms = $dbh->selectall_arrayref("SELECT * FROM vertical_comms WHERE vert_id = ?", { Slice => {} }, $self->vert_id);
+    return [] unless $comms;
+    my $max_num = scalar @$comms;
+    my $count   = $args{'count'};
+    $count = $max_num if $count > $max_num;
+    my @result = ();
+    while ($count--) {
+        my $comm = $comms->[int(rand($max_num))];
+        push @result, $comm if $comm;
+    }
+
+    return \@result;
+}
+
 ## Return a list of Top Entries in communities
 ## Arg:
 ##     - count     - for admin page - all (Inf), for widget - 5, for example
@@ -1026,6 +1047,33 @@
 # Accessors
 #
 
+sub delete_community {
+    my $self = shift;
+    my %args = @_;
+
+    my $journalid = $args{'comm_id'};
+
+    return undef unless $journalid;
+
+    my $dbh = LJ::get_db_writer();
+    my $res = $dbh->do("DELETE FROM vertical_comms WHERE vert_id = ? AND journalid = ?", undef, $self->vert_id, $journalid);
+    return $res;
+}
+
+sub add_community {
+    my $self = shift;
+    my $url  = shift;
+
+    my $c = LJ::User->new_from_url ($url);
+    return undef unless $c;
+
+    my $dbh = LJ::get_db_writer();
+    my $res = $dbh->do("INSERT INTO vertical_comms (vert_id, journalid, timecreate, timeadded) VALUES
+        (?, ?, ?, UNIX_TIMESTAMP())", undef, $self->vert_id, $c->userid, $c->timecreate);
+
+    return $res;
+}
+
 sub delete_post {
     my $self = shift;
     my %args = @_;

Modified: trunk/cgi-bin/LJ/Widget/Browse.pm
===================================================================
--- trunk/cgi-bin/LJ/Widget/Browse.pm	2010-11-01 09:35:38 UTC (rev 17625)
+++ trunk/cgi-bin/LJ/Widget/Browse.pm	2010-11-01 10:24:55 UTC (rev 17626)
@@ -288,6 +288,21 @@
     # merge args to uri.
     $uri .= '?' . $args if $args;
 
+    ## Prepare DATA for Featured Communities Widget
+    my $comms = $vertical->load_communities( count => $vertical->show_entries, is_random => 1 );
+    my @top_comms = ();
+    foreach my $comm (@$comms) {
+        my $c = LJ::load_userid($comm->{journalid});
+        next unless $c;
+        my $userpic = $c->userpic;
+        push @top_comms, {
+            username        => $c->display_name,
+            userpic         => $userpic ? $userpic->url : '',
+            community       => $c->user,
+            bio             => $c->bio,
+        };
+    }
+
     ## Prepare DATA for Featured Posts Widget
     my $posts = $vertical->load_vertical_posts( count => $vertical->show_entries, is_random => 1 );
     my @top_posts = ();
@@ -326,6 +341,7 @@
         add_community_widget    => LJ::Widget::AddCommunity->render(),
         search_widget           => LJ::Widget::Search->render(type => 'yandex'),
         top_posts               => \@top_posts,
+        top_comms               => \@top_comms,
         view                    => $view,
     );
 

Added: trunk/htdocs/admin/browse/featured_communities.bml
===================================================================
--- trunk/htdocs/admin/browse/featured_communities.bml	                        (rev 0)
+++ trunk/htdocs/admin/browse/featured_communities.bml	2010-11-01 10:24:55 UTC (rev 17626)
@@ -0,0 +1,99 @@
+<?page
+body<=
+<?_code
+{
+    use strict;
+    use vars qw(%GET %POST $title $headextra @errors @warnings);
+    use Class::Autouse qw( LJ::Browse );
+
+    my $remote = LJ::get_remote();
+
+    return "<?needlogin?>"
+        unless $remote;
+
+    return "You are not allowed to view this page"
+        unless LJ::check_priv($remote, 'siteadmin', 'community_directory') || $LJ::IS_DEV_SERVER;
+
+    $title = "Top Entries in communities";
+    my $ret = "";
+
+    my $vert_id = $POST{'vert_id'};
+    my $journals = $POST{'journals'};
+
+    $ret .= "<form method='POST'>\n";
+    $ret .= LJ::form_auth();
+
+    # Get the full list of categories
+    my @verticals = LJ::Vertical->load_all;
+    # Don't include the top level categories and get the unique URI for each
+
+    my @vert_ids = map { { value => $_->vert_id, text => $_->name } } @verticals;
+    @vert_ids = sort { $a cmp $b } @vert_ids;
+
+    $ret .= "<p>Select vertical:";
+    $ret .= LJ::html_select({
+                name => 'vert_id',
+                selected => $vert_id },
+                { text => 'Select Category',
+                value => '' },
+                @vert_ids
+            );
+    $ret .= "&nbsp;" . LJ::html_submit('go', 'Go') . "</p>";
+
+    $ret .= "</form>";
+
+    my $vertical = undef;
+    if ($vert_id) {
+        $vertical = LJ::Vertical->load_by_id($vert_id);
+        $ret .= <<ADDPOST;
+<form method="post">
+<input type="hidden" name="vert_id" value="$vert_id">
+<table>
+    <caption>Enter URL for adding featured post:</caption>
+    <tr>
+        <td>URL:</td><td colspan="2"><input type="text" size="50" name="url"></td>
+        <td colspan="2"><input type="submit" name="add" value="Add"></td>
+    </tr>
+</table>
+</form>
+<br/>
+<br/>
+ADDPOST
+
+    }
+
+    if (LJ::did_post() && $POST{'add'}) {
+        $vertical->add_community ($POST{'url'});
+    }
+
+    if (LJ::did_post() && $POST{'delete_comm'}) {
+        $vertical->delete_community (comm_id => $POST{'rec_id'});
+    }
+
+    if (LJ::did_post()) {
+        $ret .= "<b><div>Added Communities</div></p>";
+        my $comms = $vertical->load_communities( count => '100', is_random => 0 );
+
+        foreach (@$comms) {
+            my $comm = LJ::load_userid($_->{journalid});
+            my $url = $comm->journal_base;
+            $ret .= "<li><a href='".$url."'>".$comm->display_name."</a>";
+            $ret .= "<form method='POST'>\n";
+            $ret .= LJ::form_auth();
+            $ret .= "<input type='hidden' name='rec_id' value='".$comm->userid."'>";
+            $ret .= "<input type='hidden' name='vert_id' value='".$vertical->vert_id."'>";
+            $ret .= "<input type='submit' name='delete_comm' value='Remove post'>";
+            $ret .= "</form>";
+        }
+    }
+
+
+    return "<body>$ret</body>";
+}
+_code?>
+<=body
+title=><?_code return $title; _code?>
+head<=
+<?_code return $headextra; _code?>
+<=head
+page?>

Modified: trunk/htdocs/admin/browse/index.bml
===================================================================
--- trunk/htdocs/admin/browse/index.bml	2010-11-01 09:35:38 UTC (rev 17625)
+++ trunk/htdocs/admin/browse/index.bml	2010-11-01 10:24:55 UTC (rev 17626)
@@ -24,6 +24,7 @@
     $ret .= "<p><a href='./upload_verticals.bml'>Upload Verticals</a></p>";
     $ret .= "<p><a href='./recent_posts.bml'>Manage Recent Posts</a></p>";
     $ret .= "<p><a href='./top_entries_vertical.bml'>Top Entries in communities</a></p>";
+    $ret .= "<p><a href='./featured_communities.bml'>Featured Communities</a></p>";
     $ret .= "</blockquote>";
 
     $ret .= "<h2>Manage Communities</h2><blockquote>";

Added: trunk/templates/Browse/featured_communities.tmpl
===================================================================
--- trunk/templates/Browse/featured_communities.tmpl	                        (rev 0)
+++ trunk/templates/Browse/featured_communities.tmpl	2010-11-01 10:24:55 UTC (rev 17626)
@@ -0,0 +1,8 @@
+<tmpl_loop top_comms>
+<tmpl_var username><br/>
+<tmpl_var community><br/>
+<tmpl_var userpic><br/>
+<tmpl_var bio><br/>
+<hr/>
+</tmpl_loop>
+

Modified: trunk/templates/Browse/sidebar.tmpl
===================================================================
--- trunk/templates/Browse/sidebar.tmpl	2010-11-01 09:35:38 UTC (rev 17625)
+++ trunk/templates/Browse/sidebar.tmpl	2010-11-01 10:24:55 UTC (rev 17626)
@@ -8,4 +8,6 @@
 <TMPL_VAR add_community_widget>
 
 <tmpl_include top_posts.tmpl>
+<br/>
+<tmpl_include featured_communities.tmpl>
 

Tags: bml, livejournal, pl, pm, tmpl, wisest-owl
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments