DB/Util/SystemConfig.pm untested

filter: Statementstmt 0.0% 0 / 195 Branchbran 0.0% 0 / 26 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 22 Podpod 0.0% 0 / 17 totaltotal 0.0% 0 / 260 CC 14 SCAR 23.7
CC
14
Cov
0%
delete_all
42.0
process_requests
6.0
display_user_options
6.0
CRAP 10.7
1 # SystemConfig.pm handles administration and configuration requests including
2 # Adding, deleting, and editing users.
3 # Deleting all playlists
4 # Setting up overall formatting issues
5  
6 # This has a *lot* of hard-coded formatting
7 # Would instead prefer to return each of these
8 # letting users format them at will
9  
10 package Audio::DB::Util::SystemConfig;
11  
12 # $Id: SystemConfig.pm,v 1.2 2005/02/27 16:56:25 todd Exp $
13  
14 use strict 'vars';
15 use vars qw($VERSION @ISA);
16 use lib '/Users/todd/projects_personal';
17 use CGI qw/:standard *table *TR *td *div/;
18 use Audio::DB::Web;
19 @ISA = 'Audio::DB::Web';
20  
21 my %links = (
22 add_user => 'display_user_options',
23 edit_users => 'display_user_options',
24 delete_user => 'display_user_options',
25 delete_all_users => 'display_user_options',
26 user_summary => 'display_user_options',
27 delete_all_playlists => 'display_playlist_options');
28  
29  
30  
31 my $MAIN = 'System Configuration';
32  
33  
34 # Check to see if the current user has permissions to do whatever they are trying to do
35 sub check_permissions {
36  
37 }
38  
39 # Need to store a dbh handle here
40 sub new {
41 my ($self,$dbh) = @_;
42 my $this = bless { dbh => $dbh },$self;
43 $this->process_requests();
44 return $this;
45 }
46  
47  
48 sub process_requests {
49 my $self = shift;
50 my $coderef = url_param('admin');
51 $self->$coderef();
52  
53 # Print out the original option list for the subcategory
54 # I am working within
55 if (my $footer_ref = $links{$coderef}) { $self->$footer_ref(1); }
56 print end_div;
57 }
58  
59 sub display_options {
60 my $self = shift;
61 $self->print_navigation();
62 print start_div({-class=>'configoptions'});
63 print h4($self->build_nav_link(-class=>'admin',-action=>'display_user_options',
64 -text=>'User Management'));
65 print h4($self->build_nav_link(-class=>'admin',-action=>'display_playlist_options',
66 -text=>'Global Playlist Management'));
67 }
68  
69  
70  
71 ##################################################
72 # USER MANAGEMENT SUBROUTINES
73 ##################################################
74 sub display_user_options {
75 my ($self,$suppress) = @_;
76
77 # I don't want to doubly print the navigation...
78 unless ($suppress) {
79 $self->print_navigation($self->build_nav_link(-class=>'admin',-action=>'display_user_options',
80 -text=>'User Configuration'));
81 }
82
83 print start_div({-class=>'configoptions'});
84 print h4($self->build_nav_link(-class=>'admin',-action=>'add_user',-text=>'Add A New User'));
85 print h4($self->build_nav_link(-class=>'admin',-action=>'edit_users',-text=>'Edit Users'));
86 print h4($self->build_nav_link(-class=>'admin',-action=>'delete_user',-text=>'Delete A User')
87 . ' | ' .
88 $self->build_nav_link(-class=>'admin',-action=>'delete_all_users',-text=>'Delete All Users'));
89 print h4($self->build_nav_link(-class=>'admin',-action=>'user_summary',-text=>'View All Users'));
90 }
91  
92  
93 sub display_playlist_options {
94 my ($self,$suppress) = @_;
95
96 # I don't want to doubly print the navigation...
97 unless ($suppress) {
98 $self->print_navigation($self->build_nav_link(-class=>'admin',-action=>'display_playlist_options',
99 -text=>'Global Playlist Options'));
100 }
101
102 print start_div({-class=>'configoptions'});
103 print h4($self->build_nav_link(-class=>'admin',-action=>'delete_all_playlists',
104 -text=>'Delete All Playlists'));
105 }
106  
107 sub add_user {
108 my $self = shift;
109 if (param('submit')) {
110 $self->print_navigation($self->build_nav_link(-class=>'admin',-action=>'display_user_options',
111 -text=>'User Configuration'),
112 'Add A New User (Results)');
113 $self->add_entry(-table => 'users',
114 -success_msg => span({-class=>'name'},param('first_name')
115 . ' ' . param('last_name')) .
116 ' has been succesfully added to the system with the following parameters:',
117 -detailed => 'true');
118 } else {
119 $self->print_navigation($self->build_nav_link(-class=>'admin',-action=>'display_user_options',
120 -text=>'User Configuration'),
121 $self->build_nav_link(-class=>'admin',-action=>'add_user',
122 -text=>'Add A New User'));
123 $self->print_add_form();
124 }
125 }
126  
127  
128 sub edit_users {
129 my $self = shift;
130 if (param('submit')) {
131 $self->print_navigation($self->build_nav_link(-class=>'admin',-action=>'display_user_options',
132 -text=>'User Configuration'),
133 'Edit Users (Results)');
134 $self->process_edit_form();
135 } else {
136 $self->print_navigation($self->build_nav_link(-class=>'admin',-action=>'display_user_options',
137 -text=>'User Configuration'),
138 $self->build_nav_link(-class=>'admin',-action=>'edit_users',
139 -text=>'Edit Users'));
140 $self->print_edit_form();
141 }
142 }
143  
144 sub print_add_form {
145 my $self = shift;
146 # Print out the form
147
148 print div({-class=>'actionbyline'},"Use this form to add new users to the Music Repository");
149 print start_div({-class=>'actioncontent'});
150 print startform(),
151 start_table(),
152 TR(td('first name'),td(textfield({-name=>'first_name'}))),
153 TR(td('last name'),td(textfield({-name=>'last_name'}))),
154 TR(td('email'),td(textfield({-name=>'email'}))),
155 TR(td('username'),td(textfield({-name=>'username'}))),
156 TR(td('password'),td(textfield({-name=>'password'}))),
157 TR(td('priviledges'),
158 td(popup_menu({-name=>'privs',-values=>[qw/user admin overlord/]}))),
159 TR(td({-colspan=>2,-align=>'right'},
160 submit(-name=>'submit',-label=>'Add New User'))),
161 end_form,
162 end_table;
163 print end_div;
164 }
165  
166 sub print_edit_form {
167 my $self = shift;
168 my @cols = qw/First Last Email Username Password Priviledges/;
169 my $dbh = $self->{dbh};
170 my $sth = $dbh->prepare(qq{select * from users});
171 print div({-class=>'actionbyline'},
172 "Use this form to edit the information of all users simultaneously.");
173 print start_div({-class=>'actioncontent'});
174 print startform(),
175 start_table();
176 print TR(td(\@cols));
177 $sth->execute();
178 while (my $h = $sth->fetchrow_hashref) {
179 print
180 hidden(-name=>'user_id',-value=>$h->{user_id});
181 print
182 TR(td(textfield({-name=>'first_name',-value=>$h->{first_name},-size=>15})),
183 td(textfield({-name=>'last_name',-value=>$h->{last_name},-size=>15})),
184 td(textfield({-name=>'email',-value=>$h->{email},-size=>20})),
185 td(textfield({-name=>'username',-value=>$h->{username},-size=>10})),
186 td(textfield({-name=>'password',-value=>$h->{password},-size=>10})),
187 td(popup_menu({-name=>'privs',-values=>[qw/user admin overlord/],
188 -default=>$h->{privs}})));
189 }
190 print end_table;
191 print submit(-name=>'submit',-label=>'Update Users');
192 print end_div;
193 }
194  
195  
196 sub process_edit_form {
197 my $self = shift;
198 my $dbh = $self->{dbh};
199 # iterate over each param...
200 my @fields = qw/first_name last_name email username password privs/;
201 my @params = param();
202 my $params = { id => [ param('user_id') ],
203 first_name => [ param('first_name') ],
204 last_name => [ param('last_name') ],
205 email => [ param('email') ],
206 username => [ param('username') ],
207 password => [ param('password') ],
208 privs => [ param('privs') ] };
209
210 # Yuck, there has to be a better way
211 my $count = 0;
212 my $result;
213 foreach my $id (@{$params->{id}}) {
214 my @vals;
215 foreach (@fields) {
216 push (@vals,$_ . "=" . $dbh->quote($params->{$_}[$count]));
217 }
218  
219 $result = $dbh->do("update users set "
220 . join(",",@vals)
221 . " where user_id=$id");
222 $count++;
223 }
224 # This is kind of bogus - only looks at the last record updated.
225 if ($result) {
226 print div({-class=>'success'},"The users have been succesfully updated.");
227 } else {
228 $self->print_sql_error($dbh->errstr);
229 }
230 }
231  
232  
233 sub user_summary {
234 my $self = shift;
235 $self->print_navigation($self->build_nav_link(-class=>'admin',-action=>'display_user_options',
236 -text=>'User Configuration'),
237 $self->build_nav_link(-class=>'admin',-action=>'user_summary',
238 -text=>'View All Users'));
239 my $dbh = $self->{dbh};
240 my $sth = $dbh->prepare(qq{select * from users ORDER BY last_name});
241 $sth->execute();
242
243 my @cols = ('First','Last','Email','Username','Status','Joined','Last Login','Songs Played');
244 my @keys = ('first_name','last_name','email','username','privs','joined','last_access','songs_played');
245
246 my $rows = [];
247 while (my $h = $sth->fetchrow_hashref) {
248 my @row;
249 foreach (@keys) {
250 push (@row,$h->{$_});
251 }
252 push (@$rows,[ @row ]);
253 }
254 if (@$rows) {
255 print div({-class=>'actionbyline'},"Summary Information For All Users");
256 print start_div({-class=>'actioncontent'});
257 print start_table();
258 print TR(td(\@cols));
259 foreach (@$rows) {
260 print TR(td(\@$_));
261 }
262 print end_table;
263 print end_div;
264 } else {
265 print div({-class=>'warning'},"There are no users currently in the database");
266 }
267 }
268  
269  
270 # First parameter is for establishing the navigation
271 sub delete_all_users {
272 my $self = shift;
273 $self->delete_all('User Configuration','users');
274 }
275  
276 # Generalized delete all
277  
278 # THIS IS KIND OF SILLY, AND MOSTLY JUST USED FOR DEBUGGING.
279 # Since ALL PLAYLISTS ARE ATACHED TO USERS
280 # I MUST NECESSARILY EMPTY OUT THE PLAYLISTS AND PLAYLIST_SONGS TABLES AS WELL
281 sub delete_all {
282 my ($self,$text,$table) = @_;
283 my $param = url_param('admin');
284 my $action = $links{$param};
285 if (param('delete')) {
286 $self->print_navigation($self->build_nav_link(-class=>'admin',-action=>$action,-text=>$text),
287 'Delete All ' . uc $table . ' (Results)');
288 my $dbh = $self->{dbh};
289 my $result = $dbh->do(qq{delete from } . $table);
290 # If I am deleting playlists, then I also need to empty out the songs table
291 if ($table eq 'playlists') {
292 my $result = $dbh->do(qq{delete from playlist_songs});
293 }
294
295 if ($table eq 'users') {
296 my $result = $dbh->do(qq{delete from playlist_songs});
297 my $result = $dbh->do(qq{delete from playlists});
298 }
299
300 if ($result) {
301 print div({-class=>'success'},"All " . $table . " were deleted from the database.");
302 } else {
303 $self->print_sql_error($dbh->errstr);
304 }
305 } elsif (param('submit') eq 'Cancel') {
306 $self->print_navigation(
307 $self->build_nav_link(-class=>'admin',-action=>$action,-text=>$text),
308 'Delete All ' . uc $table . ' (Cancelled)');
309 print div({-class=>'success'},'The deletion was cancelled; no ' . $table . ' were deleted.');
310 } else {
311 $self->print_navigation(
312 $self->build_nav_link(-class=>'admin',-action=>$action,-text=>$text),
313 'Delete All ' . uc $table . ' (Confirmation)');
314 print start_div({-class=>'warning'}),"You are about to delete ALL " . $table .
315 " from the database.";
316 print start_form;
317 print
318 submit({-label=>'Delete All ' . uc $table,-name=>'delete'}),
319 submit(-label=>'Cancel',-name=>'submit');
320 print end_form,end_div;
321 }
322 }
323  
324  
325  
326 ##################################################
327 # GLOBAL PLAYLIST MANAGEMENT SUBROUTINES
328 ##################################################
329 sub delete_all_playlists {
330 my $self = shift;
331 $self->delete_all('Global Playlist Options','playlists');
332 }
333  
334  
335  
336 ##################################################
337 # USER INTERFACE CONFIGURATION SUBROUTINES
338 ##################################################
339 # THIS CONTROLS NAVIGATIONAL FORMATTING
340 sub print_navigation {
341 my ($self,@fields) = @_;
342 print start_div({-class=>'actiontitle'}),
343 $self->build_nav_link(-class=>'admin',-action=>'display_options',
344 -text=>$MAIN);
345 print ' / ' if (@fields);
346 print join (' / ',@fields);
347 print end_div;
348 }
349  
350  
351  
352 sub print_sql_err {
353 my $err;
354 print span({-class=>'error'},$err);
355 }
356  
357  
358  
359  
360  
361  
362 1;
363  
364 =pod
365 Methods:
366 user management
367 multiplaylists / user (and option to share with others)
368
369 user ratings (for songs and playlists)
370
371 browse by letter of alphabet, genre, album
372 Stats reporting
373  
374  
375 =head1 NAME
376  
377 Apache::Audio::DB - Generate a database of your tunes complete with searchable interface and nifty statistical analyses!
378  
379 =head1 SYNOPSIS
380  
381 # httpd.conf or srm.conf
382 AddType audio/mpeg mp3 MP3
383
384 # httpd.conf or access.conf
385 <Location /songs>
386 SetHandler perl-script
387 PerlHandler Apache::MP3::Sorted
388 PerlSetVar SortFields Album,Title,-Duration
389 PerlSetVar Fields Title,Artist,Album,Duration
390 </Location>
391  
392 =head1 TODO
393  
394  
395 Streaming code and links
396  
397 BUILDING URLs from non-blessed items...need to handle this because
398 it will come up alot
399  
400 DB.pm Use lincolns code for scanning files
401  
402 Browse filesystem
403  
404 Update DB scripts
405  
406 HTML module of common formatting options
407 configuration page...
408 limits to return
409 option to download tarballs
410
411 Figure out how to track paths and such...
412 Preserving this state is kinda hairy
413  
414 CLEAN UP THE BUILD URL SUB
415 OPTIMIZE QUERIES AND OBJECT CONSTRUCTION
416 SEPEERATE OUT OBJECT CODE
417  
418 Seperate out HTML formatting into a seperate module
419  
420 Error checking and handling
421  
422 Streaming code
423 Column Sorting
424 User management
425 Playlist integration
426 Playlist sharing
427 Multiple Playlists
428 Interface preferences
429  
430  
431 =head1 DESCRIPTION
432  
433 Apache::Audio::DB subclasses Apache::MP3 to generate a relational database
434 of your music collection. This allows browsing by various criteria that
435 are not available when simply browsing the filesystem. For example,
436 users my browse by genre, year, or era. Apache::Audio::DB also provides
437 search capabilities.
438  
439 =head1 CUSTOMIZING
440  
441 This class adds several new Apache configuration variable.
442  
443 Database specific variables:
444 ----------------------------
445 Value Default
446 PerlSetVar DB_Name database name musicdb
447 PerlSetVar Create boolean no
448 PerlSetVar Host database user name localhost
449 PerlSetVar User user name
450 PerlSetVar Password db password
451
452 =over 4
453  
454 =item B<DB_Name> I<field>
455  
456 This is the name of the database. If not provided, musicdb will be used.
457  
458 =item B<Create>
459  
460 Examples:
461  
462 PerlSetVar SortFields Album,Title # sort ascending by album, then title
463 PerlSetVar SortFields +Artist,-Kbps # sort ascending by artist, descending by kbps
464  
465 When constructing a playlist from a recursive directory listing,
466 sorting will be B<global> across all directories. If no sort order is
467 specified, then the module reverts to sorting by file and directory
468 name. A good value for SortFields is to sort by Artist,Album and
469 track:
470  
471 PerlSetVar SortFields Artist,Album,Track
472  
473 Alternatively, you might want to sort by Description, which
474 effectively sorts by title, artist and album.
475  
476 The following are valid fields:
477  
478 Field Description
479
480 album The album
481 artist The artist
482 bitrate Streaming rate of song in kbps
483 comment The comment field
484 description Description, as controlled by DescriptionFormat
485 duration Duration of the song in hour, minute, second format
486 filename The physical name of the .mp3 file
487 genre The genre
488 samplerate Sample rate, in KHz
489 seconds Duration of the song in seconds
490 title The title of the song
491 track The track number
492  
493 Field names are case insensitive.
494  
495 =back
496  
497 =head1 METHODS
498  
499 Apache::MP3::Sorted overrides the following methods:
500  
501 sort_mp3s() mp3_table_header() mp3_list_bottom()
502  
503 It adds one new method:
504  
505 =over 4
506  
507 =item $field = $mp3->sort_fields
508  
509 Returns a list of the names of the fields to sort on by default.
510  
511  
512  
513 #### UI ELEMENTS THAT HAVEN'T BEEN REWORKED YET
514 #sub amg_link {
515 # my ($artist,$link_text) = @_;
516
517 # # Encode $artist for searching: UC, replace \s with |, replace ' and ,
518 # my $artist_encoded = uc $artist;
519 # $artist_encoded =~ s/\s/|/g;
520 # $artist_encoded =~ s/[,\'\-]//g;
521 # my $base_url = 'http://allmusic.com/cg/x.dll?p=amg&optl=1&sql=1';
522
523 # my $full_url = $base_url . $artist_encoded;
524
525 # # Formulate the AMG URL
526 # my $amg_link = '<a href="' . $base_url . $artist_encoded . '">' .
527 # $link_text . "</a>";
528
529 # return($amg_link);
530 #}
531  
532 #sub print_search_button {
533 # print center(
534 # startform(-action=>$full_url),
535 # submit(-name=>'submit',
536 # -value=>'Search Again'),
537 # endform);
538 # return;
539 #}