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 | /** @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 | 30 | public function __construct(\phpbb\db\driver\driver_interface $db, $items_table, $pk, $sql_where = '') |
|
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 | 5 | public function is_ancestor(array $object, array $subject) |
|
59 | { |
||
60 | 5 | 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 | */ |
||
68 | 11 | public function count_descendants(array $row) |
|
69 | { |
||
70 | 11 | 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 | 6 | public function get_node_info($node_id) |
|
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 | 3 | public function qet_tree_sql($start = 0, $max_depth = 0, $sql_array = array()) |
|
103 | { |
||
104 | 3 | $sql_array = array_merge_recursive( |
|
105 | array( |
||
106 | 3 | 'SELECT' => array('i.*'), |
|
107 | 'FROM' => array( |
||
108 | 3 | $this->items_table => 'i' |
|
109 | 3 | ), |
|
110 | 'WHERE' => array( |
||
111 | 3 | 'i.depth ' . (($max_depth) ? " BETWEEN $start AND " . ($start + $max_depth) : ' >= ' . $start), |
|
112 | 3 | (($this->sql_where) ? 'i.' . $this->sql_where : ''), |
|
113 | 3 | ), |
|
114 | 3 | 'ORDER_BY' => 'i.left_id ASC', |
|
115 | 3 | ), |
|
116 | $sql_array |
||
117 | 3 | ); |
|
118 | |||
119 | 3 | $sql_array['SELECT'] = join(', ', array_filter($sql_array['SELECT'])); |
|
120 | 3 | $sql_array['WHERE'] = join(' AND ', array_filter($sql_array['WHERE'])); |
|
121 | |||
122 | 3 | 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 | 3 | public function get_tree_data($start = 0, $max_depth = 0, $sql_array = array()) |
|
138 | { |
||
139 | 3 | $sql_array = $this->qet_tree_sql($start, $max_depth, $sql_array); |
|
140 | 3 | $sql = $this->db->sql_build_query('SELECT', $sql_array); |
|
141 | 3 | $result = $this->db->sql_query($sql); |
|
142 | |||
143 | 3 | $data = array(); |
|
144 | 3 | while ($row = $this->db->sql_fetchrow($result)) |
|
145 | { |
||
146 | 3 | $data[$row[$this->pk]] = $row; |
|
147 | 3 | } |
|
148 | 3 | $this->db->sql_freeresult($result); |
|
149 | |||
150 | 3 | return $data; |
|
151 | } |
||
152 | |||
153 | /** |
||
154 | * @param array $data |
||
155 | * @param \phpbb\template\twig\twig $template |
||
156 | * @param string $handle |
||
157 | */ |
||
158 | 1 | public function display_list(array $data, \phpbb\template\twig\twig &$template, $handle = 'tree') |
|
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 | 3 | public function display_options($db_data, $title_column, $selected_ids = array(), $return_mode = 'options', $pad_with = ' ') |
|
237 | } |
||
238 |