Displaying the Most Recently Added Page Categorized by Template

Do something cool with CMS? Show us ...
This board is for 'Answers', and the discussion of answers... Not for questions.
Post Reply
Ned Nowotny
Forum Members
Forum Members
Posts: 32
Joined: Mon Jan 29, 2007 1:19 am

Displaying the Most Recently Added Page Categorized by Template

Post by Ned Nowotny »

I am converting a web site to CMSMS that includes a section of reviews.  The link from the front page is intended to lead to the most recently posted review while each review page includes a menu down the left side linking to all of the other review pages.

To enable this, I wrote the following PHP to a file referenced by the link on the front page and from the navigation menus throughout the web site.

Code: Select all

<?php

require_once( '../include.php' );

// The following script redirects to the most
// most recently created active review.

global $gCms;

$db = &$gCms->db;

$result = array();

$cms_db_prefix = cms_db_prefix();

$query = "SELECT " . $cms_db_prefix . "content.* " .
         "FROM   " . $cms_db_prefix . "content, " .
                     $cms_db_prefix . "templates " .
         "WHERE  " . $cms_db_prefix . "content.type = 'content' " .
         "AND    " . $cms_db_prefix . "content.active = 1 " .
         "AND    " . $cms_db_prefix . "content.template_id = " .
                     $cms_db_prefix . "templates.template_id ".
         "AND    " . $cms_db_prefix . "templates.template_name = 'Review' " .
         "ORDER BY create_date DESC LIMIT 1";

$dbresult = $db->Execute( $query );

$location = 'Location: http://' . $_SERVER[ 'HTTP_HOST' ];

if ( $dbresult && $dbresult->RecordCount() > 0 )
{
    $newest_review_alias = '';

    while ( $row = $dbresult->FetchRow() )
    {
        $newest_review_alias = $row[ 'content_alias' ];

        $location .= rtrim( dirname( $_SERVER[ 'PHP_SELF' ] ), '/\\' ) .
                     '/' .
                     $newest_review_alias .
                     '/';
    }
}
else
{
    // Punt back to the home page if the
    // review has disappeared.
    
    $location .= '/';
}

header( $location );

?>
When a site visitor clicks on any of the links that reference this page, they are redirected to the most recently published review.  Note that the implementation only selects a review that is "Active".

To use the CMSMS menu system to manage site naviagtion, I added a "Link" content item using the "Add New Content" function in the admin panel specifying the relative URL to the PHP file.

While this is working well, is there some other method that should be used for adding dynamic pages like this to CMSMS?  I suppose that simple helper functions should be added as "User Defined Tags", but it does not seem that the PHP above should be in a user defined tag that is then referenced from a "Content" page in CMSMS.  On the other hand, that appears to be the way to have the code stored in the database and managed by CMSMS rather that simply uploaded to the web server document tree file system.  Any comments or suggestions?

This implementation is derived from latest articles plugin.
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm

Re: Displaying the Most Recently Added Page Categorized by Template

Post by calguy1000 »

The proper location for this type of stuff, depending on it's size is either a plugin (to be stored in the plugins directory or a user defined tag.  if you do that, then you won't have to worry about including include.php.
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm

Re: Displaying the Most Recently Added Page Categorized by Template

Post by calguy1000 »

and actually, after looking at this, I'd rather just modify the menu manager to allow it to order the results differently.

something like {menu start_element='some_parent_page' order_by='last_modified'} or something like that.
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
Ned Nowotny
Forum Members
Forum Members
Posts: 32
Joined: Mon Jan 29, 2007 1:19 am

Re: Displaying the Most Recently Added Page Categorized by Template

Post by Ned Nowotny »

Except that I don't want the menu reordered, I just want a link on one page to redirect to the most recently added or edited page.  From there, the site visitor navigates using a sorted menu on the left-hand side of the page.

In my case (Need to Vent!), the site has several sections at the top-level.  One is Movie Reviews.  It is logically a section header, since there is no real content for it.  Now, each new review could be created within it  so that the link on the home page or across the top of each page could easily navigate to the newest review.  However, the same review needs to be accessible in the sorted list of all reviews requiring it to be duplicated or some other complication.  The PHP code means the people who edit the site, just need to create a new review (the default content type--i.e. default template) and it will be the "featured" review.

Not rocket science, but not an entirely unreasonable design.
Ziggywigged
Power Poster
Power Poster
Posts: 424
Joined: Sat Feb 02, 2008 12:42 am

Re: Displaying the Most Recently Added Page Categorized by Template

Post by Ziggywigged »

calguy1000 wrote: and actually, after looking at this, I'd rather just modify the menu manager to allow it to order the results differently.

something like {menu start_element='some_parent_page' order_by='last_modified'} or something like that.
calguy, i don't see " order_by='last_modified " as an available menu param... is that because it would require a UDT? If so, could you share?

I need a menu that always shows most recently added pages first (descending) under a certain parent.
Take a penny, leave a penny.
Ziggywigged
Power Poster
Power Poster
Posts: 424
Joined: Sat Feb 02, 2008 12:42 am

Re: Displaying the Most Recently Added Page Categorized by Template

Post by Ziggywigged »

so, i'll assume then that this isn't possible.
Take a penny, leave a penny.
Post Reply

Return to “Tips and Tricks”