source: extensions/pwg_images_addSimple/main.inc.php @ 7479

Last change on this file since 7479 was 7275, checked in by plg, 14 years ago

improvement: pwg.images.addSimple returns image_id and a valid URL to show the uploaded photo

File size: 3.0 KB
Line 
1<?php
2/*
3Plugin Name: pwg.images.addSimple
4Version: auto
5Description: A simpler method to add photo with web API, based on HTTP file upload protocol
6Plugin URI: http://piwigo.org/ext/extension_view.php?eid=411
7Author: plg
8Author URI: http://piwigo.wordpress.com
9*/
10
11if (!defined('PHPWG_ROOT_PATH'))
12{
13  die('Hacking attempt!');
14}
15
16add_event_handler('ws_add_methods', 'ws_add_method_addSimple');
17
18function ws_add_method_addSimple($arr)
19{
20  global $conf;
21  $service = &$arr[0];
22 
23  $service->addMethod(
24    'pwg.images.addSimple',
25    'ws_images_addSimple',
26    array(
27      'category' => array('default' => null),
28      'name' => array('default' => null),
29      'author' => array('default' => null),
30      'comment' => array('default' => null),
31      'level' => array(
32        'default' => 0,
33        'maxValue' => $conf['available_permission_levels']
34        ),
35      'tags' => array('default' => null),
36      ),
37    'POST method only.<br>Use the <b>image</b> field for uploading file.<br>Set the form encoding to "form-data"<br><b>category</b> is the numeric identifier of the destination category.'
38    );
39}
40
41function ws_images_addSimple($params, &$service)
42{
43  global $conf;
44  if (!is_admin() || is_adviser() )
45  {
46    return new PwgError(401, 'Access denied');
47  }
48
49  if (!$service->isPost())
50  {
51    return new PwgError(405, "This method requires HTTP POST");
52  }
53
54  // category
55  $params['category'] = (int)$params['category'];
56  if ($params['category'] <= 0)
57  {
58    return new PwgError(WS_ERR_INVALID_PARAM, "Invalid category_id");
59  }
60
61  include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php');
62 
63  $image_id = add_uploaded_file(
64    $_FILES['image']['tmp_name'],
65    $_FILES['image']['name'],
66    array($params['category']),
67    8
68    );
69
70  $info_columns = array(
71    'name',
72    'author',
73    'comment',
74    'level',
75    'date_creation',
76    );
77
78  foreach ($info_columns as $key)
79  {
80    if (isset($params[$key]))
81    {
82      $update[$key] = $params[$key];
83    }
84  }
85
86  if (count(array_keys($update)) > 0)
87  {
88    $update['id'] = $image_id;
89
90    include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
91    mass_updates(
92      IMAGES_TABLE,
93      array(
94        'primary' => array('id'),
95        'update'  => array_diff(array_keys($update), array('id'))
96        ),
97      array($update)
98      );
99  }
100
101
102  if (isset($params['tags']) and !empty($params['tags']))
103  {
104    $tag_ids = array();
105    $tag_names = explode(',', $params['tags']);
106    foreach ($tag_names as $tag_name)
107    {
108      $tag_id = tag_id_from_tag_name($tag_name);
109      array_push($tag_ids, $tag_id);
110    }
111
112    add_tags($tag_ids, array($image_id));
113  }
114
115  $query = '
116SELECT id, name, permalink
117  FROM '.CATEGORIES_TABLE.'
118  WHERE id = '.$params['category'].'
119;';
120  $result = pwg_query($query);
121  $category = pwg_db_fetch_assoc($result);
122
123  return array(
124    'image_id' => $image_id,
125    'url' => make_picture_url(
126      array(
127        'image_id' => $image_id,
128        'section' => 'categories',
129        'category' => $category
130        )
131      ),
132    );
133}
134?>
Note: See TracBrowser for help on using the repository browser.