|
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\Export\Project\Issue; |
|
13
|
|
|
|
|
14
|
|
|
use Maatwebsite\Excel\Classes\LaravelExcelWorksheet; |
|
15
|
|
|
use Maatwebsite\Excel\Writers\CellWriter; |
|
16
|
|
|
use Tinyissue\Model\Project; |
|
17
|
|
|
use Tinyissue\Services\Exporter; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* XlsHandler is an export class for exporting a project issues into a Xls or Xlsx file. |
|
21
|
|
|
* |
|
22
|
|
|
* @author Mohamed Alsharaf <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
class XlsHandler |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* Instance of the project. |
|
28
|
|
|
* |
|
29
|
|
|
* @var Project |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $project; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Collection of project issues. |
|
35
|
|
|
* |
|
36
|
|
|
* @var Project\Issue[] |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $issues; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Columns to export from the issues table. |
|
42
|
|
|
* |
|
43
|
|
|
* @var array |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $columns = [ |
|
46
|
|
|
'id' => '', |
|
47
|
|
|
'title' => '', |
|
48
|
|
|
'time_quote' => '', |
|
49
|
|
|
'created_at' => '', |
|
50
|
|
|
'updated_at' => '', |
|
51
|
|
|
'closed_at' => '', |
|
52
|
|
|
'status' => '', |
|
53
|
|
|
]; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Columns in the output file. |
|
57
|
|
|
* |
|
58
|
|
|
* @var array |
|
59
|
|
|
*/ |
|
60
|
|
|
protected $header = [ |
|
61
|
|
|
'tinyissue.id', |
|
62
|
|
|
'tinyissue.title', |
|
63
|
|
|
'tinyissue.time_quote', |
|
64
|
|
|
'tinyissue.label_created', |
|
65
|
|
|
'tinyissue.updated', |
|
66
|
|
|
'tinyissue.label_closed', |
|
67
|
|
|
'tinyissue.status', |
|
68
|
|
|
]; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Set an instance of a project model. |
|
72
|
|
|
* |
|
73
|
|
|
* @param Project $project |
|
74
|
|
|
* |
|
75
|
|
|
* @return $this |
|
76
|
|
|
*/ |
|
77
|
1 |
|
public function setProject(Project $project) |
|
78
|
|
|
{ |
|
79
|
1 |
|
$this->project = $project; |
|
80
|
|
|
|
|
81
|
1 |
|
return $this; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param Exporter $exporter |
|
86
|
|
|
* |
|
87
|
|
|
* @return void |
|
88
|
|
|
*/ |
|
89
|
1 |
|
public function handle(Exporter $exporter) |
|
90
|
|
|
{ |
|
91
|
1 |
|
$params = $exporter->getParams(); |
|
92
|
|
|
|
|
93
|
1 |
|
$this->setProject($exporter->getParams('route.project')); |
|
|
|
|
|
|
94
|
|
|
|
|
95
|
1 |
|
$query = $this->project->issues()->select(array_keys($this->columns)); |
|
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
// Filter issues |
|
98
|
1 |
|
$this->project->filterTitleOrBody($query, $params['keyword']); |
|
99
|
1 |
|
$this->project->filterAssignTo($query, $params['keyword']); |
|
100
|
1 |
|
$this->project->filterTitleOrBody($query, $params['keyword']); |
|
101
|
|
|
|
|
102
|
|
|
// Fetch issues |
|
103
|
1 |
|
$this->issues = $query->get(); |
|
104
|
|
|
|
|
105
|
|
|
// Create CSV file |
|
106
|
|
|
$exporter->sheet($this->project->name, function (LaravelExcelWorksheet $sheet) { |
|
107
|
|
|
|
|
108
|
|
|
// Global sheet styles |
|
109
|
1 |
|
$this->globalStyle($sheet); |
|
110
|
|
|
|
|
111
|
|
|
// Title |
|
112
|
1 |
|
$sheet->mergeCells('A1:G1'); |
|
113
|
1 |
|
$sheet->setHeight(1, 50); |
|
|
|
|
|
|
114
|
|
|
$sheet->cell('A1', function (CellWriter $cell) { |
|
115
|
1 |
|
$this->sheetTitle($cell); |
|
116
|
1 |
|
}); |
|
117
|
|
|
|
|
118
|
|
|
// Header |
|
119
|
1 |
|
$sheet->row(2, array_map('trans', $this->header)); |
|
120
|
|
|
|
|
121
|
|
|
// Rows |
|
122
|
1 |
|
$index = 3; |
|
123
|
1 |
|
foreach ($this->issues as $issue) { |
|
124
|
1 |
|
$this->sheetRow($sheet, $index, $issue); |
|
125
|
1 |
|
++$index; |
|
126
|
|
|
} |
|
127
|
1 |
|
}); |
|
128
|
1 |
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Setup sheet global styles. |
|
132
|
|
|
* |
|
133
|
|
|
* @param LaravelExcelWorksheet $sheet |
|
134
|
|
|
* |
|
135
|
|
|
* @return void |
|
136
|
|
|
*/ |
|
137
|
1 |
|
protected function globalStyle(LaravelExcelWorksheet $sheet) |
|
138
|
|
|
{ |
|
139
|
|
|
// Font size & family |
|
140
|
1 |
|
$sheet->setFontSize(15); |
|
141
|
1 |
|
$sheet->setFontFamily('Calibri'); |
|
142
|
1 |
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Sheet title in the first row. |
|
146
|
|
|
* |
|
147
|
|
|
* @param CellWriter $cell |
|
148
|
|
|
* |
|
149
|
|
|
* @return void |
|
150
|
|
|
*/ |
|
151
|
1 |
|
public function sheetTitle(CellWriter $cell) |
|
152
|
|
|
{ |
|
153
|
1 |
|
$cell->setFontWeight('bold'); |
|
154
|
1 |
|
$cell->setBorder('none', 'none', 'none', 'thin'); |
|
155
|
1 |
|
$title = trans('tinyissue.export_xls_title', ['name' => $this->project->name, 'count' => count($this->issues)]); |
|
156
|
1 |
|
$cell->setValue($title); |
|
157
|
1 |
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Sheet data row. |
|
161
|
|
|
* |
|
162
|
|
|
* @param LaravelExcelWorksheet $sheet |
|
163
|
|
|
* @param int $index |
|
164
|
|
|
* @param Project\Issue $issue |
|
165
|
|
|
* |
|
166
|
|
|
* @return void |
|
167
|
|
|
*/ |
|
168
|
1 |
|
protected function sheetRow(LaravelExcelWorksheet $sheet, $index, Project\Issue $issue) |
|
169
|
|
|
{ |
|
170
|
|
|
// Setup row data |
|
171
|
|
|
array_walk($this->columns, function (&$column, $key, Project\Issue $issue) { |
|
172
|
1 |
|
$column = (string) $issue->$key; |
|
173
|
1 |
|
if ($key === 'status') { |
|
174
|
1 |
|
$column = (int) $issue->status === Project\Issue::STATUS_OPEN ? 'open' : 'closed'; |
|
175
|
1 |
|
$column = trans('tinyissue.' . $column); |
|
176
|
|
|
} |
|
177
|
1 |
|
}, $issue); |
|
178
|
|
|
|
|
179
|
|
|
// Write row |
|
180
|
1 |
|
$sheet->row($index, $this->columns); |
|
181
|
|
|
|
|
182
|
|
|
// Format last cell |
|
183
|
1 |
|
$sheet->cell('G' . $index, function (CellWriter $cell) use ($issue) { |
|
184
|
1 |
|
$color = (int) $issue->status === Project\Issue::STATUS_CLOSED ? '#FF0000' : '#00FF00'; |
|
185
|
1 |
|
$cell->setBackground($color); |
|
186
|
1 |
|
}); |
|
187
|
1 |
|
} |
|
188
|
|
|
} |
|
189
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: