From 62d1735e1cfc345ce7c33016b43daf5528339fb5 Mon Sep 17 00:00:00 2001 From: Carsten Schmitz Date: Fri, 2 Nov 2012 14:01:00 +0100 Subject: [PATCH] Fixed issue #6748: In PDF export the token table fields placeholders are not replaced --- .../controllers/PrintanswersController.php | 500 +++++++++--------- 1 file changed, 250 insertions(+), 250 deletions(-) diff --git a/application/controllers/PrintanswersController.php b/application/controllers/PrintanswersController.php index 39c7d3b8f17..bd47e6b3018 100644 --- a/application/controllers/PrintanswersController.php +++ b/application/controllers/PrintanswersController.php @@ -1,250 +1,250 @@ -loadHelper("frontend"); - - Yii::import('application.libraries.admin.pdf'); - - $surveyid = (int)($surveyid); - Yii::app()->loadHelper('database'); - - if (isset($_SESSION['survey_'.$surveyid]['sid'])) - { - $surveyid = $_SESSION['survey_'.$surveyid]['sid']; - } - else - { - die('Invalid survey/session'); - } - - //Debut session time out - if (!isset($_SESSION['survey_'.$surveyid]['finished']) || !isset($_SESSION['survey_'.$surveyid]['srid'])) - // Argh ... a session time out! RUN! - //display "sorry but your session has expired" - { - //require_once($rootdir.'/classes/core/language.php'); - $baselang = Survey::model()->findByPk($surveyid)->language; - Yii::import('application.libraries.Limesurvey_lang', true); - $clang = new Limesurvey_lang($baselang); - //A nice exit - - sendCacheHeaders(); - doHeader(); - - echo templatereplace(file_get_contents(getTemplatePath(validateTemplateDir("default"))."/startpage.pstpl"),array(),array()); - echo "

\n" - ."\t".$clang->gT("Error")."
\n" - ."\t".$clang->gT("We are sorry but your session has expired.")."
".$clang->gT("Either you have been inactive for too long, you have cookies disabled for your browser, or there were problems with your connection.")."
\n" - ."\t".sprintf($clang->gT("Please contact %s ( %s ) for further assistance."),$siteadminname,$siteadminemail)."\n" - ."

\n"; - - echo templatereplace(file_get_contents(getTemplatePath(validateTemplateDir("default"))."/endpage.pstpl"),array(),array()); - doFooter(); - exit; - } - //Fin session time out - - $id = $_SESSION['survey_'.$surveyid]['srid']; //I want to see the answers with this id - $clang = $_SESSION['survey_'.$surveyid]['s_lang']; - - //Ensure script is not run directly, avoid path disclosure - //if (!isset($rootdir) || isset($_REQUEST['$rootdir'])) {die( "browse - Cannot run this script directly");} - - // Set the language for dispay - //require_once($rootdir.'/classes/core/language.php'); // has been secured - if (isset($_SESSION['survey_'.$surveyid]['s_lang'])) - { - - $clang = SetSurveyLanguage( $surveyid, $_SESSION['survey_'.$surveyid]['s_lang']); - $language = $_SESSION['survey_'.$surveyid]['s_lang']; - } - else - { - $language = Survey::model()->findByPk($surveyid)->language; - $clang = SetSurveyLanguage( $surveyid, $language); - } - - // Get the survey inforamtion - $thissurvey = getSurveyInfo($surveyid,$language); - - //SET THE TEMPLATE DIRECTORY - if (!isset($thissurvey['templatedir']) || !$thissurvey['templatedir']) - { - $thistpl = validateTemplateDir("default"); - } - else - { - $thistpl = validateTemplateDir($thissurvey['templatedir']); - } - - if ($thissurvey['printanswers'] == 'N') - { - die(); //Die quietly if print answers is not permitted - } - - //CHECK IF SURVEY IS ACTIVATED AND EXISTS - - $surveytable = "{{survey_{$surveyid}}}"; - $surveyname = $thissurvey['surveyls_title']; - $anonymized = $thissurvey['anonymized']; - - - //OK. IF WE GOT THIS FAR, THEN THE SURVEY EXISTS AND IT IS ACTIVE, SO LETS GET TO WORK. - //SHOW HEADER - $printoutput = ''; - $printoutput .= "
\n
"; - if($printableexport == 'pdf') - { - require (Yii::app()->getConfig('rootdir').'/application/config/tcpdf.php'); - Yii::import('application.libraries.admin.pdf', true); - $pdf = new pdf(); - $pdf->setConfig($tcpdf); - //$pdf->SetFont($pdfdefaultfont,'',$pdffontsize); - $pdf->AddPage(); - //$pdf->titleintopdf($clang->gT("Survey name (ID)",'unescaped').": {$surveyname} ({$surveyid})"); - $pdf->SetTitle($clang->gT("Survey name (ID)",'unescaped').": {$surveyname} ({$surveyid})"); - } - $printoutput .= "\t
".$clang->gT("Survey name (ID):")." $surveyname ($surveyid)

 \n"; - - LimeExpressionManager::StartProcessingPage(true); // means that all variables are on the same page - // Since all data are loaded, and don't need JavaScript, pretend all from Group 1 - LimeExpressionManager::StartProcessingGroup(1,($thissurvey['anonymized']!="N"),$surveyid); - - $aFullResponseTable = getFullResponseTable($surveyid,$id,$language,true); - - //Get the fieldmap @TODO: do we need to filter out some fields? - unset ($aFullResponseTable['id']); - unset ($aFullResponseTable['token']); - unset ($aFullResponseTable['lastpage']); - unset ($aFullResponseTable['startlanguage']); - unset ($aFullResponseTable['datestamp']); - unset ($aFullResponseTable['startdate']); - - $printoutput .= "\n"; - if($printableexport == 'pdf') - { - $pdf->intopdf($clang->gT("Question",'unescaped').": ".$clang->gT("Your answer",'unescaped')); - } - - $oldgid = 0; - $oldqid = 0; - foreach ($aFullResponseTable as $sFieldname=>$fname) - { - if (substr($sFieldname,0,4) == 'gid_') - { - - if($printableexport) - { - $pdf->intopdf(flattenText($fname[0],false,true)); - $pdf->ln(2); - } - else - { - $printoutput .= "\t\n"; - } - } - elseif (substr($sFieldname,0,4)=='qid_') - { - if($printableexport == 'pdf') - { - $pdf->intopdf(flattenText($fname[0].$fname[1],false,true).": ".$fname[2]); - $pdf->ln(2); - } - else - { - $printoutput .= "\t\n"; - } - } - elseif ($sFieldname=='submitdate') - { - if($anonymized != 'Y') - { - if($printableexport == 'pdf') - { - $pdf->intopdf(flattenText($fname[0].$fname[1],false,true).": ".$fname[2]); - $pdf->ln(2); - } - else - { - $printoutput .= "\t"; - } - } - } - else - { - if($printableexport == 'pdf') - { - $pdf->intopdf(flattenText($fname[0].$fname[1],false,true).": ".$fname[2]); - $pdf->ln(2); - } - else - { - $printoutput .= "\t"; - } - } - } - - $printoutput .= "
{$fname[0]}
{$fname[0]}
{$fname[0]} {$fname[1]} {$sFieldname}{$fname[2]}
{$fname[0]} {$fname[1]}{$fname[2]}
\n"; - - if($printableexport == 'pdf') - { - - header("Pragma: public"); - header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); - - $sExportFileName = sanitize_filename($surveyname); - $pdf->Output($sExportFileName."-".$surveyid.".pdf","D"); - } - - //Display the page with user answers - if(!$printableexport) - { - - sendCacheHeaders(); - doHeader(); - - echo templatereplace(file_get_contents(getTemplatePath($thistpl).'/startpage.pstpl')); - echo templatereplace(file_get_contents(getTemplatePath($thistpl).'/printanswers.pstpl'),array('ANSWERTABLE'=>$printoutput)); - echo templatereplace(file_get_contents(getTemplatePath($thistpl).'/endpage.pstpl')); - echo ""; - } - - LimeExpressionManager::FinishProcessingGroup(); - LimeExpressionManager::FinishProcessingPage(); - } -} +loadHelper("frontend"); + + Yii::import('application.libraries.admin.pdf'); + + $surveyid = (int)($surveyid); + Yii::app()->loadHelper('database'); + + if (isset($_SESSION['survey_'.$surveyid]['sid'])) + { + $surveyid = $_SESSION['survey_'.$surveyid]['sid']; + } + else + { + die('Invalid survey/session'); + } + + //Debut session time out + if (!isset($_SESSION['survey_'.$surveyid]['finished']) || !isset($_SESSION['survey_'.$surveyid]['srid'])) + // Argh ... a session time out! RUN! + //display "sorry but your session has expired" + { + //require_once($rootdir.'/classes/core/language.php'); + $baselang = Survey::model()->findByPk($surveyid)->language; + Yii::import('application.libraries.Limesurvey_lang', true); + $clang = new Limesurvey_lang($baselang); + //A nice exit + + sendCacheHeaders(); + doHeader(); + + echo templatereplace(file_get_contents(getTemplatePath(validateTemplateDir("default"))."/startpage.pstpl"),array(),array()); + echo "


\n" + ."\t".$clang->gT("Error")."
\n" + ."\t".$clang->gT("We are sorry but your session has expired.")."
".$clang->gT("Either you have been inactive for too long, you have cookies disabled for your browser, or there were problems with your connection.")."
\n" + ."\t".sprintf($clang->gT("Please contact %s ( %s ) for further assistance."),$siteadminname,$siteadminemail)."\n" + ."

\n"; + + echo templatereplace(file_get_contents(getTemplatePath(validateTemplateDir("default"))."/endpage.pstpl"),array(),array()); + doFooter(); + exit; + } + //Fin session time out + + $id = $_SESSION['survey_'.$surveyid]['srid']; //I want to see the answers with this id + $clang = $_SESSION['survey_'.$surveyid]['s_lang']; + + //Ensure script is not run directly, avoid path disclosure + //if (!isset($rootdir) || isset($_REQUEST['$rootdir'])) {die( "browse - Cannot run this script directly");} + + // Set the language for dispay + //require_once($rootdir.'/classes/core/language.php'); // has been secured + if (isset($_SESSION['survey_'.$surveyid]['s_lang'])) + { + + $clang = SetSurveyLanguage( $surveyid, $_SESSION['survey_'.$surveyid]['s_lang']); + $language = $_SESSION['survey_'.$surveyid]['s_lang']; + } + else + { + $language = Survey::model()->findByPk($surveyid)->language; + $clang = SetSurveyLanguage( $surveyid, $language); + } + + // Get the survey inforamtion + $thissurvey = getSurveyInfo($surveyid,$language); + + //SET THE TEMPLATE DIRECTORY + if (!isset($thissurvey['templatedir']) || !$thissurvey['templatedir']) + { + $thistpl = validateTemplateDir("default"); + } + else + { + $thistpl = validateTemplateDir($thissurvey['templatedir']); + } + + if ($thissurvey['printanswers'] == 'N') + { + die(); //Die quietly if print answers is not permitted + } + + //CHECK IF SURVEY IS ACTIVATED AND EXISTS + + $surveytable = "{{survey_{$surveyid}}}"; + $surveyname = $thissurvey['surveyls_title']; + $anonymized = $thissurvey['anonymized']; + + + //OK. IF WE GOT THIS FAR, THEN THE SURVEY EXISTS AND IT IS ACTIVE, SO LETS GET TO WORK. + //SHOW HEADER + $printoutput = ''; + $printoutput .= "
\n
"; + if($printableexport == 'pdf') + { + require (Yii::app()->getConfig('rootdir').'/application/config/tcpdf.php'); + Yii::import('application.libraries.admin.pdf', true); + $pdf = new pdf(); + $pdf->setConfig($tcpdf); + //$pdf->SetFont($pdfdefaultfont,'',$pdffontsize); + $pdf->AddPage(); + //$pdf->titleintopdf($clang->gT("Survey name (ID)",'unescaped').": {$surveyname} ({$surveyid})"); + $pdf->SetTitle($clang->gT("Survey name (ID)",'unescaped').": {$surveyname} ({$surveyid})"); + } + $printoutput .= "\t
".$clang->gT("Survey name (ID):")." $surveyname ($surveyid)

 \n"; + + LimeExpressionManager::StartProcessingPage(true); // means that all variables are on the same page + // Since all data are loaded, and don't need JavaScript, pretend all from Group 1 + LimeExpressionManager::StartProcessingGroup(1,($thissurvey['anonymized']!="N"),$surveyid); + + $aFullResponseTable = getFullResponseTable($surveyid,$id,$language,true); + + //Get the fieldmap @TODO: do we need to filter out some fields? + unset ($aFullResponseTable['id']); + unset ($aFullResponseTable['token']); + unset ($aFullResponseTable['lastpage']); + unset ($aFullResponseTable['startlanguage']); + unset ($aFullResponseTable['datestamp']); + unset ($aFullResponseTable['startdate']); + + $printoutput .= "\n"; + if($printableexport == 'pdf') + { + $pdf->intopdf($clang->gT("Question",'unescaped').": ".$clang->gT("Your answer",'unescaped')); + } + + $oldgid = 0; + $oldqid = 0; + foreach ($aFullResponseTable as $sFieldname=>$fname) + { + if (substr($sFieldname,0,4) == 'gid_') + { + + if($printableexport) + { + $pdf->intopdf(flattenText($fname[0],false,true)); + $pdf->ln(2); + } + else + { + $printoutput .= "\t\n"; + } + } + elseif (substr($sFieldname,0,4)=='qid_') + { + if($printableexport == 'pdf') + { + $pdf->intopdf(flattenText($fname[0].$fname[1],false,true).": ".$fname[2]); + $pdf->ln(2); + } + else + { + $printoutput .= "\t\n"; + } + } + elseif ($sFieldname=='submitdate') + { + if($anonymized != 'Y') + { + if($printableexport == 'pdf') + { + $pdf->intopdf(flattenText($fname[0].$fname[1],false,true).": ".$fname[2]); + $pdf->ln(2); + } + else + { + $printoutput .= "\t"; + } + } + } + else + { + if($printableexport == 'pdf') + { + $pdf->intopdf(flattenText(templatereplace($fname[0]).$fname[1],false,true).": ".$fname[2]); + $pdf->ln(2); + } + else + { + $printoutput .= "\t"; + } + } + } + + $printoutput .= "
{$fname[0]}
{$fname[0]}
{$fname[0]} {$fname[1]} {$sFieldname}{$fname[2]}
{$fname[0]} {$fname[1]}{$fname[2]}
\n"; + + if($printableexport == 'pdf') + { + + header("Pragma: public"); + header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); + + $sExportFileName = sanitize_filename($surveyname); + $pdf->Output($sExportFileName."-".$surveyid.".pdf","D"); + } + + //Display the page with user answers + if(!$printableexport) + { + + sendCacheHeaders(); + doHeader(); + + echo templatereplace(file_get_contents(getTemplatePath($thistpl).'/startpage.pstpl')); + echo templatereplace(file_get_contents(getTemplatePath($thistpl).'/printanswers.pstpl'),array('ANSWERTABLE'=>$printoutput)); + echo templatereplace(file_get_contents(getTemplatePath($thistpl).'/endpage.pstpl')); + echo ""; + } + + LimeExpressionManager::FinishProcessingGroup(); + LimeExpressionManager::FinishProcessingPage(); + } +}