/** * constructor de la clase, incializa el tipo de formato en que se va a sacar la consulta. * Crea un objeto groupby para cada groupby del json y los guarda en un array. Cada groupby * tiene un array de objetos sumatorio, uno para cada sumatorio o contador del json.Asi el json pasa a ser * una estructura de arrays y objetos */ function SQLFrame($tipo) { /*parsea el json donde se configura las posiciones del informe en pdf*/ $jsonfile = file_get_contents(realpath(dirname(__FILE__)) . '\\informes\\PDF.json'); SQLFrame::$json = json_decode($jsonfile, true); $this->tipo = $tipo; $this->groups = array(); //$xml = file_get_contents('PHPDataGrid\config.xml'); //$xml = file_get_contents('C:\Archivos de programa\Apache Software Foundation\Apache2.2\htdocs\SQLFrame\PHPDataGrid\config.xml'); //$DOM = new DOMDocument('1.0', 'utf-8'); //$DOM->loadXML($xml);*/ //$groupsby = $DOM->getElementsByTagName('groupby'); //$xml = simplexml_load_file("config.xml");//new SimpleXMLElement("config.xml"); $jsonfile = file_get_contents('PHPDataGrid\\config.json'); $json = json_decode($jsonfile, true); $groupsby = $json['groupby']; foreach ($groupsby as $gby) { $group = new groupby($gby['nombre'], $gby['posicion'], $gby['class'], $gby['total']); array_push($this->groups, $group); } $sumatorios = $json['sumatorio']; //$sumatorios = $DOM->getElementsByTagName('sumatorio'); foreach ($sumatorios as $suma) { foreach ($this->groups as $group) { $group->add($suma['nombre'], $suma['posicion'], false); } } $contadores = $json['$contador']; //$contadores = $DOM->getElementsByTagName('contador'); foreach ($contadores as $cont) { foreach ($this->groups as $group) { $group->add($cont['nombre'], $cont->getAttribute['posicion'], true); } } }
/** * se le introduce la consulta en sql y el tipo de formato en que se quiere importar:"pdf","csv" o "html" */ public static function printar($query, $tipo) { //Cargamos los datos de la BD y el numero de lineas por pagina de config.json cargar_configuracion(); //inicializamos el SQLFrame pasandole el tipo en que queremos el informe $sqlframe = new SQLFrame($tipo); //conectamos con la BD $con = conectar(); //guardamos en data lo que nos viene de la base de datos DataGrid::$data = mysql_query("{$query}", $con) or die(mysql_error()); //Si el tipo es pdf creamos el objeto pdf para guardarlo if (strcmp($tipo, "pdf") == 0) { DataGrid::$pdf = new PDF(rand(5, 50)); } //Si el tipo es html creamos la tabla y la fila donde iran los datos de la cabecera if (strcmp($tipo, "html") == 0) { echo "<table class='tabla'>"; echo "<tr class='cabecera'>"; } /*Si no es el tipo pdf, printamos el nombre de las columnas de la BD, en pdf ya lo hace el cabecera informe*/ if (strcmp($tipo, "pdf") != 0) { $i = 0; while ($i < mysql_num_fields(DataGrid::$data)) { if (strcmp($tipo, "html") == 0) { echo "<td>" . mysql_field_name(DataGrid::$data, $i) . "</td>"; } else { echo mysql_field_name(DataGrid::$data, $i) . ";"; } $i++; } if (strcmp($tipo, "html") == 0) { echo "</tr>"; } else { if (strcmp($tipo, "csv") == 0) { echo "\n"; } } } /*Para cada fila llamamos al add del sqlframe para ver si tiene que printar algun subtotal y printamos la fila*/ while ($row = mysql_fetch_assoc(DataGrid::$data)) { $sqlframe->add($row); if (strcmp($tipo, "html") == 0) { echo "<tr class='fila'>"; } $i = 0; if (strcmp($tipo, "pdf") != 0) { while ($i < mysql_num_fields(DataGrid::$data)) { if (is_float($row[mysql_field_name(DataGrid::$data, $i)] + 0)) { if (strcmp($tipo, "html") == 0) { echo "<td>" . number_format($row[mysql_field_name(DataGrid::$data, $i)], 2) . "</td>"; } else { if (strcmp($tipo, "csv") == 0) { echo number_format($row[mysql_field_name(DataGrid::$data, $i)], 2) . ";"; } } } else { if (strcmp($tipo, "html") == 0) { echo "<td>" . $row[mysql_field_name(DataGrid::$data, $i)] . "</td>"; } else { if (strcmp($tipo, "csv") == 0) { echo $row[mysql_field_name(DataGrid::$data, $i)] . ";"; } } } $i++; } } else { DataGrid::$pdf->add_row($row); /*Para el pdf ademas incrementamos la linea en que estamos y miramos si hemos llegado al final de la pagina, * si hemos llegado creamo una nueva y guardamos la vieja en el array de gif. */ DataGrid::$pdf->add_linea(); DataGrid::$pdf->comprobar_tamaño(); } if (strcmp($tipo, "html") == 0) { echo "</tr>"; } else { if (strcmp($tipo, "csv") == 0) { echo "\n"; } } } /* printamos todos los groupby así tenemos los subtotales que quedaban y el TOTAL*/ $sqlframe->printar_todos(); /*para pdf añadimos al array de gif el gif actual, montamos el pdf con el array de gif y ponemos el html que muestra el pdf*/ if (strcmp($tipo, "pdf") == 0) { DataGrid::$pdf->añadir_gif(); $nompdf = DataGrid::$pdf->crear_PDF(); DataGrid::printar_pdf($nompdf); } /* en html cerramos la tabla*/ if (strcmp($tipo, "html") == 0) { echo "</table>"; } }
/** * printa el groupby en csv */ public function printar_csv() { while (SQLFrame::$pos_file < $this->posicion) { echo ";"; SQLFrame::$pos_file++; } echo "TOTAL;"; SQLFrame::$pos_file++; foreach ($this->sumatorios as $suma) { $suma->printar("csv"); } echo "\n"; SQLFrame::$pos_file = 0; }