PageRenderTime 61ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/forum/includes/acp/acp_profile.php

https://bitbucket.org/itoxable/chiron-gaming
PHP | 1626 lines | 1253 code | 272 blank | 101 comment | 252 complexity | e59267d8255877ec84dce1881c8a7184 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0

Large files files are truncated, but you can click here to view the full file

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

Large files files are truncated, but you can click here to view the full file