Back to main article

Tweaking things out

There are quite a lot of ways to customize this script, and the customizations may be more or less technical.  Let’s start with the easy ones!

Turn off statistics tracking
By default, the module keeps a record of each time a download as made- it records the member id of the person who downloads the file, the entry_id associated with the file, how many times that member has downloaded that file, and the date of that members last download of a particular file.  If you don’t care about the stats, set $stats = FALSE and save yourself a database query.  This is a harmless tweak- it would be pretty hard to screw anything up.

//  Required.  Set to FALSE if you don't want to keep track of download statistics.
var $stats = TRUE;


Specify allowed groups
By default, the module denies access to anybody with a member_id of 0 - which is everybody but logged in members.  Frankly- I just go with the default on this one.  However, if you want to set it so that only particular member groups have access to the downloads- you need to add each of the member groups you want to have access to the the list.  This is a fairly harmless tweak- about the worst thing you can do wrong is use the wrong member ids- which will grant or restrict access in ways you don’t want.

//  Optional- can have a comma delimited list of allowed user group ids.  If this is not set,
//  downloads are restricted to logged in members.  
//  var $allowed_groups = '';

Use .htaccess to protect your hidden folder
Want even more protection?  If your server setup allows htaccess, I’ve included a handy one that should be uploaded to the HIDDEN FOLDER.  Note- nobody will be able to access the folder via http once this file is uploaded to it (or any folders below it)- so make sure you don’t put it in a folder you want available!  Once it’s up, even with a direct link to the file, folks are out of luck- the file can only be served up via script. 

Screw up level?  Mostly harmless.  It will completely lock out browser access if you put it in the wrong folder- but you’d notice it pretty quickly- and then just delete it via ftp.  So- double check you stuck it in the right folder- other than that, not much danger.

Basic cron- track monthly downloads
So I like to show ‘what’s hot’ in downloads- which for me means the most downloaded files of the month.  Happily, Paul wrote the ExpressionEngine Cron Plugin which lets you schedule a function to be run, er- on a schedule.  Since the plugin was just sitting there begging to be used, I wrote a little function that will clear out your total monthly count in your Download Lock statistics table.

To use it- you’ll need to download and install Paul’s plugin.  The tag you use will then look like:

{exp:cron minute="0" hour="1" day="1" month="*" module="download_lock:monthly_reset"}{/exp:cron}


Put that on a public template- something with some traffic.  Once a month, the total_m_downloads will be reset to 0.  This?  Is harmless- unless you go mucking about with the code in the monthly_reset function.  That would be bad.

**Warning-

Add stats to weblog custom fields
I personally like to be able to show how many times a file has been downloaded using our friend the weblog tag.  Since EE has unlimited custom fields, this is easy to do.  When the stats are adding to the download count in the download_lock table, I just ask it to increment the weblog_data table as well.  Sweet! 

For this to work, you have to do two things.  First- create a custom field to hold your ‘download count’.  If you want to know both the total downloads and the unique downloads- make two custom fields.  Done?  Good.  Now add the custom field id number(s) in place of the example numbers in the file, uncomment the code, and you’re done.  If you want to display the number of downloads, you now have a custom field to do it with.

//  Optional - you can specify a custom weblog field id to hold the total number of downloads.
//  var $total_field_id = '';  

//  Optional - you can specify a custom field id to hold the total number of unique downloads.
//  var $unique_total_field_id = '';

Track monthly downloads
**Warning- it is a world of bad idea to do the following:
Since we already know how to enter the number of downloads into a custom weblog field, if we want one of those custom fields to hold monthly data, all we need is a way to reset one or both of those fields to 0 once a month.  Er- and we already have a ‘fake’ cron plugin and a function to reset the nice, happy, safe Download Lock table.  Do the math- look at the monthly_reset() function.  I even gave you extra variables for duplicate data- in case you want to reset one monthly and let the other keep a total tally.

My set-up- an example of what not to do
Here’s what I wanted to be able to do with this- show the number of members who have downloaded a given file/entry in the weblog tag for each module/plugin/extension.  I also want to be able to show a ‘What’s Hot’ section- for that one, I want the top ‘x’ downloads for the month, based on total downloads (not unique downloads)- using the weblog tag.  Given that what’s being downloaded is software- where new versions are important and should be ‘hot’ even if the downloads aren’t ‘unique’- this makes sense.

So- my set-up:
I use two custom fields- one for total downloads and one for unique downloads- so my only defined, uncommented custom variables are:

//  Optional - you can specify a custom weblog field id to hold the total number of downloads.
var $total_field_id = 7;  

//  Optional - you can specify a custom field id to hold the total number of unique downloads.
var $unique_total_field_id = 8;

Now, I want the number over-written every month for the total downloads only- so in my cron function, the only ‘if’ I uncomment looks like:

if (isset($this->total_field_id))
{
$total_id
= 'field_id_'.$this->total_field_id;        
$DB->query("UPDATE exp_weblog_data set $total_id = 0 WHERE $total_id != ''");
}

Make sense?  And then I did one more thing- I want to be able to sort on those custom fields as numbers- but they’re in the db as strings.  So- I ran a

ALTER TABLE exp_weblog_data MODIFY field_id_7 smallint(4) NOT NULL default '0';
ALTER TABLE exp_weblog_data MODIFY field_id_8 smallint(4) NOT NULL default '0';

And for
my own setup:

ALTER TABLE exp_weblog_data MODIFY field_ft_7 varchar(40) NOT NULL default 'none';
ALTER TABLE exp_weblog_data MODIFY field_ft_8 varchar(40) NOT NULL default 'none';

If it’s not obvious why you’d want to do this, you probably don’t need to be mucking about with writing the stats to a custom weblog field.  Just say’n.

And that IS that.

Back to main article