source: extensions/download_counter/main.inc.php @ 27940

Last change on this file since 27940 was 23774, checked in by plg, 11 years ago

increase counter of each photo inside a batch download

File size: 3.3 KB
Line 
1<?php
2/*
3Plugin Name: Download Counter
4Version: auto
5Description: Count and display number of downloads for each photo
6Plugin URI: http://piwigo.org/ext/extension_view.php?eid=
7Author: plg
8Author URI: http://le-gall.net/pierrick
9*/
10
11if (!defined('PHPWG_ROOT_PATH'))
12{
13  die('Hacking attempt!');
14}
15
16// +-----------------------------------------------------------------------+
17// | Define plugin constants                                               |
18// +-----------------------------------------------------------------------+
19
20global $prefixeTable;
21
22defined('DLCOUNT_ID') or define('DLCOUNT_ID', basename(dirname(__FILE__)));
23define('DLCOUNT_PATH' , PHPWG_PLUGINS_PATH.basename(dirname(__FILE__)).'/');
24define('DLCOUNT_VERSION', 'auto');
25
26// init the plugin
27add_event_handler('init', 'dlcount_init');
28/**
29 * plugin initialization
30 *   - check for upgrades
31 *   - unserialize configuration
32 *   - load language
33 */
34function dlcount_init()
35{ 
36  global $conf, $user, $pwg_loaded_plugins;
37
38  // apply upgrade if needed
39  if (
40    DLCOUNT_VERSION == 'auto' or
41    $pwg_loaded_plugins[DLCOUNT_ID]['version'] == 'auto' or
42    version_compare($pwg_loaded_plugins[DLCOUNT_ID]['version'], DLCOUNT_VERSION, '<')
43  )
44  { 
45    // call install function
46    include_once(DLCOUNT_PATH.'include/install.inc.php');
47    dlcount_install();
48
49    // update plugin version in database
50    if ( $pwg_loaded_plugins[DLCOUNT_ID]['version'] != 'auto' and DLCOUNT_VERSION != 'auto' )
51    {
52      $query = '
53UPDATE '. PLUGINS_TABLE .'
54SET version = "'. DLCOUNT_VERSION .'"
55WHERE id = "'. DLCOUNT_ID .'"';
56      pwg_query($query);
57
58      $pwg_loaded_plugins[DLCOUNT_ID]['version'] = DLCOUNT_VERSION;
59    }
60  }
61
62  load_language('plugin.lang', DLCOUNT_PATH);
63  if (script_basename() == 'picture')
64  {
65    // "Downloads" is a key already available in admin.lang.php
66    load_language('admin.lang');
67  }
68
69  if (script_basename() == 'action' and isset($_GET['download']))
70  {
71    if (!isset($_GET['id'])
72        or !is_numeric($_GET['id'])
73        or !isset($_GET['part'])
74        or !in_array($_GET['part'], array('e','r') ) )
75    {
76      return;
77    }
78
79    $query = '
80UPDATE '.IMAGES_TABLE.'
81  SET download_counter = download_counter + 1
82  WHERE id = '.$_GET['id'].'
83;';
84    pwg_query($query);
85  }
86}
87
88
89add_event_handler('loc_end_picture', 'dlcount_picture');
90function dlcount_picture()
91{
92  global $conf, $template, $picture;
93
94  $template->set_prefilter('picture', 'dlcount_picture_prefilter');
95
96  $template->assign(
97    array(
98      'DOWNLOAD_COUNTER' => $picture['current']['download_counter'],
99      )
100    );
101}
102
103function dlcount_picture_prefilter($content, &$smarty)
104{
105  $search = '{if $display_info.rating_score';
106 
107  $replace = '
108        <div id="DownloadCounter" class="imageInfo">
109                <dt>{\'Downloads\'|@translate}</dt>
110                <dd>{$DOWNLOAD_COUNTER}</dd>
111        </div>
112'.$search;
113 
114  $content = str_replace($search, $replace, $content);
115
116  return $content;
117}
118
119/**
120 * increase counter of each photo inside a batch download
121 */
122add_event_handler('batchdownload_end_zip', 'dlcount_batchdownload', EVENT_HANDLER_PRIORITY_NEUTRAL, 2);
123function dlcount_batchdownload($data, $images)
124{
125  if (count($images) > 0)
126  {
127    $query = '
128UPDATE '.IMAGES_TABLE.'
129  SET download_counter = download_counter + 1
130  WHERE id IN ('.implode(',', $images).')
131;';
132    pwg_query($query);
133  }
134}
135?>
Note: See TracBrowser for help on using the repository browser.