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
|
<?php
/**
* DatafieldCondition.php
*
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* @author André Noack <noack@data-quest.de>
* @license http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
* @category Stud.IP
*/
namespace UserFilterFields;
class DatafieldCondition extends \UserFilterField
{
public static $isParameterized = true;
public $datafield_id, $null_yields, $datafield_name;
public static $sortOrder = 6;
public static function getParameterizedTypes()
{
$ret = [];
try {
foreach (\DataField::findBySQL("object_type='user' AND (object_class & (1|2|4|8) OR object_class IS NULL) AND is_userfilter = 1 ORDER BY priority") as $df) {
$ret[__CLASS__ . '_' . $df->id] = utf8_encode(chr(160)) . _("Datenfeld") . ': ' . $df->name;
}
} catch (\PDOException $e) {} //migration 128 chokes on this...
return $ret;
}
/**
* @see UserFilterField::__construct
*/
public function __construct($typeparam, $fieldId = '')
{
$this->validCompareOperators = [
'>=' => _('mindestens'),
'<=' => _('höchstens'),
'=' => _('ist'),
'!=' => _('ist nicht')
];
if ($fieldId) {
$this->id = $fieldId;
$this->load();
} else {
$this->id = $this->generateId();
$this->datafield_id = $typeparam;
}
$df = \DataField::find($this->datafield_id);
if ($df) {
$this->datafield_name = $df->name;
} else {
throw new \UnexpectedValueException('datafield not found, id: ' . $typeparam);
}
$typed_df = \DataFieldEntry::createDataFieldEntry($df);
if ($typed_df instanceof \DataFieldBoolEntry) {
$this->validValues = [1 => _('Ja'), 0 => _('Nein')];
unset($this->validCompareOperators['>=']);
unset($this->validCompareOperators['<=']);
unset($this->validCompareOperators['!=']);
$this->null_yields = 0;
} else if ($typed_df instanceof \DataFieldSelectboxEntry) {
list($valid_values, $is_assoc) = $typed_df->getParameters();
if (!$is_assoc) {
$valid_values = array_combine($valid_values, $valid_values);
}
$this->validValues = $valid_values;
$this->null_yields = $typed_df instanceof \DataFieldSelectboxMultipleEntry ? '' : key($valid_values);
} else {
$this->null_yields = '';
}
}
/**
* Get this field's display name.
*
* @return String
*/
public function getName()
{
return $this->datafield_name;
}
public function getUsers($restrictions = [])
{
$db = \DBManager::get();
// Standard query getting the values without respecting other values.
$select = "SELECT user_id FROM
auth_user_md5 LEFT JOIN
datafields_entries ON range_id = user_id AND datafield_id = ?
WHERE perms IN ('user','autor','tutor','dozent') AND IFNULL(content, ?)
" . $this->compareOperator . " ?";
$users = $db->fetchFirst($select, [$this->datafield_id, $this->null_yields,$this->value]);
return $users;
}
/**
* Gets the value for the given user that is relevant for this
*
* @param String $userId User to check.
* @param Array $additional additional conditions that are required for check.
* @return array The value(s) for this user.
*/
public function getUserValues($userId, $additional = null)
{
$result = \DBManager::get()->fetchColumn(
"SELECT content FROM datafields_entries
WHERE datafield_id = ? AND range_id = ?", [$this->datafield_id, $userId]);
return [$result === null || $result === false ? $this->null_yields : $result];
}
/**
* Helper function for loading data from DB.
*/
public function load()
{
$stmt = \DBManager::get()->prepare(
"SELECT * FROM `userfilter_fields` WHERE `field_id`=? LIMIT 1");
$stmt->execute([$this->id]);
if ($data = $stmt->fetch(\PDO::FETCH_ASSOC)) {
$this->conditionId = $data['filter_id'];
$this->value = $data['value'];
$this->compareOperator = $data['compare_op'];
list(,$this->datafield_id) = explode('_', $data['type']);
}
}
/**
* Sets a new selected value.
*
* @param String newValue
* @return UserFilterField
*/
public function setValue($newValue)
{
$this->value = $newValue;
return $this;
}
/**
* Stores data to DB.
*
*/
public function store()
{
// Generate new ID if field entry doesn't exist in DB yet.
if (!$this->id) {
$this->id = $this->generateId();
}
// Store field data.
$stmt = \DBManager::get()->prepare("INSERT INTO `userfilter_fields`
(`field_id`, `filter_id`, `type`, `value`, `compare_op`,
`mkdate`, `chdate`) VALUES (?, ?, ?, ?, ?, ?, ?)
ON DUPLICATE KEY UPDATE `filter_id`=VALUES(`filter_id`),
`type`=VALUES(`type`),`value`=VALUES(`value`),
`compare_op`=VALUES(`compare_op`), `chdate`=VALUES(`chdate`)");
$stmt->execute([$this->id, $this->conditionId, get_class($this).'_'.$this->datafield_id,
$this->value, $this->compareOperator, time(), time()]);
}
}
|