summaryrefslogtreecommitdiff
path: root/includes/controller/user_driver_licenses_controller.php
blob: b18a78b6f4de5308e0e5002c0e7344d4dedf80ed (plain)
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
<?php

/**
 * Route user driver licenses actions.
 */
function user_driver_licenses_controller() {
  global $privileges, $user;
  
  if (! isset($user))
    redirect(page_link_to(''));
  
  if (! isset($_REQUEST['action']))
    $_REQUEST['action'] = 'edit';
  
  switch ($_REQUEST['action']) {
    default:
    case 'edit':
      return user_driver_license_edit_controller();
  }
}

/**
 * Link to user driver license edit page for given user.
 *
 * @param User $user          
 */
function user_driver_license_edit_link($user = null) {
  if ($user == null)
    return page_link_to('user_driver_licenses');
  return page_link_to('user_driver_licenses') . '&user_id=' . $user['UID'];
}

/**
 * Edit a users driver license information.
 */
function user_driver_license_edit_controller() {
  global $privileges, $user;
  
  if (isset($_REQUEST['user_id'])) {
    $user_source = User($_REQUEST['user_id']);
    if ($user_source === false)
      engelsystem_error('Unable to load angeltype.');
    if ($user_source == null)
      redirect(user_driver_license_edit_link());
      
      // only privilege admin_user can edit other users driver license information
    if ($user['UID'] != $user_source['UID'] && ! in_array('admin_user', $privileges))
      redirect(user_driver_license_edit_link());
  } else {
    $user_source = $user;
  }
  
  $wants_to_drive = false;
  $has_car = false;
  $has_license_car = false;
  $has_license_3_5t_transporter = false;
  $has_license_7_5t_truck = false;
  $has_license_12_5t_truck = false;
  $has_license_forklift = false;
  
  $user_driver_license = UserDriverLicense($user_source['UID']);
  if ($user_driver_license === false)
    engelsystem_error('Unable to load user driver license.');
  if ($user_driver_license != null) {
    $wants_to_drive = true;
    $has_car = $user_driver_license['has_car'];
    $has_license_car = $user_driver_license['has_license_car'];
    $has_license_3_5t_transporter = $user_driver_license['has_license_3_5t_transporter'];
    $has_license_7_5t_truck = $user_driver_license['has_license_7_5t_truck'];
    $has_license_12_5t_truck = $user_driver_license['has_license_12_5t_truck'];
    $has_license_forklift = $user_driver_license['has_license_forklift'];
  }
  
  if (isset($_REQUEST['submit'])) {
    $ok = true;
    $wants_to_drive = isset($_REQUEST['wants_to_drive']);
    $has_car = isset($_REQUEST['has_car']);
    $has_license_car = isset($_REQUEST['has_license_car']);
    $has_license_3_5t_transporter = isset($_REQUEST['has_license_3_5t_transporter']);
    $has_license_7_5t_truck = isset($_REQUEST['has_license_7_5t_truck']);
    $has_license_12_5t_truck = isset($_REQUEST['has_license_12_5t_truck']);
    $has_license_forklift = isset($_REQUEST['has_license_forklift']);
    
    if ($wants_to_drive && ! $has_license_car && ! $has_license_3_5t_transporter && ! $has_license_7_5t_truck && ! $has_license_12_5t_truck && ! $has_license_forklift) {
      $ok = false;
      error(_("Please select at least one driving license."));
    }
    
    if ($ok) {
      if (! $wants_to_drive && $user_driver_license != null) {
        $result = UserDriverLicenses_delete($user_source['UID']);
        if ($result === false)
          engelsystem_error('Unable to remove user driver license information');
        success(_("Your driver license information has been removed."));
      } else {
        if ($wants_to_drive) {
          if ($user_driver_license == null)
            $result = UserDriverLicenses_create($user_source['UID'], $has_car, $has_license_car, $has_license_3_5t_transporter, $has_license_7_5t_truck, $has_license_12_5t_truck, $has_license_forklift);
          else
            $result = UserDriverLicenses_update($user_source['UID'], $has_car, $has_license_car, $has_license_3_5t_transporter, $has_license_7_5t_truck, $has_license_12_5t_truck, $has_license_forklift);
          
          if ($result === false)
            engelsystem_error('Unable to save user driver license information.');
        }
        success(_("Your driver license information has been saved."));
      }
      
      redirect(user_link($user_source));
    }
  }
  
  return [
      sprintf(_("Edit %s driving license information"), $user_source['Nick']),
      UserDriverLicense_edit_view($user_source, $wants_to_drive, $has_car, $has_license_car, $has_license_3_5t_transporter, $has_license_7_5t_truck, $has_license_12_5t_truck, $has_license_forklift) 
  ];
}

?>