NewItems Module (Hot Items!)

LJ Munz
LJ Munz's picture

Joined: 2005-09-12
Posts: 48
Posted: Thu, 2005-11-24 01:11

Ok, so I've been wanting to add functionality to the NewItems module which displays a 'Hot!' next to albums which have over a specified view count (which of course can be set from the admin). I finally spent a little time today working on this and have it almost done with one little snag...

First off here's what I've got accomplished so far. I've added the form input in NewItemsSiteAdmin.tpl so that the view count can be set from the NewItems page within the admin. Next, I added the functionality to NewItemsSiteAdmin.inc to handle this new field. I've also added the neccessary row to the PluginParameterMap table within the database. So, the backend portion works perfectly. I can change this value and it properly saves. Step 1 done!

Next, the actual functionality on the albums page (this is where I've hit my snag). In the modules.inc file, I'm fairly certain I have everything done to make this work. The problem I am having is trying to pull the items viewCount so that I may compare it to the value set within the admin. For example, with the New or Updated functions, they compare their values to $item->getCreationTimestamp & $item->getModificationTimestamp. I've tryed using $item->getviewCount but get a 'Call to undefined function: getviewCount' error when visiting my gallery. So all I really need at this point is the proper way to call the items view count from the module.inc file.

Any help would be greatly appreciated! And of course as soon as this works I'll be more than happy to share this mod with the rest of the community. Thanks in advance!

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Thu, 2005-11-24 01:24

IMO this should be a module of its own, since "Hot" = popularity is unrelated to "new".

the viewcount isn't a item member. the viewcount is stored in a separate map.
see: modules/core/classes/GalleryCoreApi.class

/**
* Get the view counts for many item ids
* @param array the item ids
* @return array object GalleryStatus a status code
* array (itemId => viewCount, ..)
* @static
*/
function fetchItemViewCounts($itemIds) {

 
LJ Munz
LJ Munz's picture

Joined: 2005-09-12
Posts: 48
Posted: Fri, 2005-11-25 19:47

I'm sure your right, this should be a module in it's own. Unfortunately I don't think I know enough about gallery to make this it's own module.

I've found the above reference in the GalleryCoreApi.class but I'm still not sure what to do with that. How do I use this to call the viewcount from the modules.inc file?

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Fri, 2005-11-25 20:05

if you look around in other files of G2, you see ... = GalleryCoreApi::....(...); calls everywhere.
the fetchItemViewCounts function has some docs above it:
@return array object GalleryStatus a status code, array (itemId => viewCount, ..)
means it returns an array and the first element is the $ret status code, the second element the information you're asking for.
and it also describes what it expects as argument (@param). a single array with the item ids for which you want the viewcount.

so in getItemSummaries, call:

$itemIds = array();
foreach ($items as $item) {
$itemIds[] = $item->getId();
}
list ($ret, $viewCounts) = GalleryCoreApi::fetchItemViewCounts($itemIds);
if ($ret->isError()) {
return array($ret->wrap(__FILE__, __LINE__), array());
}
return array(GalleryStatus::success(), $viewCounts);
}

or something like that.
maybe you also need to check the permissions (core.view).

 
LJ Munz
LJ Munz's picture

Joined: 2005-09-12
Posts: 48
Posted: Fri, 2005-11-25 21:52

Ok, I'm definately getting a better understanding of how things work in g2. I must admit I'm no programmer, I focus on design. But I'm doing my best to learn and the help is much appreciated valiant :)

So using the above pulls the viewcounts in, but I'm still unsure of how to call the individule item viewcount to be compared against the limit set in the admin...

if ($hotCount > 0 && $item->$viewCount > $hotCount) {
$summaries[$item->getId()] = '<span class="giHot">'
. $hotString . '</span>';
}

I've tryed a bunch variable combinations here with no success. Do I still need to define the variable $viewCount?

Also, what do you mean I should check the permissions (core.view)?

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Fri, 2005-11-25 22:04

see modules/newitem/module.inc function getItemSummaries on how to compare the values with the db value:
- get the value from the db $treshold
- compare:

$summary = array();
foreach ($viewCounts as $itemId => $views) {
if ($views > $treshold) {
$summary[$itemId] = "This item is hot";
}
}

return $summary;

 
LJ Munz
LJ Munz's picture

Joined: 2005-09-12
Posts: 48
Posted: Fri, 2005-11-25 23:03

I'm receiving the following error

Fatal error: Call to a member function on a non-object in /home/omahanig/public_html/gallery2/modules/core/classes/GalleryTheme.class on line 1377

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Fri, 2005-11-25 23:09

sorry,

return array(GalleryStatus::success(), $summary);