Completed
Push — develop ( 9ed6be...7acac6 )
by Daniel
11:40
created

display::is_ancestor()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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