| 35 | | c.*, nb_images, date_last, max_date_last, count_images, count_categories |
| 36 | | FROM '.CATEGORIES_TABLE.' c INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.' |
| 37 | | ON id = cat_id and user_id = '.$user['id'].' |
| 38 | | WHERE date_last >= '.pwg_db_get_recent_period_expression($user['recent_period']).' |
| 39 | | '.get_sql_condition_FandF |
| 40 | | ( |
| 41 | | array |
| 42 | | ( |
| 43 | | 'visible_categories' => 'id', |
| 44 | | ), |
| 45 | | 'AND' |
| 46 | | ).' |
| | 33 | c.*, |
| | 34 | user_representative_picture_id, |
| | 35 | nb_images, |
| | 36 | date_last, |
| | 37 | max_date_last, |
| | 38 | count_images, |
| | 39 | count_categories |
| | 40 | FROM '.CATEGORIES_TABLE.' c |
| | 41 | INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.' ucc ON id = cat_id AND user_id = '.$user['id']; |
| | 42 | |
| | 43 | if ('recent_cats' == $page['section']) |
| | 44 | { |
| | 45 | $query.= ' |
| | 46 | WHERE date_last >= '.pwg_db_get_recent_period_expression($user['recent_period']); |
| | 47 | } |
| | 48 | else |
| | 49 | { |
| | 50 | $query.= ' |
| | 51 | WHERE id_uppercat '.(!isset($page['category']) ? 'is NULL' : '= '.$page['category']['id']); |
| | 52 | } |
| | 53 | |
| | 54 | $query.= get_sql_condition_FandF( |
| | 55 | array( |
| | 56 | 'visible_categories' => 'id', |
| | 57 | ), |
| | 58 | 'AND' |
| | 59 | ); |
| | 60 | |
| | 61 | if ('recent_cats' != $page['section']) |
| | 62 | { |
| | 63 | $query.= ' |
| | 64 | ORDER BY rank'; |
| | 65 | } |
| | 66 | |
| | 67 | $query.= ' |
| 48 | | } |
| 49 | | else |
| 50 | | { |
| 51 | | // $user['forbidden_categories'] including with USER_CACHE_CATEGORIES_TABLE |
| 52 | | $query = ' |
| 53 | | SELECT |
| 54 | | c.*, nb_images, date_last, max_date_last, count_images, count_categories |
| 55 | | FROM '.CATEGORIES_TABLE.' c INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.' |
| 56 | | ON id = cat_id and user_id = '.$user['id'].' |
| 57 | | WHERE id_uppercat '. |
| 58 | | (!isset($page['category']) ? 'is NULL' : '= '.$page['category']['id']).' |
| 59 | | '.get_sql_condition_FandF |
| 60 | | ( |
| 61 | | array |
| 62 | | ( |
| 63 | | 'visible_categories' => 'id', |
| 64 | | ), |
| 65 | | 'AND' |
| 66 | | ).' |
| 67 | | ORDER BY rank |
| 68 | | ;'; |
| 69 | | } |
| 86 | | {// searching a random representant among elements in sub-categories |
| 87 | | if ($row['count_images']>0) |
| 88 | | { |
| 89 | | $query = ' |
| 90 | | SELECT image_id |
| 91 | | FROM '.CATEGORIES_TABLE.' AS c INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic |
| 92 | | ON ic.category_id = c.id'; |
| 93 | | $query.= ' |
| 94 | | WHERE (c.id='.$row['id'].' OR uppercats LIKE \''.$row['uppercats'].',%\')' |
| 95 | | .get_sql_condition_FandF |
| 96 | | ( |
| 97 | | array |
| 98 | | ( |
| 99 | | 'forbidden_categories' => 'c.id', |
| 100 | | 'visible_categories' => 'c.id', |
| 101 | | 'visible_images' => 'image_id' |
| 102 | | ), |
| 103 | | "\n AND" |
| 104 | | ).' |
| 105 | | ORDER BY '.DB_RANDOM_FUNCTION.'() |
| 106 | | LIMIT 1 |
| 107 | | ;'; |
| 108 | | $subresult = pwg_query($query); |
| 109 | | if (pwg_db_num_rows($subresult) > 0) |
| 110 | | { |
| 111 | | list($image_id) = pwg_db_fetch_row($subresult); |
| 112 | | } |
| 113 | | } |
| | 89 | { |
| | 90 | // searching a random representant among elements in sub-categories |
| | 91 | $image_id = get_random_image_in_category($row); |
| 205 | | $thumbnail_src_of[$row['id']] = get_thumbnail_url($row); |
| 206 | | } |
| | 189 | if ($row['level'] <= $user['level']) |
| | 190 | { |
| | 191 | $thumbnail_src_of[$row['id']] = get_thumbnail_url($row); |
| | 192 | } |
| | 193 | else |
| | 194 | { |
| | 195 | // problem: we must not display the thumbnail of a photo which has a |
| | 196 | // higher privacy level than user privacy level |
| | 197 | // |
| | 198 | // * what is the represented category? |
| | 199 | // * find a random photo matching user permissions |
| | 200 | // * register it at user_representative_picture_id |
| | 201 | // * set it as the representative_picture_id for the category |
| | 202 | |
| | 203 | foreach ($categories as &$category) |
| | 204 | { |
| | 205 | if ($row['id'] == $category['representative_picture_id']) |
| | 206 | { |
| | 207 | if ($category['count_images']>0) |
| | 208 | { |
| | 209 | // searching a random representant among elements in sub-categories |
| | 210 | $image_id = get_random_image_in_category($category); |
| | 211 | |
| | 212 | if (isset($image_id)) |
| | 213 | { |
| | 214 | if (!in_array($image_id, $image_ids)) |
| | 215 | { |
| | 216 | array_push($new_image_ids, $image_id); |
| | 217 | } |
| | 218 | |
| | 219 | $user_representative_updates_for[ $user['id'].'#'.$category['id'] ] = $image_id; |
| | 220 | |
| | 221 | $category['representative_picture_id'] = $image_id; |
| | 222 | } |
| | 223 | } |
| | 224 | } |
| | 225 | } |
| | 226 | unset($category); |
| | 227 | } |
| | 228 | } |
| | 229 | |
| | 230 | if (count($new_image_ids) > 0) |
| | 231 | { |
| | 232 | $query = ' |
| | 233 | SELECT id, path, tn_ext |
| | 234 | FROM '.IMAGES_TABLE.' |
| | 235 | WHERE id IN ('.implode(',', $new_image_ids).') |
| | 236 | ;'; |
| | 237 | $result = pwg_query($query); |
| | 238 | while ($row = pwg_db_fetch_assoc($result)) |
| | 239 | { |
| | 240 | $thumbnail_src_of[$row['id']] = get_thumbnail_url($row); |
| | 241 | } |
| | 242 | } |
| | 243 | } |
| | 244 | |
| | 245 | if (count($user_representative_updates_for)) |
| | 246 | { |
| | 247 | $updates = array(); |
| | 248 | |
| | 249 | foreach ($user_representative_updates_for as $user_cat => $image_id) |
| | 250 | { |
| | 251 | list($user_id, $cat_id) = explode('#', $user_cat); |
| | 252 | |
| | 253 | array_push( |
| | 254 | $updates, |
| | 255 | array( |
| | 256 | 'user_id' => $user_id, |
| | 257 | 'cat_id' => $cat_id, |
| | 258 | 'user_representative_picture_id' => $image_id, |
| | 259 | ) |
| | 260 | ); |
| | 261 | } |
| | 262 | |
| | 263 | mass_updates( |
| | 264 | USER_CACHE_CATEGORIES_TABLE, |
| | 265 | array( |
| | 266 | 'primary' => array('user_id', 'cat_id'), |
| | 267 | 'update' => array('user_representative_picture_id') |
| | 268 | ), |
| | 269 | $updates |
| | 270 | ); |