Skip to content

Commit

Permalink
Improved ExportManager to reduce source code and allow better customi…
Browse files Browse the repository at this point in the history
…zation.
  • Loading branch information
NeoRazorX committed Oct 5, 2019
1 parent 9056cb4 commit 8b77eff
Show file tree
Hide file tree
Showing 18 changed files with 196 additions and 162 deletions.
2 changes: 1 addition & 1 deletion Core/Controller/AccountingReports.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ protected function exportData(&$pages, $format)

foreach ($pages as $data) {
$headers = empty($data) ? [] : array_keys($data[0]);
$this->exportManager->generateTablePage($headers, $data);
$this->exportManager->addTablePage($headers, $data);
}

$this->exportManager->show($this->response);
Expand Down
18 changes: 4 additions & 14 deletions Core/Controller/EditRegularizacionImpuesto.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,21 +211,11 @@ protected function disableButtons($viewName)
*/
protected function execAfterAction($action)
{
switch ($action) {
case 'export':
$this->setTemplate(false);
$this->exportManager->newDoc($this->request->get('option', ''));
$this->exportManager->setOrientation('landscape');
foreach ($this->views as $selectedView) {
$selectedView->export($this->exportManager);
}
$this->exportManager->show($this->response);
break;

default:
parent::execAfterAction($action);
break;
if ($action === 'export') {
$this->exportManager->setOrientation('landscape');
}

parent::execAfterAction($action);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion Core/Lib/Accounting/AccountingPlanExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function exportCSV($code)
$rows = array_merge($this->getAccountsData($code), $this->getSubaccountsData($code));

$csvExport = new CSVExport();
$csvExport->generateTablePage($columns, $rows);
$csvExport->addTablePage($columns, $rows);
return $csvExport->getDoc();
}

Expand Down
31 changes: 25 additions & 6 deletions Core/Lib/Export/CSVExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
use Symfony\Component\HttpFoundation\Response;

/**
* Class to export to CSV
* Follow the XLSExport style to have a more uniform code
* Class to export data to CSV format.
*
* @author Carlos García Gómez <carlos@facturascripts.com>
*/
Expand Down Expand Up @@ -59,8 +58,10 @@ class CSVExport implements ExportInterface
* Adds a new page with the document data.
*
* @param BusinessDocument $model
*
* @return bool
*/
public function generateBusinessDocPage($model)
public function addBusinessDocPage($model): bool
{
$tableData = [];
foreach ((array) $model as $key => $value) {
Expand All @@ -73,6 +74,9 @@ public function generateBusinessDocPage($model)
}

$this->writeSheet($tableData, ['key' => 'string', 'value' => 'string']);

/// do not continue with export
return false;
}

/**
Expand All @@ -84,8 +88,10 @@ public function generateBusinessDocPage($model)
* @param int $offset
* @param array $columns
* @param string $title
*
* @return bool
*/
public function generateListModelPage($model, $where, $order, $offset, $columns, $title = '')
public function addListModelPage($model, $where, $order, $offset, $columns, $title = ''): bool
{
$tableCols = [];
$sheetHeaders = [];
Expand Down Expand Up @@ -113,6 +119,9 @@ public function generateListModelPage($model, $where, $order, $offset, $columns,
$offset += self::LIST_LIMIT;
$cursor = $model->all($where, $order, $offset, self::LIST_LIMIT);
}

/// do not continue with export
return false;
}

/**
Expand All @@ -121,8 +130,10 @@ public function generateListModelPage($model, $where, $order, $offset, $columns,
* @param ModelClass $model
* @param array $columns
* @param string $title
*
* @return bool
*/
public function generateModelPage($model, $columns, $title = '')
public function addModelPage($model, $columns, $title = ''): bool
{
$tableData = [];
foreach ((array) $model as $key => $value) {
Expand All @@ -135,15 +146,20 @@ public function generateModelPage($model, $columns, $title = '')
}

$this->writeSheet($tableData, ['key' => 'string', 'value' => 'string']);

/// do not continue with export
return false;
}

/**
* Adds a new page with the table.
*
* @param array $headers
* @param array $rows
*
* @return bool
*/
public function generateTablePage($headers, $rows)
public function addTablePage($headers, $rows): bool
{
/// fix headers
foreach ($headers as $key => $value) {
Expand All @@ -163,6 +179,9 @@ public function generateTablePage($headers, $rows)
$body[] = \implode($this->separator, $row);
}
$this->csv[] = \implode(PHP_EOL, $body);

/// do not continue with export
return false;
}

/**
Expand Down
35 changes: 5 additions & 30 deletions Core/Lib/Export/ExportInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@
*/
namespace FacturaScripts\Core\Lib\Export;

use FacturaScripts\Core\Base\DataBase\DataBaseWhere;
use FacturaScripts\Core\Model\Base\BusinessDocument;
use FacturaScripts\Core\Model\Base\ModelClass;
use Symfony\Component\HttpFoundation\Response;

/**
Expand All @@ -33,63 +30,41 @@ interface ExportInterface

/**
* Adds a new page with the document data.
*
* @param BusinessDocument $model
*/
public function generateBusinessDocPage($model);
public function addBusinessDocPage($model): bool;

/**
* Adds a new page with a table listing the models data.
*
* @param ModelClass $model
* @param DataBaseWhere[] $where
* @param array $order
* @param int $offset
* @param array $columns
* @param string $title
*/
public function generateListModelPage($model, $where, $order, $offset, $columns, $title = '');
public function addListModelPage($model, $where, $order, $offset, $columns, $title = ''): bool;

/**
* Adds a new page with the model data.
*
* @param ModelClass $model
* @param array $columns
* @param string $title
*/
public function generateModelPage($model, $columns, $title = '');
public function addModelPage($model, $columns, $title = ''): bool;

/**
* Adds a new page with the table.
*
* @param array $headers
* @param array $rows
*/
public function generateTablePage($headers, $rows);
public function addTablePage($headers, $rows): bool;

/**
* Return the full document.
*
* @return mixed
*/
public function getDoc();

/**
* Blank document.
*/
public function newDoc();

/**
* Sets default orientation.
*
* @param string $orientation
*/
public function setOrientation(string $orientation);

/**
* Set headers and output document content to response.
*
* @param Response $response
*/
public function show(Response &$response);
}
34 changes: 19 additions & 15 deletions Core/Lib/Export/MAILExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,29 @@ class MAILExport extends PDFExport
/**
*
* @param BusinessDocument $model
*
* @return bool
*/
public function addBusinessDocPage($model): bool
{
$this->sendParams['modelClassName'] = $model->modelClassName();
$this->sendParams['modelCode'] = $model->primaryColumnValue();
return parent::addBusinessDocPage($model);
}

/**
*
* @param ModelClass $model
* @param array $columns
* @param string $title
*
* @return bool
*/
public function generateBusinessDocPage($model)
public function addModelPage($model, $columns, $title = ''): bool
{
parent::generateBusinessDocPage($model);
$this->sendParams['modelClassName'] = $model->modelClassName();
$this->sendParams['modelCode'] = $model->primaryColumnValue();
return parent::addModelPage($model, $columns, $title);
}

/**
Expand All @@ -61,19 +78,6 @@ public function getDoc()
return $this->pdf->ezOutput();
}

/**
*
* @param ModelClass $model
* @param array $columns
* @param string $title
*/
public function generateModelPage($model, $columns, $title = '')
{
parent::generateModelPage($model, $columns, $title);
$this->sendParams['modelClassName'] = $model->modelClassName();
$this->sendParams['modelCode'] = $model->primaryColumnValue();
}

/**
*
* @param Response $response
Expand Down
22 changes: 18 additions & 4 deletions Core/Lib/Export/PDFExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ class PDFExport extends PDFDocument implements ExportInterface
* Adds a new page with the document data.
*
* @param BusinessDocument $model
*
* @return bool
*/
public function generateBusinessDocPage($model)
public function addBusinessDocPage($model): bool
{
$this->format = $this->getDocumentFormat($model);

Expand All @@ -50,6 +52,9 @@ public function generateBusinessDocPage($model)
$this->insertBusinessDocHeader($model);
$this->insertBusinessDocBody($model);
$this->insertBusinessDocFooter($model);

/// do not continue with export
return false;
}

/**
Expand All @@ -61,8 +66,10 @@ public function generateBusinessDocPage($model)
* @param int $offset
* @param array $columns
* @param string $title
*
* @return bool
*/
public function generateListModelPage($model, $where, $order, $offset, $columns, $title = '')
public function addListModelPage($model, $where, $order, $offset, $columns, $title = ''): bool
{
$orientation = 'portrait';
$tableCols = [];
Expand Down Expand Up @@ -98,6 +105,7 @@ public function generateListModelPage($model, $where, $order, $offset, $columns,

$this->newLongTitles($longTitles, $tableColsTitle);
$this->insertFooter();
return true;
}

/**
Expand All @@ -106,8 +114,10 @@ public function generateListModelPage($model, $where, $order, $offset, $columns,
* @param ModelClass $model
* @param array $columns
* @param string $title
*
* @return bool
*/
public function generateModelPage($model, $columns, $title = '')
public function addModelPage($model, $columns, $title = ''): bool
{
$this->newPage();
$this->insertHeader();
Expand Down Expand Up @@ -137,15 +147,18 @@ public function generateModelPage($model, $columns, $title = '')

$this->insertParalellTable($tableDataAux, '', $tableOptions);
$this->insertFooter();
return true;
}

/**
* Adds a new page with the table.
*
* @param array $headers
* @param array $rows
*
* @return bool
*/
public function generateTablePage($headers, $rows)
public function addTablePage($headers, $rows): bool
{
$orientation = count($headers) > 5 ? 'landscape' : 'portrait';
$this->newPage($orientation);
Expand All @@ -154,6 +167,7 @@ public function generateTablePage($headers, $rows)
$this->insertHeader();
$this->pdf->ezTable($rows, $headers, '', $tableOptions);
$this->insertFooter();
return true;
}

/**
Expand Down

0 comments on commit 8b77eff

Please sign in to comment.