1 | <?php |
||
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 | 30 | public function __construct(\phpbb\db\driver\driver_interface $db, $items_table, $pk, $sql_where = '') |
|
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 | 5 | public function is_ancestor(array $object, array $subject) |
|
72 | |||
73 | /** |
||
74 | * Count node descendants |
||
75 | * @param array $row |
||
76 | * @return int |
||
77 | */ |
||
78 | 11 | public function count_descendants(array $row) |
|
82 | |||
83 | /** |
||
84 | * Get node row |
||
85 | * @param int $node_id |
||
86 | * @return mixed |
||
87 | */ |
||
88 | 6 | public function get_node_info($node_id) |
|
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 | 3 | public function qet_tree_sql($start = 0, $max_depth = 0, $sql_array = array()) |
|
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 | 3 | public function get_tree_data($start = 0, $max_depth = 0, $sql_array = array()) |
|
162 | |||
163 | /** |
||
164 | * @param array $data |
||
165 | * @param \phpbb\template\twig\twig $template |
||
166 | * @param string $handle |
||
167 | */ |
||
168 | 1 | public function display_list(array $data, \phpbb\template\twig\twig &$template, $handle = 'tree') |
|
169 | { |
||
170 | 1 | $prev_depth = 0; |
|
171 | 1 | $parental_depth = array(0 => -1); |
|
172 | 1 | $data = array_values($data); |
|
173 | |||
174 | 1 | for ($i = 0, $size = sizeof($data); $i < $size; $i++) |
|
175 | { |
||
176 | 1 | $row = $data[$i]; |
|
177 | 1 | $this_depth = $parental_depth[$row[$this->column_parent_id]] + 1; |
|
178 | 1 | $repeat = abs($prev_depth - $this_depth); |
|
179 | |||
180 | $tpl_data = array( |
||
181 | 1 | 'PREV_DEPTH' => $prev_depth, |
|
182 | 1 | 'THIS_DEPTH' => $this_depth, |
|
183 | 1 | 'NUM_KIDS' => $this->count_descendants($row), |
|
184 | 1 | ); |
|
185 | |||
186 | 1 | $template->assign_block_vars($handle, array_merge($tpl_data, array_change_key_case($row, CASE_UPPER))); |
|
187 | 1 | $this->recursively_close_tags($repeat, $handle . '.close', $template); |
|
188 | |||
189 | 1 | $prev_depth = $this_depth; |
|
190 | 1 | $parental_depth[$row[$this->pk]] = $this_depth; |
|
191 | 1 | } |
|
192 | 1 | $this->recursively_close_tags($prev_depth, 'close_' . $handle, $template); |
|
193 | 1 | } |
|
194 | |||
195 | /** |
||
196 | * @param int $repeat |
||
197 | * @param string $handle |
||
198 | * @param \phpbb\template\twig\twig $template |
||
199 | * @return void |
||
200 | */ |
||
201 | 1 | protected function recursively_close_tags($repeat, $handle, \phpbb\template\twig\twig &$template) |
|
202 | { |
||
203 | 1 | for ($i = 0; $i < $repeat; $i++) |
|
204 | { |
||
205 | 1 | $template->assign_block_vars('close_' . $handle, array()); |
|
206 | 1 | } |
|
207 | 1 | } |
|
208 | |||
209 | /** |
||
210 | * Get tree as form options or data |
||
211 | * |
||
212 | * @param array $db_data Raw tree data from database |
||
213 | * @param string $title_column Database column name to use as label/title for each item |
||
214 | * @param array $selected_ids Array of selected items |
||
215 | * @param string $return_mode options | data |
||
216 | * @param string $pad_with Character used to denote nesting |
||
217 | * |
||
218 | * @return mixed Returns array of padded titles or html string of options depending on $return_mode |
||
219 | */ |
||
220 | 3 | public function display_options($db_data, $title_column, $selected_ids = array(), $return_mode = 'options', $pad_with = ' ') |
|
242 | |||
243 | /** |
||
244 | * @param string $padding |
||
245 | * @param string $pad_with |
||
246 | * @param array $row |
||
247 | * @param array $padding_store |
||
248 | * @param int $right |
||
249 | * @retur void |
||
250 | */ |
||
251 | 3 | protected function set_padding(&$padding, $pad_with, array $row, array $padding_store, $right) |
|
263 | |||
264 | /** |
||
265 | * @param string $padding |
||
266 | * @param string $title |
||
267 | * @return string |
||
268 | */ |
||
269 | 3 | protected function get_padded_title($padding, $title) |
|
273 | |||
274 | /** |
||
275 | * @param array $row |
||
276 | * @param array $selected_ids |
||
277 | * @param string $title |
||
278 | * @return string |
||
279 | */ |
||
280 | 3 | protected function get_html_option(array $row, array $selected_ids, $title) |
|
285 | } |
||
286 |