Completed
Push — develop ( 8387b5...1b51bd )
by Daniel
11:34
created

items::update_items()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 1
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
use blitze\sitemaker\services\menus\nestedset;
14
15
class items extends base_mapper
16
{
17
	/** @var \phpbb\config\config */
18
	protected $config;
19
20
	/** @var \blitze\sitemaker\services\menus\nestedset */
21
	protected $tree;
22
23
	/** @var string */
24
	protected $entity_class = 'blitze\sitemaker\model\entity\item';
25
26
	/** @var string */
27
	protected $entity_pkey = 'item_id';
28
29
	/**
30
	 * Constructor
31
	 *
32
	 * @param \phpbb\db\driver\driver_interface				$db					Database object
33
	 * @param \blitze\sitemaker\model\base_collection		$collection			Entity collection
34
	 * @param \blitze\sitemaker\model\mapper_factory		$mapper_factory		Mapper factory object
35
	 * @param string										$entity_table		Menu Items table
36
	 * @param \phpbb\config\config							$config				Config object
37
	 */
38 54
	public function __construct(\phpbb\db\driver\driver_interface $db, \blitze\sitemaker\model\base_collection $collection, \blitze\sitemaker\model\mapper_factory $mapper_factory, $entity_table, \phpbb\config\config $config)
39
	{
40 54
		parent::__construct($db, $collection, $mapper_factory, $entity_table);
41
42 54
		$this->config = $config;
43 54
		$this->tree = new nestedset(
44 54
			$db,
45 54
			new \phpbb\lock\db('sitemaker.table_lock.menu_items_table', $this->config, $db),
46 54
			$this->entity_table
47 54
		);
48 54
	}
49
50
	/**
51
	 * {@inheritdoc}
52
	 */
53 11
	public function load(array $condition = array())
54
	{
55 11
		$sql_where = join(' AND ', $this->get_sql_condition($condition));
56 11
		$row = $this->tree
57 11
			->set_sql_where($sql_where)
58 11
			->get_item_info();
59
60
		if ($row)
61 11
		{
62 8
			return $this->create_entity($row);
63
		}
64 3
		return null;
65
	}
66
67
	/**
68
	 * {@inheritdoc}
69
	 */
70 38
	public function find(array $condition = array())
71
	{
72 38
		$sql_where = join(' AND ', $this->get_sql_condition($condition));
73 38
		$tree_data = $this->tree
74 38
			->set_sql_where($sql_where)
75 38
			->get_all_tree_data();
76
77 38
		$this->collection->clear();
78 38
		foreach ($tree_data as $id => $row)
79
		{
80 34
			$this->collection[$id] = $this->create_entity($row);
81 38
		}
82
83 38
		return $this->collection;
84
	}
85
86
	/**
87
	 * {@inheritdoc}
88
	 */
89 6
	public function save(\blitze\sitemaker\model\entity_interface $entity)
90
	{
91
		/** @type \blitze\sitemaker\model\entity\item $entity */
92 6
		$sql_data = $entity->to_db();
93
94 6
		$this->tree->set_sql_where($this->get_sql_where($entity->get_menu_id()));
95
96 6
		if ($entity->get_item_id())
97 6
		{
98 4
			$item = $this->tree->update_item($entity->get_item_id(), $sql_data);
99 4
		}
100
		else
101
		{
102 2
			$item = $this->tree->insert($sql_data);
103
		}
104
105 6
		return $this->create_entity($item);
106
	}
107
108
	/**
109
	 * Add multiple items via string depicting hierarchical structure
110
	 *
111
	 * @param int $menu_id
112
	 * @param int $parent_id
113
	 * @param $string
114
	 * @return \blitze\sitemaker\model\base_collection
115
	 */
116 7
	public function add_items($menu_id, $parent_id, $string)
117
	{
118 7
		$items = $this->tree->string_to_nestedset($string, array('item_title' => '', 'item_url' => ''), array('menu_id' => $menu_id));
119
120 6
		$new_item_ids = array();
121 6
		if (sizeof($items))
122 6
		{
123 6
			$branch = $this->prep_items_for_storage($items);
124
125 6
			$new_item_ids = $this->tree->set_sql_where($this->get_sql_where($menu_id))
126 6
				->add_branch($branch, $parent_id);
127 5
		}
128
129 5
		return $this->find(array('item_id', '=', $new_item_ids));
130
	}
131
132
	/**
133
	 * Update entire tree saving parent-child relationships in a single go
134
	 *
135
	 * @param int $menu_id
136
	 * @param array $items
137
	 * @return array
138
	 */
139 3
	public function update_items($menu_id, array $items)
140
	{
141 3
		return $this->tree->set_sql_where($this->get_sql_where($menu_id))
142 3
			->update_tree($items);
143
	}
144
145
	/**
146
	 * {@inheritdoc}
147
	 */
148 44
	public function create_entity(array $row)
149
	{
150 44
		return new $this->entity_class($row, $this->config['enable_mod_rewrite']);
151
	}
152
153
	/**
154
	 * @param array $items
155
	 * @return array
156
	 */
157 6
	protected function prep_items_for_storage(array $items)
158
	{
159 6
		$branch = array();
160 6
		foreach ($items as $key => $row)
161
		{
162 6
			$entity = $this->create_entity($row);
163 6
			$branch[$key] = array_merge($entity->to_db(), array(
164 6
				'item_id'	=> $key,
165 6
			));
166 6
		}
167
168 6
		return $branch;
169
	}
170
171
	/**
172
	 * @param int $menu_id
173
	 * @return string
174
	 */
175 15
	protected function get_sql_where($menu_id)
176
	{
177 15
		return '%smenu_id = ' . (int) $menu_id;
178
	}
179
}
180