[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/maniphest/controller/ -> ManiphestExportController.php (source)

   1  <?php
   2  
   3  final class ManiphestExportController extends ManiphestController {
   4  
   5    private $key;
   6  
   7    public function willProcessRequest(array $data) {
   8      $this->key = $data['key'];
   9      return $this;
  10    }
  11  
  12    /**
  13     * @phutil-external-symbol class PHPExcel
  14     * @phutil-external-symbol class PHPExcel_IOFactory
  15     * @phutil-external-symbol class PHPExcel_Style_NumberFormat
  16     * @phutil-external-symbol class PHPExcel_Cell_DataType
  17     */
  18    public function processRequest() {
  19      $request = $this->getRequest();
  20      $user = $request->getUser();
  21  
  22      $ok = @include_once 'PHPExcel.php';
  23      if (!$ok) {
  24        $dialog = new AphrontDialogView();
  25        $dialog->setUser($user);
  26  
  27        $inst1 = pht(
  28          'This system does not have PHPExcel installed. This software '.
  29          'component is required to export tasks to Excel. Have your system '.
  30          'administrator install it from:');
  31  
  32        $inst2 = pht(
  33          'Your PHP "include_path" needs to be updated to include the '.
  34          'PHPExcel Classes directory.');
  35  
  36        $dialog->setTitle(pht('Excel Export Not Configured'));
  37        $dialog->appendChild(hsprintf(
  38          '<p>%s</p>'.
  39          '<br />'.
  40          '<p>'.
  41            '<a href="http://www.phpexcel.net/">http://www.phpexcel.net/</a>'.
  42          '</p>'.
  43          '<br />'.
  44          '<p>%s</p>',
  45          $inst1,
  46          $inst2));
  47  
  48        $dialog->addCancelButton('/maniphest/');
  49        return id(new AphrontDialogResponse())->setDialog($dialog);
  50      }
  51  
  52      // TODO: PHPExcel has a dependency on the PHP zip extension. We should test
  53      // for that here, since it fatals if we don't have the ZipArchive class.
  54  
  55      $saved = id(new PhabricatorSavedQueryQuery())
  56        ->setViewer($user)
  57        ->withQueryKeys(array($this->key))
  58        ->executeOne();
  59      if (!$saved) {
  60        $engine = id(new ManiphestTaskSearchEngine())
  61          ->setViewer($user);
  62        if ($engine->isBuiltinQuery($this->key)) {
  63          $saved = $engine->buildSavedQueryFromBuiltin($this->key);
  64        }
  65        if (!$saved) {
  66          return new Aphront404Response();
  67        }
  68      }
  69  
  70      $formats = ManiphestExcelFormat::loadAllFormats();
  71      $export_formats = array();
  72      foreach ($formats as $format_class => $format_object) {
  73        $export_formats[$format_class] = $format_object->getName();
  74      }
  75  
  76      if (!$request->isDialogFormPost()) {
  77        $dialog = new AphrontDialogView();
  78        $dialog->setUser($user);
  79  
  80        $dialog->setTitle(pht('Export Tasks to Excel'));
  81        $dialog->appendChild(phutil_tag('p', array(), pht(
  82          'Do you want to export the query results to Excel?')));
  83  
  84        $form = id(new PHUIFormLayoutView())
  85          ->appendChild(
  86            id(new AphrontFormSelectControl())
  87              ->setLabel(pht('Format:'))
  88              ->setName('excel-format')
  89              ->setOptions($export_formats));
  90  
  91        $dialog->appendChild($form);
  92  
  93        $dialog->addCancelButton('/maniphest/');
  94        $dialog->addSubmitButton(pht('Export to Excel'));
  95        return id(new AphrontDialogResponse())->setDialog($dialog);
  96      }
  97  
  98      $format = idx($formats, $request->getStr('excel-format'));
  99      if ($format === null) {
 100        throw new Exception('Excel format object not found.');
 101      }
 102  
 103      $saved->makeEphemeral();
 104      $saved->setParameter('limit', PHP_INT_MAX);
 105  
 106      $engine = id(new ManiphestTaskSearchEngine())
 107        ->setViewer($user);
 108  
 109      $query = $engine->buildQueryFromSavedQuery($saved);
 110      $query->setViewer($user);
 111      $tasks = $query->execute();
 112  
 113      $all_projects = array_mergev(mpull($tasks, 'getProjectPHIDs'));
 114      $all_assigned = mpull($tasks, 'getOwnerPHID');
 115  
 116      $handles = id(new PhabricatorHandleQuery())
 117        ->setViewer($user)
 118        ->withPHIDs(array_merge($all_projects, $all_assigned))
 119        ->execute();
 120  
 121      $workbook = new PHPExcel();
 122      $format->buildWorkbook($workbook, $tasks, $handles, $user);
 123      $writer = PHPExcel_IOFactory::createWriter($workbook, 'Excel2007');
 124  
 125      ob_start();
 126      $writer->save('php://output');
 127      $data = ob_get_clean();
 128  
 129      $mime = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
 130  
 131      return id(new AphrontFileResponse())
 132        ->setMimeType($mime)
 133        ->setDownload($format->getFileName().'.xlsx')
 134        ->setContent($data);
 135    }
 136  
 137  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1