Changeset 28542


Ignore:
Timestamp:
May 27, 2014, 12:03:57 AM (10 years ago)
Author:
mistic100
Message:

feature 3077 : factorize code for categories cache (TODO for other collections) + fix incorrect categories list for dissociation

Location:
trunk
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/admin/batch_manager_global.php

    r28533 r28542  
    520520}
    521521
    522 $template->assign( 'filter_category_selected', $selected_category);
    523 
    524 // Dissociate from a category : categories listed for dissociation can only
    525 // represent virtual links. We can't create orphans. Links to physical
    526 // categories can't be broken.
    527 if (count($page['cat_elements_id']) > 0)
    528 {
    529   $query = '
    530 SELECT
    531     DISTINCT(category_id) AS id,
    532     c.name,
    533     c.uppercats,
    534     c.global_rank
    535   FROM '.IMAGE_CATEGORY_TABLE.' AS ic
    536     JOIN '.CATEGORIES_TABLE.' AS c ON c.id = ic.category_id
    537     JOIN '.IMAGES_TABLE.' AS i ON i.id = ic.image_id
    538   WHERE ic.image_id IN ('.implode(',', $page['cat_elements_id']).')
    539     AND (
    540       ic.category_id != i.storage_category_id
    541       OR i.storage_category_id IS NULL
    542     )
    543 ;';
    544   display_select_cat_wrapper($query, array(), 'dissociate_options', true);
    545 }
     522$template->assign('filter_category_selected', $selected_category);
     523
    546524
    547525if (count($page['cat_elements_id']) > 0)
  • trunk/admin/picture_modify.php

    r28532 r28542  
    4343  WHERE representative_picture_id = '.$_GET['image_id'].'
    4444;';
    45 $represent_options_selected = query2array($query, null, 'id');
     45$represented_albums = query2array($query, null, 'id');
    4646
    4747// +-----------------------------------------------------------------------+
     
    166166  }
    167167
    168   $no_longer_thumbnail_for = array_diff($represent_options_selected, $_POST['represent']);
     168  $no_longer_thumbnail_for = array_diff($represented_albums, $_POST['represent']);
    169169  if (count($no_longer_thumbnail_for) > 0)
    170170  {
     
    172172  }
    173173
    174   $new_thumbnail_for = array_diff($_POST['represent'], $represent_options_selected);
     174  $new_thumbnail_for = array_diff($_POST['represent'], $represented_albums);
    175175  if (count($new_thumbnail_for) > 0)
    176176  {
     
    183183  }
    184184
    185   $represent_options_selected = $_POST['represent'];
     185  $represented_albums = $_POST['represent'];
    186186
    187187  $page['infos'][] = l10n('Photo informations updated');
     
    407407  WHERE image_id = '.$_GET['image_id'].'
    408408;';
    409 $associate_options_selected = query2array($query, null, 'id');
     409$associated_albums = query2array($query, null, 'id');
    410410
    411411$template->assign(array(
    412   'associate_options_selected' => $associate_options_selected,
    413   'represent_options_selected' => $represent_options_selected,
     412  'associated_albums' => $associated_albums,
     413  'represented_albums' => $represented_albums,
     414  'STORAGE_ALBUM' => $storage_category_id,
    414415  'CACHE_KEYS' => get_admin_client_cache_keys(array('tags', 'categories')),
    415416  ));
  • trunk/admin/themes/default/js/LocalStorageCache.js

    r28540 r28542  
     1/**
     2 * Base LocalStorage cache
     3 *
     4 * @param options {object}
     5 *    - key (required) identifier of the collection
     6 *    - serverId (recommended) identifier of the Piwigo instance
     7 *    - serverKey (required) state of collection server-side
     8 *    - lifetime (optional) cache lifetime in seconds
     9 *    - loader (required) function called to fetch data, takes a callback as first argument
     10 *        which must be called with the loaded date
     11 */
    112var LocalStorageCache = function(options) {
     13  this._init(options);
     14};
     15
     16/*
     17 * Constructor (deported for easy inheritance)
     18 */
     19LocalStorageCache.prototype._init = function(options) {
    220  this.key = options.key + '_' + options.serverId;
    321  this.serverKey = options.serverKey;
     
    927};
    1028
     29/*
     30 * Get the cache content
     31 * @param callback {function} called with the data as first parameter
     32 */
    1133LocalStorageCache.prototype.get = function(callback) {
    1234  var now = new Date().getTime(),
     
    2850};
    2951
     52/*
     53 * Manually set the cache content
     54 * @param data {mixed}
     55 */
    3056LocalStorageCache.prototype.set = function(data) {
    3157  if (this.ready) {
     
    3864};
    3965
     66/*
     67 * Manually clear the cache
     68 */
    4069LocalStorageCache.prototype.clear = function() {
    4170  if (this.ready) {
     
    4372  }
    4473};
     74
     75
     76/**
     77 * Special LocalStorage for admin categories list
     78 *
     79 * @param options {object}
     80 *    - serverId (recommended) identifier of the Piwigo instance
     81 *    - serverKey (required) state of collection server-side
     82 *    - rootUrl (required) used for WS call
     83 */
     84var CategoriesCache = function(options) {
     85  options.key = 'categoriesAdminList';
     86 
     87  options.loader = function(callback) {
     88    jQuery.getJSON(options.rootUrl + 'ws.php?format=json&method=pwg.categories.getAdminList', function(data) {
     89      callback(data.result.categories);
     90    });
     91  };
     92 
     93  this._init(options);
     94};
     95
     96CategoriesCache.prototype = new LocalStorageCache({});
     97
     98/*
     99 * Init Selectize with cache content
     100 * @param $target {jQuery}
     101 * @param options {object}
     102 *    - default (optional) default value which will be forced if the select is emptyed
     103 *    - filter (optional) function called for each select before applying the data
     104 *      takes two parameters: cache data, options
     105 *      must return new data
     106 */
     107CategoriesCache.prototype.selectize = function($target, options) {
     108  options = options || {};
     109
     110  $target.selectize({
     111    valueField: 'id',
     112    labelField: 'fullname',
     113    sortField: 'global_rank',
     114    searchField: ['fullname'],
     115    plugins: ['remove_button']
     116  });
     117 
     118  this.get(function(categories) {
     119    $target.each(function() {
     120      var data;
     121      if (options.filter != undefined) {
     122        data = options.filter.call(this, categories, options);
     123      }
     124      else {
     125        data = categories;
     126      }
     127     
     128      this.selectize.load(function(callback) {
     129        callback(data);
     130      });
     131
     132      if (jQuery(this).data('value')) {
     133        jQuery.each(jQuery(this).data('value'), jQuery.proxy(function(i, id) {
     134          this.selectize.addItem(id);
     135        }, this));
     136      }
     137     
     138      if (options.default != undefined) {
     139        if (this.selectize.getValue() == '') {
     140          this.selectize.addItem(options.default);
     141        }
     142
     143        // if multiple: prevent item deletion
     144        if (this.multiple) {
     145          this.selectize.getItem(options.default).find('.remove').hide();
     146         
     147          this.selectize.on('item_remove', function(id) {
     148            if (id == options.default) {
     149              this.addItem(id);
     150              this.getItem(id).find('.remove').hide();
     151            }
     152          });
     153        }
     154        // if single: restore default on blur
     155        else {
     156          this.selectize.on('dropdown_close', function() {
     157            if (this.getValue() == '') {
     158              this.addItem(options.default);
     159            }
     160          });
     161        }
     162      }
     163    });
     164  });
     165};
  • trunk/admin/themes/default/template/batch_manager_global.tpl

    r28540 r28542  
    120120 
    121121  {* <!-- CATEGORIES --> *}
    122   var categoriesCache = new LocalStorageCache({
    123     key: 'categoriesAdminList',
     122  var categoriesCache = new CategoriesCache({
    124123    serverKey: '{$CACHE_KEYS.categories}',
    125124    serverId: '{$CACHE_KEYS._hash}',
    126 
    127     loader: function(callback) {
    128       jQuery.getJSON('{$ROOT_URL}ws.php?format=json&method=pwg.categories.getAdminList', function(data) {
    129         callback(data.result.categories);
    130       });
    131     }
     125    rootUrl: '{$ROOT_URL}'
    132126  });
    133127 
    134   jQuery('[data-selectize=categories]').selectize({
    135     valueField: 'id',
    136     labelField: 'fullname',
    137     sortField: 'global_rank',
    138     searchField: ['fullname'],
    139     plugins: ['remove_button']
    140   });
    141  
    142   categoriesCache.get(function(categories) {
    143     jQuery('[data-selectize=categories]').each(function() {
    144       this.selectize.load(function(callback) {
    145         callback(categories);
    146       });
    147 
    148       if (jQuery(this).data('value')) {
    149         this.selectize.setValue(jQuery(this).data('value')[0]);
     128  categoriesCache.selectize(jQuery('[data-selectize=categories]'), {
     129    filter: function(categories, options) {
     130      if (this.name == 'dissociate') {
     131        var filtered = jQuery.grep(categories, function(cat) {
     132          return !cat.dir;
     133        });
     134       
     135        if (filtered.length > 0) {
     136          jQuery('.albumDissociate').show();
     137          options.default = filtered[0].id;
     138        }
     139       
     140        return filtered;
    150141      }
    151      
    152       // prevent empty value
    153       if (this.selectize.getValue() == '') {
    154         this.selectize.setValue(categories[0].id);
     142      else {
     143        options.default = categories[0].id;
     144        return categories;
    155145      }
    156       this.selectize.on('dropdown_close', function() {
    157         if (this.getValue() == '') {
    158           this.setValue(categories[0].id);
    159         }
    160       });
    161     });
     146    }
    162147  });
    163148 
     
    838823      <option value="associate">{'Associate to album'|@translate}</option>
    839824      <option value="move">{'Move to album'|@translate}</option>
    840   {if !empty($dissociate_options)}
    841       <option value="dissociate">{'Dissociate from album'|@translate}</option>
    842   {/if}
     825      <option value="dissociate" class="albumDissociate" style="display:none">{'Dissociate from album'|@translate}</option>
    843826      <option value="add_tags">{'Add tags'|@translate}</option>
    844827  {if !empty($associated_tags)}
     
    885868
    886869    <!-- dissociate -->
    887     <div id="action_dissociate" class="bulkAction">
     870    <div id="action_dissociate" class="bulkAction albumDissociate" style="display:none">
    888871      <select data-selectize="categories" name="dissociate" style="width:400px"></select>
    889872    </div>
  • trunk/admin/themes/default/template/cat_modify.tpl

    r28540 r28542  
    66{footer_script}
    77{* <!-- CATEGORIES --> *}
    8 var categoriesCache = new LocalStorageCache({
    9   key: 'categoriesAdminList',
     8var categoriesCache = new CategoriesCache({
    109  serverKey: '{$CACHE_KEYS.categories}',
    1110  serverId: '{$CACHE_KEYS._hash}',
    12 
    13   loader: function(callback) {
    14     jQuery.getJSON('{$ROOT_URL}ws.php?format=json&method=pwg.categories.getAdminList', function(data) {
    15       callback(data.result.categories);
    16     });
    17   }
     11  rootUrl: '{$ROOT_URL}'
    1812});
    1913
    20 jQuery('[data-selectize=categories]').selectize({
    21   valueField: 'id',
    22   labelField: 'fullname',
    23   sortField: 'global_rank',
    24   searchField: ['fullname'],
    25   plugins: ['remove_button']
    26 });
    27 
    28 categoriesCache.get(function(categories) {
    29   categories.push({
    30     id: 0,
    31     fullname: '------------',
    32     global_rank: 0
    33   });
    34  
    35   // remove itself and children
    36   categories = jQuery.grep(categories, function(cat) {
    37     return !(/\b{$CAT_ID}\b/.test(cat.uppercats));
    38   });
    39  
    40   jQuery('[data-selectize=categories]').each(function() {
    41     this.selectize.load(function(callback) {
    42       callback(categories);
     14categoriesCache.selectize(jQuery('[data-selectize=categories]'), {
     15  default: 0,
     16  filter: function(categories, options) {
     17    // remove itself and children
     18    var filtered = jQuery.grep(categories, function(cat) {
     19      return !(/\b{$CAT_ID}\b/.test(cat.uppercats));
    4320    });
    44 
    45     if (jQuery(this).data('value')) {
    46       this.selectize.setValue(jQuery(this).data('value')[0]);
    47     }
    4821   
    49     // prevent empty value
    50     if (this.selectize.getValue() == '') {
    51       this.selectize.setValue(0);
    52     }
    53     this.selectize.on('dropdown_close', function() {
    54       if (this.getValue() == '') {
    55         this.setValue(0);
    56       }
     22    filtered.push({
     23      id: 0,
     24      fullname: '------------',
     25      global_rank: 0
    5726    });
    58   });
     27   
     28    return filtered;
     29  }
    5930});
    6031{/footer_script}
  • trunk/admin/themes/default/template/photos_add_direct.tpl

    r28540 r28542  
    1717{footer_script}
    1818{* <!-- CATEGORIES --> *}
    19 var categoriesCache = new LocalStorageCache({
    20   key: 'categoriesAdminList',
     19var categoriesCache = new CategoriesCache({
    2120  serverKey: '{$CACHE_KEYS.categories}',
    2221  serverId: '{$CACHE_KEYS._hash}',
    23 
    24   loader: function(callback) {
    25     jQuery.getJSON('{$ROOT_URL}ws.php?format=json&method=pwg.categories.getAdminList', function(data) {
    26       callback(data.result.categories);
    27     });
     22  rootUrl: '{$ROOT_URL}'
     23});
     24
     25categoriesCache.selectize(jQuery('[data-selectize=categories]'), {
     26  filter: function(categories, options) {
     27    if (categories.length > 0) {
     28      jQuery("#albumSelection").show();
     29      options.default = categories[0].id;
     30    }
     31   
     32    return categories;
    2833  }
    29 });
    30 
    31 jQuery('[data-selectize=categories]').selectize({
    32   valueField: 'id',
    33   labelField: 'fullname',
    34   sortField: 'global_rank',
    35   searchField: ['fullname'],
    36   plugins: ['remove_button']
    37 });
    38 
    39 categoriesCache.get(function(categories) {
    40   if (categories.length > 0) {
    41     jQuery("#albumSelection").show();
    42   }
    43  
    44   jQuery('[data-selectize=categories]').each(function() {
    45     this.selectize.load(function(callback) {
    46       callback(categories);
    47     });
    48 
    49     if (jQuery(this).data('value')) {
    50       this.selectize.setValue(jQuery(this).data('value')[0]);
    51     }
    52    
    53     // prevent empty value
    54     if (this.selectize.getValue() == '') {
    55       this.selectize.setValue(categories[0].id);
    56     }
    57     this.selectize.on('dropdown_close', function() {
    58       if (this.getValue() == '') {
    59         this.setValue(categories[0].id);
    60       }
    61     });
    62   });
    6334});
    6435
  • trunk/admin/themes/default/template/picture_modify.tpl

    r28540 r28542  
    1111(function(){
    1212{* <!-- CATEGORIES --> *}
    13 var categoriesCache = new LocalStorageCache({
    14   key: 'categoriesAdminList',
     13var categoriesCache = new CategoriesCache({
    1514  serverKey: '{$CACHE_KEYS.categories}',
    1615  serverId: '{$CACHE_KEYS._hash}',
    17 
    18   loader: function(callback) {
    19     jQuery.getJSON('{$ROOT_URL}ws.php?format=json&method=pwg.categories.getAdminList', function(data) {
    20       callback(data.result.categories);
    21     });
    22   }
     16  rootUrl: '{$ROOT_URL}'
    2317});
    2418
    25 jQuery('[data-selectize=categories]').selectize({
    26   valueField: 'id',
    27   labelField: 'fullname',
    28   sortField: 'global_rank',
    29   searchField: ['fullname'],
    30   plugins: ['remove_button']
    31 });
    32 
    33 categoriesCache.get(function(categories) {
    34   jQuery('[data-selectize=categories]').each(function() {
    35     this.selectize.load(function(callback) {
    36       callback(categories);
    37     });
    38 
    39     jQuery.each(jQuery(this).data('value'), jQuery.proxy(function(i, id) {
    40       this.selectize.addItem(id);
    41     }, this));
    42   });
    43 });
     19categoriesCache.selectize(jQuery('[data-selectize=categories]'), { {if $STORAGE_ALBUM}
     20  filter: function(categories, options) {
     21    options.default = (this.name == 'associate[]') ? {$STORAGE_ALBUM} : undefined;
     22    return categories;
     23  }
     24{/if} });
    4425
    4526{* <!-- TAGS --> *}
     
    158139      <strong>{'Linked albums'|@translate}</strong>
    159140      <br>
    160       <select data-selectize="categories" data-value="{$associate_options_selected|@json_encode|escape:html}"
     141      <select data-selectize="categories" data-value="{$associated_albums|@json_encode|escape:html}"
    161142        name="associate[]" multiple style="width:600px;" ></select>
    162143    </p>
     
    165146      <strong>{'Representation of albums'|@translate}</strong>
    166147      <br>
    167       <select data-selectize="categories" data-value="{$represent_options_selected|@json_encode|escape:html}"
     148      <select data-selectize="categories" data-value="{$represented_albums|@json_encode|escape:html}"
    168149        name="represent[]" multiple style="width:600px;" ></select>
    169150    </p>
  • trunk/include/ws_functions/pwg.categories.php

    r28494 r28542  
    490490
    491491  $query = '
    492 SELECT id, name, comment, uppercats, global_rank
     492SELECT id, name, comment, uppercats, global_rank, dir
    493493  FROM '. CATEGORIES_TABLE .'
    494494;';
Note: See TracChangeset for help on using the changeset viewer.