root/tags/09nov/table.php

Revision 3, 11.8 kB (checked in by chriskeene, 10 months ago)

add files

Line 
1<?php
2
3///////////////////////////////////////////////////
4include 'include.php';
5
6
7$debug=1;
8
9// process some common arguments and set some defaults
10process_args();
11
12$pagetitle = "$currentCountryName Respositories : table of record totals";
13
14$ircount_table = "ircount";
15
16///////////////////////////////////////////////////
17// read passed params if any...
18$order1 = $_GET['order'];
19$freq = $_GET['frequency'];
20
21//set defaults if no varibles set in get request
22if ($order1 == "") {
23        $order1 = "archivename";
24}
25if ($freq == "") {
26    $freq = "weekly";
27}
28$output1 = $output;
29
30// db table name
31$tablename = $ircount_table;
32$pagefilename = $_SERVER['SCRIPT_NAME'];
33
34
35
36
37
38// var to hold any error messages
39$warningmsg;
40
41//////////////////////////////////////////////////
42// FUNCTIONS
43//////////////////////////////////////////////////
44// linktome
45// make a link back to this page with the correct
46// (ie values that have been set) arguments.
47// the function is passed the arg name and its value
48//
49function linktome ($varname, $varvalue) {
50    $pagefilename = $_SERVER['SCRIPT_NAME'];
51    global $pagefilename, $order1, $output1, $freq, $warningmsg, $country;
52    //
53    // use varible varibles
54    // first keep a copy of what the real var value is
55    $tmpvar = $$varname;
56    // now set whatever var name we were passed to its value
57    // for the purpose of making this link
58    $$varname = $varvalue;
59    // now set the query string
60    $tmpreturn = "$pagefilename?order=$order1&output=$output1&frequency=$freq&country=$country";   
61    // reset what ever value it is to its orig value.
62    $$varname = $tmpvar;
63    //$warningmsg .= "<p>BB varname $varname tmpvalue $tmpvar</p>\n";
64    return "$tmpreturn";
65}
66
67
68//////////////////////////////////////////////////////
69// table_column
70// we do show this at top and bottom so may as well make
71// it a function
72function table_column ($collected_dates, $freq="monthly") {
73    foreach ($collected_dates as $cdate => $totcdate) {
74        list($year, $month, $day) = split('[/.-]', $cdate);
75        $tmpurl = linktome("order1", "$cdate");
76        if ($freq == "weekly") {
77                echo "<th><a href=\"$tmpurl\">$day/$month<br />$year</a></th>\n";
78        } else {
79                echo "<th><a href=\"$tmpurl\">$month $year</a></th>\n";
80        }
81    }
82}
83
84///////////////////////////////////////////////////////////////
85// if user wants text file prompt to download.
86if ($output1 == "tsv") {
87    // If no headers are sent, send one
88    if (!headers_sent()) {
89           //header('Location: http://www.example.com/');
90        header('Content-type: text/tab-separated-values');
91        header('Content-Disposition: attachment; filename="ir_records.tsv"');
92    }
93}
94elseif ($output1 == "csv") {
95    // If no headers are sent, send one
96        if (!headers_sent()) {
97                header('Content-type: text/plain');
98                header('Content-Disposition: attachment; filename="ir_records.csv"');
99        }
100}
101
102
103// only print the following html if we want html
104if ($output1=="html") {
105    printhtmlhead ($pagetitle);
106}
107
108
109
110
111
112
113
114//####################################################
115// Connection to DB
116connectdb();
117
118
119
120
121//###################################################
122// Print out title and top of page stuff
123if ($output1=="html") {
124echo "<h1>$pagetitle</h1>";
125echo "<p>Ordered by: <em>$order1</em>.  \n";
126echo "Frequency: <em>$freq</em>  change to:";
127if ($freq != "weekly") {
128        // not using linktome function as we don't
129        // want to use the current order date if changing from
130        // weekly to monthly
131        //$tmpurl = linktome("freq", "weekly");
132        echo " [<a href=\"?frequency=weekly&country=$country\">weekly</a>] \n";
133}
134if ($freq != "monthly") {
135        //$tmpurl = linktome("freq", "monthly");
136        echo " [<a href=\"?frequency=monthly&country=$country\">monthly</a>] \n";
137}
138echo "</p>\n";
139
140$tmpurl = linktome("output1", "tsv");
141$tmpurl2 = linktome("output1", "csv");
142echo "<p><strong>Export to text</strong>: <a href=\"$tmpurl\">tab seperated</a> or <a href=\"$tmpurl2\">comma seperate fields enclosed by doublequotes</a>.</p>\n";
143
144
145
146}
147
148//###################################################
149// Database connection and SQL
150// get a list of uniqure archive names
151$query = "
152SELECT DISTINCT archivename, identifier, country
153FROM $ircount_table
154WHERE country = '$country'
155";
156
157
158$result_sussex = mysql_query($query) or die("Query failed : " . mysql_error());
159$fields=mysql_num_rows($result_sussex);
160
161
162
163//#####################################################
164// Debug info
165// before we do much, print out some html comment debug
166if ($output1=="html") {
167echo "<!-- \n\n Debug info \n";
168echo "Order1: \t $order1 \noutput1:\t $output1\n";
169echo "-->\n";
170}
171
172
173
174
175
176
177// an array to hold names of archives
178$archivelist;
179
180
181// ##########################################################
182// loop for each record returned.
183while ($line = mysql_fetch_array($result_sussex, MYSQL_ASSOC)) {
184    $archivename1 = $line[archivename];
185    $identifier = $line[identifier];
186
187    // remove spaces
188    $archivename1 = trim($archivename1);
189    //
190    // add an entry to the array, the key being the archive name,
191    // the value being a new object created at the same time.
192    $archivelist[$archivename1] = new ArchiveClass();
193
194    // tell the object it's name
195    $archivelist[$archivename1]->setName($archivename1);
196
197    // tell the object it's IR identifier
198    $archivelist[$archivename1]->setIdentifier($identifier);
199
200    // for sorting later on, add this name to a hash
201    $justnames[$archivename1]="$archivename1";
202
203
204}
205
206
207
208// #######################################################################
209// We now have an array - archivelist - which is a list of all archives.
210// Each key is an archive name, each value is an archive object.
211
212
213// SQL to get archives and dates
214$query2 = "
215SELECT archivename, records, DATE(collected_date) as dateonly, country
216FROM ircount
217WHERE country = '$country'
218";
219
220
221$dbresult_list = mysql_query($query2) or die("Query failed : " . mysql_error());
222
223
224// an array to hold the dates we collected data from
225$collected_dates;
226
227// an array to sort the results later
228$sortorder;
229
230
231// for each row returned - which will be an archive, date, and num of records etc
232// - add it to the object for that archive.
233// also... add the date to collected_dates if not already there
234while ($line2 = mysql_fetch_array($dbresult_list, MYSQL_ASSOC)) {
235    $datestamp2 = $line2["dateonly"];
236    $archivename2 = $line2["archivename"];
237    $archivename2 = trim($archivename2);
238    $records2 = $line2["records"];
239    //
240    // lets do some error checking...
241    if ($datestamp2 == "" ||
242        $archivename2 == "" ||
243        $records2 == "") {
244        echo "ERROR $datestamp2 $records2 $archivename2 \n <p>";
245    }
246    if ($records2 == "0") {
247        continue;
248    }
249   
250    // Processing monthly is a little bit of a hack.
251    // We go through each row (one for each archive each week)
252    // but simply over write any entry we already have
253    // for the month in question, creating one a month.
254    // if we only want monthly, remove day from datestamp
255    // the mmyy record for this repository will be overwritten
256    // with the last record for this month
257    if ($freq == "monthly") {
258        list($year, $month, $day) = split('[/.-]', $datestamp2);
259        $datestamp2 = "$year-$month";
260    }
261   
262    // add this date (and num of records) to the object for this
263    // archive.
264    $archivelist[$archivename2]->addToRecordslist($datestamp2,$records2);
265   
266    // record that we have come aross a line which was collected on this date
267    $collected_dates[$datestamp2]++;
268    // are we planning to sort the list based on *this* date? if so lets remember
269    // the number of records for this archive in the sort order
270    if ($order1==$datestamp2) {
271        $sortorder[$archivename2]=$records2;
272    }
273}
274
275
276// we have now added each line of data to the repository object it is associated with,
277// we have each archive which has a list of dates, attached to each date is the
278// number of records it had at the time.
279
280// We also now have collected_dates, a list of dates (as keys in the array, the
281// actual value being the number of times we saw that date).
282
283
284// we also have sortorder, which will already be complete if the sort order
285// was a date column. we need to populate it now if not
286//  if user wants the order to be alphabetic by name (default) just make sort order
287// the same as justnames (which maps name to name (itself))
288if ($order1=="archivename") {
289    $sortorder = $justnames;
290}
291if ($sortorder =="") {
292    $sortorder = $justnames;
293}
294
295// sort by the value in sort order.
296// the key is the archive name
297asort($sortorder);
298
299
300// sort archives alphabetically
301//ksort($archivelist);
302
303// sort the list of dates, earliest first.
304ksort($collected_dates);
305
306
307
308
309
310//#####################################################
311// print out html
312// first print table header, which includes all the collected dates
313if ($output1=="html") {
314
315# print the top row
316echo "<table><tr><th class=\"repname\">Respository Name ";
317$tmplink = linktome("order1", "archivename");
318echo "[<a href=\"$tmplink\">order by</a>]</th>\n";
319table_column($collected_dates, $freq);
320echo "<th>Repository Name</th>";
321echo "</tr>\n";
322
323
324// go through each archive, print out the record totals in to the table
325foreach ($sortorder as $aname => $aobject) {
326   
327    echo "<tr>\n";
328
329    $name = $archivelist[$aname]->getName();
330    $identifier = urlencode ($archivelist[$aname]->getIdentifier());
331
332    echo "<td class=\"repname\">";
333   
334    $querystring = "id=$identifier&country=$country";
335    echo '<a href="archive.php?' . htmlentities($querystring) . '">' . $name . "</a></td> \n";
336   
337    //echo '<a href=\"archive.php?id=$identifier\">$name</a></td>\n';
338
339        //$archivelist[$aname]->printRecordList();
340    $records=$archivelist[$aname]->returnRecords();
341
342    foreach ($collected_dates as $cdate => $totalcdate) {
343        $numrecord = $records[$cdate];
344        if ($numrecord == "") {
345            $numrecord = "&nbsp; ";
346        }
347        // if we are sorted by this col make it bold
348        if ($cdate==$order1) {
349            echo "<td><strong><abbr title=\"$aname\">$numrecord</abbr></strong></td>\n";
350        }
351        else {
352            echo "<td>$numrecord</td>\n";
353        }
354        // include this in the total for this date
355        $date_totals[$cdate]+=$numrecord;
356    }
357
358    // print the archive name again
359    echo "<td>$name</td>\n";
360
361    echo "</tr>\n";
362       
363}
364
365//Finally print out the column heads and grand totals
366echo "<tr><td>Total</td>";
367foreach ($date_totals as $cdate => $totcdate) {
368    echo "<td>$totcdate</td>\n";
369}
370echo "<td>$nbsp</td>";
371echo "</tr>\n";
372
373// this is an awful hack to make the repository name column
374// a decent size.
375echo "<tr><th style=\"color:#FFF\">";
376echo "..................................................... </th>";
377table_column($collected_dates);
378echo "<td style=\"color:#FFF\">";
379echo "..................................................... </td>";
380echo "</tr>\n";
381
382
383echo "</table>";
384}
385
386
387
388
389//##################################################
390// Print txt/csv/tsv file
391// we can do either tab or comma seperated
392if ($output1=="csv" || $output1=="tsv") {
393
394// set these vars for tsv or csv
395if ($output1=="csv") {
396    $precsv = "\"";
397    $posttext = "\",";
398} else {
399    $precsv = "";
400    $posttext = "\t";
401}
402
403
404echo "$precsv" . "Archive Name$posttext";
405foreach ($collected_dates as $cdate => $totcdate) {
406        echo "$precsv$cdate$posttext";
407}
408echo "\n";
409
410// go through each archive, print out the record totals in to the table
411foreach ($archivelist as $aname => $aobject) {
412
413        $name = $archivelist[$aname]->getName();
414        echo "$precsv$name$posttext";
415
416        //$archivelist[$aname]->printRecordList();
417        $records=$archivelist[$aname]->returnRecords();
418
419        foreach ($collected_dates as $cdate => $totalcdate) {
420                $numrecord = $records[$cdate];
421                echo "$precsv$numrecord$posttext";
422                // include this in the total for this date
423                $date_totals[$cdate]+=$numrecord;
424        }
425        echo "\n";
426
427}
428
429//Finally print out grand totals
430echo "$precsv" . "Total$posttext";
431foreach ($date_totals as $cdate => $totcdate) {
432        echo "$precsv$totcdate$posttext";
433}
434echo "\n";
435}
436// end of text/csv output
437// #####################################################
438
439
440
441// only print footer if we are doing html
442if ($output1=="html") {
443    printhtmlfooter();
444}
445?>
Note: See TracBrowser for help on using the browser.