-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
therapist-clientlistxls.php
184 lines (159 loc) · 6.38 KB
/
therapist-clientlistxls.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
<?php
class Therapist_ClientListSpreadsheet
{
private $oApp;
private $oPeopleDB;
private $clinics;
function __construct( SEEDAppConsole $oApp )
{
$this->oApp = $oApp;
$this->oPeopleDB = new PeopleDB( $this->oApp );
$this->clinics = new Clinics($oApp);
$this->clinics->GetCurrentClinic();
}
function OutputXLSX()
{
// Initialize the spreadsheet with three sheets (one is created by default)
$oXls = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$oXls->createSheet();
$oXls->createSheet();
// Set document properties
$oXls->getProperties()->setCreator('Collaborative Approach Therapy Services')
->setLastModifiedBy('CATS')
->setTitle('Clients / Staff / Providers')
->setSubject('Clients / Staff / Providers')
->setDescription('Spreadsheet containing contacts for a clinic')
->setKeywords('')
->setCategory('CATS clients, staff, providers list');
$filename = "cats-people.xlsx";
$condClinic = $this->clinics->isCoreClinic() ? "" : ("clinic='".$this->clinics->GetCurrentClinic()."'");
// Array of common DB fields
$raPeople = array(
'P_first_name' => 'First name',
'P_last_name' => 'Last name',
'P_address' => 'Address',
'P_city' => 'City',
'P_province' => 'Province',
'P_postal_code' => 'Postal Code',
'P_dob' => 'Date of Birth',
'P_phone_number' => 'Phone Number',
'P_email' => 'Email',
);
/* Sheet 0 is Clients
*/
$raClients = $this->oPeopleDB->GetList('C', $condClinic);
$raClientCols = $raPeople + array(
'_key' => "Client number",
'fk_people' => "P key",
'parents_name' => "Parents Name",
'parents_separate' => "Parents Separate",
'referral' => "Referral",
'background_info' => "Background Info",
'clinic' => 'Clinic'
);
$this->storeSheet( $oXls, 0, "Clients", $raClients, $raClientCols );
/* Sheet 1 is Staff
*/
$raStaff = $this->oPeopleDB->GetList('PI', $condClinic);
$raStaffCols = $raPeople + array(
'_key' => "Staff number",
'fk_people' => "P key",
'pro_role' => "Role",
'fax_number' => "Fax Number",
'rate' => "Rate",
'clinic' => 'Clinic'
);
$this->storeSheet( $oXls, 1, "Staff", $raStaff, $raStaffCols );
/* Sheet 2 is External providers
*/
$raPros = $this->oPeopleDB->GetList('PE', $condClinic);
$raProsCols = $raPeople + array(
'_key' => "Provider number",
'fk_people' => "P key",
'pro_role' => "Role",
'fax_number' => "Fax Number",
'rate' => "Rate",
'clinic' => 'Clinic'
);
$this->storeSheet( $oXls, 2, "Providers", $raPros, $raProsCols );
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$oXls->setActiveSheetIndex(0);
// Redirect output to a client’s web browser (Xlsx)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header("Content-Disposition: attachment;filename=\"$filename\"");
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
// If you're serving to IE over SSL, then the following may be needed
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header('Pragma: public'); // HTTP/1.0
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($oXls, 'Xlsx');
$writer->save('php://output');
}
private function storeSheet( $oXls, $iSheet, $sSheetName, $raRows, $raCols )
{
// $c = 'A';
// foreach( $raCols as $ra ) {
// var_dump($c);
// }exit;
$oSheet = $oXls->setActiveSheetIndex( $iSheet );
$oSheet->setTitle( $sSheetName );
// Set the headers in row 1
$c = 'A';
foreach( $raCols as $dbfield => $label ) {
$oSheet->setCellValue($c.'1', $label );
$c = chr(ord($c)+1); // Change A to B, B to C, etc
}
// Put the data starting at row 2
$row = 2;
foreach( $raRows as $ra ) {
$col = 'A';
foreach( $raCols as $dbfield => $label ) {
$oSheet->setCellValue($col.$row, $ra[$dbfield] );
$col = chr(ord($col)+1); // Change A to B, B to C, etc
}
++$row;
}
}
}
function Therapist_ClientList_OutputXLSX( SEEDAppConsole $oApp )
{
$o = new Therapist_ClientListSpreadsheet( $oApp );
$o->OutputXLSX();
}
function Therapist_ClientList_LoadXLSX( SEEDAppConsole $oApp, $sFilename )
{
$outSheets = array();
if( !($oXls = \PhpOffice\PhpSpreadsheet\IOFactory::load( $sFilename )) ) {
goto done;
}
$raSheets = $oXls->getAllSheets();
$iSheet = 1;
foreach( $raSheets as $sheet ) {
$highestRow = $sheet->getHighestDataRow();
$highestColumn = $sheet->getHighestDataColumn();
$raRows = array();
for( $row = 1; $row <= $highestRow; $row++ ) {
$ra = $sheet->rangeToArray( 'A'.$row.':'.$highestColumn.$row,
NULL, TRUE, FALSE );
// if( $this->raParms['charset'] != 'utf-8' ) {
// for( $i = 0; $i < count($ra[0]); ++$i ) {
// if( is_string($ra[0][$i]) ) {
// $ra[0][$i] = iconv( 'utf-8', $this->raParms['charset'], $ra[0][$i] );
// }
// }
// }
$raRows[] = $ra[0]; // $ra is an array of rows, with only one row
}
if( !($sheetName = $sheet->getTitle()) ) {
$sheetName = "Sheet".$iSheet;
}
$outSheets[$sheetName] = $raRows;
++$iSheet;
}
done:
return( $outSheets );
}
?>