source: extensions/PWG_Stuffs/trunk/modules/MostVisited/main.inc.php @ 12401

Last change on this file since 12401 was 12401, checked in by patdenice, 13 years ago

Best rated module is now compatible with piwigo 2.3

File size: 4.0 KB
Line 
1<?php
2
3if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
4
5global $page, $user, $conf, $template, $pwg_event_handlers, $yop;
6
7if (script_basename() == 'picture'
8  or ($datas['cat_display'] == 'wo_thumb' and !empty($page['items']))
9  or ($datas['cat_display'] == 'w_thumb' and empty($page['items']) and isset($page['category']))
10  or ($datas['cat_display'] == 'selected_cats' and isset($page['category']) and !in_array($page['category']['id'], $datas['cat_selection'])))
11{
12  return false;
13}
14
15$forbidden = get_sql_condition_FandF
16  (
17    array
18      (
19        'forbidden_categories' => 'ic.category_id',
20        'visible_categories' => 'ic.category_id',
21        'visible_images' => 'i.id'
22      ),
23    'AND'
24  );
25
26$query = '
27  SELECT DISTINCT (i.id)
28    FROM '.IMAGES_TABLE.' as i
29      INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON i.id = ic.image_id
30      INNER JOIN '.CATEGORIES_TABLE.' AS c ON ic.category_id = c.id
31    WHERE i.hit > 0';
32
33if (isset($page['category']))
34{
35  $query .= '
36        AND ( c.uppercats LIKE \''.$page['category']['uppercats'].',%\' OR c.id = '.$page['category']['id'].' )
37  ';
38}
39
40$query .= '
41      '.$forbidden.'
42      ORDER BY i.hit DESC, i.file ASC
43    LIMIT 0, '.$datas['nb_images'].'
44  ;';
45
46$pictures = array();
47$selection = array_from_query($query, 'id');
48 
49if (count($selection) > 0)
50{
51  $rank_of = array_flip($selection);
52
53  $query = '
54SELECT *
55  FROM '.IMAGES_TABLE.'
56  WHERE id IN ('.implode(',', $selection).')
57;';
58  $result = pwg_query($query);
59  while ($row = mysql_fetch_assoc($result))
60  {
61    $row['rank'] = $rank_of[ $row['id'] ];
62
63    array_push($pictures, $row);
64  }
65
66  usort($pictures, 'rank_compare');
67  unset($rank_of);
68}
69
70if (count($pictures) > 0)
71{
72  if ($user['show_nb_comments'])
73  {
74    $query = '
75SELECT image_id, COUNT(*) AS nb_comments
76  FROM '.COMMENTS_TABLE.'
77  WHERE validated = \'true\'
78    AND image_id IN ('.implode(',', $selection).')
79  GROUP BY image_id
80;';
81    $nb_comments_of = simple_hash_from_query($query, 'image_id', 'nb_comments');
82  }
83
84  // template thumbnail initialization
85  trigger_action('loc_begin_index_thumbnails', $pictures);
86  $block['thumbnails'] = array();
87
88  foreach ($pictures as $row)
89  {
90    // link on picture.php page
91    $url = duplicate_picture_url(
92          array(
93            'image_id' => $row['id'],
94            'image_file' => $row['file']
95          ),
96          array('start')
97        );
98    $url = add_url_params($url, array('pwgs_mv' => implode(',',$selection)));
99
100    $tpl_var =
101      array(
102        'ID'        => $row['id'],
103        'TN_SRC'    => get_thumbnail_url($row),
104        'TN_ALT'    => $row['file'],
105        'TN_TITLE'  => get_thumbnail_title($row),
106        'ICON_TS'   => get_icon($row['date_available']),
107        'URL'       => $url,
108
109     /* Fields for template-extension usage */
110        'FILE_PATH' => $row['path'],
111        'FILE_POSTED' => $row['date_available'],
112        'FILE_CREATED' => $row['date_creation'],
113        'FILE_DESC' => $row['comment'],
114        'FILE_AUTHOR' => $row['author'],
115        'FILE_HIT' => $row['hit'],
116        'FILE_SIZE' => $row['filesize'],
117        'FILE_WIDTH' => $row['width'],
118        'FILE_HEIGHT' => $row['height'],
119        'FILE_METADATE' => $row['date_metadata_update'],
120        'FILE_HAS_HD' => ($row['has_high'] and $user['enabled_high']=='true') ?
121                  true:false, /* lack of include/functions_picture.inc.php */
122      );
123
124    if ($user['show_nb_hits'])
125    {
126      $tpl_var['NB_HITS'] = $row['hit'];
127    }
128
129    if (isset($row['name']) and $row['name'] != '')
130    {
131      $name = $row['name'];
132    }
133    else
134    {
135      $name = str_replace('_', ' ', get_filename_wo_extension($row['file']));
136    }
137
138    if ( !$user['show_nb_hits'])
139    {
140      $name = '('.$row['hit'].') '.$name;
141    }
142
143    $tpl_var['NAME'] = $name;
144
145    if ( isset($nb_comments_of) )
146    {
147      $tpl_var['NB_COMMENTS'] = (int)@$nb_comments_of[$row['id']];
148    }
149
150    $block['thumbnails'][] = $tpl_var;
151  }
152
153  $block['thumbnails'] = trigger_event('loc_end_index_thumbnails', $block['thumbnails'], $pictures);
154
155  $block['TEMPLATE'] = 'stuffs_thumbnails.tpl';
156}
157
158?>
Note: See TracBrowser for help on using the repository browser.