Changeset 2919


Ignore:
Timestamp:
Dec 3, 2008, 11:55:17 PM (15 years ago)
Author:
plg
Message:

merge r2722 from branch 2.0 to trunk

feature 892 added: pwg.images.setInfo added so that once we have discovered
the photo was already in the database (thanks to pwg.images.exist), we can
only set the photo metadata.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/ws_functions.inc.php

    r2917 r2919  
    11241124
    11251125  // let's add links between the image and the categories
    1126   //
    1127   // $params['categories'] should look like 123,12;456,auto;789 which means:
    1128   //
    1129   // 1. associate with category 123 on rank 12
    1130   // 2. associate with category 456 on automatic rank
    1131   // 3. associate with category 789 on automatic rank
    11321126  if (isset($params['categories']))
    11331127  {
    1134     $cat_ids = array();
    1135     $rank_on_category = array();
    1136     $search_current_ranks = false;
    1137 
    1138     $tokens = explode(';', $params['categories']);
    1139     foreach ($tokens as $token)
    1140     {
    1141       list($cat_id, $rank) = explode(',', $token);
    1142 
    1143       array_push($cat_ids, $cat_id);
    1144 
    1145       if (!isset($rank))
    1146       {
    1147         $rank = 'auto';
    1148       }
    1149       $rank_on_category[$cat_id] = $rank;
    1150 
    1151       if ($rank == 'auto')
    1152       {
    1153         $search_current_ranks = true;
    1154       }
    1155     }
    1156 
    1157     $cat_ids = array_unique($cat_ids);
    1158 
    1159     if (count($cat_ids) > 0)
    1160     {
    1161       if ($search_current_ranks)
    1162       {
    1163         $query = '
    1164 SELECT
    1165     category_id,
    1166     MAX(rank) AS max_rank
    1167   FROM '.IMAGE_CATEGORY_TABLE.'
    1168   WHERE rank IS NOT NULL
    1169     AND category_id IN ('.implode(',', $cat_ids).')
    1170   GROUP BY category_id
    1171 ;';
    1172         $current_rank_of = simple_hash_from_query(
    1173           $query,
    1174           'category_id',
    1175           'max_rank'
    1176           );
    1177 
    1178         foreach ($cat_ids as $cat_id)
    1179         {
    1180           if ('auto' == $rank_on_category[$cat_id])
    1181           {
    1182             $rank_on_category[$cat_id] = $current_rank_of[$cat_id] + 1;
    1183           }
    1184         }
    1185       }
    1186 
    1187       $inserts = array();
    1188 
    1189       foreach ($cat_ids as $cat_id)
    1190       {
    1191         array_push(
    1192           $inserts,
    1193           array(
    1194             'image_id' => $image_id,
    1195             'category_id' => $cat_id,
    1196             'rank' => $rank_on_category[$cat_id],
    1197             )
    1198           );
    1199       }
    1200 
    1201       mass_inserts(
    1202         IMAGE_CATEGORY_TABLE,
    1203         array_keys($inserts[0]),
    1204         $inserts
    1205         );
    1206 
    1207       update_category($cat_ids);
    1208     }
     1128    ws_add_image_category_relations($image_id, $params['categories']);
    12091129  }
    12101130
     
    15481468  return $result;
    15491469}
     1470
     1471function ws_images_setInfo($params, &$service)
     1472{
     1473  global $conf;
     1474  if (!is_admin() || is_adviser() )
     1475  {
     1476    return new PwgError(401, 'Access denied');
     1477  }
     1478
     1479  // name
     1480  // category_id
     1481  // file_content
     1482  // file_sum
     1483  // thumbnail_content
     1484  // thumbnail_sum
     1485 
     1486  $params['image_id'] = (int)$params['image_id'];
     1487  if ($params['image_id'] <= 0)
     1488  {
     1489    return new PwgError(WS_ERR_INVALID_PARAM, "Invalid image_id");
     1490  }
     1491
     1492  $query='
     1493SELECT *
     1494  FROM '.IMAGES_TABLE.'
     1495  WHERE id = '.$params['image_id'].'
     1496;';
     1497
     1498  $image_row = mysql_fetch_assoc(pwg_query($query));
     1499  if ($image_row == null)
     1500  {
     1501    return new PwgError(404, "image_id not found");
     1502  }
     1503
     1504  // database registration
     1505  $update = array(
     1506    'id' => $params['image_id'],
     1507    );
     1508
     1509  $info_columns = array(
     1510    'name',
     1511    'author',
     1512    'comment',
     1513    'level',
     1514    'date_creation',
     1515    );
     1516
     1517  $perform_update = false;
     1518  foreach ($info_columns as $key)
     1519  {
     1520    if (isset($params[$key]))
     1521    {
     1522      $perform_update = true;
     1523      $update[$key] = $params[$key];
     1524    }
     1525  }
     1526
     1527  if ($perform_update)
     1528  {
     1529    include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
     1530    mass_updates(
     1531      IMAGES_TABLE,
     1532      array(
     1533        'primary' => array('id'),
     1534        'update'  => array_diff(array_keys($update), array('id'))
     1535        ),
     1536      array($update)
     1537      );
     1538  }
     1539 
     1540  if (isset($params['categories']))
     1541  {
     1542    ws_add_image_category_relations(
     1543      $params['image_id'],
     1544      $params['categories']
     1545      );
     1546  }
     1547
     1548  // and now, let's create tag associations
     1549  if (isset($params['tag_ids']))
     1550  {
     1551    include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
     1552    add_tags(
     1553      explode(',', $params['tag_ids']),
     1554      array($params['image_id'])
     1555      );
     1556  }
     1557
     1558  invalidate_user_cache();
     1559}
     1560
     1561function ws_add_image_category_relations($image_id, $categories_string)
     1562{
     1563  // let's add links between the image and the categories
     1564  //
     1565  // $params['categories'] should look like 123,12;456,auto;789 which means:
     1566  //
     1567  // 1. associate with category 123 on rank 12
     1568  // 2. associate with category 456 on automatic rank
     1569  // 3. associate with category 789 on automatic rank
     1570  $cat_ids = array();
     1571  $rank_on_category = array();
     1572  $search_current_ranks = false;
     1573
     1574  $tokens = explode(';', $categories_string);
     1575  foreach ($tokens as $token)
     1576  {
     1577    list($cat_id, $rank) = explode(',', $token);
     1578
     1579    array_push($cat_ids, $cat_id);
     1580
     1581    if (!isset($rank))
     1582    {
     1583      $rank = 'auto';
     1584    }
     1585    $rank_on_category[$cat_id] = $rank;
     1586
     1587    if ($rank == 'auto')
     1588    {
     1589      $search_current_ranks = true;
     1590    }
     1591  }
     1592
     1593  $cat_ids = array_unique($cat_ids);
     1594
     1595  if (count($cat_ids) > 0)
     1596  {
     1597    if ($search_current_ranks)
     1598    {
     1599      $query = '
     1600SELECT
     1601    category_id,
     1602    MAX(rank) AS max_rank
     1603  FROM '.IMAGE_CATEGORY_TABLE.'
     1604  WHERE rank IS NOT NULL
     1605    AND category_id IN ('.implode(',', $cat_ids).')
     1606  GROUP BY category_id
     1607;';
     1608      $current_rank_of = simple_hash_from_query(
     1609        $query,
     1610        'category_id',
     1611        'max_rank'
     1612        );
     1613
     1614      foreach ($cat_ids as $cat_id)
     1615      {
     1616        if ('auto' == $rank_on_category[$cat_id])
     1617        {
     1618          $rank_on_category[$cat_id] = $current_rank_of[$cat_id] + 1;
     1619        }
     1620      }
     1621    }
     1622
     1623    $inserts = array();
     1624
     1625    foreach ($cat_ids as $cat_id)
     1626    {
     1627      array_push(
     1628        $inserts,
     1629        array(
     1630          'image_id' => $image_id,
     1631          'category_id' => $cat_id,
     1632          'rank' => $rank_on_category[$cat_id],
     1633          )
     1634        );
     1635    }
     1636
     1637    include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
     1638    mass_inserts(
     1639      IMAGE_CATEGORY_TABLE,
     1640      array_keys($inserts[0]),
     1641      $inserts
     1642      );
     1643
     1644    update_category($cat_ids);
     1645  }
     1646}
    15501647?>
  • trunk/tools/piwigo_remote.pl

    r2683 r2919  
    193193}
    194194
     195if ($opt{action} eq 'pwg.images.setInfo') {
     196    $form = {
     197        method => $opt{action},
     198    };
     199
     200    foreach my $key (keys %{ $opt{define} }) {
     201        $form->{$key} = $opt{define}{$key};
     202    }
     203
     204    my $response = $ua->post(
     205        $conf{base_url}.'/ws.php?format=json',
     206        $form
     207    );
     208
     209    use Data::Dumper;
     210    # print Dumper(from_json($response->content)->{result});
     211    print Dumper($response);
     212}
     213
    195214$query = pwg_ws_get_query(
    196215    method => 'pwg.session.logout'
  • trunk/ws.php

    r2683 r2919  
    239239    'check existence of a photo list'
    240240    );
     241
     242  $service->addMethod(
     243    'pwg.images.setInfo',
     244    'ws_images_setInfo',
     245    array(
     246      'image_id' => array(),
     247     
     248      'name' => array('default' => null),
     249      'author' => array('default' => null),
     250      'date_creation' => array('default' => null),
     251      'comment' => array('default' => null),
     252      'categories' => array('default' => null),
     253      'tag_ids' => array('default' => null),
     254      'level' => array(
     255        'default' => 0,
     256        'maxValue' => $conf['available_permission_levels']
     257        ),
     258      ),
     259    'POST method only. Admin only
     260<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.'
     261    );
    241262}
    242263
Note: See TracChangeset for help on using the changeset viewer.