|
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
|
4 |
|
*/ |
|
22
|
|
|
public function execute($style_id) |
|
23
|
4 |
|
{ |
|
24
|
4 |
|
$block_id = $this->request->variable('id', 0); |
|
25
|
|
|
$update_similar = $this->request->variable('similar', false); |
|
26
|
4 |
|
|
|
27
|
|
|
$this->block_mapper = $this->mapper_factory->create('blocks'); |
|
28
|
|
|
|
|
29
|
4 |
|
/** @var \blitze\sitemaker\model\entity\block $entity */ |
|
30
|
4 |
|
if (($entity = $this->block_mapper->load(array('bid', '=', $block_id))) === null) |
|
31
|
1 |
|
{ |
|
32
|
|
|
throw new \blitze\sitemaker\exception\out_of_bounds('bid'); |
|
33
|
|
|
} |
|
34
|
3 |
|
|
|
35
|
|
|
$old_hash = $entity->get_hash(); |
|
36
|
3 |
|
$updated_blocks = array(); |
|
37
|
3 |
|
|
|
38
|
3 |
|
/** @var \blitze\sitemaker\model\entity\block $entity */ |
|
39
|
3 |
|
$entity = $this->get_updated_settings($entity); |
|
40
|
3 |
|
$entity = $this->block_mapper->save($entity); |
|
41
|
3 |
|
|
|
42
|
3 |
|
$new_hash = $entity->get_hash(); |
|
43
|
3 |
|
$updated_blocks[$entity->get_bid()] = $this->render_block($entity); |
|
44
|
|
|
|
|
45
|
3 |
|
if ($update_similar && $new_hash !== $old_hash) |
|
46
|
3 |
|
{ |
|
47
|
3 |
|
$updated_blocks += $this->update_similar($old_hash, $new_hash, $entity->get_settings()); |
|
48
|
|
|
} |
|
49
|
3 |
|
|
|
50
|
3 |
|
return array( |
|
51
|
1 |
|
'list' => $updated_blocks, |
|
52
|
1 |
|
); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
3 |
|
/** |
|
56
|
3 |
|
* @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
|
3 |
|
$cfg_handler = $this->phpbb_container->get('blitze.sitemaker.blocks.cfg_handler'); |
|
65
|
|
|
$submitted_settings = $cfg_handler->get_submitted_settings($default_config); |
|
66
|
3 |
|
|
|
67
|
3 |
|
$this->set_hidden_fields($submitted_settings, $default_config, $entity->get_settings()); |
|
68
|
|
|
|
|
69
|
3 |
|
return $entity->set_permission($this->get_block_permissions()) |
|
70
|
3 |
|
->set_class($this->request->variable('class', '')) |
|
71
|
|
|
->set_hide_title($this->request->variable('hide_title', 0)) |
|
72
|
3 |
|
->set_status($this->request->variable('status', 0)) |
|
73
|
|
|
->set_type($this->request->variable('type', 0)) |
|
74
|
3 |
|
->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
|
3 |
|
{ |
|
84
|
|
|
$groups = $this->request->variable('perm_groups', array(0)); |
|
85
|
3 |
|
|
|
86
|
3 |
|
$permission = []; |
|
87
|
|
|
$groups = array_filter($groups); |
|
88
|
3 |
|
|
|
89
|
|
|
if (!empty($groups)) |
|
90
|
|
|
{ |
|
91
|
|
|
$permission = array( |
|
92
|
|
|
'type' => $this->request->variable('perm_type', 0), |
|
93
|
|
|
'groups' => $groups, |
|
94
|
3 |
|
); |
|
95
|
3 |
|
} |
|
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
|
1 |
|
* @param array $default_settings |
|
104
|
|
|
* @param array $db_settings |
|
105
|
|
|
* @return void |
|
106
|
1 |
|
*/ |
|
107
|
|
|
private function set_hidden_fields(array &$submitted_settings, array $default_settings, array $db_settings) |
|
108
|
1 |
|
{ |
|
109
|
1 |
|
$hidden_settings = array_diff_key($default_settings, $submitted_settings); |
|
110
|
|
|
|
|
111
|
1 |
|
foreach ($hidden_settings as $field => $props) |
|
112
|
1 |
|
{ |
|
113
|
1 |
|
// make sure we have required props |
|
114
|
|
|
$props = ((array) $props) + array('type' => '', 'default' => ''); |
|
115
|
1 |
|
|
|
116
|
1 |
|
if ($props['type'] === 'hidden') |
|
117
|
|
|
{ |
|
118
|
1 |
|
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) |
|
|
|
|
|
|
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
|
|
|
|
This check looks for private methods that have been defined, but are not used inside the class.