public function __construct($app)
 {
     View::inject($app);
 }
 /**
  * Generates the output of a report for a given type.
  *
  * @param string   $type   html|pdf|csv
  * @param bool     $stream when true, streams the resulting file to the client (pdf, csv only)
  * @param Response $res    when streaming, response object to use
  *
  * @return string|array|false
  */
 public function output($type, $stream = false, Response $res = null)
 {
     // $this->organization->useTimezone();
     $type = strtolower($type);
     if ($type == 'html') {
         $this->htmlOutput = true;
         // NOTE host name has the development port number stripped,
         // otherwise the css is not loaded
         $data = ['css' => 'file://' . INFUSE_PUBLIC_DIR . '/css/report.css', 'header' => $this->getHeader(), 'sections' => $this->getSections()];
         $this->htmlOutput = false;
         $view = new View('report', $data);
         return $view->render();
     } elseif ($type == 'pdf') {
         $html = $this->output('html');
         // Run wkhtmltopdf
         $descriptorspec = [0 => ['pipe', 'r'], 1 => ['pipe', 'w'], 2 => ['pipe', 'w']];
         $process = proc_open(WKHTMLTOPDF_CMD, $descriptorspec, $pipes);
         // Send the HTML on stdin
         fwrite($pipes[0], $html);
         fclose($pipes[0]);
         // Read the outputs
         $pdf = stream_get_contents($pipes[1]);
         $errors = stream_get_contents($pipes[2]);
         // Close the process
         fclose($pipes[1]);
         $return_value = proc_close($process);
         // Handle errors
         if ($errors) {
             error_log($errors);
         }
         // Output the results
         if ($stream) {
             $res->setContentType('application/pdf')->setHeader('Cache-Control', 'public, must-revalidate, max-age=0')->setHeader('Pragma', 'public')->setHeader('Expires', 'Sat, 26 Jul 1997 05:00:00 GMT')->setHeader('Last-Modified', gmdate('D, d M Y H:i:s') . ' GMT')->setHeader('Content-Length', strlen($pdf))->setHeader('Content-Disposition', 'attachment; filename="' . $this->baseFilename() . '.pdf";')->setBody($pdf);
         } else {
             return $pdf;
         }
     } elseif ($type == 'csv') {
         $output = [];
         $header = $this->getHeader();
         foreach ($header as $key => $value) {
             $output[] = [$key, $value];
         }
         $output[] = [];
         $sections = $this->getSections();
         foreach ($sections as $section) {
             if (isset($section['title'])) {
                 $output[] = [$section['title']];
             }
             if (isset($section['keyvalue'])) {
                 foreach ($section['keyvalue'] as $key => $value) {
                     $output[] = [$key, $value];
                 }
                 $output[] = [];
             }
             $entireTable = array_merge([(array) U::array_value($section, 'header')], (array) U::array_value($section, 'rows'), [(array) U::array_value($section, 'footer')]);
             foreach ($entireTable as $row) {
                 $output[] = $row;
             }
             $output[] = [];
         }
         $csv = fopen('php://output', 'w');
         ob_start();
         foreach ($output as $row) {
             fputcsv($csv, $row);
         }
         fclose($csv);
         $output = ob_get_clean();
         if ($stream) {
             $res->setContentType('text/csv')->setHeader('Cache-Control', 'public, must-revalidate, max-age=0')->setHeader('Pragma', 'public')->setHeader('Expires', 'Sat, 26 Jul 1997 05:00:00 GMT')->setHeader('Last-Modified', gmdate('D, d M Y H:i:s') . ' GMT')->setHeader('Content-Length', strlen($output))->setHeader('Content-Disposition', 'attachment; filename="' . $this->baseFilename() . '".csv')->setBody($output);
         } else {
             return $output;
         }
     }
     return false;
 }
 /**
  * Adds in HTML and text values to a message from the template (if not already set).
  *
  * @param array  $message
  * @param string $template
  * @param array  $templateVars
  *
  * @return array
  */
 private function messageFromTemplate(array $message, $template, array $templateVars)
 {
     if (!isset($message['html'])) {
         $htmlTemplate = 'emails/' . $template;
         $htmlView = new View($htmlTemplate, $templateVars);
         $message['html'] = $htmlView->render();
     }
     if (!isset($message['text'])) {
         $textTemplate = 'emails/text/' . $template;
         $textView = new View($textTemplate, $templateVars);
         $message['text'] = $textView->render();
     }
     return $message;
 }