Changeset View
Changeset View
Standalone View
Standalone View
src/applications/maniphest/controller/ManiphestExportController.php
<?php | <?php | ||||
final class ManiphestExportController extends ManiphestController { | final class ManiphestExportController extends ManiphestController { | ||||
private $key; | |||||
public function willProcessRequest(array $data) { | |||||
$this->key = $data['key']; | |||||
return $this; | |||||
} | |||||
/** | /** | ||||
* @phutil-external-symbol class PHPExcel | * @phutil-external-symbol class PHPExcel | ||||
* @phutil-external-symbol class PHPExcel_IOFactory | * @phutil-external-symbol class PHPExcel_IOFactory | ||||
* @phutil-external-symbol class PHPExcel_Style_NumberFormat | * @phutil-external-symbol class PHPExcel_Style_NumberFormat | ||||
* @phutil-external-symbol class PHPExcel_Cell_DataType | * @phutil-external-symbol class PHPExcel_Cell_DataType | ||||
*/ | */ | ||||
public function processRequest() { | public function handleRequest(AphrontRequest $request) { | ||||
$request = $this->getRequest(); | $viewer = $this->getViewer(); | ||||
$user = $request->getUser(); | $key = $request->getURIData('key'); | ||||
$ok = @include_once 'PHPExcel.php'; | $ok = @include_once 'PHPExcel.php'; | ||||
if (!$ok) { | if (!$ok) { | ||||
$dialog = new AphrontDialogView(); | $dialog = $this->newDialog(); | ||||
epriestley: Or `$dialog = $this->newDialog();` to automatically set the user.
| |||||
$dialog->setUser($user); | |||||
$inst1 = pht( | $inst1 = pht( | ||||
'This system does not have PHPExcel installed. This software '. | 'This system does not have PHPExcel installed. This software '. | ||||
'component is required to export tasks to Excel. Have your system '. | 'component is required to export tasks to Excel. Have your system '. | ||||
'administrator install it from:'); | 'administrator install it from:'); | ||||
$inst2 = pht( | $inst2 = pht( | ||||
'Your PHP "%s" needs to be updated to include the '. | 'Your PHP "%s" needs to be updated to include the '. | ||||
Show All 15 Lines | if (!$ok) { | ||||
$dialog->addCancelButton('/maniphest/'); | $dialog->addCancelButton('/maniphest/'); | ||||
return id(new AphrontDialogResponse())->setDialog($dialog); | return id(new AphrontDialogResponse())->setDialog($dialog); | ||||
} | } | ||||
// TODO: PHPExcel has a dependency on the PHP zip extension. We should test | // TODO: PHPExcel has a dependency on the PHP zip extension. We should test | ||||
// for that here, since it fatals if we don't have the ZipArchive class. | // for that here, since it fatals if we don't have the ZipArchive class. | ||||
$saved = id(new PhabricatorSavedQueryQuery()) | $saved = id(new PhabricatorSavedQueryQuery()) | ||||
->setViewer($user) | ->setViewer($viewer) | ||||
->withQueryKeys(array($this->key)) | ->withQueryKeys(array($key)) | ||||
->executeOne(); | ->executeOne(); | ||||
if (!$saved) { | if (!$saved) { | ||||
$engine = id(new ManiphestTaskSearchEngine()) | $engine = id(new ManiphestTaskSearchEngine()) | ||||
->setViewer($user); | ->setViewer($viewer); | ||||
if ($engine->isBuiltinQuery($this->key)) { | if ($engine->isBuiltinQuery($key)) { | ||||
$saved = $engine->buildSavedQueryFromBuiltin($this->key); | $saved = $engine->buildSavedQueryFromBuiltin($key); | ||||
} | } | ||||
if (!$saved) { | if (!$saved) { | ||||
return new Aphront404Response(); | return new Aphront404Response(); | ||||
} | } | ||||
} | } | ||||
$formats = ManiphestExcelFormat::loadAllFormats(); | $formats = ManiphestExcelFormat::loadAllFormats(); | ||||
$export_formats = array(); | $export_formats = array(); | ||||
foreach ($formats as $format_class => $format_object) { | foreach ($formats as $format_class => $format_object) { | ||||
$export_formats[$format_class] = $format_object->getName(); | $export_formats[$format_class] = $format_object->getName(); | ||||
} | } | ||||
if (!$request->isDialogFormPost()) { | if (!$request->isDialogFormPost()) { | ||||
$dialog = new AphrontDialogView(); | $dialog = new AphrontDialogView(); | ||||
$dialog->setUser($user); | $dialog->setUser($viewer); | ||||
$dialog->setTitle(pht('Export Tasks to Excel')); | $dialog->setTitle(pht('Export Tasks to Excel')); | ||||
$dialog->appendChild( | $dialog->appendChild( | ||||
phutil_tag( | phutil_tag( | ||||
'p', | 'p', | ||||
array(), | array(), | ||||
pht('Do you want to export the query results to Excel?'))); | pht('Do you want to export the query results to Excel?'))); | ||||
Show All 15 Lines | public function handleRequest(AphrontRequest $request) { | ||||
if ($format === null) { | if ($format === null) { | ||||
throw new Exception(pht('Excel format object not found.')); | throw new Exception(pht('Excel format object not found.')); | ||||
} | } | ||||
$saved->makeEphemeral(); | $saved->makeEphemeral(); | ||||
$saved->setParameter('limit', PHP_INT_MAX); | $saved->setParameter('limit', PHP_INT_MAX); | ||||
$engine = id(new ManiphestTaskSearchEngine()) | $engine = id(new ManiphestTaskSearchEngine()) | ||||
->setViewer($user); | ->setViewer($viewer); | ||||
$query = $engine->buildQueryFromSavedQuery($saved); | $query = $engine->buildQueryFromSavedQuery($saved); | ||||
$query->setViewer($user); | $query->setViewer($viewer); | ||||
$tasks = $query->execute(); | $tasks = $query->execute(); | ||||
$all_projects = array_mergev(mpull($tasks, 'getProjectPHIDs')); | $all_projects = array_mergev(mpull($tasks, 'getProjectPHIDs')); | ||||
$all_assigned = mpull($tasks, 'getOwnerPHID'); | $all_assigned = mpull($tasks, 'getOwnerPHID'); | ||||
$handles = id(new PhabricatorHandleQuery()) | $handles = id(new PhabricatorHandleQuery()) | ||||
->setViewer($user) | ->setViewer($viewer) | ||||
->withPHIDs(array_merge($all_projects, $all_assigned)) | ->withPHIDs(array_merge($all_projects, $all_assigned)) | ||||
->execute(); | ->execute(); | ||||
$workbook = new PHPExcel(); | $workbook = new PHPExcel(); | ||||
$format->buildWorkbook($workbook, $tasks, $handles, $user); | $format->buildWorkbook($workbook, $tasks, $handles, $viewer); | ||||
$writer = PHPExcel_IOFactory::createWriter($workbook, 'Excel2007'); | $writer = PHPExcel_IOFactory::createWriter($workbook, 'Excel2007'); | ||||
ob_start(); | ob_start(); | ||||
$writer->save('php://output'); | $writer->save('php://output'); | ||||
$data = ob_get_clean(); | $data = ob_get_clean(); | ||||
$mime = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'; | $mime = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'; | ||||
return id(new AphrontFileResponse()) | return id(new AphrontFileResponse()) | ||||
->setMimeType($mime) | ->setMimeType($mime) | ||||
->setDownload($format->getFileName().'.xlsx') | ->setDownload($format->getFileName().'.xlsx') | ||||
->setContent($data); | ->setContent($data); | ||||
} | } | ||||
} | } |
Or $dialog = $this->newDialog(); to automatically set the user.