summaryrefslogtreecommitdiff
path: root/includes/model/AngelType_model.php
blob: fa81349dbf0c33435baabc66a2eb8784291845cb (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
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
<?php

/**
 * Delete an Angeltype.
 * 
 * @param Angeltype $angeltype          
 */
function AngelType_delete($angeltype) {
  return sql_query("
      DELETE FROM `AngelTypes` 
      WHERE `id`='" . sql_escape($angeltype['id']) . "' 
      LIMIT 1");
}

/**
 * Update Angeltype.
 *
 * @param int $angeltype_id          
 * @param string $name          
 * @param bool $restricted          
 * @param string $description          
 * @param bool $requires_driver_license          
 */
function AngelType_update($angeltype_id, $name, $restricted, $description, $requires_driver_license) {
  return sql_query("
      UPDATE `AngelTypes` SET 
      `name`='" . sql_escape($name) . "', 
      `restricted`=" . sql_bool($restricted) . ",
      `description`='" . sql_escape($description) . "',
      `requires_driver_license`=" . sql_bool($requires_driver_license) . "
      WHERE `id`='" . sql_escape($angeltype_id) . "' 
      LIMIT 1");
}

/**
 * Create an Angeltype.
 *
 * @param string $name          
 * @param boolean $restricted          
 * @param string $description          
 * @return New Angeltype id
 */
function AngelType_create($name, $restricted, $description, $requires_driver_license) {
  $result = sql_query("
      INSERT INTO `AngelTypes` SET 
      `name`='" . sql_escape($name) . "', 
      `restricted`=" . sql_bool($restricted) . ",
      `description`='" . sql_escape($description) . "',
      `requires_driver_license`=" . sql_bool($requires_driver_license));
  if ($result === false) {
    return false;
  }
  return sql_id();
}

/**
 * Validates a name for angeltypes.
 * Returns ValidationResult containing validation success and validated name.
 *
 * @param string $name
 *          Wanted name for the angeltype
 * @param AngelType $angeltype
 *          The angeltype the name is for
 * @return ValidationResult result and validated name
 */
function AngelType_validate_name($name, $angeltype) {
  $name = strip_item($name);
  if ($name == "") {
    return new ValidationResult(false, "");
  }
  if ($angeltype != null && isset($angeltype['id'])) {
    $valid = sql_num_query("
        SELECT * 
        FROM `AngelTypes` 
        WHERE `name`='" . sql_escape($name) . "' 
        AND NOT `id`='" . sql_escape($angeltype['id']) . "'
        LIMIT 1") == 0;
    return new ValidationResult($valid, $name);
  }
  $valid = sql_num_query("
        SELECT `id` 
        FROM `AngelTypes` 
        WHERE `name`='" . sql_escape($name) . "' 
        LIMIT 1") == 0;
  return new ValidationResult($valid, $name);
}

/**
 * Returns all angeltypes and subscription state to each of them for given user.
 *
 * @param User $user          
 */
function AngelTypes_with_user($user) {
  $result = sql_select("
      SELECT `AngelTypes`.*, 
      `UserAngelTypes`.`id` as `user_angeltype_id`,
      `UserAngelTypes`.`confirm_user_id`,
      `UserAngelTypes`.`supporter`
      FROM `AngelTypes` 
      LEFT JOIN `UserAngelTypes` ON `AngelTypes`.`id`=`UserAngelTypes`.`angeltype_id` 
      AND `UserAngelTypes`.`user_id`=" . $user['UID'] . "
      ORDER BY `name`");
  if ($result === false) {
    engelsystem_error("Unable to load angeltypes.");
  }
  return $result;
}

/**
 * Returns all angeltypes.
 */
function AngelTypes() {
  $result = sql_select("
      SELECT * 
      FROM `AngelTypes` 
      ORDER BY `name`");
  if ($result === false) {
    engelsystem_error("Unable to load angeltypes.");
  }
  return $result;
}

/**
 * Returns AngelType id array
 */
function AngelType_ids() {
  $result = sql_select("SELECT `id` FROM `AngelTypes`");
  if ($result === false) {
    engelsystem_error("Unable to load angeltypes.");
  }
  return select_array($result, 'id', 'id');
}

/**
 * Returns angelType by id.
 *
 * @param $angeltype_id angelType
 *          ID
 */
function AngelType($angeltype_id) {
  $angelType_source = sql_select("SELECT * FROM `AngelTypes` WHERE `id`='" . sql_escape($angeltype_id) . "' LIMIT 1");
  if ($angelType_source === false) {
    engelsystem_error("Unable to load angeltype.");
  }
  if (count($angelType_source) > 0) {
    return $angelType_source[0];
  }
  return null;
}

?>