1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Tinyissue package. |
5
|
|
|
* |
6
|
|
|
* (c) Mohamed Alsharaf <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Tinyissue\Services; |
13
|
|
|
|
14
|
|
|
use Maatwebsite\Excel\Exceptions\LaravelExcelException; |
15
|
|
|
use Maatwebsite\Excel\Files\NewExcelFile; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Exporter is class for initialising the exporter, process data, and export the generated file. |
19
|
|
|
* |
20
|
|
|
* @author Mohamed Alsharaf <[email protected]> |
21
|
|
|
* |
22
|
|
|
* @method $this sheet($sheetID, $callback = null) |
23
|
|
|
* @method $this store($ext = 'xls', $path = false, $returnInfo = false) |
24
|
|
|
* @method $this setFileName($fileName) |
25
|
|
|
*/ |
26
|
|
|
class Exporter extends NewExcelFile |
27
|
|
|
{ |
28
|
|
|
/** Current supported files type */ |
29
|
|
|
const TYPE_CSV = 'csv'; |
30
|
|
|
const TYPE_XLS = 'xls'; |
31
|
|
|
const TYPE_XLSX = 'xlsx'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Parameters. |
35
|
|
|
* |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
protected $params = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Export file format. |
42
|
|
|
* |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
protected $format = self::TYPE_CSV; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Type of data to export. |
49
|
|
|
* |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
protected $type = ''; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Returns the parameters. |
56
|
|
|
* |
57
|
|
|
* @param string $key |
58
|
|
|
* |
59
|
|
|
* @return mixed|array |
60
|
|
|
*/ |
61
|
4 |
|
public function getParams($key = null) |
62
|
|
|
{ |
63
|
4 |
|
if (null === $key) { |
64
|
4 |
|
return $this->params; |
65
|
|
|
} |
66
|
|
|
|
67
|
1 |
|
return array_get($this->params, $key); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Start importing. |
72
|
|
|
* |
73
|
|
|
* @param string $className |
74
|
|
|
* @param string $format |
75
|
|
|
* @param array $params |
76
|
|
|
* |
77
|
|
|
* @return array['full', 'path', 'file', 'title', 'ext'] |
|
|
|
|
78
|
|
|
*/ |
79
|
4 |
|
public function exportFile($className, $format = self::TYPE_CSV, array $params = []) |
80
|
|
|
{ |
81
|
4 |
|
$params['route'] = $this->app->request->route()->parameters(); |
82
|
4 |
|
$this->format = $format; |
83
|
4 |
|
$this->params = $params; |
84
|
4 |
|
$this->type = $className; |
85
|
|
|
|
86
|
|
|
// Update file name |
87
|
4 |
|
$this->setFileName($this->getFilename()); |
88
|
|
|
|
89
|
|
|
// Start exporting |
90
|
4 |
|
$this->handle($className); |
91
|
|
|
|
92
|
|
|
// Store file and return info |
93
|
4 |
|
return $this->store($format, false, true); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Returns export file name. |
98
|
|
|
* |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
4 |
|
public function getFilename() |
102
|
|
|
{ |
103
|
4 |
|
return 'export_' . str_replace('\\', '_', strtolower($this->type)) . '_' . time(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Construct the export class full name. |
108
|
|
|
* |
109
|
|
|
* @param string $type |
110
|
|
|
* |
111
|
|
|
* @return string |
112
|
|
|
* |
113
|
|
|
* @throws LaravelExcelException |
114
|
|
|
*/ |
115
|
4 |
|
protected function getHandlerClassName($type) |
116
|
|
|
{ |
117
|
4 |
|
$handler = '\\Tinyissue\Export\\' . $type . '\\' . ucfirst(substr($this->format, 0, 3)) . 'Handler'; |
118
|
|
|
|
119
|
|
|
// Check if the handler exists |
120
|
4 |
|
if (!class_exists($handler)) { |
121
|
|
|
throw new LaravelExcelException("$type handler [$handler] does not exist."); |
122
|
|
|
} |
123
|
|
|
|
124
|
4 |
|
return $handler; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.