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 [] |
|
|
|
|
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
|
|
|
* @param array $submitted_settings |
102
|
|
|
* @param array $default_settings |
103
|
1 |
|
* @param array $db_settings |
104
|
|
|
* @return void |
105
|
|
|
*/ |
106
|
1 |
|
private function set_hidden_fields(array &$submitted_settings, array $default_settings, array $db_settings) |
107
|
|
|
{ |
108
|
1 |
|
$not_submitted = array_diff_key($default_settings, $submitted_settings); |
109
|
1 |
|
$hidden_settings = array_intersect_key($db_settings, $not_submitted); |
110
|
|
|
|
111
|
1 |
|
foreach ($hidden_settings as $field => $value) |
112
|
1 |
|
{ |
113
|
1 |
|
if ($default_settings[$field]['type'] === 'hidden') |
114
|
|
|
{ |
115
|
1 |
|
$submitted_settings[$field] = $value; |
116
|
1 |
|
} |
117
|
|
|
} |
118
|
1 |
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param string $old_hash |
122
|
|
|
* @param string $new_hash |
123
|
|
|
* @param array $settings |
124
|
|
|
* @return array |
125
|
|
|
*/ |
126
|
|
|
private function update_similar($old_hash, $new_hash, array $settings) |
127
|
|
|
{ |
128
|
|
|
// find all similar blocks |
129
|
|
|
$blocks_collection = $this->block_mapper->find(array('hash', '=', $old_hash)); |
130
|
|
|
|
131
|
|
|
$blocks = array(); |
132
|
|
|
foreach ($blocks_collection as $entity) |
133
|
|
|
{ |
134
|
|
|
$entity->set_hash($new_hash); |
135
|
|
|
$entity->set_settings($settings); |
136
|
|
|
$this->block_mapper->save($entity); |
137
|
|
|
|
138
|
|
|
$blocks[$entity->get_bid()] = $this->render_block($entity); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return $blocks; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|