Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
setup_languages
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 5
72
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 run
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
12
 execute_step
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 get_step_count
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_task_lang_name
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 *
4 * This file is part of the phpBB Forum Software package.
5 *
6 * @copyright (c) phpBB Limited <https://www.phpbb.com>
7 * @license GNU General Public License, version 2 (GPL-2.0)
8 *
9 * For full copyright and license information, please see
10 * the docs/CREDITS.txt file.
11 *
12 */
13
14namespace phpbb\install\module\install_data\task;
15
16use Doctrine\DBAL\Connection;
17use Doctrine\DBAL\Driver\Statement as DriverStatement;
18use Doctrine\DBAL\Exception;
19use Doctrine\DBAL\Statement;
20use phpbb\install\database_task;
21use phpbb\install\helper\config;
22use phpbb\install\helper\container_factory;
23use phpbb\install\helper\database;
24use phpbb\install\helper\iohandler\iohandler_interface;
25use phpbb\install\sequential_task;
26
27class setup_languages extends database_task
28{
29    use sequential_task;
30
31    /**
32     * @var config
33     */
34    protected $config;
35
36    /**
37     * @var Connection
38     */
39    protected $db;
40
41    /**
42     * @var iohandler_interface
43     */
44    protected $iohandler;
45
46    /**
47     * @var DriverStatement|Statement
48     */
49    protected $stmt;
50
51    /**
52     * @var array
53     */
54    protected $installed_languages;
55
56    /**
57     * @var string
58     */
59    protected $profile_fields_table;
60
61    /**
62     * @var string
63     */
64    protected $profile_lang_table;
65
66    /**
67     * Constructor.
68     *
69     * @param config                $config
70     * @param database                $db_helper
71     * @param iohandler_interface    $io
72     * @param container_factory        $container
73     */
74    public function __construct(config $config,
75                                database $db_helper,
76                                iohandler_interface $io,
77                                container_factory $container)
78    {
79        $this->config        = $config;
80        $this->db            = self::get_doctrine_connection($db_helper, $config);
81        $this->iohandler    = $io;
82
83        $this->profile_fields_table    = $container->get_parameter('tables.profile_fields');
84        $this->profile_lang_table    = $container->get_parameter('tables.profile_fields_language');
85
86        parent::__construct($this->db, $io, true);
87    }
88
89    /**
90     * {@inheritdoc}
91     */
92    public function run()
93    {
94        $this->installed_languages = $this->config->get('installed_languages', []);
95        $profile_fields = $this->config->get('profile_field_rows', false);
96        if ($profile_fields === false)
97        {
98            try
99            {
100                $rows = $this->db->fetchAllAssociative('SELECT * FROM ' . $this->profile_fields_table);
101            }
102            catch (Exception $e)
103            {
104                $this->iohandler->add_error_message('INST_ERR_DB', $e->getMessage());
105                $rows = [];
106            }
107
108            $this->config->set('profile_field_rows', $rows);
109            $profile_fields = $rows;
110        }
111
112        $sql = 'INSERT INTO ' . $this->profile_lang_table
113            . ' (field_id, lang_id, lang_name, lang_explain, lang_default_value)'
114            . " VALUES (:field_id, :lang_id, :lang_name, '', '')";
115        $this->stmt = $this->create_prepared_stmt($sql);
116        $this->execute($this->config, $profile_fields);
117    }
118
119    /**
120     * {@inheritdoc}
121     */
122    protected function execute_step($key, $value) : void
123    {
124        foreach ($this->installed_languages as $lang_id)
125        {
126            $this->exec_prepared_stmt($this->stmt, [
127                'field_id'    => $value['field_id'],
128                'lang_id'    => $lang_id,
129
130                // Remove phpbb_ from field name
131                'lang_name' => strtoupper(substr($value['field_name'], 6)),
132            ]);
133        }
134    }
135
136    /**
137     * {@inheritdoc}
138     */
139    public static function get_step_count() : int
140    {
141        return 1;
142    }
143
144    /**
145     * {@inheritdoc}
146     */
147    public function get_task_lang_name() : string
148    {
149        return 'TASK_SET_LANGUAGES';
150    }
151}