blocks::delete()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 1
dl 0
loc 13
ccs 9
cts 9
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 *
4
 * @package sitemaker
5
 * @copyright (c) 2015 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\model\mapper;
11
12
use blitze\sitemaker\model\base_mapper;
13
14
class blocks extends base_mapper
15
{
16
	/** @var string */
17
	protected $entity_class = 'blitze\sitemaker\model\entity\block';
18
19
	/** @var string */
20
	protected $entity_pkey = 'bid';
21
22
	/**
23
	 * @param array $sql_where
24
	 * @return string
25
	 */
26 59
	protected function find_sql(array $sql_where)
27
	{
28 59
		return 'SELECT * FROM ' . $this->entity_table .
29 59
			((sizeof($sql_where)) ? ' WHERE ' . join(' AND ', $sql_where) : '') . '
30 59
			ORDER BY position, weight ASC';
31
	}
32
33
	/**
34
	 * @param array|\blitze\sitemaker\model\entity\block $condition
35
	 */
36 11
	public function delete($condition)
37
	{
38 11
		parent::delete($condition);
39
40
		// move blocks up for position
41 10
		if ($condition instanceof $this->entity_class)
42 10
		{
43 2
			$this->db->sql_query('UPDATE ' . $this->entity_table . '
44
				SET weight = weight - 1
45 2
				WHERE weight > ' . (int) $condition->get_weight() . '
46 2
					AND style = ' . (int) $condition->get_style() . '
47 2
					AND route_id = ' . (int) $condition->get_route_id() . "
48 2
					AND position = '" . $this->db->sql_escape($condition->get_position()) . "'");
49 2
		}
50 10
	}
51
52
	/**
53
	 * {@inheritdoc}
54
	 */
55 9
	protected function insert(\blitze\sitemaker\model\entity_interface $entity)
56
	{
57 9
		// do we have other existing blocks for route at this position?
58
		/** @type \blitze\sitemaker\model\entity\block $entity */
59 9
		$collection = $this->find(array(
60
			array('route_id', '=', $entity->get_route_id()),
61
			array('style', '=', $entity->get_style()),
62
			array('position', '=', $entity->get_position()),
63
		));
64
65 9
		// if we don't have existing blocks, make sure weight = 0
66
		if (!$collection->valid())
67
		{
68 9
			$entity->set_weight(0);
69
		}
70 9
		// if we have existing blocks, increase block weight by 1 for any block that is below the new block
71 9
		else
72 9
		{
73 9
			$this->move_blocks_down($entity);
74 9
		}
75
76
		return parent::insert($entity);
77
	}
78
79
	/**
80
	 * @param \blitze\sitemaker\model\entity\block $entity
81
	 */
82
	protected function move_blocks_down(\blitze\sitemaker\model\entity\block $entity)
83
	{
84
		$sql = 'UPDATE ' . $this->entity_table . '
85
			SET weight = weight + 1
86
			WHERE weight >= ' . (int) $entity->get_weight() . '
87
				AND route_id = ' . (int) $entity->get_route_id() . '
88
				AND style = ' . (int) $entity->get_style() . "
89
				AND position = '" . $this->db->sql_escape($entity->get_position()) . "'";
90
		$this->db->sql_query($sql);
91
	}
92
}
93