~azzar1/unity/add-show-desktop-key

443 by dcoles
Added Forum application along with unmodifed version of phpBB3 "Olympus" 3.0.0
1
<?php
2
/**
3
*
4
* @package acp
5
* @version $Id: acp_profile.php,v 1.61 2007/11/15 19:54:37 kellanved Exp $
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
*/
14
if (!defined('IN_PHPBB'))
15
{
16
	exit;
17
}
18
19
/**
20
* @package acp
21
*/
22
class 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'		=> utf8_clean_string(request_var('field_ident', '', true)),
367
						'field_required'	=> 0,
368
						'field_hide'		=> 0,
369
						'field_no_view'		=> 0,
370
						'field_show_on_reg'	=> 0,
371
						'lang_name'			=> utf8_normalize_nfc(request_var('field_ident', '', true)),
372
						'lang_explain'		=> '',
373
						'lang_default_value'=> '')
374
					);
375
376
					$s_hidden_fields = '<input type="hidden" name="field_type" value="' . $field_type . '" />';
377
				}
378
379
				// $exclude contains the data we gather in each step
380
				$exclude = array(
381
					1	=> array('field_ident', 'lang_name', 'lang_explain', 'field_option', 'field_no_view'),
382
					2	=> array('field_length', 'field_maxlen', 'field_minlen', 'field_validation', 'field_novalue', 'field_default_value'),
383
					3	=> array('l_lang_name', 'l_lang_explain', 'l_lang_default_value', 'l_lang_options')
384
				);
385
386
				// Text-based fields require the lang_default_value to be excluded
387
				if ($field_type == FIELD_STRING || $field_type == FIELD_TEXT)
388
				{
389
					$exclude[1][] = 'lang_default_value';
390
				}
391
392
				// option-specific fields require lang_options to be excluded
393
				if ($field_type == FIELD_BOOL || $field_type == FIELD_DROPDOWN)
394
				{
395
					$exclude[1][] = 'lang_options';
396
				}
397
398
				$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']);
399
				$cp->vars['lang_name']			= utf8_normalize_nfc(request_var('lang_name', $field_row['lang_name'], true));
400
				$cp->vars['lang_explain']		= utf8_normalize_nfc(request_var('lang_explain', $field_row['lang_explain'], true));
401
				$cp->vars['lang_default_value']	= utf8_normalize_nfc(request_var('lang_default_value', $field_row['lang_default_value'], true));
402
403
				// Field option...
404
				if (isset($_REQUEST['field_option']))
405
				{
406
					$field_option = request_var('field_option', '');
407
408
					$cp->vars['field_required'] = ($field_option == 'field_required') ? 1 : 0;
409
					$cp->vars['field_show_on_reg'] = ($field_option == 'field_show_on_reg') ? 1 : 0;
410
					$cp->vars['field_hide'] = ($field_option == 'field_hide') ? 1 : 0;
411
				}
412
				else
413
				{
414
					$cp->vars['field_required'] = $field_row['field_required'];
415
					$cp->vars['field_show_on_reg'] = $field_row['field_show_on_reg'];
416
					$cp->vars['field_hide'] = $field_row['field_hide'];
417
418
					$field_option = ($field_row['field_required']) ? 'field_required' : (($field_row['field_show_on_reg']) ? 'field_show_on_reg' : (($field_row['field_hide']) ? 'field_hide' : ''));
419
				}
420
421
				$cp->vars['field_no_view'] = request_var('field_no_view', $field_row['field_no_view']);
422
423
				// A boolean field expects an array as the lang options
424
				if ($field_type == FIELD_BOOL)
425
				{
426
					$options = utf8_normalize_nfc(request_var('lang_options', array(''), true));
427
				}
428
				else
429
				{
430
					$options = utf8_normalize_nfc(request_var('lang_options', '', true));
431
				}
432
433
				// If the user has submitted a form with options (i.e. dropdown field)
434
				if ($options)
435
				{
436
					$exploded_options = (is_array($options)) ? $options : explode("\n", $options);
437
438
					if (sizeof($exploded_options) == sizeof($lang_options) || $action == 'create')
439
					{
440
						// The number of options in the field is equal to the number of options already in the database
441
						// Or we are creating a new dropdown list.
442
						$cp->vars['lang_options'] = $exploded_options;
443
					}
444
					else if ($action == 'edit')
445
					{
446
						// Changing the number of options? (We remove and re-create the option fields)
447
						$cp->vars['lang_options'] = $exploded_options;
448
					}
449
				}
450
				else
451
				{
452
					$cp->vars['lang_options'] = $lang_options;
453
				}
454
455
				// step 2
456
				foreach ($exclude[2] as $key)
457
				{
458
					$var = utf8_normalize_nfc(request_var($key, $field_row[$key], true));
459
460
					// Manipulate the intended variables a little bit if needed
461
					if ($field_type == FIELD_DROPDOWN && $key == 'field_maxlen')
462
					{
463
						// Get the number of options if this key is 'field_maxlen'
464
						$var = sizeof(explode("\n", utf8_normalize_nfc(request_var('lang_options', '', true))));
465
					}
466
					else if ($field_type == FIELD_TEXT && $key == 'field_length')
467
					{
468
						if (isset($_REQUEST['rows']))
469
						{
470
							$cp->vars['rows'] = request_var('rows', 0);
471
							$cp->vars['columns'] = request_var('columns', 0);
472
							$var = $cp->vars['rows'] . '|' . $cp->vars['columns'];
473
						}
474
						else
475
						{
476
							$row_col = explode('|', $var);
477
							$cp->vars['rows'] = $row_col[0];
478
							$cp->vars['columns'] = $row_col[1];
479
						}
480
					}
481
					else if ($field_type == FIELD_DATE && $key == 'field_default_value')
482
					{
483
						$always_now = request_var('always_now', -1);
484
						
485
						if ($always_now == 1 || ($always_now === -1 && $var == 'now'))
486
						{
487
							$now = getdate();
488
489
							$cp->vars['field_default_value_day'] = $now['mday'];
490
							$cp->vars['field_default_value_month'] = $now['mon'];
491
							$cp->vars['field_default_value_year'] = $now['year'];
492
							$var = $_POST['field_default_value'] = 'now';
493
						}
494
						else
495
						{
496
							if (isset($_REQUEST['field_default_value_day']))
497
							{
498
								$cp->vars['field_default_value_day'] = request_var('field_default_value_day', 0);
499
								$cp->vars['field_default_value_month'] = request_var('field_default_value_month', 0);
500
								$cp->vars['field_default_value_year'] = request_var('field_default_value_year', 0);
501
								$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']);
502
							}
503
							else
504
							{
505
								list($cp->vars['field_default_value_day'], $cp->vars['field_default_value_month'], $cp->vars['field_default_value_year']) = explode('-', $var);
506
							}
507
						}	
508
					}
509
					/* else if ($field_type == FIELD_BOOL && $key == 'field_default_value')
510
					{
511
						// Get the number of options if this key is 'field_maxlen'
512
						$var = request_var('field_default_value', 0);
513
					}*/
514
515
					$cp->vars[$key] = $var;
516
				}
517
518
				// step 3 - all arrays
519
				if ($action == 'edit')
520
				{
521
					// Get language entries
522
					$sql = 'SELECT *
523
						FROM ' . PROFILE_FIELDS_LANG_TABLE . '
524
						WHERE lang_id <> ' . $this->edit_lang_id . "
525
							AND field_id = $field_id
526
						ORDER BY option_id ASC";
527
					$result = $db->sql_query($sql);
528
529
					$l_lang_options = array();
530
					while ($row = $db->sql_fetchrow($result))
531
					{
532
						$l_lang_options[$row['lang_id']][$row['option_id']] = $row['lang_value'];
533
					}
534
					$db->sql_freeresult($result);
535
536
		
537
					$sql = 'SELECT lang_id, lang_name, lang_explain, lang_default_value
538
						FROM ' . PROFILE_LANG_TABLE . '
539
						WHERE lang_id <> ' . $this->edit_lang_id . "
540
							AND field_id = $field_id
541
						ORDER BY lang_id ASC";
542
					$result = $db->sql_query($sql);
543
544
					$l_lang_name = $l_lang_explain = $l_lang_default_value = array();
545
					while ($row = $db->sql_fetchrow($result))
546
					{
547
						$l_lang_name[$row['lang_id']] = $row['lang_name'];
548
						$l_lang_explain[$row['lang_id']] = $row['lang_explain'];
549
						$l_lang_default_value[$row['lang_id']] = $row['lang_default_value'];
550
					}
551
					$db->sql_freeresult($result);
552
				}
553
		
554
				foreach ($exclude[3] as $key)
555
				{
556
					$cp->vars[$key] = utf8_normalize_nfc(request_var($key, array(0 => ''), true));
557
558
					if (!$cp->vars[$key] && $action == 'edit')
559
					{
560
						$cp->vars[$key] = $$key;
561
					}
562
					else if ($key == 'l_lang_options' && $field_type == FIELD_BOOL)
563
					{
564
						$cp->vars[$key] = utf8_normalize_nfc(request_var($key, array(0 => array('')), true));
565
					}
566
					else if ($key == 'l_lang_options' && is_array($cp->vars[$key]))
567
					{
568
						foreach ($cp->vars[$key] as $lang_id => $options)
569
						{
570
							$cp->vars[$key][$lang_id] = explode("\n", $options);
571
						}
572
						
573
					}
574
				}
575
576
				// Check for general issues in every step
577
				if ($submit) //  && $step == 1
578
				{
579
					// Check values for step 1
580
					if ($cp->vars['field_ident'] == '')
581
					{
582
						$error[] = $user->lang['EMPTY_FIELD_IDENT'];
583
					}
584
585
					if (!preg_match('/^[a-z_]+$/', $cp->vars['field_ident']))
586
					{
587
						$error[] = $user->lang['INVALID_CHARS_FIELD_IDENT'];
588
					}
589
590
					if (strlen($cp->vars['field_ident']) > 17)
591
					{
592
						$error[] = $user->lang['INVALID_FIELD_IDENT_LEN'];
593
					}
594
595
					if ($cp->vars['lang_name'] == '')
596
					{
597
						$error[] = $user->lang['EMPTY_USER_FIELD_NAME'];
598
					}
599
600
					if ($field_type == FIELD_DROPDOWN && !sizeof($cp->vars['lang_options']))
601
					{
602
						$error[] = $user->lang['NO_FIELD_ENTRIES'];
603
					}
604
605
					if ($field_type == FIELD_BOOL && (empty($cp->vars['lang_options'][0]) || empty($cp->vars['lang_options'][1])))
606
					{
607
						$error[] = $user->lang['NO_FIELD_ENTRIES'];
608
					}
609
610
					// Check for already existing field ident
611
					if ($action != 'edit')
612
					{
613
						$sql = 'SELECT field_ident
614
							FROM ' . PROFILE_FIELDS_TABLE . "
615
							WHERE field_ident = '" . $db->sql_escape($cp->vars['field_ident']) . "'";
616
						$result = $db->sql_query($sql);
617
						$row = $db->sql_fetchrow($result);
618
						$db->sql_freeresult($result);
619
620
						if ($row)
621
						{
622
							$error[] = $user->lang['FIELD_IDENT_ALREADY_EXIST'];
623
						}
624
					}
625
				}
626
627
				$step = (isset($_REQUEST['next'])) ? $step + 1 : ((isset($_REQUEST['prev'])) ? $step - 1 : $step);
628
629
				if (sizeof($error))
630
				{
631
					$step--;
632
					$submit = false;
633
				}
634
635
				// Build up the specific hidden fields
636
				foreach ($exclude as $num => $key_ary)
637
				{
638
					if ($num == $step)
639
					{
640
						continue;
641
					}
642
643
					$_new_key_ary = array();
644
645
					foreach ($key_ary as $key)
646
					{
647
						if ($field_type == FIELD_TEXT && $key == 'field_length' && isset($_REQUEST['rows']))
648
						{
649
							$cp->vars['rows'] = request_var('rows', 0);
650
							$cp->vars['columns'] = request_var('columns', 0);
651
							$_new_key_ary[$key] = $cp->vars['rows'] . '|' . $cp->vars['columns'];
652
						}
653
						else if ($field_type == FIELD_DATE && $key == 'field_default_value')
654
						{
655
							$always_now = request_var('always_now', 0);
656
657
							if ($always_now)
658
							{
659
								$_new_key_ary[$key] = 'now';
660
							}
661
							else if (isset($_REQUEST['field_default_value_day']))
662
							{
663
								$cp->vars['field_default_value_day'] = request_var('field_default_value_day', 0);
664
								$cp->vars['field_default_value_month'] = request_var('field_default_value_month', 0);
665
								$cp->vars['field_default_value_year'] = request_var('field_default_value_year', 0);
666
								$_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']);
667
							}
668
						}
669
						else if ($field_type == FIELD_BOOL && $key == 'l_lang_options' && isset($_REQUEST['l_lang_options']))
670
						{
671
							$_new_key_ary[$key] = utf8_normalize_nfc(request_var($key, array(array('')), true));
672
						}
673
						else
674
						{
675
							if (!isset($_REQUEST[$key]))
676
							{
677
								$var = false;
678
							}
679
							else if ($key == 'field_ident' && isset($cp->vars[$key]))
680
							{
681
								$_new_key_ary[$key]= $cp->vars[$key];
682
							}
683
							else
684
							{
685
								$_new_key_ary[$key] = (is_array($_REQUEST[$key])) ? utf8_normalize_nfc(request_var($key, array(''), true)) : utf8_normalize_nfc(request_var($key, '', true));
686
							}
687
						}
688
					}
689
690
					$s_hidden_fields .= build_hidden_fields($_new_key_ary);
691
				}
692
693
				if (!sizeof($error))
694
				{
695
					if ($step == 3 && (sizeof($this->lang_defs['iso']) == 1 || $save))
696
					{
697
						$this->save_profile_field($cp, $field_type, $action);
698
					}
699
					else if ($action == 'edit' && $save)
700
					{
701
						$this->save_profile_field($cp, $field_type, $action);
702
					}
703
				}
704
705
				$template->assign_vars(array(
706
					'S_EDIT'			=> true,
707
					'S_EDIT_MODE'		=> ($action == 'edit') ? true : false,
708
					'ERROR_MSG'			=> (sizeof($error)) ? implode('<br />', $error) : '',
709
710
					'L_TITLE'			=> $user->lang['STEP_' . $step . '_TITLE_' . strtoupper($action)],
711
					'L_EXPLAIN'			=> $user->lang['STEP_' . $step . '_EXPLAIN_' . strtoupper($action)],
712
					
713
					'U_ACTION'			=> $this->u_action . "&amp;action=$action&amp;step=$step",
714
					'U_BACK'			=> $this->u_action)
715
				);
716
717
				// Now go through the steps
718
				switch ($step)
719
				{
720
					// Create basic options - only small differences between field types
721
					case 1:
722
	
723
						// Build common create options
724
						$template->assign_vars(array(
725
							'S_STEP_ONE'		=> true,
726
							'S_FIELD_REQUIRED'	=> ($cp->vars['field_required']) ? true : false,
727
							'S_SHOW_ON_REG'		=> ($cp->vars['field_show_on_reg']) ? true : false,
728
							'S_FIELD_HIDE'		=> ($cp->vars['field_hide']) ? true : false,
729
							'S_FIELD_NO_VIEW'	=> ($cp->vars['field_no_view']) ? true : false,
730
731
							'L_LANG_SPECIFIC'	=> sprintf($user->lang['LANG_SPECIFIC_OPTIONS'], $config['default_lang']),
732
							'FIELD_TYPE'		=> $user->lang['FIELD_' . strtoupper($cp->profile_types[$field_type])],
733
							'FIELD_IDENT'		=> $cp->vars['field_ident'],
734
							'LANG_NAME'			=> $cp->vars['lang_name'],
735
							'LANG_EXPLAIN'		=> $cp->vars['lang_explain'])
736
						);
737
738
						// String and Text needs to set default values here...
739
						if ($field_type == FIELD_STRING || $field_type == FIELD_TEXT)
740
						{
741
							$template->assign_vars(array(
742
								'S_TEXT'		=> ($field_type == FIELD_TEXT) ? true : false,
743
								'S_STRING'		=> ($field_type == FIELD_STRING) ? true : false,
744
745
								'L_DEFAULT_VALUE_EXPLAIN'	=> $user->lang[strtoupper($cp->profile_types[$field_type]) . '_DEFAULT_VALUE_EXPLAIN'],
746
								'LANG_DEFAULT_VALUE'		=> $cp->vars['lang_default_value'])
747
							);
748
						}
749
			
750
						if ($field_type == FIELD_BOOL || $field_type == FIELD_DROPDOWN)
751
						{
752
							// Initialize these array elements if we are creating a new field
753
							if (!sizeof($cp->vars['lang_options']))
754
							{
755
								if ($field_type == FIELD_BOOL)
756
								{
757
									// No options have been defined for a boolean field.
758
									$cp->vars['lang_options'][0] = '';
759
									$cp->vars['lang_options'][1] = '';
760
								}
761
								else
762
								{
763
									// No options have been defined for the dropdown menu
764
									$cp->vars['lang_options'] = array();
765
								}
766
							}
767
768
							$template->assign_vars(array(
769
								'S_BOOL'		=> ($field_type == FIELD_BOOL) ? true : false,
770
								'S_DROPDOWN'	=> ($field_type == FIELD_DROPDOWN) ? true : false,
771
772
								'L_LANG_OPTIONS_EXPLAIN'	=> $user->lang[strtoupper($cp->profile_types[$field_type]) . '_ENTRIES_EXPLAIN'],
773
								'LANG_OPTIONS'				=> ($field_type == FIELD_DROPDOWN) ? implode("\n", $cp->vars['lang_options']) : '',
774
								'FIRST_LANG_OPTION'			=> ($field_type == FIELD_BOOL) ? $cp->vars['lang_options'][0] : '',
775
								'SECOND_LANG_OPTION'		=> ($field_type == FIELD_BOOL) ? $cp->vars['lang_options'][1] : '')
776
							);
777
						}
778
					
779
					break;
780
781
					case 2:
782
						
783
						$template->assign_vars(array(
784
							'S_STEP_TWO'		=> true,
785
							'L_NEXT_STEP'			=> (sizeof($this->lang_defs['iso']) == 1) ? $user->lang['SAVE'] : $user->lang['PROFILE_LANG_OPTIONS'])
786
						);
787
788
						// Build options based on profile type
789
						$function = 'get_' . $cp->profile_types[$field_type] . '_options';
790
						$options = $cp->$function();
791
792
						foreach ($options as $num => $option_ary)
793
						{
794
							$template->assign_block_vars('option', $option_ary);
795
						}
796
797
					break;
798
799
					// Define remaining language variables
800
					case 3:
801
802
						$template->assign_var('S_STEP_THREE', true);
803
						$options = $this->build_language_options($cp, $field_type, $action);
804
805
						foreach ($options as $lang_id => $lang_ary)
806
						{
807
							$template->assign_block_vars('options', array(
808
								'LANGUAGE'		=> sprintf($user->lang[(($lang_id == $this->edit_lang_id) ? 'DEFAULT_' : '') . 'ISO_LANGUAGE'], $lang_ary['lang_iso']))
809
							);
810
811
							foreach ($lang_ary['fields'] as $field_ident => $field_ary)
812
							{
813
								$template->assign_block_vars('options.field', array(
814
									'L_TITLE'		=> $field_ary['TITLE'],
815
									'L_EXPLAIN'		=> (isset($field_ary['EXPLAIN'])) ? $field_ary['EXPLAIN'] : '',
816
									'FIELD'			=> $field_ary['FIELD'])
817
								);
818
							}
819
						}
820
		
821
					break;
822
				}
823
824
				$template->assign_vars(array(
825
					'S_HIDDEN_FIELDS'	=> $s_hidden_fields)
826
				);
827
828
				return;
829
830
			break;
831
		}
832
		
833
		$sql = 'SELECT *
834
			FROM ' . PROFILE_FIELDS_TABLE . '
835
			ORDER BY field_order';
836
		$result = $db->sql_query($sql);
837
838
		$s_one_need_edit = false;
839
		while ($row = $db->sql_fetchrow($result))
840
		{
841
			$active_lang = (!$row['field_active']) ? 'ACTIVATE' : 'DEACTIVATE';
842
			$active_value = (!$row['field_active']) ? 'activate' : 'deactivate';
843
			$id = $row['field_id'];
844
845
			$s_need_edit = (sizeof($this->lang_defs['diff'][$row['field_id']])) ? true : false;
846
847
			if ($s_need_edit)
848
			{
849
				$s_one_need_edit = true;
850
			}
851
852
			$template->assign_block_vars('fields', array(
853
				'FIELD_IDENT'		=> $row['field_ident'],
854
				'FIELD_TYPE'		=> $user->lang['FIELD_' . strtoupper($cp->profile_types[$row['field_type']])],
855
856
				'L_ACTIVATE_DEACTIVATE'		=> $user->lang[$active_lang],
857
				'U_ACTIVATE_DEACTIVATE'		=> $this->u_action . "&amp;action=$active_value&amp;field_id=$id",
858
				'U_EDIT'					=> $this->u_action . "&amp;action=edit&amp;field_id=$id",
859
				'U_TRANSLATE'				=> $this->u_action . "&amp;action=edit&amp;field_id=$id&amp;step=3",
860
				'U_DELETE'					=> $this->u_action . "&amp;action=delete&amp;field_id=$id",
861
				'U_MOVE_UP'					=> $this->u_action . "&amp;action=move_up&amp;order={$row['field_order']}",
862
				'U_MOVE_DOWN'				=> $this->u_action . "&amp;action=move_down&amp;order={$row['field_order']}",
863
864
				'S_NEED_EDIT'				=> $s_need_edit)
865
			);
866
		}
867
		$db->sql_freeresult($result);
868
869
		// At least one option field needs editing?
870
		if ($s_one_need_edit)
871
		{
872
			$template->assign_var('S_NEED_EDIT', true);
873
		}
874
875
		$s_select_type = '';
876
		foreach ($cp->profile_types as $key => $value)
877
		{
878
			$s_select_type .= '<option value="' . $key . '">' . $user->lang['FIELD_' . strtoupper($value)] . '</option>';
879
		}
880
881
		$template->assign_vars(array(
882
			'U_ACTION'			=> $this->u_action,
883
			'S_TYPE_OPTIONS'	=> $s_select_type)
884
		);
885
	}
886
887
	/**
888
	* Build all Language specific options
889
	*/
890
	function build_language_options(&$cp, $field_type, $action = 'create')
891
	{
892
		global $user, $config, $db;
893
894
		$default_lang_id = (!empty($this->edit_lang_id)) ? $this->edit_lang_id : $this->lang_defs['iso'][$config['default_lang']];
895
896
		$sql = 'SELECT lang_id, lang_iso
897
			FROM ' . LANG_TABLE . '
898
			WHERE lang_id <> ' . (int) $default_lang_id . '
899
			ORDER BY lang_english_name';
900
		$result = $db->sql_query($sql);
901
902
		$languages = array();
903
		while ($row = $db->sql_fetchrow($result))
904
		{
905
			$languages[$row['lang_id']] = $row['lang_iso'];
906
		}
907
		$db->sql_freeresult($result);
908
		
909
		$options = array();
910
		$options['lang_name'] = 'string';
911
		if ($cp->vars['lang_explain'])
912
		{
913
			$options['lang_explain'] = 'text';
914
		}
915
	
916
		switch ($field_type)
917
		{
918
			case FIELD_BOOL:
919
				$options['lang_options'] = 'two_options';
920
			break;
921
922
			case FIELD_DROPDOWN:
923
				$options['lang_options'] = 'optionfield';
924
			break;
925
			
926
			case FIELD_TEXT:
927
			case FIELD_STRING:
928
				if ($cp->vars['lang_default_value'])
929
				{
930
					$options['lang_default_value'] = ($field_type == FIELD_STRING) ? 'string' : 'text';
931
				}
932
			break;
933
		}
934
	
935
		$lang_options = array();
936
937
		foreach ($options as $field => $field_type)
938
		{
939
			$lang_options[1]['lang_iso'] = $this->lang_defs['id'][$default_lang_id];
940
			$lang_options[1]['fields'][$field] = array(
941
				'TITLE'		=> $user->lang['CP_' . strtoupper($field)],
942
				'FIELD'		=> '<dd>' . ((is_array($cp->vars[$field])) ? implode('<br />', $cp->vars[$field]) : bbcode_nl2br($cp->vars[$field])) . '</dd>'
943
			);
944
945
			if (isset($user->lang['CP_' . strtoupper($field) . '_EXPLAIN']))
946
			{
947
				$lang_options[1]['fields'][$field]['EXPLAIN'] = $user->lang['CP_' . strtoupper($field) . '_EXPLAIN'];
948
			}
949
		}
950
951
		foreach ($languages as $lang_id => $lang_iso)
952
		{
953
			$lang_options[$lang_id]['lang_iso'] = $lang_iso;
954
			foreach ($options as $field => $field_type)
955
			{
956
				$value = ($action == 'create') ? utf8_normalize_nfc(request_var('l_' . $field, array(0 => ''), true)) : $cp->vars['l_' . $field];
957
				if ($field == 'lang_options')
958
				{
959
					$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];
960
					
961
					switch ($field_type)
962
					{
963
						case 'two_options':
964
965
							$lang_options[$lang_id]['fields'][$field] = array(
966
								'TITLE'		=> $user->lang['CP_' . strtoupper($field)],
967
								'FIELD'		=> '
968
											<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>
969
											<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>'
970
							);
971
						break;
972
973
						case 'optionfield':
974
							$value = ((isset($value[$lang_id])) ? ((is_array($value[$lang_id])) ?  implode("\n", $value[$lang_id]) : $value[$lang_id]) : implode("\n", $var));
975
							$lang_options[$lang_id]['fields'][$field] = array(
976
								'TITLE'		=> $user->lang['CP_' . strtoupper($field)],
977
								'FIELD'		=> '<dd><textarea name="l_' . $field . '[' . $lang_id . ']" rows="7" cols="80">' . $value . '</textarea></dd>'
978
							);
979
						break;
980
					}
981
				
982
					if (isset($user->lang['CP_' . strtoupper($field) . '_EXPLAIN']))
983
					{
984
						$lang_options[$lang_id]['fields'][$field]['EXPLAIN'] = $user->lang['CP_' . strtoupper($field) . '_EXPLAIN'];
985
					}
986
				}
987
				else
988
				{
989
					$var = ($action == 'create' || !is_array($cp->vars[$field])) ? $cp->vars[$field] : $cp->vars[$field][$lang_id];
990
991
					$lang_options[$lang_id]['fields'][$field] = array(
992
						'TITLE'		=> $user->lang['CP_' . strtoupper($field)],
993
						'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>'
994
					);
995
			
996
					if (isset($user->lang['CP_' . strtoupper($field) . '_EXPLAIN']))
997
					{
998
						$lang_options[$lang_id]['fields'][$field]['EXPLAIN'] = $user->lang['CP_' . strtoupper($field) . '_EXPLAIN'];
999
					}
1000
				}
1001
			}
1002
		}
1003
1004
		return $lang_options;
1005
	}
1006
1007
	/**
1008
	* Save Profile Field
1009
	*/
1010
	function save_profile_field(&$cp, $field_type, $action = 'create')
1011
	{
1012
		global $db, $config, $user;
1013
1014
		$field_id = request_var('field_id', 0);
1015
1016
		// Collect all information, if something is going wrong, abort the operation
1017
		$profile_sql = $profile_lang = $empty_lang = $profile_lang_fields = array();
1018
1019
		$default_lang_id = (!empty($this->edit_lang_id)) ? $this->edit_lang_id : $this->lang_defs['iso'][$config['default_lang']];
1020
1021
		if ($action == 'create')
1022
		{
1023
			$sql = 'SELECT MAX(field_order) as max_field_order
1024
				FROM ' . PROFILE_FIELDS_TABLE;
1025
			$result = $db->sql_query($sql);
1026
			$new_field_order = (int) $db->sql_fetchfield('max_field_order');
1027
			$db->sql_freeresult($result);
1028
1029
			$field_ident = $cp->vars['field_ident'];
1030
		}
1031
1032
		// Save the field
1033
		$profile_fields = array(
1034
			'field_length'			=> $cp->vars['field_length'],
1035
			'field_minlen'			=> $cp->vars['field_minlen'],
1036
			'field_maxlen'			=> $cp->vars['field_maxlen'],
1037
			'field_novalue'			=> $cp->vars['field_novalue'],
1038
			'field_default_value'	=> $cp->vars['field_default_value'],
1039
			'field_validation'		=> $cp->vars['field_validation'],
1040
			'field_required'		=> $cp->vars['field_required'],
1041
			'field_show_on_reg'		=> $cp->vars['field_show_on_reg'],
1042
			'field_hide'			=> $cp->vars['field_hide'],
1043
			'field_no_view'			=> $cp->vars['field_no_view']
1044
		);
1045
1046
		if ($action == 'create')
1047
		{
1048
			$profile_fields += array(
1049
				'field_type'		=> $field_type,
1050
				'field_ident'		=> $field_ident,
1051
				'field_name'		=> $field_ident,
1052
				'field_order'		=> $new_field_order + 1,
1053
				'field_active'		=> 1
1054
			);
1055
1056
			$sql = 'INSERT INTO ' . PROFILE_FIELDS_TABLE . ' ' . $db->sql_build_array('INSERT', $profile_fields);
1057
			$db->sql_query($sql);
1058
1059
			$field_id = $db->sql_nextid();
1060
		}
1061
		else
1062
		{
1063
			$sql = 'UPDATE ' . PROFILE_FIELDS_TABLE . '
1064
				SET ' . $db->sql_build_array('UPDATE', $profile_fields) . "
1065
				WHERE field_id = $field_id";
1066
			$db->sql_query($sql);
1067
		}
1068
		
1069
		if ($action == 'create')
1070
		{
1071
			$field_ident = 'pf_' . $field_ident;
1072
			$profile_sql[] = $this->add_field_ident($field_ident, $field_type);
1073
		}
1074
1075
		$sql_ary = array(
1076
			'lang_name'				=> $cp->vars['lang_name'],
1077
			'lang_explain'			=> $cp->vars['lang_explain'],
1078
			'lang_default_value'	=> $cp->vars['lang_default_value']
1079
		);
1080
1081
		if ($action == 'create')
1082
		{
1083
			$sql_ary['field_id'] = $field_id;
1084
			$sql_ary['lang_id'] = $default_lang_id;
1085
		
1086
			$profile_sql[] = 'INSERT INTO ' . PROFILE_LANG_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
1087
		}
1088
		else
1089
		{
1090
			$this->update_insert(PROFILE_LANG_TABLE, $sql_ary, array('field_id' => $field_id, 'lang_id' => $default_lang_id));
1091
		}
1092
1093
		if (is_array($cp->vars['l_lang_name']) && sizeof($cp->vars['l_lang_name']))
1094
		{
1095
			foreach ($cp->vars['l_lang_name'] as $lang_id => $data)
1096
			{
1097
				if (($cp->vars['lang_name'] != '' && $cp->vars['l_lang_name'][$lang_id] == '')
1098
					|| ($cp->vars['lang_explain'] != '' && $cp->vars['l_lang_explain'][$lang_id] == '')
1099
					|| ($cp->vars['lang_default_value'] != '' && $cp->vars['l_lang_default_value'][$lang_id] == ''))
1100
				{
1101
					$empty_lang[$lang_id] = true;
1102
					break;
1103
				}
1104
1105
				if (!isset($empty_lang[$lang_id]))
1106
				{
1107
					$profile_lang[] = array(
1108
						'field_id'		=> $field_id,
1109
						'lang_id'		=> $lang_id,
1110
						'lang_name'		=> $cp->vars['l_lang_name'][$lang_id],
1111
						'lang_explain'	=> (isset($cp->vars['l_lang_explain'][$lang_id])) ? $cp->vars['l_lang_explain'][$lang_id] : '',
1112
						'lang_default_value'	=> (isset($cp->vars['l_lang_default_value'][$lang_id])) ? $cp->vars['l_lang_default_value'][$lang_id] : ''
1113
					);
1114
				}
1115
			}
1116
1117
			foreach ($empty_lang as $lang_id => $NULL)
1118
			{
1119
				$sql = 'DELETE FROM ' . PROFILE_LANG_TABLE . "
1120
					WHERE field_id = $field_id
1121
					AND lang_id = " . (int) $lang_id;
1122
				$db->sql_query($sql);
1123
			}
1124
		}
1125
1126
		// These are always arrays because the key is the language id...
1127
		$cp->vars['l_lang_name']			= utf8_normalize_nfc(request_var('l_lang_name', array(0 => ''), true));
1128
		$cp->vars['l_lang_explain']			= utf8_normalize_nfc(request_var('l_lang_explain', array(0 => ''), true));
1129
		$cp->vars['l_lang_default_value']	= utf8_normalize_nfc(request_var('l_lang_default_value', array(0 => ''), true));
1130
1131
		if ($field_type != FIELD_BOOL)
1132
		{
1133
			$cp->vars['l_lang_options']			= utf8_normalize_nfc(request_var('l_lang_options', array(0 => ''), true));
1134
		}
1135
		else
1136
		{
1137
			/**
1138
			* @todo check if this line is correct...
1139
			$cp->vars['l_lang_default_value']	= request_var('l_lang_default_value', array(0 => array('')), true);
1140
			*/
1141
			$cp->vars['l_lang_options']	= utf8_normalize_nfc(request_var('l_lang_options', array(0 => array('')), true));
1142
		}
1143
1144
		if ($cp->vars['lang_options'])
1145
		{
1146
			if (!is_array($cp->vars['lang_options']))
1147
			{
1148
				$cp->vars['lang_options'] = explode("\n", $cp->vars['lang_options']);
1149
			}
1150
1151
			if ($action != 'create')
1152
			{
1153
				$sql = 'DELETE FROM ' . PROFILE_FIELDS_LANG_TABLE . "
1154
					WHERE field_id = $field_id
1155
						AND lang_id = " . (int) $default_lang_id;
1156
				$db->sql_query($sql);
1157
			}
1158
			
1159
			foreach ($cp->vars['lang_options'] as $option_id => $value)
1160
			{
1161
				$sql_ary = array(
1162
					'field_type'	=> (int) $field_type,
1163
					'lang_value'	=> $value
1164
				);
1165
1166
				if ($action == 'create')
1167
				{
1168
					$sql_ary['field_id'] = $field_id;
1169
					$sql_ary['lang_id'] = $default_lang_id;
1170
					$sql_ary['option_id'] = (int) $option_id;
1171
1172
					$profile_sql[] = 'INSERT INTO ' . PROFILE_FIELDS_LANG_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
1173
				}
1174
				else
1175
				{
1176
					$this->update_insert(PROFILE_FIELDS_LANG_TABLE, $sql_ary, array(
1177
						'field_id'	=> $field_id,
1178
						'lang_id'	=> (int) $default_lang_id,
1179
						'option_id'	=> (int) $option_id)
1180
					);
1181
				}
1182
			}
1183
		}
1184
1185
		if (is_array($cp->vars['l_lang_options']) && sizeof($cp->vars['l_lang_options']))
1186
		{
1187
			$empty_lang = array();
1188
1189
			foreach ($cp->vars['l_lang_options'] as $lang_id => $lang_ary)
1190
			{
1191
				if (!is_array($lang_ary))
1192
				{
1193
					$lang_ary = explode("\n", $lang_ary);
1194
				}
1195
1196
				if (sizeof($lang_ary) != sizeof($cp->vars['lang_options']))
1197
				{
1198
					$empty_lang[$lang_id] = true;
1199
				}
1200
1201
				if (!isset($empty_lang[$lang_id]))
1202
				{
1203
					if ($action != 'create')
1204
					{
1205
						$sql = 'DELETE FROM ' . PROFILE_FIELDS_LANG_TABLE . "
1206
							WHERE field_id = $field_id
1207
							AND lang_id = " . (int) $lang_id;
1208
						$db->sql_query($sql);
1209
					}
1210
1211
					foreach ($lang_ary as $option_id => $value)
1212
					{
1213
						$profile_lang_fields[] = array(
1214
							'field_id'		=> (int) $field_id,
1215
							'lang_id'		=> (int) $lang_id,
1216
							'option_id'		=> (int) $option_id,
1217
							'field_type'	=> (int) $field_type,
1218
							'lang_value'	=> $value
1219
						);
1220
					}
1221
				}
1222
			}
1223
1224
			foreach ($empty_lang as $lang_id => $NULL)
1225
			{
1226
				$sql = 'DELETE FROM ' . PROFILE_FIELDS_LANG_TABLE . "
1227
					WHERE field_id = $field_id
1228
					AND lang_id = " . (int) $lang_id;
1229
				$db->sql_query($sql);
1230
			}
1231
		}
1232
1233
		foreach ($profile_lang as $sql)
1234
		{
1235
			if ($action == 'create')
1236
			{
1237
				$profile_sql[] = 'INSERT INTO ' . PROFILE_LANG_TABLE . ' ' . $db->sql_build_array('INSERT', $sql);
1238
			}
1239
			else
1240
			{
1241
				$lang_id = $sql['lang_id'];
1242
				unset($sql['lang_id'], $sql['field_id']);
1243
1244
				$this->update_insert(PROFILE_LANG_TABLE, $sql, array('lang_id' => (int) $lang_id, 'field_id' => $field_id));
1245
			}
1246
		}
1247
1248
		if (sizeof($profile_lang_fields))
1249
		{
1250
			foreach ($profile_lang_fields as $sql)
1251
			{
1252
				if ($action == 'create')
1253
				{
1254
					$profile_sql[] = 'INSERT INTO ' . PROFILE_FIELDS_LANG_TABLE . ' ' . $db->sql_build_array('INSERT', $sql);
1255
				}
1256
				else
1257
				{
1258
					$lang_id = $sql['lang_id'];
1259
					$option_id = $sql['option_id'];
1260
					unset($sql['lang_id'], $sql['field_id'], $sql['option_id']);
1261
					
1262
					$this->update_insert(PROFILE_FIELDS_LANG_TABLE, $sql, array(
1263
						'lang_id'	=> $lang_id,
1264
						'field_id'	=> $field_id,
1265
						'option_id'	=> $option_id)
1266
					);
1267
				}
1268
			}
1269
		}
1270
1271
1272
		$db->sql_transaction('begin');
1273
1274
		if ($action == 'create')
1275
		{
1276
			foreach ($profile_sql as $sql)
1277
			{
1278
				$db->sql_query($sql);
1279
			}
1280
		}
1281
		
1282
		$db->sql_transaction('commit');
1283
1284
		if ($action == 'edit')
1285
		{
1286
			add_log('admin', 'LOG_PROFILE_FIELD_EDIT', $cp->vars['field_ident'] . ':' . $cp->vars['lang_name']);
1287
			trigger_error($user->lang['CHANGED_PROFILE_FIELD'] . adm_back_link($this->u_action));
1288
		}
1289
		else
1290
		{
1291
			add_log('admin', 'LOG_PROFILE_FIELD_CREATE', substr($field_ident, 3) . ':' . $cp->vars['lang_name']);
1292
			trigger_error($user->lang['ADDED_PROFILE_FIELD'] . adm_back_link($this->u_action));
1293
		}
1294
	}
1295
1296
	/**
1297
	* Update, then insert if not successfull
1298
	*/
1299
	function update_insert($table, $sql_ary, $where_fields)
1300
	{
1301
		global $db;
1302
1303
		$where_sql = array();
1304
		$check_key = '';
1305
1306
		foreach ($where_fields as $key => $value)
1307
		{
1308
			$check_key = (!$check_key) ? $key : $check_key;
1309
			$where_sql[] = $key . ' = ' . ((is_string($value)) ? "'" . $db->sql_escape($value) . "'" : (int) $value);
1310
		}
1311
1312
		if (!sizeof($where_sql))
1313
		{
1314
			return;
1315
		}
1316
1317
		$sql = "SELECT $check_key
1318
			FROM $table
1319
			WHERE " . implode(' AND ', $where_sql);
1320
		$result = $db->sql_query($sql);
1321
		$row = $db->sql_fetchrow($result);
1322
		$db->sql_freeresult($result);
1323
1324
		if (!$row)
1325
		{
1326
			$sql_ary = array_merge($where_fields, $sql_ary);
1327
			
1328
			if (sizeof($sql_ary))
1329
			{
1330
				$db->sql_query("INSERT INTO $table " . $db->sql_build_array('INSERT', $sql_ary));
1331
			}
1332
		}
1333
		else
1334
		{
1335
			if (sizeof($sql_ary))
1336
			{
1337
				$sql = "UPDATE $table SET " . $db->sql_build_array('UPDATE', $sql_ary) . '
1338
					WHERE ' . implode(' AND ', $where_sql);
1339
				$db->sql_query($sql);
1340
			}
1341
		}
1342
	}
1343
1344
	/**
1345
	* Return sql statement for adding a new field ident (profile field) to the profile fields data table
1346
	*/
1347
	function add_field_ident($field_ident, $field_type)
1348
	{
1349
		global $db;
1350
1351
		switch ($db->sql_layer)
1352
		{
1353
			case 'mysql':
1354
			case 'mysql4':
1355
			case 'mysqli':
1356
1357
				// We are defining the biggest common value, because of the possibility to edit the min/max values of each field.
1358
				$sql = 'ALTER TABLE ' . PROFILE_FIELDS_DATA_TABLE . " ADD `$field_ident` ";
1359
1360
				switch ($field_type)
1361
				{
1362
					case FIELD_STRING:
1363
						$sql .= ' VARCHAR(255) ';
1364
					break;
1365
1366
					case FIELD_DATE:
1367
						$sql .= 'VARCHAR(10) ';
1368
					break;
1369
1370
					case FIELD_TEXT:
1371
						$sql .= "TEXT";
1372
		//						ADD {$field_ident}_bbcode_uid VARCHAR(5) NOT NULL,
1373
		//						ADD {$field_ident}_bbcode_bitfield INT(11) UNSIGNED";
1374
					break;
1375
1376
					case FIELD_BOOL:
1377
						$sql .= 'TINYINT(2) ';
1378
					break;
1379
				
1380
					case FIELD_DROPDOWN:
1381
						$sql .= 'MEDIUMINT(8) ';
1382
					break;
1383
1384
					case FIELD_INT:
1385
						$sql .= 'BIGINT(20) ';
1386
					break;
1387
				}
1388
1389
			break;
1390
1391
			case 'sqlite':
1392
1393
				switch ($field_type)
1394
				{
1395
					case FIELD_STRING:
1396
						$type = ' VARCHAR(255) ';
1397
					break;
1398
1399
					case FIELD_DATE:
1400
						$type = 'VARCHAR(10) ';
1401
					break;
1402
1403
					case FIELD_TEXT:
1404
						$type = "TEXT(65535)";
1405
		//						ADD {$field_ident}_bbcode_uid VARCHAR(5) NOT NULL,
1406
		//						ADD {$field_ident}_bbcode_bitfield INT(11) UNSIGNED";
1407
					break;
1408
1409
					case FIELD_BOOL:
1410
						$type = 'TINYINT(2) ';
1411
					break;
1412
1413
					case FIELD_DROPDOWN:
1414
						$type = 'MEDIUMINT(8) ';
1415
					break;
1416
1417
					case FIELD_INT:
1418
						$type = 'BIGINT(20) ';
1419
					break;
1420
				}
1421
1422
				// We are defining the biggest common value, because of the possibility to edit the min/max values of each field.
1423
				if (version_compare(sqlite_libversion(), '3.0') == -1)
1424
				{
1425
					$sql = "SELECT sql
1426
						FROM sqlite_master
1427
						WHERE type = 'table'
1428
							AND name = '" . PROFILE_FIELDS_DATA_TABLE . "'
1429
						ORDER BY type DESC, name;";
1430
					$result = $db->sql_query($sql);
1431
					$row = $db->sql_fetchrow($result);
1432
					$db->sql_freeresult($result);
1433
1434
					// Create a temp table and populate it, destroy the existing one
1435
					$db->sql_query(preg_replace('#CREATE\s+TABLE\s+"?' . PROFILE_FIELDS_DATA_TABLE . '"?#i', 'CREATE TEMPORARY TABLE ' . PROFILE_FIELDS_DATA_TABLE . '_temp', $row['sql']));
1436
					$db->sql_query('INSERT INTO ' . PROFILE_FIELDS_DATA_TABLE . '_temp SELECT * FROM ' . PROFILE_FIELDS_DATA_TABLE);
1437
					$db->sql_query('DROP TABLE ' . PROFILE_FIELDS_DATA_TABLE);
1438
1439
					preg_match('#\((.*)\)#s', $row['sql'], $matches);
1440
1441
					$new_table_cols = trim($matches[1]);
1442
					$old_table_cols = explode(',', $new_table_cols);
1443
					$column_list = array();
1444
1445
					foreach ($old_table_cols as $declaration)
1446
					{
1447
						$entities = preg_split('#\s+#', trim($declaration));
1448
						if ($entities[0] == 'PRIMARY')
1449
						{
1450
							continue;
1451
						}
1452
						$column_list[] = $entities[0];
1453
					}
1454
1455
					$columns = implode(',', $column_list);
1456
1457
					$new_table_cols = $field_ident . ' ' . $type . ',' . $new_table_cols;
1458
1459
					// create a new table and fill it up. destroy the temp one
1460
					$db->sql_query('CREATE TABLE ' . PROFILE_FIELDS_DATA_TABLE . ' (' . $new_table_cols . ');');
1461
					$db->sql_query('INSERT INTO ' . PROFILE_FIELDS_DATA_TABLE . ' (' . $columns . ') SELECT ' . $columns . ' FROM ' . PROFILE_FIELDS_DATA_TABLE . '_temp;');
1462
					$db->sql_query('DROP TABLE ' . PROFILE_FIELDS_DATA_TABLE . '_temp');
1463
				}
1464
				else
1465
				{
1466
					$sql = 'ALTER TABLE ' . PROFILE_FIELDS_DATA_TABLE . " ADD $field_ident [$type]";
1467
				}
1468
1469
			break;
1470
1471
			case 'mssql':
1472
			case 'mssql_odbc':
1473
1474
				// We are defining the biggest common value, because of the possibility to edit the min/max values of each field.
1475
				$sql = 'ALTER TABLE [' . PROFILE_FIELDS_DATA_TABLE . "] ADD [$field_ident] ";
1476
1477
				switch ($field_type)
1478
				{
1479
					case FIELD_STRING:
1480
						$sql .= ' [VARCHAR] (255) ';
1481
					break;
1482
1483
					case FIELD_DATE:
1484
						$sql .= '[VARCHAR] (10) ';
1485
					break;
1486
1487
					case FIELD_TEXT:
1488
						$sql .= "[TEXT]";
1489
		//						ADD {$field_ident}_bbcode_uid [VARCHAR] (5) NOT NULL,
1490
		//						ADD {$field_ident}_bbcode_bitfield [INT] UNSIGNED";
1491
					break;
1492
1493
					case FIELD_BOOL:
1494
					case FIELD_DROPDOWN:
1495
						$sql .= '[INT] ';
1496
					break;
1497
1498
					case FIELD_INT:
1499
						$sql .= '[FLOAT] ';
1500
					break;
1501
				}
1502
1503
			break;
1504
1505
			case 'postgres':
1506
1507
				// We are defining the biggest common value, because of the possibility to edit the min/max values of each field.
1508
				$sql = 'ALTER TABLE ' . PROFILE_FIELDS_DATA_TABLE . " ADD COLUMN \"$field_ident\" ";
1509
1510
				switch ($field_type)
1511
				{
1512
					case FIELD_STRING:
1513
						$sql .= ' VARCHAR(255) ';
1514
					break;
1515
1516
					case FIELD_DATE:
1517
						$sql .= 'VARCHAR(10) ';
1518
					break;
1519
1520
					case FIELD_TEXT:
1521
						$sql .= "TEXT";
1522
		//						ADD {$field_ident}_bbcode_uid VARCHAR(5) NOT NULL,
1523
		//						ADD {$field_ident}_bbcode_bitfield INT4 UNSIGNED";
1524
					break;
1525
1526
					case FIELD_BOOL:
1527
						$sql .= 'INT2 ';
1528
					break;
1529
1530
					case FIELD_DROPDOWN:
1531
						$sql .= 'INT4 ';
1532
					break;
1533
1534
					case FIELD_INT:
1535
						$sql .= 'INT8 ';
1536
					break;
1537
				}
1538
1539
			break;
1540
1541
			case 'firebird':
1542
1543
				// We are defining the biggest common value, because of the possibility to edit the min/max values of each field.
1544
				$sql = 'ALTER TABLE ' . PROFILE_FIELDS_DATA_TABLE . " ADD \"$field_ident\" ";
1545
1546
				switch ($field_type)
1547
				{
1548
					case FIELD_STRING:
1549
						$sql .= ' VARCHAR(255) ';
1550
					break;
1551
1552
					case FIELD_DATE:
1553
						$sql .= 'VARCHAR(10) ';
1554
					break;
1555
1556
					case FIELD_TEXT:
1557
						$sql .= "BLOB SUB_TYPE TEXT";
1558
		//						ADD {$field_ident}_bbcode_uid VARCHAR(5) NOT NULL,
1559
		//						ADD {$field_ident}_bbcode_bitfield INTEGER UNSIGNED";
1560
					break;
1561
1562
					case FIELD_BOOL:
1563
					case FIELD_DROPDOWN:
1564
						$sql .= 'INTEGER ';
1565
					break;
1566
1567
					case FIELD_INT:
1568
						$sql .= 'DOUBLE PRECISION ';
1569
					break;
1570
				}
1571
1572
			break;
1573
1574
			case 'oracle':
1575
1576
				// We are defining the biggest common value, because of the possibility to edit the min/max values of each field.
1577
				$sql = 'ALTER TABLE ' . PROFILE_FIELDS_DATA_TABLE . " ADD $field_ident ";
1578
1579
				switch ($field_type)
1580
				{
1581
					case FIELD_STRING:
1582
						$sql .= ' VARCHAR2(255) ';
1583
					break;
1584
1585
					case FIELD_DATE:
1586
						$sql .= 'VARCHAR2(10) ';
1587
					break;
1588
1589
					case FIELD_TEXT:
1590
						$sql .= "CLOB";
1591
		//						ADD {$field_ident}_bbcode_uid VARCHAR2(5) NOT NULL,
1592
		//						ADD {$field_ident}_bbcode_bitfield NUMBER(11) UNSIGNED";
1593
					break;
1594
1595
					case FIELD_BOOL:
1596
						$sql .= 'NUMBER(2) ';
1597
					break;
1598
1599
					case FIELD_DROPDOWN:
1600
						$sql .= 'NUMBER(8) ';
1601
					break;
1602
1603
					case FIELD_INT:
1604
						$sql .= 'NUMBER(20) ';
1605
					break;
1606
				}
1607
1608
			break;
1609
		}
1610
1611
		return $sql;
1612
	}
1613
}
1614
1615
?>