"Fossies" - the Fresh Open Source Software Archive

Member "betterawstats/modules/render_table.inc.php" (17 Mar 2008, 11248 Bytes) of package /linux/www/old/betterawstats-1.0.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 <?php
    2 /**
    3  * betterawstats - an alternative display for awstats data
    4  *
    5  * @author      Oliver Spiesshofer, support at betterawstats dot com
    6  * @copyright   2007 Oliver Spiesshofer
    7  * @version     1.0
    8  * @link        http://betterawstats.com
    9 
   10  * Based on the GPL AWStats Totals script by:
   11  * Jeroen de Jong <jeroen@telartis.nl>
   12  * copyright   2004-2006 Telartis
   13  * version 1.13 (http://www.telartis.nl/xcms/awstats)
   14  *
   15  * This program is free software; you can redistribute it and/or
   16  * modify it under the terms of the GNU General Public License
   17  * as published by the Free Software Foundation; either version 2
   18  * of the License, or (at your option) any later version.
   19  *
   20  * This program is distributed in the hope that it will be useful,
   21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   23  * GNU General Public License for more details.
   24  *
   25  * You should have received a copy of the GNU General Public License
   26  * along with this program; if not, write to the Free Software
   27  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
   28  */
   29 
   30 // this file can't be used on its own
   31 if (strpos ($_SERVER['PHP_SELF'], 'reder_table.inc.php') !== false) {
   32     die ('This file can not be used on its own!');
   33 }
   34 
   35 // this function generates ratio-calculations for a whole line
   36 // it requires that the format of the data contains
   37 // 'format' => 'layout_ratio', 'ratio' => '2/1'
   38 // it would take the second field and divide it by the first
   39 function baw_calc_ratio($data_set, $ratio_set) {
   40     foreach ($ratio_set as $field => $formula) {
   41         $frm = explode("/", $formula);
   42         $data_set[$field] = $data_set[$frm[0]] / $data_set[$frm[1]];
   43     }
   44     return $data_set;
   45 }
   46 
   47 // this function does std. processing that this needed in 2 places
   48 function baw_process_cell($cell, $format, $target = null) {
   49     if ($target == null) {
   50         $target = 0;
   51     }
   52     if (is_numeric($cell)) {
   53         if ($format == 'layout_date') {
   54             $target = max($target, $cell);
   55         } else if ($format == 'layout_ratio') {
   56             $target = 0;
   57         } else {
   58             $target += $cell;
   59         }
   60     } else {
   61         $target = '';
   62     }
   63     return $target;
   64 }
   65 
   66 
   67 function baw_render_table($section_name, $table, $format, $get_avg = false, $get_sum = false,
   68                       $top_x = false, $dataformat = false) {
   69     global $BAW_CONF, $BAW_LIB, $BAW_MES, $BAW_CONF_DIS;
   70     baw_debug("rendering table");
   71     $out = '';
   72     $theader = "\n<table class=\"datatable\">\n";
   73     // HEADER -----------------------------------------------------------------
   74     $out .= "    <tr>\n";
   75     $format_arr = array();
   76     $column = 0;
   77     $itemcount = count($table);
   78     $ratio_set = array();
   79     // iterate formats for header
   80     foreach ($format as $cell => $attr) {
   81         $class = '';
   82         $tags ='';
   83         $percent_header = '';
   84         // iterate one formats attributes
   85         foreach ($attr as $name => $value){
   86             if ($name == 'title') {
   87                 $data = $value;
   88             } else if ($name == 'format') {
   89                 $format_arr[] = $value;
   90                 $class_arr[] = $value;
   91                 $class = " $value";
   92             } else if ($name == 'ratio') {
   93                 $ratio_set[$cell] = $value;
   94             // percentage header insert ... this could be set per column in the config?
   95             } else if ($name == 'percent' && $value){
   96                 $percent_header = "        <th class=\"layout_percent\">{$BAW_MES[15]}</th>\n";
   97             } else if (strlen($value)>0) {
   98                 $tags .= " $name=\"$value\"";
   99             }
  100         }
  101         // find out how many items in whole table
  102         if ($column == 0) { // add only to first column
  103             $data = sprintf($BAW_MES['records'], baw_num_format($itemcount));
  104         }
  105         if (isset($attr['title'])) {
  106             $out .="        <th class=\"header_wrap$class\"$tags>$data</th>\n$percent_header";
  107             $column ++;
  108         }
  109     }
  110     $out .= "    </tr>\n";
  111 
  112     // SUM & averages calculation ----------------------------------------------
  113     // the sum has to be calculated before so the percentages can be calculated
  114     // iterate all the table and count the sums of all numeric data
  115     $sum_arr = array(0 => $BAW_MES[102]); // we set the text here to make sure its in the right position
  116     $avg_arr = array(0 => $BAW_MES[96]);
  117     $others = array();
  118     $filled_lines = $itemcount; // get the value for substraction
  119     $row_no = 0;
  120     $othercount = 0;
  121     $hasothers = false;
  122     foreach ($table as $lineid => $row) {
  123         $cell_no = 0;
  124         $rowsum = 0;
  125         foreach ($row as $cell) {
  126             // sum calculation
  127             $sum_arr[$cell_no] = baw_process_cell($cell, @$format_arr[$cell_no] , @$sum_arr[$cell_no]);
  128             // average calculation
  129             if (is_numeric($cell) && $get_avg) {
  130                 $rowsum += $cell;
  131                 if ($row_no == ($itemcount-1) && $filled_lines >= 1) { // last line, assume sums are done
  132                     if ($rowsum == 0 && $cell_no == 0) { // do once again for last line, dont substract further after first cell
  133                         $filled_lines--;
  134                     }
  135                     if ($format_arr[$cell_no] == 'layout_date') {
  136                         $avg_arr[$cell_no] = false; // this will prevent formatting, averages on date not possible?
  137                     } else {
  138                         $avg_arr[$cell_no] = $sum_arr[$cell_no] / $filled_lines;
  139                     }
  140                 }
  141             } else if ($get_avg) {
  142                 $avg_arr[$cell_no] = '';
  143             }
  144             // others calculation
  145             if ($top_x && ($row_no >= $top_x)) {
  146                 $hasothers = true;
  147                 $others[$cell_no] = baw_process_cell($cell, @$format_arr[$cell_no] , @$others[$cell_no]);
  148             }
  149             $cell_no ++;
  150         }
  151         // we processes the line, if we are already in the 'others' remove the line
  152         if ($hasothers) {
  153             $othercount ++;
  154             unset($table[$lineid]);
  155         }
  156         // remove one line from avg-count if data empty
  157         if ($rowsum == 0) {
  158             $filled_lines--;
  159         }
  160         $row_no++; // count to find out if we are ready to do averages
  161     }
  162     // add others to the table
  163     if ($hasothers) { // we got others, add them to the end
  164         if ($top_x < $BAW_CONF['maxlines']) {
  165             $text = baw_display_list_link($BAW_MES[2], $BAW_MES[2], $section_name, false, 'full_list')
  166                 . " (". baw_num_format($othercount) . ")";
  167         } else {
  168             $text = $BAW_MES[2]
  169                 . " (". baw_num_format($othercount) . ")<br>"
  170                 . sprintf($BAW_MES['table_max_hits_exceed'], baw_num_format($BAW_CONF['maxlines']));
  171         }
  172         $others = baw_calc_ratio($others, $ratio_set);
  173         $others[0] = $text;
  174         $table += array('layout_others'=> $others);
  175     }
  176     // add averages to the table
  177     if ($get_avg) {
  178         // re-set the value here since it might have been overwritten
  179         $avg_arr[0] = $BAW_MES[96];
  180         $avg_arr = baw_calc_ratio($avg_arr, $ratio_set);
  181         $table += array('layout_avg' => $avg_arr);
  182     }
  183     // add the sum to the table
  184     if ($get_sum) {
  185         $sum_arr[0] = $BAW_MES[102];
  186         $sum_arr = baw_calc_ratio($sum_arr, $ratio_set);
  187         $table += array('layout_sum' => $sum_arr);
  188     }
  189 
  190     // these are set later to make sure they are not overwritten in the func before
  191     // this saves one more check
  192 
  193     // DATA --------------------------------------------------------------------
  194     $lastlineid = '';
  195     $rowspan = array();
  196     foreach ($table as $lineid => $row) {
  197         $cell_no = 0;
  198         $class = '';
  199         // get the class for the whole row
  200         if (isset($dataformat[$lineid])) {
  201             $class = " class=\"{$dataformat[$lineid]}\"";
  202             // for some _STRANGE_ reason, the 403-error lineid is equal to the string here, so use ===
  203         } else if ($lineid === 'layout_others' || $lineid === 'layout_avg' || $lineid === 'layout_sum') {
  204             $class = " class=\"$lineid\"";
  205         }
  206         $out .= "    <tr$class>\n";
  207         $iteration = 0;
  208         foreach ($row as $cell) {
  209             $class = ''; // get the class for the column (set by field)
  210              // for some _STRANGE_ reason, the 403-error lineid is equal to the string here, so use ===
  211             if ($lineid === 'layout_others' || $lineid === 'layout_avg' || $lineid === 'layout_sum') {
  212                 $span = 0;
  213                 $attr = '';
  214                 if ($cell_no > $iteration) {
  215                     $iteration ++;
  216                     continue;
  217                 }
  218                 if (isset($format[$cell_no]['colspan'])) {
  219                     $span = $format[$cell_no]['colspan'];
  220                     $class .= " colspan=\"$span\"";
  221                     $span --;
  222                     $cell_no += $span;
  223                 }
  224                 $iteration ++;
  225             } else {
  226                 $class = '';
  227             }
  228             // check if the row has data, otherwise remove 1 line for average count
  229             $function = $BAW_LIB['formats'][$format_arr[$cell_no]]['frm'];
  230             $cell_str = exec_function($function, $cell);
  231             if (isset($class_arr[$cell_no]) && strlen($class_arr[$cell_no]) > 0) {
  232                 $class .= " class=\"{$class_arr[$cell_no]}\"";
  233             }
  234             // do rowspan
  235             $doline = true;
  236             $rowspan_str = '';
  237             if (($format_arr[$cell_no] === 'layout_text') &&
  238                 ($lineid !== 'layout_others' && $lineid !== 'layout_avg' && $lineid !== 'layout_sum')) {
  239                 if (isset($table[$lastlineid][$cell_no]) && $cell === $table[$lastlineid][$cell_no]) {
  240                     $rowspan[$lineid][$cell_no] = $rowspan[$lastlineid][$cell_no] + 1;
  241                     $search = " rowspan=\"{$rowspan[$lastlineid][$cell_no]}\">$cell_str</td>";
  242                     $replace = " rowspan=\"{$rowspan[$lineid][$cell_no]}\">$cell_str</td>";
  243                     // we search the last time we had this data and replace it
  244                     $pos = strrpos($out, $search);
  245                     $out = substr_replace($out, $replace, $pos, strlen($search));
  246                     $doline = false;
  247                 } else { // no match, restart with 1
  248                     $rowspan_str = ' rowspan="1"'; // do =1 always so we can replace later
  249                     $rowspan[$lineid][$cell_no] = 1;
  250                 }
  251             }
  252             if ($doline) {
  253                 $out .= "        <td$class$rowspan_str>$cell_str</td>\n";
  254             }
  255             if (isset($format[$cell_no]['percent']) &&
  256                     $format[$cell_no]['percent'] == true &&
  257                     $sum_arr[$cell_no] !== 0) {
  258                 $percent = $cell / ($sum_arr[$cell_no] / 100);
  259                 $out .="        <td$class>" . @baw_percent_format($percent) . "</td>\n";
  260             }
  261             $cell_no ++;
  262         }
  263         $lastlineid = $lineid;
  264         $out .= "    </tr>\n";
  265     }
  266     $out .= "</table>\n";
  267     baw_debug("rendering table finished");
  268     return $theader . $out;
  269 }
  270 
  271 ?>