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.
|
|
|
|
|
2012-02-29 06:07:12 +01:00
|
|
|
$query = id(new PhabricatorSearchQuery())->loadOneWhere(
|
|
|
|
'queryKey = %s',
|
|
|
|
$this->key);
|
|
|
|
if (!$query) {
|
|
|
|
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-04-11 20:22:06 +02:00
|
|
|
$form = id(new AphrontFormLayoutView())
|
|
|
|
->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.');
|
|
|
|
}
|
|
|
|
|
2012-02-29 06:07:12 +01:00
|
|
|
$query->setParameter('limit', null);
|
|
|
|
$query->setParameter('offset', null);
|
|
|
|
$query->setParameter('order', 'p');
|
|
|
|
$query->setParameter('group', 'n');
|
|
|
|
|
2013-03-01 02:15:09 +01:00
|
|
|
list($tasks, $handles) = ManiphestTaskListController::loadTasks(
|
|
|
|
$query,
|
|
|
|
$user);
|
2012-02-29 06:07:12 +01:00
|
|
|
// Ungroup tasks.
|
|
|
|
$tasks = array_mergev($tasks);
|
|
|
|
|
|
|
|
$all_projects = array_mergev(mpull($tasks, 'getProjectPHIDs'));
|
2012-09-05 04:02:56 +02:00
|
|
|
$project_handles = $this->loadViewerHandles($all_projects);
|
2012-02-29 06:07:12 +01:00
|
|
|
$handles += $project_handles;
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|