/
page.php
executable file
·349 lines (301 loc) · 9.06 KB
/
page.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<?php
/**
*
* Pages extension for the phpBB Forum Software package.
*
* @copyright (c) 2014 phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
*/
namespace phpbb\pages\operators;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Operator for a set of pages
*/
class page implements page_interface
{
/** @var \phpbb\cache\driver\driver_interface */
protected $cache;
/** @var ContainerInterface */
protected $container;
/** @var \phpbb\db\driver\driver_interface */
protected $db;
/** @var \phpbb\extension\manager */
protected $extension_manager;
/** @var string */
protected $pages_table;
/** @var string */
protected $pages_links_table;
/** @var string */
protected $pages_pages_links_table;
/**
* Constructor
*
* @param \phpbb\cache\driver\driver_interface $cache Cache driver interface
* @param ContainerInterface $container Service container interface
* @param \phpbb\db\driver\driver_interface $db Database connection
* @param \phpbb\extension\manager $extension_manager Extension manager object
* @param string $pages_table Table name
* @param string $pages_links_table Table name
* @param string $pages_pages_links_table Table name
* @return \phpbb\pages\operators\page
* @access public
*/
public function __construct(\phpbb\cache\driver\driver_interface $cache, ContainerInterface $container, \phpbb\db\driver\driver_interface $db, \phpbb\extension\manager $extension_manager, $pages_table, $pages_links_table, $pages_pages_links_table)
{
$this->cache = $cache;
$this->container = $container;
$this->db = $db;
$this->extension_manager = $extension_manager;
$this->pages_table = $pages_table;
$this->pages_links_table = $pages_links_table;
$this->pages_pages_links_table = $pages_pages_links_table;
}
/**
* Get all pages
*
* @return array Array of page data entities
* @access public
*/
public function get_pages()
{
$entities = array();
// Load all page data from the database
$sql = 'SELECT *
FROM ' . $this->pages_table . '
ORDER BY page_order ASC, page_id ASC';
$result = $this->db->sql_query($sql);
while ($row = $this->db->sql_fetchrow($result))
{
// Import each page row into an entity
$entities[] = $this->container->get('phpbb.pages.entity')->import($row);
}
$this->db->sql_freeresult($result);
// Return all page entities
return $entities;
}
/**
* Add a page
*
* @param object $entity Page entity with new data to insert
* @return page_interface Added page entity
* @access public
*/
public function add_page($entity)
{
// Insert the page data to the database
$entity->insert();
// Get the newly inserted page's identifier
$page_id = $entity->get_id();
// Reload the data to return a fresh page entity
return $entity->load($page_id);
}
/**
* Delete a page
*
* @param int $page_id The page identifier to delete
* @return bool True if row was deleted, false otherwise
* @throws \phpbb\pages\exception\out_of_bounds
* @access public
*/
public function delete_page($page_id)
{
// Remove any existing page link data for this page
// An exception will be thrown if page identifier is invalid
$this->remove_page_links($page_id);
// Delete the page from the database
$sql = 'DELETE FROM ' . $this->pages_table . '
WHERE page_id = ' . (int) $page_id;
$this->db->sql_query($sql);
// Return true/false if a page was deleted
return (bool) $this->db->sql_affectedrows();
}
/**
* Get page routes (for use in viewonline)
*
* @return array Array of routes and page titles for all pages
* @access public
*/
public function get_page_routes()
{
$routes = array();
// Load all page routes and titles
$sql = 'SELECT page_route, page_title
FROM ' . $this->pages_table;
$result = $this->db->sql_query($sql);
while ($row = $this->db->sql_fetchrow($result))
{
$routes[$row['page_route']] = $row['page_title'];
}
$this->db->sql_freeresult($result);
// Return an array of all page routes
return $routes;
}
/**
* Get custom page link icons (pages_*.gif)
* Added by the user to the core style/theme/images directores
*
* @return array Array of icon image paths
* @access public
*/
public function get_page_icons()
{
// For efficiency, found icons are cached
if (($icons = $this->cache->get('_pages_icons')) === false)
{
$icons = $this->find('styles', 'pages_', '.gif');
$this->cache->put('_pages_icons', $icons);
}
return $icons;
}
/**
* Get custom page templates (pages_*.html)
* Added by the user to the core style/template directores
*
* @return array Array of template file paths
* @access public
*/
public function get_page_templates()
{
return $this->find('styles', 'pages_', '.html');
}
/**
* Find files
*
* @param string $path The path to search in
* @param string $prefix The prefix to search for
* @param string $suffix The suffix to search for
* @return array Array of found file paths
* @access protected
*/
protected function find($path, $prefix, $suffix)
{
$finder = $this->extension_manager->get_finder();
return $finder
->set_extensions(array('phpbb/pages'))
->prefix($prefix)
->suffix($suffix)
->core_path("$path/")
->extension_directory("/$path")
->find()
;
}
/**
* Get all page link location data for generating page links
*
* @param array $page_ids Optional array of page ids
* @return array Array of page link location data for the specified pages, or all pages
* @access public
*/
public function get_page_links($page_ids = array())
{
$sql_array = array(
'SELECT' => 'ppl.*, pl.page_link_location, pl.page_link_event_name, p.page_route, p.page_title, p.page_display, p.page_display_to_guests',
'FROM' => array($this->pages_pages_links_table => 'ppl'),
'LEFT_JOIN' => array(
array(
'FROM' => array($this->pages_links_table => 'pl'),
'ON' => 'pl.page_link_id = ppl.page_link_id',
),
array(
'FROM' => array($this->pages_table => 'p'),
'ON' => 'p.page_id = ppl.page_id',
),
),
'WHERE' => (!empty($page_ids)) ? $this->db->sql_in_set('ppl.page_id', $page_ids) : '',
'ORDER_BY' => 'p.page_order ASC, ppl.page_link_id ASC',
);
// Cache the SQL query for 1 hour if page_ids is empty
$cache_ttl = (empty($page_ids)) ? 3600 : 0;
$sql = $this->db->sql_build_query('SELECT', $sql_array);
$result = $this->db->sql_query($sql, $cache_ttl);
$rows = $this->db->sql_fetchrowset($result);
$this->db->sql_freeresult($result);
return $rows;
}
/**
* Insert page link location data for a page
*
* @param int $page_id Page identifier
* @param array $link_ids Page link location identifiers
* @return page_interface $this object for chaining calls
* @throws \phpbb\pages\exception\out_of_bounds
* @access public
*/
public function insert_page_links($page_id, $link_ids)
{
// First remove any existing page link data for this page
$this->remove_page_links($page_id);
$sql_ary = array();
foreach ($link_ids as $link_id)
{
$sql_ary[] = array(
'page_id' => (int) $page_id,
'page_link_id' => (int) $link_id,
);
}
if (sizeof($sql_ary))
{
// Insert the new page link data for this page
$this->db->sql_multi_insert($this->pages_pages_links_table, $sql_ary);
}
return $this;
}
/**
* Remove page link location data for a page
* This method usually need not be called outside of this class
*
* @param int $page_id Page identifier
* @return page_interface $this object for chaining calls
* @throws \phpbb\pages\exception\out_of_bounds
* @access protected
*/
protected function remove_page_links($page_id)
{
// Throw an exception if page identifier is invalid
if (!$this->page_id_exists($page_id))
{
throw new \phpbb\pages\exception\out_of_bounds('page_id');
}
// Delete the page's links from the database
$sql = 'DELETE FROM ' . $this->pages_pages_links_table . '
WHERE page_id = ' . (int) $page_id;
$this->db->sql_query($sql);
// Destroy cached page links
$this->cache->destroy('sql', $this->pages_pages_links_table);
return $this;
}
/**
* Get page link location names and identifiers
*
* @return array Array of page link location names and identifiers
* @access public
*/
public function get_link_locations()
{
$sql = 'SELECT page_link_id, page_link_location
FROM ' . $this->pages_links_table . '
ORDER BY page_link_id ASC';
$result = $this->db->sql_query($sql);
$rows = $this->db->sql_fetchrowset($result);
$this->db->sql_freeresult($result);
return $rows;
}
/**
* Check if a page identifier exists in the database
*
* @param int $id Page identifier
* @return bool $row True if page exists, false otherwise
* @access protected
*/
protected function page_id_exists($id)
{
$sql = 'SELECT 1
FROM ' . $this->pages_table . '
WHERE page_id = ' . (int) $id;
$result = $this->db->sql_query_limit($sql, 1);
$row = $this->db->sql_fetchrow($result);
$this->db->sql_freeresult($result);
return (bool) $row;
}
}