@@ -126,20 +126,42 @@ function simple_pages_display_breadcrumbs($pageId = null, $seperator=' > ', $inc
126126 return $ html ;
127127}
128128
129- function simple_pages_display_hierarchy ($ parentPageId = 0 , $ partialFilePath = 'index/browse-hierarchy-page.php ' )
129+ /**
130+ * Recursively list the pages under a page for editing.
131+ *
132+ * @param SimplePage $page A page to list.
133+ * @return string
134+ */
135+ function simple_pages_edit_page_list ($ page )
130136{
131- $ html = '' ;
132- $ childrenPages = get_db ()->getTable ('SimplePagesPage ' )->findChildrenPages ($ parentPageId );
133- if (count ($ childrenPages )) {
137+ $ html = '<li class="page" id="page_ ' . $ page ->id . '"> ' ;
138+ $ html .= '<div class="sortable-item"> ' ;
139+ $ html .= sprintf ('<a href="%s">%s</a> ' , html_escape (record_url ($ page )), html_escape ($ page ->title ));
140+ $ html .= ' ( ' . html_escape ($ page ->slug ) . ') ' ;
141+ $ html .= '<ul class="action-links group"> ' ;
142+ $ html .= '<li> ' . sprintf ('<a href="%s" id="simplepage-%s" class="simplepage toggle-status status %s">%s</a> ' ,
143+ ADMIN_BASE_URL ,
144+ $ page ->id ,
145+ ($ page ->is_published ? 'public ' : 'private ' ),
146+ ($ page ->is_published ? __ ('Published ' ) : __ ('Private ' ))) . '</li> ' ;
147+ $ html .= '<li> ' . link_to ($ page , 'edit ' , __ ('Edit ' ), array ('class ' => 'edit ' )) . '</li> ' ;
148+ $ html .= '</ul> ' ;
149+ $ html .= '<br /> ' ;
150+ $ html .= __ ('<strong>%1$s</strong> on %2$s ' ,
151+ html_escape (metadata ($ page , 'modified_username ' )),
152+ html_escape (format_date (metadata ($ page , 'updated ' ), Zend_Date::DATETIME_SHORT )));
153+ $ html .= '<a class="delete-toggle delete-element" href="#"> ' . __ ('Delete ' ) . '</a> ' ;
154+ $ html .= '</div> ' ;
155+
156+ $ childrenPages = $ page ->getChildren ();
157+ if (count ($ childrenPages )) {
134158 $ html .= '<ul> ' ;
135- foreach ($ childrenPages as $ childPage ) {
136- $ html .= '<li> ' ;
137- $ html .= get_view ()->partial ($ partialFilePath , array ('simple_pages_page ' => $ childPage ));
138- $ html .= simple_pages_display_hierarchy ($ childPage ->id , $ partialFilePath );
139- $ html .= '</li> ' ;
159+ foreach ($ childrenPages as $ childPage ) {
160+ $ html .= simple_pages_edit_page_list ($ childPage );
140161 }
141162 $ html .= '</ul> ' ;
142163 }
164+ $ html .= '</li> ' ;
143165 return $ html ;
144166}
145167
@@ -171,4 +193,56 @@ function simple_pages_get_parent_options($page)
171193 }
172194 }
173195 return $ valuePairs ;
174- }
196+ }
197+
198+ /**
199+ * Update orders of all simple pages that have been modified.
200+ */
201+ function simple_pages_update_order ($ newOrder )
202+ {
203+ $ db = get_db ();
204+ $ table = $ db ->SimplePagesPage ;
205+
206+ // Pages are ordered by order, then by title, so two passes are needed.
207+
208+ // First step: update parent if needed.
209+ $ sql = "SELECT `id`, `parent_id` FROM ` $ table` ORDER BY `order` ASC, `title` ASC " ;
210+ $ currentOrder = $ db ->fetchPairs ($ sql );
211+
212+ foreach ($ newOrder as $ id => $ parentId ) {
213+ if ($ currentOrder [$ id ] != $ parentId ) {
214+ $ db ->update ($ table ,
215+ array ('parent_id ' => $ parentId ),
216+ 'id = ' . $ id );
217+ // Update old hierarchy for next step.
218+ $ currentOrder [$ id ] = $ parentId ;
219+ }
220+ }
221+
222+ // Second step: update order if needed for each sibling.
223+ // For each parent, check if current children are ordered as new ones.
224+ while (!empty ($ newOrder )) {
225+ $ parentId = reset ($ newOrder );
226+
227+ // Get all current and new pages with this parent.
228+ $ currentChildren = array_intersect ($ currentOrder , array ($ parentId ));
229+ $ newChildren = array_intersect ($ newOrder , array ($ parentId ));
230+
231+ // Compare them and update all values if they are different.
232+ // Orders are compared as csv because no function allows to check order.
233+ if (implode (', ' , array_keys ($ currentChildren )) != implode (', ' , array_keys ($ newChildren ))) {
234+ // Order by 10 for easier insert and update of edited pages.
235+ $ order = 10 ;
236+ foreach ($ newChildren as $ id => $ parentId ) {
237+ $ db ->update ($ table ,
238+ array ('order ' => $ order ),
239+ 'id = ' . $ id );
240+ $ order += 10 ;
241+ }
242+ }
243+
244+ // Remove filtered keys before loop.
245+ $ currentOrder = array_diff_key ($ currentOrder , $ currentChildren );
246+ $ newOrder = array_diff_key ($ newOrder , $ newChildren );
247+ }
248+ }
0 commit comments