git-svn-id: https://192.168.0.254/svn/Proyectos.FundacionLQDVI_WebCongresos/trunk@2 94ccb1af-fd9d-d947-8d90-7f70ea60afc8
1809 lines
58 KiB
PHP
1809 lines
58 KiB
PHP
<?php
|
|
/*
|
|
* @component SQL 2 Excel Component
|
|
* @copyright Copyright (C) Joomla-R-Us, joomla-r-us.com
|
|
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU/GPLv3
|
|
*/
|
|
|
|
// no direct access
|
|
defined( '_JEXEC' ) or die( 'Restricted access' );
|
|
|
|
require_once ( JPATH_SITE.DS.'components'.DS.'com_sql2excel'.DS.'helpers'.DS.'parms.php');
|
|
|
|
|
|
|
|
class writeExcel
|
|
{
|
|
|
|
var $wbFormatsArr;
|
|
|
|
function writeFile($wbParms, $wsParms, $cmpParms, $substParms, $useCache=1, $silent=0, $wbFN='-', $noComp=0)
|
|
{
|
|
|
|
// Holder for WB format pointers
|
|
$this->wbFormatsArr = array();
|
|
|
|
|
|
// Get Excel Writer
|
|
$writerVersion = writeExcel::get_writer($wbParms, $cmpParms);
|
|
|
|
switch ($writerVersion) {
|
|
case '2000_utf8':
|
|
require_once(JPATH_SITE.DS.'components'.DS.'com_sql2excel'.DS.'helpers'.DS.'PEAR'.DS.'Writer_joomlarus.php');
|
|
break;
|
|
case '2000_limited':
|
|
default:
|
|
require_once(JPATH_SITE.DS.'components'.DS.'com_sql2excel'.DS.'helpers'.DS.'Writer'.DS.'Worksheet.php');
|
|
require_once(JPATH_SITE.DS.'components'.DS.'com_sql2excel'.DS.'helpers'.DS.'Writer'.DS.'Workbook.php');
|
|
}
|
|
|
|
|
|
|
|
$db = & JFactory::getDBO();
|
|
$wbParms = $wbParms[0];
|
|
$extraParms = array();
|
|
$extraParms['SQL2EXCEL_WB_LINK'] = $wbParms->link_title;
|
|
$extraParms['SQL2EXCEL_WB_DLCNT'] = $wbParms->count + 1;
|
|
$colHeadRow=1;
|
|
|
|
// Filename
|
|
$fileName = trim($wbParms->filename);
|
|
$fileName = writeExcel::replace_vars($fileName, $substParms, $extraParms, $cmpParms );
|
|
$fileName = trim($fileName);
|
|
if ( $fileName == '' || $fileName == null ) {
|
|
$fileName = 'download.xls';
|
|
}
|
|
|
|
// SheetNames unique?
|
|
writeExcel::checkSheetNames($wsParms, $cmpParms, $substParms);
|
|
|
|
|
|
// Compression?
|
|
$usingCompression = false;
|
|
if ( !$noComp && ($wbParms->compress_wb == 'Yes' || ($wbParms->compress_wb == 'Global' && Sql2excelParms::get($cmpParms,'compress_wb', 0) ) )) {
|
|
|
|
// Load ZIP class
|
|
require_once ( JPATH_SITE.DS.'components'.DS.'com_sql2excel'.DS.'helpers'.DS.'zip.lib.php');
|
|
|
|
$usingCompression = true;
|
|
}
|
|
|
|
|
|
|
|
// Check extension
|
|
$path_info = pathinfo($fileName);
|
|
if ( !isset($path_info['extension']) ||
|
|
( isset($path_info['extension']) && strtolower($path_info['extension']) != 'xls' ) ) {
|
|
$fileName .= '.xls';
|
|
}
|
|
$extraParms['SQL2EXCEL_WB_FN'] = $fileName;
|
|
|
|
|
|
// Write Header
|
|
if ( ! $silent && !$usingCompression ) {
|
|
writeExcel::ExcelHeader($fileName);
|
|
}
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
// Use Cache?
|
|
//////////////////////////////////////////////////////////////////
|
|
$cacheEnabled = Sql2excelParms::get($cmpParms,'cache_enabled', 0);
|
|
$cacheDir = writeExcel::get_cache_dir($cmpParms);
|
|
$cacheTime = Sql2excelParms::get($cmpParms,'cache_time', 30);
|
|
$wbCache = $wbParms->cache;
|
|
$usingCache = false;
|
|
if ( ( $useCache && ($cacheEnabled && $wbCache == 'Global') ) || ( $useCache && $wbCache == 'Yes' ) ) {
|
|
$usingCache = true;
|
|
}
|
|
|
|
|
|
if ( $usingCache || $usingCompression ) {
|
|
// Cache or Compression
|
|
if ( file_exists($cacheDir) ) {
|
|
$cacheFileName = writeExcel::get_cache_filename($wbParms, $wsParms);
|
|
$wbFN = $cacheDir . 'download_' . $cacheFileName . '.cache';
|
|
if ( $usingCompression ) { $wbFN .= '_c'; }
|
|
$cacheFN = $wbFN;
|
|
if ( $silent && $usingCompression ) {
|
|
// Write out original filename (.xls), will be zippped later into the cache file
|
|
$wbFN = $wbParms->filename;
|
|
if ( substr($wbFN,strlen($wbFN)-4,4) != '.xls' ) {
|
|
$wbFN .= '.xls';
|
|
}
|
|
$wbFN = $cacheDir . $wbFN;
|
|
}
|
|
|
|
if ( $usingCache && file_exists($wbFN) && !$silent ) {
|
|
// Serve from Cache!?
|
|
$fileModified = filemtime($wbFN);
|
|
$age = time() - $fileModified;
|
|
$minutesOld = $age / 60;
|
|
if ( $cacheTime >= $minutesOld ) {
|
|
// Server from cache
|
|
if ( $usingCompression ) {
|
|
$fsize = @filesize($wbFN);
|
|
$fh = fopen($wbFN, 'rb', false);
|
|
if ($fh == false) {
|
|
die("Can't read cache file.");
|
|
}
|
|
$data = fread($fh, $fsize);
|
|
fclose($fh);
|
|
|
|
// Write the ZIP header
|
|
writeExcel::ZipHeader(str_replace('.xls','.zip', $fileName),strlen($data));
|
|
|
|
// Output cached file
|
|
echo $data;
|
|
|
|
} else {
|
|
|
|
$fh = fopen($wbFN, "rb");
|
|
if ($fh == false) {
|
|
die("Can't read cache file.");
|
|
}
|
|
fpassthru($fh);
|
|
}
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
// Create a workbook
|
|
if ( $writerVersion == '2000_utf8' ) {
|
|
if ( $wbFN == '-' ) { $wbFN = NULL; }
|
|
$workbook = new Spreadsheet_Excel_Writer_joomlarus($wbFN);
|
|
|
|
// Set Workbook Version
|
|
$workbook->setVersion(8);
|
|
|
|
} else {
|
|
$workbook = new Workbook($wbFN);
|
|
}
|
|
|
|
|
|
$i=1;
|
|
$nrWorksheets=0;
|
|
$marginScaleFactor = (real) Sql2excelParms::get($cmpParms,'marginscalefactor', 1);
|
|
$removeCR = Sql2excelParms::get($cmpParms,'removereturnxls', 0);
|
|
$substExtraCells = Sql2excelParms::get($cmpParms,'subst_extracells', '1');
|
|
|
|
foreach ( $wsParms as $ws ) {
|
|
|
|
$sheetName = $ws->sheetname;
|
|
$sheetName = writeExcel::replace_vars($sheetName, $substParms,0,$cmpParms);
|
|
$sheetName = substr($sheetName,0,40);
|
|
if ( trim($sheetName) == '' ) {
|
|
$wsIDnr = $nrWorksheets + 1;
|
|
$sheetName = 'Sheet ' . $wsIDnr;
|
|
}
|
|
$extraParms['SQL2EXCEL_WS_SN'] = $sheetName;
|
|
$extraParms['SQL2EXCEL_WS_DLCNT'] = $ws->count + 1;
|
|
|
|
$query = $ws->query;
|
|
$extraParms['SQL2EXCEL_WS_SQL_RAW'] = $query;
|
|
|
|
// Substitute variables in SQL query?
|
|
$substSQL = Sql2excelParms::get($cmpParms,'subst_sql', '1');
|
|
if ( $substSQL ) {
|
|
$query = writeExcel::replace_vars($query, $substParms,0,$cmpParms);
|
|
}
|
|
$extraParms['SQL2EXCEL_WS_SQL'] = $query;
|
|
|
|
$formulas = explode(',', $ws->formulas );
|
|
|
|
// Formats
|
|
$formats = writeExcel::get_formats($ws->cellformat, $cmpParms);
|
|
|
|
|
|
if ( trim($query) != '' ) {
|
|
// Get Database Connection
|
|
$querydb = writeExcel::getDB($ws);
|
|
|
|
// Run query!
|
|
if ( $querydb ) {
|
|
$rows = writeExcel::getResults($querydb, $query, $cmpParms, $ws->db_type);
|
|
} else {
|
|
$rows = JText::_( 'DB_CONNECT_ERROR');
|
|
}
|
|
|
|
$extraParms['SQL2EXCEL_WS_ROWS'] = count($rows);
|
|
} else {
|
|
$rows = '';
|
|
$extraParms['SQL2EXCEL_WS_ROWS'] = 0;
|
|
}
|
|
|
|
// Create a worksheet
|
|
$worksheet1 =& $workbook->addWorksheet(substr($sheetName,0,30));
|
|
$nrWorksheets++;
|
|
|
|
// Set encoding
|
|
if ( $writerVersion == '2000_utf8' ) {
|
|
$worksheet1->setInputEncoding('utf-8');
|
|
}
|
|
|
|
|
|
// Set Margins, if defined
|
|
$print_parms = $ws->print_parms;
|
|
$print_parms = explode(',',$print_parms);
|
|
if ( isset($print_parms[0]) && $print_parms[0] != '' ) {
|
|
$worksheet1->setMarginLeft($print_parms[0]*$marginScaleFactor);
|
|
}
|
|
if ( isset($print_parms[1]) && $print_parms[1] != '' ) {
|
|
$worksheet1->setMarginRight($print_parms[1]*$marginScaleFactor);
|
|
}
|
|
if ( isset($print_parms[2]) && $print_parms[2] != '' ) {
|
|
$worksheet1->setMarginTop($print_parms[2]*$marginScaleFactor);
|
|
}
|
|
if ( isset($print_parms[3]) && $print_parms[3] != '' ) {
|
|
$worksheet1->setMarginBottom($print_parms[3]*$marginScaleFactor);
|
|
}
|
|
if ( isset($print_parms[4]) ) {
|
|
if ( $print_parms[4] == '1' )
|
|
$worksheet1->setPortrait();
|
|
elseif ( $print_parms[4] == '2' )
|
|
$worksheet1->setLandscape();
|
|
}
|
|
if ( isset($print_parms[5]) && $print_parms[5] != '0' && $print_parms[5] != '') {
|
|
$worksheet1->setPaper($print_parms[5]);
|
|
}
|
|
if ( isset($print_parms[6]) && $print_parms[6] == '1' ) {
|
|
$worksheet1->centerHorizontally(1);
|
|
}
|
|
if ( isset($print_parms[7]) && $print_parms[7] == '1' ) {
|
|
$worksheet1->centerVertically(1);
|
|
}
|
|
if ( isset($print_parms[8]) && $print_parms[8] == '1' ) {
|
|
$worksheet1->hideGridlines();
|
|
}
|
|
|
|
// Zoom level
|
|
if ( $ws->zoom != '' && $ws->zoom != 100 ) {
|
|
$worksheet1->setZoom($ws->zoom);
|
|
}
|
|
|
|
// Hide screen grid lines?
|
|
if ( $ws->hide_grid ) {
|
|
$worksheet1->hideScreenGridlines();
|
|
}
|
|
|
|
// Set column widths - if defined
|
|
$colWidths = $ws->colwidths;
|
|
$colWidths = explode(',', $colWidths );
|
|
for ($i=0; $i<count($colWidths); $i++)
|
|
{
|
|
if ( is_numeric($colWidths[$i]) && $colWidths[$i] >= 0 ) {
|
|
if ( $colWidths[$i] > 0 ) {
|
|
$worksheet1->setColumn($i, $i, $colWidths[$i]);
|
|
} else {
|
|
$worksheet1->setColumn($i, $i, $colWidths[$i],0,1); // Hidden
|
|
}
|
|
}
|
|
}
|
|
|
|
// Add Header, if specified
|
|
$substHeadFoot = Sql2excelParms::get($cmpParms,'subst_head_footer', '1');
|
|
$currRow = 0;
|
|
$header = $ws->header;
|
|
if ( $header != '' ) {
|
|
$headertextArr = explode("\n",$header);
|
|
if ( is_array($headertextArr) ) {
|
|
if ( $ws->header_parms == '' )
|
|
$ws->header_parms = 'Arial,10,normal,black,none';
|
|
|
|
$hparms = explode(",",$ws->header_parms);
|
|
foreach ($headertextArr as $headerrow) {
|
|
$headerrow = trim($headerrow);
|
|
if ( $headerrow != null ) {
|
|
if ( $substHeadFoot ) {
|
|
$headerrow = writeExcel::replace_vars($headerrow, $substParms, $extraParms, $cmpParms );
|
|
}
|
|
$cellFormat = writeExcel::get_cellformat($formats,$currRow,0);
|
|
if ( writeExcel::is_url($headerrow) ) {
|
|
$format_head =& writeExcel::addFormat($workbook, $hparms[0], $hparms[1], $hparms[2], 'blue', $hparms[4], '', $cellFormat);
|
|
writeExcel::writeCell($worksheet1, $currRow, 0, $headerrow, $format_head);
|
|
} else {
|
|
$format_head =& writeExcel::addFormat($workbook, $hparms[0], $hparms[1], $hparms[2], $hparms[3], $hparms[4], '', $cellFormat);
|
|
writeExcel::writeCell($worksheet1, $currRow, 0, $headerrow, $format_head);
|
|
}
|
|
}
|
|
$currRow++;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
$mindatarow = $currRow;
|
|
$maxdatarow = $currRow;
|
|
|
|
if ( is_array($rows) && is_object($rows[0]) ) {
|
|
// Add column titles
|
|
$colNames = writeExcel::getColumnNames($rows);
|
|
//Number of Columns in user query (exclude JoomFish fields if any)
|
|
|
|
$nrCols = writeExcel::getColumnCount($colNames, $query);
|
|
if ( $ws->show_colheader <> 0 ) {
|
|
$headerTextSize = $ws->heading_text_size;
|
|
if ( $headerTextSize == null || $headerTextSize == 0 ) { $headerTextSize = 10; }
|
|
$headerTextCol = $ws->heading_text_col;
|
|
$headerBgCol = $ws->heading_bg_col;
|
|
|
|
for ($i=0; $i<$nrCols; $i++)
|
|
{
|
|
$cellFormat = writeExcel::get_cellformat($formats,$currRow,$i);
|
|
$format_colhead =& writeExcel::addFormat($workbook, 'Arial', $headerTextSize, 'normal', $headerTextCol, $headerBgCol, 'center', $cellFormat);
|
|
writeExcel::writeCell($worksheet1, $currRow, $i, $colNames[$i], $format_colhead, $formulas);
|
|
}
|
|
$currRow++;
|
|
}
|
|
|
|
$colHeadRow = $currRow;
|
|
|
|
|
|
// Add all the data
|
|
$mindatarow = $currRow + 1;
|
|
$maxdatarow = $currRow + count($rows);
|
|
if ( $ws->data_parms == '' )
|
|
$ws->data_parms = 'Arial,10,normal,black,None,silver,1,27,1';
|
|
|
|
$dparms = explode(",",$ws->data_parms);
|
|
|
|
if ( sizeof($dparms) >= 9 ) {
|
|
if ( $dparms[4] == 'None' ) {
|
|
$dparms[5] = 'none';
|
|
$dparms[7] = 'none';
|
|
}
|
|
$format_data =& writeExcel::addFormat($workbook, $dparms[0], $dparms[1], $dparms[2], $dparms[3], $dparms[5]);
|
|
$format_data_url =& writeExcel::addFormat($workbook, $dparms[0], $dparms[1], $dparms[2], 'blue', $dparms[5]);
|
|
$format_data_2 =& writeExcel::addFormat($workbook, $dparms[0], $dparms[1], $dparms[2], $dparms[3], $dparms[7]);
|
|
$format_data_2_url =& writeExcel::addFormat($workbook, $dparms[0], $dparms[1], $dparms[2], 'blue', $dparms[7]);
|
|
|
|
$col1_cnt = 100000;
|
|
$col2_cnt = 0;
|
|
if ( $dparms[4] == 2 ) {
|
|
$col1_cnt = $dparms[6];
|
|
$col2_cnt = $dparms[8];
|
|
}
|
|
|
|
} else {
|
|
$format_data = null;
|
|
}
|
|
|
|
// Handle alternating formats
|
|
$currCol = 1;
|
|
$currFormat = $format_data;
|
|
$currFormat_url = $format_data_url;
|
|
$currCounter = $col1_cnt;
|
|
$colCnt = 0;
|
|
|
|
foreach ( $rows as $row ) {
|
|
for ( $i=0; $i<$nrCols; $i++) {
|
|
if ( sizeof($dparms) >= 9 && $format_data != null ) {
|
|
$cellFormat = writeExcel::get_cellformat($formats,$currRow,$i, $mindatarow, $maxdatarow, $row->$colNames[$i]);
|
|
if ( writeExcel::is_url($row->$colNames[$i]) )
|
|
if ( $cellFormat ) {
|
|
if ( $currCol == 1 ) {
|
|
$format_data_cust =& writeExcel::addFormat($workbook, $dparms[0], $dparms[1], $dparms[2], 'blue', $dparms[5], '', $cellFormat);
|
|
} else {
|
|
$format_data_cust =& writeExcel::addFormat($workbook, $dparms[0], $dparms[1], $dparms[2], 'blue', $dparms[7], '', $cellFormat);
|
|
}
|
|
writeExcel::writeCell($worksheet1, $currRow, $i, $row->$colNames[$i], $format_data_cust, $formulas,$mindatarow,$maxdatarow, $removeCR);
|
|
} else {
|
|
writeExcel::writeCell($worksheet1, $currRow, $i, $row->$colNames[$i], $currFormat_url, $formulas,$mindatarow,$maxdatarow, $removeCR);
|
|
}
|
|
else {
|
|
if ( $cellFormat ) {
|
|
if ( $currCol == 1 ) {
|
|
$format_data_cust =& writeExcel::addFormat($workbook, $dparms[0], $dparms[1], $dparms[2], $dparms[3], $dparms[5], '', $cellFormat);
|
|
} else {
|
|
$format_data_cust =& writeExcel::addFormat($workbook, $dparms[0], $dparms[1], $dparms[2], $dparms[3], $dparms[7], '', $cellFormat);
|
|
}
|
|
writeExcel::writeCell($worksheet1, $currRow, $i, $row->$colNames[$i], $format_data_cust, $formulas,$mindatarow,$maxdatarow, $removeCR);
|
|
} else {
|
|
writeExcel::writeCell($worksheet1, $currRow, $i, $row->$colNames[$i], $currFormat, $formulas,$mindatarow,$maxdatarow, $removeCR);
|
|
}
|
|
|
|
}
|
|
} else {
|
|
writeExcel::writeCell($worksheet1, $currRow, $i, $row->$colNames[$i], null, $formulas,$mindatarow,$maxdatarow, $removeCR);
|
|
}
|
|
}
|
|
$currRow++;
|
|
$colCnt++;
|
|
if ( $colCnt == $currCounter ) {
|
|
if ( $currCol == 1 ) {
|
|
$currCol = 2;
|
|
$currFormat = $format_data_2;
|
|
$currFormat_url = $format_data_2_url;
|
|
$currCounter = $col2_cnt;
|
|
}
|
|
else {
|
|
$currCol = 1;
|
|
$currFormat = $format_data;
|
|
$currFormat_url = $format_data_url;
|
|
$currCounter = $col1_cnt;
|
|
}
|
|
$colCnt = 0;
|
|
}
|
|
}
|
|
|
|
} elseif ( ( $rows == '' || ( is_array($rows) && !is_object($rows[0]) ) ) && $query != '' ) {
|
|
// Empty recordset.
|
|
$includeNoRec = $ws->include_no_records;
|
|
if ( $includeNoRec ) {
|
|
$errMsg = $ws->error_norecords;
|
|
if ( trim($errMsg) == '' ) {
|
|
$errMsg = JText::_( 'No records found' );
|
|
writeExcel::writeCell($worksheet1, $currRow, 0, $errMsg);
|
|
} else {
|
|
$errMsg = explode("\n", $errMsg);
|
|
$i=0;
|
|
foreach ( $errMsg as $row ) {
|
|
$row = trim($row);
|
|
$row = writeExcel::replace_vars($row, $substParms, $extraParms, $cmpParms );
|
|
writeExcel::writeCell($worksheet1, $currRow + $i, 0, $row);
|
|
$i++;
|
|
}
|
|
$currRow = $currRow + $i;
|
|
}
|
|
}
|
|
} elseif ( $rows != '' && is_string($rows) && Sql2excelParms::get($cmpParms,'show_sql_errors', 1) ) {
|
|
$errMsg = explode("\n", $rows);
|
|
$i=0;
|
|
foreach ( $errMsg as $row ) {
|
|
$row = trim($row);
|
|
writeExcel::writeCell($worksheet1, $currRow + $i, 0, $row);
|
|
$i++;
|
|
}
|
|
$currRow = $currRow + $i;
|
|
}
|
|
|
|
// Extra Cells?
|
|
$extraCells = explode("\n",$ws->extracells);
|
|
$maxExtraRow = 0;
|
|
foreach ( $extraCells as $row ) {
|
|
$cellInfo = explode(',',$row);
|
|
|
|
|
|
if ( count($cellInfo) > 6 ) {
|
|
$cellType = $cellInfo[0];
|
|
|
|
$cellDBtype = 1;
|
|
if ( isset($cellInfo[7]) ) { $cellDBtype = $cellInfo[7]; }
|
|
|
|
$cellFont = 'Arial';
|
|
if ( isset($cellInfo[8]) ) { $cellFont = $cellInfo[8]; }
|
|
|
|
$cellFontSize = 10;
|
|
if ( isset($cellInfo[9]) ) { $cellFontSize = $cellInfo[9]; }
|
|
|
|
$cellAlign = '';
|
|
if ( isset($cellInfo[10]) ) { $cellAlign = $cellInfo[10]; }
|
|
|
|
// Replace escape sequence with commas
|
|
$cellData = trim(str_replace('@~@',',',$cellInfo[3]));
|
|
$ecrow = $cellInfo[1];
|
|
$eccol = $cellInfo[2];
|
|
// Substitute variables in celldata
|
|
if ( $substExtraCells ) {
|
|
$cellData = writeExcel::replace_vars($cellData, $substParms, $extraParms, $cmpParms );
|
|
$cellData = trim(writeExcel::replace_row_col_ids(' '.$cellData, 0, 0, $mindatarow, $maxdatarow));
|
|
$ecrow = trim(writeExcel::replace_row_col_ids(' '.$ecrow, 0, 0, $mindatarow, $maxdatarow));
|
|
$eccol = trim(writeExcel::replace_row_col_ids(' '.$eccol, 0, 0, $mindatarow, $maxdatarow));
|
|
}
|
|
$ecrow = (int) $ecrow-1;
|
|
$eccol = (int) $eccol-1;
|
|
$cellFormat = writeExcel::get_cellformat($formats,$ecrow,$eccol, $mindatarow, $maxdatarow);
|
|
$formatH =& writeExcel::addFormat($workbook, $cellFont, $cellFontSize, $cellInfo[5], $cellInfo[4], trim($cellInfo[6]), $cellAlign, $cellFormat);
|
|
|
|
if ( $cellType == 1 ) {
|
|
// Text
|
|
writeExcel::writeCell($worksheet1, $ecrow, $eccol, $cellData, $formatH );
|
|
if ( $cellInfo[1]-2 > $maxExtraRow ) {
|
|
$maxExtraRow = $cellInfo[1]-1;
|
|
}
|
|
} elseif ( $cellType == 2 ) {
|
|
// Formula
|
|
writeExcel::writeCell($worksheet1, $ecrow, $eccol, $cellData, $formatH, array($eccol+1), $mindatarow, $maxdatarow );
|
|
if ( $cellInfo[1]-2 > $maxExtraRow ) {
|
|
$maxExtraRow = $cellInfo[1]-1;
|
|
}
|
|
} elseif ( $cellType == 3 ) {
|
|
// Text after last datarow
|
|
writeExcel::writeCell($worksheet1, $currRow, $eccol, $cellData, $formatH);
|
|
if ( $currRow > $maxExtraRow ) {
|
|
$maxExtraRow = $currRow;
|
|
}
|
|
} elseif ( $cellType == 4 ) {
|
|
// Formula after last datarow
|
|
writeExcel::writeCell($worksheet1, $currRow, $eccol, $cellData, $formatH, array($eccol+1), $mindatarow, $maxdatarow);
|
|
if ( $currRow > $maxExtraRow ) {
|
|
$maxExtraRow = $currRow;
|
|
}
|
|
} elseif ( $cellType == 5 ) {
|
|
// Insert Bitmap Image
|
|
writeExcel::insertImage($worksheet1, $ecrow, $eccol, $cellData);
|
|
if ( $currRow > $maxExtraRow ) {
|
|
$maxExtraRow = $currRow;
|
|
}
|
|
} elseif ( $cellType == 6 ) {
|
|
// SQL query data
|
|
$maxExtraRow = writeExcel::insertextraSQL($worksheet1, $ecrow, $eccol, strrev($cellData), $cellDBtype, $formatH, $maxExtraRow, $db, $cmpParms, $formulas, $substSQL, $substParms);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Move down the footer below the lowest cell
|
|
$maxExtraRow++;
|
|
if ( $maxExtraRow > $currRow ) {
|
|
$currRow = $maxExtraRow;
|
|
}
|
|
|
|
|
|
|
|
// Add Footer, if specified
|
|
$footer = $ws->footer;
|
|
$footertextArr = explode("\n",$footer);
|
|
|
|
if ( is_array($footertextArr) ) {
|
|
if ( $ws->footer_parms == '' )
|
|
$ws->footer_parms = 'Arial,10,normal,black,none';
|
|
|
|
$fparms = explode(",",$ws->footer_parms);
|
|
foreach ($footertextArr as $footerrow) {
|
|
$footerrow = trim($footerrow);
|
|
if ( $footerrow != null ) {
|
|
if ( $substHeadFoot ) {
|
|
$footerrow = writeExcel::replace_vars($footerrow, $substParms, $extraParms, $cmpParms );
|
|
}
|
|
$cellFormat = writeExcel::get_cellformat($formats,$currRow,0, $mindatarow, $maxdatarow);
|
|
if ( writeExcel::is_url($footerrow) ) {
|
|
$format_footer_url =& writeExcel::addFormat($workbook, $fparms[0], $fparms[1], $fparms[2], 'blue', $fparms[4], '', $cellFormat);
|
|
writeExcel::writeCell($worksheet1, $currRow, 0, $footerrow, $format_footer_url);
|
|
} else {
|
|
$format_footer =& writeExcel::addFormat($workbook, $fparms[0], $fparms[1], $fparms[2], $fparms[3], $fparms[4],'',$cellFormat);
|
|
writeExcel::writeCell($worksheet1, $currRow, 0, $footerrow, $format_footer);
|
|
}
|
|
}
|
|
$currRow++;
|
|
}
|
|
}
|
|
|
|
// Password Protect the Worksheet?
|
|
if ( trim($ws->ws_password) != '' ) {
|
|
$pwd = writeExcel::convert_utf8(trim($ws->ws_password));
|
|
$worksheet1->protect($pwd);
|
|
}
|
|
|
|
// Add Freeze Pane?
|
|
if ( $ws->pane_horiz >= 0 || $ws->pane_vert > 0 ) {
|
|
$paneRow = $ws->pane_horiz;
|
|
if ( $paneRow == 0 ) { $paneRow = $colHeadRow; }
|
|
if ( $paneRow < 0 ) { $paneRow = 0; }
|
|
$paneCol = $ws->pane_vert;
|
|
if ( $paneCol < 0 ) { $paneCol = 0; }
|
|
|
|
$worksheet1->freezePanes(array($paneRow,$paneCol,$paneRow,$paneCol));
|
|
}
|
|
|
|
|
|
// Unset database handle
|
|
if ( isset($querydb) ) {
|
|
unset($querydb);
|
|
}
|
|
|
|
}
|
|
|
|
if ( $nrWorksheets == 0 ) {
|
|
$worksheet1 =& $workbook->addWorksheet('Sheet 1');
|
|
writeExcel::writeCell($worksheet1, 0, 0, JText::_( 'Workbook does not contain any valid worksheets with data.') );
|
|
writeExcel::writeCell($worksheet1, 1, 0, JText::_( 'Turn on SQL Error Messages in SQL 2 Excel Component to debug the problem.') );
|
|
}
|
|
|
|
|
|
$workbook->close();
|
|
|
|
|
|
|
|
// Stream from cache?
|
|
if ( !is_null($wbFN) && is_string($wbFN) && $wbFN != '-' ) {
|
|
|
|
if ( $usingCompression ) {
|
|
|
|
// Compress the file with ZIP
|
|
$zip = new zipfile();
|
|
$fsize = @filesize($wbFN);
|
|
$fh = fopen($wbFN, 'rb', false);
|
|
$data = fread($fh, $fsize);
|
|
fclose($fh);
|
|
$zip->addFile($data,$fileName);
|
|
$zipcontents = $zip->file();
|
|
|
|
if ( !$silent ) {
|
|
|
|
// Write the ZIP header
|
|
writeExcel::ZipHeader(str_replace('.xls','.zip', $fileName),strlen($zipcontents));
|
|
|
|
// Output ZIP data to browser
|
|
echo $zipcontents;
|
|
}
|
|
|
|
// Cleanup or Save ZIP file to cache!?
|
|
if ( !$usingCache ) {
|
|
unlink($wbFN);
|
|
} else {
|
|
$fh = fopen($cacheFN, 'wb', false);
|
|
fputs($fh,$zipcontents);
|
|
fclose($fh);
|
|
}
|
|
|
|
} elseif ( !$silent ) {
|
|
// Stream file from cache
|
|
$fh = fopen($wbFN, "rb");
|
|
if ($fh == false) {
|
|
die("Can't read cache file.");
|
|
}
|
|
fpassthru($fh);
|
|
}
|
|
|
|
|
|
|
|
if ( $usingCache && $silent ) {
|
|
// Copy cache file to ./scheduler subfolder for email attachment
|
|
writeExcel::copyCacheFile($cacheFN, $fileName, $cacheDir);
|
|
|
|
// Compress the cache file?
|
|
if ( $wbParms->compress_wb == 'Yes' || ($wbParms->compress_wb == 'Global' && Sql2excelParms::get($cmpParms,'compress_wb', 0) ) ) {
|
|
$zip = new zipfile();
|
|
$fsize = @filesize($cacheFN);
|
|
$fh = fopen($cacheFN, 'rb', false);
|
|
$data = fread($fh, $fsize);
|
|
fclose($fh);
|
|
$zip->addFile($data,$fileName);
|
|
$zipcontents = $zip->file();
|
|
$fh = fopen($cacheFN.'_c', 'wb', false);
|
|
fputs($fh,$zipcontents);
|
|
fclose($fh);
|
|
unlink($cacheFN);
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
|
|
// Increments Row Number and adds Freeze Pane if specified
|
|
function incrRow($ws, $worksheet, $currRow, $colHead=false) {
|
|
|
|
// Add Freeze Pane?
|
|
if ( $ws->pane_horiz >= 0 || $ws->pane_vert > 0 ) {
|
|
$paneRow = $ws->pane_horiz;
|
|
if ( $paneRow == 0 && $colHead ) { $paneRow = $currRow; }
|
|
$paneCol = $ws->pane_vert;
|
|
if ( $paneCol < 0 ) { $paneCol = 0; }
|
|
if ( $paneRow == $currRow+1 ) {
|
|
|
|
$worksheet->freezePanes(array($paneRow,$paneCol,$paneRow,$paneCol));
|
|
}
|
|
}
|
|
|
|
return $currRow+1;
|
|
}
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
// Check for bad words in an array
|
|
// Returns '' if ok
|
|
// Returns Error Message if something bad is found.
|
|
////////////////////////////////////////////////////////////
|
|
function check_bad_words_arr($cmpParms, $wordArr) {
|
|
|
|
|
|
// Get list of bad words to check
|
|
$badWordsArr = writeExcel::get_bad_words_array($cmpParms);
|
|
|
|
$badWordsMsg = '';
|
|
|
|
if ( is_array($badWordsArr) && is_array($wordArr) ) {
|
|
$varNames = array_keys($wordArr);
|
|
foreach ( $varNames as $varName ) {
|
|
$varValue = $wordArr{$varName};
|
|
$varValueArr = explode(' ', $varValue);
|
|
foreach ( $varValueArr as $token ) {
|
|
$token = strtoupper(trim($token));
|
|
if ($token != '' && in_array($token, $badWordsArr) ) {
|
|
$badWordsMsg = JText::_( 'BAD_WORDS_ERROR_MSG');
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return $badWordsMsg;
|
|
}
|
|
|
|
|
|
function get_bad_words_array($cmpParms)
|
|
{
|
|
$badWordsArr = null;
|
|
$badWordsStr = Sql2excelParms::get($cmpParms,'badwords', 'ALTER,CREATE,DELETE,DROP,GRANT,INSERT,KILL,LOAD,LOCK,RENAME,REPLACE,REVOKE,SET,TRUNCATE,UPDATE');
|
|
if ( trim($badWordsStr) != '' ) {
|
|
$badWordsStr = strtoupper($badWordsStr);
|
|
$badWordsArr = explode(',',$badWordsStr);
|
|
}
|
|
return $badWordsArr;
|
|
}
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
// Check final SQL for bad words
|
|
// Returns '' if ok
|
|
// Returns Error Message if something bad is found.
|
|
////////////////////////////////////////////////////////////
|
|
function check_sql_bad_words($cmpParms, $sql) {
|
|
|
|
// Get list of bad words to check
|
|
$badWordsArr = writeExcel::get_bad_words_array($cmpParms);
|
|
|
|
$sql = str_replace("\n", ' ', $sql);
|
|
$sqlWordArr = explode(' ', $sql);
|
|
|
|
return writeExcel::check_bad_words_arr($cmpParms, $sqlWordArr);
|
|
}
|
|
|
|
|
|
|
|
function replace_vars($str, $commonVars, $extraVars=0, $cmpParms=0) {
|
|
|
|
if ( !is_object($cmpParms) ) {
|
|
$cmpParms = Sql2excelParms::getParms();
|
|
}
|
|
|
|
$i=0;
|
|
$strArr = explode("\n", $str);
|
|
foreach ( $strArr as $row ) {
|
|
$row = writeExcel::replace_date_codes($row,$cmpParms);
|
|
$row = writeExcel::replace_vars_arr($row, $commonVars);
|
|
$row = writeExcel::replace_vars_arr($row, $extraVars);
|
|
$strArr[$i] = writeExcel::replace_date_codes($row,$cmpParms);
|
|
$i++;
|
|
}
|
|
return implode("\n",$strArr);
|
|
}
|
|
|
|
function replace_vars_arr($str, $varArr) {
|
|
$retStr = $str;
|
|
if ( is_array($varArr) ) {
|
|
$varNames = array_keys($varArr);
|
|
foreach ( $varNames as $varName ) {
|
|
if ( substr($varName,0,16) == 'SQL2EXCEL_WS_SQL' ) {
|
|
// Possible multi-line parameters, convert to one line
|
|
$varValues = explode("\n", $varArr{$varName});
|
|
$varValue = '';
|
|
foreach ( $varValues as $row ) {
|
|
$varValue .= trim($row) . ' ';
|
|
}
|
|
$retStr = str_replace('{' . $varName . '}', trim($varValue), $retStr);
|
|
} else {
|
|
$retStr = str_replace('{' . $varName . '}', $varArr{$varName}, $retStr);
|
|
}
|
|
}
|
|
}
|
|
return $retStr;
|
|
}
|
|
|
|
function writeCell(&$worksheet1, $row, $col, $str, $format=0, $formulas=0, $mindatarow=0, $maxdatarow=0, $removeCR=0) {
|
|
$str = writeExcel::convert_utf8($str);
|
|
if ( $removeCR > 0 ) { $str = writeExcel::removeCR($str, $removeCR); }
|
|
if ( substr(trim($str),0,1) == '=' || substr(trim($str),0,1) == '@' ) {
|
|
$colUser = $col+1;
|
|
$rowUser = $row+1;
|
|
if ( is_array($formulas) && in_array($colUser, $formulas) ) {
|
|
// Formula! => Parse and replace Row and Col ID's
|
|
$str = writeExcel::replace_row_col_ids($str, $rowUser, $colUser, $mindatarow, $maxdatarow);
|
|
|
|
// Write the formula
|
|
$worksheet1->write($row, $col, $str, $format);
|
|
} else {
|
|
// Export as Text
|
|
$worksheet1->writeString($row, $col, $str, $format);
|
|
}
|
|
} else {
|
|
if ( substr($str,0,1) == "0" && ( strlen($str) > 1 && strpos($str, ".") === false )) {
|
|
$worksheet1->writeString($row, $col, $str, $format);
|
|
} else {
|
|
$worksheet1->write($row, $col, $str, $format);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
function removeCR($str,$removeCR) {
|
|
|
|
$repl = " ";
|
|
switch ($removeCR) {
|
|
case 2:
|
|
$repl = "\n";
|
|
break;
|
|
case 3:
|
|
$repl = "||";
|
|
break;
|
|
case 4:
|
|
$repl = "<br>";
|
|
break;
|
|
case 5:
|
|
$repl = "";
|
|
break;
|
|
}
|
|
|
|
$str = str_replace("\r\n",$repl, $str);
|
|
$str = str_replace("\n\r",$repl, $str);
|
|
$str = str_replace("\n",$repl, $str);
|
|
$str = str_replace("\r",$repl, $str);
|
|
|
|
return $str;
|
|
}
|
|
|
|
|
|
|
|
function replace_row_col_ids($str, $row, $col, $mindatarow=0, $maxdatarow=0) {
|
|
$retStr = $str;
|
|
$pos = strpos($str, '{ROWID');
|
|
if ( $pos > 0 ) {
|
|
$endPos = strpos($str, '}', $pos );
|
|
$leftStr = substr($str,0,$pos);
|
|
$rightStr = substr($str,$endPos+1);
|
|
$midStr = substr($str,$pos+1,$endPos-$pos-1);
|
|
$midStr = str_replace('ROWID', $row, $midStr);
|
|
eval("\$mStr = $midStr;");
|
|
$retStr = writeExcel::replace_row_col_ids($leftStr . $mStr . $rightStr, $row, $col);
|
|
}
|
|
|
|
$pos = strpos($str, '{COLID');
|
|
if ( $pos > 0 ) {
|
|
$endPos = strpos($str, '}', $pos );
|
|
$leftStr = substr($str,0,$pos);
|
|
$rightStr = substr($str,$endPos+1);
|
|
$midStr = substr($str,$pos+1,$endPos-$pos-1);
|
|
$midStr = str_replace('COLID', $col, $midStr);
|
|
eval("\$mStr = $midStr;");
|
|
$retStr = writeExcel::replace_row_col_ids($leftStr . $mStr . $rightStr, $row, $col);
|
|
}
|
|
|
|
$pos = strpos($str, '{MINROWID');
|
|
if ( $pos > 0 ) {
|
|
$endPos = strpos($str, '}', $pos );
|
|
$leftStr = substr($str,0,$pos);
|
|
$rightStr = substr($str,$endPos+1);
|
|
$midStr = substr($str,$pos+1,$endPos-$pos-1);
|
|
$midStr = str_replace('MINROWID', $mindatarow, $midStr);
|
|
eval("\$mStr = $midStr;");
|
|
$retStr = writeExcel::replace_row_col_ids($leftStr . $mStr . $rightStr, $row, $col, $mindatarow, $maxdatarow);
|
|
}
|
|
|
|
$pos = strpos($str, '{MAXROWID');
|
|
if ( $pos > 0 ) {
|
|
$endPos = strpos($str, '}', $pos );
|
|
$leftStr = substr($str,0,$pos);
|
|
$rightStr = substr($str,$endPos+1);
|
|
$midStr = substr($str,$pos+1,$endPos-$pos-1);
|
|
$midStr = str_replace('MAXROWID', $maxdatarow, $midStr);
|
|
eval("\$mStr = $midStr;");
|
|
$retStr = writeExcel::replace_row_col_ids($leftStr . $mStr . $rightStr, $row, $col, $mindatarow, $maxdatarow);
|
|
}
|
|
|
|
return $retStr;
|
|
}
|
|
|
|
|
|
function replace_date_codes($str, $cmpParms)
|
|
{
|
|
|
|
$retStr = $str;
|
|
$dateMode = Sql2excelParms::get($cmpParms,'datemode', 0);
|
|
|
|
// date() vs JDate / strtime() codes
|
|
$dcodes = array('d' => array('d','d'), // Day of month 01-31
|
|
'D' => array('D','a'), // Mon through Sun
|
|
'j' => array('j','d'), // Day of the month, 1-31
|
|
'l' => array('l','A'), // Sunday through Saturday
|
|
'N' => array('N','w'), // 1 (for Monday) through 7 (for Sunday)
|
|
'S' => array('S',''), // st, nd, rd or th. Works well with j
|
|
'w' => array('w','w'), // 0 (for Sunday) through 6 (for Saturday)
|
|
'z' => array('z','j'), // 0 through 365
|
|
'W' => array('W','W'), // Week number (not exactly same)
|
|
'F' => array('F','B'), // January through December
|
|
'm' => array('m','m'), // Month, 01 through 12
|
|
'M' => array('M','b'), // Jan through Dec
|
|
'n' => array('n','m'), // Month, 1 through 12
|
|
't' => array('t',''), // Number days in given month 28 to 31
|
|
'L' => array('L',''), // Whether it's a leap year (0 or 1)
|
|
'o' => array('o','G'), // ISO-8601 year number.
|
|
'Y' => array('Y','Y'), // Year YYYY
|
|
'y' => array('y','y'), // Year YY
|
|
'a' => array('a','p'), // am/pm
|
|
'A' => array('A','p'), // AM/PM
|
|
'B' => array('B',''), // Swatch Internet time 000-999
|
|
'g' => array('g','I'), // Hour 1 through 12
|
|
'G' => array('G','H'), // Hour 0 through 23
|
|
'h' => array('h','I'), // Hour 01 through 12
|
|
'H' => array('H','H'), // Hour 00 through 23
|
|
'i' => array('i','M'), // Minutes with leading zeros, 00 - 59
|
|
's' => array('s','S'), // Seconds, with leading zeros
|
|
'u' => array('u',''), // Microseconds
|
|
'e' => array('e',''), // Timezone abbreviation / offset (depends on OS in Joomla mode)
|
|
'I' => array('I',''), // daylight saving time (1 or 0 )
|
|
'O' => array('O',''), // Difference to Greenwich time (GMT) in hours (depends on OS in Joomla mode)
|
|
'P' => array('P',''), // Difference to Greenwich time (GMT) with colon between hours and minutes (added in PHP 5.1.3)
|
|
'T' => array('T',''), // Timezone abbreviation
|
|
'Z' => array('Z',''), // Timezone offset in seconds.
|
|
'c' => array('c',''), // ISO 8601 date (added in PHP 5)
|
|
'r' => array('r',''), // RFC 2822 formatted date
|
|
'U' => array('U','s') // Seconds since the Unix Epoch
|
|
);
|
|
|
|
global $mainframe;
|
|
$mdate = JFactory::getDate();
|
|
$mdate->setOffset($mainframe->getCfg('offset'));
|
|
|
|
foreach(array_keys($dcodes) as $key) {
|
|
$code = $dcodes{$key}[$dateMode];
|
|
$search = '{' . $key . '}';
|
|
if ( $dateMode == 1 ) {
|
|
$replace = $mdate->toFormat('%'.$code);
|
|
if ( $key == 'z' ) { $replace--; }
|
|
if ( ( $key == 'j' || $key == 'n' || $key == 'G' || $key == 'g' ) && substr($replace,0,1) == '0' ) { $replace = substr($replace,1); }
|
|
if ( $key == 'N' && $replace == 0 ) { $replace = 7; }
|
|
if ( $key == 'a' ) { $replace = strtolower($replace); }
|
|
if ( $key == 'U' ) { $replace = time(); }
|
|
if ( $key == 't' ) { $replace = writeExcel::days_in_month($mdate->toFormat('%m'), $mdate->toFormat('%Y')); }
|
|
if ( $key == 'L' ) { $replace = 0; if ( writeExcel::isleapyear($mdate->toFormat('%Y'))) { $replace = 1; } }
|
|
if ( $key == 'I' ) { $replace = date('I'); }
|
|
} else {
|
|
$replace = date($code);
|
|
}
|
|
|
|
$retStr = str_replace($search,$replace,$retStr);
|
|
}
|
|
|
|
return $retStr;
|
|
}
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
// Connect to database
|
|
// Returns connection handle if successful, null if error
|
|
//////////////////////////////////////////////////////////////
|
|
function getDB($ws) {
|
|
|
|
$querydb = null;
|
|
|
|
$databaseType = $ws->db_type;
|
|
$host = $ws->db_host;
|
|
$username = $ws->db_username;
|
|
$password = $ws->db_password;
|
|
$database = $ws->db_database;
|
|
$prefix = $ws->db_prefix;
|
|
|
|
if ( $databaseType == 2 ) {
|
|
// External MySQL Database
|
|
$option = array ();
|
|
$option ['driver'] = 'mysql';
|
|
$option ['host'] = $host ;
|
|
$option ['user'] = $username;
|
|
$option ['password'] = $password;
|
|
$option ['database'] = trim($database);
|
|
$option ['prefix'] = $prefix;
|
|
$querydb = JFactory::getDBO ();
|
|
$querydb = & JDatabase::getInstance ($option);
|
|
if ( get_class($querydb) != 'JDatabaseMySQL' ) {
|
|
$querydb = null;
|
|
}
|
|
}
|
|
elseif ( $databaseType == 3 ) {
|
|
// Oracle
|
|
$querydb = oci_connect($username, $password, $host);
|
|
}
|
|
elseif ( $databaseType == 4 ) {
|
|
// MSSQL
|
|
$querydb = mssql_connect($host, $username, $password);
|
|
if ( $database != '' ) {
|
|
if ( strpos($database,'.') > 0 || strpos($database,'_') > 0 ) {
|
|
$database = '['.$database.']';
|
|
}
|
|
mssql_select_db($database,$querydb);
|
|
}
|
|
}
|
|
elseif ( $databaseType == 5 ) {
|
|
// postgreSQL
|
|
$connectStr = "host=" . $host . " port=" . $prefix . " dbname=" . $database . " user=" . $username . " password=" . $password;
|
|
$querydb = @pg_connect($connectStr);
|
|
}
|
|
elseif ( $databaseType == 6 ) {
|
|
// Custom Driver
|
|
if ( file_exists($ws->db_prefix) ) {
|
|
require_once($ws->db_prefix);
|
|
if ( method_exists('sql2excel_customDB','connect') ) {
|
|
$querydb = sql2excel_customDB::connect($host, $database , $username, $password);
|
|
}
|
|
}
|
|
} else {
|
|
// Joomla! Default Database
|
|
$querydb = & JFactory::getDBO();
|
|
}
|
|
|
|
return $querydb;
|
|
}
|
|
|
|
|
|
function getResults($querydb, $query, $cmpParms, $databaseType, $returnResults=1) {
|
|
|
|
// Check for bad words in the SQL?
|
|
$badWordsMsg = '';
|
|
if ( Sql2excelParms::get($cmpParms,'badwordssql', '1') ) {
|
|
$badWordsMsg = writeExcel::check_sql_bad_words($cmpParms, $query);
|
|
}
|
|
|
|
if ( $badWordsMsg == '' ) {
|
|
|
|
// Parse the query for multiple statements
|
|
// Make sure we have only \n for line breaks
|
|
$queryStr = writeExcel::removeCR($query,2);
|
|
|
|
// Split statements that end with ; and a new line
|
|
$queryTokens = explode(";\n",$query);
|
|
$errMsg = '';
|
|
$result = true;
|
|
|
|
if ( $databaseType <= 2 ) {
|
|
// Joomla DB or External MySQL DB
|
|
|
|
// Execute all statements (if more than one)
|
|
foreach ( $queryTokens as $queryStmt ) {
|
|
if ( is_string($queryStmt) && strlen( $queryStmt) > 0 ) {
|
|
$querydb->setQuery( $queryStmt );
|
|
$ret = $querydb->query();
|
|
if ( !$ret ) {
|
|
$result = false;
|
|
} else {
|
|
$errMsg .= $querydb->getErrorMsg() . ' ';
|
|
}
|
|
}
|
|
}
|
|
|
|
// Return results from last statement
|
|
if ( !$returnResults ) {
|
|
if ( $result ) {
|
|
return true;
|
|
} else {
|
|
return $errMsg;
|
|
}
|
|
} else {
|
|
$rows = $querydb->loadObjectList();
|
|
if ( !$rows ) {
|
|
if ( is_null($rows) ) {
|
|
return $querydb->getErrorMsg();
|
|
} else {
|
|
return ''; // No results returned
|
|
}
|
|
} else {
|
|
return $rows;
|
|
}
|
|
}
|
|
}
|
|
elseif ( $databaseType == 3 ) {
|
|
// Oracle
|
|
$stmt = @oci_parse($querydb, $query);
|
|
if (!$stmt) {
|
|
$e = oci_error($querydb);
|
|
return $e['message'];
|
|
}
|
|
$res = oci_execute($stmt);
|
|
if (!$res) {
|
|
$e = oci_error($stmt);
|
|
return $e['message'] . ' | ' . $e['sqltext'];
|
|
|
|
} else {
|
|
if ( !$returnResults ) {
|
|
return true;
|
|
} else {
|
|
$nrows = oci_fetch_all($stmt, $rows, 0, -1, OCI_FETCHSTATEMENT_BY_ROW);
|
|
oci_free_statement($stmt);
|
|
oci_close($querydb);
|
|
|
|
if ( $nrows == 0 ) {
|
|
return ''; // No results returned
|
|
} else {
|
|
// Convert to an array of StdClass Obj's
|
|
$i=0;
|
|
foreach ( $rows as $row ) {
|
|
$rows[$i]=(object)$row;
|
|
$i++;
|
|
}
|
|
return $rows;
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
elseif ( $databaseType == 4 ) {
|
|
// MSSQL
|
|
$conn = @mssql_query($query);
|
|
if ( $conn ) {
|
|
if ( !$returnResults ) {
|
|
return true;
|
|
} else {
|
|
if(!mssql_num_rows($conn)) {
|
|
return ''; // No results returned
|
|
}
|
|
else {
|
|
$rows = array();
|
|
$i=0;
|
|
while($row = mssql_fetch_assoc($conn)) {
|
|
$rows[$i]=(object)$row;
|
|
$i++;
|
|
}
|
|
return $rows;
|
|
}
|
|
}
|
|
} else {
|
|
return JText::_( 'SQL Error') . ' : ' . mssql_get_last_message();
|
|
}
|
|
}
|
|
elseif ( $databaseType == 5 ) {
|
|
// postgreSQL
|
|
$res = @pg_query($querydb, $query);
|
|
if (!$res) {
|
|
return pg_last_error($querydb); // An error occured
|
|
} else {
|
|
if ( !$returnResults ) {
|
|
return true;
|
|
} else {
|
|
$rows = array();
|
|
$i=0;
|
|
while ($row = pg_fetch_object($res)) {
|
|
$rows[$i] = $row;
|
|
$i++;
|
|
}
|
|
return $rows;
|
|
}
|
|
}
|
|
}
|
|
elseif ( $databaseType == 6 ) {
|
|
if ( method_exists('sql2excel_customDB','query') ) {
|
|
return sql2excel_customDB::query($querydb, $query);
|
|
} else {
|
|
return JText::_( 'ERROR: Could not locate custom query function!');
|
|
}
|
|
}
|
|
} else {
|
|
return $badWordsMsg;
|
|
}
|
|
}
|
|
|
|
function getColumnNames($rows)
|
|
{
|
|
$firstRow = $rows[0];
|
|
if ( is_object($firstRow) ) {
|
|
$colNames = array_keys(get_object_vars($firstRow));
|
|
} else {
|
|
$colNames = array();
|
|
}
|
|
return $colNames;
|
|
}
|
|
|
|
|
|
// Joom!Fish adds columns to query.
|
|
//
|
|
// This function gets the column count from the original query string
|
|
function getColumnCount($colNames, $query)
|
|
{
|
|
$query = trim(strtoupper($query));
|
|
$query = str_replace("\n", ' ', $query);
|
|
|
|
$query = writeExcel::remove_quote($query,'`');
|
|
$query = writeExcel::remove_quote($query,"'");
|
|
$query = writeExcel::remove_quote($query,'"');
|
|
|
|
|
|
// Parentesis
|
|
$startP=0;
|
|
$retStr = '';
|
|
for ( $i=0; $i<strlen($query);$i++) {
|
|
$char = substr($query,$i,1);
|
|
if ( $char == "(" ) {
|
|
$startP++;
|
|
} elseif ( $char == ")" ) {
|
|
$startP--;
|
|
$char = 'Y';
|
|
}
|
|
|
|
if ( $startP > 0 ) { $char = 'Y'; }
|
|
|
|
$retStr .= $char;
|
|
}
|
|
|
|
$pos = strpos($retStr, ' FROM ');
|
|
if ( $pos > 0 && ! strpos($retStr,'*') ) {
|
|
$retStr = substr($retStr,0,$pos);
|
|
$cols = explode(',', $retStr);
|
|
if ( count($cols) < count($colNames) ) {
|
|
return count($cols);
|
|
} else {
|
|
return count($colNames);
|
|
}
|
|
} else {
|
|
return count($colNames);
|
|
}
|
|
}
|
|
|
|
|
|
function remove_quote($query, $match) {
|
|
$found = false;
|
|
$retStr = '';
|
|
for ( $i=0; $i<strlen($query);$i++) {
|
|
$char = substr($query,$i,1);
|
|
if ( $found && $char != $match ) {
|
|
$char = 'X';
|
|
} elseif ( $found && $char == $match ) {
|
|
$found = false;
|
|
$char = 'X';
|
|
} elseif ( !$found && $char == $match ) {
|
|
$found = true;
|
|
$char = 'X';
|
|
}
|
|
|
|
$retStr .= $char;
|
|
}
|
|
return $retStr;
|
|
}
|
|
|
|
|
|
function convert_utf8($str) {
|
|
if ( $this->_writer_version == '2000_limited' ) {
|
|
$ret = $str;
|
|
if ( function_exists('iconv') ) {
|
|
$s = @iconv('UTF-8', 'cp1252//TRANSLIT', $str);
|
|
$ret = preg_replace("/([\xC2\xC4])([\x80-\xBF])/e", "chr(ord('\\1')<<6&0xC0|ord('\\2')&0x3F)", $s);
|
|
} elseif (function_exists('mb_detect_encoding') && function_exists('utf8_decode') && mb_detect_encoding($str) == 'UTF-8' ) {
|
|
$ret = utf8_decode($str);
|
|
}
|
|
return $ret;
|
|
} else {
|
|
return $str;
|
|
}
|
|
}
|
|
|
|
|
|
function ExcelHeader($fileName) {
|
|
header("Content-type: application/vnd.ms-excel");
|
|
header("Content-Disposition: attachment; filename=\"$fileName\"" );
|
|
header("Expires: 0");
|
|
header("Cache-Control: must-revalidate, post-check=0,pre-check=0");
|
|
header("Pragma: public");
|
|
}
|
|
|
|
function ZipHeader($fileName, $size) {
|
|
header("Content-type: application/octet-stream");
|
|
header("Content-Disposition: attachment; filename=\"" . $fileName . "\"");
|
|
header("Content-length: " . $size . "\n\n");
|
|
}
|
|
|
|
function get_cache_filename($wbParms, $wsParms) {
|
|
$subFolder = $wbParms->id;
|
|
|
|
foreach ($wsParms as $ws ) {
|
|
$subFolder .= '_' . $ws->ws_id;
|
|
}
|
|
return $subFolder;
|
|
}
|
|
|
|
// Which Excel Writer Version to use
|
|
function get_writer($wbParms,$cmpParms)
|
|
{
|
|
$writerVersion = Sql2excelParms::get($cmpParms,'excelwriter', 'auto');
|
|
if ( $writerVersion == 'auto' ) {
|
|
if ( function_exists('iconv') && function_exists('mb_strlen') ) {
|
|
$writerVersion = '2000_utf8';
|
|
} else {
|
|
$writerVersion = '2000_limited';
|
|
}
|
|
}
|
|
$this->_writer_version = $writerVersion;
|
|
return $writerVersion;
|
|
}
|
|
|
|
|
|
function get_cache_dir($cmpParms)
|
|
{
|
|
$cacheDir = Sql2excelParms::get($cmpParms,'cache_dir', JPATH_SITE.DS.'components'.DS.'com_sql2excel'.DS.'cache'.DS);
|
|
if ( substr($cacheDir,strlen($cacheDir)-1,1) != DS ) {
|
|
$cacheDir .= DS;
|
|
}
|
|
return $cacheDir;
|
|
}
|
|
|
|
function get_formats($cellformat, $cmpParms)
|
|
{
|
|
$wsformats = array();
|
|
if ( trim($cellformat) != '' ) {
|
|
$wsFormats = explode("\n",$cellformat);
|
|
foreach ( $wsFormats as $format ) {
|
|
$currFormat = new stdClass();
|
|
$formatInfo = explode(',',$format);
|
|
if ( count($formatInfo) >= 5 ) {
|
|
$currFormat->row1 = $formatInfo[0];
|
|
$currFormat->col1 = $formatInfo[1];
|
|
$currFormat->row2 = $formatInfo[2];
|
|
$currFormat->col2 = $formatInfo[3];
|
|
$currFormat->textcol = $formatInfo[4];
|
|
$currFormat->font = $formatInfo[5];
|
|
$currFormat->fontsize = $formatInfo[6];
|
|
$currFormat->fontweight = $formatInfo[7];
|
|
$currFormat->fontalign = $formatInfo[8];
|
|
$currFormat->bgcol = $formatInfo[9];
|
|
$currFormat->numformat = $formatInfo[10];
|
|
$currFormat->border = $formatInfo[11];
|
|
$currFormat->bordercol = $formatInfo[12];
|
|
if ( isset($formatInfo[13]) ) { $currFormat->cond = $formatInfo[13]; } else $currFormat->cond = '';
|
|
if ( isset($formatInfo[14]) ) { $currFormat->condval = str_replace('@~@',',',strrev($formatInfo[14])); } else $currFormat->condval = '';
|
|
if ( isset($formatInfo[15]) ) { $currFormat->conddb = $formatInfo[15]; } else $currFormat->conddb = 1;
|
|
if ( isset($formatInfo[16]) ) { $currFormat->fontalignv = $formatInfo[16]; } else $currFormat->fontalignv = '';
|
|
if ( isset($formatInfo[17]) ) { $currFormat->wrap = $formatInfo[17]; } else $currFormat->wrap = '';
|
|
|
|
|
|
// SQL based condition value ?
|
|
if ( $currFormat->cond > 100 ) {
|
|
if ( $currFormat->conddb > 1 ) {
|
|
// External DB
|
|
$jdb = & JFactory::getDBO();
|
|
// Get DB info
|
|
$jdb->setQuery('SELECT * FROM #__sql2excel_databases WHERE id=' . $currFormat->conddb);
|
|
$dbInfo = $jdb->loadObject();
|
|
// Get DB connection
|
|
$fdb = writeExcel::getDB($dbInfo);
|
|
$dbType = $dbInfo->db_type;
|
|
} else {
|
|
$fdb = & JFactory::getDBO();
|
|
$dbType = 1;
|
|
}
|
|
|
|
if ( $fdb ) {
|
|
$rows = writeExcel::getResults($fdb, $currFormat->condval, $cmpParms, $dbType);
|
|
if ( is_array($rows) && is_object($rows[0]) ) {
|
|
$colNames = writeExcel::getColumnNames($rows);
|
|
$firstRow = $rows[0];
|
|
$currFormat->condval = $firstRow->$colNames[0];
|
|
} elseif ( $rows = '' ) {
|
|
$currFormat->condval = '';
|
|
} else {
|
|
$currFormat->condval = $rows;
|
|
}
|
|
} else {
|
|
$currFormat->condval = JText::_('DB_CONNECT_ERROR');
|
|
}
|
|
}
|
|
$wsformats[] = $currFormat;
|
|
}
|
|
}
|
|
}
|
|
|
|
return $wsformats;
|
|
}
|
|
|
|
function get_cellformat($formats, $row, $col, $mindatarow=1, $maxdatarow=1, $cellData = '') {
|
|
if ( count($formats) > 0 ) {
|
|
$cellFormat = new stdClass();
|
|
$cellFormat->row1 = '';
|
|
$cellFormat->col1 = '';
|
|
$cellFormat->row2 = '';
|
|
$cellFormat->col2 = '';
|
|
$cellFormat->textcol = '';
|
|
$cellFormat->font = '';
|
|
$cellFormat->fontsize = '';
|
|
$cellFormat->fontweight = '';
|
|
$cellFormat->fontalign = '';
|
|
$cellFormat->bgcol = '';
|
|
$cellFormat->numformat = '';
|
|
$cellFormat->border = '';
|
|
$cellFormat->bordercol = '';
|
|
$cellFormat->cond = '';
|
|
$cellFormat->condval = '';
|
|
$cellFormat->conddb = 1;
|
|
$cellFormat->fontalignv = '';
|
|
$cellFormat->wrap = '';
|
|
|
|
foreach ( $formats as $format ) {
|
|
$row1 = $format->row1;
|
|
$row2 = $format->row2;
|
|
if ( strpos($row1,'}') > 0 ) { $row1 = trim(writeExcel::replace_row_col_ids(' '.trim($format->row1), $row, $col, $mindatarow, $maxdatarow)); }
|
|
if ( strpos($row2,'}') > 0 ) { $row2 = trim(writeExcel::replace_row_col_ids(' '.trim($format->row2), $row, $col, $mindatarow, $maxdatarow)); }
|
|
if ( $row1 <= $row+1 && $format->col1 <= $col+1 && $row2 >= $row+1 && $format->col2 >= $col+1 ) {
|
|
$cellFormat = writeExcel::append_format($cellFormat,$format, $cellData, $row, $col);
|
|
}
|
|
}
|
|
return $cellFormat;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
function append_format($oldformat, $newformat, $cellData, $row, $col) {
|
|
$applyFormat = 1;
|
|
if ( $newformat->cond != '' ) {
|
|
$applyFormat = 0;
|
|
$cond = $newformat->cond;
|
|
if ( $cond > 100 ) { $cond = $cond - 20; }
|
|
switch ($cond) {
|
|
case 1:
|
|
// Even Rows
|
|
if ( $row %2 ) { $applyFormat = 1; }
|
|
break;
|
|
case 2:
|
|
// Odd Rows
|
|
if ( !($row %2) ) { $applyFormat = 1; }
|
|
break;
|
|
case 3:
|
|
// Even Cols
|
|
if ( $col %2 ) { $applyFormat = 1; }
|
|
break;
|
|
case 4:
|
|
// Odd Cols
|
|
if ( ! ($col %2) ) { $applyFormat = 1; }
|
|
break;
|
|
case 5:
|
|
// String
|
|
if ( is_string($cellData) && !is_numeric($cellData) ) { $applyFormat = 1; }
|
|
break;
|
|
case 6:
|
|
// ! String
|
|
if ( is_numeric($cellData) ) { $applyFormat = 1; }
|
|
break;
|
|
case 7:
|
|
// Numeric
|
|
if ( is_numeric($cellData) ) { $applyFormat = 1; }
|
|
break;
|
|
case 8:
|
|
// ! Numeric
|
|
if ( !is_numeric($cellData) ) { $applyFormat = 1; }
|
|
break;
|
|
case 9:
|
|
// Integer
|
|
if ( is_numeric($cellData) && (string)(int) $cellData == $cellData ) { $applyFormat = 1; }
|
|
break;
|
|
case 10:
|
|
// !Integer
|
|
if ( !(is_numeric($cellData) && (string)(int) $cellData == $cellData) ) { $applyFormat = 1; }
|
|
break;
|
|
case 11:
|
|
// Empty
|
|
if ( $cellData == '' ) { $applyFormat = 1; }
|
|
break;
|
|
case 12:
|
|
// !Empty
|
|
if ( strlen($cellData) > 0 ) { $applyFormat = 1; }
|
|
break;
|
|
case 81:
|
|
// =
|
|
if ( $cellData == $newformat->condval ) { $applyFormat = 1; }
|
|
break;
|
|
case 82:
|
|
// >=
|
|
if ( $cellData >= $newformat->condval ) { $applyFormat = 1; }
|
|
break;
|
|
case 83:
|
|
// >
|
|
if ( $cellData > $newformat->condval ) { $applyFormat = 1; }
|
|
break;
|
|
case 84:
|
|
// <=
|
|
if ( $cellData <= $newformat->condval ) { $applyFormat = 1; }
|
|
break;
|
|
case 85:
|
|
// <
|
|
if ( $cellData < $newformat->condval ) { $applyFormat = 1; }
|
|
break;
|
|
case 86:
|
|
// <>
|
|
if ( $cellData <> $newformat->condval ) { $applyFormat = 1; }
|
|
break;
|
|
default:
|
|
$applyFormat = 0;
|
|
}
|
|
}
|
|
|
|
if ( $applyFormat ) {
|
|
if ( $newformat->textcol != '' ) { $oldformat->textcol = $newformat->textcol; }
|
|
if ( $newformat->font != '' ) { $oldformat->font = $newformat->font; }
|
|
if ( $newformat->fontsize != '' ) { $oldformat->fontsize = $newformat->fontsize; }
|
|
if ( $newformat->fontweight != '' ) { $oldformat->fontweight = $newformat->fontweight; }
|
|
if ( $newformat->fontalign != '' ) { $oldformat->fontalign = $newformat->fontalign; }
|
|
if ( $newformat->bgcol != '' ) { $oldformat->bgcol = $newformat->bgcol; }
|
|
if ( $newformat->numformat != '' ) { $oldformat->numformat = $newformat->numformat; }
|
|
if ( $newformat->border != '' ) { $oldformat->border = $newformat->border; }
|
|
if ( $newformat->bordercol != '' ) { $oldformat->bordercol = $newformat->bordercol; }
|
|
if ( $newformat->fontalignv != '' ) { $oldformat->fontalignv = $newformat->fontalignv; }
|
|
if ( $newformat->wrap != '' ) { $oldformat->wrap = $newformat->wrap; }
|
|
}
|
|
return $oldformat;
|
|
}
|
|
|
|
|
|
function &addFormat(&$workbook, $fontFamily='Arial', $fontSize=0, $fontWeight='normal', $fontCol='none', $fontBgCol='none', $fontAlign='', $cellFormat=0) {
|
|
|
|
$numFormat = '';
|
|
$fontAlignV = '';
|
|
$wrap = '';
|
|
$wordwrap = '';
|
|
$border = '';
|
|
|
|
if ( is_object($cellFormat) ) {
|
|
// Custom format defined for this cell
|
|
|
|
if ( isset($cellFormat->wordwrap) && trim($cellFormat->wordwrap) == '1' ) {
|
|
$wordwrap == '1';
|
|
}
|
|
|
|
if ( isset($cellFormat->textcol) && $cellFormat->textcol != '' ) {
|
|
$fontCol = $cellFormat->textcol;
|
|
}
|
|
if ( isset($cellFormat->font) && $cellFormat->font != '' ) {
|
|
$fontFamily = $cellFormat->font;
|
|
}
|
|
if ( isset($cellFormat->fontsize) && $cellFormat->fontsize != '' ) {
|
|
$fontSize = $cellFormat->fontsize;
|
|
}
|
|
if ( isset($cellFormat->fontweight) && $cellFormat->fontweight != '' ) {
|
|
$fontWeight = $cellFormat->fontweight;
|
|
}
|
|
if ( isset($cellFormat->fontalign) && $cellFormat->fontalign != '' ) {
|
|
$fontAlign = $cellFormat->fontalign;
|
|
}
|
|
if ( isset($cellFormat->bgcol) && trim($cellFormat->bgcol) != '' ) {
|
|
$fontBgCol = trim($cellFormat->bgcol);
|
|
}
|
|
if ( isset($cellFormat->numformat) && trim($cellFormat->numformat) != '' ) {
|
|
$numFormat = str_replace('@~@',',',trim($cellFormat->numformat));
|
|
}
|
|
if ( isset($cellFormat->border) && $cellFormat->border != '' ) {
|
|
$border = $cellFormat->border;
|
|
}
|
|
if ( isset($cellFormat->fontalignv) && trim($cellFormat->fontalignv) != '' ) {
|
|
$fontAlignV = trim($cellFormat->fontalignv);
|
|
}
|
|
if ( isset($cellFormat->wrap) && trim($cellFormat->wrap) != '' ) {
|
|
$wrap = trim($cellFormat->wrap);
|
|
}
|
|
}
|
|
|
|
// Check and see if we have defined a format like this already
|
|
foreach ( $this->wbFormatsArr as $cFormat ) {
|
|
if ( $cFormat->fontFamily == $fontFamily &&
|
|
$cFormat->fontSize == $fontSize &&
|
|
$cFormat->fontWeight == $fontWeight &&
|
|
$cFormat->fontCol == $fontCol &&
|
|
$cFormat->fontBgCol == $fontBgCol &&
|
|
$cFormat->fontAlign == $fontAlign &&
|
|
$cFormat->numFormat == $numFormat &&
|
|
$cFormat->border == $border &&
|
|
$cFormat->fontAlignV == $fontAlignV &&
|
|
$cFormat->wrap == $wrap
|
|
) {
|
|
return $cFormat->formatH;
|
|
}
|
|
}
|
|
|
|
// Add new format
|
|
$myformat =& $workbook->addFormat();
|
|
if ( $fontFamily != 'Arial' ) {
|
|
$myformat->setFontFamily($fontFamily);
|
|
}
|
|
$myformat->setSize($fontSize);
|
|
if ( $fontWeight == 'bold' ) {
|
|
$myformat->setBold();
|
|
}
|
|
if ( $fontCol != 'none' && $fontCol != '' ) {
|
|
$myformat->setColor($fontCol);
|
|
}
|
|
if ( $fontBgCol != 'none' && $fontBgCol != '' ) {
|
|
$myformat->setPattern();
|
|
$myformat->setFgColor($fontBgCol);
|
|
}
|
|
if ( $fontAlign != '' ) {
|
|
$myformat->setAlign($fontAlign);
|
|
}
|
|
if ( $numFormat != '' ) {
|
|
$myformat->setNumFormat($numFormat);
|
|
}
|
|
|
|
if ( $border != '' ) {
|
|
|
|
$border_thickness = substr($cellFormat->border,1,1);
|
|
|
|
if ( $cellFormat->border == 'B11111' || $cellFormat->border == 'B21111' ) {
|
|
$myformat->setBorder($border_thickness);
|
|
} else {
|
|
$borderLeft = substr($cellFormat->border,2,1);
|
|
$borderTop = substr($cellFormat->border,3,1);
|
|
$borderBottom = substr($cellFormat->border,4,1);
|
|
$borderRight = substr($cellFormat->border,5,1);
|
|
if ( $borderLeft ) { $myformat->setLeft($border_thickness); }
|
|
if ( $borderTop ) { $myformat->setTop($border_thickness); }
|
|
if ( $borderBottom ) { $myformat->setBottom($border_thickness); }
|
|
if ( $borderRight ) { $myformat->setRight($border_thickness); }
|
|
}
|
|
|
|
if ( isset($cellFormat->bordercol) ) {
|
|
$borderCol = trim($cellFormat->bordercol);
|
|
if ( $borderCol != '' && $borderCol != 'black' ) {
|
|
$myformat->setBorderColor($borderCol);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
if ( $fontAlignV != '' ) {
|
|
$myformat->setVAlign($fontAlignV);
|
|
}
|
|
if ( $wrap == '1' ) {
|
|
$myformat->setTextWrap();
|
|
}
|
|
|
|
// Save format for later
|
|
$currFormat = new StdClass();
|
|
$currFormat->formatH = $myformat;
|
|
$currFormat->fontFamily = $fontFamily;
|
|
$currFormat->fontSize = $fontSize;
|
|
$currFormat->fontWeight = $fontWeight;
|
|
$currFormat->fontCol = $fontCol;
|
|
$currFormat->fontBgCol = $fontBgCol;
|
|
$currFormat->fontAlign = $fontAlign;
|
|
$currFormat->numFormat = $numFormat;
|
|
$currFormat->border = $border;
|
|
$currFormat->fontAlignV = $fontAlignV;
|
|
$currFormat->wrap = $wrap;
|
|
$this->wbFormatsArr[] = $currFormat;
|
|
|
|
// Return format handle
|
|
return $myformat;
|
|
|
|
}
|
|
|
|
|
|
function is_url($str)
|
|
{
|
|
if ( substr($str,0,7) == 'http://' )
|
|
return true;
|
|
else
|
|
return false;
|
|
}
|
|
|
|
function insertImage($worksheet1, $row, $col, $fn)
|
|
{
|
|
if ( file_exists($fn) ) {
|
|
$worksheet1->insertBitmap($row, $col, $fn, 0, 0, 1, 1);
|
|
} else {
|
|
writeExcel::writeCell($worksheet1, $row, $col, JText::_( 'ERROR: Could not open bitmap file - ' . $fn));
|
|
}
|
|
}
|
|
|
|
// Copy cache file to ./scheduler subfolder for email attachment
|
|
function copyCacheFile($cacheFN, $fileName, $cacheDir)
|
|
{
|
|
$toFN = $cacheDir.'scheduler'.DS.$fileName;
|
|
copy($cacheFN, $toFN);
|
|
}
|
|
|
|
|
|
// calculate number of days in a month
|
|
function days_in_month($month, $year) {
|
|
return $month == 2 ? ($year % 4 ? 28 : ($year % 100 ? 29 : ($year % 400 ? 28 : 29))) : (($month - 1) % 7 % 2 ? 30 : 31);
|
|
}
|
|
|
|
// Is Leap year?
|
|
function isleapyear($year = '') {
|
|
if (empty($year)) {
|
|
$year = date('Y');
|
|
}
|
|
$year = (int) $year;
|
|
if ($year % 4 == 0) {
|
|
if ($year % 100 == 0) {
|
|
return ($year % 400 == 0);
|
|
} else {
|
|
return true;
|
|
}
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
function checkSheetNames($wsParms, $cmpParms, $substParms)
|
|
{
|
|
|
|
$sheetNames = array();
|
|
$i=0;
|
|
$str = '';
|
|
foreach ($wsParms as $ws) {
|
|
$sheetNames[$i] = strtolower(trim($ws->sheetname));
|
|
$str .= '<li>' . $ws->ws_id . ' - ' . $ws->sheetname . '</li>';
|
|
$i++;
|
|
}
|
|
|
|
$uniqueNames = array_unique($sheetNames);
|
|
|
|
if ( count($sheetNames) != count($uniqueNames) ) {
|
|
JError::raiseError(500, '<h1>' . JText::_('Non unique Sheet Names!') . '</h1><p>' . JText::_('The Worksheet Sheet Names does not have unique names.<br>Two more more Worksheets have the same Sheet Name.') . '</p><p>' . JText::_('Please rename the Sheet Names on the Worksheets so that they become unique.') . '</p><ul>' . $str . '</ul>' );
|
|
exit;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
function insertextraSQL ( $worksheet1, $row, $col, $query, $dbID, $formatH, $maxExtraRow, $db, $cmpParms, $formulas, $substSQL, $substParms ) {
|
|
|
|
$currRow = $row;
|
|
$cellDB = $db;
|
|
$dbType = 1;
|
|
$dbInfo = 0;
|
|
|
|
if ( $dbID > 1 ) {
|
|
// Get DB connection
|
|
$db->setQuery('SELECT * FROM #__sql2excel_databases WHERE id=' . $dbID);
|
|
$dbInfo = $db->loadObject();
|
|
$dbType = $dbInfo->db_type;
|
|
$cellDB = writeExcel::getDB($dbInfo);
|
|
}
|
|
|
|
if ( $cellDB ) {
|
|
|
|
|
|
if ( $substSQL ) {
|
|
$query = writeExcel::replace_vars($query, $substParms,0,$cmpParms);
|
|
}
|
|
|
|
|
|
$rows = writeExcel::getResults($cellDB, $query, $cmpParms, $dbType);
|
|
|
|
if ( is_array($rows) && is_object($rows[0]) ) {
|
|
|
|
// Get number of columns
|
|
$colNames = writeExcel::getColumnNames($rows);
|
|
//Number of Columns in user query (exclude JoomFish fields if any)
|
|
$nrCols = writeExcel::getColumnCount($colNames, $query);
|
|
|
|
// Output the data
|
|
|
|
foreach ( $rows as $row ) {
|
|
for ( $i=0; $i<$nrCols; $i++ ) {
|
|
writeExcel::writeCell($worksheet1, $currRow, $col+$i, trim($row->$colNames[$i]), $formatH, $formulas);
|
|
}
|
|
$currRow++;
|
|
}
|
|
}
|
|
|
|
unset($cellDB);
|
|
|
|
// Was max extra row exceeded?
|
|
$currRow--;
|
|
if ( $currRow > $maxExtraRow ) {
|
|
return $currRow;
|
|
} else {
|
|
return $maxExtraRow;
|
|
}
|
|
|
|
} else {
|
|
if ( Sql2excelParms::get($cmpParms,'show_sql_errors', 1) ) {
|
|
writeExcel::writeCell($worksheet1, $row, $col, JText::_('DB_CONNECT_ERROR'), $formatH, array());
|
|
}
|
|
return $maxExtraRow;
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
?>
|