Index: subpages.php =================================================================== --- subpages.php (revision 2520) +++ subpages.php (working copy) @@ -1,4 +1,3 @@ -Subpages: 'subpages', + 'name' => self::$vocabulary, 'description' => 'A vocabulary for describing hierarchical relationships between pages', - 'feature_mask' => Vocabulary::feature_mask(true, false, false, false) + 'features' => array( 'hierarchical' ) ); - $subpages = new Vocabulary($params); + + $subpages = new Vocabulary( $params ); $subpages->insert(); } } - /** - * Add the necessary template - * - **/ - function action_init() + public function action_plugin_deactivation($file) { - $this->add_template( 'subpages', dirname(__FILE__) . '/subpages.php' ); + $subpages = Vocabulary::get( self::$vocabulary ); + $subpages->delete(); } - /** - * Add the page parent control to the publish page - * - * @param FormUI $form The publish page form - * @param Post $post The post being edited - **/ + public function action_init() + { + $name = strtolower( get_class( $this ) ); + $showAll = Options::get( $name.'__showallpages' ); + + if ( !$showAll ) + Plugins::register( array( &$this, '_plugin_add_template_vars' ), 'action', 'add_template_vars' ); + + $this->add_template( self::$vocabulary, dirname(__FILE__) . '/' . self::$vocabulary . '.php' ); + } + + public function _plugin_add_template_vars( $theme ) + { + if ( !isset($theme->pages) ) + return; + + // Caching, since this is called multiple times per page, and memory is cheap. + if ( $this->cache_rootPages !== null ) + $theme->pages = $this->cache_rootPages; + + $pages = $theme->pages; + $subpage_vocab = Vocabulary::get( self::$vocabulary ); + + $rootPages = array(); + foreach ($pages as $p) + { + $term = $subpage_vocab->get_term( $p->slug ); + + if ( ( $term == null ) || ( $term->parent() == null ) ) + $rootPages[] = $p; + } + + if ( count( $rootPages ) > 0 ) + $theme->pages = $this->cache_rootPages = $rootPages; + else + $theme->pages = $this->cache_rootPages = array(); + } + + public function filter_plugin_config( $actions, $plugin_id ) + { + if ( $plugin_id == $this->plugin_id() ) { + $actions[] = _t( 'Configure' ); + } + return $actions; + } + + public function action_plugin_ui( $plugin_id, $action ) + { + if ( $plugin_id == $this->plugin_id() ) { + $name = strtolower( get_class( $this ) ); + switch ( $action ) { + case _t( 'Configure' ) : + $showAll = Options::get( $name . '__showallpages' ); + + $ui = new FormUI( $name ); + $customvalue = $ui->append( 'checkbox', 'showallpages', $name.'__showallpages', _t('Show all pages in menus:') ); + $customvalue->value = $showAll; + + $ui->append( 'static', 'Please note', _t( '
This setting relies on the theme auto assigning the available pages via the add_template_vars in the templates theme.php.
' ) ); + $ui->append( 'submit', 'save', _t( 'Save' ) ); + $ui->out(); + break; + } + } + } + + public function theme_subpages($theme, $post) + { + if ( $post->content_type == Post::type( self::$content_type ) ) { + $subpage_vocab = Vocabulary::get( self::$vocabulary ); + $page_term = $subpage_vocab->get_term( $post->slug ); + + if ( null != $page_term ) { + // TODO this should be get_objects etc + + $slugs = array(); + $children = $page_term->children(); + + foreach ( $children as $child ) + $slugs[] = $child->term; + + if ( count($slugs) > 0 ) { + $theme->{self::$vocabulary} = Posts::get( array( 'slug' => $slugs ) ); + return $theme->display( self::$vocabulary ); + } + } + } + } + public function action_form_publish ( $form, $post ) { - if ( $form->content_type->value == Post::type( 'page' ) ) { - + if ( $form->content_type->value == Post::type( self::$content_type ) ) { $parent_term = null; $descendants = null; - $subpage_vocab = Vocabulary::get('subpages'); + $subpage_vocab = Vocabulary::get( self::$vocabulary ); // If there's an existing page, see if it has a related term, parent, and descendants if ( null != $post->slug ) { - $page_term = $subpage_vocab->get_term($post->slug); + $page_term = $subpage_vocab->get_term( $post->slug ); if ( null != $page_term ) { $parent_term = $page_term->parent(); $descendants = $page_term->descendants(); @@ -53,20 +134,22 @@ } // If there are pages, work out which ones can be a parent to this page - $pages = (array)Posts::get(array('content_type'=>'page', 'status'=>'published', 'nolimit'=>true)); + $pages = (array)Posts::get( array( 'content_type' => self::$content_type, 'nolimit'=>true ) ); - if ( 0 != count($pages) ) { + // Add a parent selector to the page settings + $parent_select = $form->settings->append( 'text', 'parent', 'null:null', _t( 'Parent: '), 'tabcontrol_select' ); + if ( 0 != count($pages) ) { // Descendants of the current page can't be its parent if ( null != $descendants ) { /* TODO Why doesn't this work ? $pages = array_udiff($pages, $descendants, create_function( '$a, $b', 'return $a->slug != $b->term;' ) ); */ - for ( $i=0;$iTo output subpages in a page, insert this code where you want them linked from:
-subpages(); ?>
+subpages( $post ); ?>
The default theme inserts a link to each subpage. If you want to alter this, you should copy the subpages.php template included with this plugin to your current theme directory and make changes to it there.
@@ -185,7 +268,7 @@ } // Check the stub matches the expected stub - return $stub == self::subpage_stub(array_pop($slugs)); + return $stub == self::subpage_stub( array_pop( $slugs ) ); } /** @@ -194,52 +277,21 @@ **/ public function filter_post_permalink( $permalink, $post ) { - if ( $post->content_type == Post::type( 'page' ) ) { - $subpage_vocab = Vocabulary::get('subpages'); - $page_term = $subpage_vocab->get_term($post->slug); + if ( $post->content_type == Post::type( self::$content_type ) ) { + $subpage_vocab = Vocabulary::get( self::$vocabulary ); + $page_term = $subpage_vocab->get_term( $post->slug ); if ( null != $page_term ) { - $permalink = Site::get_url('habari') . '/' . self::subpage_stub( $page_term ); + $permalink = Site::get_url( 'habari' ) . '/' . self::subpage_stub( $page_term ); } } return $permalink; } - /** - * Enable update notices to be sent using the Habari beacon - **/ - public function action_update_check() - { - Update::add( 'SubPages', 'c6a39795-d5cb-4042-b05b-9666825b1bb4', $this->info->version ); - } - - public function theme_subpages($theme, $post) - { - if ( $post->content_type == Post::type( 'page' ) ) { - $subpages = null; - $subpage_vocab = Vocabulary::get('subpages'); - $page_term = $subpage_vocab->get_term($post->slug); - if ( null != $page_term ) { - - // TODO this should be get_objects etc - - $slugs = array(); - $children = $page_term->children(); - foreach ( $children as $child ) { - $slugs[] = $child->term; - } - if ( count($slugs) > 0 ) { - $theme->subpages = Posts::get(array('slug' => $slugs)); - return $theme->display('subpages'); - } - } - } - } - private static function subpage_stub( $term ) { if ( is_string($term) ) { - $term = Vocabulary::get('subpages')->get_term($term); + $term = Vocabulary::get( self::$vocabulary )->get_term($term); } if ( null == $term ) { return false; @@ -254,7 +306,6 @@ return implode('/', $stub_parts); } - } ?>