Issue
I am currently working in a system that was built on symfony 1.4 long before i came into this company.
I am trying to modify a function that forces an excel download, but i cant figure out where the content is being set. My assumption is that it is somehow grabbing the entirety of the '$this' object?
i see headers:
$this->getResponse()->setHttpHeader('Content-Disposition', 'filename=winners.xls');
$this->getResponse()->setHttpHeader('Content-Type', 'application/vnd.ms-excel; charset=utf-8');
and i see this function being invoked:
$this->setLayout('none');
and everything else is variables that are being passed to the view.
I just need to be able to reference the content being sent for download, so that i can run it through PHPExcel.
Any idea how to reference the content being sent?
UPDATE: Here's the entire function
$round = $request->getParameter("round");
$league = $request->getParameter("league");
$this->all_contest_ids = array();
$contest = asPickem::getInstance()->getContest();
// find all child contest ids
$this->all_contest_ids[] = $contest->getId();
if ($contest->getNode()->hasChildren()) {
foreach ($contest->getNode()->getDescendants() as $child) {
$this->all_contest_ids[] = $child->getId();
}
}
if (!$round || $round == "-1") {
$tablename = ucfirst(asPickem::getInstance()->getProduct()->getSlug()) . 'RankUserOverall';
$query = Doctrine_Core::getTable($tablename)->getRankingsQuery(asPickem::getInstance()->getContest()->getId(), $this->getExcludedUserEntryIds());
$this->title = "Overall Rankings";
} // show round results
else {
if (isset($league)) {
$tablename = ucfirst(asPickem::getInstance()->getProduct()->getSlug()) . 'RankLeagueUser';
$query = Doctrine_Core::getTable($tablename)->getRankingsQuery($league, $round);
} else {
$tablename = ucfirst(asPickem::getInstance()->getProduct()->getSlug()) . 'RankUser';
$query = Doctrine_Core::getTable($tablename)->getRankingsQuery(asPickem::getInstance()->getContest()->getId(), $round, $this->getExcludedUserEntryIds($this->round));
}
$r = Doctrine_Core::getTable('CampaignRound')->find($round);
$this->title = $r->getView() . " Rankings";
}
$limit = $request->getParameter("limit", 100);
if ($limit > 0) {
$query->limit($limit);
}
$this->rankings = $query->execute();
// load survey questions
$survies = array();
$thisSurvies = Doctrine_Core::getTable('Survey')->getByContest(asPickem::getInstance()->getContest()->getId());
foreach ($thisSurvies as $survey) {
$survies[] = $survey;
}
// if this contest is a child of a portal then include the parent surveys
$c = asPickem::getInstance()->getContest();
while ($c->getParent()) {
$c = $c->getParent();
if ($c->getProduct()->getSlug() == 'portal') {
$this->includeParentSurveyQuestions = true;
break;
}
}
if ($this->includeParentSurveyQuestions) {
$contest = asPickem::getInstance()->getContest();
while ($contest->getParent()) {
$contest = $contest->getParent();
$parentSurvies = Doctrine_Core::getTable('Survey')->getByContest($contest->getId());
foreach ($parentSurvies as $survey) {
$survies[] = $survey;
}
}
}
$this->questions = array();
foreach ($survies as $survey) {
foreach ($survey->getSortedQuestions() as $question) {
$this->questions[$question->getId()] = $question->getQuestionText();
}
}
unset($survies);
$temp = tempnam(sys_get_temp_dir(), 'html');
file_put_contents($temp, $this->getResponse()->getTemplate());
//build instance of PHPExcel object, and load/read html file
require_once dirname(__FILE__) . '/../../../../../EXCEL/PHPExcel.php';
$objPHPExcel = new PHPExcel();
$excelHTMLReader = PHPExcel_IOFactory::createReader('HTML');
$excelHTMLReader->loadIntoExisting($temp, $objPHPExcel);
$objPHPExcel->getActiveSheet()->setTitle('userlist');
//delete the temporary file
unlink($temp);
$this->getResponse()->setHttpHeader('Content-Disposition', 'filename=winners.xls');
$this->getResponse()->setHttpHeader('Content-Type', 'application/vnd.ms-excel; charset=utf-8');
$writer = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$this->renderText($writer->save('php://output'));
$this->setLayout('none');
Solution
I was able to figure this out, and the answer is as follows...
this is how i was able to reference the information being sent to the view:
$this->setLayout('none');
$view = $this->getController()->getView($this->getContext()->getModuleName(), $this->getContext()->getActionName(), sfview::SUCCESS);
$view->execute();
$view->getAttributeHolder()->add($this->getVarHolder()->getAll());
$exContent = $view->render();
for those who may end up facing a similar problem, here is a look at the entirety of what i used to get the reference, and run it through PHPExcel in order to stop the Excel alert pop-up about the file and it:
$this->setLayout('none');
$view = $this->getController()->getView($this->getContext()->getModuleName(), $this->getContext()->getActionName(), sfview::SUCCESS);
$view->execute();
$view->getAttributeHolder()->add($this->getVarHolder()->getAll());
$exContent = $view->render();
$temp = tempnam(sys_get_temp_dir(), 'html');
file_put_contents($temp, $exContent);
//build instance of PHPExcel object, and load/read html file
require_once dirname(__FILE__) . '/../../../../../EXCEL/PHPExcel.php';
$objPHPExcel = new PHPExcel();
$excelHTMLReader = PHPExcel_IOFactory::createReader('HTML');
$excelHTMLReader->loadIntoExisting($temp, $objPHPExcel);
$objPHPExcel->getActiveSheet()->setTitle('userlist');
//delete the temporary file
unlink($temp);
header('Content-Type: application/vnd.ms-excel'); // header for .xls file
header('Content-Disposition: attachment;filename=winners.xls'); // specify the download file name
header('Cache-Control: max-age=0');
$writer = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$writer->save('php://output');
$this->setLayout(false);
Hope this is able to help someone. Thanks!
Answered By - finesse.png Answer Checked By - Marie Seifert (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.