Ignore:
Timestamp:
Jun 19, 2013, 2:43:41 PM (11 years ago)
Author:
mistic100
Message:

all collections are "active", display a menu when adding to a collection

File:
1 edited

Legend:

Unmodified
Added
Removed
  • extensions/UserCollections/include/events.inc.php

    r21382 r23361  
    2020    if (in_array(@$tokens[1], array('edit','view','list')))
    2121    {
    22        $page['sub_section'] = $tokens[1];
     22      $page['sub_section'] = $tokens[1];
     23      if ($tokens[1]=='edit' and isset($conf['GThumb']) && is_array($conf['GThumb']))
     24      {
     25        $conf['GThumb']['big_thumb'] = false; // big thumb is buggy with removes
     26      }
    2327    }
    2428    else
     
    5559// | CATEGORY PAGE
    5660// +-----------------------------------------------------------------------+
    57 /* toggle an image, in case of no javascript or first call (must create the collection) */
    58 function user_collections_index_actions()
    59 {
    60   if (is_a_guest()) return;
    61  
    62   global $page, $UserCollection;
    63      
    64   // add image to collection list
    65   if ( isset($_GET['collection_toggle']) and  preg_match('#^[0-9]+$#', $_GET['collection_toggle']) )
    66   {
    67     if (empty($UserCollection))
    68     {
    69       $UserCollection = new UserCollection(get_current_collection_id(true));
    70     }
    71     $UserCollection->toggleImage($_GET['collection_toggle']);
    72     redirect(duplicate_index_url(array(), array('collection_toggle')));
    73   }
    74 }
    75 
    7661/* add buttons on thumbnails list */
    7762function user_collections_thumbnails_list($tpl_thumbnails_var, $pictures)
     
    7964  if (is_a_guest()) return $tpl_thumbnails_var;
    8065 
    81   global $page, $template, $UserCollection;
     66  global $page, $template, $user;
    8267 
    8368  // the content is different on collection edition page and no button on batch downloader set edition page
     
    8772  }
    8873 
    89   // get existing collections
    90   $col_id = get_current_collection_id(false);
    91   if (empty($UserCollection) and $col_id !== false)
    92   {
    93     $UserCollection = new UserCollection($col_id);
    94     $collection = $UserCollection->getImages();
    95   }
    96   else if (!empty($UserCollection))
    97   {
    98     $collection = $UserCollection->getImages();
    99   }
    100   else
    101   {
    102     $collection = array();
    103   }
    104  
    105   // if the collection doesn't exists we don't use AJAX to force menu refresh
    106   if ($col_id === false)
    107   {
    108     $template->assign('NO_AJAX', true);
    109   }
    110   else
    111   {
    112     $template->assign('AJAX_COL_ID', $col_id );
    113   }
    114  
    115   // template vars
    116   $url = duplicate_index_url(array(), array('collection_toggle'));
     74  $image_ids = array_map(create_function('$i', 'return $i["id"];'), $pictures);
     75 
     76  // get collections for each picture
     77  $query = '
     78SELECT
     79    image_id,
     80    GROUP_CONCAT(col_id) AS col_ids
     81  FROM '.COLLECTION_IMAGES_TABLE.'
     82  WHERE col_id IN (
     83      SELECT id
     84      FROM '.COLLECTIONS_TABLE.'
     85      WHERE user_id = '.$user['id'].'
     86    )
     87    AND image_id IN('.implode(',', $image_ids).')
     88  GROUP BY image_id
     89;';
     90  $image_collections = simple_hash_from_query($query, 'image_id', 'col_ids');
    11791 
    11892  foreach ($tpl_thumbnails_var as &$thumbnail)
    11993  {
    120     if (in_array($thumbnail['id'], $collection))
    121     {
    122       $thumbnail['COLLECTION_SELECTED'] = true;
    123     }
    124     $thumbnail['COLLECTION_TOGGLE_URL'] = add_url_params($url, array('collection_toggle'=>$thumbnail['id']));
     94    $thumbnail['COLLECTIONS'] = @$image_collections[ $thumbnail['id'] ];
    12595  }
    12696  unset($thumbnail);
    12797 
     98  // get all collections
     99  $query = '
     100SELECT id, name, nb_images, active
     101  FROM '.COLLECTIONS_TABLE.'
     102  WHERE user_id = '.$user['id'].'
     103  ORDER BY name ASC
     104;';
     105  $collections = hash_from_query($query, 'id');
     106 
    128107  $template->assign(array(
     108    'COLLECTIONS' => $collections,
    129109    'USER_COLLEC_PATH' => USER_COLLEC_PATH,
    130110    ));
    131111 
    132112  // thumbnails buttons
    133   $template->set_prefilter('index_thumbnails', 'user_collections_thumbnails_list_prefilter');
     113  $template->set_prefilter('index_thumbnails', 'user_collections_thumbnails_list_button');
     114  $template->set_prefilter('index', 'user_collections_thumbnails_list_cssjs');
    134115 
    135116  return $tpl_thumbnails_var;
    136117}
    137118
    138 function user_collections_thumbnails_list_prefilter($content, &$smarty)
    139 {
    140   // add links
     119// add links
     120function user_collections_thumbnails_list_button($content, &$smarty)
     121{
    141122  $search = '#(<li>|<li class="gthumb">)#';
    142123  $replace = '$1
    143 {strip}<a class="addCollection" href="{$thumbnail.COLLECTION_TOGGLE_URL}" data-id="{$thumbnail.id}" data-stat="{if $thumbnail.COLLECTION_SELECTED}remove{else}add{/if}" rel="nofollow">
    144 <span class="uc_remove" {if not $thumbnail.COLLECTION_SELECTED}style="display:none;"{/if}>
    145 {\'Remove from collection\'|@translate}&nbsp;<img src="{$ROOT_URL}{$USER_COLLEC_PATH}template/resources/image_delete.png" title="{\'Remove from collection\'|@translate}">
    146 </span>
    147 <span class="uc_add" {if $thumbnail.COLLECTION_SELECTED}style="display:none;"{/if}>
    148 {\'Add to collection\'|@translate}&nbsp;<img src="{$ROOT_URL}{$USER_COLLEC_PATH}template/resources/image_add.png" title="{\'Add to collection\'|@translate}">
    149 </span>
     124{strip}<a class="addCollection" data-id="{$thumbnail.id}" data-cols="[{$thumbnail.COLLECTIONS}]" rel="nofollow">
     125{if not $UC_IN_EDIT}
     126{\'Add to collection\'|@translate}&nbsp;<img src="{$ROOT_URL}{$USER_COLLEC_PATH}template/resources/image_add.png" alt="[+]">
     127{else}
     128{\'Remove from collection\'|@translate}&nbsp;<img src="{$ROOT_URL}{$USER_COLLEC_PATH}template/resources/image_delete.png" alt="[+]">
     129{/if}
    150130</a>{/strip}';
    151 
    152   // custom CSS and AJAX request
     131 
     132  return preg_replace($search, $replace, $content);
     133}
     134
     135// add css & js and menu
     136function user_collections_thumbnails_list_cssjs($content, &$smarty)
     137{
    153138  $content.= file_get_contents(USER_COLLEC_PATH.'template/thumbnails_css_js.tpl');
    154  
    155   return preg_replace($search, $replace, $content);
     139  return $content;
    156140}
    157141
     
    165149  if (is_a_guest()) return;
    166150 
    167   global $template, $picture, $UserCollection;
    168  
    169   // add image to collection list
    170   if ( isset($_GET['action']) and $_GET['action'] == 'collection_toggle' )
    171   {
    172     if (empty($UserCollection))
    173     {
    174       $UserCollection = new UserCollection(get_current_collection_id(true));
    175     }
    176    
    177     $UserCollection->toggleImage($picture['current']['id']);
    178     redirect(duplicate_picture_url());
    179   }
    180  
    181   // get existing collection
    182   if (empty($UserCollection) and ($col_id = get_current_collection_id(false)) !== false)
    183   {
    184     $UserCollection = new UserCollection($col_id);
    185     $collection = $UserCollection->isInSet($picture['current']['id']);
    186   }
    187   else if (!empty($UserCollection))
    188   {
    189     $collection = $UserCollection->isInSet($picture['current']['id']);
    190   }
    191   else
    192   {
    193     $collection = false;
    194   } 
    195  
    196   $url = add_url_params(duplicate_picture_url(), array('action'=>'collection_toggle'));   
    197  
    198   $button = '
    199 <a href="'.$url.'" title="'.($collection?l10n('Remove from collection'):l10n('Add to collection')).'" class="pwg-state-default pwg-button" rel="nofollow">
    200   <span class="pwg-icon" style="background:url(\''.get_root_url().USER_COLLEC_PATH.'template/resources/image_'.($collection?'delete':'add').'.png\') center center no-repeat;"> </span>
    201   <span class="pwg-button-text">'.($collection?l10n('Remove from collection'):l10n('Add to collection')).'</span>
    202 </a>';
    203   // $template->add_picture_button($button, 50);
    204   $template->concat('PLUGIN_PICTURE_ACTIONS', $button);
     151  global $template, $picture, $user;
     152 
     153  // get collections for this picture
     154  $query = '
     155SELECT GROUP_CONCAT(col_id)
     156  FROM '.COLLECTION_IMAGES_TABLE.'
     157  WHERE col_id IN (
     158      SELECT id
     159      FROM '.COLLECTIONS_TABLE.'
     160      WHERE user_id = '.$user['id'].'
     161    )
     162    AND image_id = '.$picture['current']['id'].'
     163  GROUP BY image_id
     164;';
     165  list($image_collections) = pwg_db_fetch_row(pwg_query($query));
     166 
     167  // get all collections
     168  $query = '
     169SELECT id, name, nb_images, active
     170  FROM '.COLLECTIONS_TABLE.'
     171  WHERE user_id = '.$user['id'].'
     172  ORDER BY name ASC
     173;';
     174  $collections = hash_from_query($query, 'id');
     175 
     176  $template->assign(array(
     177    'CURRENT_COLLECTIONS' => $image_collections,
     178    'COLLECTIONS' => $collections,
     179    'USER_COLLEC_PATH' => USER_COLLEC_PATH,
     180    'USER_COLLEC_ABS_PATH' => realpath(USER_COLLEC_PATH).'/',
     181    'IN_PICTURE' => true,
     182    ));
     183 
     184  // toolbar button
     185  $template->set_filename('usercol_button', realpath(USER_COLLEC_PATH.'template/picture_button.tpl'));
     186  $button = $template->parse('usercol_button', true);
     187  $template->add_picture_button($button, 50);
    205188}
    206189
     
    225208  $max = 6;
    226209 
    227   global $template, $page, $conf, $user, $UserCollection;
     210  global $template, $page, $conf, $user;
    228211  $menu = &$menu_ref_arr[0];
    229  
    230   // the editable counter is for the active collection, except if we are currently editing a collection
    231   $col_in_edit = 0;
    232   if ( @$page['section'] == 'collections' and @$page['sub_section']=='edit' and !empty($page['col_id']) )
    233   {
    234     $col_in_edit = $page['col_id'];
    235   }
    236212 
    237213  if (($block = $menu->get_block('mbUserCollection')) != null)
     
    250226    for ($i=0; $i<$max && $i<count($collections); $i++)
    251227    {
    252       $collections[$i]['count_handler'] = $col_in_edit!=0 ? $collections[$i]['id']==$col_in_edit : $collections[$i]['active'];
    253228      $collections[$i]['U_EDIT'] = USER_COLLEC_PUBLIC.'edit/'.$collections[$i]['id'];
    254       array_push($data['collections'], $collections[$i]);
     229      $data['collections'][] = $collections[$i];
    255230    }
    256231   
Note: See TracChangeset for help on using the changeset viewer.