1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* @package sitemaker |
5
|
|
|
* @copyright (c) 2013 Daniel A. (blitze) |
6
|
|
|
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace blitze\sitemaker\services\blocks\action; |
11
|
|
|
|
12
|
|
|
class save_blocks extends base_action |
13
|
|
|
{ |
14
|
|
|
/** @var \blitze\sitemaker\model\blocks\mapper\blocks */ |
15
|
|
|
protected $block_mapper; |
16
|
|
|
|
17
|
|
|
/** @var \blitze\sitemaker\model\blocks\mapper\routes */ |
18
|
|
|
protected $route_mapper; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* {@inheritdoc} |
22
|
|
|
*/ |
23
|
4 |
|
public function execute($style_id) |
24
|
|
|
{ |
25
|
4 |
|
$route = $this->request->variable('route', ''); |
26
|
4 |
|
$blocks = $this->request->variable('blocks', array(0 => array('' => ''))); |
27
|
|
|
|
28
|
4 |
|
$this->route_mapper = $this->mapper_factory->create('blocks', 'routes'); |
29
|
4 |
|
$this->block_mapper = $this->mapper_factory->create('blocks', 'blocks'); |
30
|
|
|
|
31
|
4 |
|
$entity = $this->force_get_route(array( |
32
|
4 |
|
'route' => $route, |
33
|
4 |
|
'style' => $style_id, |
34
|
4 |
|
)); |
35
|
|
|
|
36
|
4 |
|
$this->save($entity, $blocks); |
|
|
|
|
37
|
|
|
|
38
|
4 |
|
return array('message' => $this->user->lang('LAYOUT_SAVED')); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param \blitze\sitemaker\model\blocks\entity\route $entity |
43
|
|
|
* @param array $blocks |
44
|
|
|
*/ |
45
|
4 |
|
protected function save(\blitze\sitemaker\model\blocks\entity\route $entity, array $blocks) |
46
|
|
|
{ |
47
|
|
|
// find all blocks for this route |
48
|
4 |
|
$db_blocks = $this->get_blocks($entity); |
49
|
|
|
|
50
|
4 |
|
$blocks_to_delete = array_filter(array_diff_key($db_blocks, $blocks)); |
51
|
4 |
|
$blocks_to_update = array_filter(array_intersect_key($db_blocks, $blocks)); |
52
|
|
|
|
53
|
4 |
|
$this->delete_blocks($blocks_to_delete); |
54
|
4 |
|
$this->update_blocks($blocks_to_update, $blocks); |
55
|
4 |
|
$this->update_route($blocks_to_update, $entity); |
56
|
4 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get all blocks for route |
60
|
|
|
* |
61
|
|
|
* @param \blitze\sitemaker\model\blocks\entity\route $entity |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
4 |
|
protected function get_blocks(\blitze\sitemaker\model\blocks\entity\route $entity) |
65
|
|
|
{ |
66
|
4 |
|
$collection = $entity->get_blocks(); |
67
|
|
|
|
68
|
4 |
|
if (!empty($collection)) |
69
|
4 |
|
{ |
70
|
4 |
|
return $collection->get_entities(); |
71
|
|
|
} |
72
|
|
|
else |
73
|
|
|
{ |
74
|
|
|
return array(); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Delete specified blocks |
80
|
|
|
* |
81
|
|
|
* @param array $blocks_to_delete |
82
|
|
|
*/ |
83
|
4 |
|
protected function delete_blocks(array $blocks_to_delete) |
84
|
|
|
{ |
85
|
4 |
|
if (sizeof($blocks_to_delete)) |
86
|
4 |
|
{ |
87
|
2 |
|
$this->block_mapper->delete(array('bid', '=', array_keys($blocks_to_delete))); |
88
|
2 |
|
} |
89
|
4 |
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Update block position and weight |
93
|
|
|
* |
94
|
|
|
* @param array $blocks_to_update |
95
|
|
|
* @param array $data |
96
|
|
|
*/ |
97
|
4 |
|
protected function update_blocks(array $blocks_to_update, array $data) |
98
|
|
|
{ |
99
|
4 |
|
foreach ($blocks_to_update as $entity) |
100
|
|
|
{ |
101
|
2 |
|
$row = $data[$entity->get_bid()]; |
102
|
|
|
|
103
|
2 |
|
$entity->set_position($row['position']); |
104
|
2 |
|
$entity->set_weight($row['weight']); |
105
|
2 |
|
$this->block_mapper->save($entity); |
106
|
4 |
|
} |
107
|
4 |
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Update route if it has blocks or route is customized, otherwise, delete the route |
111
|
|
|
* |
112
|
|
|
* @param array $blocks_to_update |
113
|
|
|
* @param \blitze\sitemaker\model\blocks\entity\route $entity |
114
|
|
|
*/ |
115
|
4 |
|
protected function update_route(array $blocks_to_update, \blitze\sitemaker\model\blocks\entity\route $entity) |
116
|
|
|
{ |
117
|
4 |
|
$has_blocks = (sizeof($blocks_to_update)) ? true : false; |
118
|
|
|
|
119
|
4 |
|
if (!$has_blocks && !$this->route_is_customized($entity->to_array())) |
120
|
4 |
|
{ |
121
|
1 |
|
$this->route_mapper->delete($entity); |
122
|
1 |
|
} |
123
|
|
|
else |
124
|
|
|
{ |
125
|
3 |
|
$entity->set_has_blocks($has_blocks); |
126
|
3 |
|
$this->route_mapper->save($entity); |
127
|
|
|
} |
128
|
4 |
|
} |
129
|
|
|
} |
130
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.