Advertisement
Guest User

Untitled

a guest
May 16th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.00 KB | None | 0 0
  1.     public function downloadJournalRecordAction()
  2.     {
  3.         $journalRecordId = $this->params()->fromRoute()['house'];
  4.  
  5.         //$dbConn = pg_connect ("host=monitor.reformagkh.ru port=54325 dbname=rgkh_migration_1206_logs user=readonlyuser password=tralivali");
  6.         $myDbConn = pg_connect ("host=rgkh-dev-postgres port=5432 dbname=mylogs user=postgreadmin password=1");
  7.  
  8.         $sql = <<<SQL
  9. SELECT
  10.     jr.object_type_id AS object_type_id,
  11.     ot.table_name     AS object_type_name,
  12.     jr.action         AS action,
  13.     jr.user_id        AS user_id,
  14.     jr.created_at     AS created_at,
  15.     jr.changes        AS changes,
  16.     jr.context        AS context,
  17.     jr.object_pk      AS object_pk,
  18.     jr.id             AS id
  19. FROM journal.journal_record jr
  20.    JOIN journal.journal_object_type ot ON ot.id = jr.object_type_id
  21. WHERE jr.id = $journalRecordId
  22. SQL;
  23.  
  24.         $query = pg_query($myDbConn, $sql);
  25.         $result = pg_fetch_all($query);
  26.  
  27.         $csvFile = fopen('php://temp', 'r+');
  28.         fputcsv($csvFile, ['object_type_id','object_type_name','action','user_id','created_at','changes','context','object_pk','id']);
  29.  
  30.         foreach($result as $row) {
  31.             fputcsv($csvFile, $row);
  32.         }
  33.  
  34.         $response = new Stream();
  35.         $response->setStream($csvFile);
  36.         $response->setStatusCode(200);
  37.         $response->setStreamName('journalRecord.csv');
  38.         $headers = new Headers();
  39.         $headers->addHeaders([
  40. //            'Content-Disposition' => 'attachment; filename=journalRecord.csv',
  41. //            'Content-Type'        => 'application/octet-stream',
  42. //            'Expires'             => '@0', // @0, because zf2 parses date as string to \DateTime() object
  43. //            'Cache-Control'       => 'must-revalidate',
  44. //            'Pragma'              => 'public',
  45.         ]);
  46.         $response->setHeaders($headers);
  47.  
  48.  
  49.  
  50.         fclose($csvFile);
  51.         pg_close($myDbConn);
  52.  
  53.         //return $response;
  54.         return $this->jsonSuccess();
  55.  
  56.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement