CGBlog - field based article counter

Do something cool with CMS? Show us ...
This board is for 'Answers', and the discussion of answers... Not for questions.
Post Reply
Stikki

CGBlog - field based article counter

Post by Stikki »

Hi!

I had to add short of article counter to CGBlog recently, but since CGBlog ain't offering such a thing internally i decided make it UDT like.

So here is nice little script that add's simple counter based on custom field of CGBlog.

It requires two parameters:

- id = entry id
- field = witch custom field of CGBlog counter is

Example how to use:

let's say we call UDT as: cgblogcounter

Into detail template add: {cgblogcounter id=$entry->id field=''}

What this will do is add one to field value and return updated number of view's. If it meets failure, it will announce failure.

Basically that's it. Everytime someone enter your detail view of entry, it will update your counter and show new result. You may also wan't to add counter to summary template, but in that case don't use this script call. In my previous project i used:
{$entry->fieldsbyname.->value} to gain current view's without updating results.

Actual script:

Code: Select all

// Simple blog entry view's counter
global $gCms;
$db =& $gCms->GetDb();

// Check if CGBlog installed
if(!$gCms->modules['CGBlog']['object']) {

	return "CGBlog module not installed!";
}

// Check if parameter id given
$id = '';
if(isset($params['id'])) {
	
	$id = $params['id'];
} else {

	return "Error: no id given";
}

// Check if parameter field given
$field = '';
if(isset($params['field'])) {
	
	$field = $params['field'];
} else {

	return "Error: no field given";
}

// Get fielddef_id from db if it exists, if failure, exit.
$query = "SELECT id FROM ".cms_db_prefix()."module_cgblog_fielddefs WHERE name=?";
$fielddef_id = $db->GetOne($query, array($field));

if(!$fielddef_id) {

	return "Error: invalid field $field";
}

// Actual script. Try to update given field in given entry.
$query = "SELECT value FROM ".cms_db_prefix()."module_cgblog_fieldvals WHERE cgblog_id =? AND fielddef_id=?";
$row = $db->GetOne($query, array($id,$fielddef_id));

if($row) {

	// If field dosen't consist numbers, exit.
	if(!is_numeric($row)) {
	
		return "Error: field $field exists but isn't numeric";
	}

	// Add one to results and update db.
	$row++;
	$query ="UPDATE ".cms_db_prefix()."module_cgblog_fieldvals SET value=? WHERE cgblog_id=? AND fielddef_id=?";
	$db->Execute($query,array($row, $id, $fielddef_id));
	
    return $row;

} else {

	// If we didn't have anything in db yet, make new row.
	$now = trim($db->DBTimeStamp(time()), "'");
	$query = "INSERT INTO ".cms_db_prefix()."module_cgblog_fieldvals (cgblog_id,fielddef_id,value,create_date,modified_date) VALUES (?,?,?,?,?)";
	$dbr = $db->Execute($query, array($id, $fielddef_id, 1, $now, $now));
	
	// If this failed, its bad :(
	if(!$dbr) {
		die('FATAL SQL ERROR: '.$db->ErrorMsg().'<br/>QUERY: '.$db->sql);
	}	

	return 1;
}
I tryed to make this script as failsafe as possible, but there can always be some holes, so use it with your own risk. If you have ideas or bug reports for this UDT, please feel free to catch me from #cms @ freenode.net nick would be Stikki.

I hope some people found this useful, atleast i did. And best part of this script is, it let's you manipulate total view's of article :)

Enjoy

-Stikki-
Last edited by Stikki on Sat Apr 17, 2010 6:49 pm, edited 1 time in total.
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm
Location: Fernie British Columbia, Canada

Re: CGBlog - field based article counter

Post by calguy1000 »

uhm.... Hitcounter would've done it :)
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.
Stikki

Re: CGBlog - field based article counter

Post by Stikki »

Hahah

You are tottaly right, but here is another example how you can do it.

-Stikki-
kendo451

Re: CGBlog - field based article counter

Post by kendo451 »

If you want to be able to sort by "most viewed" as I've had a client recently request, then modify this UDT to store the count in the field cgblog_extra instead of a user defined field.  Now you can display a sorted list of entries using the sortby action baked into CGBlog.

I saved this UDT as increment_counter

Code: Select all

// Simple blog entry views counter
global $gCms;
$db =& $gCms->GetDb();

// Check if CGBlog installed
if(!$gCms->modules['CGBlog']['object']) {

	return "CGBlog module not installed!";
}

// Check if parameter id given
$id = '';
if(isset($params['id'])) {
	
	$id = $params['id'];
} else {

	return "Error: no id given";
}

// Try to update given field in given entry.
$query = "SELECT cgblog_extra FROM ".cms_db_prefix()."module_cgblog WHERE cgblog_id =?";
$row = $db->GetOne($query, array($id));

$row = (int)$row;

	// If field dosen't consist numbers, convert it to zero.
	if(!is_numeric($row)) {
	
		$row == 0;
	}

	// Add one to results and update db.
	$row ++;
	$query ="UPDATE ".cms_db_prefix()."module_cgblog SET cgblog_extra=? WHERE cgblog_id=?";
	$db->Execute($query,array($row, $id));
	
return;
To use this just put {increment_counter id=$entry->id} in your CGBlog detail template at the end of the template.

Then to use CGBlog sort action create a page and put this tag in:

{CGBlog sortby='cgblog_extra'}
Last edited by kendo451 on Mon Jun 14, 2010 1:19 pm, edited 1 time in total.
Post Reply

Return to “Tips and Tricks”