You are here

function imagepicker_admin_orphans_do in Image Picker 7

Same name and namespace in other branches
  1. 6.2 imagepicker.admin.inc \imagepicker_admin_orphans_do()
1 call to imagepicker_admin_orphans_do()
imagepicker_admin_orphans_form_submit in ./imagepicker.admin.inc

File

./imagepicker.admin.inc, line 1275
@author Bob Hutchinson http://drupal.org/user/52366 @copyright GNU GPL

Code

function imagepicker_admin_orphans_do($orphanid, $orphans_delete = FALSE, $reallocate_to_user = '') {
  $OK = FALSE;
  $message = '';
  $return = '';

  // check that the incoming user id is nonexistent
  $dir = imagepicker_get_files_directory();
  $orphanids = imagepicker_check_orphans($dir);

  // sanity check
  if (in_array($orphanid, $orphanids)) {

    // delete images
    if ($orphans_delete == 1) {

      // create 'phony' user object
      $user_obj = new stdClass();
      $user_obj->uid = $orphanid;

      // get all image ids belonging to orphanids
      $ct = 0;
      $result = db_query('SELECT img_id FROM {imagepicker} WHERE uid=:uid', array(
        ':uid' => $user_obj->uid,
      ));
      while ($row = $result
        ->fetchAssoc()) {
        _imagepicker_image_delete($row['img_id'], $user_obj, 'admin', TRUE);
        imagepicker_delete_group_image($row['img_id']);
        $ct++;
      }
      $OK = imagepicker_delete_olduser_dirs($user_obj->uid);
      if ($OK) {
        $message = t('!ct Files deleted.', array(
          '!ct' => $ct,
        ));
      }
      else {
        $message = t('There was an error in deleting files');
      }
    }
    elseif ($reallocate_to_user != '') {

      // reallocate to new user
      $result = db_query("SELECT uid FROM {users} WHERE name=:s", array(
        ':s' => trim($reallocate_to_user),
      ));
      if ($row = $result
        ->fetchAssoc()) {
        $new_uid = $row['uid'];
        $ct = 0;
        $result2 = db_query('SELECT img_id FROM {imagepicker} WHERE uid=:uid', array(
          ':uid' => $orphanid,
        ));
        while ($row2 = $result2
          ->fetchAssoc()) {
          $newf = imagepicker_reallocate_image($row2['img_id'], $orphanid, $new_uid);
          if ($newf) {

            // move this entry in imagepicker to $new_uid
            $img_name = basename($newf);
            db_update('imagepicker')
              ->fields(array(
              'uid' => $new_uid,
              'img_name' => $img_name,
            ))
              ->condition('img_id', $row2['img_id'])
              ->execute();
            $ct++;
          }
        }

        // give the new user the groups if any
        db_update('imagepicker_user_groups')
          ->fields(array(
          'uid' => $new_uid,
        ))
          ->condition('uid', $orphanid)
          ->execute();
        $OK = imagepicker_delete_olduser_dirs($orphanid);
        if ($OK) {
          $message = t('!ct Files moved.', array(
            '!ct' => $ct,
          ));
        }
        else {
          $message = t('There was an error in moving files.');
        }
      }
    }
    if ($message) {
      $return = array(
        $message,
        $OK,
      );
    }
  }
  return $return;
}