Changeset 7533


Ignore:
Timestamp:
Nov 1, 2010, 2:01:09 AM (13 years ago)
Author:
plg
Message:

add methods pwg.categories.delete and pwg.images.delete (pwg_token required)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • extensions/pwg_images_addSimple/main.inc.php

    r7275 r7533  
    3737    '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.'
    3838    );
     39
     40  $service->addMethod(
     41    'pwg.images.delete',
     42    'ws_images_delete',
     43    array(
     44      'image_id'=>array('default'=>0),
     45      'pwg_token' => array('default' => null),
     46      ),
     47    'Delete photos. You can give several image_ids, comma separated'
     48    );
     49
     50  $service->addMethod(
     51    'pwg.categories.delete',
     52    'ws_categories_delete',
     53    array(
     54      'category_id'=>array('default'=>0),
     55      'pwg_token' => array('default' => null),
     56      ),
     57    'Delete categories. You can give several category_ids, comma separated'
     58    );
    3959}
    4060
     
    132152    );
    133153}
     154
     155function ws_images_delete($params, &$service)
     156{
     157  global $conf;
     158  if (!is_admin() || is_adviser() )
     159  {
     160    return new PwgError(401, 'Access denied');
     161  }
     162
     163  if (!$service->isPost())
     164  {
     165    return new PwgError(405, "This method requires HTTP POST");
     166  }
     167
     168  if (empty($params['pwg_token']) or get_pwg_token() != $params['pwg_token'])
     169  {
     170    return new PwgError(403, 'Invalid security token');
     171  }
     172
     173  $params['image_id'] = preg_split(
     174    '/[\s,;\|]/',
     175    $params['image_id'],
     176    -1,
     177    PREG_SPLIT_NO_EMPTY
     178    );
     179  $params['image_id'] = array_map('intval', $params['image_id']);
     180
     181  $image_ids = array();
     182  foreach ($params['image_id'] as $image_id)
     183  {
     184    if ($image_id > 0)
     185    {
     186      array_push($image_ids, $image_id);
     187    }
     188  }
     189
     190  include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
     191  delete_elements($params['image_id'], true);
     192}
     193
     194function ws_categories_delete($params, &$service)
     195{
     196  global $conf;
     197  if (!is_admin() || is_adviser() )
     198  {
     199    return new PwgError(401, 'Access denied');
     200  }
     201
     202  if (!$service->isPost())
     203  {
     204    return new PwgError(405, "This method requires HTTP POST");
     205  }
     206
     207  if (empty($params['pwg_token']) or get_pwg_token() != $params['pwg_token'])
     208  {
     209    return new PwgError(403, 'Invalid security token');
     210  }
     211
     212  $params['category_id'] = preg_split(
     213    '/[\s,;\|]/',
     214    $params['category_id'],
     215    -1,
     216    PREG_SPLIT_NO_EMPTY
     217    );
     218  $params['category_id'] = array_map('intval', $params['category_id']);
     219
     220  $category_ids = array();
     221  foreach ($params['category_id'] as $category_id)
     222  {
     223    if ($category_id > 0)
     224    {
     225      array_push($category_ids, $category_id);
     226    }
     227  }
     228
     229  // We don't want to create orphans. If a photo is belonging to a category
     230  // that will be deleted and to no other category, we must delete the photo
     231  // as well.
     232  //
     233  // In the future, this algorithm must be integrated into the
     234  // delete_categories function.
     235 
     236  if (count($category_ids) == 0)
     237  {
     238    return;
     239  }
     240
     241  // add sub-category ids to the given ids : if a category is deleted, all
     242  // sub-categories must be so
     243  $all_category_ids = get_subcat_ids($category_ids);
     244
     245  $query = '
     246SELECT
     247    DISTINCT(image_id)
     248  FROM '.IMAGE_CATEGORY_TABLE.'
     249  WHERE category_id IN ('.implode(',', $all_category_ids).')
     250;';
     251  $image_ids_linked = array_from_query($query, 'image_id');
     252
     253  $query = '
     254SELECT
     255    DISTINCT(image_id)
     256  FROM '.IMAGE_CATEGORY_TABLE.'
     257  WHERE image_id IN ('.implode(',', $image_ids_linked).')
     258    AND category_id NOT IN ('.implode(',', $all_category_ids).')
     259;';
     260  $image_ids_not_orphans = array_from_query($query, 'image_id');
     261
     262  $image_ids_orphans = array_diff($image_ids_linked, $image_ids_not_orphans);
     263 
     264  // print_r($image_ids_not_orphans); exit();
     265
     266  include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
     267  delete_categories($category_ids);
     268  update_global_rank();
     269
     270  delete_elements($image_ids_orphans, true);
     271}
    134272?>
Note: See TracChangeset for help on using the changeset viewer.