fixes #377 use Ajax calls to refresh/delete album thumbnail

This commit is contained in:
plegall 2015-12-01 12:08:10 +01:00
parent 27389102d1
commit be6afad731
6 changed files with 211 additions and 40 deletions

View file

@ -172,21 +172,6 @@ UPDATE '.CATEGORIES_TABLE.'
$_SESSION['page_infos'][] = l10n('Album updated successfully'); $_SESSION['page_infos'][] = l10n('Album updated successfully');
$redirect = true; $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)) if (isset($redirect))
{ {
@ -336,8 +321,7 @@ else
} }
// representant management // representant management
if ($category['has_images'] if ($category['has_images'] or !empty($category['representative_picture_id']))
or !empty($category['representative_picture_id']))
{ {
$tpl_representant = array(); $tpl_representant = array();
@ -345,20 +329,7 @@ if ($category['has_images']
// representant ? // representant ?
if (!empty($category['representative_picture_id'])) if (!empty($category['representative_picture_id']))
{ {
$query = ' $tpl_representant['picture'] = get_category_representant_properties($category['representative_picture_id']);
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
);
} }
// can the admin choose to set a new random representant ? // can the admin choose to set a new random representant ?

View file

@ -886,6 +886,25 @@ SELECT uppercats
return $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. * Set a new random representant to the categories.
* *

View file

@ -28,6 +28,67 @@ categoriesCache.selectize(jQuery('[data-selectize=categories]'), {
return filtered; 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} {/footer_script}
@ -35,7 +96,7 @@ categoriesCache.selectize(jQuery('[data-selectize=categories]'), {
<h2><span style="letter-spacing:0">{$CATEGORIES_NAV}</span> &#8250; {'Edit album'|@translate} {$TABSHEET_TITLE}</h2> <h2><span style="letter-spacing:0">{$CATEGORIES_NAV}</span> &#8250; {'Edit album'|@translate} {$TABSHEET_TITLE}</h2>
</div> </div>
<form action="{$F_ACTION}" method="POST" id="catModify"> <div id="catModify">
<fieldset> <fieldset>
<legend>{'Informations'|@translate}</legend> <legend>{'Informations'|@translate}</legend>
@ -44,19 +105,18 @@ categoriesCache.selectize(jQuery('[data-selectize=categories]'), {
<tr> <tr>
<td id="albumThumbnail"> <td id="albumThumbnail">
{if isset($representant) } {if isset($representant) }
{if isset($representant.picture) } <a class="albumThumbnailImage" style="{if !isset($representant.picture)}display:none{/if}" href="{$representant.picture.url}"><img src="{$representant.picture.src}"></a>
<a href="{$representant.picture.URL}"><img src="{$representant.picture.SRC}" alt=""></a> <img class="albumThumbnailRandom" style="{if isset($representant.picture)}display:none{/if}" src="{$ROOT_URL}{$themeconf.admin_icon_dir}/category_representant_random.png" alt="{'Random photo'|@translate}">
{else}
<img src="{$ROOT_URL}{$themeconf.admin_icon_dir}/category_representant_random.png" alt="{'Random photo'|@translate}">
{/if}
<p class="albumThumbnailActions">
{if $representant.ALLOW_SET_RANDOM } {if $representant.ALLOW_SET_RANDOM }
<p style="text-align:center;"><input class="submit" type="submit" name="set_random_representant" value="{'Refresh'|@translate}" title="{'Find a new representant by random'|@translate}"></p> <a href="#refresh" data-category_id="{$CAT_ID}" class="refreshRepresentative" title="{'Find a new representant by random'|@translate}">{'Refresh'|@translate}</a>
{/if} {/if}
{if isset($representant.ALLOW_DELETE) } {if isset($representant.ALLOW_DELETE) }
<p><input class="submit" type="submit" name="delete_representant" value="{'Delete Representant'|@translate}"></p> | <a href="#delete" data-category_id="{$CAT_ID}" class="deleteRepresentative" title="{'Delete Representant'|@translate}">{'Delete'|translate}</a>
{/if} {/if}
</p>
{/if} {/if}
</td> </td>
@ -90,6 +150,7 @@ categoriesCache.selectize(jQuery('[data-selectize=categories]'), {
</fieldset> </fieldset>
<form action="{$F_ACTION}" method="POST">
<fieldset> <fieldset>
<legend>{'Properties'|@translate}</legend> <legend>{'Properties'|@translate}</legend>
<p> <p>
@ -137,3 +198,4 @@ categoriesCache.selectize(jQuery('[data-selectize=categories]'), {
</fieldset> </fieldset>
</form> </form>
</div> {* #catModify *}

View file

@ -213,7 +213,7 @@ LI.menuLi {
} }
/* */ /* */
FORM#catModify TABLE { width: auto; } #catModify TABLE { width: auto; }
#catModify p { #catModify p {
line-height: 20px; line-height: 20px;
@ -221,6 +221,11 @@ FORM#catModify TABLE { width: auto; }
text-align: left; text-align: left;
} }
#catModify p.albumThumbnailActions {
text-align:center;
white-space:nowrap;
}
FIELDSET.elementEdit .thumb { FIELDSET.elementEdit .thumb {
display: block; display: block;
float: right; float: right;

View file

@ -661,6 +661,98 @@ UPDATE '. USER_CACHE_CATEGORIES_TABLE .'
pwg_query($query); 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 * API method
* Deletes a category * Deletes a category

22
ws.php
View file

@ -570,6 +570,28 @@ function ws_addDefaultMethods( $arr )
$ws_functions_root . 'pwg.categories.php', $ws_functions_root . 'pwg.categories.php',
array('admin_only'=>true, 'post_only'=>true) 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( $service->addMethod(
'pwg.tags.getAdminList', 'pwg.tags.getAdminList',