Ignore:
Timestamp:
Nov 3, 2011, 4:43:03 PM (13 years ago)
Author:
plg
Message:

feature 2352 added: pwg.categories.getList now returns a tn_url parameter.
This is the album thumbnail.

Warning: if the API method is called with $paramspublic, the album
thumbnail may be not accurate. The thumbnail can be viewed by the connected
user, but maybe not by the guest. Changing the filtering method would be too
complicated for now. We will simply avoid to persist the
user_representative_picture_id in the database if $paramspublic

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2.3/include/ws_functions.inc.php

    r12175 r12537  
    491491    comment,
    492492    nb_images, count_images AS total_nb_images,
     493    user_representative_picture_id, count_images,
    493494    date_last, max_date_last, count_categories AS nb_categories
    494495  FROM '.CATEGORIES_TABLE.'
     
    499500  $result = pwg_query($query);
    500501
     502  // management of the album thumbnail -- starts here
     503  $image_ids = array();
     504  $categories = array();
     505  $user_representative_updates_for = array();
     506  // management of the album thumbnail -- stops here
     507 
    501508  $cats = array();
    502509  while ($row = pwg_db_fetch_assoc($result))
     
    535542      );
    536543
     544    // management of the album thumbnail -- starts here
     545    //
     546    // on branch 2.3, the algorithm is duplicated from
     547    // include/category_cats, but we should use a common code for Piwigo 2.4
     548    //
     549    // warning : if the API method is called with $params['public'], the
     550    // album thumbnail may be not accurate. The thumbnail can be viewed by
     551    // the connected user, but maybe not by the guest. Changing the
     552    // filtering method would be too complicated for now. We will simply
     553    // avoid to persist the user_representative_picture_id in the database
     554    // if $params['public']
     555    if (!empty($row['user_representative_picture_id']))
     556    {
     557      $image_id = $row['user_representative_picture_id'];
     558    }
     559    else if (!empty($row['representative_picture_id']))
     560    { // if a representative picture is set, it has priority
     561      $image_id = $row['representative_picture_id'];
     562    }
     563    else if ($conf['allow_random_representative'])
     564    {
     565      // searching a random representant among elements in sub-categories
     566      $image_id = get_random_image_in_category($row);
     567    }
     568    else
     569    { // searching a random representant among representant of sub-categories
     570      if ($row['count_categories']>0 and $row['count_images']>0)
     571      {
     572        $query = '
     573  SELECT representative_picture_id
     574    FROM '.CATEGORIES_TABLE.' INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.'
     575    ON id = cat_id and user_id = '.$user['id'].'
     576    WHERE uppercats LIKE \''.$row['uppercats'].',%\'
     577      AND representative_picture_id IS NOT NULL'
     578          .get_sql_condition_FandF
     579          (
     580            array
     581            (
     582              'visible_categories' => 'id',
     583              ),
     584            "\n  AND"
     585            ).'
     586    ORDER BY '.DB_RANDOM_FUNCTION.'()
     587    LIMIT 1
     588  ;';
     589        $subresult = pwg_query($query);
     590        if (pwg_db_num_rows($subresult) > 0)
     591        {
     592          list($image_id) = pwg_db_fetch_row($subresult);
     593        }
     594      }
     595    }
     596   
     597    if (isset($image_id))
     598    {
     599      if ($conf['representative_cache_on_subcats'] and $row['user_representative_picture_id'] != $image_id)
     600      {
     601        $user_representative_updates_for[ $user['id'].'#'.$row['id'] ] = $image_id;
     602      }
     603   
     604      $row['representative_picture_id'] = $image_id;
     605      array_push($image_ids, $image_id);
     606      array_push($categories, $row);
     607    }
     608    unset($image_id);
     609    // management of the album thumbnail -- stops here
     610
     611
    537612    array_push($cats, $row);
    538613  }
    539614  usort($cats, 'global_rank_compare');
     615
     616  // management of the album thumbnail -- starts here
     617  if (count($categories) > 0)
     618  {
     619    $thumbnail_src_of = array();
     620    $new_image_ids = array();
     621
     622    $query = '
     623SELECT id, path, tn_ext, level
     624  FROM '.IMAGES_TABLE.'
     625  WHERE id IN ('.implode(',', $image_ids).')
     626;';
     627    $result = pwg_query($query);
     628    while ($row = pwg_db_fetch_assoc($result))
     629    {
     630      if ($row['level'] <= $user['level'])
     631      {
     632        $thumbnail_src_of[$row['id']] = get_thumbnail_url($row);
     633      }
     634      else
     635      {
     636        // problem: we must not display the thumbnail of a photo which has a
     637        // higher privacy level than user privacy level
     638        //
     639        // * what is the represented category?
     640        // * find a random photo matching user permissions
     641        // * register it at user_representative_picture_id
     642        // * set it as the representative_picture_id for the category
     643       
     644        foreach ($categories as &$category)
     645        {
     646          if ($row['id'] == $category['representative_picture_id'])
     647          {
     648            // searching a random representant among elements in sub-categories
     649            $image_id = get_random_image_in_category($category);
     650           
     651            if (isset($image_id) and !in_array($image_id, $image_ids))
     652            {
     653              array_push($new_image_ids, $image_id);
     654            }
     655           
     656            if ($conf['representative_cache_on_level'])
     657            {
     658              $user_representative_updates_for[ $user['id'].'#'.$category['id'] ] = $image_id;
     659            }
     660           
     661            $category['representative_picture_id'] = $image_id;
     662          }
     663        }
     664        unset($category);
     665      }
     666    }
     667   
     668    if (count($new_image_ids) > 0)
     669    {
     670      $query = '
     671SELECT id, path, tn_ext
     672  FROM '.IMAGES_TABLE.'
     673  WHERE id IN ('.implode(',', $new_image_ids).')
     674;';
     675      $result = pwg_query($query);
     676      while ($row = pwg_db_fetch_assoc($result))
     677      {
     678        $thumbnail_src_of[$row['id']] = get_thumbnail_url($row);
     679      }
     680    }
     681  }
     682
     683  // compared to code in include/category_cats, we only persist the new
     684  // user_representative if we have used $user['id'] and not the guest id,
     685  // or else the real guest may see thumbnail that he should not
     686  if (!$params['public'] and count($user_representative_updates_for))
     687  {
     688    $updates = array();
     689 
     690    foreach ($user_representative_updates_for as $user_cat => $image_id)
     691    {
     692      list($user_id, $cat_id) = explode('#', $user_cat);
     693   
     694      array_push(
     695        $updates,
     696        array(
     697          'user_id' => $user_id,
     698          'cat_id' => $cat_id,
     699          'user_representative_picture_id' => $image_id,
     700          )
     701        );
     702    }
     703
     704    mass_updates(
     705      USER_CACHE_CATEGORIES_TABLE,
     706      array(
     707        'primary' => array('user_id', 'cat_id'),
     708        'update'  => array('user_representative_picture_id')
     709        ),
     710      $updates
     711      );
     712  }
     713
     714  foreach ($cats as &$cat)
     715  {
     716    foreach ($categories as $category)
     717    {
     718      if ($category['id'] == $cat['id'])
     719      {
     720        $cat['tn_url'] = $thumbnail_src_of[$category['representative_picture_id']];
     721      }
     722    }
     723    // we don't want them in the output
     724    unset($cat['user_representative_picture_id']);
     725    unset($cat['count_images']);
     726  }
     727  unset($cat); 
     728  // management of the album thumbnail -- stops here
    540729
    541730  if ($params['tree_output'])
Note: See TracChangeset for help on using the changeset viewer.