Changeset 314 for trunk/DNSDB.pm


Ignore:
Timestamp:
04/24/12 16:43:39 (12 years ago)
Author:
Kris Deugau
Message:

/trunk

Move SQL for "Manage groups" to DNSDB.pm. See #1
Extend new group list sub to return a revzone count. See #26

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/DNSDB.pm

    r312 r314  
    4242        &getZoneCount &getZoneList
    4343        &addGroup &delGroup &getChildren &groupName
     44        &getGroupCount &getGroupList
    4445        &addUser &updateUser &delUser &userFullName &userStatus &getUserData
    4546        &getSOA &updateSOA &getRecLine &getDomRecs &getRecCount
     
    6465                &getZoneCount &getZoneList
    6566                &addGroup &delGroup &getChildren &groupName
     67                &getGroupCount &getGroupList
    6668                &addUser &updateUser &delUser &userFullName &userStatus &getUserData
    6769                &getSOA &updateSOA &getRecLine &getDomRecs &getRecCount
     
    21472149  return $groupname if $groupname;
    21482150} # end groupName
     2151
     2152
     2153## DNSDB::getGroupCount()
     2154# Get count of subgroups in group or groups
     2155# Takes a database handle and hash containing:
     2156#  - the "current" group
     2157#  - an array of "acceptable" groups
     2158#  - Optionally accept a "starts with" and/or "contains" filter argument
     2159# Returns an integer count of the resulting group list.
     2160sub getGroupCount {
     2161  my $dbh = shift;
     2162
     2163  my %args = @_;
     2164
     2165  my @filterargs;
     2166
     2167  $args{startwith} = undef if $args{startwith} && $args{startwith} !~ /^(?:[a-z]|0-9)$/;
     2168  push @filterargs, "^$args{startwith}" if $args{startwith};
     2169  push @filterargs, $args{filter} if $args{filter};
     2170
     2171  my $sql = "SELECT count(*) FROM groups ".
     2172        "WHERE parent_group_id IN ($args{curgroup}".($args{childlist} ? ",$args{childlist}" : '').")".
     2173        ($args{startwith} ? " AND group_name ~* ?" : '').
     2174        ($args{filter} ? " AND group_name ~* ?" : '');
     2175  my ($count) = $dbh->selectrow_array($sql, undef, (@filterargs) );
     2176  $errstr = $dbh->errstr if !$count;
     2177  return $count;
     2178} # end getGroupCount
     2179
     2180
     2181## DNSDB::getGroupList()
     2182# Get a list of sub^n-groups in the specified group(s)
     2183# Takes the same arguments as getGroupCount() above
     2184# Returns an arrayref containing hashrefs suitable for feeding straight to HTML::Template
     2185sub getGroupList {
     2186  my $dbh = shift;
     2187
     2188  my %args = @_;
     2189
     2190  my @filterargs;
     2191
     2192  $args{startwith} = undef if $args{startwith} && $args{startwith} !~ /^(?:[a-z]|0-9)$/;
     2193  push @filterargs, "^$args{startwith}" if $args{startwith};
     2194  push @filterargs, $args{filter} if $args{filter};
     2195
     2196  # protection against bad or missing arguments
     2197  $args{sortorder} = 'ASC' if !$args{sortorder};
     2198  $args{offset} = 0 if !$args{offset};
     2199
     2200  # munge sortby for columns in database
     2201  $args{sortby} = 'g.group_name' if $args{sortby} eq 'group';
     2202  $args{sortby} = 'g2.group_name' if $args{sortby} eq 'parent';
     2203
     2204  my $sql = q(SELECT g.group_id AS groupid, g.group_name AS groupname, g2.group_name AS pgroup,
     2205        count(distinct(u.username)) AS nusers, count(distinct(d.domain)) AS ndomains,
     2206        count(distinct(r.revnet)) AS nrevzones
     2207        FROM groups g
     2208        INNER JOIN groups g2 ON g2.group_id=g.parent_group_id
     2209        LEFT OUTER JOIN users u ON u.group_id=g.group_id
     2210        LEFT OUTER JOIN domains d ON d.group_id=g.group_id
     2211        LEFT OUTER JOIN revzones r ON r.group_id=g.group_id
     2212        ).
     2213        "WHERE g.parent_group_id IN ($args{curgroup}".($args{childlist} ? ",$args{childlist}" : '').")".
     2214        ($args{startwith} ? " AND g.group_name ~* ?" : '').
     2215        ($args{filter} ? " AND g.group_name ~* ?" : '').
     2216        " GROUP BY g.group_id, g.group_name, g2.group_name ".
     2217        " ORDER BY $args{sortby} $args{sortorder} ".
     2218        ($args{offset} eq 'all' ? '' : " LIMIT $config{perpage} OFFSET ".$args{offset}*$config{perpage});
     2219  my $glist = $dbh->selectall_arrayref($sql, { Slice => {} }, (@filterargs) );
     2220  $errstr = $dbh->errstr if !$glist;
     2221  return $glist;
     2222} # end getGroupList
    21492223
    21502224
Note: See TracChangeset for help on using the changeset viewer.