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();
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
$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);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
$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();
|
|
|
|
$sheet = $workbook->setActiveSheetIndex(0);
|
2013-03-13 07:30:03 +01:00
|
|
|
$sheet->setTitle(pht('Tasks'));
|
2012-02-29 06:07:12 +01:00
|
|
|
|
|
|
|
$widths = array(
|
|
|
|
null,
|
2012-03-02 02:23:29 +01:00
|
|
|
15,
|
2012-02-29 06:07:12 +01:00
|
|
|
null,
|
2012-03-02 02:23:29 +01:00
|
|
|
10,
|
2012-02-29 06:07:12 +01:00
|
|
|
15,
|
2012-03-02 02:23:29 +01:00
|
|
|
15,
|
|
|
|
60,
|
2012-02-29 06:07:12 +01:00
|
|
|
30,
|
2012-03-02 02:23:29 +01:00
|
|
|
20,
|
|
|
|
100,
|
2012-02-29 06:07:12 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
foreach ($widths as $col => $width) {
|
|
|
|
if ($width !== null) {
|
2012-03-02 02:23:29 +01:00
|
|
|
$sheet->getColumnDimension($this->col($col))->setWidth($width);
|
2012-02-29 06:07:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$status_map = ManiphestTaskStatus::getTaskStatusMap();
|
|
|
|
$pri_map = ManiphestTaskPriority::getTaskPriorityMap();
|
|
|
|
|
2012-03-02 02:23:29 +01:00
|
|
|
$date_format = null;
|
|
|
|
|
2012-02-29 06:07:12 +01:00
|
|
|
$rows = array();
|
|
|
|
$rows[] = array(
|
2013-03-13 07:30:03 +01:00
|
|
|
pht('ID'),
|
|
|
|
pht('Owner'),
|
|
|
|
pht('Status'),
|
|
|
|
pht('Priority'),
|
|
|
|
pht('Date Created'),
|
|
|
|
pht('Date Updated'),
|
|
|
|
pht('Title'),
|
|
|
|
pht('Projects'),
|
|
|
|
pht('URI'),
|
|
|
|
pht('Description'),
|
2012-02-29 06:07:12 +01:00
|
|
|
);
|
2012-03-02 02:23:29 +01:00
|
|
|
|
|
|
|
$is_date = array(
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
2012-02-29 06:07:12 +01:00
|
|
|
);
|
|
|
|
|
2012-03-02 02:23:29 +01:00
|
|
|
$header_format = array(
|
|
|
|
'font' => array(
|
|
|
|
'bold' => true,
|
|
|
|
),
|
|
|
|
);
|
2012-02-29 06:07:12 +01:00
|
|
|
|
|
|
|
foreach ($tasks as $task) {
|
|
|
|
$task_owner = null;
|
|
|
|
if ($task->getOwnerPHID()) {
|
|
|
|
$task_owner = $handles[$task->getOwnerPHID()]->getName();
|
|
|
|
}
|
|
|
|
|
|
|
|
$projects = array();
|
|
|
|
foreach ($task->getProjectPHIDs() as $phid) {
|
|
|
|
$projects[] = $handles[$phid]->getName();
|
|
|
|
}
|
|
|
|
$projects = implode(', ', $projects);
|
|
|
|
|
|
|
|
$rows[] = array(
|
|
|
|
'T'.$task->getID(),
|
|
|
|
$task_owner,
|
|
|
|
idx($status_map, $task->getStatus(), '?'),
|
|
|
|
idx($pri_map, $task->getPriority(), '?'),
|
|
|
|
$this->computeExcelDate($task->getDateCreated()),
|
|
|
|
$this->computeExcelDate($task->getDateModified()),
|
|
|
|
$task->getTitle(),
|
|
|
|
$projects,
|
|
|
|
PhabricatorEnv::getProductionURI('/T'.$task->getID()),
|
2012-03-02 02:23:29 +01:00
|
|
|
phutil_utf8_shorten($task->getDescription(), 512),
|
2012-02-29 06:07:12 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($rows as $row => $cols) {
|
|
|
|
foreach ($cols as $col => $spec) {
|
2012-03-02 02:23:29 +01:00
|
|
|
$cell_name = $this->col($col).($row + 1);
|
2013-01-21 19:11:35 +01:00
|
|
|
$sheet
|
|
|
|
->setCellValue($cell_name, $spec, $return_cell = true)
|
|
|
|
->setDataType(PHPExcel_Cell_DataType::TYPE_STRING);
|
2012-03-02 02:23:29 +01:00
|
|
|
|
2012-02-29 06:07:12 +01:00
|
|
|
if ($row == 0) {
|
2012-03-02 02:23:29 +01:00
|
|
|
$sheet->getStyle($cell_name)->applyFromArray($header_format);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($is_date[$col]) {
|
|
|
|
$code = PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDD2;
|
|
|
|
$sheet
|
|
|
|
->getStyle($cell_name)
|
|
|
|
->getNumberFormat()
|
|
|
|
->setFormatCode($code);
|
2012-02-29 06:07:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
->setDownload('maniphest_tasks_'.date('Ymd').'.xlsx')
|
2012-02-29 06:07:12 +01:00
|
|
|
->setContent($data);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function computeExcelDate($epoch) {
|
|
|
|
$seconds_per_day = (60 * 60 * 24);
|
|
|
|
$offset = ($seconds_per_day * 25569);
|
|
|
|
|
|
|
|
return ($epoch + $offset) / $seconds_per_day;
|
|
|
|
}
|
|
|
|
|
2012-03-02 02:23:29 +01:00
|
|
|
private function col($n) {
|
|
|
|
return chr(ord('A') + $n);
|
|
|
|
}
|
|
|
|
|
2012-02-29 06:07:12 +01:00
|
|
|
}
|