Passed
Push — github-actions ( 480d5f...4845b0 )
by Daniel
03:51
created

save_block::get_updated_settings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 12
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 17
rs 9.8666
1
<?php
2
3
/**
4
 *
5
 * @package sitemaker
6
 * @copyright (c) 2013 Daniel A. (blitze)
7
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
8
 *
9
 */
10
11
namespace blitze\sitemaker\services\blocks\action;
12
13
class save_block extends base_action
14
{
15
	/** @var \blitze\sitemaker\model\mapper\blocks */
16
	protected $block_mapper;
17
18
	/**
19
	 * {@inheritdoc}
20
	 * @throws \blitze\sitemaker\exception\out_of_bounds
21
	 */
22
	public function execute($style_id)
23
	{
24
		$block_id = $this->request->variable('id', 0);
25
		$update_similar = $this->request->variable('similar', false);
26
27
		$this->block_mapper = $this->mapper_factory->create('blocks');
28
29
		/** @var \blitze\sitemaker\model\entity\block $entity */
30
		if (($entity = $this->block_mapper->load(array('bid', '=', $block_id))) === null)
31
		{
32
			throw new \blitze\sitemaker\exception\out_of_bounds('bid');
33
		}
34
35
		$old_hash = $entity->get_hash();
36
		$updated_blocks = array();
37
38
		/** @var \blitze\sitemaker\model\entity\block $entity */
39
		$entity = $this->get_updated_settings($entity);
40
		$entity = $this->block_mapper->save($entity);
41
42
		$new_hash = $entity->get_hash();
43
		$updated_blocks[$entity->get_bid()] = $this->render_block($entity);
44
45
		if ($update_similar && $new_hash !== $old_hash)
46
		{
47
			$updated_blocks += $this->update_similar($old_hash, $new_hash, $entity->get_settings());
48
		}
49
50
		return array(
51
			'list'	=> $updated_blocks,
52
		);
53
	}
54
55
	/**
56
	 * @param \blitze\sitemaker\model\entity\block $entity
57
	 * @return array
58
	 */
59
	private function get_updated_settings(\blitze\sitemaker\model\entity\block $entity)
60
	{
61
		$block_instance = $this->block_factory->get_block($entity->get_name());
62
		$default_config = $block_instance->get_config(array());
63
64
		$cfg_handler = $this->phpbb_container->get('blitze.sitemaker.blocks.cfg_handler');
65
		$submitted_settings = $cfg_handler->get_submitted_settings($default_config);
66
67
		$this->set_hidden_fields($submitted_settings, $default_config, $entity->get_settings());
68
69
		return $entity->set_permission($this->get_block_permissions())
70
			->set_class($this->request->variable('class', ''))
71
			->set_hide_title($this->request->variable('hide_title', 0))
72
			->set_status($this->request->variable('status', 0))
73
			->set_type($this->request->variable('type', 0))
74
			->set_view($this->request->variable('view', ''))
75
			->set_settings($submitted_settings);
76
	}
77
78
	/**
79
	 * Get submitted block permissions
80
	 * @return array
81
	 */
82
	private function get_block_permissions()
83
	{
84
		$groups = $this->request->variable('perm_groups', array(0));
85
86
		$permission = [];
87
		$groups = array_filter($groups);
88
89
		if (!empty($groups))
90
		{
91
			$permission =  array(
92
				'type'		=> $this->request->variable('perm_type', 0),
93
				'groups'	=> $groups,
94
			);
95
		}
96
97
		return $permission;
98
	}
99
100
	/**
101
	 * Hidden/static fields are not generated when editing block, so we add them manually
102
	 * @param array $submitted_settings
103
	 * @param array $default_settings
104
	 * @param array $db_settings
105
	 * @return void
106
	 */
107
	private function set_hidden_fields(array &$submitted_settings, array $default_settings, array $db_settings)
108
	{
109
		$hidden_settings = array_diff_key($default_settings, $submitted_settings);
110
111
		foreach ($hidden_settings as $field => $props)
112
		{
113
			// make sure we have required props
114
			$props = ((array) $props) + array('type' => '', 'default' => '');
115
116
			if ($props['type'] === 'hidden')
117
			{
118
				if (!empty($db_settings[$field]))
119
				{
120
					$submitted_settings[$field] = $db_settings[$field];
121
				}
122
				else if (!empty($props['default']))
123
				{
124
					$submitted_settings[$field] = $props['default'];
125
				}
126
			}
127
		}
128
	}
129
130
	/**
131
	 * @param string $field
132
	 * @param array $settings
133
	 * @return bool
134
	 */
135
	private function is_hidden_field($field, array $settings)
0 ignored issues
show
Unused Code introduced by
The method is_hidden_field() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
136
	{
137
		return $settings[$field] && $settings[$field]['type'] === 'hidden';
138
	}
139
140
	/**
141
	 * @param string $old_hash
142
	 * @param string $new_hash
143
	 * @param array  $settings
144
	 * @return array
145
	 */
146
	private function update_similar($old_hash, $new_hash, array $settings)
147
	{
148
		// find all similar blocks
149
		$blocks_collection = $this->block_mapper->find(array('hash', '=', $old_hash));
150
151
		$blocks = array();
152
		foreach ($blocks_collection as $entity)
153
		{
154
			$entity->set_hash($new_hash);
155
			$entity->set_settings($settings);
156
			$this->block_mapper->save($entity);
157
158
			$blocks[$entity->get_bid()] = $this->render_block($entity);
159
		}
160
161
		return $blocks;
162
	}
163
}
164