Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add `assets_count()` to Desk and use it for desk counts.

This is a class method that returns a hash reference mapping desk IDs to counts of assets on those desks. Desks not in the hash refernce should be considered to have 0 assets.

Using it to provide desk counts when the "Show Desk Asset Counts" preference is on. This should be *far* more efficient than the old approach of loading every asset on every desk. [#239 state:resolved]
  • Loading branch information...
commit a0b62cee96c391e9672fed029d114c14752d6bee 1 parent 97e9831
@theory theory authored
View
9 comp/widgets/wrappers/sideNav.mc
@@ -71,13 +71,18 @@ my $get_cookie = sub {
return exists($cookies{$name}) ? $cookies{$name} : '';
};
+my $asset_counts;
my $desk_caption = sub {
my ($desk_id, $desk_name) = @_;
return $desk_name unless get_pref('Show Desk Asset Counts');
- my $desk = Bric::Biz::Workflow::Parts::Desk->lookup({ id => $desk_id });
- return sprintf("$desk_name (%d)", scalar(@{$desk->assets}));
+ return sprintf "$desk_name (%d)", $asset_counts->{$desk_id} || 0;
};
</%once>\
+<%init>;
+$asset_counts = get_pref('Show Desk Asset Counts')
+ ? Bric::Biz::Workflow::Parts::Desk->asset_counts
+ : undef;
+</%init>
<%perl>;
my $site_id = $c->get_user_cx(get_user_id);
# Make sure we always have a site ID. If the server has just been restarted,
View
48 lib/Bric/Biz/Workflow/Parts/Desk.pm
@@ -398,6 +398,54 @@ B<Notes:> Searches against C<name> and C<description> use the LIKE operator, so
sub list_ids { wantarray ? @{ &$get_em(@_, 1) } : &$get_em(@_, 1) }
+##############################################################################
+
+=item $asset_counts = Bric::Biz::Workflow::Parts::Desk->asset_counts
+
+Returns a hash reference mapping desk IDs to the number of assets on the desk.
+
+B<Throws:>
+
+=over 4
+
+=item *
+
+Unable to prepare SQL statement.
+
+=item *
+
+Unable to connect to database.
+
+=item *
+
+Unable to select column into arrayref.
+
+=item *
+
+Unable to execute SQL statement.
+
+=back
+
+B<Side Effects:> NONE.
+
+=cut
+
+sub asset_counts {
+ # Use desk__id > 0 because the index is defined that way.
+ my $sth = prepare_c qq{
+ SELECT desk__id, COUNT(*) FROM (
+ SELECT desk__id FROM story WHERE desk__id > 0
+ UNION ALL
+ SELECT desk__id FROM media WHERE desk__id > 0
+ UNION ALL
+ SELECT desk__id FROM template WHERE desk__id > 0
+ ) AS c
+ GROUP BY desk__id
+ };
+ $sth->execute;
+ return { map { @{ $_ } } @{ $sth->fetchall_arrayref } };
+}
+
#--------------------------------------#
=back
Please sign in to comment.
Something went wrong with that request. Please try again.