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

use Engelsystem\Database\DB;
use Engelsystem\ValidationResult;

/**
 * Returns an array containing the basic attributes of angeltypes.
 * FIXME! This is the big sign for needing entity objects
 */
function AngelType_new()
{
    return [
        'id'                      => null,
        'name'                    => '',
        'restricted'              => false,
        'no_self_signup'          => false,
        'description'             => '',
        'requires_driver_license' => false,
        'contact_user_id'         => null,
        'contact_name'            => null,
        'contact_dect'            => null,
        'contact_email'           => null
    ];
}

/**
 * Validates the contact user
 *
 * @param array $angeltype The angeltype
 * @return ValidationResult
 */
function AngelType_validate_contact_user_id($angeltype)
{
    if (!isset($angeltype['contact_user_id'])) {
        return new ValidationResult(true, null);
    }
    if (isset($angeltype['contact_name']) || isset($angeltype['contact_dect']) || isset($angeltype['contact_email'])) {
        return new ValidationResult(false, $angeltype['contact_user_id']);
    }
    if (User($angeltype['contact_user_id']) == null) {
        return new ValidationResult(false, $angeltype['contact_user_id']);
    }
    return new ValidationResult(true, $angeltype['contact_user_id']);
}

/**
 * Returns contact data (name, dect, email) for given angeltype or null
 *
 * @param array $angeltype The angeltype
 * @return array|null
 */
function AngelType_contact_info($angeltype)
{
    if (isset($angeltype['contact_user_id'])) {
        $contact_user = User($angeltype['contact_user_id']);
        $contact_data = [
            'contact_name' => $contact_user['Nick'],
            'contact_dect' => $contact_user['DECT']
        ];
        if ($contact_user['email_by_human_allowed']) {
            $contact_data['contact_email'] = $contact_user['email'];
        }
        return $contact_data;
    }
    if (isset($angeltype['contact_name'])) {
        return [
            'contact_name'  => $angeltype['contact_name'],
            'contact_dect'  => $angeltype['contact_dect'],
            'contact_email' => $angeltype['contact_email']
        ];
    }

    return null;
}

/**
 * Delete an Angeltype.
 *
 * @param array $angeltype
 */
function AngelType_delete($angeltype)
{
    DB::delete('
      DELETE FROM `AngelTypes`
      WHERE `id`=?
      LIMIT 1
    ', [$angeltype['id']]);
    engelsystem_log('Deleted angeltype: ' . AngelType_name_render($angeltype));
}

/**
 * Update Angeltype.
 *
 * @param array $angeltype The angeltype
 */
function AngelType_update($angeltype)
{
    DB::update('
          UPDATE `AngelTypes` SET
          `name` = ?,
          `restricted` = ?,
          `description` = ?,
          `requires_driver_license` = ?,
          `no_self_signup` = ?,
          `contact_user_id` = ?,
          `contact_name` = ?,
          `contact_dect` = ?,
          `contact_email` = ?
          WHERE `id` = ?',
        [
            $angeltype['name'],
            $angeltype['restricted'],
            $angeltype['description'],
            $angeltype['requires_driver_license'],
            $angeltype['no_self_signup'],
            $angeltype['contact_user_id'],
            $angeltype['contact_name'],
            $angeltype['contact_dect'],
            $angeltype['contact_email'],
            $angeltype['id'],
        ]
    );

    engelsystem_log(
        'Updated angeltype: ' . $angeltype['name'] . ($angeltype['restricted'] ? ', restricted' : '')
        . ($angeltype['no_self_signup'] ? ', no_self_signup' : '')
        . ($angeltype['requires_driver_license'] ? ', requires driver license' : '')
    );
}

/**
 * Create an Angeltype.
 *
 * @param array $angeltype The angeltype
 * @return array the created angeltype
 */
function AngelType_create($angeltype)
{
    DB::insert('
          INSERT INTO `AngelTypes` (
              `name`,
              `restricted`,
              `description`,
              `requires_driver_license`,
              `no_self_signup`,
              `contact_user_id`,
              `contact_name`,
              `contact_dect`,
              `contact_email`
          )
          VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
        ',
        [
            $angeltype['name'],
            (bool)$angeltype['restricted'],
            $angeltype['description'],
            (bool)$angeltype['requires_driver_license'],
            (bool)$angeltype['no_self_signup'],
            $angeltype['contact_user_id'],
            $angeltype['contact_name'],
            $angeltype['contact_dect'],
            $angeltype['contact_email'],
        ]
    );

    $angeltype['id'] = DB::getPdo()->lastInsertId();
    engelsystem_log(
        'Created angeltype: ' . $angeltype['name']
        . ($angeltype['restricted'] ? ', restricted' : '')
        . ($angeltype['requires_driver_license'] ? ', requires driver license' : '')
    );
    return $angeltype;
}

/**
 * Validates a name for angeltypes.
 * Returns ValidationResult containing validation success and validated name.
 *
 * @param string $name      Wanted name for the angeltype
 * @param array  $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 = (count(DB::select('
            SELECT `id`
            FROM `AngelTypes`
            WHERE `name`=?
            AND NOT `id`=?
            LIMIT 1
        ', [$name, $angeltype['id']])) == 0);
        return new ValidationResult($valid, $name);
    }
    $valid = (count(DB::select('
        SELECT `id`
        FROM `AngelTypes`
        WHERE `name`=?
        LIMIT 1', [$name])) == 0);
    return new ValidationResult($valid, $name);
}

/**
 * Returns all angeltypes and subscription state to each of them for given user.
 *
 * @param array $user
 * @return array
 */
function AngelTypes_with_user($user)
{
    return DB::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` = ?
      ORDER BY `name`', [$user['UID']]);
}

/**
 * Returns all angeltypes.
 *
 * @return array
 */
function AngelTypes()
{
    return DB::select('
      SELECT *
      FROM `AngelTypes`
      ORDER BY `name`');
}

/**
 * Returns AngelType id array
 *
 * @return array
 */
function AngelType_ids()
{
    $result = DB::select('SELECT `id` FROM `AngelTypes`');
    return select_array($result, 'id', 'id');
}

/**
 * Returns angelType by id.
 *
 * @param int $angeltype_id angelType ID
 * @return array|null
 */
function AngelType($angeltype_id)
{
    $angelType_source = DB::select(
        'SELECT * FROM `AngelTypes` WHERE `id`=?',
        [$angeltype_id]
    );

    if (empty($angelType_source)) {
        return null;
    }

    return array_shift($angelType_source);
}