2012-02-29 06:07:12 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group maniphest
|
|
|
|
*/
|
|
|
|
final class ManiphestExportController extends ManiphestController {
|
|
|
|
|
|
|
|
private $key;
|
|
|
|
|
|
|
|
public function willProcessRequest(array $data) {
|
|
|
|
$this->key = $data['key'];
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-03-02 02:23:29 +01:00
|
|
|
* @phutil-external-symbol class PHPExcel
|
|
|
|
* @phutil-external-symbol class PHPExcel_IOFactory
|
|
|
|
* @phutil-external-symbol class PHPExcel_Style_NumberFormat
|
2013-01-21 19:11:35 +01:00
|
|
|
* @phutil-external-symbol class PHPExcel_Cell_DataType
|
2012-02-29 06:07:12 +01:00
|
|
|
*/
|
|
|
|
public function processRequest() {
|
|
|
|
$request = $this->getRequest();
|
|
|
|
$user = $request->getUser();
|
|
|
|
|
2012-03-02 02:23:29 +01:00
|
|
|
$ok = @include_once 'PHPExcel.php';
|
2012-02-29 06:07:12 +01:00
|
|
|
if (!$ok) {
|
|
|
|
$dialog = new AphrontDialogView();
|
|
|
|
$dialog->setUser($user);
|
|
|
|
|
2013-03-13 07:30:03 +01:00
|
|
|
$inst1 = pht(
|
|
|
|
'This system does not have PHPExcel installed. This software '.
|
2012-03-02 02:23:29 +01:00
|
|
|
'component is required to export tasks to Excel. Have your system '.
|
2013-03-13 07:30:03 +01:00
|
|
|
'administrator install it from:');
|
|
|
|
|
|
|
|
$inst2 = pht(
|
|
|
|
'Your PHP "include_path" needs to be updated to include the '.
|
|
|
|
'PHPExcel Classes directory.');
|
|
|
|
|
|
|
|
$dialog->setTitle(pht('Excel Export Not Configured'));
|
|
|
|
$dialog->appendChild(hsprintf(
|
|
|
|
'<p>%s</p>'.
|
2012-02-29 06:07:12 +01:00
|
|
|
'<br />'.
|
2012-03-02 02:23:29 +01:00
|
|
|
'<p>'.
|
|
|
|
'<a href="http://www.phpexcel.net/">http://www.phpexcel.net/</a>'.
|
|
|
|
'</p>'.
|
|
|
|
'<br />'.
|
2013-03-13 07:30:03 +01:00
|
|
|
'<p>%s</p>',
|
|
|
|
$inst1,
|
|
|
|
$inst2));
|
2012-02-29 06:07:12 +01:00
|
|
|
|
|
|
|
$dialog->addCancelButton('/maniphest/');
|
|
|
|
return id(new AphrontDialogResponse())->setDialog($dialog);
|
|
|
|
}
|
|
|
|
|
2013-01-21 19:11:35 +01:00
|
|
|
// 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.
|
|
|
|
|
2013-09-13 18:35:39 +02:00
|
|
|
$saved = id(new PhabricatorSavedQueryQuery())
|
|
|
|
->setViewer($user)
|
|
|
|
->withQueryKeys(array($this->key))
|
|
|
|
->executeOne();
|
|
|
|
if (!$saved) {
|
2012-02-29 06:07:12 +01:00
|
|
|
return new Aphront404Response();
|
|
|
|
}
|
|
|
|
|
2013-04-11 20:22:06 +02:00
|
|
|
$formats = ManiphestExcelFormat::loadAllFormats();
|
|
|
|
$export_formats = array();
|
|
|
|
foreach ($formats as $format_class => $format_object) {
|
|
|
|
$export_formats[$format_class] = $format_object->getName();
|
|
|
|
}
|
|
|
|
|
2012-02-29 06:07:12 +01:00
|
|
|
if (!$request->isDialogFormPost()) {
|
|
|
|
$dialog = new AphrontDialogView();
|
|
|
|
$dialog->setUser($user);
|
|
|
|
|
2013-03-13 07:30:03 +01:00
|
|
|
$dialog->setTitle(pht('Export Tasks to Excel'));
|
2013-02-13 23:50:15 +01:00
|
|
|
$dialog->appendChild(phutil_tag('p', array(), pht(
|
|
|
|
'Do you want to export the query results to Excel?')));
|
2012-02-29 06:07:12 +01:00
|
|
|
|
2013-08-26 20:53:11 +02:00
|
|
|
$form = id(new PHUIFormLayoutView())
|
2013-04-11 20:22:06 +02:00
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormSelectControl())
|
|
|
|
->setLabel(pht('Format:'))
|
|
|
|
->setName("excel-format")
|
|
|
|
->setOptions($export_formats));
|
|
|
|
|
|
|
|
$dialog->appendChild($form);
|
|
|
|
|
2012-02-29 06:07:12 +01:00
|
|
|
$dialog->addCancelButton('/maniphest/');
|
2013-03-13 07:30:03 +01:00
|
|
|
$dialog->addSubmitButton(pht('Export to Excel'));
|
2012-02-29 06:07:12 +01:00
|
|
|
return id(new AphrontDialogResponse())->setDialog($dialog);
|
|
|
|
}
|
|
|
|
|
2013-04-11 20:22:06 +02:00
|
|
|
$format = idx($formats, $request->getStr("excel-format"));
|
|
|
|
if ($format === null) {
|
|
|
|
throw new Exception('Excel format object not found.');
|
|
|
|
}
|
|
|
|
|
2013-09-13 18:35:39 +02:00
|
|
|
$saved->makeEphemeral();
|
|
|
|
$saved->setParameter('limit', PHP_INT_MAX);
|
|
|
|
|
|
|
|
$engine = id(new ManiphestTaskSearchEngine())
|
|
|
|
->setViewer($user);
|
2012-02-29 06:07:12 +01:00
|
|
|
|
2013-09-13 18:35:39 +02:00
|
|
|
$query = $engine->buildQueryFromSavedQuery($saved);
|
|
|
|
$query->setViewer($user);
|
|
|
|
$tasks = $query->execute();
|
2012-02-29 06:07:12 +01:00
|
|
|
|
|
|
|
$all_projects = array_mergev(mpull($tasks, 'getProjectPHIDs'));
|
2013-09-13 18:35:39 +02:00
|
|
|
$all_assigned = mpull($tasks, 'getOwnerPHID');
|
|
|
|
|
|
|
|
$handles = id(new PhabricatorHandleQuery())
|
|
|
|
->setViewer($user)
|
|
|
|
->withPHIDs(array_merge($all_projects, $all_assigned))
|
|
|
|
->execute();
|
2012-02-29 06:07:12 +01:00
|
|
|
|
2012-03-02 02:23:29 +01:00
|
|
|
$workbook = new PHPExcel();
|
2013-04-11 20:22:06 +02:00
|
|
|
$format->buildWorkbook($workbook, $tasks, $handles, $user);
|
2012-03-02 02:23:29 +01:00
|
|
|
$writer = PHPExcel_IOFactory::createWriter($workbook, 'Excel2007');
|
|
|
|
|
2012-02-29 06:07:12 +01:00
|
|
|
ob_start();
|
2012-03-02 02:23:29 +01:00
|
|
|
$writer->save('php://output');
|
2012-02-29 06:07:12 +01:00
|
|
|
$data = ob_get_clean();
|
|
|
|
|
2012-03-02 02:23:29 +01:00
|
|
|
$mime = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
|
|
|
|
|
2012-02-29 06:07:12 +01:00
|
|
|
return id(new AphrontFileResponse())
|
2012-03-02 02:23:29 +01:00
|
|
|
->setMimeType($mime)
|
2013-04-11 20:22:06 +02:00
|
|
|
->setDownload($format->getFileName().'.xlsx')
|
2012-02-29 06:07:12 +01:00
|
|
|
->setContent($data);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|