Completed
Push — master ( d5fbc8...4c9094 )
by Erwan
03:26
created

main_module::main()   F

Complexity

Conditions 67
Paths > 20000

Size

Total Lines 397
Code Lines 235

Duplication

Lines 12
Ratio 3.02 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 12
loc 397
rs 2
cc 67
eloc 235
nc 1153064
nop 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 *
4
 * @package Quick Title Edition Extension
5
 * @copyright (c) 2015 ABDev
6
 * @copyright (c) 2015 PastisD
7
 * @copyright (c) 2015 Geolim4 <http://geolim4.com>
8
 * @copyright (c) 2015 Zoddo <[email protected]>
9
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
10
 *
11
 */
12
13
namespace ernadoo\qte\acp;
14
15
class main_module
16
{
17
	/** @var string */
18
	public $u_action;
19
20
	/** @var \ernadoo\qte\qte */
21
	protected $qte;
22
23
	public function main($id, $mode)
24
	{
25
		/** @var \phpbb\request\request $request */
26
		/** @var \phpbb\log\log $phpbb_log */
27
		global $phpbb_container, $db, $user, $phpbb_log, $template, $cache, $request, $table_prefix, $phpbb_root_path;
28
		$this->qte = $phpbb_container->get('ernadoo.qte');
29
30
		$ext_root_path = $phpbb_root_path . 'ext/ernadoo/qte/';
31
32
		$action = $request->variable('action', '');
33
		$submit = $request->is_set_post('submit');
34
		$attr_id = $request->variable('id', 0);
35
		$attr_auth_id = $request->variable('attr_auth_id', 0);
36
37
		$error = array();
38
39
		$this->tpl_name = 'acp_attributes';
0 ignored issues
show
Bug introduced by
The property tpl_name does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
40
		$this->page_title = 'QTE_MANAGE_TITLE';
0 ignored issues
show
Bug introduced by
The property page_title does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
41
42
		$user->add_lang_ext('ernadoo/qte', array('attributes', 'attributes_acp'));
43
44
		// Display a warning when a development version is installed or if the database is outdated
45
		$this->display_version_warning();
46
47
		add_form_key('acp_attributes');
48
49
		switch ($action)
50
		{
51
			case 'edit':
52
			case 'add':
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
53
54
				$attr_type = $request->variable('attr_type', 0);
55
				$attr_name = $request->variable('attr_name', '', true);
56
				$attr_img = $request->variable('attr_img', '');
57
				$attr_desc = $request->variable('attr_desc', '', true);
58
				$attr_date = $request->variable('attr_date', '');
59
				$attr_colour = $request->variable('attr_colour', '');
60
				$attr_user_colour = $request->variable('attr_user_colour', 0);
61
62
				// is it too complex for u ? pastisd has no limit :)
63
				$attr_auths = array(array('forums_ids' => array(), 'groups_ids' => array(), 'author' => false));
64
				if ($request->is_set_post('attr_auths'))
65
				{
66
					$attr_auths = $request->variable('attr_auths', array(array('' => array(0))));
67
					foreach ($attr_auths as &$attr_auth)
68
					{
69
						$attr_auth = array(
70
							'forums_ids' => isset($attr_auth['forums_ids']) ? $attr_auth['forums_ids'] : array(),
71
							'groups_ids' => isset($attr_auth['groups_ids']) ? $attr_auth['groups_ids'] : array(),
72
							'author' => isset($attr_auth['author']) ? true : false,
73
						);
74
					}
75
				}
76
77
				if ($submit)
78
				{
79
					if (!check_form_key('acp_attributes'))
80
					{
81
						$error[] = $user->lang['FORM_INVALID'];
82
					}
83
84
					if (empty($attr_name))
85
					{
86
						$error[] = $user->lang['QTE_NAME_ERROR'];
87
					}
88
89
					if (isset($attr_desc[60]))
90
					{
91
						$error[] = $user->lang['QTE_DESC_ERROR'];
92
					}
93
94
					// fully xhtml compatibility : no capital letters
95
					if (!empty($attr_colour))
96
					{
97
						$attr_colour = strtolower($attr_colour);
98
						if (!preg_match('#^([a-f0-9]){6}#i', $attr_colour))
99
						{
100
							$error[] = $user->lang['QTE_COLOUR_ERROR'];
101
						}
102
					}
103
104
					// we don't need user colour when an image is used as attribute
105
					if ($attr_type && $attr_user_colour)
106
					{
107
						$attr_user_colour = false;
108
					}
109
110
					$attr_name_tmp = $this->qte->attr_lng_key($attr_name);
111
					if ($attr_user_colour)
112
					{
113
						if (strpos($attr_name_tmp, '%mod%') === false)
114
						{
115
							$error[] = $user->lang['QTE_USER_COLOUR_ERROR'];
116
						}
117
					}
118
119
					if (!empty($attr_date))
120
					{
121
						if (strpos($attr_name_tmp, '%date%') === false)
122
						{
123
							$error[] = $user->lang['QTE_DATE_ARGUMENT_ERROR'];
124
						}
125
					}
126
					else
127
					{
128
						if (strpos($attr_name_tmp, '%date%') !== false)
129
						{
130
							$error[] = $user->lang['QTE_DATE_FORMAT_ERROR'];
131
						}
132
					}
133
					unset($attr_name_tmp);
134
135
					if (!sizeof($error))
136
					{
137
						$sql_ary = array(
138
							'attr_type' => $attr_type,
139
							'attr_name' => $attr_name,
140
							'attr_img' => $attr_img,
141
							'attr_desc' => $attr_desc,
142
							'attr_date' => $attr_date,
143
							'attr_colour' => $attr_colour,
144
							'attr_user_colour' => $attr_user_colour,
145
							'attr_auths' => sizeof($attr_auths) ? json_encode($attr_auths) : '',
146
						);
147
148
						if ($attr_id)
149
						{
150
							$sql = 'UPDATE ' . $table_prefix . 'topics_attr
151
								SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
152
								WHERE attr_id = ' . (int) $attr_id;
153
							$db->sql_query($sql);
154
155
							$message = 'UPDATED';
156
						}
157
						else
158
						{
159
							$sql = 'SELECT MAX(right_id) AS right_id
160
								FROM ' . $table_prefix . 'topics_attr';
161
							$result = $db->sql_query($sql);
162
							$right_id = (int) $db->sql_fetchfield('right_id');
163
							$db->sql_freeresult($result);
164
165
							$sql_ary['left_id'] = ($right_id + 1);
166
							$sql_ary['right_id'] = ($right_id + 2);
167
168
							$sql = 'INSERT INTO ' . $table_prefix . 'topics_attr ' . $db->sql_build_array('INSERT', $sql_ary);
169
							$db->sql_query($sql);
170
171
							$message = 'ADDED';
172
						}
173
174
						$cache->destroy('_attr');
175
176
						$phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_ATTRIBUTE_' . $message, time(), array($attr_name));
177
178
						trigger_error($user->lang['QTE_' . $message] . adm_back_link($this->u_action));
179
					}
180
				}
181
				else if ($attr_id)
182
				{
183
					$set_permissions = $this->set_auths($attr_id);
184
					$attr = $set_permissions['attr'];
185
					$attr_auths = $set_permissions['attr_auths'];
186
				}
187
188
				if ($action == 'edit')
189
				{
190
					$template->assign_vars(array(
191
						'L_QTE_ADD_EDIT' => $user->lang['QTE_EDIT'],
192
						'L_QTE_ADD_EDIT_EXPLAIN' => $user->lang['QTE_EDIT_EXPLAIN'],
193
					));
194
				}
195
				else
196
				{
197
					$template->assign_vars(array(
198
						'L_QTE_ADD_EDIT' => $user->lang['QTE_ADD'],
199
						'L_QTE_ADD_EDIT_EXPLAIN' => $user->lang['QTE_ADD_EXPLAIN'],
200
					));
201
202
					$attr_auths = array(array(
203
						'forums_ids' => array(),
204
						'groups_ids' => array(),
205
						'author' => false,
206
					));
207
				}
208
209
				$this->qte_attr_select($attr_id);
210
				$this->add_auths($attr_auths);
211
212
				if (sizeof($error))
213
				{
214
					$template->assign_vars(array(
215
						'S_ERROR' => true,
216
						'ERROR_MSG' => implode('<br />', $error),
217
					));
218
				}
219
220
				$attr_type_state = ((isset($attr['attr_type']) && $attr['attr_type']) || (isset($attr_type) && $attr_type));
0 ignored issues
show
Bug introduced by
The variable $attr does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
221
				$attr_user_colour_state = ((isset($attr['attr_user_colour']) && $attr['attr_user_colour']) || (isset($attr_user_colour) && $attr_user_colour));
222
223
				$template->assign_vars(array(
224
					'S_EDIT' => true,
225
226
					'U_ACTION' => $this->u_action . '&amp;action=' . (($action == 'add') ? 'add' : 'edit&amp;id=' . (int) $attr_id),
227
					'U_BACK' => $this->u_action,
228
					'U_AJAX' => str_replace('&amp;', '&', $this->u_action),
229
230
					'L_QTE_NAME_EXPLAIN' => $user->lang('QTE_NAME_EXPLAIN', $user->data['username']),
231
232
					'ATTR_ID' => isset($attr['attr_id']) ? $attr['attr_id'] : $attr_id,
233
					'ATTR_NAME' => isset($attr['attr_name']) ? $attr['attr_name'] : $attr_name,
234
					'ATTR_IMG' => isset($attr['attr_img']) ? $attr['attr_img'] : $attr_img,
235
					'ATTR_DESC' => isset($attr['attr_desc']) ? $attr['attr_desc'] : $attr_desc,
236
					'ATTR_DATE' => isset($attr['attr_date']) ? $attr['attr_date'] : $attr_date,
237
					'ATTR_COLOUR' => isset($attr['attr_colour']) ? $attr['attr_colour'] : $attr_colour,
238
239
					'S_TEXT' => $attr_type_state ? true : false,
240
					'S_USER_COLOUR' => $attr_user_colour_state ? true : false,
241
242
					'ICON_ATTR_AUTH_ADD' => '<img src="' . $ext_root_path . 'adm/images/qte_auth_add.gif" alt="' . $user->lang['QTE_AUTH_ADD'] . '" title="' . $user->lang['QTE_AUTH_ADD'] . '" />',
243
					'ICON_ATTR_AUTH_REMOVE' => '<img src="' . $ext_root_path . 'adm/images/qte_auth_remove.gif" alt="' . $user->lang['QTE_AUTH_REMOVE'] . '" title="' . $user->lang['QTE_AUTH_REMOVE'] . '" />',
244
				));
245
246
				return;
247
248
			break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
249
250
			case 'set_permissions':
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
251
252
				$set_permissions = $this->set_auths($attr_auth_id);
253
				$this->add_auths($set_permissions['attr_auths']);
254
255
				$template->assign_vars(array(
256
					'ICON_ATTR_AUTH_ADD' => '<img src="' . $ext_root_path . 'adm/images/qte_auth_add.gif" alt="' . $user->lang['QTE_AUTH_ADD'] . '" title="' . $user->lang['QTE_AUTH_ADD'] . '" />',
257
					'ICON_ATTR_AUTH_REMOVE' => '<img src="' . $ext_root_path . 'adm/images/qte_auth_remove.gif" alt="' . $user->lang['QTE_AUTH_REMOVE'] . '" title="' . $user->lang['QTE_AUTH_REMOVE'] . '" />',
258
				));
259
				$this->tpl_name = 'acp_attributes_auths';
260
261
			break;
262
263
			case 'delete':
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
264
265 View Code Duplication
				if (!$attr_id)
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
266
				{
267
					trigger_error($user->lang['QTE_MUST_SELECT'] . adm_back_link($this->u_action), E_USER_WARNING);
268
				}
269
270
				if (confirm_box(true))
271
				{
272
					$sql = 'SELECT topic_id, topic_attr_id
273
						FROM ' . TOPICS_TABLE . '
274
						WHERE topic_attr_id = ' . (int) $attr_id;
275
					$result = $db->sql_query($sql);
276
277
					$topic_id_ary = array();
278
					while ($row = $db->sql_fetchrow($result))
279
					{
280
						$topic_id_ary[] = (int) $row['topic_id'];
281
					}
282
					$db->sql_freeresult($result);
283
284
					if (sizeof($topic_id_ary))
285
					{
286
						$fields = array('topic_attr_id' => 0, 'topic_attr_user' => 0, 'topic_attr_time' => 0);
287
288
						$sql = 'UPDATE ' . TOPICS_TABLE . '
289
							SET ' . $db->sql_build_array('UPDATE', $fields) . '
290
							WHERE ' . $db->sql_in_set('topic_id', array_map('intval', $topic_id_ary));
291
						$db->sql_query($sql);
292
					}
293
294
					$sql = 'SELECT attr_name
295
						FROM ' . $table_prefix . 'topics_attr
296
						WHERE attr_id = ' . (int) $attr_id;
297
					$result = $db->sql_query($sql);
298
					$attr_name = (string) $db->sql_fetchfield('attr_name');
299
					$db->sql_freeresult($result);
300
301
					$phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_ATTRIBUTE_REMOVED', time(), array($attr_name));
302
303
					$sql = 'DELETE FROM ' . $table_prefix . 'topics_attr
304
						WHERE attr_id = ' . (int) $attr_id;
305
					$db->sql_query($sql);
306
307
					$cache->destroy('_attr');
308
309
					if ($request->is_ajax())
310
					{
311
						$json_response = new \phpbb\json_response;
312
						$json_response->send(array(
313
							'success' => 'true',
314
							'MESSAGE_TITLE' => $user->lang['INFORMATION'],
315
							'MESSAGE_TEXT' => $user->lang['QTE_REMOVED'],
316
							'REFRESH_DATA' => array(
317
								'time'	=> 3,
318
							)
319
						));
320
					}
321
					else
322
					{
323
						trigger_error($user->lang['QTE_REMOVED'] . adm_back_link($this->u_action));
324
					}
325
				}
326
				else
327
				{
328
					confirm_box(false, $user->lang['CONFIRM_OPERATION'], build_hidden_fields(array(
329
						'i' => $id,
330
						'mode' => $mode,
331
						'attr_id' => $attr_id,
332
						'action' => 'delete',
333
					)));
334
				}
335
336
			break;
337
338
			case 'move_up':
339
			case 'move_down':
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
340
341 View Code Duplication
				if (!$attr_id)
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
342
				{
343
					trigger_error($user->lang['QTE_MUST_SELECT'] . adm_back_link($this->u_action), E_USER_WARNING);
344
				}
345
346
				$sql = 'SELECT *
347
					FROM ' . $table_prefix . 'topics_attr
348
					WHERE attr_id = ' . (int) $attr_id;
349
				$result = $db->sql_query($sql);
350
				$row = $db->sql_fetchrow($result);
351
				$db->sql_freeresult($result);
352
353 View Code Duplication
				if (!$row)
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
354
				{
355
					trigger_error($user->lang['QTE_MUST_SELECT'] . adm_back_link($this->u_action), E_USER_WARNING);
356
				}
357
358
				$move_attr_name = $this->qte_move($row, $action, 1);
359
				if ($move_attr_name !== false)
360
				{
361
					$phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_ATTRIBUTE_' . strtoupper($action), time(), array($move_attr_name));
362
				}
363
364
				if ($request->is_ajax())
365
				{
366
					$json_response = new \phpbb\json_response;
367
					$json_response->send(array('success' => true));
368
				}
369
370
			break;
371
		}
372
373
		$template->assign_vars(array('U_ACTION' => $this->u_action));
374
375
		$sql = 'SELECT topic_attr_id, COUNT(topic_id) AS total_topics
376
			FROM ' . TOPICS_TABLE . '
377
			GROUP BY topic_attr_id';
378
		$result = $db->sql_query($sql);
379
		$stats = array();
380
		$total_topics = 0;
381
		while ($row = $db->sql_fetchrow($result))
382
		{
383
			$stats[$row['topic_attr_id']] = $row['total_topics'];
384
			$total_topics += $row['total_topics'];
385
		}
386
		$db->sql_freeresult($result);
387
388
		$sql = 'SELECT * FROM ' . $table_prefix . 'topics_attr ORDER BY left_id';
389
		$result = $db->sql_query($sql);
390
391
		while ($row = $db->sql_fetchrow($result))
392
		{
393
			$attribute_name = str_replace(array('%mod%', '%date%'), array($user->lang['QTE_KEY_USERNAME'], $user->lang['QTE_KEY_DATE']), $this->qte->attr_lng_key($row['attr_name']));
394
			$attribute_count = isset($stats[$row['attr_id']]) ? $stats[$row['attr_id']] : 0;
395
396
			$template->assign_block_vars('row', array(
397
				'S_IMAGE' => $row['attr_type'] ? true : false,
398
				'S_COLOUR' => $row['attr_colour'] ? true : false,
399
				'S_DESC' => $row['attr_desc'] ? true : false,
400
				'S_DATE' => $row['attr_date'] ? true : false,
401
				'S_USER_COLOUR' => $row['attr_user_colour'] ? true : false,
402
				'S_CSS' => (!$row['attr_type'] && isset($user->lang[$row['attr_name']]) && empty($row['attr_colour'])) ? true : false,
403
404
				'QTE_TXT' => $attribute_name,
405
				'QTE_DESC' => $this->qte->attr_lng_key($row['attr_desc']),
406
				'QTE_IMG' => $this->qte->attr_img_key($row['attr_img'], $attribute_name),
407
				'QTE_COLOUR' => $row['attr_colour'],
408
				'QTE_DATE' => $row['attr_date'],
409
				'QTE_COUNT' => (int) $attribute_count,
410
				'QTE_PER_CENT' => empty($total_topics) ? 0 : round(intval($attribute_count) * 100 / $total_topics),
411
412
				'U_EDIT' => $this->u_action . '&amp;action=edit&amp;id=' . $row['attr_id'],
413
				'U_MOVE_UP' => $this->u_action . '&amp;action=move_up&amp;id=' . $row['attr_id'],
414
				'U_MOVE_DOWN' => $this->u_action . '&amp;action=move_down&amp;id=' . $row['attr_id'],
415
				'U_DELETE' => $this->u_action . '&amp;action=delete&amp;id=' . $row['attr_id'],
416
			));
417
		}
418
		$db->sql_freeresult($result);
419
	}
420
421
	protected function set_auths($attr_id)
422
	{
423
		global $db, $table_prefix;
424
425
		$sql = 'SELECT * FROM ' . $table_prefix . 'topics_attr WHERE attr_id = ' . (int) $attr_id;
426
		$result = $db->sql_query($sql);
427
		$attr = $db->sql_fetchrow($result);
428
		$db->sql_freeresult($result);
429
430
		if (!$attr['attr_auths'])
431
		{
432
			$attr_auths = array(array(
433
				'forums_ids' => array(),
434
				'groups_ids' => array(),
435
				'author' => false,
436
			));
437
		}
438
		else
439
		{
440
			$attr_auths = json_decode($attr['attr_auths'], true);
441
		}
442
443
		return array('attr' => $attr, 'attr_auths' => $attr_auths);
444
	}
445
446
	protected function add_auths($attr_auths)
447
	{
448
		global $template;
449
450
		$offset = 0;
451
		foreach ($attr_auths as $attr_auth)
452
		{
453
			$template->assign_block_vars('auths_row', array(
454
				'OFFSET' => $offset,
455
456
				'S_FORUM_ID_OPTIONS' => $this->qte_forum_select($attr_auth['forums_ids']),
457
				'S_GROUP_ID_OPTIONS' => $this->qte->qte_group_select($attr_auth['groups_ids'], false, false),
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
458
459
				'S_AUTHOR' => $attr_auth['author'],
460
			));
461
			$offset++;
462
		}
463
	}
464
465
	protected function qte_move($attr_row, $action = 'move_up', $steps = 1)
466
	{
467
		global $db, $table_prefix;
468
469
		$sql = 'SELECT attr_id, attr_name, left_id, right_id
470
			FROM ' . $table_prefix . "topics_attr
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal topics_attr\n WHERE does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
471
			WHERE " . (($action == 'move_up') ? "right_id < {$attr_row['right_id']} ORDER BY right_id DESC" : "left_id > {$attr_row['left_id']} ORDER BY left_id ASC");
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $attr_row instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
472
		$result = $db->sql_query_limit($sql, $steps);
473
474
		$target = array();
475
		while ($row = $db->sql_fetchrow($result))
476
		{
477
			$target = $row;
478
		}
479
		$db->sql_freeresult($result);
480
481
		if (!sizeof($target))
482
		{
483
			return false;
484
		}
485
486
		if ($action == 'move_up')
487
		{
488
			$left_id = $target['left_id'];
489
			$right_id = $attr_row['right_id'];
490
491
			$diff_up = $attr_row['left_id'] - $target['left_id'];
492
			$diff_down = $attr_row['right_id'] + 1 - $attr_row['left_id'];
493
494
			$move_up_left = $attr_row['left_id'];
495
			$move_up_right = $attr_row['right_id'];
496
		}
497
		else
498
		{
499
			$left_id = $attr_row['left_id'];
500
			$right_id = $target['right_id'];
501
502
			$diff_up = $attr_row['right_id'] + 1 - $attr_row['left_id'];
503
			$diff_down = $target['right_id'] - $attr_row['right_id'];
504
505
			$move_up_left = $attr_row['right_id'] + 1;
506
			$move_up_right = $target['right_id'];
507
		}
508
509
		$sql = 'UPDATE ' . $table_prefix . "topics_attr
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $move_up_left instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $move_up_right instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $diff_up instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $diff_down instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $left_id instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $right_id instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
510
			SET left_id = left_id + CASE
511
				WHEN left_id BETWEEN {$move_up_left} AND {$move_up_right} THEN -{$diff_up}
512
				ELSE {$diff_down}
513
			END,
514
			right_id = right_id + CASE
515
				WHEN right_id BETWEEN {$move_up_left} AND {$move_up_right} THEN -{$diff_up}
516
				ELSE {$diff_down}
517
			END
518
			WHERE left_id BETWEEN {$left_id} AND {$right_id}
519
				AND right_id BETWEEN {$left_id} AND {$right_id}";
520
		$db->sql_query($sql);
521
522
		return $target['attr_name'];
523
	}
524
525
	// borrowed from "includes/acp/acp_attachments.php" file
526
	protected function qte_forum_select($forum_ids)
527
	{
528
		global $db;
529
530
		$s_forum_id_options = '';
531
532
		$sql = 'SELECT forum_id, forum_name, parent_id, forum_type, left_id, right_id
533
			FROM ' . FORUMS_TABLE . '
534
			ORDER BY left_id ASC';
535
		$result = $db->sql_query($sql, 600);
536
537
		$right = $cat_right = $padding_inc = 0;
0 ignored issues
show
Unused Code introduced by
$padding_inc is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
538
		$padding = $forum_list = $holding = '';
0 ignored issues
show
Unused Code introduced by
$forum_list is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
539
		$padding_store = array('0' => '');
540
541
		while ($row = $db->sql_fetchrow($result))
542
		{
543
			if (($row['forum_type'] == FORUM_CAT) && ($row['left_id'] + 1 == $row['right_id']))
544
			{
545
				continue;
546
			}
547
548
			if ($row['left_id'] < $right)
549
			{
550
				$padding .= '&nbsp; &nbsp;';
551
				$padding_store[$row['parent_id']] = $padding;
552
			}
553
			else if ($row['left_id'] > $right + 1)
554
			{
555
				$padding = empty($padding_store[$row['parent_id']]) ? '' : $padding_store[$row['parent_id']];
556
			}
557
558
			$right = $row['right_id'];
559
560
			$selected = in_array($row['forum_id'], $forum_ids) ? ' selected="selected"' : '';
561
562
			if ($row['left_id'] > $cat_right)
563
			{
564
				$s_forum_id_options .= $holding;
565
				$holding = '';
566
			}
567
568
			if ($row['right_id'] - $row['left_id'] > 1)
569
			{
570
				$cat_right = max($cat_right, $row['right_id']);
571
572
				$holding .= '<option value="' . $row['forum_id'] . '"' . (($row['forum_type'] == FORUM_POST) ? ' class="sep"' : ' disabled="disabled"') . $selected . '>' . $padding . $row['forum_name'] . '</option>';
573
			}
574
			else
575
			{
576
				$s_forum_id_options .= $holding . '<option value="' . $row['forum_id'] . '"' . (($row['forum_type'] == FORUM_POST) ? ' class="sep"' : ' disabled="disabled"') . $selected . '>' . $padding . $row['forum_name'] . '</option>';
577
				$holding = '';
578
			}
579
		}
580
581
		if ($holding)
582
		{
583
			$s_forum_id_options .= $holding;
584
		}
585
586
		$db->sql_freeresult($result);
587
588
		return $s_forum_id_options;
589
	}
590
591
	protected function qte_attr_select($attr_id)
592
	{
593
		global $user, $template;
594
595
		$current_time = time();
596
597
		foreach ($this->qte->getAttr() as $attr)
598
		{
599
			if ($attr['attr_id'] != $attr_id)
600
			{
601
				$attribute_name = str_replace(array('%mod%', '%date%'), array($user->data['username'], $user->format_date($current_time, $attr['attr_date'])), $this->qte->attr_lng_key($attr['attr_name']));
602
603
				$template->assign_block_vars('select_row', array(
604
					'QTE_ID' => $attr['attr_id'],
605
					'QTE_TYPE' => $attr['attr_type'],
606
					'QTE_NAME' => $attribute_name,
607
					'QTE_DESC' => $this->qte->attr_lng_key($attr['attr_desc']),
608
					'QTE_COLOUR' => $this->qte->attr_colour($attr['attr_name'], $attr['attr_colour']),
609
				));
610
			}
611
		}
612
	}
613
614
	protected function display_version_warning()
615
	{
616
		global $config, $user, $template;
617
618
		$version = \ernadoo\qte\ext::VERSION;
619
620
		// Check if the database is up-to-date (we don't display warning if we are on a -dev version since versions doesn't matches)
621
		if ($config['qte_version'] != $version && stripos($version, '-dev') === false)
622
		{
623
			trigger_error($user->lang('QTE_MIGRATIONS_OUTDATED', $config['qte_version'], $version), E_USER_ERROR);
624
		}
625
626
		// Display a warning for unstable versions
627
		if (stripos($version, '-dev') !== false)
628
		{
629
			$template->assign_vars(array(
630
				'S_VERSION_UNSTABLE' => true,
631
				'S_VERSION_DEV' => true,
632
				'VERSION_WARNING' => $user->lang('QTE_DEV_WARNING', $version) . '<br />' . $user->lang['QTE_DEV_WARNING_DEV'],
633
			));
634
		}
635
		else if (stripos($version, '-a') !== false)
636
		{
637
			$template->assign_vars(array(
638
				'S_VERSION_UNSTABLE' => true,
639
				'S_VERSION_DEV' => true,
640
				'VERSION_WARNING' => $user->lang('QTE_DEV_WARNING', $version),
641
			));
642
		}
643
		else if (stripos($version, '-b') !== false)
644
		{
645
			$template->assign_vars(array(
646
				'S_VERSION_UNSTABLE' => true,
647
				'VERSION_WARNING' => $user->lang('QTE_BETA_WARNING', $version),
648
			));
649
		}
650
	}
651
}
652