Completed
Push — master ( b9932d...5e979e )
by Erwan
10s
created

convert_old_permissions::convert_permissions()   D

Complexity

Conditions 13
Paths 384

Size

Total Lines 73
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 73
rs 4.0766
cc 13
eloc 35
nc 384
nop 0

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\migrations\v200;
14
15
use phpbb\db\migration\container_aware_migration;
16
17
class convert_old_permissions extends container_aware_migration
18
{
19
	static public function depends_on()
20
	{
21
		return array(
22
			'\ernadoo\qte\migrations\v200\alpha1',
23
		);
24
	}
25
26
	public function update_data()
27
	{
28
		return array(
29
			array('permission.add', array('m_qte_attr_del', false)),
30
			array('permission.add', array('m_qte_attr_edit', false)),
31
32
			array('custom', array(array(&$this, 'convert_permissions'))),
33
		);
34
	}
35
36
	public function revert_data()
37
	{
38
		return array(
39
40
			array('custom', array(array(&$this, 'remove_permissions'))),
41
		);
42
	}
43
44
	public function convert_permissions()
45
	{
46
		$attr_permissions_array = $groups_array = array();
47
48
		$migrator_tool_permission = $this->container->get('migrator.tool.permission');
49
50
		$sql = 'SELECT * FROM ' . $this->table_prefix . 'topics_attr';
51
		$result = $this->db->sql_query($sql);
52
53
		while ($row = $this->db->sql_fetchrow($result))
54
		{
55
			$auth_option = 'f_qte_attr_'.$row['attr_id'];
56
			$migrator_tool_permission->add($auth_option, false);
57
			$attr_permissions_array[$auth_option] = json_decode($row['attr_auths'], true);
58
		}
59
60
		if (!class_exists('auth_admin'))
61
		{
62
			include($this->phpbb_root_path . 'includes/acp/auth.' . $this->php_ext);
63
		}
64
		$auth_admin = new \auth_admin();
65
66
		$hold_ary = array();
67
68
		foreach ($attr_permissions_array as $auth_option => $attr_permissions)
69
		{
70
			foreach ($attr_permissions as $attr_permission)
71
			{
72
				foreach ($attr_permission['forums_ids'] as $forum_id)
73
				{
74
					foreach ($attr_permission['groups_ids'] as $group_id)
75
					{
76
						if (!isset($hold_ary[$group_id][$forum_id]))
77
						{
78
							$hold_ary = $auth_admin->get_mask('set', false, $group_id, $forum_id, 'f_', 'local', ACL_NO);
79
						}
80
81
						$hold_ary[$group_id][$forum_id][$auth_option] = ACL_YES;
82
83
						$auth_admin->acl_set('group', $forum_id, $group_id, $hold_ary[$group_id][$forum_id]);
84
					}
85
				}
86
			}
87
		}
88
89
		$sql = 'SELECT group_id FROM ' . GROUPS_TABLE .
90
			(!$this->config['coppa_enable'] ? " WHERE group_name <> 'REGISTERED_COPPA'" : '');
91
		$result = $this->db->sql_query($sql);
92
93
		while ($row = $this->db->sql_fetchrow($result))
94
		{
95
			$groups_array[] = $row['group_id'];
96
		}
97
98
		$sql = 'SELECT forum_id, hide_attr FROM ' . FORUMS_TABLE . '
99
			WHERE hide_attr <> ""';
100
		$result = $this->db->sql_query($sql);
101
102
		while ($row = $this->db->sql_fetchrow($result))
103
		{
104
			$result_diff = array_diff($groups_array, unserialize($row['hide_attr']));
105
106
			if (sizeof($result_diff))
107
			{
108
				foreach ($result_diff as $group_id)
109
				{
110
					$hold_ary = $auth_admin->get_mask('set', false, $group_id, $row['forum_id'], 'm_', 'local', ACL_NO);
111
					$hold_ary[$group_id][$row['forum_id']]['m_qte_attr_del'] = ACL_YES;
112
					$auth_admin->acl_set('group', $row['forum_id'], $group_id, $hold_ary[$group_id][$row['forum_id']]);
113
				}
114
			}
115
		}
116
	}
117
118
	public function remove_permissions()
119
	{
120
		$migrator_tool_permission = $this->container->get('migrator.tool.permission');
121
122
		$sql = 'SELECT auth_option FROM ' . ACL_OPTIONS_TABLE . '
123
			WHERE auth_option ' . $this->db->sql_like_expression('f_qte_attr_' . $this->db->get_any_char());
124
		$result = $this->db->sql_query($sql);
125
126
		while ($row = $this->db->sql_fetchrow($result))
127
		{
128
			$migrator_tool_permission->remove($row['auth_option'], false);
129
		}
130
		$this->db->sql_freeresult($result);
131
	}
132
}
133