source: extensions/MostCommented/main.inc.php @ 29931

Revision 27196, 3.2 KB checked in by mistic100, 6 years ago (diff)

Update for Piwigo 2.6

Line 
1<?php
2/*
3Plugin Name: Most Commented
4Version: auto
5Description: Add a "Most Commented" link in "Specials" menu.
6Plugin URI: auto
7Author: P@t
8Author URI: http://www.gauchon.com
9*/
10
11defined('PHPWG_ROOT_PATH') or die('Hacking attempt!');
12
13define('MOSTCOMMENTED_PATH' , PHPWG_PLUGINS_PATH . basename(dirname(__FILE__)) . '/');
14
15
16add_event_handler('init', 'init_mostcommented');
17
18
19function init_mostcommented()
20{
21  global $conf;
22 
23  if ($conf['activate_comments'])
24  {
25    load_language('plugin.lang', MOSTCOMMENTED_PATH);
26
27    add_event_handler('blockmanager_apply' , 'add_link_mostcommented');
28    add_event_handler('loc_end_section_init', 'section_init_mostcommented');
29  }
30}
31
32function add_link_mostcommented($menu_ref_arr)
33{
34  global $conf;
35
36  $menu = &$menu_ref_arr[0];
37  $position = (isset($conf['mostcommented_position']) and is_numeric($conf['mostcommented_position'])) ? $conf['mostcommented_position'] : 3;
38 
39  if (($block = $menu->get_block('mbSpecials')) != null)
40  {
41    array_splice($block->data, $position-1, 0, array('most_commented' =>
42      array(
43        'URL' => make_index_url(array('section' => 'most_commented')),
44        'TITLE' => l10n('displays most commented pictures'),
45        'NAME' => l10n('Most commented')
46        )
47      )
48    );
49  }
50}
51
52function section_init_mostcommented()
53{
54  global $tokens, $page, $conf;
55 
56  if (!in_array('most_commented', $tokens))
57  {
58    return;
59  }
60 
61  $page['section'] = 'most_commented';
62  $page['title'] = '<a href="' . duplicate_index_url() . '">' . l10n('Most commented') . '</a>';
63  $page['section_title'] = '<a href="'.get_gallery_home_url().'">' . l10n('Home') . '</a>'
64    . $conf['level_separator'] . $page['title'];
65
66  $query = '
67SELECT DISTINCT image_id
68    FROM ' . IMAGE_CATEGORY_TABLE . '
69    WHERE ' . 
70      get_sql_condition_FandF(
71        array(
72          'forbidden_categories' => 'category_id',
73          'visible_categories' => 'category_id',
74          'visible_images' => 'image_id',
75          ),
76        '', true
77        )
78    . '
79;';
80
81  $img = query2array($query, null, 'image_id');
82
83  if (empty($img))
84  {
85    $page['items'] = array();
86  }
87  else
88  {
89    $query = '
90SELECT
91    img.id,
92    COUNT(*) AS count
93  FROM ' . IMAGES_TABLE . ' AS img
94    INNER JOIN ' . COMMENTS_TABLE . ' AS com
95    ON com.image_id = img.id
96  WHERE
97    img.id IN (' . implode(',', $img) . ')
98    AND com.validated = "true"
99  GROUP BY img.id
100  ORDER BY count DESC, img.hit DESC
101  LIMIT ' . $conf['top_number'] . '
102;';
103    $page['items'] = query2array($query, null, 'id');
104  }
105 
106  add_event_handler('loc_end_index_thumbnails', 'add_nb_comments_mostcommented');
107}
108
109function add_nb_comments_mostcommented($tpl_thumbnails_var)
110{
111  global $template, $user, $selection;
112
113  // unsed sort order
114  $template->clear_assign('image_orders');
115
116  // display nb comments if hidden by user
117  if (!$user['show_nb_comments'])
118  {
119    $query = '
120SELECT image_id, COUNT(*) AS nb_comments
121  FROM '.COMMENTS_TABLE.'
122  WHERE validated = \'true\'
123    AND image_id IN ('.implode(',', $selection).')
124  GROUP BY image_id
125;';
126    $nb_comments_of = query2array($query, 'image_id', 'nb_comments');
127   
128    foreach ($tpl_thumbnails_var as &$row)
129    {
130      $row['NB_COMMENTS'] = $nb_comments_of[$row['id']];
131    }
132    unset($row);
133  }
134
135  return $tpl_thumbnails_var;
136}
Note: See TracBrowser for help on using the repository browser.