Skip to content

Commit

Permalink
improvement: WebService method pwg.images.add can set fill #images table
Browse files Browse the repository at this point in the history
columns. rank is directly related to a category and several categories can
be linked at once. Basic technical metadata {filesize, width, height} are
automaticaly filled.


git-svn-id: http://piwigo.org/svn/trunk@2569 68402e56-0260-453c-a942-63ccdbb3a9ee
  • Loading branch information
plegall committed Sep 22, 2008
1 parent d2d9f7a commit aa41a46
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 31 deletions.
123 changes: 104 additions & 19 deletions include/ws_functions.inc.php
Expand Up @@ -974,15 +974,36 @@ function ws_images_add($params, &$service)
// fwrite($fh_log, 'output: '.md5_file($file_path)."\n");
// fwrite($fh_log, 'output: '.md5_file($thumbnail_path)."\n");

list($width, $height) = getimagesize($file_path);

// database registration
$insert = array(
'file' => $filename_wo_ext.'.jpg',
'date_available' => $dbnow,
'tn_ext' => 'jpg',
'name' => $params['name'],
'path' => $file_path,
'filesize' => floor(filesize($file_path)/1024),
'width' => $width,
'height' => $height,
);

$info_columns = array(
'name',
'author',
'comment',
'level',
'date_creation',
);

foreach ($info_columns as $key)
{
if (isset($params[$key]))
{
$insert[$key] = $params[$key];
}
}

include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
mass_inserts(
IMAGES_TABLE,
Expand All @@ -992,34 +1013,99 @@ function ws_images_add($params, &$service)

$image_id = mysql_insert_id();

$insert = array(
'category_id' => $params['category_id'],
'image_id' => $image_id,
);
// let's add links between the image and the categories
//
// $params['categories'] should look like 123,12;456,auto;789 which means:
//
// 1. associate with category 123 on rank 12
// 2. associate with category 456 on automatic rank
// 3. associate with category 789 on automatic rank
if (isset($params['categories']))
{
$cat_ids = array();
$rank_on_category = array();
$search_current_ranks = false;

$tokens = explode(';', $params['categories']);
foreach ($tokens as $token)
{
list($cat_id, $rank) = explode(',', $token);

if ('auto' == $params['rank'])
{
$query = '
array_push($cat_ids, $cat_id);

if (!isset($rank))
{
$rank = 'auto';
}
$rank_on_category[$cat_id] = $rank;

if ($rank == 'auto')
{
$search_current_ranks = true;
}
}

$cat_ids = array_unique($cat_ids);

if (count($cat_ids) > 0)
{
if ($search_current_ranks)
{
$query = '
SELECT
category_id,
MAX(rank) AS max_rank
FROM '.IMAGE_CATEGORY_TABLE.'
WHERE rank IS NOT NULL
AND category_id = '.$params['category_id'].'
AND category_id IN ('.implode(',', $cat_ids).')
GROUP BY category_id
;';
$row = mysql_fetch_assoc(pwg_query($query));
$insert['rank'] = isset($row['max_rank']) ? $row['max_rank']+1 : 1;
$current_rank_of = simple_hash_from_query(
$query,
'category_id',
'max_rank'
);

foreach ($cat_ids as $cat_id)
{
if ('auto' == $rank_on_category[$cat_id])
{
$rank_on_category[$cat_id] = $current_rank_of[$cat_id] + 1;
}
}
}

$inserts = array();

foreach ($cat_ids as $cat_id)
{
array_push(
$inserts,
array(
'image_id' => $image_id,
'category_id' => $cat_id,
'rank' => $rank_on_category[$cat_id],
)
);
}

mass_inserts(
IMAGE_CATEGORY_TABLE,
array_keys($inserts[0]),
$inserts
);
}
}
else if (is_numeric($params['rank']))

// and now, let's create tag associations
if (isset($params['tag_ids']))
{
$insert['rank'] = (int)$params['rank'];
set_tags(
explode(',', $params['tag_ids']),
$image_id
);
}

mass_inserts(
IMAGE_CATEGORY_TABLE,
array_keys($insert),
array($insert)
);

invalidate_user_cache();

// fclose($fh_log);
Expand Down Expand Up @@ -1249,5 +1335,4 @@ function ws_tags_getImages($params, &$service)
)
);
}

?>
10 changes: 6 additions & 4 deletions tools/piwigo_remote.pl
Expand Up @@ -10,7 +10,7 @@
my %opt = ();
GetOptions(
\%opt,
qw/action=s file=s thumbnail=s category_id=i name=s rank=s/
qw/action=s file=s thumbnail=s categories=s define=s%/
);

our $ua = LWP::UserAgent->new;
Expand Down Expand Up @@ -59,11 +59,13 @@
file_content => $file_content,
thumbnail_sum => $thumbnail_sum,
thumbnail_content => $thumbnail_content,
category_id => $opt{category_id},
name => $opt{name},
rank => defined($opt{rank}) ? $opt{rank} : 'auto',
categories => $opt{categories},
};

foreach my $key (keys %{ $opt{define} }) {
$form->{$key} = $opt{define}{$key};
}

my $response = $ua->post(
$conf{base_url}.'/ws.php?format=json',
$form
Expand Down
24 changes: 16 additions & 8 deletions ws.php
Expand Up @@ -176,15 +176,23 @@ function ws_addDefaultMethods( $arr )
'pwg.images.add',
'ws_images_add',
array(
'name',
'category_id',
'file_content',
'file_sum',
'thumbnail_content',
'thumbnail_sum',
'rank',
'file_content' => array(),
'file_sum' => array(),
'thumbnail_content' => array(),
'thumbnail_sum' => array(),
'name' => array('default' => null),
'author' => array('default' => null),
'date_creation' => array('default' => null),
'comment' => array('default' => null),
'categories' => array('default' => null),
'tag_ids' => array('default' => null),
'level' => array(
'default' => 0,
'maxValue' => $conf['available_permission_levels']
),
),
'POST method only'
'POST method only.
<br/><b>categories</b> is a string list "category_id[,rank];category_id[,rank]" The rank is optional and is equivalent to "auto" if not given.'
);

$service->addMethod(
Expand Down

0 comments on commit aa41a46

Please sign in to comment.