php Abstract Class Report protected sheet_id protected generator publi

  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
<?php
Abstract Class Report {
protected $sheet_id = 0;
protected $generator;
public function __construct ()
{
$this->generator = new PHPExcel;
$this->generator->getProperties()->setCreator("Sdm-club administrator")
->setLastModifiedBy("Sdm-club administrator")
->setTitle("Office 2007 XLSX Document")
->setSubject("Office 2007 XLSX Document")
->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
->setKeywords("office 2007 openxml php")
->setCategory("Report file");
}
public function prettyName ($s)
{
$arr = explode(' ', $s);
if (count($arr) == 3) {
$new_s = $arr[0].' '.mb_substr($arr[1], 0, 1, 'utf-8').'.'.mb_substr($arr[2], 0, 1, 'utf-8').'.';
} elseif (count($arr) == 2) {
$new_s = $arr[0].' '.mb_substr($arr[1], 0, 1, 'utf-8').'.';
} else {
$new_s = $s;
}
return $new_s;
}
abstract public function compile();
}
Class ReportManager extends Report {
private $excel;
protected $reports = array();
public function __construct (array $reports)
{
$this->reports = $reports;
}
public function compile()
{
foreach ($this->reports as $report)
{
$report->compile();
$this->excel = $report->generator;
}
// echo '<pre>';
// print_r($data);
// echo '</pre>';
}
public function save ()
{
$writer = PHPExcel_IOFactory::createWriter($this->excel, 'Excel2007');
$writer->save('php://output');
}
}
Class ReportAll extends Report {
private $provider;
private $farmusersstats;
public function __construct (&$dbProvider)
{
parent::__construct();
$this->provider = $dbProvider;
}
public function getData ()
{
$sql = "SELECT users.id,users.dispname as username,city.id as city_id,city.name as city,drugnet.id as drugnet_id,drugnet.name as drugnet FROM `users`
INNER JOIN city ON city.id = users.city_id
INNER JOIN drugnet ON drugnet.id = users.drugnet_id
ORDER BY city.name ASC";
$request = $this->provider->Select($sql);
$row = array();
foreach ($request as $i => $r)
{
$row[$i][id] = $r[id];
// $row[$i][username] = $this->prettyName($r[username]);
$row[$i][username] = $r[username];
$row[$i][city] = $r[city];
$row[$i][drugnet] = $r[drugnet];
$row[$i][usercount] = $this->getUsersCount($r[city_id], $r[drugnet_id]);
$row[$i][userstats] = $this->getUsersStat($r[id]);
}
return $row;
}
public function getUsersCount ($city_id, $drugnet_id)
{
if (is_numeric($city_id) && is_numeric($drugnet_id))
{
return $this->provider->Count("SELECT id FROM `users` WHERE city_id = {$city_id} AND drugnet_id = {$drugnet_id}");
}
return null;
}
public function getUsersStat ($user_id)
{
return '4/9';
}
public function compile ()
{
// Add some header
$this->generator->setActiveSheetIndex($this->sheet_id)
->setCellValue('A1', 'Город')
->setCellValue('B1', 'Аптечная сеть')
->setCellValue('C1', 'Количество')
->setCellValue('D1', 'Фармацевты')
->setCellValue('E1', 'Статистика (прошел/не прошел)');
$this->generator->getActiveSheet()->getStyle('A1:E1')->getFont()->setBold(true);
$this->generator->getActiveSheet()->setTitle('Отчет №1');
// Add some data
$index = 1;
$result = array();
foreach ($this->getData() as $row)
{
$index++;
$this->generator->getActiveSheet()
->setCellValue('A' . $index, $row[city])
->setCellValue('B' . $index, $row[drugnet])
->setCellValue('C' . $index, $row[usercount])
->setCellValue('D' . $index, $row[username])
->setCellValue('E' . $index, $row[userstats]);
}
$this->generator->setActiveSheetIndex(0);
}
}
Class ReportUsers extends Report {
private $composer;
protected $provider;
protected $group_id;
public function __construct (&$dbProvider, $group_id)
{
parent::__construct();
$this->provider = $dbProvider;
$this->group_id = $group_id;
}
public function getComposer ()
{
switch ($this->group_id)
{
case '3':
return new ReportUsersFarm;
break;
case '4':
return new ReportUsersCorp;
break;
default:
return null;
break;
}
}
public function compile ()
{
if (!is_null($this->getComposer()))
{
$this->getComposer()->compile();
}
}
}
Class ReportUsersFarm extends ReportUsers {
public function getData ()
{
$sql = "SELECT
users.id,users.dispname as username,users.drugstore,users.position,users.phone,
city.name as city,drugnet.name as drugnet FROM `users`
INNER JOIN city ON city.id = users.city_id
INNER JOIN drugnet ON drugnet.id = users.drugnet_id
WHERE users.group_id = {$this->group_id}
ORDER BY users.id DESC";
return $this->provider->Select($sql);
}
public function compile ()
{
// Add new sheet
$this->sheet_id++;
// Add some header
$this->generator->setActiveSheetIndex($this->sheet_id)
->setCellValue('A1', 'ФИО')
->setCellValue('B1', 'Город')
->setCellValue('C1', 'Аптека')
->setCellValue('D1', 'Аптечная сеть')
->setCellValue('E1', 'Должность')
->setCellValue('F1', 'Телефон');
$this->generator->getActiveSheet()->getStyle('A1:F1')->getFont()->setBold(true);
$this->generator->getActiveSheet()->setTitle('Отчет №2 (Фармацевты)');
// Add some data
$index = 1;
$result = array();
foreach ($this->getData() as $row)
{
$index++;
$this->generator->getActiveSheet()
->setCellValue('A' . $index, $row[username])
->setCellValue('B' . $index, $row[city])
->setCellValue('C' . $index, $row[drustore])
->setCellValue('D' . $index, $row[drugnet])
->setCellValue('E' . $index, $row[position])
->setCellValue('F' . $index, $row[phone]);
}
$this->generator->setActiveSheetIndex(0);
}
}
Class ReportUsersCorp extends ReportUsers {
public function getData ()
{
$sql = "SELECT
users.id,users.dispname as username,users.position,users.phone,
city.name as city FROM `users`
INNER JOIN city ON city.id = users.city_id
WHERE users.group_id = {$this->group_id}
ORDER BY users.id DESC";
return $this->provider->Select($sql);
}
public function compile ()
{
// Add new sheet
$this->sheet_id++;
// Add some header
$this->generator->setActiveSheetIndex($this->sheet_id)
->setCellValue('A1', 'ФИО')
->setCellValue('B1', 'Город')
->setCellValue('С1', 'Должность')
->setCellValue('D1', 'Телефон');
$this->generator->getActiveSheet()->getStyle('A1:D1')->getFont()->setBold(true);
$this->generator->getActiveSheet()->setTitle('Отчет №2 (Сотрудники)');
// Add some data
$index = 1;
$result = array();
foreach ($this->getData() as $row)
{
$index++;
$this->generator->getActiveSheet()
->setCellValue('A' . $index, $row[username])
->setCellValue('B' . $index, $row[city])
->setCellValue('C' . $index, $row[position])
->setCellValue('D' . $index, $row[phone]);
}
$this->generator->setActiveSheetIndex(0);
}
}
?>