The page you are looking at now is at this URL: http://pastoid.com/byj
This paste was last updated on December 9, 2009 at 10:54 am.
Index: subpages.php =================================================================== --- subpages.php (revision 2520) +++ subpages.php (working copy) @@ -1,4 +1,3 @@ -<span>Subpages:</span> <?php $out = array(); foreach ( $subpages as $subpage ) { Index: subpages.plugin.php =================================================================== --- subpages.plugin.php (revision 2520) +++ subpages.plugin.php (working copy) @@ -2,50 +2,131 @@ class SubPagesPlugin extends Plugin { + private static $vocabulary = 'subpages'; + private static $content_type = 'page'; + private static $select_none = 'none'; - /** - * Add the subpage vocabulary - * - **/ + private $cache_rootPages = null; + public function action_plugin_activation($file) { if ( Plugins::id_from_file($file) == Plugins::id_from_file(__FILE__) ) { $params = array( - 'name' => '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( '<p>This setting relies on the theme auto assigning the available pages via the add_template_vars in the templates theme.php.</p>' ) ); + $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;$i<count($pages);$i++ ) { + for ( $i = 0; $i < count( $pages ); $i++ ) { foreach ( $descendants as $descendant ) { if ( $pages[$i]->slug == $descendant->term ) { - unset($pages[$i]); + unset( $pages[$i] ); break; } } @@ -74,40 +157,34 @@ } // Create an array of slug => title, appropriate for passing to a select control - $candidates = array('none' => 'No parent'); + $candidates = array( self::$select_none => 'No parent' ); foreach ( $pages as $page ) { if ( $page->slug != $post->slug ) { $candidates[$page->slug] = $page->title; } } - // Add a parent selector to the page settings - $parent_select = $form->settings->append( 'text', 'parent', 'null:null', _t( 'Parent: '), 'tabcontrol_select' ); - $parent_select->value = $parent_term == null ? 'none' : $parent_term->term; + $parent_select->value = $parent_term == null ? self::$select_none : $parent_term->term; $parent_select->options = $candidates; } } } - /** - * Handle update of parent - * @param Post $post The post being updated - * @param FormUI $form. The form from the publish page - **/ public function action_publish_post( $post, $form ) { - if ( $post->content_type == Post::type( 'page' ) ) { - $subpage_vocab = Vocabulary::get('subpages'); + if ( $post->content_type == Post::type( self::$content_type ) ) { + $subpage_vocab = Vocabulary::get( self::$vocabulary ); $parent_term = null; - $page_term = $subpage_vocab->get_term($post->slug); - if ( null != $page_term ) { + $page_term = $subpage_vocab->get_term( $post->slug ); + + if ( null != $page_term ) $parent_term = $page_term->parent(); - } $form_parent = $form->settings->parent->value; + // If the parent has been changed, delete this page from its children if ( null != $parent_term && $form_parent != $parent_term->term ) { - $subpage_vocab->delete_term($page_term->term); + $subpage_vocab->delete_term( $page_term->term ); // TODO If the parent no longer has descendants, delete it. // TODO What to do if the term has descendants, and we change the parent ? @@ -116,9 +193,10 @@ } // If a new term has been set, add it to the subpages vocabulary - if ( 'none' != $form_parent ) { + if ( self::$select_none != $form_parent ) { // Make sure the parent term exists. - $parent_term = $subpage_vocab->get_term($form_parent); + $parent_term = $subpage_vocab->get_term( $form_parent ); + if ( null == $parent_term ) { // There's no term for the parent, add it as a top-level term $parent_term = $subpage_vocab->add_term( $form_parent ); @@ -130,11 +208,16 @@ } } + public function action_update_check() + { + Update::add( 'SubPages', 'c6a39795-d5cb-4042-b05b-9666825b1bb4', $this->info->version ); + } + public function help() { return <<< END_HELP <p>To output subpages in a page, insert this code where you want them linked from:</p> -<blockquote><code><?php \$theme->subpages(); ?></code></blockquote> +<blockquote><code><?php \$theme->subpages( $post ); ?></code></blockquote> <p>The default theme inserts a link to each subpage. If you want to alter this, you should copy the <tt>subpages.php</tt> template included with this plugin to your current theme directory and make changes to it there.</p> @@ -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); } - } ?>
| Referring Domain | Hits |
|---|---|
| Unknown Referer | 127 |
| pastoid.com | 11 |
| www.google.com | 2 |
| www.google.de | 1 |
Tip: Use Pastoid to shorten URLs with this bookmarklet: Pastoid This