source: extensions/most_downloaded/main.inc.php @ 32016

Last change on this file since 32016 was 31453, checked in by ddtddt, 8 years ago

[extensions] - most_downloaded - First release

File size: 4.7 KB
Line 
1<?php
2/*
3Plugin Name: Most Downloaded
4Version: auto
5Description: Add a "Most Downloaded" link in "Specials" menu. (Plugin download counter must be activate)
6Plugin URI: http://piwigo.org/ext/extension_view.php?eid=827
7Author: ddtddt
8Author URI: http://temmii.com/piwigo/
9*/
10
11// +-----------------------------------------------------------------------+
12// |Most Downloaded for piwigo                                             |
13// +-----------------------------------------------------------------------+
14// | Copyright(C) 2016 ddtddt                    http://temmii.com/piwigo/ |
15// +-----------------------------------------------------------------------+
16// | This program is free software; you can redistribute it and/or modify  |
17// | it under the terms of the GNU General Public License as published by  |
18// | the Free Software Foundation                                          |
19// |                                                                       |
20// | This program is distributed in the hope that it will be useful, but   |
21// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
22// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
23// | General Public License for more details.                              |
24// |                                                                       |
25// | You should have received a copy of the GNU General Public License     |
26// | along with this program; if not, write to the Free Software           |
27// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
28// | USA.                                                                  |
29// +-----------------------------------------------------------------------+
30
31defined('PHPWG_ROOT_PATH') or die('Hacking attempt!');
32
33define('MOSTDOWNLOADED_PATH' , PHPWG_PLUGINS_PATH . basename(dirname(__FILE__)) . '/');
34
35add_event_handler('loading_lang', 'most_downloaded_loading_lang');       
36function most_downloaded_loading_lang(){
37  load_language('plugin.lang', MOSTDOWNLOADED_PATH);
38}
39
40$desac = pwg_db_fetch_assoc(pwg_query("SELECT state FROM " . PLUGINS_TABLE . " WHERE id = 'download_counter';"));
41
42if($desac['state'] != 'active'){
43  if (script_basename() == 'admin'){
44   include_once(dirname(__FILE__).'/initadmin.php');
45  }
46}else{
47
48 
49  add_event_handler('blockmanager_apply' , 'add_link_most_downloaded');
50  add_event_handler('loc_end_section_init', 'section_init_most_downloaded');
51 
52  function add_link_most_downloaded($menu_ref_arr){
53    global $conf;
54    $menu = &$menu_ref_arr[0];
55    $position = (isset($conf['most_downloaded_position']) and is_numeric($conf['most_downloaded_position'])) ? $conf['most_downloaded_position'] : 4;
56    if (($block = $menu->get_block('mbSpecials')) != null){
57     array_splice($block->data, $position-1, 0, array('most_downloaded' =>
58      array(
59        'URL' => make_index_url(array('section' => 'most_downloaded')),
60        'TITLE' => l10n('displays most downloaded Photos'),
61        'NAME' => l10n('Most downloaded')
62        )
63      )
64    );
65   }
66  }
67
68function section_init_most_downloaded()
69{
70  global $tokens, $page, $conf;
71 
72  if (!in_array('most_downloaded', $tokens))
73  {
74    return;
75  }
76 
77  $page['section'] = 'most_downloaded';
78  $page['title'] = '<a href="' . duplicate_index_url() . '">' . l10n('Most downloaded') . '</a>';
79  $page['section_title'] = '<a href="'.get_gallery_home_url().'">' . l10n('Home') . '</a>'
80    . $conf['level_separator'] . $page['title'];
81
82  $query = '
83SELECT DISTINCT image_id
84    FROM ' . IMAGE_CATEGORY_TABLE . '
85    WHERE ' . 
86      get_sql_condition_FandF(
87        array(
88          'forbidden_categories' => 'category_id',
89          'visible_categories' => 'category_id',
90          'visible_images' => 'image_id',
91          ),
92        '', true
93        )
94    . '
95;';
96
97  $img = query2array($query, null, 'image_id');
98
99  if (empty($img))
100  {
101    $page['items'] = array();
102  }
103  else
104  {
105    $query = 'SELECT id, download_counter FROM ' . IMAGES_TABLE . ' AS img WHERE img.id IN (' . implode(',', $img) . ') and download_counter > 0 ORDER BY download_counter DESC, img.hit DESC LIMIT ' . $conf['top_number'] . ';';
106    $page['items'] = query2array($query, null, 'id');
107   
108  add_event_handler('loc_end_index_thumbnails', 'add_nb_most_downloaded');
109}
110
111function add_nb_most_downloaded($tpl_thumbnails_var){
112  global $template, $user, $selection;
113
114  // unsed sort order
115  $template->clear_assign('image_orders');
116
117    $query = 'SELECT id, download_counter FROM '.IMAGES_TABLE.' WHERE id IN ('.implode(',', $selection).');';
118    $nb_download = query2array($query, 'id', 'download_counter');
119   
120    foreach ($tpl_thumbnails_var as &$row)
121    {
122      $row['download_counter'] = $nb_download[$row['id']];
123    }
124    unset($row);
125 
126
127  return $tpl_thumbnails_var;
128}
129
130
131}
132 
133}
134
135
136
137
138
139
140
141   
142
143
144
145
146
Note: See TracBrowser for help on using the repository browser.