function baw_render_htmlchart($chart, $format, $get_avg = false, $top_x = false, $dataformat = false) { baw_debug("rendering chart"); global $BAW_CONF, $BAW_LIB, $BAW_MES; // FORMATS ----------------------------------------------------------------- $format_arr = array(); $itemcount = count($chart); // we have time data, $top_x has to work from the back instead of the front $time_data = false; if ($top_x < 0) { $time_data = true; $top_x = abs($top_x); } if ($get_avg) { $top_x++; } $top_x = min($BAW_CONF['max_chart_items'], $top_x); $top_x_count = $top_x; // if its not set, use count of items (day, month etc) if (!$top_x_count) { $top_x_count = $itemcount; } // find the smaller one to know the resulting lines if (min($top_x_count, $itemcount) > 12) { $width = 4; } else { $width = 6; } // iterate formats for header $text_fields = 0; foreach ($format as $cell => $attr) { // iterate one formats attributes $format_arr[] = $attr['format']; // we need the number of textfields so we can prepend them to the averages array. // otherwise the fields for avg and others are not in the right columns if ($attr['format'] == 'layout_text') { $text_fields++; } // $title_arr[] = $attr['title']; } // Max & averages calculation ---------------------------------------------- // the max has to be calculated before so the bar height can be calculated // the sum has to be known for averages // iterate all the table and count the sums of all numeric data $max_arr = array(); $sum_arr = array(); $avg_arr = array(); $others = array(); $filled_lines = $itemcount; // get the value for substraction $row_no = 0; $othercount = 0; $hasothers = false; foreach ($chart as $lineid => $row) { $cell_no = 0; $rowsum = 0; $hasothers = false; foreach ($row as $cell) { if ($format_arr[$cell_no] != 'layout_text') { // we dont include the text // max calculation @($max_arr[$cell_no] = max($max_arr[$cell_no], $cell)); // sum calculation @($sum_arr[$cell_no] += $cell); // average calculation if (is_numeric($cell) && $get_avg) { $rowsum += $cell; if ($row_no == $itemcount - 1 && $filled_lines >= 1) { // last line, assume sums are done if ($rowsum == 0 && $cell_no == 0) { // do once again for last line, dont substract further after first cell $filled_lines--; } $avg_arr[$cell_no] = $sum_arr[$cell_no] / $filled_lines; } } else { if ($get_avg) { $avg_arr[$cell_no] = ''; } } // others calculation if (!$time_data && $top_x && $row_no >= $top_x) { // max calculation @($max_arr[$cell_no] = max($max_arr[$cell_no], $others[$cell_no])); $hasothers = true; @($others[$cell_no] += $cell); } else { if ($time_data && $top_x && $itemcount - $row_no >= $top_x) { $hasothers = true; // for now, we do not accumulate old data for months/days -- optional? // risk is that 'others' is so big that it does not make sense //@$others[$cell_no] += $cell; //@$max_arr[$cell_no] = max($max_arr[$cell_no], $others[$cell_no]); } } } $cell_no++; } // we processes the line, if we are already in the 'others' remove the line if ($hasothers) { $othercount++; unset($chart[$lineid]); } // remove one line from avg-count if data empty if ($rowsum == 0) { $filled_lines--; } $row_no++; // count to find out if we are ready to do averages } // make the array longer to fit, first index is 1 since it comes after the array title if ($text_fields >= 1) { $empty_arr = array_pad(array(), $text_fields - 1, ''); } else { $empty_arr = array(); } // add others to the table if ($hasothers && !$time_data) { // we got others, add them to the end $row_no++; $others = $empty_arr + array($text_fields - 1 => $BAW_MES[2]) + $others; $chart += array('layout_others' => $others); } else { if ($hasothers && $time_data) { $row_no++; $others = $empty_arr + array($text_fields - 1 => $BAW_MES[2]) + $others; //$temp = array('layout_others' => $others); //$chart = $temp + $empty_arr + $chart; } } // add averages to the table if ($get_avg) { $row_no++; $avg_arr = $empty_arr + array($text_fields - 1 => $BAW_MES[96]) + $avg_arr; $chart += array('layout_avg' => $avg_arr); } $out = "\n<table class=\"charttable\">\n"; $out .= " <tr>\n"; // create the title if (isset($format[0]['title']) && $BAW_CONF['chart_titles']) { $count_str = sprintf($BAW_MES['records'], baw_num_format($itemcount)); $out .= " <tr>\n <th class=\"header_wrap\" colspan=\"{$row_no}\">{$format[0]['title']} ({$count_str})</th>\n </tr>\n"; } $l = 0; $fieldcount = 0; $legend = array(); foreach ($chart as $lineid => $row) { $class = ''; if (isset($dataformat[$lineid])) { $class = " {$dataformat[$lineid]}"; } else { if ($lineid === 'layout_others' || $lineid === 'layout_avg') { $class .= " {$lineid}"; } } $out .= " <td class=\"chartcell{$class}\">\n"; $cell_no = 0; foreach ($row as $cell) { // only take the numeric values if ($format_arr[$cell_no] != 'layout_text') { $tags = ''; $function = $BAW_LIB['formats'][$format_arr[$cell_no]]['frm']; $txt = $BAW_LIB['formats'][$format_arr[$cell_no]]['txt']; $img = $BAW_LIB['formats'][$format_arr[$cell_no]]['img']; $alt = "{$BAW_MES[$txt]}: " . exec_function($function, $cell); $max_string = str_replace('layout_', '', $format[$cell_no]['format']); // scale after what? $max_key = array_search($BAW_CONF["max_{$max_string}"], $format_arr); if (isset($max_arr[$max_key]) && $max_arr[$max_key] > 0) { $height = $cell / ($max_arr[$max_key] / 100); } else { $height = 1; } if ($height < 1) { $height = 1; } $attr = array('height' => $height, 'width' => $width, 'alt' => $alt, 'title' => $alt, 'class' => 'chartimg'); $out .= baw_create_image($BAW_CONF['icons_url'] . "/other/{$img}", $attr); } else { // write legend $fieldcount = max($cell_no, $fieldcount); @($legend[$l][$cell_no] = " <td{$tags} colspan=\"1\">{$cell}</td>\n"); if (isset($legend[$l - 1][$cell_no]) && isset($cell)) { if (preg_match('#colspan=\\"(\\d+)\\">' . $cell . '</td>#', $legend[$l - 1][$cell_no], $number)) { $number[1]++; @($legend[$l][$cell_no] = " <td{$tags} colspan=\"{$number[1]}\">{$cell}</td>\n"); $legend[$l - 1][$cell_no] = ""; } } } $cell_no++; } $out .= "\n </td>\n"; $f = 1; $l++; } $out .= " </tr>\n"; // Display legend $linecount = $l; // we inverse the order so the ones standing closer to the data in the table // are on top here for ($f = $fieldcount; $f >= 0; $f--) { $out .= " <tr class=\"chartlegend\">\n"; for ($l = 0; $l < $linecount; $l++) { $out .= $legend[$l][$f]; } $out .= " </tr>\n"; } $out .= "</table>\n"; baw_debug("rendering chart finished"); return $out; }
function baw_display_visitors($set) { global $BAW_MES, $BAW_CURR, $BAW_CONF, $BAW_CONF_DIS; $format = array(0 => array('title' => $BAW_MES[81], 'format' => 'layout_text'), 1 => array('percent' => true, 'title' => $BAW_MES[56], 'format' => 'layout_pages'), 2 => array('title' => $BAW_MES[57], 'format' => 'layout_hits'), 3 => array('title' => $BAW_MES[75], 'format' => 'layout_bytes'), 4 => array('title' => $BAW_MES['ratio_pages_hits'], 'format' => 'layout_ratio', 'ratio' => '2/1'), 5 => array('title' => $BAW_MES[9], 'format' => 'layout_date')); $h_data = array(0, 0, 0, 'latest_date' => 0); $s_data = array(0, 0, 0, 'latest_date' => 0); $i = 0; $hashotlinks = false; $hasbots = false; if ($val = baw_data($BAW_CURR['site_name'], "VISITOR", $BAW_CURR['yearmonth'])) { baw_debug("data loaded"); foreach ($val as $dns => $data) { // $infolink = baw_create_link("?", "javascript:neww('$dns','{$dns}XXX')"); // $line[] = array ('data'=> $infolink, 'class' => "aligncenter", 'format' => 'layout_index'); //if (!$set['hidebots'] or $data[1] !== $data[2]) { $ratio = 0; if ($data[0] > 0) { $ratio = $data[1] / $data[0]; } // hotlinks & Bots if ($data[3] == '') { $data[3] = 0; } // $data[3] = settype($data[3], 'int'); if ($data[0] == 0 && $data[1] !== 0) { $h_data['latest_date'] = max($data[3], $h_data['latest_date']); $h_data[0] += $data[0]; $h_data[1] += $data[1]; $h_data[2] += $data[2]; $hashotlinks = true; } else { if ($ratio <= $BAW_CONF_DIS['visitors']['assumebot']) { $s_data['latest_date'] = max($data[3], $s_data['latest_date']); $s_data[0] += $data[0]; $s_data[1] += $data[1]; $s_data[2] += $data[2]; $hasbots = true; } else { $val[$i] = array($dns, $data[0], $data[1], $data[2], baw_num_format($ratio, 2), $data[3]); } } // try extra to regain the memory here since this is the largest function $val[$dns] = NULL; unset($val[$dns]); $i++; } if ($hasbots) { $val['script'] = array($BAW_MES['assumedscript'], $s_data[0], $s_data[1], $s_data[2], $s_data[1] / $s_data[0], $s_data['latest_date']); } if ($hashotlinks) { $val['hotlinks'] = array($BAW_MES['hotlinks_proxies'], $h_data[0], $h_data[1], $h_data[2], 0, $h_data['latest_date']); } $val = baw_array_sorting($val, $set['sort'], $set['sort_dir']); baw_debug("data sorted"); } else { if ($BAW_CONF['hideempty']) { return ''; } } $out = ''; if ($set['table']) { $out .= baw_render_table($set['name'], $val, $format, $set['avg'], $set['total'], $set['top_x']); } return $out; }
function baw_percent_format($number) { global $BAW_CONF; $result = baw_num_format($number, $BAW_CONF['percent_decimals']) . " %"; return $result; }
function baw_render_table($section_name, $table, $format, $get_avg = false, $get_sum = false, $top_x = false, $dataformat = false) { global $BAW_CONF, $BAW_LIB, $BAW_MES, $BAW_CONF_DIS; baw_debug("rendering table"); $out = ''; $theader = "\n<table class=\"datatable\">\n"; // HEADER ----------------------------------------------------------------- $out .= " <tr>\n"; $format_arr = array(); $column = 0; $itemcount = count($table); $ratio_set = array(); // iterate formats for header foreach ($format as $cell => $attr) { $class = ''; $tags = ''; $percent_header = ''; // iterate one formats attributes foreach ($attr as $name => $value) { if ($name == 'title') { $data = $value; } else { if ($name == 'format') { $format_arr[] = $value; $class_arr[] = $value; $class = " {$value}"; } else { if ($name == 'ratio') { $ratio_set[$cell] = $value; // percentage header insert ... this could be set per column in the config? } else { if ($name == 'percent' && $value) { $percent_header = " <th class=\"layout_percent\">{$BAW_MES[15]}</th>\n"; } else { if (strlen($value) > 0) { $tags .= " {$name}=\"{$value}\""; } } } } } } // find out how many items in whole table if ($column == 0) { // add only to first column $data = sprintf($BAW_MES['records'], baw_num_format($itemcount)); } if (isset($attr['title'])) { $out .= " <th class=\"header_wrap{$class}\"{$tags}>{$data}</th>\n{$percent_header}"; $column++; } } $out .= " </tr>\n"; // SUM & averages calculation ---------------------------------------------- // the sum has to be calculated before so the percentages can be calculated // iterate all the table and count the sums of all numeric data $sum_arr = array(0 => $BAW_MES[102]); // we set the text here to make sure its in the right position $avg_arr = array(0 => $BAW_MES[96]); $others = array(); $filled_lines = $itemcount; // get the value for substraction $row_no = 0; $othercount = 0; $hasothers = false; foreach ($table as $lineid => $row) { $cell_no = 0; $rowsum = 0; foreach ($row as $cell) { // sum calculation $sum_arr[$cell_no] = baw_process_cell($cell, @$format_arr[$cell_no], @$sum_arr[$cell_no]); // average calculation if (is_numeric($cell) && $get_avg) { $rowsum += $cell; if ($row_no == $itemcount - 1 && $filled_lines >= 1) { // last line, assume sums are done if ($rowsum == 0 && $cell_no == 0) { // do once again for last line, dont substract further after first cell $filled_lines--; } if ($format_arr[$cell_no] == 'layout_date') { $avg_arr[$cell_no] = false; // this will prevent formatting, averages on date not possible? } else { $avg_arr[$cell_no] = $sum_arr[$cell_no] / $filled_lines; } } } else { if ($get_avg) { $avg_arr[$cell_no] = ''; } } // others calculation if ($top_x && $row_no >= $top_x) { $hasothers = true; $others[$cell_no] = baw_process_cell($cell, @$format_arr[$cell_no], @$others[$cell_no]); } $cell_no++; } // we processes the line, if we are already in the 'others' remove the line if ($hasothers) { $othercount++; unset($table[$lineid]); } // remove one line from avg-count if data empty if ($rowsum == 0) { $filled_lines--; } $row_no++; // count to find out if we are ready to do averages } // add others to the table if ($hasothers) { // we got others, add them to the end if ($top_x < $BAW_CONF['maxlines']) { $text = baw_display_list_link($BAW_MES[2], $BAW_MES[2], $section_name, false, 'full_list') . " (" . baw_num_format($othercount) . ")"; } else { $text = $BAW_MES[2] . " (" . baw_num_format($othercount) . ")<br>" . sprintf($BAW_MES['table_max_hits_exceed'], baw_num_format($BAW_CONF['maxlines'])); } $others = baw_calc_ratio($others, $ratio_set); $others[0] = $text; $table += array('layout_others' => $others); } // add averages to the table if ($get_avg) { // re-set the value here since it might have been overwritten $avg_arr[0] = $BAW_MES[96]; $avg_arr = baw_calc_ratio($avg_arr, $ratio_set); $table += array('layout_avg' => $avg_arr); } // add the sum to the table if ($get_sum) { $sum_arr[0] = $BAW_MES[102]; $sum_arr = baw_calc_ratio($sum_arr, $ratio_set); $table += array('layout_sum' => $sum_arr); } // these are set later to make sure they are not overwritten in the func before // this saves one more check // DATA -------------------------------------------------------------------- $lastlineid = ''; $rowspan = array(); foreach ($table as $lineid => $row) { $cell_no = 0; $class = ''; // get the class for the whole row if (isset($dataformat[$lineid])) { $class = " class=\"{$dataformat[$lineid]}\""; // for some _STRANGE_ reason, the 403-error lineid is equal to the string here, so use === } else { if ($lineid === 'layout_others' || $lineid === 'layout_avg' || $lineid === 'layout_sum') { $class = " class=\"{$lineid}\""; } } $out .= " <tr{$class}>\n"; $iteration = 0; foreach ($row as $cell) { $class = ''; // get the class for the column (set by field) // for some _STRANGE_ reason, the 403-error lineid is equal to the string here, so use === if ($lineid === 'layout_others' || $lineid === 'layout_avg' || $lineid === 'layout_sum') { $span = 0; $attr = ''; if ($cell_no > $iteration) { $iteration++; continue; } if (isset($format[$cell_no]['colspan'])) { $span = $format[$cell_no]['colspan']; $class .= " colspan=\"{$span}\""; $span--; $cell_no += $span; } $iteration++; } else { $class = ''; } // check if the row has data, otherwise remove 1 line for average count $function = $BAW_LIB['formats'][$format_arr[$cell_no]]['frm']; $cell_str = exec_function($function, $cell); if (isset($class_arr[$cell_no]) && strlen($class_arr[$cell_no]) > 0) { $class .= " class=\"{$class_arr[$cell_no]}\""; } // do rowspan $doline = true; $rowspan_str = ''; if ($format_arr[$cell_no] === 'layout_text' && ($lineid !== 'layout_others' && $lineid !== 'layout_avg' && $lineid !== 'layout_sum')) { if (isset($table[$lastlineid][$cell_no]) && $cell === $table[$lastlineid][$cell_no]) { $rowspan[$lineid][$cell_no] = $rowspan[$lastlineid][$cell_no] + 1; $search = " rowspan=\"{$rowspan[$lastlineid][$cell_no]}\">{$cell_str}</td>"; $replace = " rowspan=\"{$rowspan[$lineid][$cell_no]}\">{$cell_str}</td>"; // we search the last time we had this data and replace it $pos = strrpos($out, $search); $out = substr_replace($out, $replace, $pos, strlen($search)); $doline = false; } else { // no match, restart with 1 $rowspan_str = ' rowspan="1"'; // do =1 always so we can replace later $rowspan[$lineid][$cell_no] = 1; } } if ($doline) { $out .= " <td{$class}{$rowspan_str}>{$cell_str}</td>\n"; } if (isset($format[$cell_no]['percent']) && $format[$cell_no]['percent'] == true && $sum_arr[$cell_no] !== 0) { $percent = $cell / ($sum_arr[$cell_no] / 100); $out .= " <td{$class}>" . @baw_percent_format($percent) . "</td>\n"; } $cell_no++; } $lastlineid = $lineid; $out .= " </tr>\n"; } $out .= "</table>\n"; baw_debug("rendering table finished"); return $theader . $out; }