Completed
Push — master ( df7035...349ca4 )
by Daniel
08:26
created

display::get_tree_array()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 14
ccs 0
cts 9
cp 0
rs 9.4285
cc 2
eloc 8
nc 2
nop 3
crap 6
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\tree;
11
12
/**
13
* Display nested sets
14
*/
15
abstract class display
16
{
17
	/** @var \phpbb\db\driver\driver_interface */
18
	protected $db;
19
20
	/** @var string */
21
	protected $items_table;
22
23
	/** @var string */
24
	protected $pk;
25
26
	/** @var string */
27
	protected $sql_where;
28
29
	/** @var array */
30
	protected $errors = array();
31
32
	/** @var array */
33
	protected $data = array();
34
35
	/**
36
	* Construct
37
	*
38
	* @param \phpbb\db\driver\driver_interface		$db             Database connection
39
	* @param string									$items_table	Table name
40
	* @param string									$pk				Primary key
41
	* @param string									$sql_where		Column restriction
42
	*/
43 18
	public function __construct(\phpbb\db\driver\driver_interface $db, $items_table, $pk, $sql_where = '')
44
	{
45 18
		$this->db = $db;
46 18
		$this->pk = $pk;
47 18
		$this->items_table = $items_table;
48 18
		$this->sql_where = $sql_where;
49 18
	}
50
51
	/**
52
	 * Is subject node an ancestor of the object node?
53
	 * @param array $object
54
	 * @param array $subject
55
	 * @return bool
56
	 */
57
	public function is_in_path(array $object, array $subject)
58
	{
59
		return ($subject['left_id'] < $object['left_id'] && $subject['right_id'] > $object['right_id']) ? true : false;
60
	}
61
62
	/**
63
	 * Count node descendants
64
	 * @param array $row
65
	 * @return int
66
	 */
67 8
	public function count_descendants(array $row)
68
	{
69 8
		return (int) (($row['right_id'] - $row['left_id'] - 1) / 2);
70
	}
71
72
	/**
73
	 * Get node row
74
	 * @param int $node_id
75
	 * @return mixed
76
	 */
77
	public function get_node_info($node_id)
78
	{
79
		$sql = "SELECT *
80
			FROM $this->items_table
81
			WHERE $this->pk = " . (int) $node_id ;
82
		$result = $this->db->sql_query($sql);
83
		$row = $this->db->sql_fetchrow($result);
84
		$this->db->sql_freeresult($result);
85
86
		return $row;
87
	}
88
89
	/**
90
	 * Get Tree Query
91
	 *
92
	 * @param	integer	$start			Starting level
93
	 * @param	integer $level			Max depth
94
	 * @param	array	$sql_array		Array of elements to merge into query
95
	 * 										array(
96
	 * 											'SELECT'	=> array('p.*'),
97
	 * 											'WHERE'		=> array('p.post_id = 2'),
98
	 * 										)
99
	 * @return	string		The sql query
100
	 */
101
	public function qet_tree_sql($start = 0, $level = 0, $sql_array = array())
102
	{
103
		$sql_array = array_merge_recursive(
104
			array(
105
				'SELECT'	=> array('t.*'),
106
				'FROM'		=> array(
107
					$this->items_table => ' t'
108
				),
109
				'WHERE'		=> array(
110
					't.depth ' . (($level) ? " BETWEEN $start AND " . ($start + $level) : ' >= ' . $start),
111
					(($this->sql_where) ? 't.' . $this->sql_where : ''),
112
				),
113
				'ORDER_BY'	=> 't.left_id ASC',
114
			),
115
			$sql_array
116
		);
117
118
		$sql_array['SELECT'] = join(', ', array_filter($sql_array['SELECT']));
119
		$sql_array['WHERE'] = join(' AND ', array_filter($sql_array['WHERE']));
120
121
		return $this->db->sql_build_query('SELECT', $sql_array);
122
	}
123
124
	/**
125
	 * @param int $start
126
	 * @param int $level
127
	 * @param array $sql_array
128
	 * @return array
129
	 */
130
	public function get_tree_array($start = 0, $level = 0, $sql_array = array())
131
	{
132
		$sql = $this->qet_tree_sql($start, $level, $sql_array);
133
		$result = $this->db->sql_query($sql);
134
135
		$data = array();
136
		while ($row = $this->db->sql_fetchrow($result))
137
		{
138
			$data[$row[$this->pk]] = $row;
139
		}
140
		$this->db->sql_freeresult($result);
141
142
		return $data;
143
	}
144
145
	/**
146
	 * @param array $data
147
	 * @param \phpbb\template\twig\twig $template
148
	 * @param string $handle
149
	 */
150 1
	public function display_list(array $data, \phpbb\template\twig\twig &$template, $handle = 'tree')
151
	{
152 1
		$prev_depth = 0;
153 1
		$parental_depth = array(0 => -1);
154 1
		$data = array_values($data);
155
156 1
		for ($i = 0, $size = sizeof($data); $i < $size; $i++)
157
		{
158 1
			$row 		= $data[$i];
159 1
			$this_depth	= $parental_depth[$row['parent_id']] + 1;
160 1
			$repeat		= abs($prev_depth - $this_depth);
161
162
			$tpl_data	= array(
163 1
				'PREV_DEPTH'	=> $prev_depth,
164 1
				'THIS_DEPTH'	=> $this_depth,
165 1
				'NUM_KIDS'		=> $this->count_descendants($row),
166 1
			);
167
168 1
			$template->assign_block_vars($handle, array_merge($tpl_data, array_change_key_case($row, CASE_UPPER)));
169
170 1
			for ($j = 0; $j < $repeat; $j++)
171
			{
172 1
				$template->assign_block_vars($handle . '.close', array());
173 1
			}
174
175 1
			$prev_depth = $this_depth;
176 1
			$parental_depth[$row[$this->pk]] = $this_depth;
177 1
		}
178
179 1
		for ($i = 0; $i < $prev_depth; $i++)
180
		{
181
			$template->assign_block_vars('close_' . $handle, array());
182
		}
183 1
	}
184
185
	/**
186
	 * Get tree as form options or data
187
	 *
188
	 * @param	array	$db_data	Raw tree data from database
189
	 * @param	string	$title_column	Database column name to use as label/title for each item
190
	 * @param	array	$selected_ids	Array of selected items
191
	 * @param	string	$return_mode	options | data
192
	 * @param	string	$pad_with		Character used to denote nesting
193
	 *
194
	 * @return	mixed	Returns array of padded titles or html string of options depending on $return_mode
195
	 */
196
	public function display_options($db_data, $title_column, $selected_ids = array(), $return_mode = 'options', $pad_with = '&nbsp;&nbsp;&nbsp;&nbsp;')
197
	{
198
		$right = 0;
199
		$padding = '';
200
		$return_options = '';
201
		$return_data = array();
202
		$padding_store = array('0' => '');
203
204
		$db_data = array_values($db_data);
205
		for ($i = 0, $size = sizeof($db_data); $i < $size; $i++)
206
		{
207
			$row = $db_data[$i];
208
209
			if ($row['left_id'] < $right)
210
			{
211
				$padding .= $pad_with;
212
				$padding_store[$row['parent_id']] = $padding;
213
			}
214
			else if ($row['left_id'] > $right + 1)
215
			{
216
				$padding = (isset($padding_store[$row['parent_id']])) ? $padding_store[$row['parent_id']] : '';
217
			}
218
219
			$right = $row['right_id'];
220
			$title = $padding . '&#x251c;&#x2500; ' . $row[$title_column];
221
			$selected = (in_array($row[$this->pk], $selected_ids)) ? ' selected="selected' : '';
222
223
			$return_options .= '<option value="' . $row[$this->pk] . '"' . $selected . '>' . $title . '</option>';
224
			$return_data[$row[$this->pk]] = $title;
225
		}
226
227
		return ($return_mode == 'options') ? $return_options : $return_data;
228
	}
229
}
230