Page 1 of 1

CGBlog - field based article counter

Posted: Sat Apr 17, 2010 6:47 pm
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-

Re: CGBlog - field based article counter

Posted: Sat Apr 17, 2010 6:50 pm
by calguy1000
uhm.... Hitcounter would've done it :)

Re: CGBlog - field based article counter

Posted: Sat Apr 17, 2010 6:58 pm
by Stikki
Hahah

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

-Stikki-

Re: CGBlog - field based article counter

Posted: Mon Jun 14, 2010 12:30 pm
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'}