function plot_graph(&$graph, $graph_ct = false)
 {
     if ($graph_ct == 0) {
         if ($this->page_started) {
             $this->text .= '</TBODY></TABLE>';
         }
         $this->page_started = false;
     }
     $this->graph_session_placeholder++;
     $graph->width_actual = check_for_default("GraphWidth", $graph->width);
     $graph->height_actual = check_for_default("GraphHeight", $graph->height);
     $graph->title_actual = reportico_assignment::reportico_meta_sql_criteria($this->query, $graph->title, true);
     $graph->xtitle_actual = reportico_assignment::reportico_meta_sql_criteria($this->query, $graph->xtitle, true);
     $graph->ytitle_actual = reportico_assignment::reportico_meta_sql_criteria($this->query, $graph->ytitle, true);
     $url_string = $graph->generate_url_params("HTML", $this->graph_session_placeholder);
     $this->text .= '<div class="swRepResultGraph">';
     if ($url_string) {
         $this->text .= $url_string;
     }
     $this->text .= '</div>';
 }
예제 #2
0
	function execute_query($in_criteria_name)
	{

		global $g_code_area;
		global $g_code_source;
		global $g_error_status;

		$text = "";
		$g_error_status = false;

		$this->fetch_column_attributes();

		// Run query for each target. Currently having more than
		// one target means first target is array which becomes source
		// for second target
		//for ($i = 0; $i < count($this->targets); $i++ )
		for ($_counter = 0; $_counter < 1; $_counter++ )
		{
			$target =& $this->targets[$_counter];
			$target->set_query($this);
			$target->set_columns($this->columns);
			$target->start();
		//}

		// Reset all old column values to junk
		foreach ( $this->columns as $k => $col )
		{
			$this->columns[$k]->old_column_value = "";
		}

		if ( $_counter > 0 )
		{
			// Execute query 2
			$this->assignment = array();
			$ds = new reportico_datasource();
			$this->set_datasource($ds);

			$ds->set_database($this->targets[0]->results);
			$ds->connect();

			foreach ( $this->columns as $k => $col )
			{
				$this->columns[$k]->in_select = true;
			}
		}

		/* Performing SQL query */ 
		$ds =& $this->datasource;
		$conn =& $this->datasource->ado_connection;

		$this->debug($this->query_statement);
		//$conn->debug = true;

		foreach ( $this->pre_sql as $sql)
		{
			$g_code_area = "Custom User SQLs";
			$nsql = reportico_assignment::reportico_meta_sql_criteria($this, $sql, true);
			handle_debug("Pre-SQL".$nsql, SW_DEBUG_LOW);
            $recordSet = false;
            $errorCode = false;
            $errorMessage = false;
            try {
			    $recordSet = $conn->Execute($nsql) ;
            }
            catch ( PDOException $ex)
            {
                $errorCode = $ex->getCode();
                $errorMessage = $ex->getMessage();
            }
            if ( !$recordSet )
            {
                if ( $errorMessage )
			        handle_error("Pre-Query Failed<BR><BR>".$nsql."<br><br>" . 
			        $errorMessage);
                else
			        handle_error("Pre-Query Failed<BR><BR>".$nsql."<br><br>" . 
			        "Status ".$conn->ErrorNo()." - ".
			        $conn->ErrorMsg());
            }
			$g_code_area = "";
		}

		// Execute Any Pre Execute Code, if not specified then
        // attempt to pick up code automatically from a file "projects/project/report.xml.php"
		$code = $this->get_attribute("PreExecuteCode");
		if ( !$code || $code == "NONE" || $code == "XX" )
        {
		    global $g_project;
	        $source_path = find_best_location_in_include_path( $this->projects_folder."/projects/".$g_project."/".$this->xmloutfile.".php" );
            if ( is_file($source_path) )
            {
                $code = file_get_contents($source_path);
            }
            else
                $code = false;
        }

		if ( $code )
		{
			$g_code_area = "";
			$code = "\$lk =& \$this->lookup_queries;". $code;
			$code = "\$ds =& \$this->datasource->ado_connection;". $code;
			$code = "\$_criteria =& \$this->lookup_queries;". $code;
			$code = "\$_pdo =& \$_connection->_connectionID;". $code;
			$code = "if ( \$_connection )". $code;
			$code = "\$_pdo = false;". $code;
			$code = "\$_connection =& \$this->datasource->ado_connection;". $code;

			// set to the user defined error handler
			global $g_eval_code;
			$g_eval_code = $code;
			// If parse error in eval code then use output buffering contents to show user the error
			$ob_level =  ob_get_level();
			if ( $ob_level > 0 )
				ob_start();
			eval($code);
			$eval_output = ob_get_contents();	
			if ( $ob_level > 0 )
			    ob_end_clean();

            // Check for parse error
			if ( preg_match ( "/.*Parse error.*on line <b>(.*)<.b>/", $eval_output, $parseerrors ) )
			{
				// There is a parse error in the evaluated code .. find the relevant line
				$errtext = "Parse Error in custom report code: <br><hr>$eval_output<PRE>";
				foreach(preg_split("/(\r?\n)/", $code) as $lno => $line){
    					// do stuff with $line
					if ( $lno > $parseerrors[1] - 3 && $lno < $parseerrors[1] + 3 )
					{
						if ( $lno == $parseerrors[1] )
							$errtext .= ">>>  ";	
						else
							$errtext .= "     ";	
						$errtext .= $line;
						$errtext .= "\n";
					}
				}
				$errtext .= "</PRE>";
				trigger_error($errtext, E_USER_ERROR);

			}
			else
			{
                echo $eval_output;
			}
			$g_code_area = "";
			$g_code_source = "";
		}
		$recordSet = false;

		if ( $in_criteria_name )
			$g_code_area = "Criteria ".$in_criteria_name;
		else
			$g_code_area = "Main Report Query";

		// User may have flagged returning before SQL performed
		global $g_no_sql;
		if ( $g_no_sql )
			return;


        $recordSet = false;
        $errorCode = false;
        $errorMessage = false;
        try {
		    if ( !$g_error_status && $conn != false )
			    $recordSet = $conn->Execute($this->query_statement) ;
        }
        catch ( PDOException $ex)
        {
            $errorCode = $ex->getCode();
            $errorMessage = $ex->getMessage();
            $g_error_status = 1;
        }
        if ( $conn && !$recordSet )
        {
            if ( $errorMessage )
                handle_error("Query Failed<BR><BR>".$this->query_statement."<br><br>" . 
                $errorMessage);
            else
                handle_error("Query Failed<BR><BR>".$this->query_statement."<br><br>" . 
                "Status ".$conn->ErrorNo()." - ".
                $conn->ErrorMsg());
        }

		if ( $conn != false )
			handle_debug($this->query_statement, SW_DEBUG_LOW);

		// Begin Target Output
		if (!$recordSet || $g_error_status) 
		{
			return;
		}


        // Main Query Result Fetching
		$this->query_count = 0;
		while (!$recordSet->EOF) {

			$line = $recordSet->FetchRow();
			$this->query_count++;

			$g_code_area = "Build Column";
			$this->build_column_results($line);

			$g_code_area = "Assignment";

			if ( $_counter < 1 )
            {
			    $target->set_default_styles();
                $this->charset_encode_db_to_output();
				$this->assign();
            }
			$g_code_source = false;

            // Skip line if required
            if ( $this->output_skipline )
            {
			    $this->query_count--;
                $this->output_skipline = false;
                continue;
            }

			$g_code_area = "Line Output";
			$target->each_line($line);

			$g_code_area = "Store Output";
			$this->store_column_results();
			if ($recordSet->EOF)
			 	break;
		}
		$g_code_area = "";

        global $g_no_data;
        $g_no_data = false;
		if ( $this->query_count == 0 && !$in_criteria_name && ( !$this->access_mode || $this->access_mode != "REPORTOUTPUT" ) )
		{
            $g_no_data = true;
			handle_error ( template_xlate("NO_DATA_FOUND"), E_USER_WARNING );
		}

		// Complete Target Output
		//for ($_counter = 0; $_counter < count($this->targets); $_counter++ )
		//{
			//$target =& $this->targets[$_counter];
			$target->finish();
			$text =& $target->text;

			/* Free resultset */
			$recordSet->Close();

		}
		return $text;

	}
예제 #3
0
 function format_group_header(&$col, $custom, $calculate_only = false)
 {
     $this->check_line_requirement($this->query->output_group_header_label_styles);
     for ($ctr = 0; $ctr < 2; $ctr++) {
         $this->draw_mode = "CALCULATE";
         if ($ctr == 1 && $calculate_only) {
             $this->draw_mode = "DRAW";
             break;
         }
         if ($ctr == 1) {
             $this->draw_mode = "DRAW";
             $this->check_page_overflow();
             if (!$custom || $this->group_headers_custom_drawn == 0) {
                 $this->new_report_page_line_by_style("REPTOPPAGE", $this->mid_page_reportbody_styles, false);
             }
             $this->group_headers_custom_drawn++;
         }
         $y = $this->document->GetY();
         if ($custom) {
             $prevx = $this->document->GetX();
             $prevy = $this->document->GetY();
             $this->yjump = 2;
             $wd = $this->abs_print_width;
             $tx = $custom;
             $styles = $this->fetch_cell_styles($tx);
             $tx = $this->reportico_string_to_php($tx);
             $tx = reportico_assignment::reportico_meta_sql_criteria($this->query, $tx);
             $just = "L";
             $this->apply_style_tags("EACHHEADMID", $this->mid_cell_reportbody_styles);
             $this->apply_style_tags("GROUPHEADER", $styles);
             $link = false;
             $pmargin = $this->extract_style_tags("EACHLINE", $this->all_page_page_styles, "margin", "left");
             $rgmargin = $this->extract_style_tags("EACHLINE", $styles, "margin", "right");
             $x = $this->all_page_page_styles["style_start"];
             $wd = $this->all_page_page_styles["style_width"];
             $this->set_position($x, $this->group_header_start);
             $this->draw_cell($wd, $this->vsize + 0, $tx, "PBF", 0, $just, "T", $link);
             $this->unapply_style_tags("GROUPHEADER", $styles);
             $this->unapply_style_tags("EACHHEADMID", $this->mid_cell_reportbody_styles);
             $this->end_line();
             //$this->draw_cell($group_data_width, $this->vsize, "");    // Blank cell to continue page breaking at this size
             $y = $this->document->GetY();
             // Store where group header reaches so we know where to start printing after row
             if ($y > $this->group_header_end) {
                 $this->group_header_end = $y;
             }
             if ($this->yjump) {
                 $this->set_position(false, $y + $this->yjump);
             }
             //$this->apply_style_tags( "DEFAULT", $this->mid_page_page_styles);
             $this->set_position($prevx, $prevy);
             continue;
         }
         $group_label = $col->get_attribute("group_header_label");
         if (!$group_label) {
             $group_label = $col->get_attribute("column_title");
         }
         if (!$group_label) {
             $group_label = $col->query_name;
             $group_label = str_replace("_", " ", $group_label);
             $group_label = ucwords(strtolower($group_label));
         }
         $group_label = sw_translate($group_label);
         $this->yjump = 2;
         // Fetch Group Header Label Start Column + display
         $margin = $this->extract_style_tags("EACHLINE", $this->query->output_group_header_label_styles, "margin", "left");
         $pmargin = $this->extract_style_tags("EACHLINE", $this->all_page_page_styles, "margin", "left");
         $rpmargin = $this->extract_style_tags("EACHLINE", $this->all_page_page_styles, "margin", "right");
         $labelwidth = 150;
         $group_xpos = $this->all_page_page_styles["style_start"];
         $group_xpos += $pmargin;
         $group_label_width = $labelwidth;
         $group_data_xpos = $group_xpos + $labelwidth + $margin;
         $group_data_width = $this->all_page_page_styles["style_width"] - $labelwidth;
         $group_data_end = $this->all_page_page_styles["style_start"] + $this->all_page_page_styles["style_width"];
         $this->apply_style_tags("EACHHEADMID", $this->mid_cell_reportbody_styles);
         if (session_request_item("target_style", "TABLE") != "FORM") {
             $this->apply_style_tags("HEADERLABEL", $this->query->output_group_header_label_styles);
         }
         $this->set_position($group_xpos, $y);
         $padstring = $group_label;
         $this->draw_cell($group_label_width, $this->vsize, "{$padstring}");
         if (session_request_item("target_style", "TABLE") != "FORM") {
             $this->unapply_style_tags("HEADERLABEL", $this->query->output_group_header_label_styles);
         }
         // Display group header value
         $contenttype = $col->derive_attribute("content_type", $col->query_name);
         if (session_request_item("target_style", "TABLE") != "FORM") {
             $this->apply_style_tags("HEADERVALUE", $this->query->output_group_header_value_styles);
         }
         $qn = get_query_column($col->query_name, $this->query->columns);
         if ($contenttype == "graphic" || preg_match("/imagesql=/", $qn->column_value)) {
             if ($this->draw_mode == "CALCULATE") {
                 if (session_request_item("target_style", "TABLE") != "FORM") {
                     $this->unapply_style_tags("HEADERVALUE", $this->query->output_group_header_value_styles);
                 }
                 $this->unapply_style_tags("EACHHEADMID", $this->mid_cell_reportbody_styles);
                 continue;
             }
             $qn = get_query_column($col->query_name, $this->query->columns);
             $sql = @preg_replace("/.*imagesql=/", "", $qn->column_value);
             $sql = @preg_replace("/'>\$/", "", $sql);
             $str =& get_db_image_string($this->query->datasource->driver, $this->query->datasource->database, $this->query->datasource->host_name, $sql, $this->query->datasource->ado_connection);
             if ($str) {
                 $tmpnam = tempnam(SW_TMP_DIR, "dbi");
                 unlink($tmpnam);
                 $width = $qn->abs_column_width;
                 $height = 20;
                 $im = imagecreatefromstring($str);
                 if (imagepng($im, $tmpnam . ".png")) {
                     $x = $qn->abs_column_start;
                     $y = $this->document->GetY();
                     $this->set_position($group_data_xpos);
                     $h = $this->draw_image($tmpnam . ".png", $group_data_xpos, $y, $width * $this->pdfImageDPIScale, 0) + 2;
                     if ($h > $this->max_line_height) {
                         $this->max_line_height = $h;
                     }
                     $this->yjump = $h;
                     unlink($tmpnam . ".png");
                 }
             }
         } else {
             $this->set_position($group_data_xpos, $y);
             $padstring = $qn->column_value;
             $this->draw_cell($group_data_width, $this->vsize, "{$padstring}");
         }
         if (session_request_item("target_style", "TABLE") != "FORM") {
             $this->unapply_style_tags("HEADERVALUE", $this->query->output_group_header_value_styles);
         }
         $this->end_line();
         //$this->draw_cell($group_data_width + 200, $this->vsize, "");    // Blank cell to continue page breaking at this size
         $y = $this->document->GetY();
         $this->unapply_style_tags("EACHHEADMID", $this->mid_cell_reportbody_styles);
         // Store where group header reaches so we know where to start printing after row
         if ($y > $this->group_header_end) {
             $this->group_header_end = $y;
         }
         //if ( $this->yjump )
         //$this->set_position(false, $y + $this->yjump);
     }
 }
 function after_group_trailers()
 {
     $this->any_trailers = false;
     $this->any_custom_trailers = false;
     // Dont apply trailers in FORM style
     if (session_request_item("target_style", "TABLE") == "FORM") {
         return;
     }
     $trailer_first = true;
     $group_changed = false;
     if ($this->line_count <= 0) {
         // No group trailers as it's the first page
     } else {
         //Plot After Group Trailers
         if (count($this->query->groups) == 0) {
             return;
         }
         $rct = 0;
         // Work out which groups have triggered trailer by passing
         // through highest to lowest level .. group changes at level cause change at lower
         // also last line does too!!
         $uppergroupchanged = false;
         reset($this->query->groups);
         do {
             $group = current($this->query->groups);
             $group->change_triggered = false;
             if ($uppergroupchanged || $this->query->changed($group->group_name) || $this->last_line) {
                 $group->change_triggered = true;
                 $uppergroupchanged = true;
             }
         } while (next($this->query->groups));
         end($this->query->groups);
         do {
             $group = current($this->query->groups);
             if ($group->change_triggered) {
                 if ($rct == 1) {
                     $this->format_report_detail_end();
                 }
                 $rct++;
                 $group_changed = true;
                 $lev = 0;
                 $tolev = 0;
                 while ($lev < $group->max_level) {
                     if ($lev == 0) {
                         $this->apply_format($group, "before_trailer");
                     }
                     $this->format_group_trailer_start($trailer_first);
                     $this->format_column_trailer_before_line();
                     $junk = 0;
                     $wc = count($this->columns);
                     // In PDF mode all trailer lines must be passed through twice
                     // to allow calculation of line height. Otherwise
                     // Only one pass through
                     $number_group_rows = 0;
                     for ($passno = 1; $passno <= 2; $passno++) {
                         if ($this->query->target_format == "PDF") {
                             if ($passno == 1) {
                                 $this->draw_mode = "CALCULATE";
                             }
                             if ($passno == 2) {
                                 $this->draw_mode = "DRAW";
                                 $this->unapply_style_tags("GROUPTRAILER", $this->query->output_group_trailer_styles);
                                 $this->check_page_overflow();
                                 $this->apply_style_tags("GROUPTRAILER", $this->query->output_group_trailer_styles);
                             }
                         } else {
                             if ($passno == 2) {
                                 break;
                             }
                         }
                         // Column Trailers
                         $linedrawn = false;
                         if ($this->draw_mode == "DRAW" && $number_group_rows == 0) {
                             $linedrawn = true;
                         } else {
                             $number_group_rows = 0;
                         }
                         foreach ($this->query->display_order_set["column"] as $w) {
                             if (!$this->show_column_header($w)) {
                                 continue;
                             }
                             if (array_key_exists($w->query_name, $group->trailers_by_column)) {
                                 $number_group_rows++;
                                 if (count($group->trailers_by_column[$w->query_name]) >= $lev + 1 && !$group->trailers_by_column[$w->query_name][$lev]["GroupTrailerCustom"]) {
                                     if (!$linedrawn) {
                                         $this->unapply_style_tags("GROUPTRAILER", $this->query->output_group_trailer_styles);
                                         $this->new_report_page_line("3");
                                         $this->apply_style_tags("GROUPTRAILER", $this->query->output_group_trailer_styles);
                                         $linedrawn = true;
                                     }
                                     $this->format_column_trailer($w, $group->trailers_by_column[$w->query_name][$lev], $trailer_first);
                                 } else {
                                     if (!$linedrawn) {
                                         $this->unapply_style_tags("GROUPTRAILER", $this->query->output_group_trailer_styles);
                                         $this->new_report_page_line("3");
                                         $this->apply_style_tags("GROUPTRAILER", $this->query->output_group_trailer_styles);
                                         $linedrawn = true;
                                     }
                                     $this->format_column_trailer($w, $junk, $trailer_first);
                                 }
                                 $this->any_trailers = true;
                                 if ($group->max_level > $tolev) {
                                     $tolev = $group->max_level;
                                 }
                             } else {
                                 if (!$linedrawn) {
                                     $this->unapply_style_tags("GROUPTRAILER", $this->query->output_group_trailer_styles);
                                     $this->new_report_page_line("3");
                                     $this->apply_style_tags("GROUPTRAILER", $this->query->output_group_trailer_styles);
                                     $linedrawn = true;
                                 }
                                 $this->format_column_trailer($w, $junk, $trailer_first);
                             }
                         }
                         // foreach
                     }
                     if (get_class($this) != "reportico_report_html") {
                         $this->format_group_trailer_end();
                     }
                     if ($trailer_first) {
                         $trailer_first = false;
                     }
                     $lev++;
                     $this->end_line();
                 }
                 // while
             }
         } while (prev($this->query->groups));
         if ($group_changed && get_class($this) == "reportico_report_html") {
             $this->format_group_trailer_end();
         }
         if ($group_changed && $this->query->target_format == "PDF") {
             $this->end_of_page_block();
         }
         // Custom trailers
         end($this->query->groups);
         do {
             $group = current($this->query->groups);
             if ($this->query->changed($group->group_name) || $this->last_line) {
                 $this->format_group_custom_trailer_start();
                 // In PDF mode all trailer lines must be passed through twice
                 // to allow calculation of line height. Otherwise
                 // Only one pass through
                 for ($passno = 1; $passno <= 2; $passno++) {
                     if ($this->query->target_format == "PDF") {
                         if ($passno == 1) {
                             $this->draw_mode = "CALCULATE";
                         }
                         if ($passno == 2) {
                             $this->draw_mode = "DRAW";
                             $this->check_page_overflow();
                             $this->custom_trailer_wrappers();
                         }
                     } else {
                         if ($passno == 2) {
                             break;
                         }
                     }
                     // Column Trailers
                     if ($this->query->target_format == "PDF") {
                         foreach ($group->trailers_by_column as $kk => $trailer) {
                             foreach ($trailer as $kk2 => $colgrp) {
                                 $this->format_custom_trailer($w, $colgrp);
                             }
                         }
                         // foreach
                     }
                 }
                 $this->format_group_custom_trailer_end();
             }
         } while (prev($this->query->groups));
         // Plot After Group Graphs
         $graph_ct = 0;
         end($this->query->groups);
         do {
             $group = current($this->query->groups);
             if ($this->query->changed($group->group_name) || $this->last_line) {
                 if (!function_exists("imagecreatefromstring")) {
                     trigger_error("Function imagecreatefromstring does not exist - ensure PHP is installed with GD option");
                 }
                 if (function_exists("imagecreatefromstring") && $this->graph_display && get_reportico_session_param("target_show_graph")) {
                     if ($graphs =& $this->query->get_graph_by_name($group->group_name)) {
                         foreach ($graphs as $graph) {
                             $graph->width_pdf_actual = check_for_default("GraphWidthPDF", $graph->width_pdf);
                             $graph->height_pdf_actual = check_for_default("GraphHeightPDF", $graph->height_pdf);
                             $graph->title_actual = reportico_assignment::reportico_meta_sql_criteria($this->query, $graph->title, true);
                             $graph->xtitle_actual = reportico_assignment::reportico_meta_sql_criteria($this->query, $graph->xtitle, true);
                             $graph->ytitle_actual = reportico_assignment::reportico_meta_sql_criteria($this->query, $graph->ytitle, true);
                             if ($url_string = $graph->generate_url_params($this->query->target_format)) {
                                 $this->plot_graph($graph, $graph_ct);
                                 $graph_ct++;
                             }
                         }
                     }
                 }
             }
         } while (prev($this->query->groups));
     }
 }
 function format_page_footer(&$footer)
 {
     $startcol = $footer->get_attribute("ColumnStartPDF");
     $tw = $this->abs_paging_width($startcol);
     if (!$tw) {
         $tw = $this->abs_left_margin;
     }
     $wd = $footer->get_attribute("ColumnWidthPDF");
     if (!$wd) {
         if ($this->abs_right_margin > $tw) {
             $wd = $this->abs_right_margin - $tw;
         } else {
             $wd = "100%";
         }
     }
     $wd = $this->abs_paging_width($wd);
     $just = $this->justifys[$footer->derive_attribute("justify", "left")];
     $y = $this->abs_bottom_margin - $this->vsize * $footer->line;
     $this->set_position($tw, $y);
     // Decode page header
     $tx = $footer->text;
     $styles = false;
     $matches = array();
     if (preg_match("/{STYLE[ ,]*([^}].*)}/", $tx, $matches)) {
         if (isset($matches[1])) {
             $stylearr = explode(";", $matches[1]);
             $tx = preg_replace("/{STYLE[ ,]*[^}].*}/", "", $tx);
             foreach ($stylearr as $v) {
                 if (!$v) {
                     continue;
                 }
                 $style = explode(":", $v);
                 if (trim($style[0]) == "width") {
                     $wd = trim($style[1]);
                 } else {
                     $styles[trim($style[0])] = trim($style[1]);
                 }
             }
         }
     }
     $tx = $this->reportico_string_to_php($tx);
     $tx = reportico_assignment::reportico_meta_sql_criteria($this->query, $tx);
     $tx = preg_replace("/<\\/*u>/", "", $tx);
     $this->apply_style_tags($styles);
     $this->draw_cell($wd, $this->vsize, $tx, "PBF", 0, $just);
     $this->unapply_style_tags($styles);
     $this->end_line();
     return;
 }