git-svn-id: https://192.168.0.254/svn/Proyectos.FundacionLQDVI_WebCongresos/trunk@2 94ccb1af-fd9d-d947-8d90-7f70ea60afc8
998 lines
33 KiB
PHP
998 lines
33 KiB
PHP
<?php
|
|
/*
|
|
* @component SQL 2 Excel Pro 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_COMPONENT.DS.'controllers'.DS.'download.php' );
|
|
require_once( JPATH_COMPONENT.DS.'helpers'.DS.'excel_writer.php' );
|
|
require_once( JPATH_COMPONENT.DS.'helpers'.DS.'common.php' );
|
|
require_once( JPATH_COMPONENT.DS.'helpers'.DS.'parms.php' );
|
|
|
|
|
|
class Sql2excelControllerPreview extends Sql2excelController
|
|
{
|
|
/**
|
|
* constructor (registers additional tasks to methods)
|
|
* @return void
|
|
*/
|
|
|
|
var $_wsArr;
|
|
var $_headArr;
|
|
var $_formulas;
|
|
|
|
function __construct()
|
|
{
|
|
parent::__construct();
|
|
|
|
// Register Extra tasks
|
|
$this->registerTask( 'ws' , 'previewWorksheet' );
|
|
$this->registerTask( 'popup' , 'previewPopup' );
|
|
|
|
$_wsArr = array();
|
|
$_formulas = false;
|
|
}
|
|
|
|
|
|
// Build popup preview page
|
|
function previewPopup()
|
|
{
|
|
global $mainframe;
|
|
$wb_id = Sql2excelParms::get_parm_value('id', 'int', 0);
|
|
|
|
$wbParms = Sql2excelControllerDownload::getWorkbookParms($wb_id,1);
|
|
$cmpparams = Sql2excelParms::getParms();
|
|
|
|
$wbRefresh = '';
|
|
|
|
if ( count($wbParms) ) {
|
|
|
|
echo "<html>\n";
|
|
echo "<head>\n";
|
|
echo '<base href="' . JURI::base() ."\" />\n";
|
|
echo "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />\n";
|
|
echo '<link rel="stylesheet" href="' . JURI::base() . "templates/system/css/system.css\" type=\"text/css\" />\n";
|
|
echo '<link rel="stylesheet" href="' . JURI::base() . "templates/system/css/general.css\" type=\"text/css\" />\n";
|
|
$jaCSS = JPATH_SITE.DS.'templates'.DS.'ja_purity'.DS.'css'.DS.'template.css';
|
|
if ( file_exists($jaCSS) ) {
|
|
echo '<link rel="stylesheet" href="' . JURI::base() . "templates/ja_purity/css/template.css\" type=\"text/css\" />\n";
|
|
}
|
|
|
|
echo '<link rel="stylesheet" href="' . JURI::base() . "components/com_sql2excel/assets/css/preview.css\" type=\"text/css\" />\n";
|
|
echo '<script type="text/javascript" src="' . JURI::base() . "components/com_sql2excel/assets/js/cba2.js\"></script>\n";
|
|
echo '<script type="text/javascript" src="' . JURI::base() . "components/com_sql2excel/assets/js/preview.js\"></script>\n";
|
|
echo '</head>';
|
|
echo '<body>';
|
|
|
|
$wbParms = $wbParms[0];
|
|
|
|
echo '<h3>' . $wbParms->link_title . '</h3>';
|
|
|
|
|
|
// User Parameters
|
|
$wbParmsHTML = '';
|
|
$wbParmVars = '';
|
|
$wbParmVarsAdd = '';
|
|
$wbParmvalidate = '';
|
|
$wbHiddenParms = '';
|
|
$nrWbParms = 0;
|
|
$wbValidate = '';
|
|
$wbMultiTicks = '';
|
|
|
|
$wbParameters = Sql2excelCommon::getWorkbookParms($wb_id);
|
|
|
|
if ( is_array($wbParameters) ) {
|
|
$wbParmsHTML = $wbParameters[0];
|
|
$wbParmVars = $wbParameters[1];
|
|
$wbParmVarsAdd = $wbParameters[2];
|
|
$wbHiddenParms = $wbParameters[3];
|
|
$nrWbParms = $wbParameters[4];
|
|
$wbRefresh = $wbParameters[5];
|
|
$wbValidate = $wbParameters[6];
|
|
$wbMultiTicks = $wbParameters[7];
|
|
}
|
|
echo $wbParmsHTML;
|
|
|
|
|
|
$wsParms = Sql2excelControllerDownload::getWorksSheetParms($wb_id);
|
|
$nrWorksheets = count($wsParms);
|
|
|
|
echo '<div id="ajaxtabs-container"></div>';
|
|
echo '<table class="ajax-tabs">';
|
|
echo '<tr class="ajax-tabs-top">';
|
|
$i=1;
|
|
|
|
foreach ($wsParms as $ws ) {
|
|
if ( $i == 1 ) {
|
|
$id = ' id="sheet1" ';
|
|
$ws1ID = $ws->ws_id;
|
|
} else {
|
|
$id='';
|
|
}
|
|
echo '<td ' . $id . ' style="" onClick="UpdateTab(this,' . $ws->ws_id . ');" onMouseOver = "selTab(this,1);" onMouseOut = "selTab(this,0);"><b> ' . str_replace(' ', ' ', $ws->sheetname) . ' </b></td> ';
|
|
$i++;
|
|
}
|
|
|
|
echo '<td style="width:100%;"></td>';
|
|
echo '</tr>';
|
|
echo '<tr>';
|
|
$cspan = $nrWorksheets + 1;
|
|
echo '<td colspan="' . $cspan. '" id="ajax-tabs-content"></td>';
|
|
echo '</tr>';
|
|
echo '</table>';
|
|
echo "</div>\n";
|
|
echo "<script language=\"Javascript\">\n";
|
|
echo "// data load\n";
|
|
echo "var nrWBParms=" . $nrWbParms . ";\n";
|
|
echo "function UpdateTab( tab, request )\n";
|
|
echo "{\n";
|
|
echo "var checkMulti=0;\n";
|
|
echo "currTabH = tab;\n";
|
|
echo "currWS = request;\n";
|
|
echo $wbValidate . "\n";
|
|
echo "var id_name = 'ajax-tabs-content';\n";
|
|
echo "var loading = '<img style=\"position:relative;top:3px;\"' + ' src=\"" . JURI::base() . 'components/com_sql2excel/assets/images/loading_01.gif"> ' . JText::_( 'Loading...') . "';\n";
|
|
echo "var caching = true;\n";
|
|
|
|
echo "var template = '%code%';\n";
|
|
echo $wbParmVars;
|
|
echo "selTab( tab, 2 );\n";
|
|
|
|
echo "var poststr = \"option=com_sql2excel\" +\n";
|
|
echo " \"&controller=preview&task=ws\" + '" . $wbParmVarsAdd . '&wbid=' . $wb_id . "&id=' + request;\n";
|
|
echo "sql2excel_ajax('" . JURI::base() . "index.php',poststr, loading);\n";
|
|
|
|
echo "var sheetH = document.getElementById('sheet');\n";
|
|
echo "if ( sheetH) { sheetH.value=request;}\n";
|
|
echo "}\n";
|
|
echo "var tab1H = document.getElementById('sheet1');\n";
|
|
echo "UpdateTab(tab1H," . $ws1ID . ");\n";
|
|
echo "var currTabH=tab1H;\n";
|
|
echo "var currWS=" . $ws1ID . ";\n";
|
|
echo "function updateParms() {\n";
|
|
echo "var checkMulti=1;\n";
|
|
echo $wbValidate . "\n";
|
|
echo "for (i=1;i<=nrWBParms;i++) {\n";
|
|
echo "var parmH=document.getElementById('parm'+i);\n";
|
|
echo "var pVal = \"\";\n";
|
|
echo "if ( parmH.length==undefined ) {\n";
|
|
echo "pVal=parmH.value;\n";
|
|
echo "} else {\n";
|
|
echo "var ticks=0;\n";
|
|
echo $wbMultiTicks . "\n";
|
|
echo "pVal=formatSelected(getSelected(parmH),ticks);\n";
|
|
echo "}\n";
|
|
echo "var formParmH=document.getElementById('wbparm'+i);\n";
|
|
echo "formParmH.value=pVal;\n";
|
|
echo "}\n";
|
|
echo "return true;\n";
|
|
echo "}\n";
|
|
echo "</script>\n";
|
|
|
|
|
|
// Show Download Button?
|
|
$action = JText::_( 'Download');
|
|
$what = JText::_('Spreadsheet');
|
|
$sheet = 0;
|
|
$dlformat = 1;
|
|
if ( isset($wbParms->dlformat) ) {
|
|
$dlformat = $wbParms->dlformat;
|
|
}
|
|
|
|
if ( $dlformat == 3 || ( $dlformat == 1 && Sql2excelParms::get($cmpparams,'dlformat', '2') == 3 ) ) {
|
|
$what = JText::_('CSV');
|
|
$sheet = 1;
|
|
}
|
|
elseif ( $dlformat == 4 || ( $dlformat == 1 && Sql2excelParms::get($cmpparams,'dlformat', '2') == 4 ) ) {
|
|
$what = JText::_('');
|
|
$action = JText::_('View Full Report');
|
|
$sheet = 1;
|
|
}
|
|
|
|
|
|
// Show Download Button?
|
|
$wbDLbtn = $wbParms->preview_dl_btn;
|
|
if ( $wbDLbtn == 'Show' || ( $wbDLbtn == 'Global' && Sql2excelParms::get($cmpparams,'previewdlbutton', 1) == 1 ) ) {
|
|
$wbRefresh .= "<input type=\"submit\" value=\" " . $action . ' ' . $what . " \">\n";
|
|
}
|
|
|
|
echo "<p>\n";
|
|
echo "<form name=\"dlform\" type=\"post\" action=\"index.php\" onSubmit=\"return updateParms();\">\n";
|
|
echo "<center>\n";
|
|
echo $wbRefresh;
|
|
echo "</center>\n";
|
|
echo "<input type=\"hidden\" name=\"option\" value=\"com_sql2excel\">\n";
|
|
echo "<input type=\"hidden\" name=\"controller\" value=\"download\">\n";
|
|
echo "<input type=\"hidden\" name=\"task\" value=\"dl\">\n";
|
|
echo "<input type=\"hidden\" name=\"id\" value=\"" . $wb_id . "\">\n";
|
|
echo "<input type=\"hidden\" name=\"sheet\" id=\"sheet\" value=\"" .$ws1ID . "\">\n";
|
|
echo $wbHiddenParms;
|
|
echo "</form>\n";
|
|
echo "<br>\n";
|
|
echo "</p>\n";
|
|
|
|
echo '</body>';
|
|
echo '</html>';
|
|
|
|
} else {
|
|
JError::raiseError( 500, JText::_( 'Workbook not found' ) );
|
|
}
|
|
|
|
exit;
|
|
}
|
|
|
|
|
|
// Return Ajax data for a worksheet
|
|
function previewWorksheet()
|
|
{
|
|
$useCache = 1;
|
|
|
|
global $mainframe;
|
|
$id = Sql2excelParms::get_parm_value('wbid', 'int', 0);
|
|
$wb_id = (int) $id;
|
|
|
|
$id = Sql2excelParms::get_parm_value('id', 'int', 0);
|
|
$ws_id = (int) $id;
|
|
$cbaid = Sql2excelParms::get_parm_value('_cba_request_id', 'int', 0);
|
|
|
|
// Workbook Parameters
|
|
$wbParms = Sql2excelControllerDownload::getWorkbookParms($wb_id,1);
|
|
|
|
if ( count($wbParms) ) {
|
|
//$cmpParms = &$mainframe->getParams();
|
|
$cmpParms = Sql2excelParms::getParms();
|
|
$substParms = Sql2excelControllerDownload::getSubstParms();
|
|
$wsParms = Sql2excelControllerDownload::getWorksSheetParms($wb_id, $ws_id);
|
|
$extraParms = array();
|
|
$substWBParms = Sql2excelParms::get($cmpParms,'subst_wbparms', '1');
|
|
|
|
// Workbook Parameters (runtime parms) ?
|
|
$wbParameters = $wbParms[0]->parms;
|
|
if ( $wbParameters != '' ) {
|
|
$wbParametersArr = explode('~@~',$wbParameters);
|
|
$i=1;
|
|
foreach ( $wbParametersArr as $parm ) {
|
|
$parmTokens = explode(',',$parm);
|
|
if ( count($parmTokens) >= 4 ) {
|
|
$parmID = 'parm' . $i;
|
|
$parmName = $parmTokens[0];
|
|
$parmVar = $parmTokens[1];
|
|
$parmType = $parmTokens[2];
|
|
$parmValue = $parmTokens[3];
|
|
$parmDB = 1;
|
|
if ( isset($parmTokens[4]) ) { $parmDB = $parmTokens[4]; }
|
|
$parmVis = 1;
|
|
if ( isset($parmTokens[5]) ) { $parmVis = $parmTokens[5]; }
|
|
|
|
if ( $parmType == "Integer" || $parmType == "UnsignedInteger" ) {
|
|
$parmValue = Sql2excelParms::get_parm_value($parmID, 'int', $parmValue);
|
|
} elseif ( $parmType == "Real" || $parmType == "UnsignedRealReal") {
|
|
$parmValue = Sql2excelParms::get_parm_value($parmID, 'float', $parmValue);
|
|
} elseif ( $parmType == "AlphaNum" ) {
|
|
$parmValue = Sql2excelParms::get_parm_value($parmID, 'alnum', $parmValue);
|
|
} elseif ( $parmType == "Username" ) {
|
|
$parmValue = Sql2excelParms::get_parm_value($parmID, 'username', $parmValue);
|
|
} elseif ( $parmType == "List" || $parmType == "ListMultiNum" || $parmType == "ListMultiStr") {
|
|
$parmList = Sql2excelParms::get_parm_value($parmID, 'string', $parmValue);
|
|
if ( $wbParms[0]->parms_prompt == 0 ) {
|
|
if ( $parmList != '' ) {
|
|
$parmList = str_replace('~COMMA~',',',$parmList);
|
|
$parmList = explode('~NL~',$parmList);
|
|
$defaultValue = explode(',',$parmList[0]);
|
|
$defaultValue = $defaultValue[0];
|
|
$multiStr = '';
|
|
foreach ( $parmList as $lisval ) {
|
|
$tokens = explode(',',$lisval);
|
|
if ( sizeof($tokens) > 2 ) {
|
|
if ( strpos($parmType,'Multi') > 0 ) {
|
|
if ( $parmType == "ListMultiNum" ) {
|
|
$pVal = $tokens[0];
|
|
} else {
|
|
$pVal = "'" . $tokens[0] . "'";
|
|
}
|
|
if ( $multiStr == '' ) {
|
|
$multiStr = $pVal;
|
|
} else {
|
|
$multiStr .= ',' . $pVal;
|
|
}
|
|
} else {
|
|
$defaultValue = $tokens[0];
|
|
}
|
|
}
|
|
}
|
|
|
|
if ( strpos($parmType,'Multi') > 0 && $multiStr) {
|
|
$parmValue = $multiStr;
|
|
} else {
|
|
$parmValue = $defaultValue;
|
|
}
|
|
} else {
|
|
$parmValue = '';
|
|
}
|
|
} else {
|
|
$parmValue = $parmList;
|
|
}
|
|
} elseif ( $parmType == "ListSQL" || $parmType == 'ListSQLMultiNum' || $parmType == 'ListSQLMultiStr' ) {
|
|
$query = Sql2excelParms::get_parm_value($parmID, 'string', $parmValue);
|
|
if ( $wbParms[0]->parms_prompt == 0 ) {
|
|
// Parameter not prompted => getting SQL query passed
|
|
$query = str_replace('~COMMA~',',',$query);
|
|
$query = str_replace('~NL~',"\n",$query);
|
|
$query = str_replace('~NL~',"\n",$query);
|
|
$query = $this->utf8_strrev($query);
|
|
if ( $substWBParms ) {
|
|
$query = writeExcel::replace_vars($query, $substParms,$extraParms);
|
|
}
|
|
// Get Database Connection
|
|
$db = null;
|
|
$dbType = 1;
|
|
if ( $parmDB == 1 ) {
|
|
$db = & JFactory::getDBO();
|
|
} else {
|
|
$jdb = & JFactory::getDBO();
|
|
$jdb->setQuery( 'SELECT * FROM #__sql2excel_databases WHERE ID=' . $parmDB );
|
|
$dbInfo = $jdb->loadObject();
|
|
if ( $dbInfo ) {
|
|
$db = writeExcel::getDB($dbInfo);
|
|
$dbType=$dbInfo->db_type;
|
|
}
|
|
}
|
|
if ( $db ) {
|
|
$rows = writeExcel::getResults($db, $query, $cmpParms, $dbType);
|
|
if ( is_Array($rows) ) {
|
|
$colNames = writeExcel::getColumnNames($rows);
|
|
$firstRow = $rows[0];
|
|
|
|
if ( strpos($parmType,'Multi') > 0 ) {
|
|
if ( count($colNames) > 2 ) {
|
|
$multiStr = '';
|
|
foreach ( $rows as $row ) {
|
|
if ( $row->$colNames[2] != '' ) {
|
|
if ( $parmType == 'ListSQLMultiStr' ) {
|
|
$pVal = "'" . $row->$colNames[2] . "'";
|
|
} else {
|
|
$pVal = $row->$colNames[2];
|
|
}
|
|
if ( $multiStr == '' ) {
|
|
$multiStr = $pVal;
|
|
} else {
|
|
$multiStr .= ',' . $pVal;
|
|
}
|
|
}
|
|
}
|
|
if ( $multiStr != '' ) {
|
|
$parmValue = $multiStr;
|
|
} else {
|
|
$parmValue = $firstRow->$colNames[0];
|
|
if ( $parmType == 'ListSQLMultiStr' ) {
|
|
$parmValue = "'" . $parmValue . "'";
|
|
}
|
|
}
|
|
} else {
|
|
$parmValue = $firstRow->$colNames[0];
|
|
if ( $parmType == 'ListSQLMultiStr' ) {
|
|
$parmValue = "'" . $parmValue . "'";
|
|
}
|
|
}
|
|
} else {
|
|
if ( count($colNames) > 2 ) {
|
|
//$parmValue = $firstRow[2];
|
|
$parmValue = $firstRow->$colNames[2];
|
|
} else {
|
|
//$parmValue = $firstRow[0];
|
|
$parmValue = $firstRow->$colNames[0];
|
|
}
|
|
}
|
|
} else {
|
|
$parmValue = '';
|
|
}
|
|
} else {
|
|
$parmValue = JText::_('ERROR: Workbook Parameter') . ' ' . $parmVar . ' - ' . JText::_('Database Connection Error!');
|
|
}
|
|
|
|
// Save this parameter for further substitutions
|
|
$extraParms[$parmVar]=$parmValue;
|
|
} else { // Parameter was prompted => getting value directly
|
|
$parmValue = $query;
|
|
}
|
|
|
|
} else {
|
|
$parmValue = Sql2excelParms::get_parm_value($parmID, 'string', $parmValue);
|
|
}
|
|
|
|
if ( is_string($parmValue) && $substWBParms ) {
|
|
$parmValue = writeExcel::replace_vars($parmValue, $substParms,$extraParms);
|
|
}
|
|
|
|
$wbP[$parmVar]=$parmValue;
|
|
|
|
// Save this parameter for further substitutions
|
|
$extraParms[$parmVar]=$parmValue;
|
|
|
|
$i++;
|
|
}
|
|
}
|
|
|
|
if ( $i > 1 ) {
|
|
$substParms = array_merge($substParms,$wbP);
|
|
|
|
// Don't use cache for Workbooks with parameters
|
|
$useCache = 0;
|
|
}
|
|
}
|
|
|
|
|
|
if ( count($wsParms) == 1 ) {
|
|
|
|
// Use Cache ?
|
|
//$cacheEnabled = $cmpParms->get( 'cache_enabled', 0 );
|
|
$cacheEnabled = Sql2excelParms::get($cmpParms,'cache_enabled', 0);
|
|
//$cacheDir = $cmpParms->get( 'cache_dir', JPATH_COMPONENT.DS.'cache'.DS );
|
|
$cacheDir = Sql2excelParms::get($cmpParms,'cache_dir', JPATH_COMPONENT.DS.'cache'.DS);
|
|
//$cacheTime = $cmpParms->get( 'cache_time', 30 );
|
|
$cacheTime = Sql2excelParms::get($cmpParms,'cache_time', 30);
|
|
|
|
$wbCache = $wbParms[0]->cache;
|
|
$wsData = '';
|
|
if ( substr($cacheDir,strlen($cacheDir)-1,1) != DS ) {
|
|
$cacheDir .= DS;
|
|
}
|
|
|
|
if ( ( $useCache && ($cacheEnabled && $wbCache == 'Global') ) || ( $useCache && $wbCache == 'Yes' ) ) {
|
|
if ( file_exists($cacheDir) ) {
|
|
$cacheFN = $cacheDir . 'preview_' . $wb_id . '_' . $ws_id . '.cache';
|
|
if ( file_exists($cacheFN) ) {
|
|
$fileModified = filemtime($cacheFN);
|
|
$age = time() - $fileModified;
|
|
$minutesOld = $age / 60;
|
|
if ( $cacheTime >= $minutesOld ) {
|
|
// Server from cache
|
|
$wsData = file_get_contents($cacheFN);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if ( $wsData == '' ) {
|
|
$wsParms = $wsParms[0];
|
|
$wsData = $this->writeWorksheet($wbParms,$wsParms,$cmpParms,$substParms);
|
|
if ( ( $useCache && ($cacheEnabled && $wbCache == 'Global') ) || ( $useCache && $wbCache == 'Yes' ) ) {
|
|
if ( file_exists($cacheDir) ) {
|
|
// Write cache file
|
|
$fh = fopen($cacheFN, 'w') or die("Can't write to preview cache file");
|
|
fwrite($fh, $wsData);
|
|
fclose($fh);
|
|
}
|
|
}
|
|
}
|
|
|
|
/*
|
|
$wsData = str_replace("\r\n",'', $wsData);
|
|
$wsData = str_replace("\n",'', $wsData);
|
|
$wsData = str_replace("\\","\\\\", $wsData);
|
|
$wsData = str_replace('"','\"', $wsData);
|
|
*/
|
|
|
|
//echo '_cba.ready ( ' . (int) $cbaid . ', { "code" : "' . $wsData . '" } );';
|
|
echo $wsData;
|
|
|
|
} else {
|
|
//echo '_cba.ready ( ' . (int) $cbaid . ', { "code" : "' . JText::_( 'ERROR : Worksheet not found' ) . '" } );';
|
|
echo JText::_( 'ERROR : Worksheet not found' );
|
|
}
|
|
|
|
} else {
|
|
//echo '_cba.ready ( ' . (int) $cbaid . ', { "code" : "' . JText::_( 'ERROR : Workbook not found' ) . '" } );';
|
|
echo JText::_( 'ERROR : Workbook not found' );
|
|
|
|
}
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
function utf8_strrev($str, $reverse_numbers = true){
|
|
$pattern = $reverse_numbers ? '/./us' : '/(\d+)?./us';
|
|
preg_match_all($pattern, $str, $ar);
|
|
return join('',array_reverse($ar[0]));
|
|
}
|
|
|
|
|
|
function writeWorksheet($wbParms,$ws,$cmpParms,$substParms)
|
|
{
|
|
$downloadLabel = JText::_( 'PLEASE_DL_FULL_REPORT');
|
|
$dlformat = 1;
|
|
if ( isset($wbParms[0]->dlformat) ) {
|
|
$dlformat = $wbParms[0]->dlformat;
|
|
}
|
|
|
|
if ( $dlformat == 3 || ( $dlformat == 1 && Sql2excelParms::get($cmpParms,'dlformat', '2') == 3 ) ) {
|
|
$downloadLabel = JText::_( 'PLEASE_DL_CVS_FULL_REPORT');
|
|
}
|
|
elseif ( $dlformat == 4 || ( $dlformat == 1 && Sql2excelParms::get($cmpParms,'dlformat', '2') == 4 ) ) {
|
|
$downloadLabel = JText::_( 'PLEASE_DL_HTM_FULL_REPORT');
|
|
}
|
|
|
|
$output = '';
|
|
$this->_formulas = false;
|
|
|
|
//$maxRows = $cmpParms->get( 'previewmaxrows', '10' );
|
|
$maxRows = Sql2excelParms::get($cmpParms,'previewmaxrows', '10');
|
|
if ( $ws->preview_rows > 0 ) { $maxRows = $ws->preview_rows; }
|
|
|
|
//$showFormulas = $cmpParms->get( 'formulas_in_preview', 'Label' );
|
|
$showFormulas = Sql2excelParms::get($cmpParms,'formulas_in_preview', 'Label');
|
|
|
|
if ( $ws->preview_formulas != 'Global' ) { $showFormulas = $ws->preview_formulas; }
|
|
|
|
$formulaLabel = JText::_('#FORMULA');
|
|
|
|
//showExtraCells = $cmpParms->get( 'extracells_in_preview', 'Show' );
|
|
$showExtraCells = Sql2excelParms::get($cmpParms,'extracells_in_preview', 'Show');
|
|
$pEc = $showExtraCells;
|
|
|
|
if ( $ws->preview_extracells != 'Global' ) { $showExtraCells = $ws->preview_extracells; }
|
|
|
|
//$this->_formulas = $this->contains_formulas($ws);
|
|
|
|
$hideColumns = $ws->preview_hidecols;
|
|
$hideColumns = explode(',',trim($hideColumns));
|
|
|
|
|
|
$db = & JFactory::getDBO();
|
|
$wbParms = $wbParms[0];
|
|
$extraParms = array();
|
|
|
|
// Write Header
|
|
$fileName = $wbParms->filename;
|
|
if ( trim($fileName) == '' ) {
|
|
$fileName = 'download.xls';
|
|
}
|
|
|
|
|
|
$extraParms['SQL2EXCEL_WB_FN'] = $fileName;
|
|
$extraParms['SQL2EXCEL_WB_LINK'] = $wbParms->link_title;
|
|
$extraParms['SQL2EXCEL_WB_DLCNT'] = $wbParms->count;
|
|
|
|
$sheetName = $ws->sheetname;
|
|
if ( trim($sheetName) == '' ) {
|
|
$sheetName = 'Sheet [N]';
|
|
}
|
|
$extraParms['SQL2EXCEL_WS_SN'] = $sheetName;
|
|
$extraParms['SQL2EXCEL_WS_DLCNT'] = $ws->count;
|
|
|
|
$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);
|
|
}
|
|
$extraParms['SQL2EXCEL_WS_SQL'] = $query;
|
|
|
|
$formulas = explode(',', $ws->formulas );
|
|
|
|
// Get Database Connection
|
|
unset($db);
|
|
$db = writeExcel::getDB($ws);
|
|
|
|
// Run query!
|
|
if ( $db ) {
|
|
$rows = writeExcel::getResults($db, $query, $cmpParms, $ws->db_type);
|
|
} else {
|
|
$rows = JText::_( 'DB_CONNECT_ERROR');
|
|
}
|
|
|
|
|
|
if ( is_array($rows) ) {
|
|
|
|
$extraParms['SQL2EXCEL_WS_ROWS'] = count($rows) + 1;
|
|
|
|
// Column titles
|
|
$colNames = writeExcel::getColumnNames($rows);
|
|
//Number of Columns in user query (exclude JoomFish fields if any)
|
|
$nrCols = writeExcel::getColumnCount($colNames, $query);
|
|
|
|
// 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) ) {
|
|
foreach ($headertextArr as $headerrow) {
|
|
$headerrow = trim($headerrow);
|
|
if ( $substHeadFoot ) {
|
|
$headerrow = writeExcel::replace_vars($headerrow, $substParms, $extraParms );
|
|
}
|
|
$output .= $this->writeCell($headerrow, '', $currRow, 0);
|
|
$currRow++;
|
|
}
|
|
}
|
|
}
|
|
$headerRows = $currRow;
|
|
|
|
|
|
// Add column titles
|
|
$actualCol=0;
|
|
for ($i=0; $i<$nrCols; $i++)
|
|
{
|
|
$currCol = $i+1;
|
|
if ( !in_array($currCol,$hideColumns) && (!in_array($currCol,$formulas) || ( in_array($currCol,$formulas) && $showFormulas != 'Hide' )) ) {
|
|
$output .= $this->writeTH($currRow, $actualCol, $colNames[$i]);
|
|
$actualCol++;
|
|
}
|
|
}
|
|
$colHeadRow = $currRow;
|
|
|
|
|
|
// Add all the data
|
|
$currRow++;
|
|
$cnt=1;
|
|
$mindatarow = $currRow + 1;
|
|
$maxdatarow = $currRow + count($rows);
|
|
foreach ( $rows as $row ) {
|
|
if ( $cnt <= $maxRows ) {
|
|
$actualCol=0;
|
|
for ( $i=0; $i<$nrCols; $i++) {
|
|
|
|
$currCol = $i+1;
|
|
if ( !in_array($currCol,$hideColumns) && (!in_array($currCol,$formulas) || ( in_array($currCol,$formulas) && $showFormulas != 'Hide' )) ) {
|
|
$output .= $this->writeCell($row->$colNames[$i],'',$currRow, $actualCol, $formulas, $showFormulas, $formulaLabel,$mindatarow,$maxdatarow);
|
|
$actualCol++;
|
|
}
|
|
|
|
}
|
|
$currRow++;
|
|
}
|
|
$cnt++;
|
|
}
|
|
|
|
// Add Extra Cells if specified
|
|
if ( $showExtraCells == 'Show' ) {
|
|
$this->writeExtraCells($ws, $maxRows+$headerRows, $showFormulas, $formulaLabel, $cmpParms, $formulas, $substSQL, $substParms);
|
|
}
|
|
|
|
|
|
// Add Footer, if specified
|
|
$footer = $ws->footer;
|
|
$footertextArr = explode("\n",$footer);
|
|
$firstFooterRow = $currRow;
|
|
if ( is_array($footertextArr) ) {
|
|
foreach ($footertextArr as $footerrow) {
|
|
$footerrow = trim($footerrow);
|
|
if ( $substHeadFoot ) {
|
|
$footerrow = writeExcel::replace_vars($footerrow, $substParms, $extraParms );
|
|
}
|
|
$output .= $this->writeCell($footerrow, '', $currRow, 0);
|
|
$currRow++;
|
|
}
|
|
}
|
|
|
|
// Generate output from the table in memory
|
|
$output = $this->formatOutput($maxRows, $colHeadRow, $headerRows, $firstFooterRow, $nrCols);
|
|
|
|
// Add truncation / formulas / hidden columns message, if applicable
|
|
$notes = '';
|
|
$hiddenStuff = 0;
|
|
if ( count($rows) > $maxRows ) {
|
|
$notes .= '<li>' . JText::_( 'Preview showing ') . $maxRows . ' ' . JText::_( 'of') . ' ' . count($rows) . ' ' . JText::_( 'records') . '</li>';
|
|
} else {
|
|
$notes .= '<li>' . JText::_( 'Total Records Found') . ' = ' . count($rows) . '</li>';
|
|
}
|
|
if ( $this->_formulas && ( $showFormulas == 'Show' || $showFormulas == 'Label') ) {
|
|
$notes .= '<li>' . JText::_('FORMULAS_NOT_IN_PREVIEW') . '.</li>';
|
|
}
|
|
if ( $this->_formulas && $showFormulas == 'Hide' ) {
|
|
$notes .= '<li>' . JText::_('FORMULAS_HIDDEN_IN_PREVIEW') . '.</li>';
|
|
}
|
|
if ( count($hideColumns) > 1 || $hideColumns[0] > 0 ) {
|
|
$notes .= '<li>' . JText::_('COLUMNS_HIDDEN_IN_PREVIEW') . '.</li>';
|
|
$hiddenStuff = 1;
|
|
}
|
|
if ( $showExtraCells == 'Hide' && $ws->extracells != '' ) {
|
|
$notes .= '<li>' . JText::_('EXTRACELLS_HIDDEN_IN_PREVIEW') . '.</li>';
|
|
$hiddenStuff = 1;
|
|
}
|
|
$output .= '<p><br><b>' . JText::_( 'Notes') . ':</b><ul>' . $notes . '</ul></p>';
|
|
|
|
|
|
$wbDLbtn = $wbParms->preview_dl_btn;
|
|
$dlBtn = 0;
|
|
if ( $wbDLbtn == 'Show' || ( $wbDLbtn == 'Global' && Sql2excelParms::get($cmpParms,'previewdlbutton', 1) ) ) { $dlBtn=1; }
|
|
if ( $dlBtn && ( $this->_formulas || count($rows) > $maxRows || $hiddenStuff) ) {
|
|
$output .= '<p>' . $downloadLabel . '</p>';
|
|
}
|
|
|
|
|
|
} elseif ( $rows == '' ) {
|
|
// Empty recordset.
|
|
$errMsg = trim($ws->error_norecords);
|
|
if (! $errMsg) { $errMsg = JText::_( 'No records found' ); }
|
|
$output .= '<tr><td>' . $errMsg . '</td></tr>';
|
|
$output .= $this->tableEnd();
|
|
} elseif ( $rows != '' && is_string($rows) && Sql2excelParms::get($cmpParms,'show_sql_errors', 1) ) {
|
|
$errMsg = explode("\n", $rows);
|
|
foreach ( $errMsg as $row ) {
|
|
$output .= $this->newRow();
|
|
$row = trim($row);
|
|
$output .= $this->writeCell($row);
|
|
$output .= $this->endRow();
|
|
}
|
|
$output .= $this->tableEnd();
|
|
}
|
|
|
|
|
|
return $output;
|
|
}
|
|
|
|
|
|
function newRow($extra='')
|
|
{
|
|
return '<tr' . $extra . '>';
|
|
}
|
|
|
|
function endRow()
|
|
{
|
|
return '</tr>';
|
|
}
|
|
|
|
function tableEnd()
|
|
{
|
|
$retStr = '</table>';
|
|
return $retStr;
|
|
}
|
|
|
|
function writeCell($str, $extra='', $row=0, $col=0, $formulas=0, $showFormulas='Label', $formulaLabel='#FORMULA',$mindatarow=0,$maxdatarow=0)
|
|
{
|
|
if ( substr(strtolower($str),0,7) == 'http://' ) {
|
|
$str = '<a href="' . $str . '" target="_NEW">' . $str . '</a>';
|
|
} elseif ( 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!
|
|
$this->_formulas = true;
|
|
if ( $showFormulas == 'Label' ) {
|
|
// Show Generic #FORMULA Label
|
|
$str = $formulaLabel;
|
|
} elseif ( $showFormulas == 'Show' ) {
|
|
// Show Full Formula Syntax
|
|
// Parse and replace Row and Col ID's
|
|
$str = writeExcel::replace_row_col_ids($str, $rowUser, $colUser, $mindatarow,$maxdatarow);
|
|
} else {
|
|
// Hide Formula
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
if ( $str == '' ) { $str=' '; }
|
|
|
|
// Add cell to array
|
|
if ( isset($this->_wsArr[$row]) ) {
|
|
$currRowArr = $this->_wsArr[$row];
|
|
} else {
|
|
$currRowArr = array();
|
|
}
|
|
$currRowArr[$col] = $str;
|
|
$this->_wsArr[$row] = $currRowArr;
|
|
|
|
}
|
|
|
|
function writeTH($row, $col, $str, $extra='')
|
|
{
|
|
|
|
// Add cell to array
|
|
if ( isset($this->_wsArr[$row]) ) {
|
|
$currRowArr = $this->_wsArr[$row];
|
|
} else {
|
|
$currRowArr = array();
|
|
}
|
|
$currRowArr[$col] = $str;
|
|
$this->_wsArr[$row] = $currRowArr;
|
|
|
|
}
|
|
|
|
|
|
function writeExtraCells($ws, $maxRow, $showFormulas, $formulaLabel, $cmpParms, $formulas, $substSQL, $substParms)
|
|
{
|
|
|
|
// Extra Cells?
|
|
$extraCells = explode("\n",$ws->extracells);
|
|
|
|
foreach ( $extraCells as $row ) {
|
|
$cellInfo = explode(',',$row);
|
|
if ( count($cellInfo) > 6 ) {
|
|
|
|
$cellData = trim($cellInfo[3]);
|
|
$cellData = str_replace('@~@',',',$cellData);
|
|
$row = (int) $cellInfo[1]-1;
|
|
$col = (int) $cellInfo[2]-1;
|
|
if ( $row < $maxRow ) {
|
|
if ( $cellInfo[0] == 2 || $cellInfo[0] == 4 ) {
|
|
// Formula
|
|
$colUser = $col+1;
|
|
$this->writeCell($cellData, '', $row, $col, array($colUser), $showFormulas, $formulaLabel);
|
|
} elseif ( $cellInfo[0] == 6 ) {
|
|
// SQL query data
|
|
$extraDB = 1;
|
|
if ( isset($cellInfo[7]) ) { $extraDB = $cellInfo[7]; }
|
|
$query = strrev($cellData);
|
|
if ( $substSQL ) {
|
|
$query = writeExcel::replace_vars($query, $substParms);
|
|
}
|
|
$this->writeExtraSQL($query, $extraDB, $maxRow, $row, $col, $formulas, $showFormulas, $formulaLabel, $cmpParms);
|
|
} else {
|
|
// Text
|
|
$this->writeCell($cellData, '', $row, $col);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
function writeExtraSQL($query, $dbID, $maxRow, $row, $col, $formulas, $showFormulas, $formulaLabel, $cmpParms) {
|
|
|
|
$jdb = & JFactory::getDBO();
|
|
$jdb->setQuery("SELECT * FROM #__sql2excel_databases WHERE ID=" . $dbID);
|
|
$dbInfo = $jdb->loadObject();
|
|
if ( $dbInfo ) {
|
|
$cellDB = writeExcel::getDB($dbInfo);
|
|
if ( $cellDB ) {
|
|
$rows = writeExcel::getResults($cellDB, $query, $cmpParms, $dbInfo->db_type);
|
|
if ( is_array($rows) ) {
|
|
// Column titles
|
|
$colNames = writeExcel::getColumnNames($rows);
|
|
//Number of Columns in user query (exclude JoomFish fields if any)
|
|
$nrCols = writeExcel::getColumnCount($colNames, $query);
|
|
|
|
$currRow = $row;
|
|
foreach ( $rows as $row ) {
|
|
if ( $currRow <= $maxRow ) {
|
|
for ( $i=0; $i<$nrCols; $i++ ) {
|
|
$this->writeCell($row->$colNames[$i], '', $currRow, $col+$i, $formulas);
|
|
}
|
|
}
|
|
$currRow++;
|
|
}
|
|
}
|
|
|
|
} else {
|
|
$this->writeCell(JText::_('DB_CONNECT_ERROR'), '', $row, $col);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
function formatOutput($maxRows, $colHeadRow, $headerRows, $firstFooterRow, $nrDataCols)
|
|
{
|
|
|
|
// Get max row number
|
|
$rowMax = max(array_keys($this->_wsArr));
|
|
|
|
// Get max column number
|
|
$maxCol = 0;
|
|
foreach ( $this->_wsArr as $row ) {
|
|
$col = max(array_keys($row));
|
|
if ( $col > $maxCol ) { $maxCol = $col; }
|
|
}
|
|
|
|
// Max Data Column Number
|
|
//$nrDataCols--;
|
|
$nrDataCols = $maxCol;
|
|
|
|
$retStr = '<table class="datatable" cellspacing=0>';
|
|
|
|
$colSpan = $maxCol+1;
|
|
// Add rows
|
|
for ( $idx=0; $idx<=$rowMax; $idx++ ) {
|
|
if ( isset($this->_wsArr[$idx]) ) {
|
|
$row = $this->_wsArr[$idx];
|
|
} else {
|
|
$row = array();
|
|
}
|
|
$retStr .= '<tr>';
|
|
|
|
$maxRowCol = max(array_keys($row));
|
|
//$retStr .= '<td>'. $maxRowCol . '</td>';
|
|
|
|
|
|
for ( $i=0; $i<=$maxRowCol; $i++ ) {
|
|
if ( isset($row[$i]) ) {
|
|
$str = $row[$i];
|
|
} else {
|
|
$str=' ';
|
|
}
|
|
if ( $idx == $colHeadRow ) {
|
|
// Data Column Header
|
|
|
|
// First Column?
|
|
$extra = '';
|
|
if ( $i == 0 ) { $extra .= 'border-left: 1px solid #000;'; }
|
|
$extra = ' style="' . $extra . '"';
|
|
|
|
if ( $str != '' ) {
|
|
$retStr .= '<th' . $extra . '>'. $str . '</th>';
|
|
} else {
|
|
$retStr .= '<td> </td>';
|
|
}
|
|
|
|
if ( $i == $maxRowCol && $maxCol > $i ) {
|
|
$rcSpan = $colSpan - $i;
|
|
$retStr .= '<th colspan="' . $rcSpan . '"> </th>';
|
|
}
|
|
|
|
} else if ( ($idx < $headerRows || $idx >= $firstFooterRow) && $i == $maxRowCol ) {
|
|
$rcSpan = $colSpan - $i;
|
|
if ( $rcSpan > 1 ) {
|
|
$retStr .= '<td colspan="' . $rcSpan . '" align="left">' . $str . '</td>';
|
|
} else {
|
|
|
|
$retStr .= '<td align="left">'. $str . '</td>';
|
|
}
|
|
} else {
|
|
// Data Rows or Multiple Cell Header / Footer Rows
|
|
$extra = '';
|
|
|
|
// First Column and inside Data?
|
|
if ( $i == 0 && $idx > $headerRows && $idx < $firstFooterRow ) { $extra .= 'border-left: 1px solid #000;'; }
|
|
|
|
// Last Column?
|
|
if ( $i == $nrDataCols ) { $extra .= 'border-right: 1px solid #000;'; }
|
|
|
|
|
|
// Last Data Row?
|
|
if ( $idx == $firstFooterRow - 1 ) {
|
|
$extra .= 'border-bottom: 1px solid #000;';
|
|
}
|
|
|
|
$extra = ' style="' . $extra . '"';
|
|
|
|
$retStr .= '<td align="left"' . $extra .'>'. $str . '</td>';
|
|
if ( $i == $maxRowCol && $i < $maxCol ) {
|
|
$rcSpan = $colSpan - $i;
|
|
$retStr .= '<td colspan="' . $rcSpan . '"> </td>';
|
|
}
|
|
}
|
|
}
|
|
|
|
$retStr .= '</tr>';
|
|
}
|
|
$retStr .= '</table>';
|
|
|
|
return $retStr;
|
|
|
|
}
|
|
|
|
|
|
|
|
function contains_formulas($ws) {
|
|
|
|
if ( $ws->formulas != '' ) {
|
|
return true;
|
|
} else {
|
|
// Check Extra Cells for Formulas
|
|
$extraCells = explode("\n",$ws->extracells);
|
|
$formulas = false;
|
|
|
|
foreach ( $extraCells as $row ) {
|
|
$cellInfo = explode(',',$row);
|
|
if ( count($cellInfo) > 6 ) {
|
|
if ( $cellInfo[0] == 2 || $cellInfo[0] == 4 ) {
|
|
// Formula
|
|
$formulas = true;
|
|
}
|
|
}
|
|
}
|
|
return $formulas;
|
|
}
|
|
}
|
|
} |