2 package BackupPC::SearchLib;
5 use BackupPC::CGI::Lib qw(:all);
6 use BackupPC::Attrib qw(:all);
13 my $dbh = DBI->connect( "dbi:SQLite:dbname=${TopDir}/$Conf{SearchDB}",
14 "", "", { RaiseError => 1, AutoCommit => 1 } );
17 " SELECT shares.ID AS ID, shares.share AS name FROM shares;");
19 push (@ret, { 'ID' => '', 'name' => '-'});
20 while ( $tmp = $st->fetchrow_hashref() ) {
21 push( @ret, { 'ID' => $tmp->{'ID'}, 'name' => $tmp->{'name'} } );
36 if ( defined( $param->{'search_backup_day_from'} ) && $param->{'search_backup_day_from'} ne "") {
38 ' strftime("%d", datetime(backups.date, "unixepoch","localtime")) >= "'
39 . $param->{'search_backup_day_from'} ."\"");
41 if ( defined( $param->{'search_backup_day_to'} ) && $param->{'search_backup_day_to'} ne "") {
43 ' strftime("%d", datetime(backups.date, "unixepoch","localtime")) <= "'
44 . $param->{'search_backup_day_from'} ."\"");
46 if ( defined( $param->{'search_backup_month_from'} ) && $param->{'search_backup_month_from'} ne "") {
48 ' strftime("%m", datetime(backups.date, "unixepoch","localtime")) >= "'
49 . $param->{'search_backup_month_from'} ."\"");
51 if ( defined( $param->{'search_backup_month_to'} ) && $param->{'search_backup_month_to'} ne "") {
53 ' strftime("%m", datetime(backups.date, "unixepoch","localtime")) <= "'
54 . $param->{'search_backup_month_to'} ."\"");
56 if ( defined( $param->{'search_backup_year_from'} ) && $param->{'search_backup_year_from'} ne "") {
58 ' strftime("%Y", datetime(backups.date, "unixepoch","localtime")) >= "'
59 . $param->{'search_backup_year_from'} ."\"");
61 if ( defined( $param->{'search_backup_year_to'} ) && $param->{'search_backup_year_to'} ne "") {
63 ' strftime("%Y", datetime(backups.date, "unixepoch","localtime")) <= "'
64 . $param->{'search_backup_year_to'} ."\"");
67 if ( defined( $param->{'search_day_from'} ) && $param->{'search_day_from'} ne "" ) {
69 ' strftime("%d", datetime(files.date, "unixepoch","localtime")) >= "'
70 . $param->{'search_day_from'} ."\"");
72 if ( defined( $param->{'search_month_from'} ) && $param->{'search_month_from'} ne "") {
74 ' strftime("%m", datetime(files.date, "unixepoch","localtime")) >= "'
75 . $param->{'search_month_from'} ."\"");
77 if ( defined( $param->{'search_year_from'} ) && $param->{'search_year_from'} ne "") {
79 ' strftime("%Y", datetime(files.date, "unixepoch","localtime")) >= "'
80 . $param->{'search_year_from'} ."\"");
82 if ( defined( $param->{'search_day_to'} ) && $param->{'search_day_to'} ne "" ) {
84 ' strftime("%d", datetime(files.date, "unixepoch","localtime")) <= "'
85 . $param->{'search_day_to'} ."\"");
87 if ( defined( $param->{'search_month_to'} ) && $param->{'search_month_to'} ne "" ) {
89 ' strftime("%m", datetime(files.date, "unixepoch","localtime")) <= "'
90 . $param->{'search_month_to'} ."\"" );
92 if ( defined( $param->{'search_year_to'} )&& $param->{'search_year_to'} ne "" ) {
94 ' strftime("%Y", datetime(files.date, "unixepoch","localtime")) <= "'
95 . $param->{'search_year_to'} ."\"");
98 if ( defined( $param->{'search_host'} ) && $param->{'search_host'} ne "") {
99 push( @conditions, ' backups.hostID = ' . $param->{'search_host'} );
102 if ( defined ($param->{'search_filename'}) && $param->{'search_filename'} ne "") {
103 push (@conditions, " files.name LIKE '".$param->{'search_filename'}."%'");
107 foreach $cond(@conditions)
122 my ($where, $offset) = @_;
125 my $dbh = DBI->connect( "dbi:SQLite:dbname=${TopDir}/$Conf{SearchDB}",
126 "", "", { RaiseError => 1, AutoCommit => 1 } );
129 SELECT files.id AS fid,
131 shares.name AS sname,
132 shares.share AS sharename,
133 files.backupNum AS backupNum,
134 files.name AS filename,
135 files.path AS filepath,
136 shares.share||files.fullpath AS networkPath,
137 date(files.date, 'unixepoch', 'localtime') AS date,
138 files.type AS filetype,
142 INNER JOIN shares ON files.shareID=shares.ID
143 INNER JOIN hosts ON hosts.ID = shares.hostID
144 LEFT JOIN dvds ON dvds.ID = files.dvdid
147 if (defined($where) && $where ne "")
149 $sql .= " WHERE ". $where;
161 my $st = $dbh->prepare(
164 if (!defined($offset) && $offset ne "")
166 $st->bind_param(1, $offset);
170 $st->bind_param(1,0);
177 while ($tmp = $st->fetchrow_hashref())
180 'hname' => $tmp->{'hname'},
181 'sname' => $tmp->{'sname'},
182 'sharename' => $tmp->{'sharename'},
183 'backupno' => $tmp->{'backupNum'},
184 'fname' => $tmp->{'filename'},
185 'fpath' => $tmp->{'filepath'},
186 'networkpath' => $tmp->{'networkPath'},
187 'date' => $tmp->{'date'},
188 'type' => $tmp->{'filetype'},
189 'size' => $tmp->{'size'},
190 'id' => $tmp->{'fid'},
191 'dvd' => $tmp->{'dvd'}
202 sub getBackupsNotBurned()
204 my $dbh = DBI->connect( "dbi:SQLite:dbname=${TopDir}/$Conf{SearchDB}",
205 "", "", { RaiseError => 1, AutoCommit => 1 } );
210 backups.num AS backupno,
211 backups.type AS type,
213 FROM backups, shares, files, hosts
215 backups.num = files.backupNum AND
216 shares.ID = files.shareID AND
217 backups.hostID = shares.hostID AND
218 hosts.ID = backups.hostID AND
221 backups.hostID, backups.num
223 my $st = $dbh -> prepare( $sql );
227 while ( my $tmp = $st -> fetchrow_hashref() )
230 'host' => $tmp->{'host'},
231 'hostid' => $tmp->{'hostID'},
232 'backupno' => $tmp->{'backupno'},
233 'type' => $tmp->{'type'},
234 'date' => $tmp->{'date'}
242 sub displayBackupsGrid()
251 <script language="javascript" type="text/javascript">
254 function checkAll(location)
256 for (var i=0;i<document.forma.elements.length;i++)
258 var e = document.forma.elements[i];
259 if ((e.checked || !e.checked) && e.name != \'all\') {
260 if (eval("document.forma."+location+".checked")) {
271 $retHTML .= q{<form name="forma" method="POST" action="}."$MyURL"."?action=burn\"";
272 $retHTML.= q{<input type="hidden" value="burn" name="action">};
273 $retHTML .= q{<input type="hidden" value="results" name="search_results">};
275 $retHTML .= "<table style=\"fview\">";
279 $retHTML .= "<td class=\"tableheader\"><input type=\"checkbox\" name=\"allFiles\" onClick=\"checkAll('allFiles');\"></td>";
281 $retHTML .= "<td class=\"tableheader\">Host</td> <td class=\"tableheader\">Backup no</td> <td class=\"tableheader\">Type</td> <td class=\"tableheader\">date</td></tr>";
282 my @backups = getBackupsNotBurned();
288 $retHTML .= "<td colspan=7 style=\"tableheader\">";
289 $retHTML .= "<input type=\"submit\" value=\"Burn selected backups on medium\" name=\"submitBurner\">";
294 foreach $backup(@backups)
301 $retHTML .= "<td class=\"fview\"> <input type=\"checkbox\" name=\"fcb"
302 .$backup->{'hostid'}."_".$backup->{'backupno'}
303 ."\" value=\"".$backup->{'hostid'}."_".$backup->{'backupno'}."\"> </td>";
306 $retHTML .= "<td class=\"fviewborder\">" . $backup->{'host'} . "</td>";
307 $retHTML .= "<td class=\"fviewborder\">" . $backup->{'backupno'} . "</td>";
308 $retHTML .= "<td class=\"fviewborder\">" . $backup->{'type'} . "</td>";
309 $retHTML .= "<td class=\"fviewborder\">" . $backup->{'date'} . "<td>";
312 $retHTML .= "</table>";
315 $retHTML .= "</form>";
323 sub displayGrid($$$$) {
324 my ($where, $addForm, $offset, $hilite) = @_;
328 $retHTML .= qq{<form name="forma" method="POST" action="}.$MyURL.qq{?action=search">};
329 $retHTML.= qq{<input type="hidden" value="search" name="action">};
330 $retHTML .= qq{<input type="hidden" value="results" name="search_results">};
333 <table style="fview" width="100%">
335 <td class="tableheader">Host</td>
336 <td class="tableheader">Type</td>
337 <td class="tableheader">Name</td>
338 <td class="tableheader">backup no.</td>
339 <td class="tableheader">size</td>
340 <td class="tableheader">date</td>
341 <td class="tableheader">Media</td>
344 my @files = getFiles($where, $offset);
347 sub hilite_html($$) {
348 my ($html, $search) = @_;
349 $html =~ s#($search)#<b>$1</b>#gis;
353 foreach $file (@files) {
355 $ftype = "dir" if ($file->{'type'} == BPC_FTYPE_DIR);
362 hilite_html( $file->{'fpath'}, $hilite ),
368 $retHTML .= qq{<td class="fviewborder">$v</td>};
373 $retHTML .= "</table>";
378 $retHTML .= "<INPUT TYPE=\"hidden\" VALUE=\"\" NAME=\"offset\">";
379 for (my $ii = 1; $ii <= $#files; $ii++) {
380 $retHTML .= "<a href = \"#\" onclick=\"document.forma.offset.value=$ii;document.forma.submit();\">$ii</a>";
386 $retHTML .= "</form>" if ($addForm);