コード例 #1
0
ファイル: twigFilter.php プロジェクト: doekia/php-reports
 public static function filter($value, $options = array(), &$report, &$row)
 {
     // If this is html
     $html = isset($options['html']) ? $options['html'] : false;
     $template = isset($options['template']) ? $options['template'] : $value->getValue();
     $result = PhpReports::renderString($template, array("value" => $value->getValue(), "row" => $row));
     $value->setValue($result, $html);
     return $value;
 }
コード例 #2
0
ファイル: imgsizeFilter.php プロジェクト: doekia/php-reports
 public static function filter($value, $options = array(), &$report, &$row)
 {
     $handle = fopen($value->getValue(), 'rb');
     $img = new Imagick();
     $img->readImageFile($handle);
     $data = $img->identifyImage();
     if (!isset($options['format'])) {
         $options['format'] = self::$default_format;
     }
     $value->setValue(PhpReports::renderString($options['format'], $data));
     return $value;
 }
コード例 #3
0
ファイル: RollupHeader.php プロジェクト: doekia/php-reports
 public static function beforeRender(&$report)
 {
     //cache for Twig parameters for each dataset/column
     $twig_params = array();
     // Now that we know how many datasets we have, expand out Rollup headers with dataset->true
     $new_rollups = array();
     foreach ($report->options['Rollup'] as $i => $rollup) {
         if ($rollup['dataset'] === true && isset($report->options['DataSets'])) {
             $copy = $rollup;
             foreach ($report->options['DataSets'] as $i => $dataset) {
                 $copy['dataset'] = $i;
                 $new_rollups[] = $copy;
             }
         } else {
             $new_rollups[] = $rollup;
         }
     }
     $report->options['Rollup'] = $new_rollups;
     // First get all the values
     foreach ($report->options['Rollup'] as $rollup) {
         // If we already got twig parameters for this dataset, skip it
         if (isset($twig_params[$rollup['dataset']])) {
             continue;
         }
         $twig_params[$rollup['dataset']] = array();
         if (isset($report->options['DataSets'])) {
             if (isset($report->options['DataSets'][$rollup['dataset']])) {
                 foreach ($report->options['DataSets'][$rollup['dataset']]['rows'] as $row) {
                     foreach ($row['values'] as $value) {
                         if (!isset($twig_params[$rollup['dataset']][$value->key])) {
                             $twig_params[$rollup['dataset']][$value->key] = array('values' => array());
                         }
                         $twig_params[$rollup['dataset']][$value->key]['values'][] = $value->getValue();
                     }
                 }
             }
         }
     }
     // Then, calculate other statistical properties
     foreach ($twig_params as $dataset => &$tp) {
         foreach ($tp as $column => &$params) {
             //get non-null values and sort them
             $real_values = array_filter($params['values'], function ($a) {
                 if ($a === null || $a === '') {
                     return false;
                 }
                 return true;
             });
             sort($real_values);
             $params['sum'] = array_sum($real_values);
             $params['count'] = count($real_values);
             if ($params['count']) {
                 $params['mean'] = $params['average'] = $params['sum'] / $params['count'];
                 $params['median'] = $params['count'] % 2 ? ($real_values[$params['count'] / 2 - 1] + $real_values[$params['count'] / 2]) / 2 : $real_values[floor($params['count'] / 2)];
                 $params['min'] = $real_values[0];
                 $params['max'] = $real_values[$params['count'] - 1];
             } else {
                 $params['mean'] = $params['average'] = $params['median'] = $params['min'] = $params['max'] = 0;
             }
             $devs = array();
             foreach ($real_values as $v) {
                 $devs[] = pow($v - $params['mean'], 2);
             }
             $params['stdev'] = sqrt(array_sum($devs) / (count($devs) - 1));
         }
     }
     //render each rollup row
     foreach ($report->options['Rollup'] as $rollup) {
         if (!isset($report->options['DataSets'][$rollup['dataset']]['footer'])) {
             $report->options['DataSets'][$rollup['dataset']]['footer'] = array();
         }
         $columns = $rollup['columns'];
         $row = array('values' => array(), 'rollup' => true);
         foreach ($twig_params[$rollup['dataset']] as $column => $p) {
             if (isset($columns[$column])) {
                 $p = array_merge($p, array('row' => $twig_params[$rollup['dataset']]));
                 $row['values'][] = new ReportValue(-1, $column, PhpReports::renderString($columns[$column], $p));
             } else {
                 $row['values'][] = new ReportValue(-1, $column, null);
             }
         }
         $report->options['DataSets'][$rollup['dataset']]['footer'][] = $row;
     }
 }
コード例 #4
0
 public static function run(&$report)
 {
     $report->conn->SetFetchMode(ADODB_FETCH_ASSOC);
     $rows = array();
     $macros = $report->macros;
     foreach ($macros as $key => $value) {
         if (is_array($value)) {
             $first = true;
             foreach ($value as $key2 => $value2) {
                 $value[$key2] = mysql_real_escape_string(trim($value2));
                 $first = false;
             }
             $macros[$key] = $value;
         } else {
             $macros[$key] = mysql_real_escape_string($value);
         }
         if ($value === 'ALL') {
             $macros[$key . '_all'] = true;
         }
     }
     //add the config and environment settings as macros
     $macros['config'] = PhpReports::$config;
     $macros['environment'] = PhpReports::$config['environments'][$report->options['Environment']];
     $raw_sql = "";
     foreach ($report->raw_query as $qry) {
         if (is_array($qry)) {
             foreach ($qry as $key => $value) {
                 // TODO handle arrays better
                 if (!is_bool($value) && !is_array($value)) {
                     $qry[$key] = PhpReports::renderString($value, $macros);
                 }
             }
             //TODO This sux - need a class or something :-)
             $raw_sql .= PivotTableSQL($report->conn, $qry['tables'], $qry['rows'], $qry['columns'], $qry['where'], $qry['orderBy'], $qry['limit'], $qry['agg_field'], $qry['agg_label'], $qry['agg_fun'], $qry['include_agg_field'], $qry['show_count']);
         } else {
             $raw_sql .= $qry;
         }
     }
     //expand macros in query
     $sql = PhpReports::render($raw_sql, $macros);
     $report->options['Query'] = $sql;
     $report->options['Query_Formatted'] = SqlFormatter::format($sql);
     //split into individual queries and run each one, saving the last result
     $queries = SqlFormatter::splitQuery($sql);
     foreach ($queries as $query) {
         if (!is_array($query)) {
             //skip empty queries
             $query = trim($query);
             if (!$query) {
                 continue;
             }
             $result = $report->conn->Execute($query);
             if (!$result) {
                 throw new Exception("Query failed: " . $report->conn->ErrorMsg());
             }
             //if this query had an assert=empty flag and returned results, throw error
             if (preg_match('/^--[\\s+]assert[\\s]*=[\\s]*empty[\\s]*\\n/', $query)) {
                 if ($result->GetAssoc()) {
                     throw new Exception("Assert failed.  Query did not return empty results.");
                 }
             }
         }
     }
     return $result->GetArray();
 }