<?php
#
# Simple Module to display Wordpress Blog posts
#
# Noel McGran
# nmcgran@shaw.ca
#
#

function myTruncate($string, $limit, $break=".", $pad="...")
  {
    // return with no change if string is shorter than $limit
    if(strlen($string) <= $limit) return $string;
 
    // is $break present between $limit and the end of the string?
    if(false !== ($breakpoint = strpos($string, $break, $limit))) {
      if($breakpoint < strlen($string) - 1) {
        $string = substr($string, 0, $breakpoint) . $pad;
      }
    }
    
    return $string;
  }

function smarty_cms_function_wp_blog_posts($params, &$smarty) {

    global $gCms;
    global $db;

    $blogurl = $params['blogurl']; 	// base url of the blog
    $posts   = $params['posts'];	// number of posts to show
    $length  = $params['length'];	// max number of characters to show

    $blogpost = "";
 
    $query = "SELECT date_format(post_date, '%Y/%m/%d') as date_format, date_format(post_date, '%b %d, %Y') as post_date, post_title, post_excerpt, post_content, post_name, comment_count FROM wp_posts WHERE post_type = 'POST' AND post_status ='publish' ORDER BY ID DESC LIMIT $posts";
    $result = mysql_query($query) or die("Error: " . mysql_error());

    while($row = mysql_fetch_array( $result )) {

        $date_format = $row["date_format"];
        $post_title = $row["post_title"];
        $post_name = $row["post_name"];
        $post_date = $row["post_date"];
        $post_excerpt = $row["post_excerpt"];
        $post_content = $row["post_content"];
        $comment_count = $row["comment_count"];

        if ($length > 0) {
            $post_content = myTruncate($post_content, $length);
        }

        $blogpost .= "<div class='blogpost'>";
        $blogpost .= "<a href='$blogurl/$date_format/$post_name/'>$post_title</a><br />$post_date<br />";
        $blogpost .= "$post_content &nbsp;<a href='$blogurl/$date_format/$post_name/'>[more]</a><br />";
        $blogpost .= "Comments: $comment_count";
        $blogpost .= "</div>";
    }

    return $blogpost;

}

function smarty_cms_help_function_wp_blog_posts() {
	?>
	<h3>What does this do?</h3>
	<p>Displays Wordpress Blog posts within divs in a page/template. The post divs have a class of "blogpost", to allow for custom styling.</p>
	<h3>How do I use it?</h3>
	<p>{wp_blog_posts params}</p>
	<h3>What parameters does it take?</h3>
	<p>These parameters are necessary:
        <ul>
        <li>blogurl = base url of the blog (without the trailing slash - eg. http://blog.mysite.com)</li>
        <li>posts = the maximum number of blog posts to show</li> 
        </ul>
        </p>
	<p>This parameter is optional:
        <ul>
        <li>length = the maximum number of characters to show from the posts - will be truncated with "..."</li> 
        </ul>
        </p>
	<h3>Examples:</h3>
        <p>
        {wp_blog_posts blogurl="http://blog.mysite.com" posts="10" length="150"}
        <br />&nbsp;<br />
        {wp_blog_posts blogurl="http://blog.mysite.com" posts="2"}
        </p>
        <h3>Prerequisites:</h3>
        <p>
        <ul>
        <li>Wordpress must be using the same database as CMSMS</li>
        <li>Wordpress tables must be using the "wp_" prefix (standard wordpress install)</li>
        </ul>
        </p>
        <?php
}

function smarty_cms_about_function_wp_blog_posts() {
	?>
	<p>Author: Noel McGran &lt;nmcgran@shaw.ca&gt;</p>
	<p>Version: 1.0</p>
	<p>
	Change History:<br/>
	None
	</p>
	<?php
}

?>
