aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/phpbb/install/module/install_data/task/add_languages.php
blob: 7ffdf4f276213b4d9413689927d0600a733481fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
/**
 *
 * This file is part of the phpBB Forum Software package.
 *
 * @copyright (c) phpBB Limited <https://www.phpbb.com>
 * @license GNU General Public License, version 2 (GPL-2.0)
 *
 * For full copyright and license information, please see
 * the docs/CREDITS.txt file.
 *
 */

namespace phpbb\install\module\install_data\task;

class add_languages extends \phpbb\install\task_base
{
	/**
	 * @var \phpbb\db\driver\driver_interface
	 */
	protected $db;

	/**
	 * @var \phpbb\install\helper\iohandler\iohandler_interface
	 */
	protected $iohandler;

	/**
	 * @var \phpbb\language\language_file_helper
	 */
	protected $language_helper;

	/**
	 * Constructor
	 *
	 * @param \phpbb\install\helper\iohandler\iohandler_interface	$iohandler			Installer's input-output handler
	 * @param \phpbb\install\helper\container_factory				$container			Installer's DI container
	 * @param \phpbb\language\language_file_helper					$language_helper	Language file helper service
	 */
	public function __construct(\phpbb\install\helper\iohandler\iohandler_interface $iohandler,
								\phpbb\install\helper\container_factory $container,
								\phpbb\language\language_file_helper $language_helper)
	{
		$this->db				= $container->get('dbal.conn');
		$this->iohandler		= $iohandler;
		$this->language_helper	= $language_helper;

		parent::__construct(true);
	}

	/**
	 * {@inheritdoc}
	 */
	public function run()
	{
		$this->db->sql_return_on_error(true);

		$languages = $this->language_helper->get_available_languages();
		$installed_languages = array();

		foreach ($languages as $lang_info)
		{
			$lang_pack = array(
				'lang_iso'			=> $lang_info['iso'],
				'lang_dir'			=> $lang_info['iso'],
				'lang_english_name'	=> htmlspecialchars($lang_info['name']),
				'lang_local_name'	=> htmlspecialchars($lang_info['local_name'], ENT_COMPAT, 'UTF-8'),
				'lang_author'		=> htmlspecialchars($lang_info['author'], ENT_COMPAT, 'UTF-8'),
			);

			$this->db->sql_query('INSERT INTO ' . LANG_TABLE . ' ' . $this->db->sql_build_array('INSERT', $lang_pack));

			$installed_languages[] = (int) $this->db->sql_nextid();
			if ($this->db->get_sql_error_triggered())
			{
				$error = $this->db->sql_error($this->db->get_sql_error_sql());
				$this->iohandler->add_error_message($error['message']);
			}
		}

		$sql = 'SELECT * FROM ' . PROFILE_FIELDS_TABLE;
		$result = $this->db->sql_query($sql);

		$insert_buffer = new \phpbb\db\sql_insert_buffer($this->db, PROFILE_LANG_TABLE);
		while ($row = $this->db->sql_fetchrow($result))
		{
			foreach ($installed_languages as $lang_id)
			{
				$insert_buffer->insert(array(
					'field_id'				=> $row['field_id'],
					'lang_id'				=> $lang_id,

					// Remove phpbb_ from field name
					'lang_name'				=> strtoupper(substr($row['field_name'], 6)),
					'lang_explain'			=> '',
					'lang_default_value'	=> '',
				));
			}
		}

		$this->db->sql_freeresult($result);

		$insert_buffer->flush();
	}

	/**
	 * {@inheritdoc}
	 */
	static public function get_step_count()
	{
		return 1;
	}

	/**
	 * {@inheritdoc}
	 */
	public function get_task_lang_name()
	{
		return 'TASK_ADD_LANGUAGES';
	}
}