Changeset 9191


Ignore:
Timestamp:
Feb 11, 2011, 11:57:23 PM (9 years ago)
Author:
plg
Message:

feature 2189 added: ability to update a photo (the JPEG/PNG file) with
pwg.images.addSimple, simply by adding an $image_id as argument.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/admin/include/functions.php

    r8967 r9191  
    167167}
    168168
    169 // The function delete_elements deletes the elements identified by the
    170 // (numeric) values of the array $ids. It also deletes (in the database) :
    171 //    - all the comments related to elements
    172 //    - all the links between categories and elements
    173 //    - all the favorites associated to elements
    174 // @return number of deleted elements
    175 function delete_elements($ids, $physical_deletion=false)
     169// Deletes all files (on disk) related to given image ids
     170// @return image ids where files are deleted successfully
     171function delete_element_files($ids)
    176172{
    177173  if (count($ids) == 0)
     
    179175    return 0;
    180176  }
    181   trigger_action('begin_delete_elements', $ids);
    182 
    183   if ($physical_deletion)
    184   {
    185     include_once(PHPWG_ROOT_PATH.'include/functions_picture.inc.php');
    186     $new_ids=array();
    187 
    188     $query = '
     177
     178  include_once(PHPWG_ROOT_PATH.'include/functions_picture.inc.php');
     179 
     180  $new_ids = array();
     181
     182  $query = '
    189183SELECT
    190184    id,
     
    196190  WHERE id IN ('.implode(',', $ids).')
    197191;';
    198     $result = pwg_query($query);
    199     while ($row = pwg_db_fetch_assoc($result))
    200     {
    201       if (url_is_remote($row['path']))
    202         continue;
    203       $files = array();
    204       $files[] = get_element_path($row);
    205       if (!empty($row['tn_ext']))
    206         $files[] = get_thumbnail_path($row);
    207       if (!empty($row['has_high']) and get_boolean($row['has_high']))
    208         $files[] = get_high_path($row);
    209       if (!empty($row['representative_ext']))
     192  $result = pwg_query($query);
     193  while ($row = pwg_db_fetch_assoc($result))
     194  {
     195    if (url_is_remote($row['path']))
     196    {
     197      continue;
     198    }
     199   
     200    $files = array();
     201    $files[] = get_element_path($row);
     202
     203    if (!empty($row['tn_ext']))
     204    {
     205      $files[] = get_thumbnail_path($row);
     206    }
     207   
     208    if (!empty($row['has_high']) and get_boolean($row['has_high']))
     209    {
     210      $files[] = get_high_path($row);
     211    }
     212   
     213    if (!empty($row['representative_ext']))
     214    {
     215      $pi = pathinfo($row['path']);
     216      $file_wo_ext = get_filename_wo_extension($pi['basename']);
     217      $files[] = PHPWG_ROOT_PATH.$pi['dirname'].'/pwg_representative/'.$file_wo_ext.'.'.$row['representative_ext'];
     218    }
     219
     220    $ok = true;
     221    foreach ($files as $path)
     222    {
     223      if (is_file($path) and !unlink($path))
    210224      {
    211         $pi = pathinfo($row['path']);
    212         $file_wo_ext = get_filename_wo_extension($pi['basename']);
    213         $files[] = PHPWG_ROOT_PATH.$pi['dirname'].'/pwg_representative/'.$file_wo_ext.'.'.$element_info['representative_ext'];
     225        $ok = false;
     226        trigger_error('"'.$path.'" cannot be removed', E_USER_WARNING);
     227        break;
    214228      }
    215 
    216       $ok = true;
    217       foreach ($files as $path)
    218       {
    219         if (is_file($path) and !unlink($path))
    220         {
    221           $ok = false;
    222           trigger_error('"'.$path.'" cannot be removed', E_USER_WARNING);
    223           break;
    224         }
    225       }
    226       if ($ok)
    227         $new_ids[] += $row['id'];
    228       else
    229         break;
    230     }
    231     $ids = $new_ids;
     229    }
     230   
     231    if ($ok)
     232    {
     233      $new_ids[] += $row['id'];
     234    }
     235    else
     236    {
     237      break;
     238    }
     239  }
     240  return $new_ids;
     241}
     242
     243// The function delete_elements deletes the elements identified by the
     244// (numeric) values of the array $ids. It also deletes (in the database) :
     245//    - all the comments related to elements
     246//    - all the links between categories and elements
     247//    - all the favorites associated to elements
     248// @return number of deleted elements
     249function delete_elements($ids, $physical_deletion=false)
     250{
     251  if (count($ids) == 0)
     252  {
     253    return 0;
     254  }
     255  trigger_action('begin_delete_elements', $ids);
     256
     257  if ($physical_deletion)
     258  {
     259    $ids = delete_element_files($ids);
    232260    if (count($ids)==0)
    233261    {
  • trunk/admin/include/functions_upload.inc.php

    r9022 r9191  
    169169}
    170170
    171 function add_uploaded_file($source_filepath, $original_filename=null, $categories=null, $level=null)
     171function add_uploaded_file($source_filepath, $original_filename=null, $categories=null, $level=null, $image_id=null)
    172172{
    173173  // Here is the plan
     
    186186 
    187187  global $conf, $user;
    188  
    189   // current date
    190   list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
    191   list($year, $month, $day) = preg_split('/[^\d]/', $dbnow, 4);
    192  
    193   // upload directory hierarchy
    194   $upload_dir = sprintf(
    195     PHPWG_ROOT_PATH.$conf['upload_dir'].'/%s/%s/%s',
    196     $year,
    197     $month,
    198     $day
    199     );
    200 
    201   // compute file path
     188
    202189  $md5sum = md5_file($source_filepath);
    203   $date_string = preg_replace('/[^\d]/', '', $dbnow);
    204   $random_string = substr($md5sum, 0, 8);
    205   $filename_wo_ext = $date_string.'-'.$random_string;
    206   $file_path = $upload_dir.'/'.$filename_wo_ext.'.';
    207 
    208   list($width, $height, $type) = getimagesize($source_filepath);
    209   if (IMAGETYPE_PNG == $type)
    210   {
    211     $file_path.= 'png';
     190  $file_path = null;
     191 
     192  if (isset($image_id))
     193  {
     194    // we are performing an update
     195    $query = '
     196SELECT
     197    path
     198  FROM '.IMAGES_TABLE.'
     199  WHERE id = '.$image_id.'
     200;';
     201    $result = pwg_query($query);
     202    while ($row = pwg_db_fetch_assoc($result))
     203    {
     204      $file_path = $row['path'];
     205    }
     206   
     207    if (!isset($file_path))
     208    {
     209      die('['.__FUNCTION__.'] this photo does not exist in the database');
     210    }
     211
     212    // delete all physical files related to the photo (thumbnail, web site, HD)
     213    delete_element_files(array($image_id));
    212214  }
    213215  else
    214216  {
    215     $file_path.= 'jpg';
    216   }
    217 
    218   prepare_directory($upload_dir);
     217    // this photo is new
     218   
     219    // current date
     220    list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
     221    list($year, $month, $day) = preg_split('/[^\d]/', $dbnow, 4);
     222 
     223    // upload directory hierarchy
     224    $upload_dir = sprintf(
     225      PHPWG_ROOT_PATH.$conf['upload_dir'].'/%s/%s/%s',
     226      $year,
     227      $month,
     228      $day
     229      );
     230
     231    // compute file path
     232    $date_string = preg_replace('/[^\d]/', '', $dbnow);
     233    $random_string = substr($md5sum, 0, 8);
     234    $filename_wo_ext = $date_string.'-'.$random_string;
     235    $file_path = $upload_dir.'/'.$filename_wo_ext.'.';
     236
     237    list($width, $height, $type) = getimagesize($source_filepath);
     238    if (IMAGETYPE_PNG == $type)
     239    {
     240      $file_path.= 'png';
     241    }
     242    else
     243    {
     244      $file_path.= 'jpg';
     245    }
     246
     247    prepare_directory($upload_dir);
     248  }
     249
    219250  if (is_uploaded_file($source_filepath))
    220251  {
     
    297328  $thumb_infos = pwg_image_infos($thumb_path);
    298329
    299   // database registration
    300   $insert = array(
    301     'file' => pwg_db_real_escape_string(isset($original_filename) ? $original_filename : basename($file_path)),
    302     'date_available' => $dbnow,
    303     'tn_ext' => 'jpg',
    304     'path' => preg_replace('#^'.preg_quote(PHPWG_ROOT_PATH).'#', '', $file_path),
    305     'filesize' => $file_infos['filesize'],
    306     'width' => $file_infos['width'],
    307     'height' => $file_infos['height'],
    308     'md5sum' => $md5sum,
    309     'added_by' => $user['id'],
    310     );
    311 
    312   if (isset($high_infos))
    313   {
    314     $insert['has_high'] = 'true';
    315     $insert['high_filesize'] = $high_infos['filesize'];
    316   }
    317 
    318   if (isset($level))
    319   {
    320     $insert['level'] = $level;
    321   }
    322  
    323   mass_inserts(
    324     IMAGES_TABLE,
    325     array_keys($insert),
    326     array($insert)
    327     );
    328  
    329   $image_id = pwg_db_insert_id(IMAGES_TABLE);
     330  if (isset($image_id))
     331  {
     332    $update = array(
     333      'id' => $image_id,
     334      'file' => pwg_db_real_escape_string(isset($original_filename) ? $original_filename : basename($file_path)),
     335      'filesize' => $file_infos['filesize'],
     336      'width' => $file_infos['width'],
     337      'height' => $file_infos['height'],
     338      'md5sum' => $md5sum,
     339      'added_by' => $user['id'],
     340      );
     341   
     342    if (isset($high_infos))
     343    {
     344      $update['has_high'] = 'true';
     345      $update['high_filesize'] = $high_infos['filesize'];
     346    }
     347    else
     348    {
     349      $update['has_high'] = 'false';
     350      $update['high_filesize'] = null;
     351    }
     352
     353    if (isset($level))
     354    {
     355      $update['level'] = $level;
     356    }
     357
     358    mass_updates(
     359      IMAGES_TABLE,
     360      array(
     361        'primary' => array('id'),
     362        'update' => array_keys($update)
     363        ),
     364      array($update)
     365      );
     366  }
     367  else
     368  {
     369    // database registration
     370    $insert = array(
     371      'file' => pwg_db_real_escape_string(isset($original_filename) ? $original_filename : basename($file_path)),
     372      'date_available' => $dbnow,
     373      'tn_ext' => 'jpg',
     374      'path' => preg_replace('#^'.preg_quote(PHPWG_ROOT_PATH).'#', '', $file_path),
     375      'filesize' => $file_infos['filesize'],
     376      'width' => $file_infos['width'],
     377      'height' => $file_infos['height'],
     378      'md5sum' => $md5sum,
     379      'added_by' => $user['id'],
     380      );
     381
     382    if (isset($high_infos))
     383    {
     384      $insert['has_high'] = 'true';
     385      $insert['high_filesize'] = $high_infos['filesize'];
     386    }
     387
     388    if (isset($level))
     389    {
     390      $insert['level'] = $level;
     391    }
     392 
     393    mass_inserts(
     394      IMAGES_TABLE,
     395      array_keys($insert),
     396      array($insert)
     397      );
     398 
     399    $image_id = pwg_db_insert_id(IMAGES_TABLE);
     400  }
    330401
    331402  if (isset($categories) and count($categories) > 0)
     
    343414  }
    344415  update_metadata(array($image_id=>$file_path));
    345  
     416
    346417  invalidate_user_cache();
    347418
  • trunk/include/ws_functions.inc.php

    r8728 r9191  
    13531353    return new PwgError(405, "This method requires HTTP POST");
    13541354  }
     1355 
     1356  $params['image_id'] = (int)$params['image_id'];
     1357  if ($params['image_id'] > 0)
     1358  {
     1359    include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
     1360
     1361    $query='
     1362SELECT *
     1363  FROM '.IMAGES_TABLE.'
     1364  WHERE id = '.$params['image_id'].'
     1365;';
     1366
     1367    $image_row = pwg_db_fetch_assoc(pwg_query($query));
     1368    if ($image_row == null)
     1369    {
     1370      return new PwgError(404, "image_id not found");
     1371    }
     1372  }
    13551373
    13561374  // category
    13571375  $params['category'] = (int)$params['category'];
    1358   if ($params['category'] <= 0)
     1376  if ($params['category'] <= 0 and $params['image_id'] <= 0)
    13591377  {
    13601378    return new PwgError(WS_ERR_INVALID_PARAM, "Invalid category_id");
     
    13671385    $_FILES['image']['tmp_name'],
    13681386    $_FILES['image']['name'],
    1369     array($params['category']),
    1370     8
     1387    $params['category'] > 0 ? array($params['category']) : null,
     1388    8,
     1389    $params['image_id'] > 0 ? $params['image_id'] : null
    13711390    );
    13721391
     
    14161435  }
    14171436
    1418   $query = '
     1437  $url_params = array('image_id' => $image_id);
     1438
     1439  if ($params['category'] > 0)
     1440  {
     1441    $query = '
    14191442SELECT id, name, permalink
    14201443  FROM '.CATEGORIES_TABLE.'
    14211444  WHERE id = '.$params['category'].'
    14221445;';
    1423   $result = pwg_query($query);
    1424   $category = pwg_db_fetch_assoc($result);
     1446    $result = pwg_query($query);
     1447    $category = pwg_db_fetch_assoc($result);
     1448
     1449    $url_params['section'] = 'categories';
     1450    $url_params['category'] = $category;
     1451  }
    14251452
    14261453  return array(
    14271454    'image_id' => $image_id,
    1428     'url' => make_picture_url(
    1429       array(
    1430         'image_id' => $image_id,
    1431         'section' => 'categories',
    1432         'category' => $category
    1433         )
    1434       ),
     1455    'url' => make_picture_url($url_params),
    14351456    );
    14361457}
  • trunk/ws.php

    r8728 r9191  
    234234        ),
    235235      'tags' => array('default' => null),
    236       ),
    237     '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.'
     236      'image_id' => array('default' => null),
     237      ),
     238    '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.<br>You can update an existing photo if you define an existing image_id.'
    238239    );
    239240
Note: See TracChangeset for help on using the changeset viewer.