From be6afad731b965a75d9fe0b254ea72858a4deec4 Mon Sep 17 00:00:00 2001 From: plegall Date: Tue, 1 Dec 2015 12:08:10 +0100 Subject: fixes #377 use Ajax calls to refresh/delete album thumbnail --- admin/cat_modify.php | 33 +--------- admin/include/functions.php | 19 ++++++ admin/themes/default/template/cat_modify.tpl | 78 ++++++++++++++++++++--- admin/themes/default/theme.css | 7 ++- include/ws_functions/pwg.categories.php | 92 ++++++++++++++++++++++++++++ ws.php | 22 +++++++ 6 files changed, 211 insertions(+), 40 deletions(-) diff --git a/admin/cat_modify.php b/admin/cat_modify.php index 3cf8239c1..2b0f37833 100644 --- a/admin/cat_modify.php +++ b/admin/cat_modify.php @@ -172,21 +172,6 @@ UPDATE '.CATEGORIES_TABLE.' $_SESSION['page_infos'][] = l10n('Album updated successfully'); $redirect = true; } -elseif (isset($_POST['set_random_representant'])) -{ - set_random_representant(array($_GET['cat_id'])); - $redirect = true; -} -elseif (isset($_POST['delete_representant'])) -{ - $query = ' -UPDATE '.CATEGORIES_TABLE.' - SET representative_picture_id = NULL - WHERE id = '.$_GET['cat_id'].' -;'; - pwg_query($query); - $redirect = true; -} if (isset($redirect)) { @@ -336,8 +321,7 @@ else } // representant management -if ($category['has_images'] - or !empty($category['representative_picture_id'])) +if ($category['has_images'] or !empty($category['representative_picture_id'])) { $tpl_representant = array(); @@ -345,20 +329,7 @@ if ($category['has_images'] // representant ? if (!empty($category['representative_picture_id'])) { - $query = ' -SELECT id,representative_ext,path - FROM '.IMAGES_TABLE.' - WHERE id = '.$category['representative_picture_id'].' -;'; - $row = pwg_db_fetch_assoc(pwg_query($query)); - $src = DerivativeImage::thumb_url($row); - $url = get_root_url().'admin.php?page=photo-'.$category['representative_picture_id']; - - $tpl_representant['picture'] = - array( - 'SRC' => $src, - 'URL' => $url - ); + $tpl_representant['picture'] = get_category_representant_properties($category['representative_picture_id']); } // can the admin choose to set a new random representant ? diff --git a/admin/include/functions.php b/admin/include/functions.php index 5cd44b31d..fbbcd8c55 100644 --- a/admin/include/functions.php +++ b/admin/include/functions.php @@ -886,6 +886,25 @@ SELECT uppercats return $uppercats; } +/** + */ +function get_category_representant_properties($image_id) +{ + $query = ' +SELECT id,representative_ext,path + FROM '.IMAGES_TABLE.' + WHERE id = '.$image_id.' +;'; + $row = pwg_db_fetch_assoc(pwg_query($query)); + $src = DerivativeImage::thumb_url($row); + $url = get_root_url().'admin.php?page=photo-'.$image_id; + + return array( + 'src' => $src, + 'url' => $url + ); +} + /** * Set a new random representant to the categories. * diff --git a/admin/themes/default/template/cat_modify.tpl b/admin/themes/default/template/cat_modify.tpl index d71ad0528..af1fb5792 100644 --- a/admin/themes/default/template/cat_modify.tpl +++ b/admin/themes/default/template/cat_modify.tpl @@ -28,6 +28,67 @@ categoriesCache.selectize(jQuery('[data-selectize=categories]'), { return filtered; } }); + +jQuery(document).ready(function() { + jQuery(document).on('click', '.refreshRepresentative', function(e) { + var $this = jQuery(this); + var method = 'pwg.categories.refreshRepresentative'; + + jQuery.ajax({ + url: "ws.php?format=json&method="+method, + type:"POST", + data: { + category_id: $this.data("category_id") + }, + success:function(data) { + var data = jQuery.parseJSON(data); + if (data.stat == 'ok') { + jQuery(".albumThumbnailImage").attr('href', data.result.url); + jQuery(".albumThumbnailImage img").attr('src', data.result.src); + jQuery(".albumThumbnailImage").show(); + jQuery(".albumThumbnailRandom").hide(); + } + else { + alert("error on "+method); + } + }, + error:function(XMLHttpRequest, textStatus, errorThrows) { + alert("serious error on "+method); + } + }); + + e.preventDefault(); + }); + + jQuery(document).on('click', '.deleteRepresentative', function(e) { + var $this = jQuery(this); + var method = 'pwg.categories.deleteRepresentative'; + + jQuery.ajax({ + url: "ws.php?format=json&method="+method, + type:"POST", + data: { + category_id: $this.data("category_id") + }, + success:function(data) { + var data = jQuery.parseJSON(data); + if (data.stat == 'ok') { + jQuery(".albumThumbnailImage").hide(); + jQuery(".albumThumbnailRandom").show(); + } + else { + alert("error on "+method); + } + }, + error:function(XMLHttpRequest, textStatus, errorThrows) { + alert("serious error on "+method); + } + }); + + e.preventDefault(); + }); +}); + {/footer_script} @@ -35,7 +96,7 @@ categoriesCache.selectize(jQuery('[data-selectize=categories]'), {

{$CATEGORIES_NAV} › {'Edit album'|@translate} {$TABSHEET_TITLE}

-
+
{'Informations'|@translate} @@ -44,19 +105,18 @@ categoriesCache.selectize(jQuery('[data-selectize=categories]'), { {if isset($representant) } - {if isset($representant.picture) } - - {else} - {'Random photo'|@translate} - {/if} + + {'Random photo'|@translate} +

{if $representant.ALLOW_SET_RANDOM } -

+ {'Refresh'|@translate} {/if} {if isset($representant.ALLOW_DELETE) } -

+ | {'Delete'|translate} {/if} +

{/if} @@ -90,6 +150,7 @@ categoriesCache.selectize(jQuery('[data-selectize=categories]'), {
+
{'Properties'|@translate}

@@ -137,3 +198,4 @@ categoriesCache.selectize(jQuery('[data-selectize=categories]'), {

+
{* #catModify *} \ No newline at end of file diff --git a/admin/themes/default/theme.css b/admin/themes/default/theme.css index 4a151b9a4..509aff377 100644 --- a/admin/themes/default/theme.css +++ b/admin/themes/default/theme.css @@ -213,7 +213,7 @@ LI.menuLi { } /* */ -FORM#catModify TABLE { width: auto; } +#catModify TABLE { width: auto; } #catModify p { line-height: 20px; @@ -221,6 +221,11 @@ FORM#catModify TABLE { width: auto; } text-align: left; } +#catModify p.albumThumbnailActions { + text-align:center; + white-space:nowrap; +} + FIELDSET.elementEdit .thumb { display: block; float: right; diff --git a/include/ws_functions/pwg.categories.php b/include/ws_functions/pwg.categories.php index b2cfc2dd4..4fece14ec 100644 --- a/include/ws_functions/pwg.categories.php +++ b/include/ws_functions/pwg.categories.php @@ -661,6 +661,98 @@ UPDATE '. USER_CACHE_CATEGORIES_TABLE .' pwg_query($query); } +/** + * API method + * + * Deletes the album thumbnail. Only possible if + * $conf['allow_random_representative'] + * + * @param mixed[] $params + * @option int category_id + */ +function ws_categories_deleteRepresentative($params, &$service) +{ + global $conf; + + // does the category really exist? + $query = ' +SELECT COUNT(*) + FROM '. CATEGORIES_TABLE .' + WHERE id = '. $params['category_id'] .' +;'; + list($count) = pwg_db_fetch_row(pwg_query($query)); + if ($count == 0) + { + return new PwgError(404, 'category_id not found'); + } + + if (!$conf['allow_random_representative']) + { + return new PwgError(401, 'not permitted'); + } + + $query = ' +UPDATE '.CATEGORIES_TABLE.' + SET representative_picture_id = NULL + WHERE id = '.$params['category_id'].' +;'; + pwg_query($query); +} + +/** + * API method + * + * Find a new album thumbnail. + * + * @param mixed[] $params + * @option int category_id + */ +function ws_categories_refreshRepresentative($params, &$service) +{ + global $conf; + + // does the category really exist? + $query = ' +SELECT COUNT(*) + FROM '. CATEGORIES_TABLE .' + WHERE id = '. $params['category_id'] .' +;'; + list($count) = pwg_db_fetch_row(pwg_query($query)); + if ($count == 0) + { + return new PwgError(404, 'category_id not found'); + } + + $query = ' +SELECT + DISTINCT category_id + FROM '.IMAGE_CATEGORY_TABLE.' + WHERE category_id = '.$params['category_id'].' + LIMIT 1 +;'; + $result = pwg_query($query); + $has_images = pwg_db_num_rows($result) > 0 ? true : false; + + if (!$has_images) + { + return new PwgError(401, 'not permitted'); + } + + include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); + + set_random_representant(array($params['category_id'])); + + // return url of the new representative + $query = ' +SELECT * + FROM '.CATEGORIES_TABLE.' + WHERE id = '.$params['category_id'].' +;'; + $category = pwg_db_fetch_assoc(pwg_query($query)); + + return get_category_representant_properties($category['representative_picture_id']); +} + /** * API method * Deletes a category diff --git a/ws.php b/ws.php index 5e4500120..a3747cfe0 100644 --- a/ws.php +++ b/ws.php @@ -570,6 +570,28 @@ function ws_addDefaultMethods( $arr ) $ws_functions_root . 'pwg.categories.php', array('admin_only'=>true, 'post_only'=>true) ); + + $service->addMethod( + 'pwg.categories.deleteRepresentative', + 'ws_categories_deleteRepresentative', + array( + 'category_id' => array('type'=>WS_TYPE_ID), + ), + 'Deletes the album thumbnail. Only possible if $conf[\'allow_random_representative\']', + $ws_functions_root . 'pwg.categories.php', + array('admin_only'=>true, 'post_only'=>true) + ); + + $service->addMethod( + 'pwg.categories.refreshRepresentative', + 'ws_categories_refreshRepresentative', + array( + 'category_id' => array('type'=>WS_TYPE_ID), + ), + 'Find a new album thumbnail.', + $ws_functions_root . 'pwg.categories.php', + array('admin_only'=>true, 'post_only'=>true) + ); $service->addMethod( 'pwg.tags.getAdminList', -- cgit v1.2.3