Passed
Push — release-3.2.0 ( 5b23cd...ed6ffb )
by Daniel
02:40
created

display::set_padding()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 5
c 1
b 0
f 0
nc 4
nop 5
dl 0
loc 10
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A display::get_html_option() 0 4 2
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
	/**
30
	 * Column names in the table
31
	 * @var string
32
	 */
33
	protected $column_item_id = 'item_id';
34
	protected $column_left_id = 'left_id';
35
	protected $column_right_id = 'right_id';
36
	protected $column_parent_id = 'parent_id';
37
	protected $column_depth = 'depth';
38
39
	/** @var array */
40
	protected $errors = array();
41
42
	/** @var array */
43
	protected $data = array();
44
45
	/**
46
	* Construct
47
	*
48
	* @param \phpbb\db\driver\driver_interface		$db             Database connection
49
	* @param string									$items_table	Table name
50
	* @param string									$pk				Primary key
51
	* @param string									$sql_where		Column restriction
52
	*/
53
	public function __construct(\phpbb\db\driver\driver_interface $db, $items_table, $pk, $sql_where = '')
54
	{
55
		$this->db = $db;
56
		$this->pk = $pk;
57
		$this->items_table = $items_table;
58
		$this->sql_where = $sql_where;
59
	}
60
61
	/**
62
	 * Is subject node an ancestor of the object node?
63
	 *
64
	 * @param array $object
65
	 * @param array $subject
66
	 * @return bool
67
	 */
68
	public function is_ancestor(array $object, array $subject)
69
	{
70
		return ($subject[$this->column_left_id] < $object[$this->column_left_id] && $subject[$this->column_right_id] > $object[$this->column_right_id]) ? true : false;
71
	}
72
73
	/**
74
	 * Count node descendants
75
	 * @param array $row
76
	 * @return int
77
	 */
78
	public function count_descendants(array $row)
79
	{
80
		return (int) (($row[$this->column_right_id] - $row[$this->column_left_id] - 1) / 2);
81
	}
82
83
	/**
84
	 * Get node row
85
	 * @param int $node_id
86
	 * @return mixed
87
	 */
88
	public function get_node_info($node_id)
89
	{
90
		$sql = "SELECT *
91
			FROM $this->items_table
92
			WHERE $this->pk = " . (int) $node_id ;
93
		$result = $this->db->sql_query($sql);
94
		$row = $this->db->sql_fetchrow($result);
95
		$this->db->sql_freeresult($result);
96
97
		return $row;
98
	}
99
100
	/**
101
	 * Get Tree Query
102
	 *
103
	 * @param	integer	$start			Starting depth
104
	 * @param	integer $max_depth		Max depth
105
	 * @param	array	$sql_array		Array of elements to merge into query
106
	 * 										array(
107
	 * 											'SELECT'	=> array('p.*'),
108
	 * 											'WHERE'		=> array('p.post_id = 2'),
109
	 * 										)
110
	 * @return	array
111
	 */
112
	public function qet_tree_sql($start = 0, $max_depth = 0, $sql_array = array())
113
	{
114
		$sql_array = array_merge_recursive(
115
			array(
116
				'SELECT'	=> array('i.*'),
117
				'FROM'		=> array(
118
					$this->items_table => 'i'
119
				),
120
				'WHERE'		=> array(
121
					'i.depth ' . (($max_depth) ? ' BETWEEN ' . (int) $start . ' AND ' . (int) ($start + $max_depth) : ' >= ' . (int) $start),
122
					$this->sql_where,
123
				),
124
				'ORDER_BY'	=> 'i.left_id ASC',
125
			),
126
			$sql_array
127
		);
128
129
		$sql_array['SELECT'] = join(', ', array_filter((array) $sql_array['SELECT']));
130
		$sql_array['WHERE'] = join(' AND ', array_filter((array) $sql_array['WHERE']));
131
132
		return $sql_array;
133
	}
134
135
	/**
136
	 * Get the tree data
137
	 *
138
	 * @param	integer	$start			Starting depth
139
	 * @param	integer $max_depth		Max depth
140
	 * @param	array	$sql_array		Array of elements to merge into query
141
	 * 										array(
142
	 * 											'SELECT'	=> array('p.*'),
143
	 * 											'WHERE'		=> array('p.post_id = 2'),
144
	 * 										)
145
	 * @return array
146
	 */
147
	public function get_tree_data($start = 0, $max_depth = 0, $sql_array = array())
148
	{
149
		$sql_array = $this->qet_tree_sql($start, $max_depth, $sql_array);
150
		$sql = $this->db->sql_build_query('SELECT', $sql_array);
151
		$result = $this->db->sql_query($sql);
152
153
		$data = array();
154
		while ($row = $this->db->sql_fetchrow($result))
155
		{
156
			$data[$row[$this->pk]] = $row;
157
			$data[$row[$this->pk]]['depth'] = $row['depth'] - $start;
158
		}
159
		$this->db->sql_freeresult($result);
160
161
		return $data;
162
	}
163
164
	/**
165
	 * @param array $data
166
	 * @param \phpbb\template\twig\twig $template
167
	 * @param string $handle
168
	 */
169
	public function display_list(array $data, \phpbb\template\twig\twig &$template, $handle = 'tree')
170
	{
171
		$prev_depth = 0;
172
		$parental_depth = array(0 => -1);
173
		$data = array_values($data);
174
175
		for ($i = 0, $size = sizeof($data); $i < $size; $i++)
176
		{
177
			$row 		= $data[$i];
178
			$this_depth	= $parental_depth[$row[$this->column_parent_id]] + 1;
179
			$repeat		= (int) abs($prev_depth - $this_depth);
180
181
			$tpl_data	= array(
182
				'PREV_DEPTH'	=> $prev_depth,
183
				'THIS_DEPTH'	=> $this_depth,
184
				'NUM_KIDS'		=> $this->count_descendants($row),
185
			);
186
187
			$template->assign_block_vars($handle, array_merge($tpl_data, array_change_key_case($row, CASE_UPPER)));
188
			$this->recursively_close_tags($repeat, $handle . '.close', $template);
189
190
			$prev_depth = $this_depth;
191
			$parental_depth[$row[$this->pk]] = $this_depth;
192
		}
193
		$this->recursively_close_tags($prev_depth, 'close_' . $handle, $template);
194
	}
195
196
	/**
197
	 * @param int $repeat
198
	 * @param string $handle
199
	 * @param \phpbb\template\twig\twig $template
200
	 * @return void
201
	 */
202
	protected function recursively_close_tags($repeat, $handle, \phpbb\template\twig\twig $template)
203
	{
204
		for ($i = 0; $i < $repeat; $i++)
205
		{
206
			$template->assign_block_vars($handle, array());
207
		}
208
	}
209
210
	/**
211
	 * Get tree as form options or data
212
	 *
213
	 * @param	array	$db_data		Raw tree data from database
214
	 * @param	string	$title_column	Database column name to use as label/title for each item
215
	 * @param	array	$selected_ids	Array of selected items
216
	 * @param	string	$return_mode	options | data
217
	 * @param	string	$pad_with		Character used to denote nesting
218
	 *
219
	 * @return	mixed	Returns array of padded titles or html string of options depending on $return_mode
220
	 */
221
	public function display_options($db_data, $title_column, $selected_ids = array(), $return_mode = 'options', $pad_with = '&nbsp;&nbsp;&nbsp;')
222
	{
223
		$return = array('options' => '', 'data' => array());
224
225
		$db_data = array_values($db_data);
226
		for ($i = 0, $size = sizeof($db_data); $i < $size; $i++)
227
		{
228
			$row = $db_data[$i];
229
230
			$padding = str_repeat($pad_with, $row['depth']);
231
			$title = $this->get_padded_title($padding, $row[$title_column]);
232
			$return['options'] .= $this->get_html_option($row, $selected_ids, $title);
233
			$return['data'][$row[$this->pk]] = $title;
234
		}
235
236
		return $return[$return_mode];
237
	}
238
239
	/**
240
	 * @param string $padding
241
	 * @param string $title
242
	 * @return string
243
	 */
244
	protected function get_padded_title($padding, $title)
245
	{
246
		return (($padding) ? $padding . '&#x2937; ' : '') . $title;
247
	}
248
249
	/**
250
	 * @param array $row
251
	 * @param array $selected_ids
252
	 * @param string $title
253
	 * @return string
254
	 */
255
	protected function get_html_option(array $row, array $selected_ids, $title)
256
	{
257
		$selected = (in_array($row[$this->pk], $selected_ids)) ? ' selected="selected' : '';
258
		return '<option value="' . $row[$this->pk] . '"' . $selected . '>' . $title . '</option>';
259
	}
260
}
261