This repository has been archived by the owner on Dec 18, 2021. It is now read-only.
/
class.xform.select_sql.inc.php
214 lines (163 loc) · 6.66 KB
/
class.xform.select_sql.inc.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
<?php
/**
* XForm
* @author jan.kristinus[at]redaxo[dot]org Jan Kristinus
* @author <a href="http://www.yakamara.de">www.yakamara.de</a>
*/
class rex_xform_select_sql extends rex_xform_abstract
{
static $getListValues = array();
function enterObject()
{
$multiple = $this->getElement('multiple') == 1;
// ----- query
$sql = $this->getElement('query');
$options_sql = rex_sql::factory();
$options_sql->debugsql = $this->params['debug'];
$options_sql->setQuery($sql);
$options = array();
$option_names = array();
foreach ($options_sql->getArray() as $t) {
$v = $t['name'];
$k = $t['id'];
$options[$k] = $v;
$option_names[$k] = $t['name'];
}
// ----- default value
if ($this->getValue() == '' && $this->getElement('default') != '') {
$this->setValue($this->getElement('default'));
}
if ($multiple) {
$size = (int) $this->getElement('size');
if ($size < 2) {
$size = count($options);
}
} else {
$size = 1;
// mit --- keine auswahl ---
if ($this->getElement('empty_option') == 1) {
$options = array('0' => $this->getElement('empty_value')) + $options;
}
}
if (!is_array($this->getValue())) {
$this->setValue(explode(',', stripslashes($this->getValue())));
}
// ---------- rex_xform_set
if (isset($this->params['rex_xform_set'][$this->getName()]) && !is_array($this->params['rex_xform_set'][$this->getName()])) {
$value = $this->params['rex_xform_set'][$this->getName()];
$values = array();
if (array_key_exists($value, $options)) {
$values[] = $value;
}
$this->setValue($values);
$this->setElement('disabled', true);
}
// ----------
$this->params['form_output'][$this->getId()] = $this->parse('value.select.tpl.php', compact('options', 'multiple', 'size'));
$this->setValue(implode(',', $this->getValue()));
$this->params['value_pool']['email'][$this->getElement(1)] = stripslashes($this->getValue());
if ($this->getElement(5) != 'no_db') {
$this->params['value_pool']['sql'][$this->getElement(1)] = $this->getValue();
}
}
function getDescription()
{
return 'select_sql -> Beispiel: select_sql|label|Bezeichnung:|select id,name from table order by name|[defaultvalue]|[no_db]|1/0 Leeroption|Leeroptionstext|1/0 Multiple Feld|selectsize';
}
function getDefinitions()
{
return array(
'type' => 'value',
'name' => 'select_sql',
'values' => array(
'name' => array( 'type' => 'name', 'label' => 'Name' ),
'label' => array( 'type' => 'text', 'label' => 'Bezeichnung'),
'query' => array( 'type' => 'text', 'label' => 'Query mit "select id, name from .."'),
'default' => array( 'type' => 'text', 'label' => 'Defaultwert (opt.)'),
'no_db' => array( 'type' => 'no_db', 'label' => 'Datenbank', 'default' => 0),
'empty_option' => array( 'type' => 'boolean', 'label' => 'Leeroption'),
'empty_value' => array( 'type' => 'text', 'label' => 'Text bei Leeroption (Bitte auswählen)'),
'multiple' => array( 'type' => 'boolean', 'label' => 'Mehrere Felder möglich'),
'size' => array( 'type' => 'text', 'label' => 'Höhe der Auswahlbox')
),
'description' => 'Hiermit kann man SQL Abfragen als Selectbox nutzen',
'dbtype' => 'text'
);
}
static function getListValue($params)
{
$return = array();
$query = $params['params']['field']['query'];
$pos = strrpos(strtoupper($query), 'ORDER BY ');
if ( $pos !== false) {
$query = substr($query, 0, $pos);
}
$pos = strrpos(strtoupper($query), 'LIMIT ');
if ( $pos !== false) {
$query = substr($query, 0, $pos);
}
$multiple = (isset($params['params']['field']['multiple'])) ? (int) $params['params']['field']['multiple'] : 0;
if ($multiple != 1) {
$where = ' `id`="' . mysql_real_escape_string($params['value']) . '"';
} else {
$where = ' FIND_IN_SET(`id`,"' . mysql_real_escape_string($params['value']) . '")';
}
$pos = strrpos(strtoupper($query), 'WHERE ');
if ( $pos !== false) {
$query = substr($query, 0, $pos) . ' WHERE ' . $where . ' AND ' . substr($query, $pos + strlen('WHERE '));
} else {
$query .= ' WHERE ' . $where;
}
$db = rex_sql::factory();
// $db->debugsql = 1;
$db_array = $db->getArray($query);
foreach ($db_array as $entry) {
$return[] = $entry['name'];
}
if (count($return) == 0 && $params['value'] != '' && $params['value'] != '0') {
$return[] = $params['value'];
}
return implode('<br />', $return);
}
public static function getSearchField($params)
{
$options = array();
$options['(empty)'] = "(empty)";
$options['!(empty)'] = "!(empty)";
$options_sql = rex_sql::factory();
$options_sql->setQuery($params['field']['query']);
foreach ($options_sql->getArray() as $t) {
$options[$t['id']] = $t['name'];
}
$params['searchForm']->setValueField('select', array(
'name' => $params['field']->getName(),
'label' => $params['field']->getLabel(),
'options' => $options,
'multiple' => 1,
'size' => 5,
)
);
}
public static function getSearchFilter($params)
{
$field = $params['field']->getName();
$values = (array) $params['value'];
$where = array();
foreach($values as $value) {
switch($value){
case("(empty)"):
$where[] = '`'.$field.'`=""';
break;
case("!(empty)"):
$where[] = '`'.$field.'`!=""';
break;
default:
$where[] = ' ( FIND_IN_SET("' . mysql_real_escape_string($value) . '", `' . mysql_real_escape_string($field) . '`) )';
break;
}
}
if (count($where) > 0) {
return ' ( ' . implode(" or ", $where) . ' )';
}
}
}