Business, Tech, Life, and Whatever else

Building a news module – 68 Classifieds

by Eric Barnes on July 1, 2009

Today I decided I should share how I built the news module for a recent site I worked on. The goal of this tutorial is to hopefully help you better understand the 68 Classifieds module system and how to build your own modules. For this tutorial I kept the module pretty basic but it should do just enough to be a workable system.

Before we get started you should download the source files so you can follow along.

If you are ready lets jump right in. The requirements of any module are pretty simple and it is a config.php which is used for the installation. Beyond that you can have a hooks.php file to enteract with the core, an index.php and admin.php which allows you to view them from the template, and finally an init.php file which handles installing, upgrading, and deleting.

Config File

The first thing I always do is build the config.php file. Inside this file it has:

<?php
$data['module']['name'] = "news";
$data['module']['displayname'] = 'News';
$data['module']['description'] = "Allows you to add news";
$data['module']['version'] = "v1.0";
$data['module']['admin_capable'] = "1";
$data['module']['user_capable'] = "1";

Each line basically tells the system something about your module.

Name – Is the module folder name.
Display Name – Is what is shown in the administration
Description – Is what is shown in the administration
Version – This the version number of the module
Admin Capable – This tells the system it has an admin file.
User Capable – This tells the system it can be accessed from your sites frontend.

Init File

The next step I did was create the init.php file so I could add a new table to the db.

This file as you can see includes three functions. install, upgrade, and uninstall. These are used for adding tables or altering existing ones. If you look at the install function I am just adding a new “posts” table to hold our news items. The upgrade is not used and the delete function drops the table.

News Class

Now we are going to create a news class file. This file will handle everything needed in the news system. Basically adding, updating, deleting, etc.. In the source download this is name “news_class.php”. I will not really go through all the code because it should be pretty straightforward. The only thing that is complex is the getNews method which uses a few internal classes to build the results. This also handles the sorting, searching, and displaying the items.

Admin

The admin.php file is used so the administrator can add/edit/delete news entries. The code at the top includes the necessary class files and also instantiates the news class.

require_once(FILESYSTEM_PATH .'includes/classes/kernel/Pagination.php');
require_once(FILESYSTEM_PATH .'includes/classes/wrapper/SuperglobalWrapper.php');
require_once(FILESYSTEM_PATH.'/modules/news/news_class.php');
$News=new News();

The pagination and wrapper files are the ones needed for paging so they must be included first. Then is the include for the news class. After that it is just a bunch of if else statements to determine what action is taking place.

Index

The index.php is very similar to the admin.php file with the exception it is only used on your sites frontend and will be responsible for showing your visitors the news.

The one difference is this code:

if(!defined('MAX_ROWS_SEARCH')) {
	define('MAX_ROWS_SEARCH', '5');
}

This the number of search results or items you want shown in the list.

Hooks

For our news module I created two hooks and you can see them in the hooks.php file. This file is used to interact with the core system and when a hook is called it will run the code in this file.

As you can see in the constructor I registered the two hooks.

function news_events(&$modules)
{
	$modules->register('admin_tpl_layout_settings', $this, 'add_link');
	$modules->register('index', $this, 'home_page');
}

In those register statements the first parameter is the hook name. The second is the class. And the third is the method in the class. So for the first one I am using the admin_tpl_layout_settings hook, the news_events class, and finally the add_link method inside this class.

That hook is very simple and all it does is display a link in your administration to manage your news. It will be shown under the settings tab.

The second hook is a custom one I created just for this module. I wanted to show the last three news items on the home page. What this does is query the database and add the results to an array which is assigned to the blog variable. With that you can edit your home.tpl template and add a foreach loop to show the latest three.

{foreach from=$blog item=entry}
<div class="newsbox">
	<h3>{$entry.post_title}</h3>
	<div class="entry">
		{$entry.post_content|truncate:150:"...":true}
		<p><a href="modules?mod=news&amp;ID={$entry.ID}">Continue Reading</a></p>
	</div>
</div>
{/foreach}

Templates

The templates folder of this module just contains the templates used in showing the news to a site visitor and for the administration. For the front end I named them “the_entry.tpl” and “the_posts.tpl”. The entry is a single item whereas the_posts is the list.

Wrap Up

In closing I hope this helps you better understand how to build a 68 Classifieds module and how everything all ties together. Also I know this module is very basic so for home work why not try to add more features to it?

You May Also Be Interested In...

Classifieds Twitter Feeder
March 14, 2009

Integrate SimplePie with 68 Classifieds
September 1, 2009

Code Igniter Template Tutorial
July 7, 2008

Project – Suzuki Kawasaki of Gastonia
June 24, 2009

Subscribe Now

If you enjoyed this post, you will definitely enjoy my others. Subscribe to the feed to get instantly updated for those awesome posts soon to come.


blog comments powered by Disqus