Completed
Push — master ( 1deced...926e5e )
by Daniel
08:35
created

save_blocks::_save_blocks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 12
ccs 8
cts 8
cp 1
rs 9.4286
cc 1
eloc 7
nc 1
nop 2
crap 1
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 4
	public function execute($style_id)
21
	{
22 4
		$route = $this->request->variable('route', '');
23 4
		$blocks = $this->request->variable('blocks', array(0 => array('' => '')));
24
25 4
		$this->route_mapper = $this->mapper_factory->create('blocks', 'routes');
26 4
		$this->block_mapper = $this->mapper_factory->create('blocks', 'blocks');
27
28 4
		$entity = $this->_force_get_route(array(
29 4
			'route'	=> $route,
30 4
			'style'	=> $style_id,
31 4
		));
32
33 4
		$this->_save_blocks($entity, $blocks);
34
35 4
		return array('message' => $this->user->lang('LAYOUT_SAVED'));
36
	}
37
38 4
	protected function _save_blocks($route_entity, array $blocks)
39
	{
40
		// find all blocks for this route
41 4
		$db_blocks = $this->_get_blocks($route_entity);
42
43 4
		$blocks_to_delete = array_filter(array_diff_key($db_blocks, $blocks));
44 4
		$blocks_to_update = array_filter(array_intersect_key($db_blocks, $blocks));
45
46 4
		$this->_delete_blocks($blocks_to_delete);
47 4
		$this->_update_blocks($blocks_to_update, $blocks);
48 4
		$this->_update_route($blocks_to_update, $route_entity);
49 4
	}
50
51 4
	protected function _get_blocks($entity)
52
	{
53 4
		$collection = $entity->get_blocks();
54
55 4
		if (!empty($collection))
56 4
		{
57 4
			return $collection->get_entities();
58
		}
59
		else
60
		{
61
			return array();
62
		}
63
	}
64
65 4
	protected function _delete_blocks(array $blocks_to_delete)
66
	{
67 4
		if (sizeof($blocks_to_delete))
68 4
		{
69 2
			$this->block_mapper->delete(array(
70 2
				'bid'	=> array_keys($blocks_to_delete)
71 2
			));
72 2
		}
73 4
	}
74
75 4
	protected function _update_blocks(array $blocks_to_update, array $data)
76
	{
77 4
		foreach ($blocks_to_update as $entity)
78
		{
79 2
			$row = $data[$entity->get_bid()];
80
81 2
			$entity->set_position($row['position']);
82 2
			$entity->set_weight($row['weight']);
83 2
			$this->block_mapper->save($entity);
84 4
		}
85 4
	}
86
87 4
	protected function _update_route(array $blocks_to_update, $route_entity)
88
	{
89 4
		$has_blocks = (sizeof($blocks_to_update)) ? true : false;
90
91 4
		if (!$has_blocks && !$this->_route_is_customized($route_entity->to_array()))
92 4
		{
93 1
			$this->route_mapper->delete($route_entity);
94 1
		}
95
		else
96
		{
97 3
			$route_entity->set_has_blocks($has_blocks);
98 3
			$this->route_mapper->save($route_entity);
99
		}
100 4
	}
101
}
102