|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Export to csv - file. |
|
5
|
|
|
* |
|
6
|
|
|
* @package Model |
|
7
|
|
|
* |
|
8
|
|
|
* @copyright YetiForce S.A. |
|
9
|
|
|
* @license YetiForce Public License 3.0 (licenses/LicenseEN.txt or yetiforce.com) |
|
10
|
|
|
* @author Adrian Kon <[email protected]> |
|
11
|
|
|
* @author Radosław Skrzypczak <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Export to csv - class. |
|
16
|
|
|
*/ |
|
17
|
|
|
class Vtiger_ExportToCsv_Model extends \App\Export\ExportRecords |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var string File extension. */ |
|
20
|
|
|
protected $fileExtension = 'csv'; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Function exports the data based on the mode. |
|
24
|
|
|
* |
|
25
|
|
|
* @return void |
|
26
|
|
|
*/ |
|
27
|
|
|
public function exportData(): void |
|
28
|
|
|
{ |
|
29
|
|
|
$this->output($this->getHeaders(), $this->getEntries()); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Get entires. |
|
34
|
|
|
* |
|
35
|
|
|
* @return array |
|
36
|
|
|
*/ |
|
37
|
|
|
public function getEntries(): array |
|
38
|
|
|
{ |
|
39
|
|
|
$entries = []; |
|
40
|
|
|
$addInventoryData = $this->fullData && $this->moduleInstance->isInventory(); |
|
41
|
|
|
if ($addInventoryData) { |
|
42
|
|
|
$inventoryModel = Vtiger_Inventory_Model::getInstance($this->moduleName); |
|
43
|
|
|
$inventoryFields = $inventoryModel->getFields(); |
|
44
|
|
|
$inventoryTable = $inventoryModel->getDataTableName(); |
|
45
|
|
|
} |
|
46
|
|
|
$rowsCounter = 0; |
|
47
|
|
|
$dataReader = $this->getExportQuery()->createCommand()->query(); |
|
48
|
|
|
while ($row = $dataReader->read()) { |
|
49
|
|
|
$sanitizedRow = $this->sanitizeValues($row); |
|
50
|
|
|
if ($addInventoryData) { |
|
51
|
|
|
$sanitizedRow[] = $rowsCounter++; |
|
52
|
|
|
$rows = (new \App\Db\Query())->from($inventoryTable)->where(['crmid' => $row['id']])->orderBy('seq')->all(); |
|
|
|
|
|
|
53
|
|
|
if ($rows) { |
|
54
|
|
|
foreach ($rows as &$row) { |
|
55
|
|
|
$sanitizedInventoryRow = $this->sanitizeInventoryValues($row, $inventoryFields); |
|
|
|
|
|
|
56
|
|
|
$entries[] = array_merge($sanitizedRow, $sanitizedInventoryRow); |
|
57
|
|
|
} |
|
58
|
|
|
} else { |
|
59
|
|
|
$entries[] = $sanitizedRow; |
|
60
|
|
|
} |
|
61
|
|
|
} else { |
|
62
|
|
|
$entries[] = $sanitizedRow; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
$dataReader->close(); |
|
66
|
|
|
|
|
67
|
|
|
return $entries; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Function that create the exported file. |
|
72
|
|
|
* |
|
73
|
|
|
* @param array $headers - output file header |
|
74
|
|
|
* @param array $entries - output file data |
|
75
|
|
|
*/ |
|
76
|
|
|
public function output(array $headers, array $entries) |
|
77
|
|
|
{ |
|
78
|
|
|
$output = fopen('php://output', 'w'); |
|
79
|
|
|
fputcsv($output, $headers); |
|
80
|
|
|
foreach ($entries as $row) { |
|
81
|
|
|
fputcsv($output, $row); |
|
82
|
|
|
} |
|
83
|
|
|
fclose($output); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|