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 backups.num 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,
143 INNER JOIN shares ON files.shareID=shares.ID
144 INNER JOIN hosts ON hosts.ID = shares.hostID
145 INNER JOIN backups ON backups.hostID = hosts.ID
146 LEFT JOIN dvds ON dvds.ID = files.dvdid
150 if (defined($where) && $where ne "")
152 $sql .= " WHERE ". $where;
164 my $st = $dbh->prepare(
167 if (!defined($offset) && $offset ne "")
169 $st->bind_param(1, $offset);
173 $st->bind_param(1,0);
180 while ($tmp = $st->fetchrow_hashref())
183 'hname' => $tmp->{'hname'},
184 'sname' => $tmp->{'sname'},
185 'sharename' => $tmp->{'sharename'},
186 'backupno' => $tmp->{'backupNum'},
187 'fname' => $tmp->{'filename'},
188 'fpath' => $tmp->{'filepath'},
189 'networkpath' => $tmp->{'networkPath'},
190 'date' => $tmp->{'date'},
191 'type' => $tmp->{'filetype'},
192 'size' => $tmp->{'size'},
193 'id' => $tmp->{'fid'},
194 'dvd' => $tmp->{'dvd'}
205 sub getBackupsNotBurned()
207 my $dbh = DBI->connect( "dbi:SQLite:dbname=${TopDir}/$Conf{SearchDB}",
208 "", "", { RaiseError => 1, AutoCommit => 1 } );
213 backups.num AS backupno,
214 backups.type AS type,
216 FROM backups, shares, files, hosts
218 backups.num = files.backupNum AND
219 shares.ID = files.shareID AND
220 backups.hostID = shares.hostID AND
221 hosts.ID = backups.hostID AND
224 backups.hostID, backups.num
226 my $st = $dbh -> prepare( $sql );
230 while ( my $tmp = $st -> fetchrow_hashref() )
233 'host' => $tmp->{'host'},
234 'hostid' => $tmp->{'hostID'},
235 'backupno' => $tmp->{'backupno'},
236 'type' => $tmp->{'type'},
237 'date' => $tmp->{'date'}
245 sub displayBackupsGrid()
254 <script language="javascript" type="text/javascript">
257 function checkAll(location)
259 for (var i=0;i<document.forma.elements.length;i++)
261 var e = document.forma.elements[i];
262 if ((e.checked || !e.checked) && e.name != \'all\') {
263 if (eval("document.forma."+location+".checked")) {
274 $retHTML .= q{<form name="forma" method="POST" action="}."$MyURL"."?action=burn\"";
275 $retHTML.= q{<input type="hidden" value="burn" name="action">};
276 $retHTML .= q{<input type="hidden" value="results" name="search_results">};
278 $retHTML .= "<table style=\"fview\">";
282 $retHTML .= "<td class=\"tableheader\"><input type=\"checkbox\" name=\"allFiles\" onClick=\"checkAll('allFiles');\"></td>";
284 $retHTML .= "<td class=\"tableheader\">Host</td> <td class=\"tableheader\">Backup no</td> <td class=\"tableheader\">Type</td> <td class=\"tableheader\">date</td></tr>";
285 my @backups = getBackupsNotBurned();
291 $retHTML .= "<td colspan=7 style=\"tableheader\">";
292 $retHTML .= "<input type=\"submit\" value=\"Burn selected backups on medium\" name=\"submitBurner\">";
297 foreach $backup(@backups)
304 $retHTML .= "<td class=\"fview\"> <input type=\"checkbox\" name=\"fcb"
305 .$backup->{'hostid'}."_".$backup->{'backupno'}
306 ."\" value=\"".$backup->{'hostid'}."_".$backup->{'backupno'}."\"> </td>";
309 $retHTML .= "<td class=\"fviewborder\">" . $backup->{'host'} . "</td>";
310 $retHTML .= "<td class=\"fviewborder\">" . $backup->{'backupno'} . "</td>";
311 $retHTML .= "<td class=\"fviewborder\">" . $backup->{'type'} . "</td>";
312 $retHTML .= "<td class=\"fviewborder\">" . $backup->{'date'} . "<td>";
315 $retHTML .= "</table>";
318 $retHTML .= "</form>";
328 my ($where, $addForm, $offset) = @_;
333 $retHTML .= q{<form name="forma" method="POST" action="}."$MyURL"."?action=search\"";
334 $retHTML.= q{<input type="hidden" value="search" name="action">};
335 $retHTML .= q{<input type="hidden" value="results" name="search_results">};
337 $retHTML .= "<table style=\"fview\">";
339 $retHTML .= "<td class=\"tableheader\">Host</td> <td class=\"tableheader\">Name</td> <td class=\"tableheader\">Type</td> <td class=\"tableheader\">backup no.</td> <td class=\"tableheader\">size</td> <td class=\"tableheader\">date</td> <td class=\"tableheader\">Media</td></tr>";
340 my @files = getFiles($where, $offset);
343 foreach $file(@files)
347 if ($file->{'type'} == BPC_FTYPE_DIR)
356 $retHTML .= "<td class=\"fviewborder\">" . $file->{'hname'} ."</td>";
357 $retHTML .= "<td class=\"fviewborder\">" . $file->{'fname'} . "</td>";
358 $retHTML .= "<td class=\"fviewborder\">" . $ftype . "</td>";
359 $retHTML .= "<td class=\"fviewborder\">" . $file->{'backupno'} . "</td>";
360 $retHTML .= "<td class=\"fviewborder\">" . $file->{'size'} . "</td>";
361 $retHTML .= "<td class=\"fviewborder\">" . $file->{'date'} . "</td>";
362 $retHTML .= "<td class=\"fviewborder\">" . $file->{'dvd'} . "</td>";
365 $retHTML .= "</table>";
369 $retHTML .= "<INPUT TYPE=\"hidden\" VALUE=\"\" NAME=\"offset\">";
370 for (my $ii = 1; $ii <= $#files; $ii++)
372 $retHTML .= "<a href = \"#\" onclick=\"document.forma.offset.value=$ii;document.forma.submit();\">$ii</a>";
382 $retHTML .= "</form>";