From d601aaad263e19b47865bca56da527f4fdf504e1 Mon Sep 17 00:00:00 2001
From: Joas Schilling <nickvergessen@gmx.de>
Date: Tue, 14 Jan 2014 13:38:24 +0100
Subject: [ticket/11201] Remove type related code from build_insert_sql_array()

PHPBB3-11201
---
 phpBB/phpbb/profilefields/profilefields.php           | 14 ++------------
 phpBB/phpbb/profilefields/type/type_bool.php          | 10 +++++++++-
 phpBB/phpbb/profilefields/type/type_date.php          | 16 +++++++++++++++-
 phpBB/phpbb/profilefields/type/type_dropdown.php      | 10 +++++++++-
 phpBB/phpbb/profilefields/type/type_int.php           | 16 +++++++++++++++-
 phpBB/phpbb/profilefields/type/type_interface.php     | 12 ++++++++++--
 phpBB/phpbb/profilefields/type/type_string.php        |  2 +-
 phpBB/phpbb/profilefields/type/type_string_common.php |  8 ++++++++
 phpBB/phpbb/profilefields/type/type_text.php          |  2 +-
 9 files changed, 70 insertions(+), 20 deletions(-)

(limited to 'phpBB/phpbb/profilefields')

diff --git a/phpBB/phpbb/profilefields/profilefields.php b/phpBB/phpbb/profilefields/profilefields.php
index ba0baa53f0..e3201712df 100644
--- a/phpBB/phpbb/profilefields/profilefields.php
+++ b/phpBB/phpbb/profilefields/profilefields.php
@@ -408,18 +408,8 @@ class profilefields
 
 		while ($row = $this->db->sql_fetchrow($result))
 		{
-			if ($row['field_default_value'] == 'now' && $row['field_type'] == FIELD_DATE)
-			{
-				$now = getdate();
-				$row['field_default_value'] = sprintf('%2d-%2d-%4d', $now['mday'], $now['mon'], $now['year']);
-			}
-			else if ($row['field_default_value'] === '' && $row['field_type'] == FIELD_INT)
-			{
-				// We cannot insert an empty string into an integer column.
-				$row['field_default_value'] = NULL;
-			}
-
-			$cp_data['pf_' . $row['field_ident']] = (in_array($row['field_type'], array(FIELD_TEXT, FIELD_STRING))) ? $row['lang_default_value'] : $row['field_default_value'];
+			$profile_field = $this->container->get('profilefields.type.' . $this->profile_types[$row['field_type']]);
+			$cp_data['pf_' . $row['field_ident']] = $profile_field->get_default_field_value($row);
 		}
 		$this->db->sql_freeresult($result);
 
diff --git a/phpBB/phpbb/profilefields/type/type_bool.php b/phpBB/phpbb/profilefields/type/type_bool.php
index ee75578a16..e60806becd 100644
--- a/phpBB/phpbb/profilefields/type/type_bool.php
+++ b/phpBB/phpbb/profilefields/type/type_bool.php
@@ -51,7 +51,7 @@ class type_bool implements type_interface
 	/**
 	* {@inheritDoc}
 	*/
-	public function get_default_values()
+	public function get_default_option_values()
 	{
 		return array(
 			'field_length'		=> 1,
@@ -63,6 +63,14 @@ class type_bool implements type_interface
 		);
 	}
 
+	/**
+	* {@inheritDoc}
+	*/
+	public function get_default_field_value($field_data)
+	{
+		return $field_data['field_default_value'];
+	}
+
 	/**
 	* {@inheritDoc}
 	*/
diff --git a/phpBB/phpbb/profilefields/type/type_date.php b/phpBB/phpbb/profilefields/type/type_date.php
index 36e82dabce..7aabafcb14 100644
--- a/phpBB/phpbb/profilefields/type/type_date.php
+++ b/phpBB/phpbb/profilefields/type/type_date.php
@@ -59,7 +59,7 @@ class type_date implements type_interface
 	/**
 	* {@inheritDoc}
 	*/
-	public function get_default_values()
+	public function get_default_option_values()
 	{
 		return array(
 			'field_length'		=> 10,
@@ -71,6 +71,20 @@ class type_date implements type_interface
 		);
 	}
 
+	/**
+	* {@inheritDoc}
+	*/
+	public function get_default_field_value($field_data)
+	{
+		if ($field_data['field_default_value'] == 'now')
+		{
+			$now = getdate();
+			$field_data['field_default_value'] = sprintf('%2d-%2d-%4d', $now['mday'], $now['mon'], $now['year']);
+		}
+
+		return $field_data['field_default_value'];
+	}
+
 	/**
 	* {@inheritDoc}
 	*/
diff --git a/phpBB/phpbb/profilefields/type/type_dropdown.php b/phpBB/phpbb/profilefields/type/type_dropdown.php
index 61fa8d7585..c461815426 100644
--- a/phpBB/phpbb/profilefields/type/type_dropdown.php
+++ b/phpBB/phpbb/profilefields/type/type_dropdown.php
@@ -55,7 +55,7 @@ class type_dropdown implements type_interface
 	/**
 	* {@inheritDoc}
 	*/
-	public function get_default_values()
+	public function get_default_option_values()
 	{
 		return array(
 			'field_length'		=> 0,
@@ -67,6 +67,14 @@ class type_dropdown implements type_interface
 		);
 	}
 
+	/**
+	* {@inheritDoc}
+	*/
+	public function get_default_field_value($field_data)
+	{
+		return $field_data['field_default_value'];
+	}
+
 	/**
 	* {@inheritDoc}
 	*/
diff --git a/phpBB/phpbb/profilefields/type/type_int.php b/phpBB/phpbb/profilefields/type/type_int.php
index 7285b0f4ba..f650436d21 100644
--- a/phpBB/phpbb/profilefields/type/type_int.php
+++ b/phpBB/phpbb/profilefields/type/type_int.php
@@ -39,7 +39,7 @@ class type_int implements type_interface
 	/**
 	* {@inheritDoc}
 	*/
-	public function get_default_values()
+	public function get_default_option_values()
 	{
 		return array(
 			'field_length'		=> 5,
@@ -51,6 +51,20 @@ class type_int implements type_interface
 		);
 	}
 
+	/**
+	* {@inheritDoc}
+	*/
+	public function get_default_field_value($field_data)
+	{
+		 if ($field_data['field_default_value'] === '')
+		{
+			// We cannot insert an empty string into an integer column.
+			return null;
+		}
+
+		return $field_data['field_default_value'];
+	}
+
 	/**
 	* {@inheritDoc}
 	*/
diff --git a/phpBB/phpbb/profilefields/type/type_interface.php b/phpBB/phpbb/profilefields/type/type_interface.php
index 92925c3023..d53c038d2d 100644
--- a/phpBB/phpbb/profilefields/type/type_interface.php
+++ b/phpBB/phpbb/profilefields/type/type_interface.php
@@ -31,11 +31,19 @@ interface type_interface
 	public function get_options($default_lang_id, $field_data);
 
 	/**
-	* Get default values for this type
+	* Get default values for the options of this type
 	*
 	* @return array with values like default field size and more
 	*/
-	public function get_default_values();
+	public function get_default_option_values();
+
+	/**
+	* Get default value for this type
+	*
+	* @param array	$field_data			Array with data for this field
+	* @return mixed default value for new users when no value is given
+	*/
+	public function get_default_field_value($field_data);
 
 	/**
 	* Get profile field value on submit
diff --git a/phpBB/phpbb/profilefields/type/type_string.php b/phpBB/phpbb/profilefields/type/type_string.php
index c9929b1110..090b5a2a54 100644
--- a/phpBB/phpbb/profilefields/type/type_string.php
+++ b/phpBB/phpbb/profilefields/type/type_string.php
@@ -39,7 +39,7 @@ class type_string extends type_string_common implements type_interface
 	/**
 	* {@inheritDoc}
 	*/
-	public function get_default_values()
+	public function get_default_option_values()
 	{
 		return array(
 			'field_length'		=> 10,
diff --git a/phpBB/phpbb/profilefields/type/type_string_common.php b/phpBB/phpbb/profilefields/type/type_string_common.php
index 46e44fa85a..02b640bb44 100644
--- a/phpBB/phpbb/profilefields/type/type_string_common.php
+++ b/phpBB/phpbb/profilefields/type/type_string_common.php
@@ -28,6 +28,14 @@ abstract class type_string_common
 		return $validate_options;
 	}
 
+	/**
+	* {@inheritDoc}
+	*/
+	public function get_default_field_value($field_data)
+	{
+		return $field_data['lang_default_value'];
+	}
+
 	/**
 	* Validate entered profile field data
 	*
diff --git a/phpBB/phpbb/profilefields/type/type_text.php b/phpBB/phpbb/profilefields/type/type_text.php
index 476e1d204f..e73ffc5375 100644
--- a/phpBB/phpbb/profilefields/type/type_text.php
+++ b/phpBB/phpbb/profilefields/type/type_text.php
@@ -39,7 +39,7 @@ class type_text extends type_string_common implements type_interface
 	/**
 	* {@inheritDoc}
 	*/
-	public function get_default_values()
+	public function get_default_option_values()
 	{
 		return array(
 			'field_length'		=> '5|80',
-- 
cgit v1.2.1