source: trunk/forum/includes/acp/acp_profile.php

Last change on this file was 702, checked in by george, 15 years ago
  • Upraveno: Aktualizace fóra.
File size: 49.4 KB
Line 
1<?php
2/**
3*
4* @package acp
5* @version $Id$
6* @copyright (c) 2005 phpBB Group
7* @license http://opensource.org/licenses/gpl-license.php GNU Public License
8*
9*/
10
11/**
12* @ignore
13*/
14if (!defined('IN_PHPBB'))
15{
16 exit;
17}
18
19/**
20* @package acp
21*/
22class acp_profile
23{
24 var $u_action;
25
26 var $edit_lang_id;
27 var $lang_defs;
28
29 function main($id, $mode)
30 {
31 global $config, $db, $user, $auth, $template, $cache;
32 global $phpbb_root_path, $phpbb_admin_path, $phpEx, $table_prefix;
33
34 include($phpbb_root_path . 'includes/functions_posting.' . $phpEx);
35 include($phpbb_root_path . 'includes/functions_user.' . $phpEx);
36 include($phpbb_root_path . 'includes/functions_profile_fields.' . $phpEx);
37
38 $user->add_lang(array('ucp', 'acp/profile'));
39 $this->tpl_name = 'acp_profile';
40 $this->page_title = 'ACP_CUSTOM_PROFILE_FIELDS';
41
42 $action = (isset($_POST['create'])) ? 'create' : request_var('action', '');
43
44 $error = array();
45 $s_hidden_fields = '';
46
47 // Define some default values for each field type
48 $default_values = array(
49 FIELD_STRING => array('field_length' => 10, 'field_minlen' => 0, 'field_maxlen' => 20, 'field_validation' => '.*', 'field_novalue' => '', 'field_default_value' => ''),
50 FIELD_TEXT => array('field_length' => '5|80', 'field_minlen' => 0, 'field_maxlen' => 1000, 'field_validation' => '.*', 'field_novalue' => '', 'field_default_value' => ''),
51 FIELD_INT => array('field_length' => 5, 'field_minlen' => 0, 'field_maxlen' => 100, 'field_validation' => '', 'field_novalue' => 0, 'field_default_value' => 0),
52 FIELD_DATE => array('field_length' => 10, 'field_minlen' => 10, 'field_maxlen' => 10, 'field_validation' => '', 'field_novalue' => ' 0- 0- 0', 'field_default_value' => ' 0- 0- 0'),
53 FIELD_BOOL => array('field_length' => 1, 'field_minlen' => 0, 'field_maxlen' => 0, 'field_validation' => '', 'field_novalue' => 0, 'field_default_value' => 0),
54 FIELD_DROPDOWN => array('field_length' => 0, 'field_minlen' => 0, 'field_maxlen' => 5, 'field_validation' => '', 'field_novalue' => 0, 'field_default_value' => 0),
55 );
56
57 $cp = new custom_profile_admin();
58
59 // Build Language array
60 // Based on this, we decide which elements need to be edited later and which language items are missing
61 $this->lang_defs = array();
62
63 $sql = 'SELECT lang_id, lang_iso
64 FROM ' . LANG_TABLE . '
65 ORDER BY lang_english_name';
66 $result = $db->sql_query($sql);
67
68 while ($row = $db->sql_fetchrow($result))
69 {
70 // Make some arrays with all available languages
71 $this->lang_defs['id'][$row['lang_id']] = $row['lang_iso'];
72 $this->lang_defs['iso'][$row['lang_iso']] = $row['lang_id'];
73 }
74 $db->sql_freeresult($result);
75
76 $sql = 'SELECT field_id, lang_id
77 FROM ' . PROFILE_LANG_TABLE . '
78 ORDER BY lang_id';
79 $result = $db->sql_query($sql);
80
81 while ($row = $db->sql_fetchrow($result))
82 {
83 // Which languages are available for each item
84 $this->lang_defs['entry'][$row['field_id']][] = $row['lang_id'];
85 }
86 $db->sql_freeresult($result);
87
88 // Have some fields been defined?
89 if (isset($this->lang_defs['entry']))
90 {
91 foreach ($this->lang_defs['entry'] as $field_id => $field_ary)
92 {
93 // Fill an array with the languages that are missing for each field
94 $this->lang_defs['diff'][$field_id] = array_diff(array_values($this->lang_defs['iso']), $field_ary);
95 }
96 }
97
98 switch ($action)
99 {
100 case 'delete':
101 $field_id = request_var('field_id', 0);
102
103 if (!$field_id)
104 {
105 trigger_error($user->lang['NO_FIELD_ID'] . adm_back_link($this->u_action), E_USER_WARNING);
106 }
107
108 if (confirm_box(true))
109 {
110 $sql = 'SELECT field_ident
111 FROM ' . PROFILE_FIELDS_TABLE . "
112 WHERE field_id = $field_id";
113 $result = $db->sql_query($sql);
114 $field_ident = (string) $db->sql_fetchfield('field_ident');
115 $db->sql_freeresult($result);
116
117 $db->sql_transaction('begin');
118
119 $db->sql_query('DELETE FROM ' . PROFILE_FIELDS_TABLE . " WHERE field_id = $field_id");
120 $db->sql_query('DELETE FROM ' . PROFILE_FIELDS_LANG_TABLE . " WHERE field_id = $field_id");
121 $db->sql_query('DELETE FROM ' . PROFILE_LANG_TABLE . " WHERE field_id = $field_id");
122
123 switch ($db->sql_layer)
124 {
125 case 'sqlite':
126 $sql = "SELECT sql
127 FROM sqlite_master
128 WHERE type = 'table'
129 AND name = '" . PROFILE_FIELDS_DATA_TABLE . "'
130 ORDER BY type DESC, name;";
131 $result = $db->sql_query($sql);
132 $row = $db->sql_fetchrow($result);
133 $db->sql_freeresult($result);
134
135 // Create a temp table and populate it, destroy the existing one
136 $db->sql_query(preg_replace('#CREATE\s+TABLE\s+"?' . PROFILE_FIELDS_DATA_TABLE . '"?#i', 'CREATE TEMPORARY TABLE ' . PROFILE_FIELDS_DATA_TABLE . '_temp', $row['sql']));
137 $db->sql_query('INSERT INTO ' . PROFILE_FIELDS_DATA_TABLE . '_temp SELECT * FROM ' . PROFILE_FIELDS_DATA_TABLE);
138 $db->sql_query('DROP TABLE ' . PROFILE_FIELDS_DATA_TABLE);
139
140 preg_match('#\((.*)\)#s', $row['sql'], $matches);
141
142 $new_table_cols = trim($matches[1]);
143 $old_table_cols = preg_split('/,(?=[\\sa-z])/im', $new_table_cols);
144 $column_list = array();
145
146 foreach ($old_table_cols as $declaration)
147 {
148 $entities = preg_split('#\s+#', trim($declaration));
149
150 if ($entities[0] == 'PRIMARY')
151 {
152 continue;
153 }
154
155 if ($entities[0] !== 'pf_' . $field_ident)
156 {
157 $column_list[] = $entities[0];
158 }
159 }
160
161 $columns = implode(',', $column_list);
162
163 $new_table_cols = preg_replace('/' . 'pf_' . $field_ident . '[^,]+,/', '', $new_table_cols);
164
165 // create a new table and fill it up. destroy the temp one
166 $db->sql_query('CREATE TABLE ' . PROFILE_FIELDS_DATA_TABLE . ' (' . $new_table_cols . ');');
167 $db->sql_query('INSERT INTO ' . PROFILE_FIELDS_DATA_TABLE . ' (' . $columns . ') SELECT ' . $columns . ' FROM ' . PROFILE_FIELDS_DATA_TABLE . '_temp;');
168 $db->sql_query('DROP TABLE ' . PROFILE_FIELDS_DATA_TABLE . '_temp');
169 break;
170
171 default:
172 $db->sql_query('ALTER TABLE ' . PROFILE_FIELDS_DATA_TABLE . " DROP COLUMN pf_$field_ident");
173 }
174
175 $order = 0;
176
177 $sql = 'SELECT *
178 FROM ' . PROFILE_FIELDS_TABLE . '
179 ORDER BY field_order';
180 $result = $db->sql_query($sql);
181
182 while ($row = $db->sql_fetchrow($result))
183 {
184 $order++;
185 if ($row['field_order'] != $order)
186 {
187 $sql = 'UPDATE ' . PROFILE_FIELDS_TABLE . "
188 SET field_order = $order
189 WHERE field_id = {$row['field_id']}";
190 $db->sql_query($sql);
191 }
192 }
193 $db->sql_freeresult($result);
194
195 $db->sql_transaction('commit');
196
197 add_log('admin', 'LOG_PROFILE_FIELD_REMOVED', $field_ident);
198 trigger_error($user->lang['REMOVED_PROFILE_FIELD'] . adm_back_link($this->u_action));
199 }
200 else
201 {
202 confirm_box(false, 'DELETE_PROFILE_FIELD', build_hidden_fields(array(
203 'i' => $id,
204 'mode' => $mode,
205 'action' => $action,
206 'field_id' => $field_id,
207 )));
208 }
209
210 break;
211
212 case 'activate':
213 $field_id = request_var('field_id', 0);
214
215 if (!$field_id)
216 {
217 trigger_error($user->lang['NO_FIELD_ID'] . adm_back_link($this->u_action), E_USER_WARNING);
218 }
219
220 $sql = 'SELECT lang_id
221 FROM ' . LANG_TABLE . "
222 WHERE lang_iso = '" . $db->sql_escape($config['default_lang']) . "'";
223 $result = $db->sql_query($sql);
224 $default_lang_id = (int) $db->sql_fetchfield('lang_id');
225 $db->sql_freeresult($result);
226
227 if (!in_array($default_lang_id, $this->lang_defs['entry'][$field_id]))
228 {
229 trigger_error($user->lang['DEFAULT_LANGUAGE_NOT_FILLED'] . adm_back_link($this->u_action), E_USER_WARNING);
230 }
231
232 $sql = 'UPDATE ' . PROFILE_FIELDS_TABLE . "
233 SET field_active = 1
234 WHERE field_id = $field_id";
235 $db->sql_query($sql);
236
237 $sql = 'SELECT field_ident
238 FROM ' . PROFILE_FIELDS_TABLE . "
239 WHERE field_id = $field_id";
240 $result = $db->sql_query($sql);
241 $field_ident = (string) $db->sql_fetchfield('field_ident');
242 $db->sql_freeresult($result);
243
244 add_log('admin', 'LOG_PROFILE_FIELD_ACTIVATE', $field_ident);
245 trigger_error($user->lang['PROFILE_FIELD_ACTIVATED'] . adm_back_link($this->u_action));
246
247 break;
248
249 case 'deactivate':
250 $field_id = request_var('field_id', 0);
251
252 if (!$field_id)
253 {
254 trigger_error($user->lang['NO_FIELD_ID'] . adm_back_link($this->u_action), E_USER_WARNING);
255 }
256
257 $sql = 'UPDATE ' . PROFILE_FIELDS_TABLE . "
258 SET field_active = 0
259 WHERE field_id = $field_id";
260 $db->sql_query($sql);
261
262 $sql = 'SELECT field_ident
263 FROM ' . PROFILE_FIELDS_TABLE . "
264 WHERE field_id = $field_id";
265 $result = $db->sql_query($sql);
266 $field_ident = (string) $db->sql_fetchfield('field_ident');
267 $db->sql_freeresult($result);
268
269 add_log('admin', 'LOG_PROFILE_FIELD_DEACTIVATE', $field_ident);
270 trigger_error($user->lang['PROFILE_FIELD_DEACTIVATED'] . adm_back_link($this->u_action));
271
272 break;
273
274 case 'move_up':
275 case 'move_down':
276 $field_order = request_var('order', 0);
277 $order_total = $field_order * 2 + (($action == 'move_up') ? -1 : 1);
278
279 $sql = 'UPDATE ' . PROFILE_FIELDS_TABLE . "
280 SET field_order = $order_total - field_order
281 WHERE field_order IN ($field_order, " . (($action == 'move_up') ? $field_order - 1 : $field_order + 1) . ')';
282 $db->sql_query($sql);
283
284 break;
285
286 case 'create':
287 case 'edit':
288
289 $field_id = request_var('field_id', 0);
290 $step = request_var('step', 1);
291
292 $submit = (isset($_REQUEST['next']) || isset($_REQUEST['prev'])) ? true : false;
293 $save = (isset($_REQUEST['save'])) ? true : false;
294
295 // The language id of default language
296 $this->edit_lang_id = $this->lang_defs['iso'][$config['default_lang']];
297
298 // We are editing... we need to grab basic things
299 if ($action == 'edit')
300 {
301 if (!$field_id)
302 {
303 trigger_error($user->lang['NO_FIELD_ID'] . adm_back_link($this->u_action), E_USER_WARNING);
304 }
305
306 $sql = 'SELECT l.*, f.*
307 FROM ' . PROFILE_LANG_TABLE . ' l, ' . PROFILE_FIELDS_TABLE . ' f
308 WHERE l.lang_id = ' . $this->edit_lang_id . "
309 AND f.field_id = $field_id
310 AND l.field_id = f.field_id";
311 $result = $db->sql_query($sql);
312 $field_row = $db->sql_fetchrow($result);
313 $db->sql_freeresult($result);
314
315 if (!$field_row)
316 {
317 // Some admin changed the default language?
318 $sql = 'SELECT l.*, f.*
319 FROM ' . PROFILE_LANG_TABLE . ' l, ' . PROFILE_FIELDS_TABLE . ' f
320 WHERE l.lang_id <> ' . $this->edit_lang_id . "
321 AND f.field_id = $field_id
322 AND l.field_id = f.field_id";
323 $result = $db->sql_query($sql);
324 $field_row = $db->sql_fetchrow($result);
325 $db->sql_freeresult($result);
326
327 if (!$field_row)
328 {
329 trigger_error($user->lang['FIELD_NOT_FOUND'] . adm_back_link($this->u_action), E_USER_WARNING);
330 }
331
332 $this->edit_lang_id = $field_row['lang_id'];
333 }
334 $field_type = $field_row['field_type'];
335
336 // Get language entries
337 $sql = 'SELECT *
338 FROM ' . PROFILE_FIELDS_LANG_TABLE . '
339 WHERE lang_id = ' . $this->edit_lang_id . "
340 AND field_id = $field_id
341 ORDER BY option_id ASC";
342 $result = $db->sql_query($sql);
343
344 $lang_options = array();
345 while ($row = $db->sql_fetchrow($result))
346 {
347 $lang_options[$row['option_id']] = $row['lang_value'];
348 }
349 $db->sql_freeresult($result);
350
351 $s_hidden_fields = '<input type="hidden" name="field_id" value="' . $field_id . '" />';
352 }
353 else
354 {
355 // We are adding a new field, define basic params
356 $lang_options = $field_row = array();
357
358 $field_type = request_var('field_type', 0);
359
360 if (!$field_type)
361 {
362 trigger_error($user->lang['NO_FIELD_TYPE'] . adm_back_link($this->u_action), E_USER_WARNING);
363 }
364
365 $field_row = array_merge($default_values[$field_type], array(
366 'field_ident' => str_replace(' ', '_', utf8_clean_string(request_var('field_ident', '', true))),
367 'field_required' => 0,
368 'field_hide' => 0,
369 'field_show_profile'=> 0,
370 'field_no_view' => 0,
371 'field_show_on_reg' => 0,
372 'field_show_on_vt' => 0,
373 'lang_name' => utf8_normalize_nfc(request_var('field_ident', '', true)),
374 'lang_explain' => '',
375 'lang_default_value'=> '')
376 );
377
378 $s_hidden_fields = '<input type="hidden" name="field_type" value="' . $field_type . '" />';
379 }
380
381 // $exclude contains the data we gather in each step
382 $exclude = array(
383 1 => array('field_ident', 'lang_name', 'lang_explain', 'field_option_none', 'field_show_on_reg', 'field_show_on_vt', 'field_required', 'field_hide', 'field_show_profile', 'field_no_view'),
384 2 => array('field_length', 'field_maxlen', 'field_minlen', 'field_validation', 'field_novalue', 'field_default_value'),
385 3 => array('l_lang_name', 'l_lang_explain', 'l_lang_default_value', 'l_lang_options')
386 );
387
388 // Text-based fields require the lang_default_value to be excluded
389 if ($field_type == FIELD_STRING || $field_type == FIELD_TEXT)
390 {
391 $exclude[1][] = 'lang_default_value';
392 }
393
394 // option-specific fields require lang_options to be excluded
395 if ($field_type == FIELD_BOOL || $field_type == FIELD_DROPDOWN)
396 {
397 $exclude[1][] = 'lang_options';
398 }
399
400 $cp->vars['field_ident'] = ($action == 'create' && $step == 1) ? utf8_clean_string(request_var('field_ident', $field_row['field_ident'], true)) : request_var('field_ident', $field_row['field_ident']);
401 $cp->vars['lang_name'] = utf8_normalize_nfc(request_var('lang_name', $field_row['lang_name'], true));
402 $cp->vars['lang_explain'] = utf8_normalize_nfc(request_var('lang_explain', $field_row['lang_explain'], true));
403 $cp->vars['lang_default_value'] = utf8_normalize_nfc(request_var('lang_default_value', $field_row['lang_default_value'], true));
404
405 // Visibility Options...
406 $visibility_ary = array(
407 'field_required',
408 'field_show_on_reg',
409 'field_show_on_vt',
410 'field_show_profile',
411 'field_hide',
412 );
413
414 foreach ($visibility_ary as $val)
415 {
416 $cp->vars[$val] = ($submit || $save) ? request_var($val, 0) : $field_row[$val];
417 }
418
419 $cp->vars['field_no_view'] = request_var('field_no_view', (int) $field_row['field_no_view']);
420
421 // A boolean field expects an array as the lang options
422 if ($field_type == FIELD_BOOL)
423 {
424 $options = utf8_normalize_nfc(request_var('lang_options', array(''), true));
425 }
426 else
427 {
428 $options = utf8_normalize_nfc(request_var('lang_options', '', true));
429 }
430
431 // If the user has submitted a form with options (i.e. dropdown field)
432 if ($options)
433 {
434 $exploded_options = (is_array($options)) ? $options : explode("\n", $options);
435
436 if (sizeof($exploded_options) == sizeof($lang_options) || $action == 'create')
437 {
438 // The number of options in the field is equal to the number of options already in the database
439 // Or we are creating a new dropdown list.
440 $cp->vars['lang_options'] = $exploded_options;
441 }
442 else if ($action == 'edit')
443 {
444 // Changing the number of options? (We remove and re-create the option fields)
445 $cp->vars['lang_options'] = $exploded_options;
446 }
447 }
448 else
449 {
450 $cp->vars['lang_options'] = $lang_options;
451 }
452
453 // step 2
454 foreach ($exclude[2] as $key)
455 {
456 $var = utf8_normalize_nfc(request_var($key, $field_row[$key], true));
457
458 // Manipulate the intended variables a little bit if needed
459 if ($field_type == FIELD_DROPDOWN && $key == 'field_maxlen')
460 {
461 // Get the number of options if this key is 'field_maxlen'
462 $var = sizeof(explode("\n", utf8_normalize_nfc(request_var('lang_options', '', true))));
463 }
464 else if ($field_type == FIELD_TEXT && $key == 'field_length')
465 {
466 if (isset($_REQUEST['rows']))
467 {
468 $cp->vars['rows'] = request_var('rows', 0);
469 $cp->vars['columns'] = request_var('columns', 0);
470 $var = $cp->vars['rows'] . '|' . $cp->vars['columns'];
471 }
472 else
473 {
474 $row_col = explode('|', $var);
475 $cp->vars['rows'] = $row_col[0];
476 $cp->vars['columns'] = $row_col[1];
477 }
478 }
479 else if ($field_type == FIELD_DATE && $key == 'field_default_value')
480 {
481 $always_now = request_var('always_now', -1);
482
483 if ($always_now == 1 || ($always_now === -1 && $var == 'now'))
484 {
485 $now = getdate();
486
487 $cp->vars['field_default_value_day'] = $now['mday'];
488 $cp->vars['field_default_value_month'] = $now['mon'];
489 $cp->vars['field_default_value_year'] = $now['year'];
490 $var = $_POST['field_default_value'] = 'now';
491 }
492 else
493 {
494 if (isset($_REQUEST['field_default_value_day']))
495 {
496 $cp->vars['field_default_value_day'] = request_var('field_default_value_day', 0);
497 $cp->vars['field_default_value_month'] = request_var('field_default_value_month', 0);
498 $cp->vars['field_default_value_year'] = request_var('field_default_value_year', 0);
499 $var = $_POST['field_default_value'] = sprintf('%2d-%2d-%4d', $cp->vars['field_default_value_day'], $cp->vars['field_default_value_month'], $cp->vars['field_default_value_year']);
500 }
501 else
502 {
503 list($cp->vars['field_default_value_day'], $cp->vars['field_default_value_month'], $cp->vars['field_default_value_year']) = explode('-', $var);
504 }
505 }
506 }
507 /* else if ($field_type == FIELD_BOOL && $key == 'field_default_value')
508 {
509 // Get the number of options if this key is 'field_maxlen'
510 $var = request_var('field_default_value', 0);
511 }*/
512 else if ($field_type == FIELD_INT && $key == 'field_default_value')
513 {
514 // Permit an empty string
515 if (request_var('field_default_value', '') === '')
516 {
517 $var = '';
518 }
519 }
520
521 $cp->vars[$key] = $var;
522 }
523
524 // step 3 - all arrays
525 if ($action == 'edit')
526 {
527 // Get language entries
528 $sql = 'SELECT *
529 FROM ' . PROFILE_FIELDS_LANG_TABLE . '
530 WHERE lang_id <> ' . $this->edit_lang_id . "
531 AND field_id = $field_id
532 ORDER BY option_id ASC";
533 $result = $db->sql_query($sql);
534
535 $l_lang_options = array();
536 while ($row = $db->sql_fetchrow($result))
537 {
538 $l_lang_options[$row['lang_id']][$row['option_id']] = $row['lang_value'];
539 }
540 $db->sql_freeresult($result);
541
542
543 $sql = 'SELECT lang_id, lang_name, lang_explain, lang_default_value
544 FROM ' . PROFILE_LANG_TABLE . '
545 WHERE lang_id <> ' . $this->edit_lang_id . "
546 AND field_id = $field_id
547 ORDER BY lang_id ASC";
548 $result = $db->sql_query($sql);
549
550 $l_lang_name = $l_lang_explain = $l_lang_default_value = array();
551 while ($row = $db->sql_fetchrow($result))
552 {
553 $l_lang_name[$row['lang_id']] = $row['lang_name'];
554 $l_lang_explain[$row['lang_id']] = $row['lang_explain'];
555 $l_lang_default_value[$row['lang_id']] = $row['lang_default_value'];
556 }
557 $db->sql_freeresult($result);
558 }
559
560 foreach ($exclude[3] as $key)
561 {
562 $cp->vars[$key] = utf8_normalize_nfc(request_var($key, array(0 => ''), true));
563
564 if (!$cp->vars[$key] && $action == 'edit')
565 {
566 $cp->vars[$key] = $$key;
567 }
568 else if ($key == 'l_lang_options' && $field_type == FIELD_BOOL)
569 {
570 $cp->vars[$key] = utf8_normalize_nfc(request_var($key, array(0 => array('')), true));
571 }
572 else if ($key == 'l_lang_options' && is_array($cp->vars[$key]))
573 {
574 foreach ($cp->vars[$key] as $lang_id => $options)
575 {
576 $cp->vars[$key][$lang_id] = explode("\n", $options);
577 }
578
579 }
580 }
581
582 // Check for general issues in every step
583 if ($submit) // && $step == 1
584 {
585 // Check values for step 1
586 if ($cp->vars['field_ident'] == '')
587 {
588 $error[] = $user->lang['EMPTY_FIELD_IDENT'];
589 }
590
591 if (!preg_match('/^[a-z_]+$/', $cp->vars['field_ident']))
592 {
593 $error[] = $user->lang['INVALID_CHARS_FIELD_IDENT'];
594 }
595
596 if (strlen($cp->vars['field_ident']) > 17)
597 {
598 $error[] = $user->lang['INVALID_FIELD_IDENT_LEN'];
599 }
600
601 if ($cp->vars['lang_name'] == '')
602 {
603 $error[] = $user->lang['EMPTY_USER_FIELD_NAME'];
604 }
605
606 if ($field_type == FIELD_DROPDOWN && !sizeof($cp->vars['lang_options']))
607 {
608 $error[] = $user->lang['NO_FIELD_ENTRIES'];
609 }
610
611 if ($field_type == FIELD_BOOL && (empty($cp->vars['lang_options'][0]) || empty($cp->vars['lang_options'][1])))
612 {
613 $error[] = $user->lang['NO_FIELD_ENTRIES'];
614 }
615
616 // Check for already existing field ident
617 if ($action != 'edit')
618 {
619 $sql = 'SELECT field_ident
620 FROM ' . PROFILE_FIELDS_TABLE . "
621 WHERE field_ident = '" . $db->sql_escape($cp->vars['field_ident']) . "'";
622 $result = $db->sql_query($sql);
623 $row = $db->sql_fetchrow($result);
624 $db->sql_freeresult($result);
625
626 if ($row)
627 {
628 $error[] = $user->lang['FIELD_IDENT_ALREADY_EXIST'];
629 }
630 }
631 }
632
633 $step = (isset($_REQUEST['next'])) ? $step + 1 : ((isset($_REQUEST['prev'])) ? $step - 1 : $step);
634
635 if (sizeof($error))
636 {
637 $step--;
638 $submit = false;
639 }
640
641 // Build up the specific hidden fields
642 foreach ($exclude as $num => $key_ary)
643 {
644 if ($num == $step)
645 {
646 continue;
647 }
648
649 $_new_key_ary = array();
650
651 foreach ($key_ary as $key)
652 {
653 if ($field_type == FIELD_TEXT && $key == 'field_length' && isset($_REQUEST['rows']))
654 {
655 $cp->vars['rows'] = request_var('rows', 0);
656 $cp->vars['columns'] = request_var('columns', 0);
657 $_new_key_ary[$key] = $cp->vars['rows'] . '|' . $cp->vars['columns'];
658 }
659 else if ($field_type == FIELD_DATE && $key == 'field_default_value')
660 {
661 $always_now = request_var('always_now', 0);
662
663 if ($always_now)
664 {
665 $_new_key_ary[$key] = 'now';
666 }
667 else if (isset($_REQUEST['field_default_value_day']))
668 {
669 $cp->vars['field_default_value_day'] = request_var('field_default_value_day', 0);
670 $cp->vars['field_default_value_month'] = request_var('field_default_value_month', 0);
671 $cp->vars['field_default_value_year'] = request_var('field_default_value_year', 0);
672 $_new_key_ary[$key] = sprintf('%2d-%2d-%4d', $cp->vars['field_default_value_day'], $cp->vars['field_default_value_month'], $cp->vars['field_default_value_year']);
673 }
674 }
675 else if ($field_type == FIELD_BOOL && $key == 'l_lang_options' && isset($_REQUEST['l_lang_options']))
676 {
677 $_new_key_ary[$key] = utf8_normalize_nfc(request_var($key, array(array('')), true));
678 }
679 else
680 {
681 if (!isset($_REQUEST[$key]))
682 {
683 $var = false;
684 }
685 else if ($key == 'field_ident' && isset($cp->vars[$key]))
686 {
687 $_new_key_ary[$key]= $cp->vars[$key];
688 }
689 else
690 {
691 $_new_key_ary[$key] = (is_array($_REQUEST[$key])) ? utf8_normalize_nfc(request_var($key, array(''), true)) : utf8_normalize_nfc(request_var($key, '', true));
692 }
693 }
694 }
695
696 $s_hidden_fields .= build_hidden_fields($_new_key_ary);
697 }
698
699 if (!sizeof($error))
700 {
701 if ($step == 3 && (sizeof($this->lang_defs['iso']) == 1 || $save))
702 {
703 $this->save_profile_field($cp, $field_type, $action);
704 }
705 else if ($action == 'edit' && $save)
706 {
707 $this->save_profile_field($cp, $field_type, $action);
708 }
709 }
710
711 $template->assign_vars(array(
712 'S_EDIT' => true,
713 'S_EDIT_MODE' => ($action == 'edit') ? true : false,
714 'ERROR_MSG' => (sizeof($error)) ? implode('<br />', $error) : '',
715
716 'L_TITLE' => $user->lang['STEP_' . $step . '_TITLE_' . strtoupper($action)],
717 'L_EXPLAIN' => $user->lang['STEP_' . $step . '_EXPLAIN_' . strtoupper($action)],
718
719 'U_ACTION' => $this->u_action . "&amp;action=$action&amp;step=$step",
720 'U_BACK' => $this->u_action)
721 );
722
723 // Now go through the steps
724 switch ($step)
725 {
726 // Create basic options - only small differences between field types
727 case 1:
728
729 // Build common create options
730 $template->assign_vars(array(
731 'S_STEP_ONE' => true,
732 'S_FIELD_REQUIRED' => ($cp->vars['field_required']) ? true : false,
733 'S_SHOW_ON_REG' => ($cp->vars['field_show_on_reg']) ? true : false,
734 'S_SHOW_ON_VT' => ($cp->vars['field_show_on_vt']) ? true : false,
735 'S_FIELD_HIDE' => ($cp->vars['field_hide']) ? true : false,
736 'S_SHOW_PROFILE' => ($cp->vars['field_show_profile']) ? true : false,
737 'S_FIELD_NO_VIEW' => ($cp->vars['field_no_view']) ? true : false,
738
739 'L_LANG_SPECIFIC' => sprintf($user->lang['LANG_SPECIFIC_OPTIONS'], $config['default_lang']),
740 'FIELD_TYPE' => $user->lang['FIELD_' . strtoupper($cp->profile_types[$field_type])],
741 'FIELD_IDENT' => $cp->vars['field_ident'],
742 'LANG_NAME' => $cp->vars['lang_name'],
743 'LANG_EXPLAIN' => $cp->vars['lang_explain'])
744 );
745
746 // String and Text needs to set default values here...
747 if ($field_type == FIELD_STRING || $field_type == FIELD_TEXT)
748 {
749 $template->assign_vars(array(
750 'S_TEXT' => ($field_type == FIELD_TEXT) ? true : false,
751 'S_STRING' => ($field_type == FIELD_STRING) ? true : false,
752
753 'L_DEFAULT_VALUE_EXPLAIN' => $user->lang[strtoupper($cp->profile_types[$field_type]) . '_DEFAULT_VALUE_EXPLAIN'],
754 'LANG_DEFAULT_VALUE' => $cp->vars['lang_default_value'])
755 );
756 }
757
758 if ($field_type == FIELD_BOOL || $field_type == FIELD_DROPDOWN)
759 {
760 // Initialize these array elements if we are creating a new field
761 if (!sizeof($cp->vars['lang_options']))
762 {
763 if ($field_type == FIELD_BOOL)
764 {
765 // No options have been defined for a boolean field.
766 $cp->vars['lang_options'][0] = '';
767 $cp->vars['lang_options'][1] = '';
768 }
769 else
770 {
771 // No options have been defined for the dropdown menu
772 $cp->vars['lang_options'] = array();
773 }
774 }
775
776 $template->assign_vars(array(
777 'S_BOOL' => ($field_type == FIELD_BOOL) ? true : false,
778 'S_DROPDOWN' => ($field_type == FIELD_DROPDOWN) ? true : false,
779
780 'L_LANG_OPTIONS_EXPLAIN' => $user->lang[strtoupper($cp->profile_types[$field_type]) . '_ENTRIES_EXPLAIN'],
781 'LANG_OPTIONS' => ($field_type == FIELD_DROPDOWN) ? implode("\n", $cp->vars['lang_options']) : '',
782 'FIRST_LANG_OPTION' => ($field_type == FIELD_BOOL) ? $cp->vars['lang_options'][0] : '',
783 'SECOND_LANG_OPTION' => ($field_type == FIELD_BOOL) ? $cp->vars['lang_options'][1] : '')
784 );
785 }
786
787 break;
788
789 case 2:
790
791 $template->assign_vars(array(
792 'S_STEP_TWO' => true,
793 'L_NEXT_STEP' => (sizeof($this->lang_defs['iso']) == 1) ? $user->lang['SAVE'] : $user->lang['PROFILE_LANG_OPTIONS'])
794 );
795
796 // Build options based on profile type
797 $function = 'get_' . $cp->profile_types[$field_type] . '_options';
798 $options = $cp->$function();
799
800 foreach ($options as $num => $option_ary)
801 {
802 $template->assign_block_vars('option', $option_ary);
803 }
804
805 break;
806
807 // Define remaining language variables
808 case 3:
809
810 $template->assign_var('S_STEP_THREE', true);
811 $options = $this->build_language_options($cp, $field_type, $action);
812
813 foreach ($options as $lang_id => $lang_ary)
814 {
815 $template->assign_block_vars('options', array(
816 'LANGUAGE' => sprintf($user->lang[(($lang_id == $this->edit_lang_id) ? 'DEFAULT_' : '') . 'ISO_LANGUAGE'], $lang_ary['lang_iso']))
817 );
818
819 foreach ($lang_ary['fields'] as $field_ident => $field_ary)
820 {
821 $template->assign_block_vars('options.field', array(
822 'L_TITLE' => $field_ary['TITLE'],
823 'L_EXPLAIN' => (isset($field_ary['EXPLAIN'])) ? $field_ary['EXPLAIN'] : '',
824 'FIELD' => $field_ary['FIELD'])
825 );
826 }
827 }
828
829 break;
830 }
831
832 $template->assign_vars(array(
833 'S_HIDDEN_FIELDS' => $s_hidden_fields)
834 );
835
836 return;
837
838 break;
839 }
840
841 $sql = 'SELECT *
842 FROM ' . PROFILE_FIELDS_TABLE . '
843 ORDER BY field_order';
844 $result = $db->sql_query($sql);
845
846 $s_one_need_edit = false;
847 while ($row = $db->sql_fetchrow($result))
848 {
849 $active_lang = (!$row['field_active']) ? 'ACTIVATE' : 'DEACTIVATE';
850 $active_value = (!$row['field_active']) ? 'activate' : 'deactivate';
851 $id = $row['field_id'];
852
853 $s_need_edit = (sizeof($this->lang_defs['diff'][$row['field_id']])) ? true : false;
854
855 if ($s_need_edit)
856 {
857 $s_one_need_edit = true;
858 }
859
860 $template->assign_block_vars('fields', array(
861 'FIELD_IDENT' => $row['field_ident'],
862 'FIELD_TYPE' => $user->lang['FIELD_' . strtoupper($cp->profile_types[$row['field_type']])],
863
864 'L_ACTIVATE_DEACTIVATE' => $user->lang[$active_lang],
865 'U_ACTIVATE_DEACTIVATE' => $this->u_action . "&amp;action=$active_value&amp;field_id=$id",
866 'U_EDIT' => $this->u_action . "&amp;action=edit&amp;field_id=$id",
867 'U_TRANSLATE' => $this->u_action . "&amp;action=edit&amp;field_id=$id&amp;step=3",
868 'U_DELETE' => $this->u_action . "&amp;action=delete&amp;field_id=$id",
869 'U_MOVE_UP' => $this->u_action . "&amp;action=move_up&amp;order={$row['field_order']}",
870 'U_MOVE_DOWN' => $this->u_action . "&amp;action=move_down&amp;order={$row['field_order']}",
871
872 'S_NEED_EDIT' => $s_need_edit)
873 );
874 }
875 $db->sql_freeresult($result);
876
877 // At least one option field needs editing?
878 if ($s_one_need_edit)
879 {
880 $template->assign_var('S_NEED_EDIT', true);
881 }
882
883 $s_select_type = '';
884 foreach ($cp->profile_types as $key => $value)
885 {
886 $s_select_type .= '<option value="' . $key . '">' . $user->lang['FIELD_' . strtoupper($value)] . '</option>';
887 }
888
889 $template->assign_vars(array(
890 'U_ACTION' => $this->u_action,
891 'S_TYPE_OPTIONS' => $s_select_type)
892 );
893 }
894
895 /**
896 * Build all Language specific options
897 */
898 function build_language_options(&$cp, $field_type, $action = 'create')
899 {
900 global $user, $config, $db;
901
902 $default_lang_id = (!empty($this->edit_lang_id)) ? $this->edit_lang_id : $this->lang_defs['iso'][$config['default_lang']];
903
904 $sql = 'SELECT lang_id, lang_iso
905 FROM ' . LANG_TABLE . '
906 WHERE lang_id <> ' . (int) $default_lang_id . '
907 ORDER BY lang_english_name';
908 $result = $db->sql_query($sql);
909
910 $languages = array();
911 while ($row = $db->sql_fetchrow($result))
912 {
913 $languages[$row['lang_id']] = $row['lang_iso'];
914 }
915 $db->sql_freeresult($result);
916
917 $options = array();
918 $options['lang_name'] = 'string';
919 if ($cp->vars['lang_explain'])
920 {
921 $options['lang_explain'] = 'text';
922 }
923
924 switch ($field_type)
925 {
926 case FIELD_BOOL:
927 $options['lang_options'] = 'two_options';
928 break;
929
930 case FIELD_DROPDOWN:
931 $options['lang_options'] = 'optionfield';
932 break;
933
934 case FIELD_TEXT:
935 case FIELD_STRING:
936 if (strlen($cp->vars['lang_default_value']))
937 {
938 $options['lang_default_value'] = ($field_type == FIELD_STRING) ? 'string' : 'text';
939 }
940 break;
941 }
942
943 $lang_options = array();
944
945 foreach ($options as $field => $field_type)
946 {
947 $lang_options[1]['lang_iso'] = $this->lang_defs['id'][$default_lang_id];
948 $lang_options[1]['fields'][$field] = array(
949 'TITLE' => $user->lang['CP_' . strtoupper($field)],
950 'FIELD' => '<dd>' . ((is_array($cp->vars[$field])) ? implode('<br />', $cp->vars[$field]) : bbcode_nl2br($cp->vars[$field])) . '</dd>'
951 );
952
953 if (isset($user->lang['CP_' . strtoupper($field) . '_EXPLAIN']))
954 {
955 $lang_options[1]['fields'][$field]['EXPLAIN'] = $user->lang['CP_' . strtoupper($field) . '_EXPLAIN'];
956 }
957 }
958
959 foreach ($languages as $lang_id => $lang_iso)
960 {
961 $lang_options[$lang_id]['lang_iso'] = $lang_iso;
962 foreach ($options as $field => $field_type)
963 {
964 $value = ($action == 'create') ? utf8_normalize_nfc(request_var('l_' . $field, array(0 => ''), true)) : $cp->vars['l_' . $field];
965 if ($field == 'lang_options')
966 {
967 $var = (!isset($cp->vars['l_lang_options'][$lang_id]) || !is_array($cp->vars['l_lang_options'][$lang_id])) ? $cp->vars['lang_options'] : $cp->vars['l_lang_options'][$lang_id];
968
969 switch ($field_type)
970 {
971 case 'two_options':
972
973 $lang_options[$lang_id]['fields'][$field] = array(
974 'TITLE' => $user->lang['CP_' . strtoupper($field)],
975 'FIELD' => '
976 <dd><input class="medium" name="l_' . $field . '[' . $lang_id . '][]" value="' . ((isset($value[$lang_id][0])) ? $value[$lang_id][0] : $var[0]) . '" /> ' . $user->lang['FIRST_OPTION'] . '</dd>
977 <dd><input class="medium" name="l_' . $field . '[' . $lang_id . '][]" value="' . ((isset($value[$lang_id][1])) ? $value[$lang_id][1] : $var[1]) . '" /> ' . $user->lang['SECOND_OPTION'] . '</dd>'
978 );
979 break;
980
981 case 'optionfield':
982 $value = ((isset($value[$lang_id])) ? ((is_array($value[$lang_id])) ? implode("\n", $value[$lang_id]) : $value[$lang_id]) : implode("\n", $var));
983 $lang_options[$lang_id]['fields'][$field] = array(
984 'TITLE' => $user->lang['CP_' . strtoupper($field)],
985 'FIELD' => '<dd><textarea name="l_' . $field . '[' . $lang_id . ']" rows="7" cols="80">' . $value . '</textarea></dd>'
986 );
987 break;
988 }
989
990 if (isset($user->lang['CP_' . strtoupper($field) . '_EXPLAIN']))
991 {
992 $lang_options[$lang_id]['fields'][$field]['EXPLAIN'] = $user->lang['CP_' . strtoupper($field) . '_EXPLAIN'];
993 }
994 }
995 else
996 {
997 $var = ($action == 'create' || !is_array($cp->vars[$field])) ? $cp->vars[$field] : $cp->vars[$field][$lang_id];
998
999 $lang_options[$lang_id]['fields'][$field] = array(
1000 'TITLE' => $user->lang['CP_' . strtoupper($field)],
1001 'FIELD' => ($field_type == 'string') ? '<dd><input class="medium" type="text" name="l_' . $field . '[' . $lang_id . ']" value="' . ((isset($value[$lang_id])) ? $value[$lang_id] : $var) . '" /></dd>' : '<dd><textarea name="l_' . $field . '[' . $lang_id . ']" rows="3" cols="80">' . ((isset($value[$lang_id])) ? $value[$lang_id] : $var) . '</textarea></dd>'
1002 );
1003
1004 if (isset($user->lang['CP_' . strtoupper($field) . '_EXPLAIN']))
1005 {
1006 $lang_options[$lang_id]['fields'][$field]['EXPLAIN'] = $user->lang['CP_' . strtoupper($field) . '_EXPLAIN'];
1007 }
1008 }
1009 }
1010 }
1011
1012 return $lang_options;
1013 }
1014
1015 /**
1016 * Save Profile Field
1017 */
1018 function save_profile_field(&$cp, $field_type, $action = 'create')
1019 {
1020 global $db, $config, $user;
1021
1022 $field_id = request_var('field_id', 0);
1023
1024 // Collect all information, if something is going wrong, abort the operation
1025 $profile_sql = $profile_lang = $empty_lang = $profile_lang_fields = array();
1026
1027 $default_lang_id = (!empty($this->edit_lang_id)) ? $this->edit_lang_id : $this->lang_defs['iso'][$config['default_lang']];
1028
1029 if ($action == 'create')
1030 {
1031 $sql = 'SELECT MAX(field_order) as max_field_order
1032 FROM ' . PROFILE_FIELDS_TABLE;
1033 $result = $db->sql_query($sql);
1034 $new_field_order = (int) $db->sql_fetchfield('max_field_order');
1035 $db->sql_freeresult($result);
1036
1037 $field_ident = $cp->vars['field_ident'];
1038 }
1039
1040 // Save the field
1041 $profile_fields = array(
1042 'field_length' => $cp->vars['field_length'],
1043 'field_minlen' => $cp->vars['field_minlen'],
1044 'field_maxlen' => $cp->vars['field_maxlen'],
1045 'field_novalue' => $cp->vars['field_novalue'],
1046 'field_default_value' => $cp->vars['field_default_value'],
1047 'field_validation' => $cp->vars['field_validation'],
1048 'field_required' => $cp->vars['field_required'],
1049 'field_show_on_reg' => $cp->vars['field_show_on_reg'],
1050 'field_show_on_vt' => $cp->vars['field_show_on_vt'],
1051 'field_hide' => $cp->vars['field_hide'],
1052 'field_show_profile' => $cp->vars['field_show_profile'],
1053 'field_no_view' => $cp->vars['field_no_view']
1054 );
1055
1056 if ($action == 'create')
1057 {
1058 $profile_fields += array(
1059 'field_type' => $field_type,
1060 'field_ident' => $field_ident,
1061 'field_name' => $field_ident,
1062 'field_order' => $new_field_order + 1,
1063 'field_active' => 1
1064 );
1065
1066 $sql = 'INSERT INTO ' . PROFILE_FIELDS_TABLE . ' ' . $db->sql_build_array('INSERT', $profile_fields);
1067 $db->sql_query($sql);
1068
1069 $field_id = $db->sql_nextid();
1070 }
1071 else
1072 {
1073 $sql = 'UPDATE ' . PROFILE_FIELDS_TABLE . '
1074 SET ' . $db->sql_build_array('UPDATE', $profile_fields) . "
1075 WHERE field_id = $field_id";
1076 $db->sql_query($sql);
1077 }
1078
1079 if ($action == 'create')
1080 {
1081 $field_ident = 'pf_' . $field_ident;
1082 $profile_sql[] = $this->add_field_ident($field_ident, $field_type);
1083 }
1084
1085 $sql_ary = array(
1086 'lang_name' => $cp->vars['lang_name'],
1087 'lang_explain' => $cp->vars['lang_explain'],
1088 'lang_default_value' => $cp->vars['lang_default_value']
1089 );
1090
1091 if ($action == 'create')
1092 {
1093 $sql_ary['field_id'] = $field_id;
1094 $sql_ary['lang_id'] = $default_lang_id;
1095
1096 $profile_sql[] = 'INSERT INTO ' . PROFILE_LANG_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
1097 }
1098 else
1099 {
1100 $this->update_insert(PROFILE_LANG_TABLE, $sql_ary, array('field_id' => $field_id, 'lang_id' => $default_lang_id));
1101 }
1102
1103 if (is_array($cp->vars['l_lang_name']) && sizeof($cp->vars['l_lang_name']))
1104 {
1105 foreach ($cp->vars['l_lang_name'] as $lang_id => $data)
1106 {
1107 if (($cp->vars['lang_name'] != '' && $cp->vars['l_lang_name'][$lang_id] == '')
1108 || ($cp->vars['lang_explain'] != '' && $cp->vars['l_lang_explain'][$lang_id] == '')
1109 || ($cp->vars['lang_default_value'] != '' && $cp->vars['l_lang_default_value'][$lang_id] == ''))
1110 {
1111 $empty_lang[$lang_id] = true;
1112 break;
1113 }
1114
1115 if (!isset($empty_lang[$lang_id]))
1116 {
1117 $profile_lang[] = array(
1118 'field_id' => $field_id,
1119 'lang_id' => $lang_id,
1120 'lang_name' => $cp->vars['l_lang_name'][$lang_id],
1121 'lang_explain' => (isset($cp->vars['l_lang_explain'][$lang_id])) ? $cp->vars['l_lang_explain'][$lang_id] : '',
1122 'lang_default_value' => (isset($cp->vars['l_lang_default_value'][$lang_id])) ? $cp->vars['l_lang_default_value'][$lang_id] : ''
1123 );
1124 }
1125 }
1126
1127 foreach ($empty_lang as $lang_id => $NULL)
1128 {
1129 $sql = 'DELETE FROM ' . PROFILE_LANG_TABLE . "
1130 WHERE field_id = $field_id
1131 AND lang_id = " . (int) $lang_id;
1132 $db->sql_query($sql);
1133 }
1134 }
1135
1136 // These are always arrays because the key is the language id...
1137 $cp->vars['l_lang_name'] = utf8_normalize_nfc(request_var('l_lang_name', array(0 => ''), true));
1138 $cp->vars['l_lang_explain'] = utf8_normalize_nfc(request_var('l_lang_explain', array(0 => ''), true));
1139 $cp->vars['l_lang_default_value'] = utf8_normalize_nfc(request_var('l_lang_default_value', array(0 => ''), true));
1140
1141 if ($field_type != FIELD_BOOL)
1142 {
1143 $cp->vars['l_lang_options'] = utf8_normalize_nfc(request_var('l_lang_options', array(0 => ''), true));
1144 }
1145 else
1146 {
1147 /**
1148 * @todo check if this line is correct...
1149 $cp->vars['l_lang_default_value'] = request_var('l_lang_default_value', array(0 => array('')), true);
1150 */
1151 $cp->vars['l_lang_options'] = utf8_normalize_nfc(request_var('l_lang_options', array(0 => array('')), true));
1152 }
1153
1154 if ($cp->vars['lang_options'])
1155 {
1156 if (!is_array($cp->vars['lang_options']))
1157 {
1158 $cp->vars['lang_options'] = explode("\n", $cp->vars['lang_options']);
1159 }
1160
1161 if ($action != 'create')
1162 {
1163 $sql = 'DELETE FROM ' . PROFILE_FIELDS_LANG_TABLE . "
1164 WHERE field_id = $field_id
1165 AND lang_id = " . (int) $default_lang_id;
1166 $db->sql_query($sql);
1167 }
1168
1169 foreach ($cp->vars['lang_options'] as $option_id => $value)
1170 {
1171 $sql_ary = array(
1172 'field_type' => (int) $field_type,
1173 'lang_value' => $value
1174 );
1175
1176 if ($action == 'create')
1177 {
1178 $sql_ary['field_id'] = $field_id;
1179 $sql_ary['lang_id'] = $default_lang_id;
1180 $sql_ary['option_id'] = (int) $option_id;
1181
1182 $profile_sql[] = 'INSERT INTO ' . PROFILE_FIELDS_LANG_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
1183 }
1184 else
1185 {
1186 $this->update_insert(PROFILE_FIELDS_LANG_TABLE, $sql_ary, array(
1187 'field_id' => $field_id,
1188 'lang_id' => (int) $default_lang_id,
1189 'option_id' => (int) $option_id)
1190 );
1191 }
1192 }
1193 }
1194
1195 if (is_array($cp->vars['l_lang_options']) && sizeof($cp->vars['l_lang_options']))
1196 {
1197 $empty_lang = array();
1198
1199 foreach ($cp->vars['l_lang_options'] as $lang_id => $lang_ary)
1200 {
1201 if (!is_array($lang_ary))
1202 {
1203 $lang_ary = explode("\n", $lang_ary);
1204 }
1205
1206 if (sizeof($lang_ary) != sizeof($cp->vars['lang_options']))
1207 {
1208 $empty_lang[$lang_id] = true;
1209 }
1210
1211 if (!isset($empty_lang[$lang_id]))
1212 {
1213 if ($action != 'create')
1214 {
1215 $sql = 'DELETE FROM ' . PROFILE_FIELDS_LANG_TABLE . "
1216 WHERE field_id = $field_id
1217 AND lang_id = " . (int) $lang_id;
1218 $db->sql_query($sql);
1219 }
1220
1221 foreach ($lang_ary as $option_id => $value)
1222 {
1223 $profile_lang_fields[] = array(
1224 'field_id' => (int) $field_id,
1225 'lang_id' => (int) $lang_id,
1226 'option_id' => (int) $option_id,
1227 'field_type' => (int) $field_type,
1228 'lang_value' => $value
1229 );
1230 }
1231 }
1232 }
1233
1234 foreach ($empty_lang as $lang_id => $NULL)
1235 {
1236 $sql = 'DELETE FROM ' . PROFILE_FIELDS_LANG_TABLE . "
1237 WHERE field_id = $field_id
1238 AND lang_id = " . (int) $lang_id;
1239 $db->sql_query($sql);
1240 }
1241 }
1242
1243 foreach ($profile_lang as $sql)
1244 {
1245 if ($action == 'create')
1246 {
1247 $profile_sql[] = 'INSERT INTO ' . PROFILE_LANG_TABLE . ' ' . $db->sql_build_array('INSERT', $sql);
1248 }
1249 else
1250 {
1251 $lang_id = $sql['lang_id'];
1252 unset($sql['lang_id'], $sql['field_id']);
1253
1254 $this->update_insert(PROFILE_LANG_TABLE, $sql, array('lang_id' => (int) $lang_id, 'field_id' => $field_id));
1255 }
1256 }
1257
1258 if (sizeof($profile_lang_fields))
1259 {
1260 foreach ($profile_lang_fields as $sql)
1261 {
1262 if ($action == 'create')
1263 {
1264 $profile_sql[] = 'INSERT INTO ' . PROFILE_FIELDS_LANG_TABLE . ' ' . $db->sql_build_array('INSERT', $sql);
1265 }
1266 else
1267 {
1268 $lang_id = $sql['lang_id'];
1269 $option_id = $sql['option_id'];
1270 unset($sql['lang_id'], $sql['field_id'], $sql['option_id']);
1271
1272 $this->update_insert(PROFILE_FIELDS_LANG_TABLE, $sql, array(
1273 'lang_id' => $lang_id,
1274 'field_id' => $field_id,
1275 'option_id' => $option_id)
1276 );
1277 }
1278 }
1279 }
1280
1281
1282 $db->sql_transaction('begin');
1283
1284 if ($action == 'create')
1285 {
1286 foreach ($profile_sql as $sql)
1287 {
1288 $db->sql_query($sql);
1289 }
1290 }
1291
1292 $db->sql_transaction('commit');
1293
1294 if ($action == 'edit')
1295 {
1296 add_log('admin', 'LOG_PROFILE_FIELD_EDIT', $cp->vars['field_ident'] . ':' . $cp->vars['lang_name']);
1297 trigger_error($user->lang['CHANGED_PROFILE_FIELD'] . adm_back_link($this->u_action));
1298 }
1299 else
1300 {
1301 add_log('admin', 'LOG_PROFILE_FIELD_CREATE', substr($field_ident, 3) . ':' . $cp->vars['lang_name']);
1302 trigger_error($user->lang['ADDED_PROFILE_FIELD'] . adm_back_link($this->u_action));
1303 }
1304 }
1305
1306 /**
1307 * Update, then insert if not successfull
1308 */
1309 function update_insert($table, $sql_ary, $where_fields)
1310 {
1311 global $db;
1312
1313 $where_sql = array();
1314 $check_key = '';
1315
1316 foreach ($where_fields as $key => $value)
1317 {
1318 $check_key = (!$check_key) ? $key : $check_key;
1319 $where_sql[] = $key . ' = ' . ((is_string($value)) ? "'" . $db->sql_escape($value) . "'" : (int) $value);
1320 }
1321
1322 if (!sizeof($where_sql))
1323 {
1324 return;
1325 }
1326
1327 $sql = "SELECT $check_key
1328 FROM $table
1329 WHERE " . implode(' AND ', $where_sql);
1330 $result = $db->sql_query($sql);
1331 $row = $db->sql_fetchrow($result);
1332 $db->sql_freeresult($result);
1333
1334 if (!$row)
1335 {
1336 $sql_ary = array_merge($where_fields, $sql_ary);
1337
1338 if (sizeof($sql_ary))
1339 {
1340 $db->sql_query("INSERT INTO $table " . $db->sql_build_array('INSERT', $sql_ary));
1341 }
1342 }
1343 else
1344 {
1345 if (sizeof($sql_ary))
1346 {
1347 $sql = "UPDATE $table SET " . $db->sql_build_array('UPDATE', $sql_ary) . '
1348 WHERE ' . implode(' AND ', $where_sql);
1349 $db->sql_query($sql);
1350 }
1351 }
1352 }
1353
1354 /**
1355 * Return sql statement for adding a new field ident (profile field) to the profile fields data table
1356 */
1357 function add_field_ident($field_ident, $field_type)
1358 {
1359 global $db;
1360
1361 switch ($db->sql_layer)
1362 {
1363 case 'mysql':
1364 case 'mysql4':
1365 case 'mysqli':
1366
1367 // We are defining the biggest common value, because of the possibility to edit the min/max values of each field.
1368 $sql = 'ALTER TABLE ' . PROFILE_FIELDS_DATA_TABLE . " ADD `$field_ident` ";
1369
1370 switch ($field_type)
1371 {
1372 case FIELD_STRING:
1373 $sql .= ' VARCHAR(255) ';
1374 break;
1375
1376 case FIELD_DATE:
1377 $sql .= 'VARCHAR(10) ';
1378 break;
1379
1380 case FIELD_TEXT:
1381 $sql .= "TEXT";
1382 // ADD {$field_ident}_bbcode_uid VARCHAR(5) NOT NULL,
1383 // ADD {$field_ident}_bbcode_bitfield INT(11) UNSIGNED";
1384 break;
1385
1386 case FIELD_BOOL:
1387 $sql .= 'TINYINT(2) ';
1388 break;
1389
1390 case FIELD_DROPDOWN:
1391 $sql .= 'MEDIUMINT(8) ';
1392 break;
1393
1394 case FIELD_INT:
1395 $sql .= 'BIGINT(20) ';
1396 break;
1397 }
1398
1399 break;
1400
1401 case 'sqlite':
1402
1403 switch ($field_type)
1404 {
1405 case FIELD_STRING:
1406 $type = ' VARCHAR(255) ';
1407 break;
1408
1409 case FIELD_DATE:
1410 $type = 'VARCHAR(10) ';
1411 break;
1412
1413 case FIELD_TEXT:
1414 $type = "TEXT(65535)";
1415 // ADD {$field_ident}_bbcode_uid VARCHAR(5) NOT NULL,
1416 // ADD {$field_ident}_bbcode_bitfield INT(11) UNSIGNED";
1417 break;
1418
1419 case FIELD_BOOL:
1420 $type = 'TINYINT(2) ';
1421 break;
1422
1423 case FIELD_DROPDOWN:
1424 $type = 'MEDIUMINT(8) ';
1425 break;
1426
1427 case FIELD_INT:
1428 $type = 'BIGINT(20) ';
1429 break;
1430 }
1431
1432 // We are defining the biggest common value, because of the possibility to edit the min/max values of each field.
1433 if (version_compare(sqlite_libversion(), '3.0') == -1)
1434 {
1435 $sql = "SELECT sql
1436 FROM sqlite_master
1437 WHERE type = 'table'
1438 AND name = '" . PROFILE_FIELDS_DATA_TABLE . "'
1439 ORDER BY type DESC, name;";
1440 $result = $db->sql_query($sql);
1441 $row = $db->sql_fetchrow($result);
1442 $db->sql_freeresult($result);
1443
1444 // Create a temp table and populate it, destroy the existing one
1445 $db->sql_query(preg_replace('#CREATE\s+TABLE\s+"?' . PROFILE_FIELDS_DATA_TABLE . '"?#i', 'CREATE TEMPORARY TABLE ' . PROFILE_FIELDS_DATA_TABLE . '_temp', $row['sql']));
1446 $db->sql_query('INSERT INTO ' . PROFILE_FIELDS_DATA_TABLE . '_temp SELECT * FROM ' . PROFILE_FIELDS_DATA_TABLE);
1447 $db->sql_query('DROP TABLE ' . PROFILE_FIELDS_DATA_TABLE);
1448
1449 preg_match('#\((.*)\)#s', $row['sql'], $matches);
1450
1451 $new_table_cols = trim($matches[1]);
1452 $old_table_cols = explode(',', $new_table_cols);
1453 $column_list = array();
1454
1455 foreach ($old_table_cols as $declaration)
1456 {
1457 $entities = preg_split('#\s+#', trim($declaration));
1458 if ($entities[0] == 'PRIMARY')
1459 {
1460 continue;
1461 }
1462 $column_list[] = $entities[0];
1463 }
1464
1465 $columns = implode(',', $column_list);
1466
1467 $new_table_cols = $field_ident . ' ' . $type . ',' . $new_table_cols;
1468
1469 // create a new table and fill it up. destroy the temp one
1470 $db->sql_query('CREATE TABLE ' . PROFILE_FIELDS_DATA_TABLE . ' (' . $new_table_cols . ');');
1471 $db->sql_query('INSERT INTO ' . PROFILE_FIELDS_DATA_TABLE . ' (' . $columns . ') SELECT ' . $columns . ' FROM ' . PROFILE_FIELDS_DATA_TABLE . '_temp;');
1472 $db->sql_query('DROP TABLE ' . PROFILE_FIELDS_DATA_TABLE . '_temp');
1473 }
1474 else
1475 {
1476 $sql = 'ALTER TABLE ' . PROFILE_FIELDS_DATA_TABLE . " ADD $field_ident [$type]";
1477 }
1478
1479 break;
1480
1481 case 'mssql':
1482 case 'mssql_odbc':
1483
1484 // We are defining the biggest common value, because of the possibility to edit the min/max values of each field.
1485 $sql = 'ALTER TABLE [' . PROFILE_FIELDS_DATA_TABLE . "] ADD [$field_ident] ";
1486
1487 switch ($field_type)
1488 {
1489 case FIELD_STRING:
1490 $sql .= ' [VARCHAR] (255) ';
1491 break;
1492
1493 case FIELD_DATE:
1494 $sql .= '[VARCHAR] (10) ';
1495 break;
1496
1497 case FIELD_TEXT:
1498 $sql .= "[TEXT]";
1499 // ADD {$field_ident}_bbcode_uid [VARCHAR] (5) NOT NULL,
1500 // ADD {$field_ident}_bbcode_bitfield [INT] UNSIGNED";
1501 break;
1502
1503 case FIELD_BOOL:
1504 case FIELD_DROPDOWN:
1505 $sql .= '[INT] ';
1506 break;
1507
1508 case FIELD_INT:
1509 $sql .= '[FLOAT] ';
1510 break;
1511 }
1512
1513 break;
1514
1515 case 'postgres':
1516
1517 // We are defining the biggest common value, because of the possibility to edit the min/max values of each field.
1518 $sql = 'ALTER TABLE ' . PROFILE_FIELDS_DATA_TABLE . " ADD COLUMN \"$field_ident\" ";
1519
1520 switch ($field_type)
1521 {
1522 case FIELD_STRING:
1523 $sql .= ' VARCHAR(255) ';
1524 break;
1525
1526 case FIELD_DATE:
1527 $sql .= 'VARCHAR(10) ';
1528 break;
1529
1530 case FIELD_TEXT:
1531 $sql .= "TEXT";
1532 // ADD {$field_ident}_bbcode_uid VARCHAR(5) NOT NULL,
1533 // ADD {$field_ident}_bbcode_bitfield INT4 UNSIGNED";
1534 break;
1535
1536 case FIELD_BOOL:
1537 $sql .= 'INT2 ';
1538 break;
1539
1540 case FIELD_DROPDOWN:
1541 $sql .= 'INT4 ';
1542 break;
1543
1544 case FIELD_INT:
1545 $sql .= 'INT8 ';
1546 break;
1547 }
1548
1549 break;
1550
1551 case 'firebird':
1552
1553 // We are defining the biggest common value, because of the possibility to edit the min/max values of each field.
1554 $sql = 'ALTER TABLE ' . PROFILE_FIELDS_DATA_TABLE . ' ADD "' . strtoupper($field_ident) . '" ';
1555
1556 switch ($field_type)
1557 {
1558 case FIELD_STRING:
1559 $sql .= ' VARCHAR(255) ';
1560 break;
1561
1562 case FIELD_DATE:
1563 $sql .= 'VARCHAR(10) ';
1564 break;
1565
1566 case FIELD_TEXT:
1567 $sql .= "BLOB SUB_TYPE TEXT";
1568 // ADD {$field_ident}_bbcode_uid VARCHAR(5) NOT NULL,
1569 // ADD {$field_ident}_bbcode_bitfield INTEGER UNSIGNED";
1570 break;
1571
1572 case FIELD_BOOL:
1573 case FIELD_DROPDOWN:
1574 $sql .= 'INTEGER ';
1575 break;
1576
1577 case FIELD_INT:
1578 $sql .= 'DOUBLE PRECISION ';
1579 break;
1580 }
1581
1582 break;
1583
1584 case 'oracle':
1585
1586 // We are defining the biggest common value, because of the possibility to edit the min/max values of each field.
1587 $sql = 'ALTER TABLE ' . PROFILE_FIELDS_DATA_TABLE . " ADD $field_ident ";
1588
1589 switch ($field_type)
1590 {
1591 case FIELD_STRING:
1592 $sql .= ' VARCHAR2(255) ';
1593 break;
1594
1595 case FIELD_DATE:
1596 $sql .= 'VARCHAR2(10) ';
1597 break;
1598
1599 case FIELD_TEXT:
1600 $sql .= "CLOB";
1601 // ADD {$field_ident}_bbcode_uid VARCHAR2(5) NOT NULL,
1602 // ADD {$field_ident}_bbcode_bitfield NUMBER(11) UNSIGNED";
1603 break;
1604
1605 case FIELD_BOOL:
1606 $sql .= 'NUMBER(2) ';
1607 break;
1608
1609 case FIELD_DROPDOWN:
1610 $sql .= 'NUMBER(8) ';
1611 break;
1612
1613 case FIELD_INT:
1614 $sql .= 'NUMBER(20) ';
1615 break;
1616 }
1617
1618 break;
1619 }
1620
1621 return $sql;
1622 }
1623}
1624
1625?>
Note: See TracBrowser for help on using the repository browser.