by
KK » Mon Sep 21, 2015 2:12 am
Hi,
I think we can build something like that using existing Couch features. Allow me to explain -
In a nutshell, the idea is to make each downloadable file be represented by a cloned-page in Couch.
We can keep track of how many times a cloned-page has been accessed (
viewtopic.php?p=18532#p18532) so that effectively would give us the count of how many times the file has been downloaded.
The details -
Use a clonable template (say, named 'downloads.php').
Define within it one type 'securefile' region to hold the downloadable file each page would represent. Define another region to track the hit counts -
- Code: Select all
<cms:template title='Downloads' clonable='1' >
<cms:editable
name='myfile'
allowed_ext='zip, rar'
max_size='20048'
type='securefile'
/>
<cms:editable label='Downloads' name='page_hits' type='text' search_type='integer' />
</cms:template>
Now create a cloned page for each file you want to be available for download.
On the front end you can list all files as follows -
- Code: Select all
<cms:if k_is_list>
<cms:pages>
<cms:show_securefile 'myfile' >
<a href="<cms:show k_page_link />"><cms:show file_name /></a> (<cms:size_format file_size />)<br/>
</cms:show_securefile>
</cms:pages>
</cms:if>
Please notice that upon clicking any of the files listed above we reach the 'page_view' of the template. In this page_view, we'll increment the hit count and automatically make the download box to popup -
- Code: Select all
<cms:if k_is_page>
<cms:no_cache />
<cms:php>
// identify bots
global $CTX;
if( isset($_SERVER['HTTP_USER_AGENT']) && preg_match('/bot|crawl|slurp|spider/i', $_SERVER['HTTP_USER_AGENT']) ){
$CTX->set( 'is_bot', '1', 'global' );
}
</cms:php>
<!-- increase hit count -->
<cms:if "<cms:not is_bot />">
<cms:db_persist
_masterpage=k_template_name
_page_id=k_page_id
_mode='edit'
page_hits="<cms:add page_hits '1' />"
/>
</cms:if>
<!-- and redirect to download the file -->
<cms:show_securefile 'myfile' >
<cms:redirect "<cms:cloak_url link=file_id />" />
</cms:show_securefile>
</cms:if>
Please let me know if this helps.