-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathSimplePagesPageTable.php
More file actions
198 lines (170 loc) · 6.87 KB
/
SimplePagesPageTable.php
File metadata and controls
198 lines (170 loc) · 6.87 KB
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
<?php
/**
* Simple Pages
*
* @copyright Copyright 2008-2012 Roy Rosenzweig Center for History and New Media
* @license http://www.gnu.org/licenses/gpl-3.0.txt GNU GPLv3
*/
/**
* The Simple Pages page table class.
*
* @package SimplePages
*/
class SimplePagesPageTable extends Omeka_Db_Table
{
/**
* Find all pages, ordered by slug name.
*
* @return array The pages ordered alphabetically by their slugs
*/
public function findAllPagesOrderBySlug()
{
$select = $this->getSelect()->order('slug');
return $this->fetchObjects($select);
}
public function applySearchFilters($select, $params)
{
$alias = $this->getTableAlias();
$paramNames = array('parent_id',
'is_published',
'is_searchable',
'title',
'slug',
'created_by_user_id',
'modified_by_user_id',
'template');
foreach($paramNames as $paramName) {
if (isset($params[$paramName])) {
$select->where($alias . '.' . $paramName . ' = ?', array($params[$paramName]));
}
}
if (isset($params['sort'])) {
switch($params['sort']) {
case 'alpha':
$select->order("{$alias}.title ASC");
$select->order("{$alias}.order ASC");
break;
case 'order':
$select->order("{$alias}.order ASC");
$select->order("{$alias}.title ASC");
break;
}
}
}
/**
* Retrieve child pages from list of pages matching page ID.
*
* Matches against the pages parameter against the page ID. Also matches all
* children for the same to retrieve all children of a page.
*
* @param int $parentId The id of the original parent
* @param array $pages The array of all pages
* @return array
*/
public function findChildrenPages($parentId, $includeAllDescendants=false, $idToPageLookup = null, $parentToChildrenLookup = null)
{
if ((string)$parentId == '') {
return array();
}
$descendantPages = array();
if ($includeAllDescendants) {
// create the id to page lookup if required
if (!$idToPageLookup) {
$idToPageLookup = $this->_createIdToPageLookup();
}
// create the parent to children lookup if required
if (!$parentToChildrenLookup) {
$parentToChildrenLookup = $this->_createParentToChildrenLookup($idToPageLookup);
}
// get all of the descendant pages of the parent page
$childrenPages = $parentToChildrenLookup[$parentId];
$descendantPages = array_merge($descendantPages, $childrenPages);
foreach ( $childrenPages as $childPage ) {
if ( $allGrandChildren = $this->findChildrenPages($childPage->id, true, $idToPageLookup, $parentToChildrenLookup) ) {
$descendantPages = array_merge($descendantPages, $allGrandChildren);
}
}
} else {
// only include the immediate children
$descendantPages = $this->findBy(array('parent_id'=>$parentId, 'sort'=>'order'));
}
return $descendantPages;
}
protected function _createIdToPageLookup()
{
// get all of the pages
// this should eventually be just the id/parent_id pairs for all pages
$allPages = $this->findAll();
// create the page lookup
$idToPageLookup = array();
foreach($allPages as $page) {
$idToPageLookup[$page->id] = $page;
}
return $idToPageLookup;
}
protected function _createParentToChildrenLookup($idToPageLookup)
{
// create an associative array that maps parent ids to an array of any children's ids
$parentToChildrenLookup = array();
$allPages = array_values($idToPageLookup);
// initialize the children array for all potential parents
foreach($allPages as $page) {
$parentToChildrenLookup[$page->id] = array();
}
// add each child to his parent's array
foreach($allPages as $page) {
$parentToChildrenLookup[$page->parent_id][] = $page;
}
return $parentToChildrenLookup;
}
/**
* Returns an array of pages that could be a parent for the current page.
* This is used to populate a dropdown for selecting a new parent for the current page.
* In particluar, a page cannot be a parent of itself, and a page cannot have one of its descendents as a parent.
*
* @param integer $pageId The id of the page whose potential parent pages are returned.
* @return array The potential parent pages.
*/
public function findPotentialParentPages($pageId)
{
// create a page lookup table for all of the pages
$idToPageLookup = $this->_createIdToPageLookup();
// find all of the page's descendants
$descendantPages = $this->findChildrenPages($pageId, true, $idToPageLookup);
// filter out all of the descendant pages from the lookup table
$allPages = array_values($idToPageLookup);
foreach($descendantPages as $descendantPage) {
unset($idToPageLookup[$descendantPage->id]);
}
// filter out the page itself from the lookup table
unset($idToPageLookup[$pageId]);
// return the values of the filtered page lookup table
return array_values($idToPageLookup);
}
/**
* Returns an array of all the ancestor pages of a page.
*
* @param integer $pageId The id of the page whose ancestors are returned.
* @return array The array of ancestor pages.
*/
public function findAncestorPages($pageId)
{
// set the default ancestor pages to an empty array
$ancestorPages = array();
// create a page lookup table for all of the pages
$page = $this->find($pageId);
while($page && $page->parent_id) {
if ($page = $this->find($page->parent_id)) {
$ancestorPages[] = $page;
}
}
return $ancestorPages;
}
public function getSelect()
{
$select = parent::getSelect();
$permissions = new Omeka_Db_Select_PublicPermissions('SimplePages_Page');
$permissions->apply($select, 'simple_pages_pages','created_by_user_id','is_published');
return $select;
}
}