Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
language: php

php:
- 5.4
- 5.5
- 5.6
- 7.0
- 7.1

env:
global:
Expand All @@ -13,10 +13,10 @@ matrix:
fast_finish: true

include:
- php: 5.4
- php: 5.6
env: PHPCS=1 DEFAULT=0

- php: 5.4
- php: 5.6
env: COVERALLS=1 DEFAULT=0

install:
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ A plugin to generate Excel files with CakePHP.

## Requirements

* CakePHP 3.x
* PHP 5.4.16 or greater
* CakePHP 3.6
* PHP 5.6.0 or greater
* Patience

## Installation
Expand Down Expand Up @@ -60,7 +60,7 @@ Finally, you can link to the current page with the .xlsx extension. This assumes
$this->Html->link('Excel file', ['_ext' => 'xlsx']);
```

Inside your view file you will have access to the PHPExcel library with `$this->PhpExcel`. Please see the [PHPExcel](https://github.com/PHPOffice/PHPExcel) documentation for a guide on how to use PHPExcel.
Inside your view file you will have access to the PhpSpreadsheet library with `$this->Spreadsheet`. Please see the [PhpSpreadsheet](https://github.com/PHPOffice/phpspreadsheet) documentation for a guide on how to use PhpSpreadsheet.

## License

Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
}
],
"require": {
"php": ">=5.4.16",
"cakephp/cakephp": "~3.1",
"phpoffice/phpexcel": "~1.8"
"php": ">=5.6.0",
"cakephp/cakephp": "^3.6",
"phpoffice/phpspreadsheet": "^1.2"
},
"require-dev": {
"phpunit/phpunit": "4.1.*"
"phpunit/phpunit": "^5.7.14|^6.0"
},
"autoload": {
"psr-4": {
Expand Down
9 changes: 5 additions & 4 deletions config/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
<?php
use Cake\Event\EventManager;
use Cake\Event\Event;
use Cake\Network\Request;
use Cake\Http\ServerRequest;


EventManager::instance()->on('Controller.initialize', function (Event $event) {
$controller = $event->subject();
$controller = $event->getSubject();
if ($controller->components()->has('RequestHandler')) {
$controller->RequestHandler->config('viewClassMap.xlsx', 'CakeExcel.Excel');
$controller->RequestHandler->setConfig('viewClassMap.xlsx', 'CakeExcel.Excel');
}
});

Request::addDetector('xlsx', [
ServerRequest::addDetector('xlsx', [
'accept' => ['application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'],
'param' => '_ext',
'value' => 'xlsx'
Expand Down
63 changes: 38 additions & 25 deletions src/View/ExcelView.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
<?php
namespace CakeExcel\View;

use Cake\Core\Exception\Exception;
use Cake\Event\EventManager;
use Cake\Network\Request;
use Cake\Network\Response;
use Cake\Utility\Inflector;
use Cake\Http\Response;
use Cake\Http\ServerRequest;
use Cake\Utility\Text;
use Cake\View\View;
use PHPExcel;
use PHPExcel_IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

/**
* @package Cake.View
Expand All @@ -31,10 +30,11 @@ class ExcelView extends View
public $subDir = 'xlsx';

/**
* PHPExcel instance
* @var PhpExcel
* Spreadsheet instance
*
* @var Spreadsheet
*/
public $PhpExcel = null;
public $Spreadsheet = null;

/**
* Constructor
Expand All @@ -45,7 +45,7 @@ class ExcelView extends View
* @param array $viewOptions An array of view options
*/
public function __construct(
Request $request = null,
ServerRequest $request = null,
Response $response = null,
EventManager $eventManager = null,
array $viewOptions = []
Expand All @@ -59,34 +59,51 @@ public function __construct(
if (isset($viewOptions['name']) && $viewOptions['name'] == 'Error') {
$this->subDir = null;
$this->layoutPath = null;
$response->type('html');
$this->response = $this->response->withType('html');

return;
}

if ($response && $response instanceof Response) {
$response->type('xlsx');
$this->response = $this->response->withType('xlsx');
}

$this->Spreadsheet = new Spreadsheet();
}

/**
* Magic accessor for helpers. Backward compatibility for PHPExcel property
*
* @param string $name Name of the attribute to get.
*
* @return mixed
*/
public function __get($name)
{
if ($name === 'PhpExcel') {
return $this->Spreadsheet;
}

$this->PhpExcel = new PHPExcel();
return parent::__get($name);
}

/**
* Render method
*
* @param string $view The view being rendered.
* @param string $layout The layout being rendered.
* @return string The rendered view.
* @param string|false|null $view Name of view file to use
* @param string|null $layout Layout to use.
* @return string|null Rendered content or null if content already rendered and returned earlier.
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
*/
public function render($view = null, $layout = null)
{
$content = parent::render($view, $layout);
if ($this->response->type() == 'text/html') {
if ($this->response->getType() == 'text/html') {
return $content;
}

$this->Blocks->set('content', $this->output());
$this->response->download($this->getFilename());
$this->response = $this->response->withDownload($this->getFilename());

return $this->Blocks->get('content');
}
Expand All @@ -95,17 +112,13 @@ public function render($view = null, $layout = null)
* Generates the binary excel data
*
* @return string
* @throws CakeException If the excel writer does not exist
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
*/
protected function output()
{
ob_start();

$writer = PHPExcel_IOFactory::createWriter($this->PhpExcel, 'Excel2007');

if (!isset($writer)) {
throw new Exception('Excel writer not found');
}
$writer = new Xlsx($this->Spreadsheet);

$writer->setPreCalculateFormulas(false);
$writer->setIncludeCharts(true);
Expand All @@ -127,6 +140,6 @@ public function getFilename()
return $this->viewVars['_filename'] . '.xlsx';
}

return Inflector::slug(str_replace('.xlsx', '', $this->request->url)) . '.xlsx';
return Text::slug(str_replace('.xlsx', '', $this->request->getRequestTarget())) . '.xlsx';
}
}
21 changes: 14 additions & 7 deletions tests/TestCase/View/ExcelViewTest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php
namespace CakeExcel\Test\TestCase\View;

use Cake\Http\Response;
use Cake\Http\ServerRequest;
use CakeExcel\View\ExcelView;
use Cake\Network\Request;
use Cake\Network\Response;
use Cake\TestSuite\TestCase;
use PHPExcel;
use PhpOffice\PhpSpreadsheet\Spreadsheet;

/**
* CsvViewTest
Expand All @@ -15,14 +15,19 @@ class ExcelViewTest extends TestCase

public $fixtures = ['core.Articles', 'core.Authors'];

/**
* @var ExcelView
*/
public $View;

/**
* setup callback
*
* @return void
*/
public function setUp()
{
$this->request = new Request([
$this->request = new ServerRequest([
'params' => [
'plugin' => null,
'controller' => 'posts',
Expand All @@ -48,14 +53,16 @@ public function tearDown()
*/
public function testConstruct()
{
$result = $this->View->response->type();
$result = $this->View->response->getType();
$this->assertEquals('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', $result);
$this->assertTrue($this->View->PhpExcel instanceof PHPExcel);
$this->assertTrue($this->View->Spreadsheet instanceof Spreadsheet);
$this->assertSame($this->View->PhpExcel, $this->View->Spreadsheet);
}

public function testRender()
{
$this->View->name = $this->View->viewPath = 'Posts';
$this->View->setTemplatePath('Posts');
$this->View->name = 'Posts';

$output = $this->View->render('index');
$this->assertSame('504b030414', bin2hex(substr($output, 0, 5)));
Expand Down
2 changes: 1 addition & 1 deletion tests/test_app/TestApp/Template/Posts/xlsx/index.ctp
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<?php
$worksheet = $this->PhpExcel->setActiveSheetIndex(0);
$worksheet = $this->Spreadsheet->setActiveSheetIndex(0);
$worksheet->setCellValueByColumnAndRow(0, 0, 'Test string');