This repository was archived by the owner on Dec 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathPilotAdmin.php
773 lines (619 loc) · 23.6 KB
/
PilotAdmin.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
<?php
/**
* phpVMS - Virtual Airline Administration Software
* Copyright (c) 2008 Nabeel Shahzad
* For more information, visit www.phpvms.net
* Forums: http://www.phpvms.net/forum
* Documentation: http://www.phpvms.net/docs
*
* phpVMS is licenced under the following license:
* Creative Commons Attribution Non-commercial Share Alike (by-nc-sa)
* View license.txt in the root, or visit http://creativecommons.org/licenses/by-nc-sa/3.0/
*
* @author Nabeel Shahzad
* @copyright Copyright (c) 2008, Nabeel Shahzad
* @link http://www.phpvms.net
* @license http://creativecommons.org/licenses/by-nc-sa/3.0/
* @package module_admin_pilots
*/
class PilotAdmin extends CodonModule {
/**
* PilotAdmin::HTMLHead()
*
* @return
*/
public function HTMLHead() {
switch ($this->controller->function) {
case 'viewpilots':
$this->set('sidebar', 'sidebar_pilots.php');
break;
case 'pendingpilots':
$this->set('sidebar', 'sidebar_pending.php');
break;
case 'pilotgroups':
case 'editgroup':
case 'addgroup':
$this->set('sidebar', 'sidebar_groups.php');
break;
}
}
/**
* PilotAdmin::index()
*
* @return
*/
public function index() {
$this->viewpilots();
}
/**
* PilotAdmin::viewpilots()
*
* @return
*/
public function viewpilots() {
/* This function is called for *ANYTHING* in that popup box
Preset all of the template items in this function and
call them in the subsequent templates
Confusing at first, but easier than loading each tab
independently via AJAX. Though may be an option later
on, but can certainly be done by a plugin (Add another
tab through AJAX). The hook is available for whoever
wants to use it
*/
switch ($this->post->action) {
case 'changepassword':
$this->ChangePassword();
return;
break;
case 'editpirep':
$editor = new PIREPAdmin();
$editor->edit_pirep_post($this->post);
break;
case 'deletepilot':
$pilotid = $this->post->pilotid;
$pilotinfo = PilotData::getPilotData($pilotid);
PilotData::DeletePilot($pilotid);
CodonEvent::Dispatch('pilot_deleted', 'PilotAdmin', $pilot);
$this->set('message', Lang::gs('pilot.deleted'));
$this->render('core_success.php');
LogData::addLog(Auth::$userinfo->pilotid, 'Deleted pilot ' .
PilotData::getPilotCode($pilotinfo->code, $pilotinfo->pilotid)
.' '.$pilotinfo->firstname.' '.$pilotinfo->lastname
);
break;
/* These are reloaded into the #pilotgroups ID
so the entire groups list is refreshed
*/
case 'addgroup':
$this->AddPilotToGroup();
$this->SetGroupsData($this->post->pilotid);
$this->render('pilots_groups.php');
$this->render('pilots_addtogroup.tpl');
return;
break;
case 'removegroup':
$this->RemovePilotGroup();
$this->SetGroupsData($this->post->pilotid);
$this->render('pilots_groups.php');
$this->render('pilots_addtogroup.tpl');
return;
break;
case 'saveprofile':
if ($this->post->firstname == '' || $this->post->lastname == '') {
$this->set('message', 'The first or lastname cannot be blank!');
$this->render('core_error.php');
return;
}
$params = array(
'code' => $this->post->code,
'firstname' => $this->post->firstname,
'lastname' => $this->post->lastname,
'email' => $this->post->email,
'location' => $this->post->location,
'hub' => $this->post->hub,
'retired' => $this->post->retired,
'totalhours' => $this->post->totalhours,
'totalflights' => $this->post->totalflights,
'totalpay' => floatval($this->post->totalpay),
'payadjust' => floatval($this->post->payadjust),
'transferhours' => $this->post->transferhours,
'comment' => $this->post->comment,
);
PilotData::updateProfile($this->post->pilotid, $params);
PilotData::SaveFields($this->post->pilotid, $_POST);
/* Don't calculate a pilot's rank if this is set */
if (Config::Get('RANKS_AUTOCALCULATE') == false) {
PilotData::changePilotRank($this->post->pilotid, $this->post->rank);
} else {
RanksData::calculateUpdatePilotRank($this->post->pilotid);
}
StatsData::UpdateTotalHours();
$this->set('message', 'Profile updated successfully');
$this->render('core_success.php');
if($this->post->resend_email == 'true') {
$this->post->id = $this->post->pilotid;
$this->resendemail(false);
}
$pilot = PilotData::getPilotData($this->post->pilotid);
LogData::addLog(Auth::$userinfo->pilotid, 'Updated profile for '
.PilotData::getPilotCode($pilot->code, $pilot->pilotid)
.' '.$pilot->firstname.' '.$pilot->lastname);
return;
break;
}
if ($this->get->action == 'viewoptions') {
$this->ViewPilotDetails();
return;
}
$this->ShowPilotsList();
}
public function pilotgrouptab($pilotid) {
$this->setGroupsData($pilotid);
Template::Show('pilots_groups.php');
Template::Show('pilots_addtogroup.php');
}
/**
* PilotAdmin::pendingpilots()
*
* @return
*/
public function pendingpilots() {
$this->checkPermission(MODERATE_REGISTRATIONS);
if (isset($this->post->action)) {
switch ($this->post->action) {
case 'approvepilot':
$this->ApprovePilot();
break;
case 'rejectpilot':
$this->RejectPilot();
break;
}
}
$this->set('allpilots', PilotData::getPendingPilots());
$this->render('pilots_pending.php');
}
/**
* PilotAdmin::resendemail()
*
* @return
*/
public function resendemail($show_pending = true) {
$this->checkPermission(MODERATE_REGISTRATIONS);
$this->checkPermission(EMAIL_PILOTS);
$pilot = PilotData::getPilotData($this->post->id);
# Send pilot notification
$subject = Lang::gs('email.register.accepted.subject');
$this->set('pilot', $pilot);
$oldPath = Template::setTemplatePath(TEMPLATES_PATH);
$oldSkinPath = Template::setSkinPath(ACTIVE_SKIN_PATH);
$message = Template::getTemplate('email_registrationaccepted.php', true, true, true);
Template::setTemplatePath($oldPath);
Template::setSkinPath($oldSkinPath);
Util::sendEmail($pilot->email, $subject, $message);
$this->set('message', 'Activation email has been re-sent to '.$pilot->firstname.' '.$pilot->lastname);
$this->render('core_success.php');
LogData::addLog(
Auth::$userinfo->pilotid,
'Activation email re-sent '.PilotData::getPilotCode($pilot->code, $pilot->pilotid).' - '.$pilot->firstname.' '.$pilot->lastname
);
if($show_pending === true) {
$this->set('allpilots', PilotData::getPendingPilots());
$this->render('pilots_pending.php');
}
}
/**
* PilotAdmin::viewbids()
*
* @return
*/
public function viewbids() {
if ($this->post->action == 'deletebid') {
$ret = SchedulesData::RemoveBid($this->post->id);
$params = array();
if ($ret == true) {
$params['status'] = 'ok';
} else {
$params['status'] = 'There was an error';
$params['message'] = DB::error();
}
echo json_encode($params);
return;
}
$this->set('allbids', SchedulesData::getAllBids());
$this->render('pilots_viewallbids.php');
}
/**
* PilotAdmin::pilotgroups()
*
* @return
*/
public function pilotgroups() {
if (isset($this->post->action)) {
if ($this->post->action == 'addgroup') {
$this->AddGroupPost();
} elseif ($this->post->action == 'editgroup') {
# Process
$this->SaveGroup();
}
}
$this->ShowGroups();
}
/**
* PilotAdmin::addgroup()
*
* @return
*/
public function addgroup() {
$this->set('title', 'Add a Group');
$this->set('action', 'addgroup');
$this->set('permission_set', Config::Get('permission_set'));
$this->render('groups_groupform.php');
}
/**
* PilotAdmin::editgroup()
*
* @return
*/
public function editgroup() {
if (!isset($this->get->groupid)) {
return;
}
$group_info = PilotGroups::GetGroup($this->get->groupid);
$this->set('group', $group_info);
$this->set('title', 'Editing ' . $group_info->name);
$this->set('action', 'editgroup');
$this->set('permission_set', Config::Get('permission_set'));
$this->render('groups_groupform.php');
}
/**
* PilotAdmin::pilotawards()
*
* @return
*/
public function pilotawards() {
if (isset($this->post->action)) {
if ($this->post->action == 'addaward') {
$this->AddAward();
} elseif ($this->post->action == 'deleteaward') {
$this->DeleteAward();
}
}
$this->set('allawards', AwardsData::GetPilotAwards($_REQUEST['pilotid']));
$this->render('pilots_awards.php');
}
/**
* PilotAdmin::ShowPilotsList()
*
* @return
*/
protected function ShowPilotsList() {
$this->render('pilots_list.php');
}
/**
* PilotAdmin::getpilotsjson()
*
* @return
*/
public function getpilotsjson() {
$page = $this->get->page; // get the requested page
$limit = $this->get->rows; // get how many rows we want to have into the grid
$sidx = $this->get->sidx; // get index row - i.e. user click to sort
$sord = $this->get->sord; // get the direction
if (!$sidx)
$sidx = 1;
/* Do the search using jqGrid */
$where = array();
if ($this->get->_search == 'true') {
$searchstr = jqgrid::strip($this->get->filters);
$where_string = jqgrid::constructWhere($searchstr);
# Append to our search, add 1=1 since it comes with AND
# from above
$where[] = "1=1 {$where_string}";
}
Config::Set('PILOT_ORDER_BY', "{$sidx} {$sord}");
# Do a search without the limits so we can find how many records
$count = count(PilotData::findPilots($where));
if ($count > 0) {
$total_pages = ceil($count / $limit);
} else {
$total_pages = 0;
}
if ($page > $total_pages) {
$page = $total_pages;
}
$start = $limit * $page - $limit; // do not put $limit*($page - 1)
if ($start < 0) {
$start = 0;
}
# And finally do a search with the limits
$allpilots = PilotData::findPilots($where, $limit, $start);
if (!$allpilots) {
$allpilots = array();
}
# Form the json header
$json = array('page' => $page, 'total' => $total_pages, 'records' => $count, 'rows' => array());
$statuses = Config::get('PILOT_STATUS_TYPES');
# Add each row to the above array
foreach ($allpilots as $row) {
$pilotid = PilotData::getPilotCode($row->code, $row->pilotid);
foreach($statuses as $id => $details) {
if($row->retired == $id) {
$status = $details['name'];
break;
}
}
$location = '<img src="' . Countries::getCountryImage($row->location) . '" alt="' . $row->location . '" />';
$edit = '<a href="' . adminurl('/pilotadmin/viewpilots?action=viewoptions&pilotid=' . $row->pilotid) . '">Edit</a>';
$tmp = array(
'id' => $row->id,
'cell' => array( # Each column, in order
$row->id,
$pilotid,
$row->firstname,
$row->lastname,
$row->email,
$location,
$status,
$row->rank,
$row->totalflights,
$row->totalhours,
$row->lastip,
$edit
)
);
$json['rows'][] = $tmp;
}
header("Content-type: text/x-json");
echo json_encode($json);
}
/**
* PilotAdmin::ViewPilotDetails()
*
* @return
*/
protected function ViewPilotDetails() {
//This is for the main tab
if(
PilotGroups::group_has_perm(Auth::$usergroups, EDIT_PILOTS) ||
PilotGroups::group_has_perm(Auth::$usergroups, EDIT_GROUPS) ||
PilotGroups::group_has_perm(Auth::$usergroups, EDIT_AWARDS) ||
PilotGroups::group_has_perm(Auth::$usergroups, MODERATE_PIREPS)
) {
$this->set('pilotinfo', PilotData::GetPilotData($this->get->pilotid));
$this->set('customfields', PilotData::GetFieldData($this->get->pilotid, true));
$this->set('allawards', AwardsData::GetPilotAwards($this->get->pilotid));
$this->set('pireps', PIREPData::GetAllReportsForPilot($this->get->pilotid));
$this->set('countries', Countries::getAllCountries());
$this->set('directPirepEdit', true);
$this->SetGroupsData($this->get->pilotid);
// For the PIREP list
$this->set('pending', false);
$this->set('load', 'pilotpireps');
$this->render('pilots_detailtabs.php');
}else{
Debug::showCritical('Unauthorized access - Invalid Permissions.');
die();
}
}
/**
* PilotAdmin::SetGroupsData()
*
* @param mixed $pilotid
* @return
*/
protected function SetGroupsData($pilotid) {
# This is for the groups tab
$freegroups = array();
$allgroups = PilotGroups::GetAllGroups();
foreach ($allgroups as $group) {
if (!PilotGroups::CheckUserInGroup($pilotid, $group->groupid)) {
array_push($freegroups, $group->name);
}
}
$this->set('pilotid', $pilotid);
$this->set('pilotgroups', PilotData::GetPilotGroups($pilotid));
$this->set('freegroups', $freegroups);
}
/**
* PilotAdmin::AddGroupPost()
*
* @return
*/
protected function AddGroupPost() {
if ($this->post->name == '') {
$this->set('message', Lang::gs('group.no.name'));
$this->render('core_error.php');
return;
}
$permissions = 0;
foreach ($this->post->permissions as $perm) {
$permissions = PilotGroups::set_permission($permissions, $perm);
}
$ret = PilotGroups::AddGroup($this->post->name, $permissions);
if (DB::errno() != 0) {
$this->set('message', sprintf(Lang::gs('error'), DB::$error));
$this->render('core_error.php');
} else {
$this->set('message', sprintf(Lang::gs('group.added'), $this->post->name));
$this->render('core_success.php');
LogData::addLog(Auth::$userinfo->pilotid, 'Added group "' . $this->post->name . '"');
}
}
/**
* PilotAdmin::SaveGroup()
*
* @return
*/
protected function SaveGroup() {
$permissions = 0;
foreach ($this->post->permissions as $perm) {
$permissions = PilotGroups::set_permission($permissions, $perm);
}
PilotGroups::EditGroup($this->post->groupid, $this->post->name, $permissions);
if (DB::errno() != 0) {
$this->set('message', sprintf(Lang::gs('error'), DB::$error));
$this->render('core_error.php');
} else {
$this->set('message', sprintf(Lang::gs('group.saved'), $this->post->name));
$this->render('core_success.php');
LogData::addLog(Auth::$userinfo->pilotid, 'Edited group "' . $this->post->name . '"');
}
}
/**
* PilotAdmin::AddPilotToGroup()
*
* @return
*/
protected function AddPilotToGroup() {
if (PilotGroups::CheckUserInGroup($this->post->pilotid, $this->post->groupname)) {
$this->set('message', Lang::gs('group.pilot.already.in'));
$this->render('core_error.php');
return;
}
$ret = PilotGroups::AddUsertoGroup($this->post->pilotid, $this->post->groupname);
if (DB::errno() != 0) {
$this->set('message', Lang::gs('group.add.error'));
$this->render('core_error.php');
} else {
LogData::addLog(Auth::$userinfo->pilotid, 'Added pilot #' . $this->post->pilotid . ' to group "' . $this->post->groupname . '"');
}
}
/**
* PilotAdmin::RemovePilotGroup()
*
* @return
*/
protected function RemovePilotGroup() {
$pilotid = $this->post->pilotid;
$groupid = $this->post->groupid;
PilotGroups::RemoveUserFromGroup($pilotid, $groupid);
if (DB::errno() != 0) {
$this->set('message', 'There was an error removing');
$this->render('core_error.php');
} else {
LogData::addLog(Auth::$userinfo->pilotid, 'Removed pilot #' . $this->post->pilotid . ' from group "' . $this->post->groupid . '"');
}
}
/**
* PilotAdmin::ShowGroups()
*
* @return
*/
protected function ShowGroups() {
$this->set('allgroups', PilotGroups::GetAllGroups());
$this->render('groups_grouplist.php');
}
/**
* PilotAdmin::ApprovePilot()
*
* @return
*/
protected function ApprovePilot() {
$this->checkPermission(MODERATE_REGISTRATIONS);
PilotData::AcceptPilot($this->post->id);
RanksData::CalculatePilotRanks();
$pilot = PilotData::getPilotData($this->post->id);
# Send pilot notification
$subject = Lang::gs('email.register.accepted.subject');
$this->set('pilot', $pilot);
$oldPath = Template::setTemplatePath(TEMPLATES_PATH);
$oldSkinPath = Template::setSkinPath(ACTIVE_SKIN_PATH);
$message = Template::getTemplate('email_registrationaccepted.php', true, true, true);
Template::setTemplatePath($oldPath);
Template::setSkinPath($oldSkinPath);
Util::SendEmail($pilot->email, $subject, $message);
CodonEvent::Dispatch('pilot_approved', 'PilotAdmin', $pilot);
LogData::addLog(Auth::$userinfo->pilotid, 'Approved ' . PilotData::getPilotCode($pilot->code, $pilot->pilotid) . ' - ' . $pilot->firstname . ' ' . $pilot->lastname);
}
/**
* PilotAdmin::RejectPilot()
*
* @return
*/
protected function RejectPilot() {
$this->checkPermission(MODERATE_REGISTRATIONS);
$pilot = PilotData::GetPilotData($this->post->id);
# Send pilot notification
$subject = Lang::gs('email.register.rejected.subject');
$this->set('pilot', $pilot);
$oldPath = Template::setTemplatePath(TEMPLATES_PATH);
$oldSkinPath = Template::setSkinPath(ACTIVE_SKIN_PATH);
$message = Template::Get('email_registrationdenied.php', true, true, true);
Template::setTemplatePath($oldPath);
Template::setSkinPath($oldSkinPath);
Util::SendEmail($pilot->email, $subject, $message);
# Reject in the end, since it's delted
PilotData::RejectPilot($this->post->id);
CodonEvent::Dispatch('pilot_rejected', 'PilotAdmin', $pilot);
LogData::addLog(
Auth::$userinfo->pilotid,
'Approved '.PilotData::getPilotCode($pilot->code, $pilot->pilotid).' - '.$pilot->firstname.' '.$pilot->lastname
);
}
/**
* PilotAdmin::ChangePassword()
*
* @return
*/
protected function ChangePassword() {
$password1 = $this->post->password1;
$password2 = $this->post->password2;
// Check password length
if (strlen($password1) <= 5) {
$this->set('message', Lang::gs('password.wrong.length'));
$this->render('core_message.php');
return;
}
// Check is passwords are the same
if ($password1 != $password2) {
$this->set('message', Lang::gs('password.no.match'));
$this->render('core_message.php');
return;
}
RegistrationData::ChangePassword($this->post->pilotid, $password1);
if (DB::errno() != 0) {
$this->set('message', 'There was an error, administrator has been notified');
$this->render('core_error.php');
} else {
$this->set('message', Lang::gs('password.changed'));
$this->render('core_success.php');
}
$pilot = PilotData::getPilotData($this->post->pilotid);
LogData::addLog(
Auth::$userinfo->pilotid,
'Changed the password for '.PilotData::getPilotCode($pilot->code, $pilot->pilotid).' - '.$pilot->firstname.' '.$pilot->lastname
);
}
/**
* PilotAdmin::AddAward()
*
* @return
*/
protected function AddAward() {
if ($this->post->awardid == '' || $this->post->pilotid == '')
return;
# Check if they already have this award
$award = AwardsData::GetPilotAward($this->post->pilotid, $this->post->awardid);
if ($award) {
$this->set('message', Lang::gs('award.exists'));
$this->render('core_error.php');
return;
}
AwardsData::AddAwardToPilot($this->post->pilotid, $this->post->awardid);
$pilot = PilotData::getPilotData($this->post->pilotid);
LogData::addLog(Auth::$userinfo->pilotid, 'Added and award to ' . PilotData::getPilotCode($pilot->code, $pilot->pilotid) . ' - ' . $pilot->firstname . ' ' . $pilot->lastname);
}
/**
* PilotAdmin::DeleteAward()
*
* @return
*/
protected function DeleteAward() {
AwardsData::DeletePilotAward($this->post->id);
if ($award) {
$this->set('message', Lang::gs('award.deleted'));
$this->render('core_success.php');
return;
}
}
}