NewsFeaturesDownloadsDevelopmentSupportAbout Us

Integrating LifeType

From LifeType Wiki

Contents

Integration With Other Web Pages

Typically, LifeType is installed in a stand-alone configuration. Either LifeType is installed in the user's web server document root directory, so it appears as the user's home page, or it is installed in a subsidiary directory (by default, named "LifeType") and the user adds a link from the home page, so his or her web log is found at http://www.mysite.com/LifeType/. In both these cases, LifeType can be customized by choosing an alternative template or the ambitious user may choose to edit a template. For example, if the user wants his home page to include a hit counter, he can modify the template that LifeType uses to display the blog.

However, a stand-alone configuration may not serve your needs. This document describes a way to integrate LifeType with other, non-blog, web pages.

Integration of LifeType with other web pages may be desirable, for example, if you have an existing web site and want to display recent blog entries on your home page. This may be particularly important if you want to use LifeType to add blog features to a commercial site, where the design of the home page is dictated by various business priorities, and blog features have to be integrated with other functionality, such as shopping carts or banner advertising.

Templates That Use the Same Stylesheet as the Home Page

LifeType templates ordinarily point to a stylesheet with the following code in the header.template file:

<link rel="stylesheet" href="{$url->getUrl("/templates/standard/default.css")}" type="text/css"/>

You can use the same stylesheet as a home page by using this code:

<link rel="stylesheet" href="{$url->getUrl("/templates/standard/default.css")}" type="text/css"/>

Sharing a stylesheet between LifeType template pages and conventional pages in a web site is a good step toward making LifeType pages look like the rest of a website, but what can be done to add blog entries to pages that are not generated by LifeType, like the home page?

Adding LifeType Blog Entries To Non-LifeType Pages

Fortunately, the architecture of LifeType was designed to easily allow access to LifeType's functionality from any PHP-generated page. You may use either Smarty templating or raw PHP in your integration and by using the API, you will save yourself the trouble of understanding the database structure that is used for LifeType and ensure that your code will work accross upgrades (as the database schema may change but the API will most likely remain the same)

Using the LifeType API requires some knowledge of PHP and object-oriented design. If you are not familiar with those concepts, you may still be able to copy and paste the code snippets below and get it working but you may get stuck if there are any issues. We suggest you get familiar with the programming concepts behind LifeType's code first and then get back to this.

In all the examples above, you will first have to set the value of the PLOG_CLASS_PATH constant in order to get them to work. Please set this constant to the path to the folder where LifeType is installed, or else LifeType will not be able to find its own class files.

Displaying the most recent posts

The following code will load the list with the 10 most recent posts and display them accordingly. For each post, we will include a link to its permalink, the intro text, the user who posted it, the date and the categories. The code is commented and ready to be used so it should be pretty easy to remove those bits that we don't need.

<html>
 <head>
  <title>LifeType posts</title>
 </head>
 <body>
<?php

	if (!defined( "PLOG_CLASS_PATH" )) {
        define( "PLOG_CLASS_PATH", "/path/to/your/lifetype/installation/" );
    }

	// necessary classes
	require_once( PLOG_CLASS_PATH."class/bootstrap.php" );	
	lt_include( PLOG_CLASS_PATH."class/dao/blogs.class.php" );
	lt_include( PLOG_CLASS_PATH."class/dao/articles.class.php" );
	
	//
	// id of your blog, you need to know this one before starting and can easily
	// be found out in the admin interface
	//
	$blogId = 1;
	
	// Load the blog articles. This method call returns an array of Article objects
	// or an empty array if there is none
	// See http://lifetype.net/api/class_articles.php#aee20e131c51a6fdbd4f0d24e16e3c76
	$articles = new Articles();
	$blogArticles = $articles->getBlogArticles(
			$blogId,                 // our blog id
			-1, 	                 // date, we don't care
			10,                      // amount of posts to fetch, increase this if needed
			0,                       // category id, but we don't care
			POST_STATUS_PUBLISHED    // only the ones that are published			
			                         // this method can take more parameters but we don't need them
		);

    if( count( $blogArticles ) == 0 ) {
	  print("Sorry, there are no posts to display" ); 
	}
	else {
		// loop through the posts and show them
		foreach( $blogArticles as $article ) {
			// See http://lifetype.net/api/class_article.php for more information regarding the Article class
			$blog = $article->getBlogInfo();
			// This returns an object of type BaseRequestGenerator
			$url = $blog->getBlogRequestGenerator();
			// This returns an object of type UserInfo
			$user = $article->getUserInfo();
			// This returns an array of ArticleCategory objects
			$categories = $article->getCategories();
			// And this returns the blog locale of type Locale
			$locale = $blog->getLocale();
			// Date when this was posted, of type Timestamp
			$date = $article->getDateObject();
?>
<p>
<a href="<?php echo $url->postPermalink($article) ?>"><?php echo $article->getTopic() ?></a><br/>
 <?php echo $article->getIntroText(); ?><br/>
 Posted by: <?php echo $user->getUsername(); ?> on the <?php echo $locale->formatDate($date); ?> | Categories:
 <?php foreach( $categories as $category ) { ?>
	<a href="<?php echo $url->categoryLink($category) ?>"><?php echo $category->getName(); ?></a>,  
 <?php } ?>
</p>
<?php
		}
	} 
?>
 </body>
</html>

Creating a user and a blog

The following code snippet will first create a user and then a blog whose owner will be the user previously created. You will probably want to add data fetching from the request and data validation to make sure that the data passed to the API is reliable, for simplicity's sake, the script below uses pre-defined usernames and blog names.

<?php

if (!defined( "PLOG_CLASS_PATH" )) {
    define( "PLOG_CLASS_PATH", "/path/to/your/lifetype/installation/");
}

// necessary classes
require_once( PLOG_CLASS_PATH."class/bootstrap.php" );	
lt_include( PLOG_CLASS_PATH."class/dao/users.class.php" );
lt_include( PLOG_CLASS_PATH."class/dao/userinfo.class.php" );
lt_include( PLOG_CLASS_PATH."class/dao/blogs.class.php" );
lt_include( PLOG_CLASS_PATH."class/dao/bloginfo.class.php" );
lt_include( PLOG_CLASS_PATH."class/dao/userpermissions.class.php" );
lt_include( PLOG_CLASS_PATH."class/dao/userpermission.class.php" );
lt_include( PLOG_CLASS_PATH."class/dao/permissions.class.php" );

// we assume that these have already been intialized
$userName = "test";
$password = "test";
$email    = "test@test.com";

// UserInfo object holding information about a user
// See http://lifetype.net/api/class_user_info.php for more information
$user = new UserInfo(
	$userName,			// username
	$password,			// password
	$email,				// email address
	"",					// "about myself", we don't care
	""			        // we don't care
);

// Once we have create the user, we can add it via Users::addUser();
// See http://lifetype.net/api/class_users.php
$users = new Users();
$result = $users->addUser( $user );
if( !$result ) {
	die( "There was an error adding the user, make sure that the data is correct and that the username does not already exist!" );
}

// grant the login_perm permission so that this user can log in
// we have to build a UserPermission object with the information required, 
// see http://www.lifetype.net/api/class_user_permission.php
$perms = new Permissions();
$loginPerm = $perms->getPermissionByName( "login_perm" );
$perm = new UserPermission( $user->getId(),    // user id
                                 0,   // it's a global permission, no blog id needed
                                 $loginPerm->getId()  // id of the permission
);
$userPerms = new UserPermissions();
$result = $userPerms->grantPermission( $perm );
if( !$result ) {
	die( "There was an error assigning the permission" );
}

// If everything was fine, now we have to create a blog. Users cannot log in unless they
// have at least one blog so we're going to create one with the BlogInfo object
// See http://lifetype.net/api/class_blog_info.php
$blog = new BlogInfo(
	"Blog name",           // name of the blog
	$user->getId(),        // id of the user who owns this blog
	"",                    // no "about" for this blog yet
	new BlogSettings()     // blog settings will be stored here
);

// Now we use Blogs::addBlog() to add the blog
// See http://lifetype.net/api/class_blogs.php
$blogs = new Blogs();
$result = $blogs->addBlog( $blog );
if( !$result ) {
	die( "There was an error adding the blog!" );
}

print( "User successfully added" );
?>

Displaying the most recent posts from all blogs

The following code simulates the behaviour of summary.php and displays the same list with the most recent posts . It will only show one post per blog in the last 7 days, because that is how the SummaryStats::getRecentArticles() has been configured to work.

Once we have loaded the list of posts, the loop that displays them is exactly the same as in the first example, except that this time we are also displaying the name of the blog and a link to it.

<html>
 <head>
  <title>Posts from all blogs</title>
 </head>
 <body>
<?php

	if (!defined( "PLOG_CLASS_PATH" )) {
        define( "PLOG_CLASS_PATH", "/path/to/your/lifetype/installation/" );
    }

	// necessary classes
	require_once( PLOG_CLASS_PATH."class/bootstrap.php" );	
	lt_include( PLOG_CLASS_PATH."class/summary/dao/summarystats.class.php" );
	
	// The SummaryStats class has access to aggregated data from all blogs, as opposed to the Articles
	// class that can only be used to load data from one blog
	// See http://lifetype.net/api/class_summary_stats.php
	$stats = new SummaryStats();
	$allArticles = $stats->getRecentArticles(
		ALL_GLOBAL_ARTICLE_CATEGORIES,       // all global article categories
		10                                   // only the last 10 posts 
	);
	
    if( count( $allArticles ) == 0 ) {
	  print("Sorry, there are no posts in this site to display" ); 
	}
	else {
		// loop through the posts and show them
		foreach( $allArticles as $article ) {
			// See http://lifetype.net/api/class_article.php for more information regarding the Article class
			$blog = $article->getBlogInfo();
			// This returns an object of type BaseRequestGenerator
			$url = $blog->getBlogRequestGenerator();
			// This returns an object of type UserInfo
			$user = $article->getUserInfo();
			// This returns an array of ArticleCategory objects
			$categories = $article->getCategories();
			// And this returns the blog locale of type Locale
			$locale = $blog->getLocale();
			// Date when this was posted, of type Timestamp
			$date = $article->getDateObject();
?>
<p>
<a href="<?php echo $url->postPermalink($article) ?>"><?php echo $article->getTopic() ?></a><br/>
 <?php echo $article->getIntroText(); ?><br/>
 Posted by: <?php echo $user->getUsername(); ?> on the <?php echo $locale->formatDate($date); ?> 
 in blog <a href="<? echo $url->blogLink()?>"><?php echo $blog->getBlog(); ?></a> | 
 Categories:
 <?php foreach( $categories as $category ) { ?>
	<a href="<?php echo $url->categoryLink($category) ?>"><?php echo $category->getName(); ?></a>,  
 <?php } ?>
</p>
<?php
		}
	} 
?>
 </body>
</html>