Passed
Push — developer ( a71332...bcab7f )
by Radosław
16:27
created

Vtiger_ExportToCsv_Model   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
eloc 31
c 0
b 0
f 0
dl 0
loc 67
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A output() 0 8 2
A exportData() 0 3 1
B getEntries() 0 31 7
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();
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $inventoryTable does not seem to be defined for all execution paths leading up to this point.
Loading history...
53
				if ($rows) {
54
					foreach ($rows as &$row) {
55
						$sanitizedInventoryRow = $this->sanitizeInventoryValues($row, $inventoryFields);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $inventoryFields does not seem to be defined for all execution paths leading up to this point.
Loading history...
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