source: extensions/PWG_Stuffs/modules/Random/main.inc.php @ 9581

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

Code optimisation.

File size: 3.9 KB
Line 
1<?php
2
3if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
4
5global $page, $user, $conf, $template;
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    'WHERE'
24  );
25
26$query ='
27SELECT 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    '.$forbidden;
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    ORDER BY RAND(NOW())
42    LIMIT 0, '.$datas['nb_images'].'
43  ;';
44
45$pictures = array();
46$selection = array_from_query($query, 'id');
47 
48if (count($selection) > 0)
49{
50  $rank_of = array_flip($selection);
51
52  $query = '
53SELECT *
54  FROM '.IMAGES_TABLE.'
55  WHERE id IN ('.implode(',', $selection).')
56;';
57  $result = pwg_query($query);
58  while ($row = mysql_fetch_assoc($result))
59  {
60    $row['rank'] = $rank_of[ $row['id'] ];
61
62    array_push($pictures, $row);
63  }
64
65  usort($pictures, 'rank_compare');
66  unset($rank_of);
67}
68
69if (count($pictures) > 0)
70{
71  if ($user['show_nb_comments'])
72  {
73    $query = '
74SELECT image_id, COUNT(*) AS nb_comments
75  FROM '.COMMENTS_TABLE.'
76  WHERE validated = \'true\'
77    AND image_id IN ('.implode(',', $selection).')
78  GROUP BY image_id
79;';
80    $nb_comments_of = simple_hash_from_query($query, 'image_id', 'nb_comments');
81  }
82
83  // template thumbnail initialization
84  trigger_action('loc_begin_index_thumbnails', $pictures);
85  $block['thumbnails'] = array();
86
87  foreach ($pictures as $row)
88  {
89    // link on picture.php page
90    $url = duplicate_picture_url(
91          array(
92            'image_id' => $row['id'],
93            'image_file' => $row['file']
94          ),
95          array('start')
96        );
97    $url = add_url_params($url, array('pwgs_ra' => implode(',',$selection)));
98
99    $tpl_var =
100      array(
101        'ID'        => $row['id'],
102        'TN_SRC'    => get_thumbnail_url($row),
103        'TN_ALT'    => $row['file'],
104        'TN_TITLE'  => get_thumbnail_title($row),
105        'ICON_TS'   => get_icon($row['date_available']),
106        'URL'       => $url,
107
108     /* Fields for template-extension usage */
109        'FILE_PATH' => $row['path'],
110        'FILE_POSTED' => $row['date_available'],
111        'FILE_CREATED' => $row['date_creation'],
112        'FILE_DESC' => $row['comment'],
113        'FILE_AUTHOR' => $row['author'],
114        'FILE_HIT' => $row['hit'],
115        'FILE_SIZE' => $row['filesize'],
116        'FILE_WIDTH' => $row['width'],
117        'FILE_HEIGHT' => $row['height'],
118        'FILE_METADATE' => $row['date_metadata_update'],
119        'FILE_HAS_HD' => ($row['has_high'] and $user['enabled_high']=='true') ?
120                  true:false, /* lack of include/functions_picture.inc.php */
121      );
122
123    if ($user['show_nb_hits'])
124    {
125      $tpl_var['NB_HITS'] = $row['hit'];
126    }
127
128    if ($conf['show_thumbnail_caption'])
129    {// name of the picture
130      if (isset($row['name']) and $row['name'] != '')
131      {
132        $name = $row['name'];
133      }
134      else
135      {
136        $name = str_replace('_', ' ', get_filename_wo_extension($row['file']));
137      }
138
139      $tpl_var['NAME'] = $name;
140    }
141
142    if ( isset($nb_comments_of) )
143    {
144      $tpl_var['NB_COMMENTS'] = (int)@$nb_comments_of[$row['id']];
145    }
146
147    $block['thumbnails'][] = $tpl_var;
148  }
149
150  $block['thumbnails'] = trigger_event('loc_end_index_thumbnails', $block['thumbnails'], $pictures);
151
152  $block['TEMPLATE'] = 'stuffs_thumbnails.tpl';
153}
154
155?>
Note: See TracBrowser for help on using the repository browser.