-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCidoc.php
306 lines (226 loc) · 7.91 KB
/
Cidoc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require FCPATH.'vendor'.DIRECTORY_SEPARATOR.'autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
Dompdf\Autoloader::register();
use Dompdf\Dompdf;
class Cidoc {
private $header;
private $body;
private $data;
private $option;
private $style;
private $doc_ex;
public function __construct(){
$CI =& get_instance();
$CI->load->helper('url');
}
public function set($column, $data = array(), $option = array()){
foreach($column as $c){
$this->header[] = $c['label'];
$this->body[] = $c['data'];
}
$this->data = $data;
$this->option = $option;
$this->processing_setup();
return $this;
}
public function createExcel(){
$excel = $this->processing_sheet();
$filename = 'untitle';
if(isset($this->option['title'])){
$filename = url_title($this->option['title'], '_', TRUE);
}
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$filename.'.xls"');
$writer = IOFactory::createWriter($excel, 'Xls');
$writer->save('php://output');
}
public function createHTML($return = false){
$excel = $this->processing_sheet();
$excel->getActiveSheet()->getPageMargins()->setTop(0.2);
$excel->getActiveSheet()->getPageMargins()->setRight(0.2);
$excel->getActiveSheet()->getPageMargins()->setLeft(0.2);
$excel->getActiveSheet()->getPageMargins()->setBottom(0.2);
$excel->getActiveSheet()->getPageSetup()->setFitToWidth(1);
$excel->getActiveSheet()->getPageSetup()->setFitToHeight(0);
$filename = 'untitle';
if(isset($this->option['title'])){
$filename = url_title($this->option['title'], '_', TRUE);
}
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Html($excel);
$html = $writer->generateHTMLHeader();
$html .= $this->getStyleHTML();
$html .= $writer->generateSheetData();
$html .= $writer->generateHTMLFooter();
if ($return) {
return $html;
}
echo $html;
}
public function createPDF(){
$filename = 'untitle';
if(isset($this->option['title'])){
$filename = url_title($this->option['title'], '_', TRUE);
}
$html = $this->createHTML(true);
$dompdf = new Dompdf();
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'landscape');
$dompdf->render();
$dompdf->stream($filename);
}
public function getStyleHTML(){
$style = array(
'table_header' => 'border: 1px solid gray; padding: 5px;',
'table_body' => 'border: 1px solid gray; padding: 5px;',
'summary' => 'border: 1px solid gray; padding: 5px;',
);
$return = '<style>';
$return .= 'table{ width: 100%; border-collapse: collapse; }';
foreach($this->style as $k => $s){
if(isset($style[$k])){
$return .= implode(',', $s).'{'.$style[$k].'}';
}
}
$return .= '</style>';
return $return;
}
public function setStyleClass($type, $rownumber){
return $this->style[$type][] = '.row'.($rownumber-1).' td';
}
public function processing_setup(){
$column = $this->header;
$data_body = $this->body;
$alpha = array();
$alpha_first = 'A';
$alpha_last = '';
$column_pos = array();
for($i=1; $i<=count($column); $i++){
$alpha[$i] = Coordinate::stringFromColumnIndex($i);
$alpha_last = $alpha[$i];
$column_pos[$data_body[$i-1]] = $alpha[$i];
}
$this->doc_ex['index_alpha'] = $alpha;
$this->doc_ex['first_alpha'] = $alpha_first;
$this->doc_ex['last_alpha'] = $alpha_last;
$this->doc_ex['index_total'] = count($column);
$this->doc_ex['column_pos'] = $column_pos;
$data = $this->data;
$formatted_data = array();
if(!empty($data)){
foreach($data as $d){
$result = array_intersect_key($d, array_flip($this->body));
$formatted_data[] = array_replace(array_flip($this->body), $result);
}
}
$this->data = $formatted_data;
}
public function processing_sheet(){
$prop = $this->doc_ex;
$row_counter = 1;
$data = $this->data;
$merged = (isset($this->option['merged']) && is_array($this->option['merged'])) ? $this->option['merged'] : false;
$excel = new Spreadsheet();
$sheet = $excel->setActiveSheetIndex(0);
$this->get_header_excel($sheet, $prop, $row_counter);
$this->get_filter_info_excel($sheet, $prop, $row_counter);
$row_counter++;
// header table
$cell_body = $prop['first_alpha'].$row_counter;
foreach($prop['index_alpha'] as $k => $a){
$sheet->setCellValue($a.$row_counter, $this->header[$k-1]);
$this->setStyleClass('table_header', $row_counter);
}
$row_counter++;
$mark_group = '';
$cell_group = array();
$cell_grouped = array();
// body table
foreach($data as $d){
$a = $prop['first_alpha'];
foreach($d as $dk => $dv){
if(isset($d[$merged['by']]) && !in_array($dk, $merged['except'])){
if($mark_group !== $d[$merged['by']]){
$mark_group = $d[$merged['by']];
if(array_key_exists($a.'_'.$mark_group, $cell_group)){
$mark_group .= date('Ymdhis');
}
$cell_group[$a.'_'.$mark_group][] = $a.$row_counter;
}else{
$cell_group[$a.'_'.$mark_group][] = $a.$row_counter;
}
}
$this->setStyleClass('table_body', $row_counter);
$sheet->setCellValue($a.$row_counter, $dv);
$a++;
}
$row_counter++;
}
if(!empty($cell_group)){
foreach($cell_group as $g){
$g_first = reset($g);
$g_last = end($g);
if($g_first && $g_last && $g_first !== $g_last){
$sheet->mergeCells($g_first.':'.$g_last);
$sheet->getStyle($g_first.':'.$g_last)->getAlignment()->setVertical(\PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER);
}
}
}
$this->get_summary_excel($sheet, $prop, $row_counter);
$cell_body .= ':'.$prop['last_alpha'].($row_counter-1);
$styleArray = array(
'borders' => array(
'allBorders' => array(
'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
),
),
);
$sheet->getStyle($cell_body)->applyFromArray($styleArray);
foreach($prop['index_alpha'] as $a){
$sheet->getColumnDimension($a)->setAutoSize(true);
}
return $excel;
}
public function get_header_excel(&$sheet, $prop, &$row_counter){
if(isset($this->option['title']) && !empty($this->option['title'])){
$sheet->setCellValue($prop['first_alpha'].$row_counter, $this->option['title']);
$sheet->mergeCells($prop['first_alpha'].$row_counter.':'.$prop['last_alpha'].$row_counter);
$this->setStyleClass('table_title', $row_counter);
$row_counter++;
}
}
public function get_filter_info_excel(&$sheet, $prop, &$row_counter){
if(!isset($this->option['filter']) || !is_array($this->option['filter'])){
return;
}
$row_counter++;
foreach($this->option['filter'] as $k => $f){
$this->setStyleClass('filter', $row_counter);
$sheet->setCellValue($prop['first_alpha'].$row_counter++, $f['label'].' : '.$f['value']);
}
$row_counter++;
}
public function get_summary_excel(&$sheet, $prop, &$row_counter)
{
if(!isset($this->option['summary']) || !is_array($this->option['summary'])){
return;
}
foreach($this->option['summary'] as $f){
$this->setStyleClass('summary', $row_counter);
$sheet->setCellValue($prop['first_alpha'].$row_counter, $f['label']);
if(!is_array($f['column'])){
if(isset($prop['column_pos'][$f['column']])){
$sheet->setCellValue($prop['column_pos'][$f['column']].$row_counter, $f['value']);
}
}else{
foreach($f['column'] as $ck => $cv){
$sheet->setCellValue($prop['column_pos'][$ck].$row_counter, $cv);
}
}
$row_counter++;
}
}
}